blob: 15400a80e4a0b592bc68a5ba512858dea567f294 [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
Svetoslav7008b512015-06-24 18:47:07 -0700241 // Activity actions an app cannot start if it uses a permission which is not granted.
242 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
243 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700244
Svetoslav7008b512015-06-24 18:47:07 -0700245 static {
246 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
247 Manifest.permission.CAMERA);
248 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
249 Manifest.permission.CAMERA);
250 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
251 Manifest.permission.CALL_PHONE);
252 }
253
Svet Ganov99b60432015-06-27 13:15:22 -0700254 /** Action restriction: launching the activity is not restricted. */
255 private static final int ACTIVITY_RESTRICTION_NONE = 0;
256 /** Action restriction: launching the activity is restricted by a permission. */
257 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
258 /** Action restriction: launching the activity is restricted by an app op. */
259 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700260
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700261 // The height/width divide used when fitting a task within a bounds with method
262 // {@link #fitWithinBounds}.
263 // We always want the task to to be visible in the bounds without affecting its size when
264 // fitting. To make sure this is the case, we don't adjust the task left or top side pass
265 // the input bounds right or bottom side minus the width or height divided by this value.
266 private static final int FIT_WITHIN_BOUNDS_DIVIDER = 3;
267
justinzhang5286d3f2014-05-12 17:06:01 -0400268 /** Status Bar Service **/
269 private IBinder mToken = new Binder();
270 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400271 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400272
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700273 // For debugging to make sure the caller when acquiring/releasing our
274 // wake lock is the system process.
275 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800276 /** The number of distinct task ids that can be assigned to the tasks of a single user */
277 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700278
Craig Mautner27084302013-03-25 08:05:25 -0700279 final ActivityManagerService mService;
280
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800281 private RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800282
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700283 final ActivityStackSupervisorHandler mHandler;
284
285 /** Short cut */
286 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800287 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700288
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700289 /** Counter for next free stack ID to use for dynamic activity stacks. */
290 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700291
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800292 /**
293 * Maps the task identifier that activities are currently being started in to the userId of the
294 * task. Each time a new task is created, the entry for the userId of the task is incremented
295 */
296 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700297
Craig Mautner2420ead2013-04-01 17:13:20 -0700298 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800299 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700300
Craig Mautnere0a38842013-12-16 16:14:02 -0800301 /** The stack containing the launcher app. Assumed to always be attached to
302 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800303 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700304
Craig Mautnere0a38842013-12-16 16:14:02 -0800305 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800306 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700307
Craig Mautner4a1cb222013-12-04 16:14:06 -0800308 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
309 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800310 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800311 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700312
313 /** List of activities that are waiting for a new activity to become visible before completing
314 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800315 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700316
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700317 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800318 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700319
320 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800321 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700322
Craig Mautnerde4ef022013-04-07 19:01:33 -0700323 /** List of activities that are ready to be stopped, but waiting for the next activity to
324 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800325 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700326
Craig Mautnerf3333272013-04-22 10:55:53 -0700327 /** List of activities that are ready to be finished, but waiting for the previous activity to
328 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800329 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700330
Craig Mautner0eea92c2013-05-16 13:35:39 -0700331 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800332 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700333
Wale Ogunwale22e25262016-02-01 10:32:02 -0800334 /** List of activities whose multi-window mode changed that we need to report to the
335 * application */
336 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
337
338 /** List of activities whose picture-in-picture mode changed that we need to report to the
339 * application */
340 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
341
Craig Mautnerf3333272013-04-22 10:55:53 -0700342 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700343 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700344
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700345 /** Used to queue up any background users being started */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700346 final ArrayList<UserState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -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
435 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700436 * Description of a request to start a new activity, which has been held
437 * due to app switches being disabled.
438 */
439 static class PendingActivityLaunch {
440 final ActivityRecord r;
441 final ActivityRecord sourceRecord;
442 final int startFlags;
443 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700444 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700445
446 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700447 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700448 r = _r;
449 sourceRecord = _sourceRecord;
450 startFlags = _startFlags;
451 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700452 callerApp = _callerApp;
453 }
454
455 void sendErrorResult(String message) {
456 try {
457 if (callerApp.thread != null) {
458 callerApp.thread.scheduleCrash(message);
459 }
460 } catch (RemoteException e) {
461 Slog.e(TAG, "Exception scheduling crash of failed "
462 + "activity launcher sourceRecord=" + sourceRecord, e);
463 }
Craig Mautneree36c772014-07-16 14:56:05 -0700464 }
465 }
466
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800467 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700468 mService = service;
Jeff Brown2c43c332014-06-12 22:38:59 -0700469 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800470 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggidc249c42015-12-15 14:57:31 -0800471 mResizeDockedStackTimeout = new ResizeDockedStackTimeout(service, this, mHandler);
Jeff Brown2c43c332014-06-12 22:38:59 -0700472 }
473
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800474 void setRecentTasks(RecentTasks recentTasks) {
475 mRecentTasks = recentTasks;
476 }
477
Jeff Brown2c43c332014-06-12 22:38:59 -0700478 /**
479 * At the time when the constructor runs, the power manager has not yet been
480 * initialized. So we initialize our wakelocks afterwards.
481 */
482 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800483 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700484 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700485 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700486 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700487 }
488
justinzhang5286d3f2014-05-12 17:06:01 -0400489 // This function returns a IStatusBarService. The value is from ServiceManager.
490 // getService and is cached.
491 private IStatusBarService getStatusBarService() {
492 synchronized (mService) {
493 if (mStatusBarService == null) {
494 mStatusBarService = IStatusBarService.Stub.asInterface(
495 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
496 if (mStatusBarService == null) {
497 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
498 }
499 }
500 return mStatusBarService;
501 }
502 }
503
Jason Monk35c62a42014-06-17 10:24:47 -0400504 private IDevicePolicyManager getDevicePolicyManager() {
505 synchronized (mService) {
506 if (mDevicePolicyManager == null) {
507 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
508 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
509 if (mDevicePolicyManager == null) {
510 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
511 }
512 }
513 return mDevicePolicyManager;
514 }
515 }
516
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700517 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800518 synchronized (mService) {
519 mWindowManager = wm;
520
521 mDisplayManager =
522 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
523 mDisplayManager.registerDisplayListener(this, null);
524
525 Display[] displays = mDisplayManager.getDisplays();
526 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
527 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800528 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700529 if (activityDisplay.mDisplay == null) {
530 throw new IllegalStateException("Default Display does not exist");
531 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800532 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700533 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800534 }
535
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800536 mHomeStack = mFocusedStack = mLastFocusedStack =
537 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800538
539 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800540 }
Craig Mautner27084302013-03-25 08:05:25 -0700541 }
542
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200543 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700544 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200545 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700546 }
547
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700548 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800549 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700550 }
551
Craig Mautnerde4ef022013-04-07 19:01:33 -0700552 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800553 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700554 }
555
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700556 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700557 if (stack == null) {
558 return false;
559 }
560
Craig Mautnerdf88d732014-01-27 09:21:32 -0800561 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
562 if (parent != null) {
563 stack = parent.task.stack;
564 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800565 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700566 }
567
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700568 /** The top most stack. */
569 boolean isFrontStack(ActivityStack stack) {
570 if (stack == null) {
571 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800572 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700573
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700574 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
575 if (parent != null) {
576 stack = parent.task.stack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800577 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700578 return stack == mHomeStack.mStacks.get((mHomeStack.mStacks.size() - 1));
579 }
580
Wale Ogunwaled046a012015-12-24 13:05:59 -0800581 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800582 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
583 if (!focusCandidate.isFocusable()) {
584 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
585 focusCandidate = focusCandidate.getNextFocusableStackLocked();
586 }
587
588 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800589 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800590 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800591
Wale Ogunwaled046a012015-12-24 13:05:59 -0800592 EventLogTags.writeAmFocusedStack(
593 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
594 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
595 }
596
597 final ActivityRecord r = topRunningActivityLocked();
Chong Zhang3aa28b22016-02-04 16:38:33 -0800598 if (!mService.mDoingSetFocusedActivity && mService.mFocusedActivity != r) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800599 // The focus activity should always be the top activity in the focused stack.
600 // There will be chaos and anarchy if it isn't...
601 mService.setFocusedActivityLocked(r, reason + " setFocusStack");
602 }
Craig Mautnerde313752015-01-22 14:28:03 -0800603
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800604 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800605 if (r != null && r.idle) {
606 checkFinishBootingLocked();
607 }
608 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700609 }
610
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700611 void moveHomeStackToFront(String reason) {
612 mHomeStack.moveToFront(reason);
613 }
614
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700615 /** Returns true if the focus activity was adjusted to the home stack top activity. */
616 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700617 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
618 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700619 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700620 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700621
Craig Mautner84984fa2014-06-19 11:19:20 -0700622 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700623
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700624 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700625 if (top == null) {
626 return false;
627 }
628 mService.setFocusedActivityLocked(top, reason);
629 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700630 }
631
Craig Mautner299f9602015-01-26 09:47:33 -0800632 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700633 if (!mService.mBooting && !mService.mBooted) {
634 // Not ready yet!
635 return false;
636 }
637
Craig Mautner84984fa2014-06-19 11:19:20 -0700638 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
639 mWindowManager.showRecentApps();
640 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700641 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700642
Craig Mautner84984fa2014-06-19 11:19:20 -0700643 if (prev != null) {
644 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
645 }
646
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700647 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
648 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800649 final String myReason = reason + " resumeHomeStackTask";
650
Mark Lua56ea122015-10-08 13:31:01 +0800651 // Only resume home activity if isn't finishing.
652 if (r != null && !r.finishing) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800653 mService.setFocusedActivityLocked(r, myReason);
654 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700655 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800656 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700657 }
658
Craig Mautner8d341ef2013-03-26 09:03:27 -0700659 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700660 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700661 }
662
663 /**
664 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
665 * @param id Id of the task we would like returned.
666 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
667 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700668 * @param stackId The stack to restore the task to (default launch stack will be used if
669 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700670 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700671 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800672 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800673 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800674 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800675 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
676 ActivityStack stack = stacks.get(stackNdx);
677 TaskRecord task = stack.taskForIdLocked(id);
678 if (task != null) {
679 return task;
680 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700681 }
682 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800683
684 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700685 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800686 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800687 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700688 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800689 return null;
690 }
691
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700692 if (!restoreFromRecents) {
693 return task;
694 }
695
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700696 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700697 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
698 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800699 return null;
700 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700701 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800702 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700703 }
704
Craig Mautner6170f732013-04-02 13:05:23 -0700705 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800706 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800707 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800708 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800709 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
710 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
711 if (r != null) {
712 return r;
713 }
Craig Mautner6170f732013-04-02 13:05:23 -0700714 }
715 }
716 return null;
717 }
718
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000719 boolean isFocusedUserLockedProfile() {
720 final int userId = mFocusedStack.topRunningActivityLocked().userId;
721 return userId != UserHandle.myUserId()
722 && mService.mUserController.shouldConfirmCredentials(userId);
723 }
724
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800725 void setNextTaskIdForUserLocked(int taskId, int userId) {
726 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
727 if (taskId > currentTaskId) {
728 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700729 }
730 }
731
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800732 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800733 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
734 // for a userId u, a taskId can only be in the range
735 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
736 // was 10, user 0 could only have taskIds 0 to 9, user 1: 10 to 19, user 2: 20 to 29, so on.
737 int candidateTaskId = currentTaskId;
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800738 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
739 || anyTaskForIdLocked(candidateTaskId, !RESTORE_FROM_RECENTS,
740 INVALID_STACK_ID) != null) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800741 candidateTaskId++;
742 if (candidateTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
743 // Wrap around as there will be smaller task ids that are available now.
744 candidateTaskId -= MAX_TASK_IDS_PER_USER;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700745 }
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800746 if (candidateTaskId == currentTaskId) {
747 // Something wrong!
748 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
749 throw new IllegalStateException("Cannot get an available task id."
750 + " Reached limit of " + MAX_TASK_IDS_PER_USER
751 + " running tasks per user.");
752 }
753 }
754 mCurTaskIdForUser.put(userId, candidateTaskId);
755 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700756 }
757
Craig Mautnerde4ef022013-04-07 19:01:33 -0700758 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800759 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700760 if (stack == null) {
761 return null;
762 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700763 ActivityRecord resumedActivity = stack.mResumedActivity;
764 if (resumedActivity == null || resumedActivity.app == null) {
765 resumedActivity = stack.mPausingActivity;
766 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700767 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700768 }
769 }
770 return resumedActivity;
771 }
772
Dianne Hackbornff072722014-09-24 10:56:28 -0700773 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700774 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800775 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800776 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
777 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800778 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
779 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700780 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800781 continue;
782 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700783 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800784 if (hr != null) {
785 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
786 && processName.equals(hr.processName)) {
787 try {
George Mount2c92c972014-03-20 09:38:23 -0700788 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800789 didSomething = true;
790 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700791 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800792 Slog.w(TAG, "Exception in new application when starting activity "
793 + hr.intent.getComponent().flattenToShortString(), e);
794 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700795 }
Craig Mautner20e72272013-04-01 13:45:53 -0700796 }
Craig Mautner20e72272013-04-01 13:45:53 -0700797 }
798 }
799 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700800 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700801 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700802 }
Craig Mautner20e72272013-04-01 13:45:53 -0700803 return didSomething;
804 }
805
806 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800807 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
808 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800809 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
810 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700811 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800812 continue;
813 }
814 final ActivityRecord resumedActivity = stack.mResumedActivity;
815 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700816 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800817 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800818 return false;
819 }
Craig Mautner20e72272013-04-01 13:45:53 -0700820 }
821 }
822 return true;
823 }
824
Craig Mautnerde4ef022013-04-07 19:01:33 -0700825 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800826 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
827 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800828 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
829 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700830 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800831 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700832 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800833 return false;
834 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700835 }
836 }
837 }
838 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700839 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800840 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
841 mLastFocusedStack + " to=" + mFocusedStack);
842 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700843 return true;
844 }
845
846 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800847 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800848 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
849 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800850 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
851 final ActivityStack stack = stacks.get(stackNdx);
852 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800853 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700854 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800855 return false;
856 }
857 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800858 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700859 }
860 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800861 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700862 }
863
Craig Mautner2acc3892013-09-23 10:28:14 -0700864 /**
865 * Pause all activities in either all of the stacks or just the back stacks.
866 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700867 * @return true if any activity was paused as a result of this call.
868 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700869 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700870 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800871 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
872 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800873 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
874 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700875 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700876 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800877 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700878 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
879 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800880 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700881 }
882 }
883 return someActivityPaused;
884 }
885
Craig Mautnerde4ef022013-04-07 19:01:33 -0700886 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700887 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800888 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
889 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800890 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
891 final ActivityStack stack = stacks.get(stackNdx);
892 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700893 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800894 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700895 Slog.d(TAG_STATES,
896 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800897 pausing = false;
898 } else {
899 return false;
900 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700901 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700902 }
903 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700904 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700905 }
906
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700907 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
908 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500909 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800910 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
911 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
912 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
913 final ActivityStack stack = stacks.get(stackNdx);
914 if (stack.mResumedActivity != null &&
915 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700916 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800917 }
918 }
919 }
920 }
921
Wale Ogunwale2be760d2016-02-17 11:16:10 -0800922 void cancelInitializingActivities() {
923 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
924 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
925 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
926 stacks.get(stackNdx).cancelInitializingActivities();
927 }
928 }
929 }
930
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700931 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700932 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700933 }
934
935 void sendWaitingVisibleReportLocked(ActivityRecord r) {
936 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700937 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700938 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700939 if (w.who == null) {
940 changed = true;
941 w.timeout = false;
942 if (r != null) {
943 w.who = new ComponentName(r.info.packageName, r.info.name);
944 }
945 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
946 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700947 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700948 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700949 if (changed) {
950 mService.notifyAll();
951 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700952 }
953
954 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
955 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700956 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700957 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700958 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700959 if (w.who == null) {
960 changed = true;
961 w.timeout = timeout;
962 if (r != null) {
963 w.who = new ComponentName(r.info.packageName, r.info.name);
964 }
965 w.thisTime = thisTime;
966 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700967 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700968 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700969 if (changed) {
970 mService.notifyAll();
971 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700972 }
973
Craig Mautner29219d92013-04-16 20:19:12 -0700974 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800975 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700976 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -0700977 if (r != null) {
978 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700979 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700980
Craig Mautner4a1cb222013-12-04 16:14:06 -0800981 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800982 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800983 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
984 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800985 if (stack != focusedStack && isFrontStack(stack) && stack.isFocusable()) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700986 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -0700987 if (r != null) {
988 return r;
989 }
990 }
991 }
992 return null;
993 }
994
Dianne Hackborn09233282014-04-30 11:33:59 -0700995 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700996 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800997 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
998 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800999 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001000 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001001 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001002 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1003 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001004 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001005 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001006 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001007 }
1008 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001009
1010 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1011 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1012 while (maxNum > 0) {
1013 long mostRecentActiveTime = Long.MIN_VALUE;
1014 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001015 final int numTaskLists = runningTaskLists.size();
1016 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1017 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001018 if (!stackTaskList.isEmpty()) {
1019 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1020 if (lastActiveTime > mostRecentActiveTime) {
1021 mostRecentActiveTime = lastActiveTime;
1022 selectedStackList = stackTaskList;
1023 }
1024 }
1025 }
1026 if (selectedStackList != null) {
1027 list.add(selectedStackList.remove(0));
1028 --maxNum;
1029 } else {
1030 break;
1031 }
1032 }
Craig Mautner20e72272013-04-01 13:45:53 -07001033 }
1034
Todd Kennedy7440f172015-12-09 14:31:22 -08001035 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1036 ProfilerInfo profilerInfo) {
1037 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001038 if (aInfo != null) {
1039 // Store the found target back into the intent, because now that
1040 // we have it we never want to do this again. For example, if the
1041 // user navigates back to this point in the history, we should
1042 // always restart the exact same activity.
1043 intent.setComponent(new ComponentName(
1044 aInfo.applicationInfo.packageName, aInfo.name));
1045
1046 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001047 if (!aInfo.processName.equals("system")) {
1048 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001049 mService.setDebugApp(aInfo.processName, true, false);
1050 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001051
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001052 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1053 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1054 }
1055
Man Caocfa78b22015-06-11 20:14:34 -07001056 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1057 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1058 }
1059
1060 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001061 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001062 }
1063 }
1064 }
1065 return aInfo;
1066 }
1067
Todd Kennedy7440f172015-12-09 14:31:22 -08001068 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001069 return resolveIntent(intent, resolvedType, userId, 0);
1070 }
1071
1072 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy7440f172015-12-09 14:31:22 -08001073 try {
1074 return AppGlobals.getPackageManager().resolveIntent(intent, resolvedType,
Kenny Guyb1b30262016-02-09 16:02:35 +00001075 PackageManager.MATCH_DEFAULT_ONLY | flags
1076 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001077 } catch (RemoteException e) {
1078 }
1079 return null;
1080 }
1081
1082 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1083 ProfilerInfo profilerInfo, int userId) {
1084 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1085 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1086 }
1087
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001088 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1089 boolean andResume, boolean checkConfig) throws RemoteException {
1090
1091 if (!allPausedActivitiesComplete()) {
1092 // While there are activities pausing we skipping starting any new activities until
1093 // pauses are complete. NOTE: that we also do this for activities that are starting in
1094 // the paused state because they will first be resumed then paused on the client side.
1095 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1096 "realStartActivityLocked: Skipping start of r=" + r
1097 + " some activities pausing...");
1098 return false;
1099 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001100
Craig Mautner2568c3a2015-03-26 14:22:34 -07001101 if (andResume) {
1102 r.startFreezingScreenLocked(app, 0);
1103 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001104
Craig Mautner2568c3a2015-03-26 14:22:34 -07001105 // schedule launch ticks to collect information about slow apps.
1106 r.startLaunchTickingLocked();
1107 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001108
1109 // Have the window manager re-evaluate the orientation of
1110 // the screen based on the new activity order. Note that
1111 // as a result of this, it can call back into the activity
1112 // manager with a new orientation. We don't care about that,
1113 // because the activity is not currently running so we are
1114 // just restarting it anyway.
1115 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001116 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001117 mService.mConfiguration,
1118 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001119 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001120 }
1121
1122 r.app = app;
1123 app.waitingToKill = null;
1124 r.launchCount++;
1125 r.lastLaunchTime = SystemClock.uptimeMillis();
1126
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001127 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001128
1129 int idx = app.activities.indexOf(r);
1130 if (idx < 0) {
1131 app.activities.add(r);
1132 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001133 mService.updateLruProcessLocked(app, true, null);
1134 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001135
Craig Mautner15df08a2015-04-01 12:17:18 -07001136 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001137 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1138 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001139 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001140 }
1141
1142 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001143 try {
1144 if (app.thread == null) {
1145 throw new RemoteException();
1146 }
1147 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001148 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001149 if (andResume) {
1150 results = r.results;
1151 newIntents = r.newIntents;
1152 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001153 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1154 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1155 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001156 if (andResume) {
1157 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1158 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001159 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001160 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001161 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001162 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001163 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001164 }
Nicolas Geoffray27c07372015-11-05 16:54:09 +00001165 mService.notifyPackageUse(r.intent.getComponent().getPackageName());
Craig Mautner2420ead2013-04-01 17:13:20 -07001166 r.sleeping = false;
1167 r.forceNewConfig = false;
1168 mService.showAskCompatModeDialogLocked(r);
1169 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001170 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001171 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1172 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1173 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001174 final String profileFile = mService.mProfileFile;
1175 if (profileFile != null) {
1176 ParcelFileDescriptor profileFd = mService.mProfileFd;
1177 if (profileFd != null) {
1178 try {
1179 profileFd = profileFd.dup();
1180 } catch (IOException e) {
1181 if (profileFd != null) {
1182 try {
1183 profileFd.close();
1184 } catch (IOException o) {
1185 }
1186 profileFd = null;
1187 }
1188 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001189 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001190
1191 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1192 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001193 }
1194 }
1195 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001196
Craig Mautner2568c3a2015-03-26 14:22:34 -07001197 if (andResume) {
1198 app.hasShownUi = true;
1199 app.pendingUiClean = true;
1200 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001201 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001202 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001203 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001204 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001205 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001206 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001207
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001208 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001209 // This may be a heavy-weight process! Note that the package
1210 // manager will ensure that only activity can run in the main
1211 // process of the .apk, which is the only thing that will be
1212 // considered heavy-weight.
1213 if (app.processName.equals(app.info.packageName)) {
1214 if (mService.mHeavyWeightProcess != null
1215 && mService.mHeavyWeightProcess != app) {
1216 Slog.w(TAG, "Starting new heavy weight process " + app
1217 + " when already running "
1218 + mService.mHeavyWeightProcess);
1219 }
1220 mService.mHeavyWeightProcess = app;
1221 Message msg = mService.mHandler.obtainMessage(
1222 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1223 msg.obj = r;
1224 mService.mHandler.sendMessage(msg);
1225 }
1226 }
1227
1228 } catch (RemoteException e) {
1229 if (r.launchFailed) {
1230 // This is the second time we failed -- finish activity
1231 // and give up.
1232 Slog.e(TAG, "Second failure launching "
1233 + r.intent.getComponent().flattenToShortString()
1234 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001235 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001236 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1237 "2nd-crash", false);
1238 return false;
1239 }
1240
1241 // This is the first time we failed -- restart process and
1242 // retry.
1243 app.activities.remove(r);
1244 throw e;
1245 }
1246
1247 r.launchFailed = false;
1248 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001249 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001250 }
1251
1252 if (andResume) {
1253 // As part of the process of launching, ActivityThread also performs
1254 // a resume.
1255 stack.minimalResumeActivityLocked(r);
1256 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001257 // This activity is not starting in the resumed state... which should look like we asked
1258 // it to pause+stop (but remain visible), and it has done so and reported back the
1259 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001260 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001261 "Moving to PAUSED: " + r + " (starting in paused state)");
1262 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001263 }
1264
1265 // Launch the new version setup screen if needed. We do this -after-
1266 // launching the initial activity (that is, home), so that it can have
1267 // a chance to initialize itself while in the background, making the
1268 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001269 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001270 mService.startSetupActivityLocked();
1271 }
1272
Dianne Hackborn465fa392014-09-14 14:21:18 -07001273 // Update any services we are bound to that might care about whether
1274 // their client may have activities.
1275 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1276
Craig Mautner2420ead2013-04-01 17:13:20 -07001277 return true;
1278 }
1279
Craig Mautnere79d42682013-04-01 19:01:53 -07001280 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001281 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001282 // Is this activity's application already running?
1283 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001284 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001285
1286 r.task.stack.setLaunchTime(r);
1287
1288 if (app != null && app.thread != null) {
1289 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001290 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1291 || !"android".equals(r.info.packageName)) {
1292 // Don't add this if it is a platform component that is marked
1293 // to run in multiple processes, because this is actually
1294 // part of the framework so doesn't make sense to track as a
1295 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001296 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1297 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001298 }
George Mount2c92c972014-03-20 09:38:23 -07001299 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001300 return;
1301 } catch (RemoteException e) {
1302 Slog.w(TAG, "Exception when starting activity "
1303 + r.intent.getComponent().flattenToShortString(), e);
1304 }
1305
1306 // If a dead object exception was thrown -- fall through to
1307 // restart the application.
1308 }
1309
1310 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001311 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001312 }
1313
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001314 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001315 String resultWho, int requestCode, int callingPid, int callingUid,
1316 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001317 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001318 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1319 callingUid);
1320 if (startAnyPerm == PERMISSION_GRANTED) {
1321 return true;
1322 }
1323 final int componentRestriction = getComponentRestrictionForCallingPackage(
1324 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1325 final int actionRestriction = getActionRestrictionForCallingPackage(
1326 intent.getAction(), callingPackage, callingPid, callingUid);
1327 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1328 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1329 if (resultRecord != null) {
1330 resultStack.sendActivityResultLocked(-1,
1331 resultRecord, resultWho, requestCode,
1332 Activity.RESULT_CANCELED, null);
1333 }
1334 final String msg;
1335 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1336 msg = "Permission Denial: starting " + intent.toString()
1337 + " from " + callerApp + " (pid=" + callingPid
1338 + ", uid=" + callingUid + ")" + " with revoked permission "
1339 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1340 } else if (!aInfo.exported) {
1341 msg = "Permission Denial: starting " + intent.toString()
1342 + " from " + callerApp + " (pid=" + callingPid
1343 + ", uid=" + callingUid + ")"
1344 + " not exported from uid " + aInfo.applicationInfo.uid;
1345 } else {
1346 msg = "Permission Denial: starting " + intent.toString()
1347 + " from " + callerApp + " (pid=" + callingPid
1348 + ", uid=" + callingUid + ")"
1349 + " requires " + aInfo.permission;
1350 }
1351 Slog.w(TAG, msg);
1352 throw new SecurityException(msg);
1353 }
1354
1355 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1356 final String message = "Appop Denial: starting " + intent.toString()
1357 + " from " + callerApp + " (pid=" + callingPid
1358 + ", uid=" + callingUid + ")"
1359 + " requires " + AppOpsManager.permissionToOp(
1360 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1361 Slog.w(TAG, message);
1362 return false;
1363 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1364 final String message = "Appop Denial: starting " + intent.toString()
1365 + " from " + callerApp + " (pid=" + callingPid
1366 + ", uid=" + callingUid + ")"
1367 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1368 Slog.w(TAG, message);
1369 return false;
1370 }
Jorim Jaggi2adba072016-03-03 13:43:39 +01001371 if (options != null && options.getLaunchTaskId() != -1) {
1372 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1373 callingPid, callingUid);
1374 if (startInTaskPerm != PERMISSION_GRANTED) {
1375 final String msg = "Permission Denial: starting " + intent.toString()
1376 + " from " + callerApp + " (pid=" + callingPid
1377 + ", uid=" + callingUid + ") with launchTaskId="
1378 + options.getLaunchTaskId();
1379 Slog.w(TAG, msg);
1380 throw new SecurityException(msg);
1381 }
1382 }
1383
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001384 return true;
1385 }
1386
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001387 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001388 final long identity = Binder.clearCallingIdentity();
1389 try {
1390 return UserManager.get(mService.mContext).getUserInfo(userId);
1391 } finally {
1392 Binder.restoreCallingIdentity(identity);
1393 }
1394 }
1395
Svet Ganov99b60432015-06-27 13:15:22 -07001396 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001397 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001398 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1399 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001400 == PackageManager.PERMISSION_DENIED) {
1401 return ACTIVITY_RESTRICTION_PERMISSION;
1402 }
1403
Christopher Tateff7add02015-08-17 10:23:22 -07001404 if (activityInfo.permission == null) {
1405 return ACTIVITY_RESTRICTION_NONE;
1406 }
1407
Svet Ganov99b60432015-06-27 13:15:22 -07001408 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1409 if (opCode == AppOpsManager.OP_NONE) {
1410 return ACTIVITY_RESTRICTION_NONE;
1411 }
1412
1413 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1414 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001415 if (!ignoreTargetSecurity) {
1416 return ACTIVITY_RESTRICTION_APPOP;
1417 }
Svet Ganov99b60432015-06-27 13:15:22 -07001418 }
1419
1420 return ACTIVITY_RESTRICTION_NONE;
1421 }
1422
Svetoslav7008b512015-06-24 18:47:07 -07001423 private int getActionRestrictionForCallingPackage(String action,
1424 String callingPackage, int callingPid, int callingUid) {
1425 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001426 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001427 }
1428
1429 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1430 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001431 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001432 }
1433
1434 final PackageInfo packageInfo;
1435 try {
1436 packageInfo = mService.mContext.getPackageManager()
1437 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1438 } catch (PackageManager.NameNotFoundException e) {
1439 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001440 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001441 }
1442
1443 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001444 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001445 }
1446
1447 if (mService.checkPermission(permission, callingPid, callingUid) ==
1448 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001449 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001450 }
1451
1452 final int opCode = AppOpsManager.permissionToOpCode(permission);
1453 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001454 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001455 }
1456
1457 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1458 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001459 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001460 }
1461
Svet Ganov99b60432015-06-27 13:15:22 -07001462 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001463 }
1464
Wale Ogunwaled046a012015-12-24 13:05:59 -08001465 boolean moveActivityStackToFront(ActivityRecord r, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001466 if (r == null) {
1467 // Not sure what you are trying to do, but it is not going to work...
1468 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001469 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001470 final TaskRecord task = r.task;
1471 if (task == null || task.stack == null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001472 Slog.w(TAG, "Can't move stack to front for r=" + r + " task=" + task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001473 return false;
1474 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001475 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001476 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001477 }
1478
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001479 void setLaunchSource(int uid) {
1480 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1481 }
1482
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001483 void acquireLaunchWakelock() {
1484 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1485 throw new IllegalStateException("Calling must be system uid");
1486 }
1487 mLaunchingActivity.acquire();
1488 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1489 // To be safe, don't allow the wake lock to be held for too long.
1490 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1491 }
1492 }
1493
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001494 /**
1495 * Called when the frontmost task is idle.
1496 * @return the state of mService.mBooting before this was called.
1497 */
1498 private boolean checkFinishBootingLocked() {
1499 final boolean booting = mService.mBooting;
1500 boolean enableScreen = false;
1501 mService.mBooting = false;
1502 if (!mService.mBooted) {
1503 mService.mBooted = true;
1504 enableScreen = true;
1505 }
1506 if (booting || enableScreen) {
1507 mService.postFinishBooting(booting, enableScreen);
1508 }
1509 return booting;
1510 }
1511
Craig Mautnerf3333272013-04-22 10:55:53 -07001512 // Checked.
1513 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
1514 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001515 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001516
Craig Mautnerf3333272013-04-22 10:55:53 -07001517 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001518 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001519 int NS = 0;
1520 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001521 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001522 boolean activityRemoved = false;
1523
Wale Ogunwale7d701172015-03-11 15:36:30 -07001524 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001525 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001526 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1527 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001528 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1529 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001530 if (fromTimeout) {
1531 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001532 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001533
1534 // This is a hack to semi-deal with a race condition
1535 // in the client where it can be constructed with a
1536 // newer configuration from when we asked it to launch.
1537 // We'll update with whatever configuration it now says
1538 // it used to launch.
1539 if (config != null) {
1540 r.configuration = config;
1541 }
1542
1543 // We are now idle. If someone is waiting for a thumbnail from
1544 // us, we can now deliver.
1545 r.idle = true;
1546
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001547 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001548 if (isFocusedStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001549 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001550 }
1551 }
1552
1553 if (allResumedActivitiesIdle()) {
1554 if (r != null) {
1555 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001556 }
1557
1558 if (mLaunchingActivity.isHeld()) {
1559 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1560 if (VALIDATE_WAKE_LOCK_CALLER &&
1561 Binder.getCallingUid() != Process.myUid()) {
1562 throw new IllegalStateException("Calling must be system uid");
1563 }
1564 mLaunchingActivity.release();
1565 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001566 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001567 }
1568
1569 // Atomically retrieve all of the other things to do.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001570 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(true);
Craig Mautnerf3333272013-04-22 10:55:53 -07001571 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001572 if ((NF = mFinishingActivities.size()) > 0) {
1573 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001574 mFinishingActivities.clear();
1575 }
1576
Craig Mautnerf3333272013-04-22 10:55:53 -07001577 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001578 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001579 mStartingUsers.clear();
1580 }
1581
Craig Mautnerf3333272013-04-22 10:55:53 -07001582 // Stop any activities that are scheduled to do so but have been
1583 // waiting for the next one to start.
1584 for (int i = 0; i < NS; i++) {
1585 r = stops.get(i);
1586 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001587 if (stack != null) {
1588 if (r.finishing) {
1589 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1590 } else {
1591 stack.stopActivityLocked(r);
1592 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001593 }
1594 }
1595
1596 // Finish any activities that are scheduled to do so but have been
1597 // waiting for the next one to start.
1598 for (int i = 0; i < NF; i++) {
1599 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07001600 final ActivityStack stack = r.task.stack;
1601 if (stack != null) {
1602 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1603 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001604 }
1605
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001606 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001607 // Complete user switch
1608 if (startingUsers != null) {
1609 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001610 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001611 }
1612 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001613 }
1614
1615 mService.trimApplications();
1616 //dump();
1617 //mWindowManager.dump();
1618
Craig Mautnerf3333272013-04-22 10:55:53 -07001619 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001620 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001621 }
1622
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001623 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001624 }
1625
Craig Mautner8e569572013-10-11 17:36:59 -07001626 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001627 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001628 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1629 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001630 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1631 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1632 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001633 }
Craig Mautner19091252013-10-05 00:03:53 -07001634 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001635 }
1636
1637 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001638 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1639 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001640 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1641 stacks.get(stackNdx).closeSystemDialogsLocked();
1642 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001643 }
1644 }
1645
Craig Mautner93529a42013-10-04 15:03:13 -07001646 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001647 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001648 }
1649
Craig Mautner8d341ef2013-03-26 09:03:27 -07001650 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001651 * Update the last used stack id for non-current user (current user's last
1652 * used stack is the focused stack)
1653 */
1654 void updateUserStackLocked(int userId, ActivityStack stack) {
1655 if (userId != mCurrentUser) {
1656 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1657 }
1658 }
1659
1660 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001661 * @return true if some activity was finished (or would have finished if doit were true).
1662 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001663 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1664 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001665 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001666 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1667 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001668 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001669 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001670 if (stack.finishDisabledPackageActivitiesLocked(
1671 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001672 didSomething = true;
1673 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001674 }
1675 }
1676 return didSomething;
1677 }
1678
Dianne Hackborna413dc02013-07-12 12:02:55 -07001679 void updatePreviousProcessLocked(ActivityRecord r) {
1680 // Now that this process has stopped, we may want to consider
1681 // it to be the previous app to try to keep around in case
1682 // the user wants to return to it.
1683
1684 // First, found out what is currently the foreground app, so that
1685 // we don't blow away the previous app if this activity is being
1686 // hosted by the process that is actually still the foreground.
1687 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001688 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1689 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001690 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1691 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001692 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001693 if (stack.mResumedActivity != null) {
1694 fgApp = stack.mResumedActivity.app;
1695 } else if (stack.mPausingActivity != null) {
1696 fgApp = stack.mPausingActivity.app;
1697 }
1698 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001699 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001700 }
1701 }
1702
1703 // Now set this one as the previous process, only if that really
1704 // makes sense to.
1705 if (r.app != null && fgApp != null && r.app != fgApp
1706 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001707 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001708 mService.mPreviousProcess = r.app;
1709 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1710 }
1711 }
1712
Wale Ogunwaled046a012015-12-24 13:05:59 -08001713 boolean resumeFocusedStackTopActivityLocked() {
1714 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001715 }
1716
Wale Ogunwaled046a012015-12-24 13:05:59 -08001717 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001718 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001719 if (targetStack != null && isFocusedStack(targetStack)) {
1720 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001721 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001722 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
1723 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001724 }
1725
Todd Kennedy39bfee52016-02-24 10:28:21 -08001726 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
1727 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1728 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1729 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1730 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
1731 }
1732 }
1733 }
1734
Adrian Roos20d7df32016-01-12 18:59:43 +01001735 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
1736 TaskRecord finishedTask = null;
1737 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08001738 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1739 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001740 final int numStacks = stacks.size();
1741 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1742 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01001743 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
1744 if (stack == focusedStack || finishedTask == null) {
1745 finishedTask = t;
1746 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001747 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001748 }
Adrian Roos20d7df32016-01-12 18:59:43 +01001749 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001750 }
1751
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07001752 void finishVoiceTask(IVoiceInteractionSession session) {
1753 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1754 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1755 final int numStacks = stacks.size();
1756 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1757 final ActivityStack stack = stacks.get(stackNdx);
1758 stack.finishVoiceTask(session);
1759 }
1760 }
1761 }
1762
Andrii Kulianc27916642016-04-12 17:59:27 -07001763 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
1764 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001765 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
1766 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001767 }
Craig Mautneraea74a52014-03-08 14:23:10 -08001768 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
1769 // Caller wants the home activity moved with it. To accomplish this,
1770 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07001771 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08001772 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07001773 if (task.stack == null) {
1774 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
1775 + task + " to front. Stack is null");
1776 return;
1777 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001778
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001779 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001780 int stackId = options.getLaunchStackId();
1781 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001782 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07001783 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08001784 if (stackId == INVALID_STACK_ID) {
1785 stackId = task.getLaunchStackId();
1786 }
Chong Zhang112eb8c2015-11-02 11:17:00 -08001787 if (stackId != task.stack.mStackId) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08001788 final ActivityStack stack = moveTaskToStackUncheckedLocked(
1789 task, stackId, ON_TOP, !FORCE_FOCUS, reason);
1790 stackId = stack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08001791 // moveTaskToStackUncheckedLocked() should already placed the task on top,
1792 // still need moveTaskToFrontLocked() below for any transition settings.
1793 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001794 if (StackId.resizeStackWithLaunchBounds(stackId)) {
1795 resizeStackLocked(stackId, bounds,
1796 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
1797 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */);
1798 } else {
1799 // WM resizeTask must be done after the task is moved to the correct stack,
1800 // because Task's setBounds() also updates dim layer's bounds, but that has
1801 // dependency on the stack.
Andrii Kulian73336d812016-03-24 12:56:08 -07001802 mWindowManager.resizeTask(task.taskId, task.mBounds, task.mOverrideConfig,
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001803 false /* relayout */, false /* forced */);
1804 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001805 }
1806 }
1807
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001808 final ActivityRecord r = task.getTopActivity();
1809 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
1810 r == null ? null : r.appTimeTracker, reason);
1811
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001812 if (DEBUG_STACK) Slog.d(TAG_STACK,
1813 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001814
Andrii Kulianc27916642016-04-12 17:59:27 -07001815 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.stack.mStackId,
1816 forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001817 }
1818
Wale Ogunwale854809c2015-12-27 16:18:19 -08001819 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001820 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08001821 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08001822 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001823 return false;
1824 }
1825 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
1826 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001827 }
1828
Craig Mautner967212c2013-04-13 21:10:58 -07001829 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07001830 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001831 }
1832
1833 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001834 ActivityContainer activityContainer = mActivityContainers.get(stackId);
1835 if (activityContainer != null) {
1836 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001837 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07001838 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001839 return null;
1840 }
1841 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001842 }
1843
Craig Mautner967212c2013-04-13 21:10:58 -07001844 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07001845 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001846 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1847 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001848 }
1849 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07001850 }
1851
Craig Mautner4a1cb222013-12-04 16:14:06 -08001852 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001853 ActivityRecord homeActivity = getHomeActivity();
1854 if (homeActivity != null) {
1855 return homeActivity.appToken;
1856 }
1857 return null;
1858 }
1859
1860 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07001861 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001862 }
1863
1864 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001865 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
1866 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1867 final TaskRecord task = tasks.get(taskNdx);
1868 if (task.isHomeTask()) {
1869 final ArrayList<ActivityRecord> activities = task.mActivities;
1870 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1871 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001872 if (r.isHomeActivity()
1873 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001874 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001875 }
1876 }
1877 }
1878 }
1879 return null;
1880 }
1881
Chong Zhangb15758a2015-11-17 12:12:03 -08001882 /**
1883 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
1884 * the docked stack itself, or if it's side-by-side to the docked stack.
1885 */
1886 boolean isStackDockedInEffect(int stackId) {
1887 return stackId == DOCKED_STACK_ID ||
1888 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
1889 }
1890
Todd Kennedyca4d8422015-01-15 15:19:22 -08001891 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08001892 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07001893 ActivityContainer activityContainer =
1894 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001895 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001896 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1897 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001898 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001899 return activityContainer;
1900 }
1901
Craig Mautner34b73df2014-01-12 21:11:08 -08001902 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001903 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
1904 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
1905 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001906 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
1907 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001908 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08001909 }
1910 }
1911
Craig Mautner95da1082014-02-24 17:54:35 -08001912 void deleteActivityContainer(IActivityContainer container) {
1913 ActivityContainer activityContainer = (ActivityContainer)container;
1914 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001915 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1916 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08001917 final int stackId = activityContainer.mStackId;
1918 mActivityContainers.remove(stackId);
1919 mWindowManager.removeStack(stackId);
1920 }
1921 }
1922
Jorim Jaggidc249c42015-12-15 14:57:31 -08001923 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
1924 boolean preserveWindows, boolean allowResizeInDockedMode) {
1925 if (stackId == DOCKED_STACK_ID) {
1926 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
1927 preserveWindows);
1928 return;
1929 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08001930 final ActivityStack stack = getStack(stackId);
1931 if (stack == null) {
1932 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
1933 return;
1934 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08001935
Jorim Jaggidc249c42015-12-15 14:57:31 -08001936 if (!allowResizeInDockedMode && getStack(DOCKED_STACK_ID) != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07001937 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
1938 // stack size changing so things don't get out of sync.
1939 return;
1940 }
1941
Wale Ogunwalecad05a02015-09-25 10:41:44 -07001942 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02001943 mWindowManager.deferSurfaceLayout();
1944 try {
Jorim Jaggidc249c42015-12-15 14:57:31 -08001945 resizeStackUncheckedLocked(stack, bounds, tempTaskBounds, tempTaskInsetBounds);
1946 ensureConfigurationAndResume(stack, stack.topRunningActivityLocked(), preserveWindows);
Jorim Jaggic4025202015-10-22 16:43:34 +02001947 } finally {
1948 mWindowManager.continueSurfaceLayout();
1949 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001950 }
1951 }
1952
Jorim Jaggi192086e2016-03-11 17:17:03 +01001953 void deferUpdateBounds(int stackId) {
1954 final ActivityStack stack = getStack(stackId);
1955 if (stack != null) {
1956 stack.deferUpdateBounds();
1957 }
1958 }
1959
1960 void continueUpdateBounds(int stackId) {
1961 final ActivityStack stack = getStack(stackId);
1962 if (stack != null) {
1963 stack.continueUpdateBounds();
1964 }
1965 }
1966
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01001967 void notifyAppTransitionDone() {
1968 continueUpdateBounds(HOME_STACK_ID);
1969 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
1970 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
1971 if (anyTaskForIdLocked(taskId) != null) {
1972 mWindowManager.setTaskDockedResizing(taskId, false);
1973 }
1974 }
1975 mResizingTasksDuringAnimation.clear();
1976 }
1977
Jorim Jaggi192086e2016-03-11 17:17:03 +01001978 void resizeStackUncheckedLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
Jorim Jaggidc249c42015-12-15 14:57:31 -08001979 Rect tempTaskInsetBounds) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001980 bounds = TaskRecord.validateBounds(bounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001981
Jorim Jaggi192086e2016-03-11 17:17:03 +01001982 if (!stack.updateBoundsAllowed(bounds, tempTaskBounds, tempTaskInsetBounds)) {
1983 return;
1984 }
1985
Jorim Jaggidc249c42015-12-15 14:57:31 -08001986 mTmpBounds.clear();
1987 mTmpConfigs.clear();
1988 mTmpInsetBounds.clear();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001989 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
Jorim Jaggif15a7352016-04-04 23:54:30 -07001990 final Rect taskBounds = tempTaskBounds != null ? tempTaskBounds : bounds;
1991 final Rect insetBounds = tempTaskInsetBounds != null ? tempTaskInsetBounds : taskBounds;
Jorim Jaggidc249c42015-12-15 14:57:31 -08001992 for (int i = tasks.size() - 1; i >= 0; i--) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001993 final TaskRecord task = tasks.get(i);
1994 if (task.isResizeable()) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08001995 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1996 // For freeform stack we don't adjust the size of the tasks to match that
1997 // of the stack, but we do try to make sure the tasks are still contained
1998 // with the bounds of the stack.
1999 tempRect2.set(task.mBounds);
2000 fitWithinBounds(tempRect2, bounds);
2001 task.updateOverrideConfiguration(tempRect2);
2002 } else {
Jorim Jaggif15a7352016-04-04 23:54:30 -07002003 task.updateOverrideConfiguration(taskBounds, insetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002004 }
2005 }
2006
2007 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
2008 mTmpBounds.put(task.taskId, task.mBounds);
2009 if (tempTaskInsetBounds != null) {
2010 mTmpInsetBounds.put(task.taskId, tempTaskInsetBounds);
2011 }
2012 }
Jorim Jaggi0429f352015-12-22 16:29:16 +01002013
2014 // We might trigger a configuration change. Save the current task bounds for freezing.
2015 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002016 stack.mFullscreen = mWindowManager.resizeStack(stack.mStackId, bounds, mTmpConfigs,
2017 mTmpBounds, mTmpInsetBounds);
2018 stack.setBounds(bounds);
2019 }
2020
2021 private void ensureConfigurationAndResume(ActivityStack stack, ActivityRecord r,
2022 boolean preserveWindows) {
Jorim Jaggic3fb3142016-02-04 19:49:28 -08002023 if (r == null || !r.visible) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002024 return;
2025 }
2026 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0,
2027 preserveWindows);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002028 if (!updated) {
2029 resumeFocusedStackTopActivityLocked();
2030 }
2031 }
2032
2033 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2034 Rect tempDockedTaskInsetBounds,
2035 Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds, boolean preserveWindows) {
2036 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2037 if (stack == null) {
2038 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2039 return;
2040 }
2041
2042 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2043 mWindowManager.deferSurfaceLayout();
2044 try {
2045 ActivityRecord r = stack.topRunningActivityLocked();
2046 resizeStackUncheckedLocked(stack, dockedBounds, tempDockedTaskBounds,
2047 tempDockedTaskInsetBounds);
2048
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002049 // TODO: Checking for isAttached might not be needed as if the user passes in null
2050 // dockedBounds then they want the docked stack to be dismissed.
2051 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2052 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002053 // In this case we make all other static stacks fullscreen and move all
2054 // docked stack tasks to the fullscreen stack.
2055 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2056 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
2057 resizeStackLocked(i, null, null, null, preserveWindows,
2058 true /* allowResizeInDockedMode */);
2059 }
2060 }
2061
2062 ArrayList<TaskRecord> tasks = stack.getAllTasks();
2063 final int count = tasks.size();
2064 for (int i = 0; i < count; i++) {
2065 moveTaskToStackLocked(tasks.get(i).taskId,
2066 FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP, FORCE_FOCUS, "resizeStack",
2067 false /* animate */);
2068 }
2069
2070 // stack shouldn't contain anymore activities, so nothing to resume.
2071 r = null;
2072 } else {
2073 // Docked stacks occupy a dedicated region on screen so the size of all other
2074 // static stacks need to be adjusted so they don't overlap with the docked stack.
2075 // We get the bounds to use from window manager which has been adjusted for any
2076 // screen controls and is also the same for all stacks.
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002077 mWindowManager.getStackDockedModeBounds(
2078 HOME_STACK_ID, tempRect, true /* ignoreVisibility */);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002079 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002080 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
2081 resizeStackLocked(i, tempRect, tempOtherTaskBounds,
2082 tempOtherTaskInsetBounds, preserveWindows,
2083 true /* allowResizeInDockedMode */);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002084 }
2085 }
2086 }
2087 ensureConfigurationAndResume(stack, r, preserveWindows);
2088 } finally {
2089 mWindowManager.continueSurfaceLayout();
2090 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2091 }
2092
2093 mResizeDockedStackTimeout.notifyResizing(dockedBounds,
2094 tempDockedTaskBounds != null
2095 || tempDockedTaskInsetBounds != null
2096 || tempOtherTaskBounds != null
2097 || tempOtherTaskInsetBounds != null);
2098 }
2099
Robert Carr0d00c2e2016-02-29 17:45:02 -08002100 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
2101 final ActivityStack stack = getStack(PINNED_STACK_ID);
2102 if (stack == null) {
2103 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2104 return;
2105 }
2106 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2107 mWindowManager.deferSurfaceLayout();
2108 try {
2109 ActivityRecord r = stack.topRunningActivityLocked();
2110 resizeStackUncheckedLocked(stack, pinnedBounds, tempPinnedTaskBounds,
2111 null);
2112 ensureConfigurationAndResume(stack, r, false);
2113 } finally {
2114 mWindowManager.continueSurfaceLayout();
2115 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2116 }
2117 }
2118
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002119 boolean resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow,
2120 boolean deferResume) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002121 if (!task.isResizeable()) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002122 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
Chong Zhangf596cd52016-01-05 13:42:44 -08002123 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002124 }
2125
Chong Zhang87b21722015-09-21 15:39:51 -07002126 // If this is a forced resize, let it go through even if the bounds is not changing,
2127 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07002128 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002129 if (Objects.equals(task.mBounds, bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002130 // Nothing to do here...
Chong Zhangf596cd52016-01-05 13:42:44 -08002131 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002132 }
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002133 bounds = TaskRecord.validateBounds(bounds);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002134
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002135 if (!mWindowManager.isValidTaskId(task.taskId)) {
2136 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07002137 // All we can do for now is update the bounds so it can be used when the task is
2138 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002139 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002140 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
2141 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07002142 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002143 }
Chong Zhangf596cd52016-01-05 13:42:44 -08002144 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002145 }
2146
Chong Zhang6de2ae82015-09-30 18:25:21 -07002147 // Do not move the task to another stack here.
2148 // This method assumes that the task is already placed in the right stack.
2149 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002150
Chong Zhang6de2ae82015-09-30 18:25:21 -07002151 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07002152
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07002153 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07002154 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002155 // way and the activity was kept the way it was. If it's false, it means the activity had
2156 // to be relaunched due to configuration change.
2157 boolean kept = true;
2158 if (overrideConfig != null) {
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002159 final ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08002160 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002161 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07002162 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002163
2164 if (!deferResume) {
2165
2166 // All other activities must be made visible with their correct configuration.
2167 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
2168 if (!kept) {
2169 resumeFocusedStackTopActivityLocked();
2170 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002171 }
2172 }
2173 }
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002174 mWindowManager.resizeTask(task.taskId, task.mBounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002175
2176 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Chong Zhangf596cd52016-01-05 13:42:44 -08002177 return kept;
Wale Ogunwale60454db2015-01-23 16:05:07 -08002178 }
2179
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002180 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002181 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2182 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002183 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002184 }
2185
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002186 ActivityContainer activityContainer = new ActivityContainer(stackId);
2187 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002188 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002189 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002190 }
2191
2192 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002193 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002194 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2195 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002196 break;
2197 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002198 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002199 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002200 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002201 }
2202
Chong Zhang5dcb2752015-08-18 13:50:26 -07002203 /**
2204 * Restores a recent task to a stack
2205 * @param task The recent task to be restored.
2206 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale3797c222015-10-27 14:21:58 -07002207 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002208 * @return true if the task has been restored successfully.
2209 */
2210 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
2211 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002212 stackId = task.getLaunchStackId();
Wale Ogunwale513346d2016-01-27 10:55:01 -08002213 } else if (stackId == DOCKED_STACK_ID && !task.canGoInDockedStack()) {
2214 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2215 // Put it in the fullscreen stack.
2216 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002217 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002218
Wale Ogunwale706ed792015-08-02 10:29:44 -07002219 if (task.stack != null) {
2220 // Task has already been restored once. See if we need to do anything more
2221 if (task.stack.mStackId == stackId) {
2222 // Nothing else to do since it is already restored in the right stack.
2223 return true;
2224 }
2225 // Remove current stack association, so we can re-associate the task with the
2226 // right stack below.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002227 task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002228 }
2229
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002230 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002231 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002232
2233 if (stack == null) {
2234 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002235 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2236 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002237 return false;
2238 }
2239
Wale Ogunwale5f986092015-12-04 15:35:38 -08002240 stack.addTask(task, false, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002241 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2242 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002243 final ArrayList<ActivityRecord> activities = task.mActivities;
2244 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002245 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002246 }
2247 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002248 }
2249
Wale Ogunwale040b4702015-08-06 18:10:50 -07002250 /**
2251 * Moves the specified task record to the input stack id.
2252 * WARNING: This method performs an unchecked/raw move of the task and
2253 * can leave the system in an unstable state if used incorrectly.
Wale Ogunwale513346d2016-01-27 10:55:01 -08002254 * Use {@link #moveTaskToStackLocked} to perform safe task movement to a stack.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002255 * @param task Task to move.
2256 * @param stackId Id of stack to move task to.
2257 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07002258 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07002259 * @param reason Reason the task is been moved.
2260 * @return The stack the task was moved to.
2261 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07002262 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07002263 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
Wale Ogunwalefb1c8642016-03-02 08:28:08 -08002264
2265 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2266 throw new IllegalStateException("moveTaskToStackUncheckedLocked: Device doesn't "
2267 + "support multi-window task=" + task + " to stackId=" + stackId);
2268 }
2269
Chong Zhang02898352015-08-21 17:27:14 -07002270 final ActivityRecord r = task.getTopActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -08002271 final ActivityStack prevStack = task.stack;
2272 final boolean wasFocused = isFocusedStack(prevStack) && (topRunningActivityLocked() == r);
2273 final boolean wasResumed = wasFocused && (prevStack.mResumedActivity == r);
2274 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
2275 // Whenever we are moving the top activity from the front stack we want to make sure to move
2276 // the stack to the front.
2277 final boolean wasFront = isFrontStack(prevStack)
2278 && (prevStack.topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07002279
Chong Zhangd545dce2016-02-29 18:09:17 -08002280 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002281 // We don't allow moving a unresizeable task to the docked stack since the docked
2282 // stack is used for split-screen mode and will cause things like the docked divider to
2283 // show up. We instead leave the task in its current stack or move it to the fullscreen
2284 // stack if it isn't currently in a stack.
2285 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002286 Slog.w(TAG, "Can not move unresizeable task=" + task
2287 + " to docked stack. Moving to stackId=" + stackId + " instead.");
2288 }
2289
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002290 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
2291 // if a docked stack is created below which will lead to the stack we are moving from and
2292 // its resizeable tasks being resized.
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002293 task.mTemporarilyUnresizable = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002294 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002295 task.mTemporarilyUnresizable = false;
Wale Ogunwale040b4702015-08-06 18:10:50 -07002296 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002297 stack.addTask(task, toTop, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002298
2299 // If the task had focus before (or we're requested to move focus),
Wale Ogunwaled046a012015-12-24 13:05:59 -08002300 // move focus to the new stack by moving the stack to the front.
2301 stack.moveToFrontAndResumeStateIfNeeded(
2302 r, forceFocus || wasFocused || wasFront, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002303
Wale Ogunwale040b4702015-08-06 18:10:50 -07002304 return stack;
2305 }
2306
Chong Zhange4fbd322016-03-01 14:44:03 -08002307 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08002308 String reason, boolean animate) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002309 return moveTaskToStackLocked(taskId, stackId, toTop, forceFocus, reason, animate,
2310 false /* deferResume */);
2311 }
2312
2313 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
2314 String reason, boolean animate, boolean deferResume) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002315 final TaskRecord task = anyTaskForIdLocked(taskId);
2316 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002317 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002318 return false;
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002319 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002320
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002321 if (task.stack != null && task.stack.mStackId == stackId) {
2322 // You are already in the right stack silly...
2323 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002324 return true;
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002325 }
2326
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002327 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
2328 throw new IllegalArgumentException("moveTaskToStack:"
2329 + "Attempt to move task " + taskId + " to unsupported freeform stack");
2330 }
2331
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002332 final ActivityRecord topActivity = task.getTopActivity();
Jorim Jaggie9098022016-01-27 19:29:40 -08002333 final int sourceStackId = task.stack != null ? task.stack.mStackId : INVALID_STACK_ID;
Chong Zhangf596cd52016-01-05 13:42:44 -08002334 final boolean mightReplaceWindow =
Jorim Jaggie9098022016-01-27 19:29:40 -08002335 StackId.replaceWindowsOnTaskMove(sourceStackId, stackId) && topActivity != null;
Chong Zhangf596cd52016-01-05 13:42:44 -08002336 if (mightReplaceWindow) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002337 // We are about to relaunch the activity because its configuration changed due to
2338 // being maximized, i.e. size change. The activity will first remove the old window
2339 // and then add a new one. This call will tell window manager about this, so it can
2340 // preserve the old window until the new one is drawn. This prevents having a gap
2341 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07002342 // entrance of the new window to be properly animated.
Chong Zhangf596cd52016-01-05 13:42:44 -08002343 // Note here we always set the replacing window first, as the flags might be needed
2344 // during the relaunch. If we end up not doing any relaunch, we clear the flags later.
Jorim Jaggi030979c2015-11-20 15:14:43 -08002345 mWindowManager.setReplacingWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002346 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002347
2348 mWindowManager.deferSurfaceLayout();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002349 final int preferredLaunchStackId = stackId;
Chong Zhangf596cd52016-01-05 13:42:44 -08002350 boolean kept = true;
Wale Ogunwale961f4852016-02-01 20:25:54 -08002351 try {
2352 final ActivityStack stack = moveTaskToStackUncheckedLocked(
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002353 task, stackId, toTop, forceFocus, reason + " moveTaskToStack");
Wale Ogunwale961f4852016-02-01 20:25:54 -08002354 stackId = stack.mStackId;
Jorim Jaggie9098022016-01-27 19:29:40 -08002355
Wale Ogunwale961f4852016-02-01 20:25:54 -08002356 if (!animate) {
2357 stack.mNoAnimActivities.add(topActivity);
2358 }
Jorim Jaggie9098022016-01-27 19:29:40 -08002359
Wale Ogunwale961f4852016-02-01 20:25:54 -08002360 // We might trigger a configuration change. Save the current task bounds for freezing.
2361 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
2362
2363 // Make sure the task has the appropriate bounds/size for the stack it is in.
2364 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002365 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2366 !mightReplaceWindow, deferResume);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002367 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID) {
2368 Rect bounds = task.getLaunchBounds();
2369 if (bounds == null) {
2370 stack.layoutTaskInStack(task, null);
2371 bounds = task.mBounds;
2372 }
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002373 kept = resizeTaskLocked(task, bounds, RESIZE_MODE_FORCED, !mightReplaceWindow,
2374 deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002375 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002376 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2377 !mightReplaceWindow, deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002378 }
2379 } finally {
2380 mWindowManager.continueSurfaceLayout();
Chong Zhangf596cd52016-01-05 13:42:44 -08002381 }
2382
2383 if (mightReplaceWindow) {
2384 // If we didn't actual do a relaunch (indicated by kept==true meaning we kept the old
2385 // window), we need to clear the replace window settings. Otherwise, we schedule a
2386 // timeout to remove the old window if the replacing window is not coming in time.
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002387 mWindowManager.scheduleClearReplacingWindowIfNeeded(topActivity.appToken, !kept);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002388 }
2389
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002390 if (!deferResume) {
2391
2392 // The task might have already been running and its visibility needs to be synchronized with
2393 // the visibility of the stack / windows.
2394 ensureActivitiesVisibleLocked(null, 0, !mightReplaceWindow);
2395 resumeFocusedStackTopActivityLocked();
2396 }
Chong Zhangb15758a2015-11-17 12:12:03 -08002397
Jorim Jaggid53f0922016-04-06 22:16:23 -07002398 handleNonResizableTaskIfNeeded(task, preferredLaunchStackId, stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002399
2400 return (preferredLaunchStackId == stackId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002401 }
2402
Wale Ogunwale079a0042015-10-24 11:44:07 -07002403 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2404 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2405 if (stack == null) {
2406 throw new IllegalArgumentException(
2407 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2408 }
2409
2410 final ActivityRecord r = stack.topRunningActivityLocked();
2411 if (r == null) {
2412 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2413 + " in stack=" + stack);
2414 return false;
2415 }
2416
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002417 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002418 Slog.w(TAG,
2419 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002420 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002421 return false;
2422 }
2423
Wale Ogunwale480dca02016-02-06 13:58:29 -08002424 moveActivityToPinnedStackLocked(r, "moveTopActivityToPinnedStack", bounds);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002425 return true;
2426 }
2427
Wale Ogunwale480dca02016-02-06 13:58:29 -08002428 void moveActivityToPinnedStackLocked(ActivityRecord r, String reason, Rect bounds) {
2429 mWindowManager.deferSurfaceLayout();
2430 try {
2431 final TaskRecord task = r.task;
2432
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002433 if (r == task.stack.getVisibleBehindActivity()) {
2434 // An activity can't be pinned and visible behind at the same time. Go ahead and
2435 // release it from been visible behind before pinning.
2436 requestVisibleBehindLocked(r, false);
2437 }
2438
Wale Ogunwale480dca02016-02-06 13:58:29 -08002439 // Need to make sure the pinned stack exist so we can resize it below...
2440 final ActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
2441
2442 // Resize the pinned stack to match the current size of the task the activity we are
2443 // going to be moving is currently contained in. We do this to have the right starting
2444 // animation bounds for the pinned stack to the desired bounds the caller wants.
2445 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2446 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
2447 true /* allowResizeInDockedMode */);
2448
2449 if (task.mActivities.size() == 1) {
2450 // There is only one activity in the task. So, we can just move the task over to
2451 // the stack without re-parenting the activity in a different task.
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08002452 if (task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE) {
2453 // Move the home stack forward if the task we just moved to the pinned stack
2454 // was launched from home so home should be visible behind it.
2455 moveHomeStackToFront(reason);
2456 }
Wale Ogunwale480dca02016-02-06 13:58:29 -08002457 moveTaskToStackLocked(
2458 task.taskId, PINNED_STACK_ID, ON_TOP, FORCE_FOCUS, reason, !ANIMATE);
2459 } else {
2460 stack.moveActivityToStack(r);
2461 }
2462 } finally {
2463 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002464 }
2465
Wale Ogunwale480dca02016-02-06 13:58:29 -08002466 // The task might have already been running and its visibility needs to be synchronized
2467 // with the visibility of the stack / windows.
Wale Ogunwale079a0042015-10-24 11:44:07 -07002468 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002469 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002470
Wale Ogunwalee75a9ad2016-03-18 20:43:49 -07002471 mWindowManager.animateResizePinnedStack(bounds, -1);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002472 mService.notifyActivityPinnedLocked();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002473 }
2474
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002475 void positionTaskInStackLocked(int taskId, int stackId, int position) {
2476 final TaskRecord task = anyTaskForIdLocked(taskId);
2477 if (task == null) {
2478 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
2479 return;
2480 }
Wale Ogunwale935e5022015-11-10 12:36:10 -08002481 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
2482
2483 task.updateOverrideConfigurationForStack(stack);
2484
2485 mWindowManager.positionTaskInStack(
2486 taskId, stackId, position, task.mBounds, task.mOverrideConfig);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002487 stack.positionTask(task, position);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002488 // The task might have already been running and its visibility needs to be synchronized with
2489 // the visibility of the stack / windows.
Jorim Jaggi8fa45222016-02-19 19:54:39 -08002490 stack.ensureActivityConfigurationLocked(task.topRunningActivityLocked(), 0,
2491 !PRESERVE_WINDOWS);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002492 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002493 resumeFocusedStackTopActivityLocked();
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002494 }
2495
Craig Mautnerac6f8432013-07-17 13:24:59 -07002496 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002497 mTmpFindTaskResult.r = null;
2498 mTmpFindTaskResult.matchedByRootAffinity = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002499 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002500 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2501 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002502 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2503 final ActivityStack stack = stacks.get(stackNdx);
2504 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002505 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002506 continue;
2507 }
2508 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002509 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2510 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002511 continue;
2512 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002513 stack.findTaskLocked(r, mTmpFindTaskResult);
2514 // It is possible to have task in multiple stacks with the same root affinity.
2515 // If the match we found was based on root affinity we keep on looking to see if
2516 // there is a better match in another stack. We eventually return the match based
2517 // on root affinity if we don't find a better match.
2518 if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
2519 return mTmpFindTaskResult.r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002520 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002521 }
2522 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002523 if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
2524 return mTmpFindTaskResult.r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002525 }
2526
2527 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002528 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2529 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002530 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2531 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2532 if (ar != null) {
2533 return ar;
2534 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002535 }
2536 }
2537 return null;
2538 }
2539
Craig Mautner8d341ef2013-03-26 09:03:27 -07002540 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002541 scheduleSleepTimeout();
2542 if (!mGoingToSleep.isHeld()) {
2543 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002544 if (mLaunchingActivity.isHeld()) {
2545 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2546 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002547 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002548 mLaunchingActivity.release();
2549 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002550 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002551 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002552 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002553 }
2554
2555 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002556 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002557
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002558 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002559 final long endTime = System.currentTimeMillis() + timeout;
2560 while (true) {
2561 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002562 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2563 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002564 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2565 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2566 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002567 }
2568 if (cantShutdown) {
2569 long timeRemaining = endTime - System.currentTimeMillis();
2570 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002571 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002572 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002573 } catch (InterruptedException e) {
2574 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002575 } else {
2576 Slog.w(TAG, "Activity manager shutdown timed out");
2577 timedout = true;
2578 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002579 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002580 } else {
2581 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002582 }
2583 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002584
2585 // Force checkReadyForSleep to complete.
2586 mSleepTimeout = true;
2587 checkReadyForSleepLocked();
2588
Craig Mautner8d341ef2013-03-26 09:03:27 -07002589 return timedout;
2590 }
2591
2592 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002593 removeSleepTimeouts();
2594 if (mGoingToSleep.isHeld()) {
2595 mGoingToSleep.release();
2596 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002597 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2598 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002599 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2600 final ActivityStack stack = stacks.get(stackNdx);
2601 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002602 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002603 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002604 }
Craig Mautner5314a402013-09-26 12:40:16 -07002605 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002606 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002607 mGoingToSleepActivities.clear();
2608 }
2609
2610 void activitySleptLocked(ActivityRecord r) {
2611 mGoingToSleepActivities.remove(r);
2612 checkReadyForSleepLocked();
2613 }
2614
2615 void checkReadyForSleepLocked() {
2616 if (!mService.isSleepingOrShuttingDown()) {
2617 // Do not care.
2618 return;
2619 }
2620
2621 if (!mSleepTimeout) {
2622 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002623 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2624 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002625 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2626 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2627 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002628 }
2629
2630 if (mStoppingActivities.size() > 0) {
2631 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002632 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002633 + mStoppingActivities.size() + " activities");
2634 scheduleIdleLocked();
2635 dontSleep = true;
2636 }
2637
2638 if (mGoingToSleepActivities.size() > 0) {
2639 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002640 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002641 + mGoingToSleepActivities.size() + " activities");
2642 dontSleep = true;
2643 }
2644
2645 if (dontSleep) {
2646 return;
2647 }
2648 }
2649
Craig Mautnere0a38842013-12-16 16:14:02 -08002650 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2651 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002652 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2653 stacks.get(stackNdx).goToSleep();
2654 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002655 }
2656
2657 removeSleepTimeouts();
2658
2659 if (mGoingToSleep.isHeld()) {
2660 mGoingToSleep.release();
2661 }
2662 if (mService.mShuttingDown) {
2663 mService.notifyAll();
2664 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002665 }
2666
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002667 boolean reportResumedActivityLocked(ActivityRecord r) {
2668 final ActivityStack stack = r.task.stack;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002669 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002670 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002671 }
2672 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002673 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002674 mWindowManager.executeAppTransition();
2675 return true;
2676 }
2677 return false;
2678 }
2679
Craig Mautner8d341ef2013-03-26 09:03:27 -07002680 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002681 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2682 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07002683 int stackNdx = stacks.size() - 1;
2684 while (stackNdx >= 0) {
2685 stacks.get(stackNdx).handleAppCrashLocked(app);
2686 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002687 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002688 }
2689 }
2690
Jose Lima4b6c6692014-08-12 17:41:12 -07002691 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002692 final ActivityStack stack = r.task.stack;
2693 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002694 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2695 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002696 return false;
2697 }
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002698
2699 if (visible && !StackId.activitiesCanRequestVisibleBehind(stack.mStackId)) {
2700 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: r=" + r
2701 + " visible=" + visible + " stackId=" + stack.mStackId
2702 + " can't contain visible behind activities");
2703 return false;
2704 }
2705
Jose Lima4b6c6692014-08-12 17:41:12 -07002706 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002707 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2708 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002709
2710 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002711 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002712 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07002713 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002714 return true;
2715 }
2716
2717 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07002718 if (visible && top.fullscreen) {
2719 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002720 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2721 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
2722 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
2723 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002724 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07002725 } else if (!visible && stack.getVisibleBehindActivity() != r) {
2726 // Only the activity set as currently visible behind should actively reset its
2727 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002728 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2729 "requestVisibleBehind: returning visible=" + visible
2730 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
2731 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07002732 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002733 }
2734
Jose Lima4b6c6692014-08-12 17:41:12 -07002735 stack.setVisibleBehindActivity(visible ? r : null);
2736 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002737 // If there is a translucent home activity, we need to force it stop being translucent,
2738 // because we can't depend on the application to necessarily perform that operation.
2739 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002740 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002741 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002742 mService.convertFromTranslucent(next.appToken);
2743 }
2744 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07002745 if (top.app != null && top.app.thread != null) {
2746 // Notify the top app of the change.
2747 try {
2748 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
2749 } catch (RemoteException e) {
2750 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07002751 }
2752 return true;
2753 }
2754
Craig Mautnerbb742462014-07-07 15:28:55 -07002755 // Called when WindowManager has finished animating the launchingBehind activity to the back.
2756 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Craig Mautnerbb742462014-07-07 15:28:55 -07002757 final TaskRecord task = r.task;
Winson730bf062016-03-31 18:04:56 -07002758 final ActivityStack stack = task.stack;
2759
2760 r.mLaunchTaskBehind = false;
2761 task.setLastThumbnailLocked(stack.screenshotActivitiesLocked(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002762 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08002763 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07002764 mWindowManager.setAppVisibility(r.appToken, false);
Winson730bf062016-03-31 18:04:56 -07002765
2766 // When launching tasks behind, update the last active time of the top task after the new
2767 // task has been shown briefly
2768 final ActivityRecord top = stack.topActivity();
2769 if (top != null) {
2770 top.task.touchActiveTime();
2771 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002772 }
2773
2774 void scheduleLaunchTaskBehindComplete(IBinder token) {
2775 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2776 }
2777
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002778 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
2779 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07002780 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002781 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2782 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002783 final int topStackNdx = stacks.size() - 1;
2784 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2785 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002786 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07002787 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002788 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002789 }
2790
Chong Zhangfdcc4d42015-10-14 16:50:12 -07002791 void invalidateTaskLayers() {
2792 mTaskLayersChanged = true;
2793 }
2794
2795 void rankTaskLayersIfNeeded() {
2796 if (!mTaskLayersChanged) {
2797 return;
2798 }
2799 mTaskLayersChanged = false;
2800 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
2801 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2802 int baseLayer = 0;
2803 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2804 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
2805 }
2806 }
2807 }
2808
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002809 void clearOtherAppTimeTrackers(AppTimeTracker except) {
2810 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2811 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2812 final int topStackNdx = stacks.size() - 1;
2813 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2814 final ActivityStack stack = stacks.get(stackNdx);
2815 stack.clearOtherAppTimeTrackers(except);
2816 }
2817 }
2818 }
2819
Craig Mautner8d341ef2013-03-26 09:03:27 -07002820 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002821 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2822 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002823 final int numStacks = stacks.size();
2824 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2825 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002826 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002827 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002828 }
2829 }
2830
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002831 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
2832 // Examine all activities currently running in the process.
2833 TaskRecord firstTask = null;
2834 // Tasks is non-null only if two or more tasks are found.
2835 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002836 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
2837 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002838 ActivityRecord r = app.activities.get(i);
2839 // First, if we find an activity that is in the process of being destroyed,
2840 // then we just aren't going to do anything for now; we want things to settle
2841 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002842 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002843 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002844 return;
2845 }
2846 // Don't consider any activies that are currently not in a state where they
2847 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002848 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
2849 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002850 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002851 continue;
2852 }
2853 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002854 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002855 + " from " + r);
2856 if (firstTask == null) {
2857 firstTask = r.task;
2858 } else if (firstTask != r.task) {
2859 if (tasks == null) {
2860 tasks = new ArraySet<>();
2861 tasks.add(firstTask);
2862 }
2863 tasks.add(r.task);
2864 }
2865 }
2866 }
2867 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002868 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002869 return;
2870 }
2871 // If we have activities in multiple tasks that are in a position to be destroyed,
2872 // let's iterate through the tasks and release the oldest one.
2873 final int numDisplays = mActivityDisplays.size();
2874 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
2875 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2876 // Step through all stacks starting from behind, to hit the oldest things first.
2877 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
2878 final ActivityStack stack = stacks.get(stackNdx);
2879 // Try to release activities in this stack; if we manage to, we are done.
2880 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
2881 return;
2882 }
2883 }
2884 }
2885 }
2886
Amith Yamasani37a40c22015-06-17 13:25:42 -07002887 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002888 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002889 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07002890 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002891
Craig Mautner858d8a62013-04-23 17:08:34 -07002892 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08002893 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2894 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002895 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002896 final ActivityStack stack = stacks.get(stackNdx);
2897 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002898 TaskRecord task = stack.topTask();
2899 if (task != null) {
2900 mWindowManager.moveTaskToTop(task.taskId);
2901 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002902 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07002903 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002904
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002905 ActivityStack stack = getStack(restoreStackId);
2906 if (stack == null) {
2907 stack = mHomeStack;
2908 }
2909 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002910 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07002911 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08002912 } else {
2913 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08002914 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08002915 }
Craig Mautner93529a42013-10-04 15:03:13 -07002916 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07002917 }
2918
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002919 /** Checks whether the userid is a profile of the current user. */
2920 boolean isCurrentProfileLocked(int userId) {
2921 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002922 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002923 }
2924
Chong Zhang45c25ce2015-08-10 22:18:26 -07002925 /** Checks whether the activity should be shown for current user. */
2926 boolean okToShowLocked(ActivityRecord r) {
2927 return r != null && (isCurrentProfileLocked(r.userId)
2928 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
2929 }
2930
Craig Mautnerde4ef022013-04-07 19:01:33 -07002931 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002932 ArrayList<ActivityRecord> stops = null;
2933
2934 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08002935 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2936 ActivityRecord s = mStoppingActivities.get(activityNdx);
2937 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002938 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08002939 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
2940 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002941 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002942 if (s.finishing) {
2943 // If this activity is finishing, it is sitting on top of
2944 // everyone else but we now know it is no longer needed...
2945 // so get rid of it. Otherwise, we need to go through the
2946 // normal flow and hide it once we determine that it is
2947 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002948 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002949 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002950 }
2951 }
Craig Mautner8c14c152015-01-15 17:32:07 -08002952 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002953 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002954 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08002955 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002956 }
2957 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08002958 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002959 }
2960 }
2961
2962 return stops;
2963 }
2964
Craig Mautnercf910b02013-04-23 11:23:27 -07002965 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002966 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2967 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2968 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2969 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002970 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002971 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002972 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002973 if (r == null) Slog.e(TAG,
2974 "validateTop...: null top activity, stack=" + stack);
2975 else {
2976 final ActivityRecord pausing = stack.mPausingActivity;
2977 if (pausing != null && pausing == r) Slog.e(TAG,
2978 "validateTop...: top stack has pausing activity r=" + r
2979 + " state=" + state);
2980 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
2981 "validateTop...: activity in front not resumed r=" + r
2982 + " state=" + state);
2983 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002984 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002985 final ActivityRecord resumed = stack.mResumedActivity;
2986 if (resumed != null && resumed == r) Slog.e(TAG,
2987 "validateTop...: back stack has resumed activity r=" + r
2988 + " state=" + state);
2989 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
2990 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002991 }
2992 }
2993 }
Craig Mautner76ea2242013-05-15 11:40:05 -07002994 }
2995
Craig Mautnere0570202015-05-13 13:06:11 -07002996 private String lockTaskModeToString() {
2997 switch (mLockTaskModeState) {
2998 case LOCK_TASK_MODE_LOCKED:
2999 return "LOCKED";
3000 case LOCK_TASK_MODE_PINNED:
3001 return "PINNED";
3002 case LOCK_TASK_MODE_NONE:
3003 return "NONE";
3004 default: return "unknown=" + mLockTaskModeState;
3005 }
3006 }
3007
Craig Mautner27084302013-03-25 08:05:25 -07003008 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003009 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003010 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003011 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003012 pw.print(prefix);
3013 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003014 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003015 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003016 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
3017 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3018 if (packages.size() > 0) {
3019 pw.println(" mLockTaskPackages (userId:packages)=");
3020 for (int i = 0; i < packages.size(); ++i) {
3021 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3022 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3023 }
3024 }
3025 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003026 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003027
Craig Mautner20e72272013-04-01 13:45:53 -07003028 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003029 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003030 }
3031
Dianne Hackborn390517b2013-05-30 15:03:32 -07003032 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3033 boolean needSep, String prefix) {
3034 if (activity != null) {
3035 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3036 if (needSep) {
3037 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003038 }
3039 pw.print(prefix);
3040 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003041 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003042 }
3043 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003044 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003045 }
3046
Craig Mautner8d341ef2013-03-26 09:03:27 -07003047 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3048 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003049 boolean printed = false;
3050 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003051 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3052 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003053 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003054 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003055 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003056 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003057 final ActivityStack stack = stacks.get(stackNdx);
3058 StringBuilder stackHeader = new StringBuilder(128);
3059 stackHeader.append(" Stack #");
3060 stackHeader.append(stack.mStackId);
3061 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003062 stackHeader.append("\n");
3063 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3064 stackHeader.append("\n");
3065 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003066 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3067 needSep, stackHeader.toString());
3068 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3069 !dumpAll, false, dumpPackage, true,
3070 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003071
Craig Mautner4a1cb222013-12-04 16:14:06 -08003072 needSep = printed;
3073 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3074 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003075 if (pr) {
3076 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003077 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003078 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003079 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3080 " mResumedActivity: ");
3081 if (pr) {
3082 printed = true;
3083 needSep = false;
3084 }
3085 if (dumpAll) {
3086 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3087 " mLastPausedActivity: ");
3088 if (pr) {
3089 printed = true;
3090 needSep = true;
3091 }
3092 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3093 needSep, " mLastNoHistoryActivity: ");
3094 }
3095 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003096 }
3097 }
3098
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003099 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3100 false, dumpPackage, true, " Activities waiting to finish:", null);
3101 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3102 false, dumpPackage, true, " Activities waiting to stop:", null);
3103 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3104 false, dumpPackage, true, " Activities waiting for another to become visible:",
3105 null);
3106 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3107 false, dumpPackage, true, " Activities waiting to sleep:", null);
3108 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3109 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003110
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003111 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003112 }
3113
Dianne Hackborn390517b2013-05-30 15:03:32 -07003114 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003115 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003116 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003117 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003118 String innerPrefix = null;
3119 String[] args = null;
3120 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003121 for (int i=list.size()-1; i>=0; i--) {
3122 final ActivityRecord r = list.get(i);
3123 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3124 continue;
3125 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003126 if (innerPrefix == null) {
3127 innerPrefix = prefix + " ";
3128 args = new String[0];
3129 }
3130 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003131 final boolean full = !brief && (complete || !r.isInHistory());
3132 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003133 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003134 needNL = false;
3135 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003136 if (header1 != null) {
3137 pw.println(header1);
3138 header1 = null;
3139 }
3140 if (header2 != null) {
3141 pw.println(header2);
3142 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003143 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003144 if (lastTask != r.task) {
3145 lastTask = r.task;
3146 pw.print(prefix);
3147 pw.print(full ? "* " : " ");
3148 pw.println(lastTask);
3149 if (full) {
3150 lastTask.dump(pw, prefix + " ");
3151 } else if (complete) {
3152 // Complete + brief == give a summary. Isn't that obvious?!?
3153 if (lastTask.intent != null) {
3154 pw.print(prefix); pw.print(" ");
3155 pw.println(lastTask.intent.toInsecureStringWithClip());
3156 }
3157 }
3158 }
3159 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3160 pw.print(" #"); pw.print(i); pw.print(": ");
3161 pw.println(r);
3162 if (full) {
3163 r.dump(pw, innerPrefix);
3164 } else if (complete) {
3165 // Complete + brief == give a summary. Isn't that obvious?!?
3166 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3167 if (r.app != null) {
3168 pw.print(innerPrefix); pw.println(r.app);
3169 }
3170 }
3171 if (client && r.app != null && r.app.thread != null) {
3172 // flush anything that is already in the PrintWriter since the thread is going
3173 // to write to the file descriptor directly
3174 pw.flush();
3175 try {
3176 TransferPipe tp = new TransferPipe();
3177 try {
3178 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3179 r.appToken, innerPrefix, args);
3180 // Short timeout, since blocking here can
3181 // deadlock with the application.
3182 tp.go(fd, 2000);
3183 } finally {
3184 tp.kill();
3185 }
3186 } catch (IOException e) {
3187 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3188 } catch (RemoteException e) {
3189 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3190 }
3191 needNL = true;
3192 }
3193 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003194 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003195 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003196
Craig Mautnerf3333272013-04-22 10:55:53 -07003197 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003198 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3199 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003200 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3201 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003202 }
3203
3204 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003205 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003206 }
3207
3208 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003209 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3210 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003211 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3212 }
3213
Craig Mautner05d29032013-05-03 13:40:13 -07003214 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003215 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3216 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3217 }
Craig Mautner05d29032013-05-03 13:40:13 -07003218 }
3219
Craig Mautner0eea92c2013-05-16 13:35:39 -07003220 void removeSleepTimeouts() {
3221 mSleepTimeout = false;
3222 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3223 }
3224
3225 final void scheduleSleepTimeout() {
3226 removeSleepTimeouts();
3227 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3228 }
3229
Craig Mautner4a1cb222013-12-04 16:14:06 -08003230 @Override
3231 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003232 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003233 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3234 }
3235
3236 @Override
3237 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003238 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003239 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3240 }
3241
3242 @Override
3243 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003244 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003245 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3246 }
3247
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003248 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003249 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003250 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003251 newDisplay = mActivityDisplays.get(displayId) == null;
3252 if (newDisplay) {
3253 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003254 if (activityDisplay.mDisplay == null) {
3255 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3256 return;
3257 }
Craig Mautner4504de52013-12-20 09:06:56 -08003258 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003259 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08003260 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003261 }
Craig Mautner4504de52013-12-20 09:06:56 -08003262 if (newDisplay) {
3263 mWindowManager.onDisplayAdded(displayId);
3264 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003265 }
3266
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003267 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Jorim Jaggi19cf2972016-04-07 23:26:10 -07003268 mDefaultMinimalSizeOfResizeableTask =
3269 mService.mContext.getResources().getDimensionPixelSize(
3270 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003271 }
3272
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003273 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003274 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003275 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3276 if (activityDisplay != null) {
3277 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003278 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003279 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003280 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003281 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003282 }
3283 }
3284 mWindowManager.onDisplayRemoved(displayId);
3285 }
3286
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003287 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003288 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003289 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3290 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003291 // TODO: Update the bounds.
3292 }
3293 }
3294 mWindowManager.onDisplayChanged(displayId);
3295 }
3296
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003297 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003298 StackInfo info = new StackInfo();
3299 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3300 info.displayId = Display.DEFAULT_DISPLAY;
3301 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003302 info.userId = stack.mCurrentUser;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003303
3304 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3305 final int numTasks = tasks.size();
3306 int[] taskIds = new int[numTasks];
3307 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003308 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003309 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003310 for (int i = 0; i < numTasks; ++i) {
3311 final TaskRecord task = tasks.get(i);
3312 taskIds[i] = task.taskId;
3313 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3314 : task.realActivity != null ? task.realActivity.flattenToString()
3315 : task.getTopActivity() != null ? task.getTopActivity().packageName
3316 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003317 taskBounds[i] = new Rect();
3318 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003319 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003320 }
3321 info.taskIds = taskIds;
3322 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003323 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003324 info.taskUserIds = taskUserIds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003325 return info;
3326 }
3327
3328 StackInfo getStackInfoLocked(int stackId) {
3329 ActivityStack stack = getStack(stackId);
3330 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003331 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003332 }
3333 return null;
3334 }
3335
3336 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003337 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003338 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3339 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003340 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003341 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003342 }
3343 }
3344 return list;
3345 }
3346
Craig Mautner15df08a2015-04-01 12:17:18 -07003347 TaskRecord getLockedTaskLocked() {
3348 final int top = mLockTaskModeTasks.size() - 1;
3349 if (top >= 0) {
3350 return mLockTaskModeTasks.get(top);
3351 }
3352 return null;
3353 }
3354
3355 boolean isLockedTask(TaskRecord task) {
3356 return mLockTaskModeTasks.contains(task);
3357 }
3358
3359 boolean isLastLockedTask(TaskRecord task) {
3360 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3361 }
3362
3363 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003364 if (!mLockTaskModeTasks.remove(task)) {
3365 return;
3366 }
3367 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3368 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003369 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003370 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3371 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003372 final Message lockTaskMsg = Message.obtain();
3373 lockTaskMsg.arg1 = task.userId;
3374 lockTaskMsg.what = LOCK_TASK_END_MSG;
3375 mHandler.sendMessage(lockTaskMsg);
3376 }
3377 }
3378
Andrii Kulianc27916642016-04-12 17:59:27 -07003379 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId, int actualStackId) {
3380 handleNonResizableTaskIfNeeded(task, preferredStackId, actualStackId,
3381 false /* forceNonResizable */);
3382 }
3383
Jorim Jaggid53f0922016-04-06 22:16:23 -07003384 void handleNonResizableTaskIfNeeded(
Andrii Kulianc27916642016-04-12 17:59:27 -07003385 TaskRecord task, int preferredStackId, int actualStackId, boolean forceNonResizable) {
Jorim Jaggid53f0922016-04-06 22:16:23 -07003386 if ((!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID)
3387 || task.isHomeTask()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003388 return;
3389 }
3390
Andrii Kulianc27916642016-04-12 17:59:27 -07003391 if (!task.canGoInDockedStack() || forceNonResizable) {
Jorim Jaggi2adba072016-03-03 13:43:39 +01003392 // Display a warning toast that we tried to put a non-dockable task in the docked stack.
Jorim Jaggid53f0922016-04-06 22:16:23 -07003393 mService.mHandler.sendEmptyMessage(NOTIFY_ACTIVITY_DISMISSING_DOCKED_STACK_MSG);
3394
Andrii Kulianc27916642016-04-12 17:59:27 -07003395 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
3396 // we need to move it to top of fullscreen stack, otherwise it will be covered.
3397 mService.moveTasksToFullscreenStack(DOCKED_STACK_ID, actualStackId == DOCKED_STACK_ID);
Jorim Jaggi2adba072016-03-03 13:43:39 +01003398 } else if (task.mResizeMode == RESIZE_MODE_FORCE_RESIZEABLE) {
3399 String packageName = task.getTopActivity() != null
3400 ? task.getTopActivity().appInfo.packageName : null;
3401 mService.mHandler.obtainMessage(NOTIFY_FORCED_RESIZABLE_MSG, task.taskId, 0,
3402 packageName).sendToTarget();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003403 }
Chong Zhangb15758a2015-11-17 12:12:03 -08003404 }
3405
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003406 void showLockTaskToast() {
3407 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04003408 }
3409
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003410 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3411 if (mLockTaskModeTasks.contains(task)) {
3412 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3413 }
3414 }
3415
Craig Mautner432f64e2015-05-20 14:59:57 -07003416 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3417 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003418 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003419 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003420 final TaskRecord lockedTask = getLockedTaskLocked();
3421 if (lockedTask != null) {
3422 removeLockedTaskLocked(lockedTask);
3423 if (!mLockTaskModeTasks.isEmpty()) {
3424 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003425 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3426 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003427 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003428 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003429 return;
3430 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003431 }
Craig Mautnere0570202015-05-13 13:06:11 -07003432 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3433 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003434 return;
3435 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003436
3437 // Should have already been checked, but do it again.
3438 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003439 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3440 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003441 return;
3442 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003443 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003444 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003445 return;
3446 }
3447
3448 if (mLockTaskModeTasks.isEmpty()) {
3449 // First locktask.
3450 final Message lockTaskMsg = Message.obtain();
3451 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3452 lockTaskMsg.arg1 = task.userId;
3453 lockTaskMsg.what = LOCK_TASK_START_MSG;
3454 lockTaskMsg.arg2 = lockTaskModeState;
3455 mHandler.sendMessage(lockTaskMsg);
3456 }
3457 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003458 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3459 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003460 mLockTaskModeTasks.remove(task);
3461 mLockTaskModeTasks.add(task);
3462
3463 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07003464 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07003465 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003466
3467 if (andResume) {
Andrii Kulianc27916642016-04-12 17:59:27 -07003468 findTaskToMoveToFrontLocked(task, 0, null, reason,
3469 lockTaskModeState != LOCK_TASK_MODE_NONE);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003470 resumeFocusedStackTopActivityLocked();
Andrii Kulianc27916642016-04-12 17:59:27 -07003471 } else if (lockTaskModeState != LOCK_TASK_MODE_NONE) {
3472 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.stack.mStackId,
3473 true /* forceNonResizable */);
Craig Mautner432f64e2015-05-20 14:59:57 -07003474 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003475 }
3476
3477 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003478 return isLockTaskModeViolation(task, false);
3479 }
3480
3481 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3482 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003483 return false;
3484 }
3485 final int lockTaskAuth = task.mLockTaskAuth;
3486 switch (lockTaskAuth) {
3487 case LOCK_TASK_AUTH_DONT_LOCK:
3488 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003489 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003490 case LOCK_TASK_AUTH_LAUNCHABLE:
3491 case LOCK_TASK_AUTH_WHITELISTED:
3492 return false;
3493 case LOCK_TASK_AUTH_PINNABLE:
3494 // Pinnable tasks can't be launched on top of locktask tasks.
3495 return !mLockTaskModeTasks.isEmpty();
3496 default:
3497 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3498 return true;
3499 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003500 }
3501
Craig Mautner15df08a2015-04-01 12:17:18 -07003502 void onLockTaskPackagesUpdatedLocked() {
3503 boolean didSomething = false;
3504 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3505 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01003506 final boolean wasWhitelisted =
3507 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3508 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07003509 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01003510 final boolean isWhitelisted =
3511 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3512 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3513 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003514 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003515 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3516 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003517 removeLockedTaskLocked(lockedTask);
3518 lockedTask.performClearTaskLocked();
3519 didSomething = true;
3520 }
3521 }
3522 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3523 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3524 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3525 final ActivityStack stack = stacks.get(stackNdx);
3526 stack.onLockTaskPackagesUpdatedLocked();
3527 }
3528 }
Craig Mautnere0570202015-05-13 13:06:11 -07003529 final ActivityRecord r = topRunningActivityLocked();
3530 final TaskRecord task = r != null ? r.task : null;
3531 if (mLockTaskModeTasks.isEmpty() && task != null
3532 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3533 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07003534 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
3535 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
3536 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
3537 false);
3538 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07003539 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003540 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003541 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003542 }
3543 }
3544
Benjamin Franz43261142015-02-11 15:59:44 +00003545 int getLockTaskModeState() {
3546 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003547 }
3548
Jorim Jaggife89d122015-12-22 16:28:44 +01003549 void activityRelaunchedLocked(IBinder token) {
3550 mWindowManager.notifyAppRelaunchingFinished(token);
3551 }
3552
3553 void activityRelaunchingLocked(ActivityRecord r) {
3554 mWindowManager.notifyAppRelaunching(r.appToken);
3555 }
3556
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003557 void logStackState() {
3558 mActivityMetricsLogger.logWindowState();
3559 }
3560
Andrii Kulian933076d2016-03-29 17:04:42 -07003561 void scheduleReportMultiWindowModeChanged(TaskRecord task) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003562 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3563 final ActivityRecord r = task.mActivities.get(i);
3564 if (r.app != null && r.app.thread != null) {
3565 mMultiWindowModeChangedActivities.add(r);
3566 }
3567 }
3568
3569 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
3570 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
3571 }
3572 }
3573
Andrii Kulian933076d2016-03-29 17:04:42 -07003574 void scheduleReportPictureInPictureModeChangedIfNeeded(TaskRecord task, ActivityStack prevStack) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003575 final ActivityStack stack = task.stack;
3576 if (prevStack == null || prevStack == stack
3577 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
3578 return;
3579 }
3580
3581 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3582 final ActivityRecord r = task.mActivities.get(i);
3583 if (r.app != null && r.app.thread != null) {
3584 mPipModeChangedActivities.add(r);
3585 }
3586 }
3587
3588 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
3589 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
3590 }
3591 }
3592
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003593 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003594
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003595 public ActivityStackSupervisorHandler(Looper looper) {
3596 super(looper);
3597 }
3598
Craig Mautnerf3333272013-04-22 10:55:53 -07003599 void activityIdleInternal(ActivityRecord r) {
3600 synchronized (mService) {
3601 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3602 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003603 }
3604
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003605 @Override
3606 public void handleMessage(Message msg) {
3607 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003608 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
3609 synchronized (mService) {
3610 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
3611 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07003612 r.scheduleMultiWindowModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003613 }
3614 }
3615 } break;
3616 case REPORT_PIP_MODE_CHANGED_MSG: {
3617 synchronized (mService) {
3618 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
3619 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07003620 r.schedulePictureInPictureModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003621 }
3622 }
3623 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07003624 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003625 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3626 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003627 if (mService.mDidDexOpt) {
3628 mService.mDidDexOpt = false;
3629 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3630 nmsg.obj = msg.obj;
3631 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3632 return;
3633 }
3634 // We don't at this point know if the activity is fullscreen,
3635 // so we need to be conservative and assume it isn't.
3636 activityIdleInternal((ActivityRecord)msg.obj);
3637 } break;
3638 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003639 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003640 activityIdleInternal((ActivityRecord)msg.obj);
3641 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003642 case RESUME_TOP_ACTIVITY_MSG: {
3643 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003644 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07003645 }
3646 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003647 case SLEEP_TIMEOUT_MSG: {
3648 synchronized (mService) {
3649 if (mService.isSleepingOrShuttingDown()) {
3650 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3651 mSleepTimeout = true;
3652 checkReadyForSleepLocked();
3653 }
3654 }
3655 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003656 case LAUNCH_TIMEOUT_MSG: {
3657 if (mService.mDidDexOpt) {
3658 mService.mDidDexOpt = false;
3659 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3660 return;
3661 }
3662 synchronized (mService) {
3663 if (mLaunchingActivity.isHeld()) {
3664 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3665 if (VALIDATE_WAKE_LOCK_CALLER
3666 && Binder.getCallingUid() != Process.myUid()) {
3667 throw new IllegalStateException("Calling must be system uid");
3668 }
3669 mLaunchingActivity.release();
3670 }
3671 }
3672 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003673 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003674 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003675 } break;
3676 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003677 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003678 } break;
3679 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003680 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003681 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003682 case CONTAINER_CALLBACK_VISIBILITY: {
3683 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003684 final IActivityContainerCallback callback = container.mCallback;
3685 if (callback != null) {
3686 try {
3687 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3688 } catch (RemoteException e) {
3689 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003690 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003691 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003692 case LOCK_TASK_START_MSG: {
3693 // When lock task starts, we disable the status bars.
3694 try {
Jason Monk62515be2014-05-21 16:06:19 -04003695 if (mLockTaskNotify == null) {
3696 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003697 }
Jason Monk62515be2014-05-21 16:06:19 -04003698 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003699 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003700 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003701 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003702 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003703 flags = StatusBarManager.DISABLE_MASK
3704 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07003705 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003706 flags = StatusBarManager.DISABLE_MASK
3707 & (~StatusBarManager.DISABLE_BACK)
3708 & (~StatusBarManager.DISABLE_HOME)
3709 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003710 }
3711 getStatusBarService().disable(flags, mToken,
3712 mService.mContext.getPackageName());
3713 }
3714 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003715 if (getDevicePolicyManager() != null) {
3716 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003717 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003718 }
justinzhang5286d3f2014-05-12 17:06:01 -04003719 } catch (RemoteException ex) {
3720 throw new RuntimeException(ex);
3721 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003722 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003723 case LOCK_TASK_END_MSG: {
3724 // When lock task ends, we enable the status bars.
3725 try {
Jason Monk62515be2014-05-21 16:06:19 -04003726 if (getStatusBarService() != null) {
3727 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3728 mService.mContext.getPackageName());
3729 }
3730 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003731 if (getDevicePolicyManager() != null) {
3732 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3733 msg.arg1);
3734 }
Jason Monk62515be2014-05-21 16:06:19 -04003735 if (mLockTaskNotify == null) {
3736 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3737 }
3738 mLockTaskNotify.show(false);
3739 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003740 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003741 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003742 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003743 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003744 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003745 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003746 new LockPatternUtils(mService.mContext)
3747 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003748 }
3749 } catch (SettingNotFoundException e) {
3750 // No setting, don't lock.
3751 }
justinzhang5286d3f2014-05-12 17:06:01 -04003752 } catch (RemoteException ex) {
3753 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003754 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07003755 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003756 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003757 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003758 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
3759 if (mLockTaskNotify == null) {
3760 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3761 }
3762 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
3763 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003764 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3765 final ActivityContainer container = (ActivityContainer) msg.obj;
3766 final IActivityContainerCallback callback = container.mCallback;
3767 if (callback != null) {
3768 try {
3769 callback.onAllActivitiesComplete(container.asBinder());
3770 } catch (RemoteException e) {
3771 }
3772 }
3773 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003774 case LAUNCH_TASK_BEHIND_COMPLETE: {
3775 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003776 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07003777 if (r != null) {
3778 handleLaunchTaskBehindCompleteLocked(r);
3779 }
3780 }
3781 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08003782
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003783 }
3784 }
3785 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003786
Ying Wangb081a592014-04-22 15:20:16 -07003787 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08003788 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
3789 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003790 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003791 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003792 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003793 ActivityRecord mParentActivity = null;
3794 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003795
Craig Mautnere3a00d72014-04-16 08:31:19 -07003796 boolean mVisible = true;
3797
Craig Mautner4a1cb222013-12-04 16:14:06 -08003798 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003799 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003800
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003801 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3802 final static int CONTAINER_STATE_NO_SURFACE = 1;
3803 final static int CONTAINER_STATE_FINISHING = 2;
3804 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3805
3806 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003807 synchronized (mService) {
3808 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003809 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003810 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003811 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003812 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003813 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003814
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003815 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003816 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003817 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08003818 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003819 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003820 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003821 }
3822
3823 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003824 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003825 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003826 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3827 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003828 return;
3829 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003830 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003831 }
3832 }
3833
3834 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003835 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003836 synchronized (mService) {
3837 if (mActivityDisplay != null) {
3838 return mActivityDisplay.mDisplayId;
3839 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003840 }
3841 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003842 }
3843
Jeff Brownca9bc702014-02-11 14:32:56 -08003844 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08003845 public int getStackId() {
3846 synchronized (mService) {
3847 return mStackId;
3848 }
3849 }
3850
3851 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003852 public boolean injectEvent(InputEvent event) {
3853 final long origId = Binder.clearCallingIdentity();
3854 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003855 synchronized (mService) {
3856 if (mActivityDisplay != null) {
3857 return mInputManagerInternal.injectInputEvent(event,
3858 mActivityDisplay.mDisplayId,
3859 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3860 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003861 }
3862 return false;
3863 } finally {
3864 Binder.restoreCallingIdentity(origId);
3865 }
3866 }
3867
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003868 @Override
3869 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003870 synchronized (mService) {
3871 if (mContainerState == CONTAINER_STATE_FINISHING) {
3872 return;
3873 }
3874 mContainerState = CONTAINER_STATE_FINISHING;
3875
Craig Mautnerd163e752014-06-13 17:18:47 -07003876 long origId = Binder.clearCallingIdentity();
3877 try {
Craig Mautneree36c772014-07-16 14:56:05 -07003878 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003879 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07003880 } finally {
3881 Binder.restoreCallingIdentity(origId);
3882 }
3883 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003884 }
3885
Craig Mautner60257702014-09-17 15:02:33 -07003886 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003887 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08003888 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08003889 if (mActivityDisplay != null) {
3890 mActivityDisplay.detachActivitiesLocked(mStack);
3891 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003892 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003893 }
3894 }
3895
3896 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08003897 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003898 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003899 }
3900
3901 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07003902 public final int startActivityIntentSender(IIntentSender intentSender)
3903 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003904 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3905
3906 if (!(intentSender instanceof PendingIntentRecord)) {
3907 throw new IllegalArgumentException("Bad PendingIntent object");
3908 }
3909
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07003910 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003911 Binder.getCallingUid(), mCurrentUser, false,
3912 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08003913
3914 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3915 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
3916 pendingIntent.key.requestResolvedType);
3917
3918 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
3919 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
3920 }
3921
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003922 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07003923 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003924 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003925 throw new SecurityException(
3926 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3927 }
3928 }
3929
Craig Mautnerdf88d732014-01-27 09:21:32 -08003930 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003931 public IBinder asBinder() {
3932 return this;
3933 }
3934
Craig Mautner4504de52013-12-20 09:06:56 -08003935 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003936 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003937 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08003938 }
3939
Craig Mautner4a1cb222013-12-04 16:14:06 -08003940 ActivityStackSupervisor getOuter() {
3941 return ActivityStackSupervisor.this;
3942 }
3943
Craig Mautnerd163e752014-06-13 17:18:47 -07003944 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003945 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003946 }
3947
Craig Mautner6985bad2014-04-21 15:22:06 -07003948 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07003949 void setVisible(boolean visible) {
3950 if (mVisible != visible) {
3951 mVisible = visible;
3952 if (mCallback != null) {
3953 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
3954 0 /* unused */, this).sendToTarget();
3955 }
3956 }
3957 }
3958
Craig Mautner6985bad2014-04-21 15:22:06 -07003959 void setDrawn() {
3960 }
3961
Craig Mautner1b4bf852014-05-26 15:06:32 -07003962 // You can always start a new task on a regular ActivityStack.
3963 boolean isEligibleForNewTasks() {
3964 return true;
3965 }
3966
Craig Mautnerd163e752014-06-13 17:18:47 -07003967 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07003968 detachLocked();
3969 deleteActivityContainer(this);
3970 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003971 }
3972
Craig Mautner34b73df2014-01-12 21:11:08 -08003973 @Override
3974 public String toString() {
3975 return mIdString + (mActivityDisplay == null ? "N" : "A");
3976 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003977 }
3978
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003979 private class VirtualActivityContainer extends ActivityContainer {
3980 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07003981 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003982
3983 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
3984 super(getNextStackId());
3985 mParentActivity = parent;
3986 mCallback = callback;
3987 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07003988 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003989 }
3990
3991 @Override
3992 public void setSurface(Surface surface, int width, int height, int density) {
3993 super.setSurface(surface, width, height, density);
3994
3995 synchronized (mService) {
3996 final long origId = Binder.clearCallingIdentity();
3997 try {
3998 setSurfaceLocked(surface, width, height, density);
3999 } finally {
4000 Binder.restoreCallingIdentity(origId);
4001 }
4002 }
4003 }
4004
4005 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4006 if (mContainerState == CONTAINER_STATE_FINISHING) {
4007 return;
4008 }
4009 VirtualActivityDisplay virtualActivityDisplay =
4010 (VirtualActivityDisplay) mActivityDisplay;
4011 if (virtualActivityDisplay == null) {
4012 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004013 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004014 mActivityDisplay = virtualActivityDisplay;
4015 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004016 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004017 }
4018
4019 if (mSurface != null) {
4020 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004021 }
4022
Craig Mautner6985bad2014-04-21 15:22:06 -07004023 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004024 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004025 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004026 } else {
4027 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004028 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004029 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004030 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004031 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004032 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004033
Craig Mautnerd163e752014-06-13 17:18:47 -07004034 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004035
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004036 if (DEBUG_STACK) Slog.d(TAG_STACK,
4037 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004038 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004039
Craig Mautner6985bad2014-04-21 15:22:06 -07004040 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004041 boolean isAttachedLocked() {
4042 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004043 }
4044
4045 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004046 void setDrawn() {
4047 synchronized (mService) {
4048 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004049 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004050 }
4051 }
4052
Craig Mautner1b4bf852014-05-26 15:06:32 -07004053 // Never start a new task on an ActivityView if it isn't explicitly specified.
4054 @Override
4055 boolean isEligibleForNewTasks() {
4056 return false;
4057 }
4058
Craig Mautnerd163e752014-06-13 17:18:47 -07004059 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004060 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004061 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4062 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4063 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4064 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4065 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004066 }
4067 }
4068
Craig Mautner4a1cb222013-12-04 16:14:06 -08004069 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4070 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004071 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004072 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004073 int mDisplayId;
4074 Display mDisplay;
4075 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004076
Craig Mautner4a1cb222013-12-04 16:14:06 -08004077 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4078 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004079 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004080
Jose Lima4b6c6692014-08-12 17:41:12 -07004081 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004082
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004083 ActivityDisplay() {
4084 }
Craig Mautner4504de52013-12-20 09:06:56 -08004085
Craig Mautner1a70a162014-09-13 12:09:31 -07004086 // After instantiation, check that mDisplay is not null before using this. The alternative
4087 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004088 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004089 final Display display = mDisplayManager.getDisplay(displayId);
4090 if (display == null) {
4091 return;
4092 }
4093 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004094 }
4095
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004096 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004097 mDisplay = display;
4098 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004099 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004100 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004101
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004102 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004103 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004104 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
4105 + " onTop=" + onTop);
4106 if (onTop) {
4107 mStacks.add(stack);
4108 } else {
4109 mStacks.add(0, stack);
4110 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004111 }
4112
Craig Mautnere0a38842013-12-16 16:14:02 -08004113 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004114 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004115 + " from displayId=" + mDisplayId);
4116 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004117 }
4118
Jose Lima4b6c6692014-08-12 17:41:12 -07004119 void setVisibleBehindActivity(ActivityRecord r) {
4120 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004121 }
4122
Jose Lima4b6c6692014-08-12 17:41:12 -07004123 boolean hasVisibleBehindActivity() {
4124 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004125 }
4126
Craig Mautner34b73df2014-01-12 21:11:08 -08004127 @Override
4128 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004129 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4130 }
4131 }
4132
4133 class VirtualActivityDisplay extends ActivityDisplay {
4134 VirtualDisplay mVirtualDisplay;
4135
Craig Mautner6985bad2014-04-21 15:22:06 -07004136 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004137 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004138 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4139 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4140 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4141 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004142
4143 init(mVirtualDisplay.getDisplay());
4144
4145 mWindowManager.handleDisplayAdded(mDisplayId);
4146 }
4147
4148 void setSurface(Surface surface) {
4149 if (mVirtualDisplay != null) {
4150 mVirtualDisplay.setSurface(surface);
4151 }
4152 }
4153
4154 @Override
4155 void detachActivitiesLocked(ActivityStack stack) {
4156 super.detachActivitiesLocked(stack);
4157 if (mVirtualDisplay != null) {
4158 mVirtualDisplay.release();
4159 mVirtualDisplay = null;
4160 }
4161 }
4162
4163 @Override
4164 public String toString() {
4165 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004166 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004167 }
Jose Lima58e66d62014-05-27 20:00:27 -07004168
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07004169 /**
4170 * Adjust bounds to stay within stack bounds.
4171 *
4172 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
4173 * that keep them unchanged, but be contained within the stack bounds.
4174 *
4175 * @param bounds Bounds to be adjusted.
4176 * @param stackBounds Bounds within which the other bounds should remain.
4177 */
4178 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
4179 if (stackBounds == null || stackBounds.contains(bounds)) {
4180 return;
4181 }
4182
4183 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
4184 final int maxRight = stackBounds.right
4185 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
4186 int horizontalDiff = stackBounds.left - bounds.left;
4187 if ((horizontalDiff < 0 && bounds.left >= maxRight)
4188 || (bounds.left + horizontalDiff >= maxRight)) {
4189 horizontalDiff = maxRight - bounds.left;
4190 }
4191 bounds.left += horizontalDiff;
4192 bounds.right += horizontalDiff;
4193 }
4194
4195 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
4196 final int maxBottom = stackBounds.bottom
4197 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
4198 int verticalDiff = stackBounds.top - bounds.top;
4199 if ((verticalDiff < 0 && bounds.top >= maxBottom)
4200 || (bounds.top + verticalDiff >= maxBottom)) {
4201 verticalDiff = maxBottom - bounds.top;
4202 }
4203 bounds.top += verticalDiff;
4204 bounds.bottom += verticalDiff;
4205 }
4206 }
4207
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004208 ActivityStack findStackBehind(ActivityStack stack) {
4209 // TODO(multi-display): We are only looking for stacks on the default display.
4210 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
4211 if (display == null) {
4212 return null;
4213 }
4214 final ArrayList<ActivityStack> stacks = display.mStacks;
4215 for (int i = stacks.size() - 1; i >= 0; i--) {
4216 if (stacks.get(i) == stack && i > 0) {
4217 return stacks.get(i - 1);
4218 }
4219 }
4220 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4221 + " in=" + stacks);
4222 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004223
Jorim Jaggic69bd222016-03-15 14:38:37 +01004224 /**
4225 * Puts a task into resizing mode during the next app transition.
4226 *
4227 * @param taskId the id of the task to put into resizing mode
4228 */
4229 private void setResizingDuringAnimation(int taskId) {
4230 mResizingTasksDuringAnimation.add(taskId);
4231 mWindowManager.setTaskDockedResizing(taskId, true);
4232 }
4233
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004234 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
4235 final TaskRecord task;
4236 final int callingUid;
4237 final String callingPackage;
4238 final Intent intent;
4239 final int userId;
4240 final ActivityOptions activityOptions = (bOptions != null)
4241 ? new ActivityOptions(bOptions) : null;
4242 final int launchStackId = (activityOptions != null)
4243 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
4244
4245 if (launchStackId == HOME_STACK_ID) {
4246 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
4247 + taskId + " can't be launch in the home stack.");
4248 }
4249 task = anyTaskForIdLocked(taskId, RESTORE_FROM_RECENTS, launchStackId);
4250 if (task == null) {
4251 throw new IllegalArgumentException(
4252 "startActivityFromRecentsInner: Task " + taskId + " not found.");
4253 }
4254
4255 if (launchStackId != INVALID_STACK_ID) {
4256 if (launchStackId == DOCKED_STACK_ID) {
4257 mWindowManager.setDockedStackCreateState(
4258 activityOptions.getDockCreateMode(), null /* initialBounds */);
4259
4260 // Defer updating the stack in which recents is until the app transition is done, to
4261 // not run into issues where we still need to draw the task in recents but the
4262 // docked stack is already created.
4263 deferUpdateBounds(HOME_STACK_ID);
4264 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
4265 }
4266 if (task.stack.mStackId != launchStackId) {
4267 moveTaskToStackLocked(
4268 taskId, launchStackId, ON_TOP, FORCE_FOCUS, "startActivityFromRecents",
4269 ANIMATE);
4270 }
4271 }
4272
4273 // If the user must confirm credentials (e.g. when first launching a work app and the
4274 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4275 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4276 && task.getRootActivity() != null) {
Jorim Jaggi09c49542016-04-09 01:39:40 -07004277 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004278 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions);
4279
4280 // If we are launching the task in the docked stack, put it into resizing mode so
4281 // the window renders full-screen with the background filling the void. Also only
4282 // call this at the end to make sure that tasks exists on the window manager side.
4283 if (launchStackId == DOCKED_STACK_ID) {
Jorim Jaggic69bd222016-03-15 14:38:37 +01004284 setResizingDuringAnimation(taskId);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004285 }
4286 return ActivityManager.START_TASK_TO_FRONT;
4287 }
4288 callingUid = task.mCallingUid;
4289 callingPackage = task.mCallingPackage;
4290 intent = task.intent;
4291 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4292 userId = task.userId;
Jorim Jaggic69bd222016-03-15 14:38:37 +01004293 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
4294 null, null, 0, 0, bOptions, userId, null, task);
4295 if (launchStackId == DOCKED_STACK_ID) {
4296 setResizingDuringAnimation(task.taskId);
4297 }
4298 return result;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004299 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004300
4301 /**
4302 * @return a list of activities which are the top ones in each visible stack. The first
4303 * entry will be the focused activity.
4304 */
4305 public List<IBinder> getTopVisibleActivities() {
4306 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
4307 if (display == null) {
4308 return Collections.EMPTY_LIST;
4309 }
4310 ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4311 final ArrayList<ActivityStack> stacks = display.mStacks;
4312 for (int i = stacks.size() - 1; i >= 0; i--) {
4313 ActivityStack stack = stacks.get(i);
4314 if (stack.getStackVisibilityLocked(null) == ActivityStack.STACK_VISIBLE) {
4315 ActivityRecord top = stack.topActivity();
4316 if (top != null) {
4317 if (stack == mFocusedStack) {
4318 topActivityTokens.add(0, top.appToken);
4319 } else {
4320 topActivityTokens.add(top.appToken);
4321 }
4322 }
4323 }
4324 }
4325 return topActivityTokens;
4326 }
Craig Mautner27084302013-03-25 08:05:25 -07004327}