blob: 22fbd9339d2f6e1487195ccd494dc77d1f483710 [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;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700101import java.util.List;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800102import java.util.Objects;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700103import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700104
Jorim Jaggife89d122015-12-22 16:28:44 +0100105import static android.Manifest.permission.START_ANY_ACTIVITY;
Jorim Jaggi2adba072016-03-03 13:43:39 +0100106import static android.Manifest.permission.START_TASKS_FROM_RECENTS;
Jorim Jaggife89d122015-12-22 16:28:44 +0100107import static android.app.ActivityManager.LOCK_TASK_MODE_LOCKED;
108import static android.app.ActivityManager.LOCK_TASK_MODE_NONE;
109import static android.app.ActivityManager.LOCK_TASK_MODE_PINNED;
110import static android.app.ActivityManager.RESIZE_MODE_FORCED;
111import static android.app.ActivityManager.RESIZE_MODE_SYSTEM;
112import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
113import static android.app.ActivityManager.StackId.FIRST_DYNAMIC_STACK_ID;
114import static android.app.ActivityManager.StackId.FIRST_STATIC_STACK_ID;
115import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
116import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
117import static android.app.ActivityManager.StackId.HOME_STACK_ID;
118import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
119import static android.app.ActivityManager.StackId.LAST_STATIC_STACK_ID;
120import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
121import static android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
122import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
123import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Jorim Jaggi51605272016-02-24 18:06:49 -0500124import static android.content.pm.ActivityInfo.RESIZE_MODE_FORCE_RESIZEABLE;
Jorim Jaggife89d122015-12-22 16:28:44 +0100125import static android.content.pm.PackageManager.PERMISSION_GRANTED;
126import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
127import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
128import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONTAINERS;
129import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_IDLE;
130import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKSCREEN;
131import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKTASK;
132import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
133import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
134import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
135import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
136import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
137import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
138import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
139import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBLE_BEHIND;
140import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONTAINERS;
141import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
142import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_LOCKTASK;
143import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
144import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
145import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
146import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
147import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
148import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
149import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
150import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBLE_BEHIND;
151import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
152import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
Wale Ogunwale480dca02016-02-06 13:58:29 -0800153import static com.android.server.am.ActivityManagerService.ANIMATE;
Jorim Jaggife89d122015-12-22 16:28:44 +0100154import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Jorim Jaggid53f0922016-04-06 22:16:23 -0700155import static com.android.server.am.ActivityManagerService.NOTIFY_ACTIVITY_DISMISSING_DOCKED_STACK_MSG;
Jorim Jaggi2adba072016-03-03 13:43:39 +0100156import static com.android.server.am.ActivityManagerService.NOTIFY_FORCED_RESIZABLE_MSG;
Jorim Jaggife89d122015-12-22 16:28:44 +0100157import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
158import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
159import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
160import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
161import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
162import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
163import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
164import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
165import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
166import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
167import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
168import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
169import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
170import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
171import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
172import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100173import static com.android.server.wm.AppTransition.TRANSIT_DOCK_TASK_FROM_RECENTS;
Jorim Jaggife89d122015-12-22 16:28:44 +0100174
Craig Mautner4a1cb222013-12-04 16:14:06 -0800175public final class ActivityStackSupervisor implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800176 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700177 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700178 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700179 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700180 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700181 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700182 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700183 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700184 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700185 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800186 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700187 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800188
Craig Mautnerf3333272013-04-22 10:55:53 -0700189 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700190 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700191
Craig Mautner0eea92c2013-05-16 13:35:39 -0700192 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700193 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700194
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700195 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700196 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700197
Craig Mautner05d29032013-05-03 13:40:13 -0700198 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
199 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
200 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700201 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700202 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800203 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
204 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
205 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700206 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400207 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
208 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700209 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700210 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700211 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800212 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
213 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800214
Wale Ogunwale040b4702015-08-06 18:10:50 -0700215 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700216
Jason Monk62515be2014-05-21 16:06:19 -0400217 private static final String LOCK_TASK_TAG = "Lock-to-App";
218
Wale Ogunwale040b4702015-08-06 18:10:50 -0700219 // Used to indicate if an object (e.g. stack) that we are trying to get
220 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800221 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700222
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700223 // Used to indicate that windows of activities should be preserved during the resize.
224 static final boolean PRESERVE_WINDOWS = true;
225
Wale Ogunwale040b4702015-08-06 18:10:50 -0700226 // Used to indicate if an object (e.g. task) should be moved/created
227 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700228 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700229
230 // Used to indicate that an objects (e.g. task) removal from its container
231 // (e.g. stack) is due to it moving to another container.
232 static final boolean MOVING = true;
233
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700234 // Force the focus to change to the stack we are moving a task to..
235 static final boolean FORCE_FOCUS = true;
236
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700237 // Restore task from the saved recents if it can't be found in any live stack.
238 static final boolean RESTORE_FROM_RECENTS = true;
239
Svetoslav7008b512015-06-24 18:47:07 -0700240 // Activity actions an app cannot start if it uses a permission which is not granted.
241 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
242 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700243
Svetoslav7008b512015-06-24 18:47:07 -0700244 static {
245 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
246 Manifest.permission.CAMERA);
247 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
248 Manifest.permission.CAMERA);
249 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
250 Manifest.permission.CALL_PHONE);
251 }
252
Svet Ganov99b60432015-06-27 13:15:22 -0700253 /** Action restriction: launching the activity is not restricted. */
254 private static final int ACTIVITY_RESTRICTION_NONE = 0;
255 /** Action restriction: launching the activity is restricted by a permission. */
256 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
257 /** Action restriction: launching the activity is restricted by an app op. */
258 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700259
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700260 // The height/width divide used when fitting a task within a bounds with method
261 // {@link #fitWithinBounds}.
262 // We always want the task to to be visible in the bounds without affecting its size when
263 // fitting. To make sure this is the case, we don't adjust the task left or top side pass
264 // the input bounds right or bottom side minus the width or height divided by this value.
265 private static final int FIT_WITHIN_BOUNDS_DIVIDER = 3;
266
justinzhang5286d3f2014-05-12 17:06:01 -0400267 /** Status Bar Service **/
268 private IBinder mToken = new Binder();
269 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400270 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400271
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700272 // For debugging to make sure the caller when acquiring/releasing our
273 // wake lock is the system process.
274 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800275 /** The number of distinct task ids that can be assigned to the tasks of a single user */
276 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700277
Craig Mautner27084302013-03-25 08:05:25 -0700278 final ActivityManagerService mService;
279
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800280 private RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800281
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700282 final ActivityStackSupervisorHandler mHandler;
283
284 /** Short cut */
285 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800286 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700287
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700288 /** Counter for next free stack ID to use for dynamic activity stacks. */
289 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700290
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800291 /**
292 * Maps the task identifier that activities are currently being started in to the userId of the
293 * task. Each time a new task is created, the entry for the userId of the task is incremented
294 */
295 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700296
Craig Mautner2420ead2013-04-01 17:13:20 -0700297 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800298 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700299
Craig Mautnere0a38842013-12-16 16:14:02 -0800300 /** The stack containing the launcher app. Assumed to always be attached to
301 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800302 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700303
Craig Mautnere0a38842013-12-16 16:14:02 -0800304 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800305 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700306
Craig Mautner4a1cb222013-12-04 16:14:06 -0800307 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
308 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800309 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800310 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700311
312 /** List of activities that are waiting for a new activity to become visible before completing
313 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800314 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700315
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700316 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800317 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700318
319 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800320 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700321
Craig Mautnerde4ef022013-04-07 19:01:33 -0700322 /** List of activities that are ready to be stopped, but waiting for the next activity to
323 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800324 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700325
Craig Mautnerf3333272013-04-22 10:55:53 -0700326 /** List of activities that are ready to be finished, but waiting for the previous activity to
327 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800328 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700329
Craig Mautner0eea92c2013-05-16 13:35:39 -0700330 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800331 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700332
Wale Ogunwale22e25262016-02-01 10:32:02 -0800333 /** List of activities whose multi-window mode changed that we need to report to the
334 * application */
335 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
336
337 /** List of activities whose picture-in-picture mode changed that we need to report to the
338 * application */
339 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
340
Craig Mautnerf3333272013-04-22 10:55:53 -0700341 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700342 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700343
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700344 /** Used to queue up any background users being started */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700345 final ArrayList<UserState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700346
Craig Mautnerde4ef022013-04-07 19:01:33 -0700347 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
348 * is being brought in front of us. */
349 boolean mUserLeaving = false;
350
Craig Mautner0eea92c2013-05-16 13:35:39 -0700351 /** Set when we have taken too long waiting to go to sleep. */
352 boolean mSleepTimeout = false;
353
354 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700355 * We don't want to allow the device to go to sleep while in the process
356 * of launching an activity. This is primarily to allow alarm intent
357 * receivers to launch an activity and get that to run before the device
358 * goes back to sleep.
359 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700360 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700361
362 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700363 * Set when the system is going to sleep, until we have
364 * successfully paused the current activity and released our wake lock.
365 * At that point the system is allowed to actually sleep.
366 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700367 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700368
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700369 /** Stack id of the front stack when user switched, indexed by userId. */
370 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700371
Craig Mautner4504de52013-12-20 09:06:56 -0800372 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800373 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700374 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800375
376 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700377 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800378
Jeff Brownca9bc702014-02-11 14:32:56 -0800379 InputManagerInternal mInputManagerInternal;
380
Craig Mautner15df08a2015-04-01 12:17:18 -0700381 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
382 * may be finished until there is only one entry left. If this is empty the system is not
383 * in lockTask mode. */
384 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000385 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700386 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
387 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000388 */
389 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400390 /**
391 * Notifies the user when entering/exiting lock-task.
392 */
393 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800394
Wale Ogunwaled046a012015-12-24 13:05:59 -0800395 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800396 boolean inResumeTopActivity;
397
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700398 // 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 -0700399 private final Rect tempRect = new Rect();
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700400 private final Rect tempRect2 = new Rect();
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700401
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700402 private final SparseArray<Configuration> mTmpConfigs = new SparseArray<>();
403 private final SparseArray<Rect> mTmpBounds = new SparseArray<>();
Jorim Jaggidc249c42015-12-15 14:57:31 -0800404 private final SparseArray<Rect> mTmpInsetBounds = new SparseArray<>();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700405
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700406 // The default minimal size that will be used if the activity doesn't specify its minimal size.
407 // It will be calculated when the default display gets added.
Wale Ogunwale9a08f822016-02-17 19:03:58 -0800408 int mDefaultMinimalSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700409
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700410 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
411 private boolean mTaskLayersChanged = true;
412
Jorim Jaggi275561a2016-02-23 10:11:02 -0500413 final ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800414
Jorim Jaggidc249c42015-12-15 14:57:31 -0800415 private final ResizeDockedStackTimeout mResizeDockedStackTimeout;
416
Wale Ogunwale39381972015-12-17 17:15:29 -0800417 static class FindTaskResult {
418 ActivityRecord r;
419 boolean matchedByRootAffinity;
420 }
421 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
422
Craig Mautneree36c772014-07-16 14:56:05 -0700423 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100424 * Used to keep track whether app visibilities got changed since the last pause. Useful to
425 * determine whether to invoke the task stack change listener after pausing.
426 */
427 boolean mAppVisibilitiesChangedSinceLastPause;
428
429 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100430 * Set of tasks that are in resizing mode during an app transition to fill the "void".
431 */
432 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
433
434 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700435 * Description of a request to start a new activity, which has been held
436 * due to app switches being disabled.
437 */
438 static class PendingActivityLaunch {
439 final ActivityRecord r;
440 final ActivityRecord sourceRecord;
441 final int startFlags;
442 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700443 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700444
445 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700446 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700447 r = _r;
448 sourceRecord = _sourceRecord;
449 startFlags = _startFlags;
450 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700451 callerApp = _callerApp;
452 }
453
454 void sendErrorResult(String message) {
455 try {
456 if (callerApp.thread != null) {
457 callerApp.thread.scheduleCrash(message);
458 }
459 } catch (RemoteException e) {
460 Slog.e(TAG, "Exception scheduling crash of failed "
461 + "activity launcher sourceRecord=" + sourceRecord, e);
462 }
Craig Mautneree36c772014-07-16 14:56:05 -0700463 }
464 }
465
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800466 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700467 mService = service;
Jeff Brown2c43c332014-06-12 22:38:59 -0700468 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800469 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggidc249c42015-12-15 14:57:31 -0800470 mResizeDockedStackTimeout = new ResizeDockedStackTimeout(service, this, mHandler);
Jeff Brown2c43c332014-06-12 22:38:59 -0700471 }
472
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800473 void setRecentTasks(RecentTasks recentTasks) {
474 mRecentTasks = recentTasks;
475 }
476
Jeff Brown2c43c332014-06-12 22:38:59 -0700477 /**
478 * At the time when the constructor runs, the power manager has not yet been
479 * initialized. So we initialize our wakelocks afterwards.
480 */
481 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800482 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700483 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700484 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700485 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700486 }
487
justinzhang5286d3f2014-05-12 17:06:01 -0400488 // This function returns a IStatusBarService. The value is from ServiceManager.
489 // getService and is cached.
490 private IStatusBarService getStatusBarService() {
491 synchronized (mService) {
492 if (mStatusBarService == null) {
493 mStatusBarService = IStatusBarService.Stub.asInterface(
494 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
495 if (mStatusBarService == null) {
496 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
497 }
498 }
499 return mStatusBarService;
500 }
501 }
502
Jason Monk35c62a42014-06-17 10:24:47 -0400503 private IDevicePolicyManager getDevicePolicyManager() {
504 synchronized (mService) {
505 if (mDevicePolicyManager == null) {
506 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
507 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
508 if (mDevicePolicyManager == null) {
509 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
510 }
511 }
512 return mDevicePolicyManager;
513 }
514 }
515
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700516 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800517 synchronized (mService) {
518 mWindowManager = wm;
519
520 mDisplayManager =
521 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
522 mDisplayManager.registerDisplayListener(this, null);
523
524 Display[] displays = mDisplayManager.getDisplays();
525 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
526 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800527 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700528 if (activityDisplay.mDisplay == null) {
529 throw new IllegalStateException("Default Display does not exist");
530 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800531 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700532 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800533 }
534
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800535 mHomeStack = mFocusedStack = mLastFocusedStack =
536 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800537
538 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800539 }
Craig Mautner27084302013-03-25 08:05:25 -0700540 }
541
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200542 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700543 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200544 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700545 }
546
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700547 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800548 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700549 }
550
Craig Mautnerde4ef022013-04-07 19:01:33 -0700551 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800552 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700553 }
554
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700555 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700556 if (stack == null) {
557 return false;
558 }
559
Craig Mautnerdf88d732014-01-27 09:21:32 -0800560 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
561 if (parent != null) {
562 stack = parent.task.stack;
563 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800564 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700565 }
566
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700567 /** The top most stack. */
568 boolean isFrontStack(ActivityStack stack) {
569 if (stack == null) {
570 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800571 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700572
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700573 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
574 if (parent != null) {
575 stack = parent.task.stack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800576 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700577 return stack == mHomeStack.mStacks.get((mHomeStack.mStacks.size() - 1));
578 }
579
Wale Ogunwaled046a012015-12-24 13:05:59 -0800580 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800581 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
582 if (!focusCandidate.isFocusable()) {
583 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
584 focusCandidate = focusCandidate.getNextFocusableStackLocked();
585 }
586
587 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800588 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800589 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800590
Wale Ogunwaled046a012015-12-24 13:05:59 -0800591 EventLogTags.writeAmFocusedStack(
592 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
593 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
594 }
595
596 final ActivityRecord r = topRunningActivityLocked();
Chong Zhang3aa28b22016-02-04 16:38:33 -0800597 if (!mService.mDoingSetFocusedActivity && mService.mFocusedActivity != r) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800598 // The focus activity should always be the top activity in the focused stack.
599 // There will be chaos and anarchy if it isn't...
600 mService.setFocusedActivityLocked(r, reason + " setFocusStack");
601 }
Craig Mautnerde313752015-01-22 14:28:03 -0800602
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800603 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800604 if (r != null && r.idle) {
605 checkFinishBootingLocked();
606 }
607 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700608 }
609
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700610 void moveHomeStackToFront(String reason) {
611 mHomeStack.moveToFront(reason);
612 }
613
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700614 /** Returns true if the focus activity was adjusted to the home stack top activity. */
615 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700616 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
617 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700618 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700619 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700620
Craig Mautner84984fa2014-06-19 11:19:20 -0700621 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700622
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700623 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700624 if (top == null) {
625 return false;
626 }
627 mService.setFocusedActivityLocked(top, reason);
628 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700629 }
630
Craig Mautner299f9602015-01-26 09:47:33 -0800631 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700632 if (!mService.mBooting && !mService.mBooted) {
633 // Not ready yet!
634 return false;
635 }
636
Craig Mautner84984fa2014-06-19 11:19:20 -0700637 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
638 mWindowManager.showRecentApps();
639 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700640 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700641
Craig Mautner84984fa2014-06-19 11:19:20 -0700642 if (prev != null) {
643 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
644 }
645
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700646 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
647 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800648 final String myReason = reason + " resumeHomeStackTask";
649
Mark Lua56ea122015-10-08 13:31:01 +0800650 // Only resume home activity if isn't finishing.
651 if (r != null && !r.finishing) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800652 mService.setFocusedActivityLocked(r, myReason);
653 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700654 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800655 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700656 }
657
Craig Mautner8d341ef2013-03-26 09:03:27 -0700658 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700659 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700660 }
661
662 /**
663 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
664 * @param id Id of the task we would like returned.
665 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
666 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700667 * @param stackId The stack to restore the task to (default launch stack will be used if
668 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700669 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700670 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800671 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800672 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800673 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800674 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
675 ActivityStack stack = stacks.get(stackNdx);
676 TaskRecord task = stack.taskForIdLocked(id);
677 if (task != null) {
678 return task;
679 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700680 }
681 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800682
683 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700684 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800685 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800686 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700687 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800688 return null;
689 }
690
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700691 if (!restoreFromRecents) {
692 return task;
693 }
694
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700695 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700696 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
697 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800698 return null;
699 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700700 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800701 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700702 }
703
Craig Mautner6170f732013-04-02 13:05:23 -0700704 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800705 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800706 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800707 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800708 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
709 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
710 if (r != null) {
711 return r;
712 }
Craig Mautner6170f732013-04-02 13:05:23 -0700713 }
714 }
715 return null;
716 }
717
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000718 boolean isFocusedUserLockedProfile() {
719 final int userId = mFocusedStack.topRunningActivityLocked().userId;
720 return userId != UserHandle.myUserId()
721 && mService.mUserController.shouldConfirmCredentials(userId);
722 }
723
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800724 void setNextTaskIdForUserLocked(int taskId, int userId) {
725 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
726 if (taskId > currentTaskId) {
727 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700728 }
729 }
730
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800731 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800732 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
733 // for a userId u, a taskId can only be in the range
734 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
735 // was 10, user 0 could only have taskIds 0 to 9, user 1: 10 to 19, user 2: 20 to 29, so on.
736 int candidateTaskId = currentTaskId;
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800737 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
738 || anyTaskForIdLocked(candidateTaskId, !RESTORE_FROM_RECENTS,
739 INVALID_STACK_ID) != null) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800740 candidateTaskId++;
741 if (candidateTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
742 // Wrap around as there will be smaller task ids that are available now.
743 candidateTaskId -= MAX_TASK_IDS_PER_USER;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700744 }
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800745 if (candidateTaskId == currentTaskId) {
746 // Something wrong!
747 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
748 throw new IllegalStateException("Cannot get an available task id."
749 + " Reached limit of " + MAX_TASK_IDS_PER_USER
750 + " running tasks per user.");
751 }
752 }
753 mCurTaskIdForUser.put(userId, candidateTaskId);
754 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700755 }
756
Craig Mautnerde4ef022013-04-07 19:01:33 -0700757 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800758 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700759 if (stack == null) {
760 return null;
761 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700762 ActivityRecord resumedActivity = stack.mResumedActivity;
763 if (resumedActivity == null || resumedActivity.app == null) {
764 resumedActivity = stack.mPausingActivity;
765 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700766 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700767 }
768 }
769 return resumedActivity;
770 }
771
Dianne Hackbornff072722014-09-24 10:56:28 -0700772 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700773 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800774 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800775 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
776 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800777 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
778 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700779 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800780 continue;
781 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700782 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800783 if (hr != null) {
784 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
785 && processName.equals(hr.processName)) {
786 try {
George Mount2c92c972014-03-20 09:38:23 -0700787 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800788 didSomething = true;
789 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700790 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800791 Slog.w(TAG, "Exception in new application when starting activity "
792 + hr.intent.getComponent().flattenToShortString(), e);
793 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700794 }
Craig Mautner20e72272013-04-01 13:45:53 -0700795 }
Craig Mautner20e72272013-04-01 13:45:53 -0700796 }
797 }
798 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700799 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700800 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700801 }
Craig Mautner20e72272013-04-01 13:45:53 -0700802 return didSomething;
803 }
804
805 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800806 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
807 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800808 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
809 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700810 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800811 continue;
812 }
813 final ActivityRecord resumedActivity = stack.mResumedActivity;
814 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700815 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800816 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800817 return false;
818 }
Craig Mautner20e72272013-04-01 13:45:53 -0700819 }
820 }
821 return true;
822 }
823
Craig Mautnerde4ef022013-04-07 19:01:33 -0700824 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800825 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
826 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800827 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
828 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700829 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800830 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700831 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800832 return false;
833 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700834 }
835 }
836 }
837 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700838 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800839 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
840 mLastFocusedStack + " to=" + mFocusedStack);
841 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700842 return true;
843 }
844
845 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800846 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800847 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
848 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800849 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
850 final ActivityStack stack = stacks.get(stackNdx);
851 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800852 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700853 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800854 return false;
855 }
856 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800857 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700858 }
859 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800860 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700861 }
862
Craig Mautner2acc3892013-09-23 10:28:14 -0700863 /**
864 * Pause all activities in either all of the stacks or just the back stacks.
865 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700866 * @return true if any activity was paused as a result of this call.
867 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700868 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700869 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800870 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
871 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800872 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
873 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700874 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700875 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800876 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700877 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
878 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800879 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700880 }
881 }
882 return someActivityPaused;
883 }
884
Craig Mautnerde4ef022013-04-07 19:01:33 -0700885 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700886 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800887 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
888 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800889 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
890 final ActivityStack stack = stacks.get(stackNdx);
891 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700892 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800893 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700894 Slog.d(TAG_STATES,
895 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800896 pausing = false;
897 } else {
898 return false;
899 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700900 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700901 }
902 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700903 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700904 }
905
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700906 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
907 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500908 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800909 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
910 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
911 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
912 final ActivityStack stack = stacks.get(stackNdx);
913 if (stack.mResumedActivity != null &&
914 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700915 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800916 }
917 }
918 }
919 }
920
Wale Ogunwale2be760d2016-02-17 11:16:10 -0800921 void cancelInitializingActivities() {
922 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
923 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
924 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
925 stacks.get(stackNdx).cancelInitializingActivities();
926 }
927 }
928 }
929
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700930 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700931 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700932 }
933
934 void sendWaitingVisibleReportLocked(ActivityRecord r) {
935 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700936 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700937 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700938 if (w.who == null) {
939 changed = true;
940 w.timeout = false;
941 if (r != null) {
942 w.who = new ComponentName(r.info.packageName, r.info.name);
943 }
944 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
945 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700946 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700947 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700948 if (changed) {
949 mService.notifyAll();
950 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700951 }
952
953 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
954 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700955 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700956 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700957 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700958 if (w.who == null) {
959 changed = true;
960 w.timeout = timeout;
961 if (r != null) {
962 w.who = new ComponentName(r.info.packageName, r.info.name);
963 }
964 w.thisTime = thisTime;
965 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700966 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700967 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700968 if (changed) {
969 mService.notifyAll();
970 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700971 }
972
Craig Mautner29219d92013-04-16 20:19:12 -0700973 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800974 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700975 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -0700976 if (r != null) {
977 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700978 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700979
Craig Mautner4a1cb222013-12-04 16:14:06 -0800980 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800981 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800982 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
983 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800984 if (stack != focusedStack && isFrontStack(stack) && stack.isFocusable()) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700985 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -0700986 if (r != null) {
987 return r;
988 }
989 }
990 }
991 return null;
992 }
993
Dianne Hackborn09233282014-04-30 11:33:59 -0700994 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700995 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800996 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
997 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800998 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800999 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001000 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001001 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1002 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001003 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001004 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001005 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001006 }
1007 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001008
1009 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1010 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1011 while (maxNum > 0) {
1012 long mostRecentActiveTime = Long.MIN_VALUE;
1013 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001014 final int numTaskLists = runningTaskLists.size();
1015 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1016 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001017 if (!stackTaskList.isEmpty()) {
1018 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1019 if (lastActiveTime > mostRecentActiveTime) {
1020 mostRecentActiveTime = lastActiveTime;
1021 selectedStackList = stackTaskList;
1022 }
1023 }
1024 }
1025 if (selectedStackList != null) {
1026 list.add(selectedStackList.remove(0));
1027 --maxNum;
1028 } else {
1029 break;
1030 }
1031 }
Craig Mautner20e72272013-04-01 13:45:53 -07001032 }
1033
Todd Kennedy7440f172015-12-09 14:31:22 -08001034 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1035 ProfilerInfo profilerInfo) {
1036 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001037 if (aInfo != null) {
1038 // Store the found target back into the intent, because now that
1039 // we have it we never want to do this again. For example, if the
1040 // user navigates back to this point in the history, we should
1041 // always restart the exact same activity.
1042 intent.setComponent(new ComponentName(
1043 aInfo.applicationInfo.packageName, aInfo.name));
1044
1045 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001046 if (!aInfo.processName.equals("system")) {
1047 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001048 mService.setDebugApp(aInfo.processName, true, false);
1049 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001050
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001051 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1052 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1053 }
1054
Man Caocfa78b22015-06-11 20:14:34 -07001055 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1056 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1057 }
1058
1059 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001060 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001061 }
1062 }
1063 }
1064 return aInfo;
1065 }
1066
Todd Kennedy7440f172015-12-09 14:31:22 -08001067 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001068 return resolveIntent(intent, resolvedType, userId, 0);
1069 }
1070
1071 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy7440f172015-12-09 14:31:22 -08001072 try {
1073 return AppGlobals.getPackageManager().resolveIntent(intent, resolvedType,
Kenny Guyb1b30262016-02-09 16:02:35 +00001074 PackageManager.MATCH_DEFAULT_ONLY | flags
1075 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001076 } catch (RemoteException e) {
1077 }
1078 return null;
1079 }
1080
1081 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1082 ProfilerInfo profilerInfo, int userId) {
1083 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1084 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1085 }
1086
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001087 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1088 boolean andResume, boolean checkConfig) throws RemoteException {
1089
1090 if (!allPausedActivitiesComplete()) {
1091 // While there are activities pausing we skipping starting any new activities until
1092 // pauses are complete. NOTE: that we also do this for activities that are starting in
1093 // the paused state because they will first be resumed then paused on the client side.
1094 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1095 "realStartActivityLocked: Skipping start of r=" + r
1096 + " some activities pausing...");
1097 return false;
1098 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001099
Craig Mautner2568c3a2015-03-26 14:22:34 -07001100 if (andResume) {
1101 r.startFreezingScreenLocked(app, 0);
1102 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001103
Craig Mautner2568c3a2015-03-26 14:22:34 -07001104 // schedule launch ticks to collect information about slow apps.
1105 r.startLaunchTickingLocked();
1106 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001107
1108 // Have the window manager re-evaluate the orientation of
1109 // the screen based on the new activity order. Note that
1110 // as a result of this, it can call back into the activity
1111 // manager with a new orientation. We don't care about that,
1112 // because the activity is not currently running so we are
1113 // just restarting it anyway.
1114 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001115 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001116 mService.mConfiguration,
1117 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001118 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001119 }
1120
1121 r.app = app;
1122 app.waitingToKill = null;
1123 r.launchCount++;
1124 r.lastLaunchTime = SystemClock.uptimeMillis();
1125
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001126 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001127
1128 int idx = app.activities.indexOf(r);
1129 if (idx < 0) {
1130 app.activities.add(r);
1131 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001132 mService.updateLruProcessLocked(app, true, null);
1133 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001134
Craig Mautner15df08a2015-04-01 12:17:18 -07001135 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001136 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1137 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001138 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001139 }
1140
1141 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001142 try {
1143 if (app.thread == null) {
1144 throw new RemoteException();
1145 }
1146 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001147 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001148 if (andResume) {
1149 results = r.results;
1150 newIntents = r.newIntents;
1151 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001152 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1153 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1154 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001155 if (andResume) {
1156 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1157 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001158 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001159 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001160 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001161 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001162 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001163 }
Nicolas Geoffray27c07372015-11-05 16:54:09 +00001164 mService.notifyPackageUse(r.intent.getComponent().getPackageName());
Craig Mautner2420ead2013-04-01 17:13:20 -07001165 r.sleeping = false;
1166 r.forceNewConfig = false;
1167 mService.showAskCompatModeDialogLocked(r);
1168 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001169 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001170 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1171 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1172 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001173 final String profileFile = mService.mProfileFile;
1174 if (profileFile != null) {
1175 ParcelFileDescriptor profileFd = mService.mProfileFd;
1176 if (profileFd != null) {
1177 try {
1178 profileFd = profileFd.dup();
1179 } catch (IOException e) {
1180 if (profileFd != null) {
1181 try {
1182 profileFd.close();
1183 } catch (IOException o) {
1184 }
1185 profileFd = null;
1186 }
1187 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001188 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001189
1190 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1191 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001192 }
1193 }
1194 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001195
Craig Mautner2568c3a2015-03-26 14:22:34 -07001196 if (andResume) {
1197 app.hasShownUi = true;
1198 app.pendingUiClean = true;
1199 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001200 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001201 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001202 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001203 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001204 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001205 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001206
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001207 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001208 // This may be a heavy-weight process! Note that the package
1209 // manager will ensure that only activity can run in the main
1210 // process of the .apk, which is the only thing that will be
1211 // considered heavy-weight.
1212 if (app.processName.equals(app.info.packageName)) {
1213 if (mService.mHeavyWeightProcess != null
1214 && mService.mHeavyWeightProcess != app) {
1215 Slog.w(TAG, "Starting new heavy weight process " + app
1216 + " when already running "
1217 + mService.mHeavyWeightProcess);
1218 }
1219 mService.mHeavyWeightProcess = app;
1220 Message msg = mService.mHandler.obtainMessage(
1221 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1222 msg.obj = r;
1223 mService.mHandler.sendMessage(msg);
1224 }
1225 }
1226
1227 } catch (RemoteException e) {
1228 if (r.launchFailed) {
1229 // This is the second time we failed -- finish activity
1230 // and give up.
1231 Slog.e(TAG, "Second failure launching "
1232 + r.intent.getComponent().flattenToShortString()
1233 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001234 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001235 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1236 "2nd-crash", false);
1237 return false;
1238 }
1239
1240 // This is the first time we failed -- restart process and
1241 // retry.
1242 app.activities.remove(r);
1243 throw e;
1244 }
1245
1246 r.launchFailed = false;
1247 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001248 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001249 }
1250
1251 if (andResume) {
1252 // As part of the process of launching, ActivityThread also performs
1253 // a resume.
1254 stack.minimalResumeActivityLocked(r);
1255 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001256 // This activity is not starting in the resumed state... which should look like we asked
1257 // it to pause+stop (but remain visible), and it has done so and reported back the
1258 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001259 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001260 "Moving to PAUSED: " + r + " (starting in paused state)");
1261 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001262 }
1263
1264 // Launch the new version setup screen if needed. We do this -after-
1265 // launching the initial activity (that is, home), so that it can have
1266 // a chance to initialize itself while in the background, making the
1267 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001268 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001269 mService.startSetupActivityLocked();
1270 }
1271
Dianne Hackborn465fa392014-09-14 14:21:18 -07001272 // Update any services we are bound to that might care about whether
1273 // their client may have activities.
1274 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1275
Craig Mautner2420ead2013-04-01 17:13:20 -07001276 return true;
1277 }
1278
Craig Mautnere79d42682013-04-01 19:01:53 -07001279 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001280 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001281 // Is this activity's application already running?
1282 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001283 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001284
1285 r.task.stack.setLaunchTime(r);
1286
1287 if (app != null && app.thread != null) {
1288 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001289 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1290 || !"android".equals(r.info.packageName)) {
1291 // Don't add this if it is a platform component that is marked
1292 // to run in multiple processes, because this is actually
1293 // part of the framework so doesn't make sense to track as a
1294 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001295 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1296 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001297 }
George Mount2c92c972014-03-20 09:38:23 -07001298 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001299 return;
1300 } catch (RemoteException e) {
1301 Slog.w(TAG, "Exception when starting activity "
1302 + r.intent.getComponent().flattenToShortString(), e);
1303 }
1304
1305 // If a dead object exception was thrown -- fall through to
1306 // restart the application.
1307 }
1308
1309 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001310 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001311 }
1312
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001313 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001314 String resultWho, int requestCode, int callingPid, int callingUid,
1315 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001316 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001317 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1318 callingUid);
1319 if (startAnyPerm == PERMISSION_GRANTED) {
1320 return true;
1321 }
1322 final int componentRestriction = getComponentRestrictionForCallingPackage(
1323 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1324 final int actionRestriction = getActionRestrictionForCallingPackage(
1325 intent.getAction(), callingPackage, callingPid, callingUid);
1326 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1327 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1328 if (resultRecord != null) {
1329 resultStack.sendActivityResultLocked(-1,
1330 resultRecord, resultWho, requestCode,
1331 Activity.RESULT_CANCELED, null);
1332 }
1333 final String msg;
1334 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1335 msg = "Permission Denial: starting " + intent.toString()
1336 + " from " + callerApp + " (pid=" + callingPid
1337 + ", uid=" + callingUid + ")" + " with revoked permission "
1338 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1339 } else if (!aInfo.exported) {
1340 msg = "Permission Denial: starting " + intent.toString()
1341 + " from " + callerApp + " (pid=" + callingPid
1342 + ", uid=" + callingUid + ")"
1343 + " not exported from uid " + aInfo.applicationInfo.uid;
1344 } else {
1345 msg = "Permission Denial: starting " + intent.toString()
1346 + " from " + callerApp + " (pid=" + callingPid
1347 + ", uid=" + callingUid + ")"
1348 + " requires " + aInfo.permission;
1349 }
1350 Slog.w(TAG, msg);
1351 throw new SecurityException(msg);
1352 }
1353
1354 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1355 final String message = "Appop Denial: starting " + intent.toString()
1356 + " from " + callerApp + " (pid=" + callingPid
1357 + ", uid=" + callingUid + ")"
1358 + " requires " + AppOpsManager.permissionToOp(
1359 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1360 Slog.w(TAG, message);
1361 return false;
1362 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1363 final String message = "Appop Denial: starting " + intent.toString()
1364 + " from " + callerApp + " (pid=" + callingPid
1365 + ", uid=" + callingUid + ")"
1366 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1367 Slog.w(TAG, message);
1368 return false;
1369 }
Jorim Jaggi2adba072016-03-03 13:43:39 +01001370 if (options != null && options.getLaunchTaskId() != -1) {
1371 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1372 callingPid, callingUid);
1373 if (startInTaskPerm != PERMISSION_GRANTED) {
1374 final String msg = "Permission Denial: starting " + intent.toString()
1375 + " from " + callerApp + " (pid=" + callingPid
1376 + ", uid=" + callingUid + ") with launchTaskId="
1377 + options.getLaunchTaskId();
1378 Slog.w(TAG, msg);
1379 throw new SecurityException(msg);
1380 }
1381 }
1382
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001383 return true;
1384 }
1385
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001386 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001387 final long identity = Binder.clearCallingIdentity();
1388 try {
1389 return UserManager.get(mService.mContext).getUserInfo(userId);
1390 } finally {
1391 Binder.restoreCallingIdentity(identity);
1392 }
1393 }
1394
Svet Ganov99b60432015-06-27 13:15:22 -07001395 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001396 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001397 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1398 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001399 == PackageManager.PERMISSION_DENIED) {
1400 return ACTIVITY_RESTRICTION_PERMISSION;
1401 }
1402
Christopher Tateff7add02015-08-17 10:23:22 -07001403 if (activityInfo.permission == null) {
1404 return ACTIVITY_RESTRICTION_NONE;
1405 }
1406
Svet Ganov99b60432015-06-27 13:15:22 -07001407 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1408 if (opCode == AppOpsManager.OP_NONE) {
1409 return ACTIVITY_RESTRICTION_NONE;
1410 }
1411
1412 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1413 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001414 if (!ignoreTargetSecurity) {
1415 return ACTIVITY_RESTRICTION_APPOP;
1416 }
Svet Ganov99b60432015-06-27 13:15:22 -07001417 }
1418
1419 return ACTIVITY_RESTRICTION_NONE;
1420 }
1421
Svetoslav7008b512015-06-24 18:47:07 -07001422 private int getActionRestrictionForCallingPackage(String action,
1423 String callingPackage, int callingPid, int callingUid) {
1424 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001425 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001426 }
1427
1428 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1429 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001430 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001431 }
1432
1433 final PackageInfo packageInfo;
1434 try {
1435 packageInfo = mService.mContext.getPackageManager()
1436 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1437 } catch (PackageManager.NameNotFoundException e) {
1438 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001439 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001440 }
1441
1442 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001443 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001444 }
1445
1446 if (mService.checkPermission(permission, callingPid, callingUid) ==
1447 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001448 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001449 }
1450
1451 final int opCode = AppOpsManager.permissionToOpCode(permission);
1452 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001453 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001454 }
1455
1456 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1457 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001458 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001459 }
1460
Svet Ganov99b60432015-06-27 13:15:22 -07001461 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001462 }
1463
Wale Ogunwaled046a012015-12-24 13:05:59 -08001464 boolean moveActivityStackToFront(ActivityRecord r, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001465 if (r == null) {
1466 // Not sure what you are trying to do, but it is not going to work...
1467 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001468 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001469 final TaskRecord task = r.task;
1470 if (task == null || task.stack == null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001471 Slog.w(TAG, "Can't move stack to front for r=" + r + " task=" + task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001472 return false;
1473 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001474 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001475 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001476 }
1477
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001478 void setLaunchSource(int uid) {
1479 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1480 }
1481
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001482 void acquireLaunchWakelock() {
1483 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1484 throw new IllegalStateException("Calling must be system uid");
1485 }
1486 mLaunchingActivity.acquire();
1487 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1488 // To be safe, don't allow the wake lock to be held for too long.
1489 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1490 }
1491 }
1492
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001493 /**
1494 * Called when the frontmost task is idle.
1495 * @return the state of mService.mBooting before this was called.
1496 */
1497 private boolean checkFinishBootingLocked() {
1498 final boolean booting = mService.mBooting;
1499 boolean enableScreen = false;
1500 mService.mBooting = false;
1501 if (!mService.mBooted) {
1502 mService.mBooted = true;
1503 enableScreen = true;
1504 }
1505 if (booting || enableScreen) {
1506 mService.postFinishBooting(booting, enableScreen);
1507 }
1508 return booting;
1509 }
1510
Craig Mautnerf3333272013-04-22 10:55:53 -07001511 // Checked.
1512 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
1513 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001514 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001515
Craig Mautnerf3333272013-04-22 10:55:53 -07001516 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001517 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001518 int NS = 0;
1519 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001520 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001521 boolean activityRemoved = false;
1522
Wale Ogunwale7d701172015-03-11 15:36:30 -07001523 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001524 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001525 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1526 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001527 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1528 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001529 if (fromTimeout) {
1530 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001531 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001532
1533 // This is a hack to semi-deal with a race condition
1534 // in the client where it can be constructed with a
1535 // newer configuration from when we asked it to launch.
1536 // We'll update with whatever configuration it now says
1537 // it used to launch.
1538 if (config != null) {
1539 r.configuration = config;
1540 }
1541
1542 // We are now idle. If someone is waiting for a thumbnail from
1543 // us, we can now deliver.
1544 r.idle = true;
1545
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001546 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001547 if (isFocusedStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001548 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001549 }
1550 }
1551
1552 if (allResumedActivitiesIdle()) {
1553 if (r != null) {
1554 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001555 }
1556
1557 if (mLaunchingActivity.isHeld()) {
1558 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1559 if (VALIDATE_WAKE_LOCK_CALLER &&
1560 Binder.getCallingUid() != Process.myUid()) {
1561 throw new IllegalStateException("Calling must be system uid");
1562 }
1563 mLaunchingActivity.release();
1564 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001565 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001566 }
1567
1568 // Atomically retrieve all of the other things to do.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001569 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(true);
Craig Mautnerf3333272013-04-22 10:55:53 -07001570 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001571 if ((NF = mFinishingActivities.size()) > 0) {
1572 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001573 mFinishingActivities.clear();
1574 }
1575
Craig Mautnerf3333272013-04-22 10:55:53 -07001576 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001577 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001578 mStartingUsers.clear();
1579 }
1580
Craig Mautnerf3333272013-04-22 10:55:53 -07001581 // Stop any activities that are scheduled to do so but have been
1582 // waiting for the next one to start.
1583 for (int i = 0; i < NS; i++) {
1584 r = stops.get(i);
1585 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001586 if (stack != null) {
1587 if (r.finishing) {
1588 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1589 } else {
1590 stack.stopActivityLocked(r);
1591 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001592 }
1593 }
1594
1595 // Finish any activities that are scheduled to do so but have been
1596 // waiting for the next one to start.
1597 for (int i = 0; i < NF; i++) {
1598 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07001599 final ActivityStack stack = r.task.stack;
1600 if (stack != null) {
1601 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1602 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001603 }
1604
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001605 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001606 // Complete user switch
1607 if (startingUsers != null) {
1608 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001609 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001610 }
1611 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001612 }
1613
1614 mService.trimApplications();
1615 //dump();
1616 //mWindowManager.dump();
1617
Craig Mautnerf3333272013-04-22 10:55:53 -07001618 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001619 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001620 }
1621
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001622 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001623 }
1624
Craig Mautner8e569572013-10-11 17:36:59 -07001625 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001626 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001627 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1628 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001629 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1630 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1631 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001632 }
Craig Mautner19091252013-10-05 00:03:53 -07001633 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001634 }
1635
1636 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001637 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1638 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001639 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1640 stacks.get(stackNdx).closeSystemDialogsLocked();
1641 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001642 }
1643 }
1644
Craig Mautner93529a42013-10-04 15:03:13 -07001645 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001646 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001647 }
1648
Craig Mautner8d341ef2013-03-26 09:03:27 -07001649 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001650 * Update the last used stack id for non-current user (current user's last
1651 * used stack is the focused stack)
1652 */
1653 void updateUserStackLocked(int userId, ActivityStack stack) {
1654 if (userId != mCurrentUser) {
1655 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1656 }
1657 }
1658
1659 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001660 * @return true if some activity was finished (or would have finished if doit were true).
1661 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001662 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1663 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001664 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001665 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1666 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001667 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001668 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001669 if (stack.finishDisabledPackageActivitiesLocked(
1670 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001671 didSomething = true;
1672 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001673 }
1674 }
1675 return didSomething;
1676 }
1677
Dianne Hackborna413dc02013-07-12 12:02:55 -07001678 void updatePreviousProcessLocked(ActivityRecord r) {
1679 // Now that this process has stopped, we may want to consider
1680 // it to be the previous app to try to keep around in case
1681 // the user wants to return to it.
1682
1683 // First, found out what is currently the foreground app, so that
1684 // we don't blow away the previous app if this activity is being
1685 // hosted by the process that is actually still the foreground.
1686 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001687 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1688 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001689 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1690 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001691 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001692 if (stack.mResumedActivity != null) {
1693 fgApp = stack.mResumedActivity.app;
1694 } else if (stack.mPausingActivity != null) {
1695 fgApp = stack.mPausingActivity.app;
1696 }
1697 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001698 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001699 }
1700 }
1701
1702 // Now set this one as the previous process, only if that really
1703 // makes sense to.
1704 if (r.app != null && fgApp != null && r.app != fgApp
1705 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001706 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001707 mService.mPreviousProcess = r.app;
1708 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1709 }
1710 }
1711
Wale Ogunwaled046a012015-12-24 13:05:59 -08001712 boolean resumeFocusedStackTopActivityLocked() {
1713 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001714 }
1715
Wale Ogunwaled046a012015-12-24 13:05:59 -08001716 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001717 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001718 if (targetStack != null && isFocusedStack(targetStack)) {
1719 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001720 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001721 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
1722 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001723 }
1724
Todd Kennedy39bfee52016-02-24 10:28:21 -08001725 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
1726 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1727 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1728 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1729 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
1730 }
1731 }
1732 }
1733
Adrian Roos20d7df32016-01-12 18:59:43 +01001734 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
1735 TaskRecord finishedTask = null;
1736 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08001737 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1738 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001739 final int numStacks = stacks.size();
1740 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1741 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01001742 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
1743 if (stack == focusedStack || finishedTask == null) {
1744 finishedTask = t;
1745 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001746 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001747 }
Adrian Roos20d7df32016-01-12 18:59:43 +01001748 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001749 }
1750
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07001751 void finishVoiceTask(IVoiceInteractionSession session) {
1752 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1753 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1754 final int numStacks = stacks.size();
1755 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1756 final ActivityStack stack = stacks.get(stackNdx);
1757 stack.finishVoiceTask(session);
1758 }
1759 }
1760 }
1761
Chong Zhang280d3322015-11-03 17:27:26 -08001762 void findTaskToMoveToFrontLocked(
1763 TaskRecord task, int flags, ActivityOptions options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001764 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
1765 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001766 }
Craig Mautneraea74a52014-03-08 14:23:10 -08001767 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
1768 // Caller wants the home activity moved with it. To accomplish this,
1769 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07001770 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08001771 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07001772 if (task.stack == null) {
1773 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
1774 + task + " to front. Stack is null");
1775 return;
1776 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001777
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001778 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001779 int stackId = options.getLaunchStackId();
1780 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001781 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07001782 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08001783 if (stackId == INVALID_STACK_ID) {
1784 stackId = task.getLaunchStackId();
1785 }
Chong Zhang112eb8c2015-11-02 11:17:00 -08001786 if (stackId != task.stack.mStackId) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08001787 final ActivityStack stack = moveTaskToStackUncheckedLocked(
1788 task, stackId, ON_TOP, !FORCE_FOCUS, reason);
1789 stackId = stack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08001790 // moveTaskToStackUncheckedLocked() should already placed the task on top,
1791 // still need moveTaskToFrontLocked() below for any transition settings.
1792 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001793 if (StackId.resizeStackWithLaunchBounds(stackId)) {
1794 resizeStackLocked(stackId, bounds,
1795 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
1796 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */);
1797 } else {
1798 // WM resizeTask must be done after the task is moved to the correct stack,
1799 // because Task's setBounds() also updates dim layer's bounds, but that has
1800 // dependency on the stack.
Andrii Kulian73336d812016-03-24 12:56:08 -07001801 mWindowManager.resizeTask(task.taskId, task.mBounds, task.mOverrideConfig,
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001802 false /* relayout */, false /* forced */);
1803 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001804 }
1805 }
1806
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001807 final ActivityRecord r = task.getTopActivity();
1808 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
1809 r == null ? null : r.appTimeTracker, reason);
1810
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001811 if (DEBUG_STACK) Slog.d(TAG_STACK,
1812 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001813
Jorim Jaggid53f0922016-04-06 22:16:23 -07001814 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.stack.mStackId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001815 }
1816
Wale Ogunwale854809c2015-12-27 16:18:19 -08001817 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001818 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08001819 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08001820 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001821 return false;
1822 }
1823 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
1824 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001825 }
1826
Craig Mautner967212c2013-04-13 21:10:58 -07001827 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07001828 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001829 }
1830
1831 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001832 ActivityContainer activityContainer = mActivityContainers.get(stackId);
1833 if (activityContainer != null) {
1834 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001835 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07001836 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001837 return null;
1838 }
1839 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001840 }
1841
Craig Mautner967212c2013-04-13 21:10:58 -07001842 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07001843 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001844 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1845 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001846 }
1847 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07001848 }
1849
Craig Mautner4a1cb222013-12-04 16:14:06 -08001850 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001851 ActivityRecord homeActivity = getHomeActivity();
1852 if (homeActivity != null) {
1853 return homeActivity.appToken;
1854 }
1855 return null;
1856 }
1857
1858 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07001859 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001860 }
1861
1862 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001863 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
1864 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1865 final TaskRecord task = tasks.get(taskNdx);
1866 if (task.isHomeTask()) {
1867 final ArrayList<ActivityRecord> activities = task.mActivities;
1868 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1869 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001870 if (r.isHomeActivity()
1871 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001872 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001873 }
1874 }
1875 }
1876 }
1877 return null;
1878 }
1879
Chong Zhangb15758a2015-11-17 12:12:03 -08001880 /**
1881 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
1882 * the docked stack itself, or if it's side-by-side to the docked stack.
1883 */
1884 boolean isStackDockedInEffect(int stackId) {
1885 return stackId == DOCKED_STACK_ID ||
1886 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
1887 }
1888
Todd Kennedyca4d8422015-01-15 15:19:22 -08001889 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08001890 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07001891 ActivityContainer activityContainer =
1892 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001893 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001894 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1895 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001896 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001897 return activityContainer;
1898 }
1899
Craig Mautner34b73df2014-01-12 21:11:08 -08001900 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001901 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
1902 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
1903 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001904 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
1905 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001906 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08001907 }
1908 }
1909
Craig Mautner95da1082014-02-24 17:54:35 -08001910 void deleteActivityContainer(IActivityContainer container) {
1911 ActivityContainer activityContainer = (ActivityContainer)container;
1912 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001913 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1914 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08001915 final int stackId = activityContainer.mStackId;
1916 mActivityContainers.remove(stackId);
1917 mWindowManager.removeStack(stackId);
1918 }
1919 }
1920
Jorim Jaggidc249c42015-12-15 14:57:31 -08001921 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
1922 boolean preserveWindows, boolean allowResizeInDockedMode) {
1923 if (stackId == DOCKED_STACK_ID) {
1924 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
1925 preserveWindows);
1926 return;
1927 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08001928 final ActivityStack stack = getStack(stackId);
1929 if (stack == null) {
1930 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
1931 return;
1932 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08001933
Jorim Jaggidc249c42015-12-15 14:57:31 -08001934 if (!allowResizeInDockedMode && getStack(DOCKED_STACK_ID) != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07001935 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
1936 // stack size changing so things don't get out of sync.
1937 return;
1938 }
1939
Wale Ogunwalecad05a02015-09-25 10:41:44 -07001940 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02001941 mWindowManager.deferSurfaceLayout();
1942 try {
Jorim Jaggidc249c42015-12-15 14:57:31 -08001943 resizeStackUncheckedLocked(stack, bounds, tempTaskBounds, tempTaskInsetBounds);
1944 ensureConfigurationAndResume(stack, stack.topRunningActivityLocked(), preserveWindows);
Jorim Jaggic4025202015-10-22 16:43:34 +02001945 } finally {
1946 mWindowManager.continueSurfaceLayout();
1947 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001948 }
1949 }
1950
Jorim Jaggi192086e2016-03-11 17:17:03 +01001951 void deferUpdateBounds(int stackId) {
1952 final ActivityStack stack = getStack(stackId);
1953 if (stack != null) {
1954 stack.deferUpdateBounds();
1955 }
1956 }
1957
1958 void continueUpdateBounds(int stackId) {
1959 final ActivityStack stack = getStack(stackId);
1960 if (stack != null) {
1961 stack.continueUpdateBounds();
1962 }
1963 }
1964
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01001965 void notifyAppTransitionDone() {
1966 continueUpdateBounds(HOME_STACK_ID);
1967 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
1968 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
1969 if (anyTaskForIdLocked(taskId) != null) {
1970 mWindowManager.setTaskDockedResizing(taskId, false);
1971 }
1972 }
1973 mResizingTasksDuringAnimation.clear();
1974 }
1975
Jorim Jaggi192086e2016-03-11 17:17:03 +01001976 void resizeStackUncheckedLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
Jorim Jaggidc249c42015-12-15 14:57:31 -08001977 Rect tempTaskInsetBounds) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001978 bounds = TaskRecord.validateBounds(bounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001979
Jorim Jaggi192086e2016-03-11 17:17:03 +01001980 if (!stack.updateBoundsAllowed(bounds, tempTaskBounds, tempTaskInsetBounds)) {
1981 return;
1982 }
1983
Jorim Jaggidc249c42015-12-15 14:57:31 -08001984 mTmpBounds.clear();
1985 mTmpConfigs.clear();
1986 mTmpInsetBounds.clear();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001987 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
Jorim Jaggif15a7352016-04-04 23:54:30 -07001988 final Rect taskBounds = tempTaskBounds != null ? tempTaskBounds : bounds;
1989 final Rect insetBounds = tempTaskInsetBounds != null ? tempTaskInsetBounds : taskBounds;
Jorim Jaggidc249c42015-12-15 14:57:31 -08001990 for (int i = tasks.size() - 1; i >= 0; i--) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001991 final TaskRecord task = tasks.get(i);
1992 if (task.isResizeable()) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08001993 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1994 // For freeform stack we don't adjust the size of the tasks to match that
1995 // of the stack, but we do try to make sure the tasks are still contained
1996 // with the bounds of the stack.
1997 tempRect2.set(task.mBounds);
1998 fitWithinBounds(tempRect2, bounds);
1999 task.updateOverrideConfiguration(tempRect2);
2000 } else {
Jorim Jaggif15a7352016-04-04 23:54:30 -07002001 task.updateOverrideConfiguration(taskBounds, insetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002002 }
2003 }
2004
2005 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
2006 mTmpBounds.put(task.taskId, task.mBounds);
2007 if (tempTaskInsetBounds != null) {
2008 mTmpInsetBounds.put(task.taskId, tempTaskInsetBounds);
2009 }
2010 }
Jorim Jaggi0429f352015-12-22 16:29:16 +01002011
2012 // We might trigger a configuration change. Save the current task bounds for freezing.
2013 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002014 stack.mFullscreen = mWindowManager.resizeStack(stack.mStackId, bounds, mTmpConfigs,
2015 mTmpBounds, mTmpInsetBounds);
2016 stack.setBounds(bounds);
2017 }
2018
2019 private void ensureConfigurationAndResume(ActivityStack stack, ActivityRecord r,
2020 boolean preserveWindows) {
Jorim Jaggic3fb3142016-02-04 19:49:28 -08002021 if (r == null || !r.visible) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002022 return;
2023 }
2024 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0,
2025 preserveWindows);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002026 if (!updated) {
2027 resumeFocusedStackTopActivityLocked();
2028 }
2029 }
2030
2031 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2032 Rect tempDockedTaskInsetBounds,
2033 Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds, boolean preserveWindows) {
2034 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2035 if (stack == null) {
2036 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2037 return;
2038 }
2039
2040 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2041 mWindowManager.deferSurfaceLayout();
2042 try {
2043 ActivityRecord r = stack.topRunningActivityLocked();
2044 resizeStackUncheckedLocked(stack, dockedBounds, tempDockedTaskBounds,
2045 tempDockedTaskInsetBounds);
2046
2047 if (stack.mFullscreen) {
2048 // The dock stack went fullscreen which is kinda like dismissing it.
2049 // In this case we make all other static stacks fullscreen and move all
2050 // docked stack tasks to the fullscreen stack.
2051 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2052 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
2053 resizeStackLocked(i, null, null, null, preserveWindows,
2054 true /* allowResizeInDockedMode */);
2055 }
2056 }
2057
2058 ArrayList<TaskRecord> tasks = stack.getAllTasks();
2059 final int count = tasks.size();
2060 for (int i = 0; i < count; i++) {
2061 moveTaskToStackLocked(tasks.get(i).taskId,
2062 FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP, FORCE_FOCUS, "resizeStack",
2063 false /* animate */);
2064 }
2065
2066 // stack shouldn't contain anymore activities, so nothing to resume.
2067 r = null;
2068 } else {
2069 // Docked stacks occupy a dedicated region on screen so the size of all other
2070 // static stacks need to be adjusted so they don't overlap with the docked stack.
2071 // We get the bounds to use from window manager which has been adjusted for any
2072 // screen controls and is also the same for all stacks.
Andrii Kulian41da89c2016-04-01 15:21:31 -07002073 if (dockedBounds != null) {
2074 mWindowManager.getStackDockedModeBounds(
2075 HOME_STACK_ID, tempRect, true /* ignoreVisibility */);
2076 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002077 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2078 if (StackId.isResizeableByDockedStack(i)) {
2079 ActivityStack otherStack = getStack(i);
2080 if (otherStack != null) {
Andrii Kulian41da89c2016-04-01 15:21:31 -07002081 resizeStackLocked(i, dockedBounds != null ? tempRect : null,
2082 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
Jorim Jaggidc249c42015-12-15 14:57:31 -08002083 true /* allowResizeInDockedMode */);
2084 }
2085 }
2086 }
2087 }
2088 ensureConfigurationAndResume(stack, r, preserveWindows);
2089 } finally {
2090 mWindowManager.continueSurfaceLayout();
2091 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2092 }
2093
2094 mResizeDockedStackTimeout.notifyResizing(dockedBounds,
2095 tempDockedTaskBounds != null
2096 || tempDockedTaskInsetBounds != null
2097 || tempOtherTaskBounds != null
2098 || tempOtherTaskInsetBounds != null);
2099 }
2100
Robert Carr0d00c2e2016-02-29 17:45:02 -08002101 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
2102 final ActivityStack stack = getStack(PINNED_STACK_ID);
2103 if (stack == null) {
2104 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2105 return;
2106 }
2107 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2108 mWindowManager.deferSurfaceLayout();
2109 try {
2110 ActivityRecord r = stack.topRunningActivityLocked();
2111 resizeStackUncheckedLocked(stack, pinnedBounds, tempPinnedTaskBounds,
2112 null);
2113 ensureConfigurationAndResume(stack, r, false);
2114 } finally {
2115 mWindowManager.continueSurfaceLayout();
2116 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2117 }
2118 }
2119
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002120 boolean resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow,
2121 boolean deferResume) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002122 if (!task.isResizeable()) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002123 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
Chong Zhangf596cd52016-01-05 13:42:44 -08002124 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002125 }
2126
Chong Zhang87b21722015-09-21 15:39:51 -07002127 // If this is a forced resize, let it go through even if the bounds is not changing,
2128 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07002129 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002130 if (Objects.equals(task.mBounds, bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002131 // Nothing to do here...
Chong Zhangf596cd52016-01-05 13:42:44 -08002132 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002133 }
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002134 bounds = TaskRecord.validateBounds(bounds);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002135
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002136 if (!mWindowManager.isValidTaskId(task.taskId)) {
2137 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07002138 // All we can do for now is update the bounds so it can be used when the task is
2139 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002140 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002141 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
2142 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07002143 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002144 }
Chong Zhangf596cd52016-01-05 13:42:44 -08002145 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002146 }
2147
Chong Zhang6de2ae82015-09-30 18:25:21 -07002148 // Do not move the task to another stack here.
2149 // This method assumes that the task is already placed in the right stack.
2150 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002151
Chong Zhang6de2ae82015-09-30 18:25:21 -07002152 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07002153
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07002154 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07002155 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002156 // way and the activity was kept the way it was. If it's false, it means the activity had
2157 // to be relaunched due to configuration change.
2158 boolean kept = true;
2159 if (overrideConfig != null) {
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002160 final ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08002161 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002162 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07002163 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002164
2165 if (!deferResume) {
2166
2167 // All other activities must be made visible with their correct configuration.
2168 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
2169 if (!kept) {
2170 resumeFocusedStackTopActivityLocked();
2171 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002172 }
2173 }
2174 }
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002175 mWindowManager.resizeTask(task.taskId, task.mBounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002176
2177 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Chong Zhangf596cd52016-01-05 13:42:44 -08002178 return kept;
Wale Ogunwale60454db2015-01-23 16:05:07 -08002179 }
2180
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002181 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002182 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2183 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002184 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002185 }
2186
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002187 ActivityContainer activityContainer = new ActivityContainer(stackId);
2188 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002189 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002190 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002191 }
2192
2193 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002194 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002195 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2196 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002197 break;
2198 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002199 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002200 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002201 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002202 }
2203
Chong Zhang5dcb2752015-08-18 13:50:26 -07002204 /**
2205 * Restores a recent task to a stack
2206 * @param task The recent task to be restored.
2207 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale3797c222015-10-27 14:21:58 -07002208 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002209 * @return true if the task has been restored successfully.
2210 */
2211 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
2212 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002213 stackId = task.getLaunchStackId();
Wale Ogunwale513346d2016-01-27 10:55:01 -08002214 } else if (stackId == DOCKED_STACK_ID && !task.canGoInDockedStack()) {
2215 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2216 // Put it in the fullscreen stack.
2217 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002218 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002219
Wale Ogunwale706ed792015-08-02 10:29:44 -07002220 if (task.stack != null) {
2221 // Task has already been restored once. See if we need to do anything more
2222 if (task.stack.mStackId == stackId) {
2223 // Nothing else to do since it is already restored in the right stack.
2224 return true;
2225 }
2226 // Remove current stack association, so we can re-associate the task with the
2227 // right stack below.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002228 task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002229 }
2230
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002231 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002232 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002233
2234 if (stack == null) {
2235 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002236 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2237 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002238 return false;
2239 }
2240
Wale Ogunwale5f986092015-12-04 15:35:38 -08002241 stack.addTask(task, false, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002242 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2243 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002244 final ArrayList<ActivityRecord> activities = task.mActivities;
2245 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002246 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002247 }
2248 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002249 }
2250
Wale Ogunwale040b4702015-08-06 18:10:50 -07002251 /**
2252 * Moves the specified task record to the input stack id.
2253 * WARNING: This method performs an unchecked/raw move of the task and
2254 * can leave the system in an unstable state if used incorrectly.
Wale Ogunwale513346d2016-01-27 10:55:01 -08002255 * Use {@link #moveTaskToStackLocked} to perform safe task movement to a stack.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002256 * @param task Task to move.
2257 * @param stackId Id of stack to move task to.
2258 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07002259 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07002260 * @param reason Reason the task is been moved.
2261 * @return The stack the task was moved to.
2262 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07002263 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07002264 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
Wale Ogunwalefb1c8642016-03-02 08:28:08 -08002265
2266 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2267 throw new IllegalStateException("moveTaskToStackUncheckedLocked: Device doesn't "
2268 + "support multi-window task=" + task + " to stackId=" + stackId);
2269 }
2270
Chong Zhang02898352015-08-21 17:27:14 -07002271 final ActivityRecord r = task.getTopActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -08002272 final ActivityStack prevStack = task.stack;
2273 final boolean wasFocused = isFocusedStack(prevStack) && (topRunningActivityLocked() == r);
2274 final boolean wasResumed = wasFocused && (prevStack.mResumedActivity == r);
2275 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
2276 // Whenever we are moving the top activity from the front stack we want to make sure to move
2277 // the stack to the front.
2278 final boolean wasFront = isFrontStack(prevStack)
2279 && (prevStack.topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07002280
Chong Zhangd545dce2016-02-29 18:09:17 -08002281 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002282 // We don't allow moving a unresizeable task to the docked stack since the docked
2283 // stack is used for split-screen mode and will cause things like the docked divider to
2284 // show up. We instead leave the task in its current stack or move it to the fullscreen
2285 // stack if it isn't currently in a stack.
2286 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002287 Slog.w(TAG, "Can not move unresizeable task=" + task
2288 + " to docked stack. Moving to stackId=" + stackId + " instead.");
2289 }
2290
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002291 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
2292 // if a docked stack is created below which will lead to the stack we are moving from and
2293 // its resizeable tasks being resized.
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002294 task.mTemporarilyUnresizable = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002295 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002296 task.mTemporarilyUnresizable = false;
Wale Ogunwale040b4702015-08-06 18:10:50 -07002297 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002298 stack.addTask(task, toTop, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002299
2300 // If the task had focus before (or we're requested to move focus),
Wale Ogunwaled046a012015-12-24 13:05:59 -08002301 // move focus to the new stack by moving the stack to the front.
2302 stack.moveToFrontAndResumeStateIfNeeded(
2303 r, forceFocus || wasFocused || wasFront, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002304
Wale Ogunwale040b4702015-08-06 18:10:50 -07002305 return stack;
2306 }
2307
Chong Zhange4fbd322016-03-01 14:44:03 -08002308 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08002309 String reason, boolean animate) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002310 return moveTaskToStackLocked(taskId, stackId, toTop, forceFocus, reason, animate,
2311 false /* deferResume */);
2312 }
2313
2314 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
2315 String reason, boolean animate, boolean deferResume) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002316 final TaskRecord task = anyTaskForIdLocked(taskId);
2317 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002318 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002319 return false;
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002320 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002321
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002322 if (task.stack != null && task.stack.mStackId == stackId) {
2323 // You are already in the right stack silly...
2324 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002325 return true;
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002326 }
2327
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002328 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
2329 throw new IllegalArgumentException("moveTaskToStack:"
2330 + "Attempt to move task " + taskId + " to unsupported freeform stack");
2331 }
2332
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002333 final ActivityRecord topActivity = task.getTopActivity();
Jorim Jaggie9098022016-01-27 19:29:40 -08002334 final int sourceStackId = task.stack != null ? task.stack.mStackId : INVALID_STACK_ID;
Chong Zhangf596cd52016-01-05 13:42:44 -08002335 final boolean mightReplaceWindow =
Jorim Jaggie9098022016-01-27 19:29:40 -08002336 StackId.replaceWindowsOnTaskMove(sourceStackId, stackId) && topActivity != null;
Chong Zhangf596cd52016-01-05 13:42:44 -08002337 if (mightReplaceWindow) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002338 // We are about to relaunch the activity because its configuration changed due to
2339 // being maximized, i.e. size change. The activity will first remove the old window
2340 // and then add a new one. This call will tell window manager about this, so it can
2341 // preserve the old window until the new one is drawn. This prevents having a gap
2342 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07002343 // entrance of the new window to be properly animated.
Chong Zhangf596cd52016-01-05 13:42:44 -08002344 // Note here we always set the replacing window first, as the flags might be needed
2345 // during the relaunch. If we end up not doing any relaunch, we clear the flags later.
Jorim Jaggi030979c2015-11-20 15:14:43 -08002346 mWindowManager.setReplacingWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002347 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002348
2349 mWindowManager.deferSurfaceLayout();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002350 final int preferredLaunchStackId = stackId;
Chong Zhangf596cd52016-01-05 13:42:44 -08002351 boolean kept = true;
Wale Ogunwale961f4852016-02-01 20:25:54 -08002352 try {
2353 final ActivityStack stack = moveTaskToStackUncheckedLocked(
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002354 task, stackId, toTop, forceFocus, reason + " moveTaskToStack");
Wale Ogunwale961f4852016-02-01 20:25:54 -08002355 stackId = stack.mStackId;
Jorim Jaggie9098022016-01-27 19:29:40 -08002356
Wale Ogunwale961f4852016-02-01 20:25:54 -08002357 if (!animate) {
2358 stack.mNoAnimActivities.add(topActivity);
2359 }
Jorim Jaggie9098022016-01-27 19:29:40 -08002360
Wale Ogunwale961f4852016-02-01 20:25:54 -08002361 // We might trigger a configuration change. Save the current task bounds for freezing.
2362 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
2363
2364 // Make sure the task has the appropriate bounds/size for the stack it is in.
2365 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002366 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2367 !mightReplaceWindow, deferResume);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002368 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID) {
2369 Rect bounds = task.getLaunchBounds();
2370 if (bounds == null) {
2371 stack.layoutTaskInStack(task, null);
2372 bounds = task.mBounds;
2373 }
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002374 kept = resizeTaskLocked(task, bounds, RESIZE_MODE_FORCED, !mightReplaceWindow,
2375 deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002376 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002377 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2378 !mightReplaceWindow, deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002379 }
2380 } finally {
2381 mWindowManager.continueSurfaceLayout();
Chong Zhangf596cd52016-01-05 13:42:44 -08002382 }
2383
2384 if (mightReplaceWindow) {
2385 // If we didn't actual do a relaunch (indicated by kept==true meaning we kept the old
2386 // window), we need to clear the replace window settings. Otherwise, we schedule a
2387 // timeout to remove the old window if the replacing window is not coming in time.
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002388 mWindowManager.scheduleClearReplacingWindowIfNeeded(topActivity.appToken, !kept);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002389 }
2390
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002391 if (!deferResume) {
2392
2393 // The task might have already been running and its visibility needs to be synchronized with
2394 // the visibility of the stack / windows.
2395 ensureActivitiesVisibleLocked(null, 0, !mightReplaceWindow);
2396 resumeFocusedStackTopActivityLocked();
2397 }
Chong Zhangb15758a2015-11-17 12:12:03 -08002398
Jorim Jaggid53f0922016-04-06 22:16:23 -07002399 handleNonResizableTaskIfNeeded(task, preferredLaunchStackId, stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002400
2401 return (preferredLaunchStackId == stackId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002402 }
2403
Wale Ogunwale079a0042015-10-24 11:44:07 -07002404 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2405 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2406 if (stack == null) {
2407 throw new IllegalArgumentException(
2408 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2409 }
2410
2411 final ActivityRecord r = stack.topRunningActivityLocked();
2412 if (r == null) {
2413 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2414 + " in stack=" + stack);
2415 return false;
2416 }
2417
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002418 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002419 Slog.w(TAG,
2420 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002421 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002422 return false;
2423 }
2424
Wale Ogunwale480dca02016-02-06 13:58:29 -08002425 moveActivityToPinnedStackLocked(r, "moveTopActivityToPinnedStack", bounds);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002426 return true;
2427 }
2428
Wale Ogunwale480dca02016-02-06 13:58:29 -08002429 void moveActivityToPinnedStackLocked(ActivityRecord r, String reason, Rect bounds) {
2430 mWindowManager.deferSurfaceLayout();
2431 try {
2432 final TaskRecord task = r.task;
2433
2434 // Need to make sure the pinned stack exist so we can resize it below...
2435 final ActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
2436
2437 // Resize the pinned stack to match the current size of the task the activity we are
2438 // going to be moving is currently contained in. We do this to have the right starting
2439 // animation bounds for the pinned stack to the desired bounds the caller wants.
2440 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2441 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
2442 true /* allowResizeInDockedMode */);
2443
2444 if (task.mActivities.size() == 1) {
2445 // There is only one activity in the task. So, we can just move the task over to
2446 // the stack without re-parenting the activity in a different task.
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08002447 if (task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE) {
2448 // Move the home stack forward if the task we just moved to the pinned stack
2449 // was launched from home so home should be visible behind it.
2450 moveHomeStackToFront(reason);
2451 }
Wale Ogunwale480dca02016-02-06 13:58:29 -08002452 moveTaskToStackLocked(
2453 task.taskId, PINNED_STACK_ID, ON_TOP, FORCE_FOCUS, reason, !ANIMATE);
2454 } else {
2455 stack.moveActivityToStack(r);
2456 }
2457 } finally {
2458 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002459 }
2460
Wale Ogunwale480dca02016-02-06 13:58:29 -08002461 // The task might have already been running and its visibility needs to be synchronized
2462 // with the visibility of the stack / windows.
Wale Ogunwale079a0042015-10-24 11:44:07 -07002463 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002464 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002465
Wale Ogunwalee75a9ad2016-03-18 20:43:49 -07002466 mWindowManager.animateResizePinnedStack(bounds, -1);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002467 mService.notifyActivityPinnedLocked();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002468 }
2469
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002470 void positionTaskInStackLocked(int taskId, int stackId, int position) {
2471 final TaskRecord task = anyTaskForIdLocked(taskId);
2472 if (task == null) {
2473 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
2474 return;
2475 }
Wale Ogunwale935e5022015-11-10 12:36:10 -08002476 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
2477
2478 task.updateOverrideConfigurationForStack(stack);
2479
2480 mWindowManager.positionTaskInStack(
2481 taskId, stackId, position, task.mBounds, task.mOverrideConfig);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002482 stack.positionTask(task, position);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002483 // The task might have already been running and its visibility needs to be synchronized with
2484 // the visibility of the stack / windows.
Jorim Jaggi8fa45222016-02-19 19:54:39 -08002485 stack.ensureActivityConfigurationLocked(task.topRunningActivityLocked(), 0,
2486 !PRESERVE_WINDOWS);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002487 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002488 resumeFocusedStackTopActivityLocked();
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002489 }
2490
Craig Mautnerac6f8432013-07-17 13:24:59 -07002491 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002492 mTmpFindTaskResult.r = null;
2493 mTmpFindTaskResult.matchedByRootAffinity = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002494 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002495 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2496 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002497 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2498 final ActivityStack stack = stacks.get(stackNdx);
2499 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002500 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002501 continue;
2502 }
2503 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002504 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2505 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002506 continue;
2507 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002508 stack.findTaskLocked(r, mTmpFindTaskResult);
2509 // It is possible to have task in multiple stacks with the same root affinity.
2510 // If the match we found was based on root affinity we keep on looking to see if
2511 // there is a better match in another stack. We eventually return the match based
2512 // on root affinity if we don't find a better match.
2513 if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
2514 return mTmpFindTaskResult.r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002515 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002516 }
2517 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002518 if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
2519 return mTmpFindTaskResult.r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002520 }
2521
2522 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002523 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2524 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002525 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2526 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2527 if (ar != null) {
2528 return ar;
2529 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002530 }
2531 }
2532 return null;
2533 }
2534
Craig Mautner8d341ef2013-03-26 09:03:27 -07002535 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002536 scheduleSleepTimeout();
2537 if (!mGoingToSleep.isHeld()) {
2538 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002539 if (mLaunchingActivity.isHeld()) {
2540 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2541 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002542 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002543 mLaunchingActivity.release();
2544 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002545 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002546 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002547 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002548 }
2549
2550 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002551 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002552
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002553 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002554 final long endTime = System.currentTimeMillis() + timeout;
2555 while (true) {
2556 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002557 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2558 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002559 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2560 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2561 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002562 }
2563 if (cantShutdown) {
2564 long timeRemaining = endTime - System.currentTimeMillis();
2565 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002566 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002567 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002568 } catch (InterruptedException e) {
2569 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002570 } else {
2571 Slog.w(TAG, "Activity manager shutdown timed out");
2572 timedout = true;
2573 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002574 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002575 } else {
2576 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002577 }
2578 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002579
2580 // Force checkReadyForSleep to complete.
2581 mSleepTimeout = true;
2582 checkReadyForSleepLocked();
2583
Craig Mautner8d341ef2013-03-26 09:03:27 -07002584 return timedout;
2585 }
2586
2587 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002588 removeSleepTimeouts();
2589 if (mGoingToSleep.isHeld()) {
2590 mGoingToSleep.release();
2591 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002592 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2593 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002594 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2595 final ActivityStack stack = stacks.get(stackNdx);
2596 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002597 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002598 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002599 }
Craig Mautner5314a402013-09-26 12:40:16 -07002600 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002601 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002602 mGoingToSleepActivities.clear();
2603 }
2604
2605 void activitySleptLocked(ActivityRecord r) {
2606 mGoingToSleepActivities.remove(r);
2607 checkReadyForSleepLocked();
2608 }
2609
2610 void checkReadyForSleepLocked() {
2611 if (!mService.isSleepingOrShuttingDown()) {
2612 // Do not care.
2613 return;
2614 }
2615
2616 if (!mSleepTimeout) {
2617 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002618 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2619 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002620 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2621 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2622 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002623 }
2624
2625 if (mStoppingActivities.size() > 0) {
2626 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002627 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002628 + mStoppingActivities.size() + " activities");
2629 scheduleIdleLocked();
2630 dontSleep = true;
2631 }
2632
2633 if (mGoingToSleepActivities.size() > 0) {
2634 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002635 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002636 + mGoingToSleepActivities.size() + " activities");
2637 dontSleep = true;
2638 }
2639
2640 if (dontSleep) {
2641 return;
2642 }
2643 }
2644
Craig Mautnere0a38842013-12-16 16:14:02 -08002645 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2646 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002647 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2648 stacks.get(stackNdx).goToSleep();
2649 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002650 }
2651
2652 removeSleepTimeouts();
2653
2654 if (mGoingToSleep.isHeld()) {
2655 mGoingToSleep.release();
2656 }
2657 if (mService.mShuttingDown) {
2658 mService.notifyAll();
2659 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002660 }
2661
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002662 boolean reportResumedActivityLocked(ActivityRecord r) {
2663 final ActivityStack stack = r.task.stack;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002664 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002665 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002666 }
2667 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002668 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002669 mWindowManager.executeAppTransition();
2670 return true;
2671 }
2672 return false;
2673 }
2674
Craig Mautner8d341ef2013-03-26 09:03:27 -07002675 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002676 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2677 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07002678 int stackNdx = stacks.size() - 1;
2679 while (stackNdx >= 0) {
2680 stacks.get(stackNdx).handleAppCrashLocked(app);
2681 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002682 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002683 }
2684 }
2685
Jose Lima4b6c6692014-08-12 17:41:12 -07002686 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002687 final ActivityStack stack = r.task.stack;
2688 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002689 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2690 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002691 return false;
2692 }
Jose Lima4b6c6692014-08-12 17:41:12 -07002693 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002694 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2695 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002696
2697 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002698 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002699 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07002700 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002701 return true;
2702 }
2703
2704 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07002705 if (visible && top.fullscreen) {
2706 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002707 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2708 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
2709 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
2710 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002711 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07002712 } else if (!visible && stack.getVisibleBehindActivity() != r) {
2713 // Only the activity set as currently visible behind should actively reset its
2714 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002715 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2716 "requestVisibleBehind: returning visible=" + visible
2717 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
2718 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07002719 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002720 }
2721
Jose Lima4b6c6692014-08-12 17:41:12 -07002722 stack.setVisibleBehindActivity(visible ? r : null);
2723 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002724 // If there is a translucent home activity, we need to force it stop being translucent,
2725 // because we can't depend on the application to necessarily perform that operation.
2726 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002727 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002728 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002729 mService.convertFromTranslucent(next.appToken);
2730 }
2731 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07002732 if (top.app != null && top.app.thread != null) {
2733 // Notify the top app of the change.
2734 try {
2735 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
2736 } catch (RemoteException e) {
2737 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07002738 }
2739 return true;
2740 }
2741
Craig Mautnerbb742462014-07-07 15:28:55 -07002742 // Called when WindowManager has finished animating the launchingBehind activity to the back.
2743 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Craig Mautnerbb742462014-07-07 15:28:55 -07002744 final TaskRecord task = r.task;
Winson730bf062016-03-31 18:04:56 -07002745 final ActivityStack stack = task.stack;
2746
2747 r.mLaunchTaskBehind = false;
2748 task.setLastThumbnailLocked(stack.screenshotActivitiesLocked(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002749 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08002750 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07002751 mWindowManager.setAppVisibility(r.appToken, false);
Winson730bf062016-03-31 18:04:56 -07002752
2753 // When launching tasks behind, update the last active time of the top task after the new
2754 // task has been shown briefly
2755 final ActivityRecord top = stack.topActivity();
2756 if (top != null) {
2757 top.task.touchActiveTime();
2758 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002759 }
2760
2761 void scheduleLaunchTaskBehindComplete(IBinder token) {
2762 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2763 }
2764
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002765 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
2766 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07002767 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002768 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2769 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002770 final int topStackNdx = stacks.size() - 1;
2771 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2772 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002773 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07002774 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002775 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002776 }
2777
Chong Zhangfdcc4d42015-10-14 16:50:12 -07002778 void invalidateTaskLayers() {
2779 mTaskLayersChanged = true;
2780 }
2781
2782 void rankTaskLayersIfNeeded() {
2783 if (!mTaskLayersChanged) {
2784 return;
2785 }
2786 mTaskLayersChanged = false;
2787 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
2788 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2789 int baseLayer = 0;
2790 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2791 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
2792 }
2793 }
2794 }
2795
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002796 void clearOtherAppTimeTrackers(AppTimeTracker except) {
2797 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2798 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2799 final int topStackNdx = stacks.size() - 1;
2800 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2801 final ActivityStack stack = stacks.get(stackNdx);
2802 stack.clearOtherAppTimeTrackers(except);
2803 }
2804 }
2805 }
2806
Craig Mautner8d341ef2013-03-26 09:03:27 -07002807 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002808 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2809 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002810 final int numStacks = stacks.size();
2811 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2812 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002813 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002814 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002815 }
2816 }
2817
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002818 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
2819 // Examine all activities currently running in the process.
2820 TaskRecord firstTask = null;
2821 // Tasks is non-null only if two or more tasks are found.
2822 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002823 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
2824 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002825 ActivityRecord r = app.activities.get(i);
2826 // First, if we find an activity that is in the process of being destroyed,
2827 // then we just aren't going to do anything for now; we want things to settle
2828 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002829 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002830 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002831 return;
2832 }
2833 // Don't consider any activies that are currently not in a state where they
2834 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002835 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
2836 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002837 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002838 continue;
2839 }
2840 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002841 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002842 + " from " + r);
2843 if (firstTask == null) {
2844 firstTask = r.task;
2845 } else if (firstTask != r.task) {
2846 if (tasks == null) {
2847 tasks = new ArraySet<>();
2848 tasks.add(firstTask);
2849 }
2850 tasks.add(r.task);
2851 }
2852 }
2853 }
2854 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002855 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002856 return;
2857 }
2858 // If we have activities in multiple tasks that are in a position to be destroyed,
2859 // let's iterate through the tasks and release the oldest one.
2860 final int numDisplays = mActivityDisplays.size();
2861 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
2862 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2863 // Step through all stacks starting from behind, to hit the oldest things first.
2864 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
2865 final ActivityStack stack = stacks.get(stackNdx);
2866 // Try to release activities in this stack; if we manage to, we are done.
2867 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
2868 return;
2869 }
2870 }
2871 }
2872 }
2873
Amith Yamasani37a40c22015-06-17 13:25:42 -07002874 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002875 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002876 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07002877 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002878
Craig Mautner858d8a62013-04-23 17:08:34 -07002879 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08002880 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2881 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002882 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002883 final ActivityStack stack = stacks.get(stackNdx);
2884 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002885 TaskRecord task = stack.topTask();
2886 if (task != null) {
2887 mWindowManager.moveTaskToTop(task.taskId);
2888 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002889 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07002890 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002891
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002892 ActivityStack stack = getStack(restoreStackId);
2893 if (stack == null) {
2894 stack = mHomeStack;
2895 }
2896 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002897 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07002898 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08002899 } else {
2900 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08002901 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08002902 }
Craig Mautner93529a42013-10-04 15:03:13 -07002903 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07002904 }
2905
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002906 /** Checks whether the userid is a profile of the current user. */
2907 boolean isCurrentProfileLocked(int userId) {
2908 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002909 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002910 }
2911
Chong Zhang45c25ce2015-08-10 22:18:26 -07002912 /** Checks whether the activity should be shown for current user. */
2913 boolean okToShowLocked(ActivityRecord r) {
2914 return r != null && (isCurrentProfileLocked(r.userId)
2915 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
2916 }
2917
Craig Mautnerde4ef022013-04-07 19:01:33 -07002918 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002919 ArrayList<ActivityRecord> stops = null;
2920
2921 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08002922 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2923 ActivityRecord s = mStoppingActivities.get(activityNdx);
2924 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002925 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08002926 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
2927 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002928 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002929 if (s.finishing) {
2930 // If this activity is finishing, it is sitting on top of
2931 // everyone else but we now know it is no longer needed...
2932 // so get rid of it. Otherwise, we need to go through the
2933 // normal flow and hide it once we determine that it is
2934 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002935 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002936 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002937 }
2938 }
Craig Mautner8c14c152015-01-15 17:32:07 -08002939 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002940 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002941 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08002942 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002943 }
2944 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08002945 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002946 }
2947 }
2948
2949 return stops;
2950 }
2951
Craig Mautnercf910b02013-04-23 11:23:27 -07002952 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002953 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2954 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2955 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2956 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002957 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002958 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002959 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002960 if (r == null) Slog.e(TAG,
2961 "validateTop...: null top activity, stack=" + stack);
2962 else {
2963 final ActivityRecord pausing = stack.mPausingActivity;
2964 if (pausing != null && pausing == r) Slog.e(TAG,
2965 "validateTop...: top stack has pausing activity r=" + r
2966 + " state=" + state);
2967 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
2968 "validateTop...: activity in front not resumed r=" + r
2969 + " state=" + state);
2970 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002971 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002972 final ActivityRecord resumed = stack.mResumedActivity;
2973 if (resumed != null && resumed == r) Slog.e(TAG,
2974 "validateTop...: back stack has resumed activity r=" + r
2975 + " state=" + state);
2976 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
2977 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002978 }
2979 }
2980 }
Craig Mautner76ea2242013-05-15 11:40:05 -07002981 }
2982
Craig Mautnere0570202015-05-13 13:06:11 -07002983 private String lockTaskModeToString() {
2984 switch (mLockTaskModeState) {
2985 case LOCK_TASK_MODE_LOCKED:
2986 return "LOCKED";
2987 case LOCK_TASK_MODE_PINNED:
2988 return "PINNED";
2989 case LOCK_TASK_MODE_NONE:
2990 return "NONE";
2991 default: return "unknown=" + mLockTaskModeState;
2992 }
2993 }
2994
Craig Mautner27084302013-03-25 08:05:25 -07002995 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002996 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002997 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002998 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08002999 pw.print(prefix);
3000 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003001 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003002 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003003 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
3004 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3005 if (packages.size() > 0) {
3006 pw.println(" mLockTaskPackages (userId:packages)=");
3007 for (int i = 0; i < packages.size(); ++i) {
3008 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3009 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3010 }
3011 }
3012 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003013 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003014
Craig Mautner20e72272013-04-01 13:45:53 -07003015 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003016 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003017 }
3018
Dianne Hackborn390517b2013-05-30 15:03:32 -07003019 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3020 boolean needSep, String prefix) {
3021 if (activity != null) {
3022 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3023 if (needSep) {
3024 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003025 }
3026 pw.print(prefix);
3027 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003028 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003029 }
3030 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003031 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003032 }
3033
Craig Mautner8d341ef2013-03-26 09:03:27 -07003034 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3035 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003036 boolean printed = false;
3037 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003038 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3039 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003040 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003041 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003042 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003043 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003044 final ActivityStack stack = stacks.get(stackNdx);
3045 StringBuilder stackHeader = new StringBuilder(128);
3046 stackHeader.append(" Stack #");
3047 stackHeader.append(stack.mStackId);
3048 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003049 stackHeader.append("\n");
3050 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3051 stackHeader.append("\n");
3052 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003053 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3054 needSep, stackHeader.toString());
3055 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3056 !dumpAll, false, dumpPackage, true,
3057 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003058
Craig Mautner4a1cb222013-12-04 16:14:06 -08003059 needSep = printed;
3060 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3061 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003062 if (pr) {
3063 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003064 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003065 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003066 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3067 " mResumedActivity: ");
3068 if (pr) {
3069 printed = true;
3070 needSep = false;
3071 }
3072 if (dumpAll) {
3073 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3074 " mLastPausedActivity: ");
3075 if (pr) {
3076 printed = true;
3077 needSep = true;
3078 }
3079 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3080 needSep, " mLastNoHistoryActivity: ");
3081 }
3082 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003083 }
3084 }
3085
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003086 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3087 false, dumpPackage, true, " Activities waiting to finish:", null);
3088 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3089 false, dumpPackage, true, " Activities waiting to stop:", null);
3090 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3091 false, dumpPackage, true, " Activities waiting for another to become visible:",
3092 null);
3093 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3094 false, dumpPackage, true, " Activities waiting to sleep:", null);
3095 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3096 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003097
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003098 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003099 }
3100
Dianne Hackborn390517b2013-05-30 15:03:32 -07003101 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003102 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003103 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003104 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003105 String innerPrefix = null;
3106 String[] args = null;
3107 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003108 for (int i=list.size()-1; i>=0; i--) {
3109 final ActivityRecord r = list.get(i);
3110 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3111 continue;
3112 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003113 if (innerPrefix == null) {
3114 innerPrefix = prefix + " ";
3115 args = new String[0];
3116 }
3117 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003118 final boolean full = !brief && (complete || !r.isInHistory());
3119 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003120 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003121 needNL = false;
3122 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003123 if (header1 != null) {
3124 pw.println(header1);
3125 header1 = null;
3126 }
3127 if (header2 != null) {
3128 pw.println(header2);
3129 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003130 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003131 if (lastTask != r.task) {
3132 lastTask = r.task;
3133 pw.print(prefix);
3134 pw.print(full ? "* " : " ");
3135 pw.println(lastTask);
3136 if (full) {
3137 lastTask.dump(pw, prefix + " ");
3138 } else if (complete) {
3139 // Complete + brief == give a summary. Isn't that obvious?!?
3140 if (lastTask.intent != null) {
3141 pw.print(prefix); pw.print(" ");
3142 pw.println(lastTask.intent.toInsecureStringWithClip());
3143 }
3144 }
3145 }
3146 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3147 pw.print(" #"); pw.print(i); pw.print(": ");
3148 pw.println(r);
3149 if (full) {
3150 r.dump(pw, innerPrefix);
3151 } else if (complete) {
3152 // Complete + brief == give a summary. Isn't that obvious?!?
3153 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3154 if (r.app != null) {
3155 pw.print(innerPrefix); pw.println(r.app);
3156 }
3157 }
3158 if (client && r.app != null && r.app.thread != null) {
3159 // flush anything that is already in the PrintWriter since the thread is going
3160 // to write to the file descriptor directly
3161 pw.flush();
3162 try {
3163 TransferPipe tp = new TransferPipe();
3164 try {
3165 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3166 r.appToken, innerPrefix, args);
3167 // Short timeout, since blocking here can
3168 // deadlock with the application.
3169 tp.go(fd, 2000);
3170 } finally {
3171 tp.kill();
3172 }
3173 } catch (IOException e) {
3174 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3175 } catch (RemoteException e) {
3176 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3177 }
3178 needNL = true;
3179 }
3180 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003181 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003182 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003183
Craig Mautnerf3333272013-04-22 10:55:53 -07003184 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003185 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3186 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003187 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3188 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003189 }
3190
3191 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003192 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003193 }
3194
3195 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003196 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3197 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003198 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3199 }
3200
Craig Mautner05d29032013-05-03 13:40:13 -07003201 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003202 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3203 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3204 }
Craig Mautner05d29032013-05-03 13:40:13 -07003205 }
3206
Craig Mautner0eea92c2013-05-16 13:35:39 -07003207 void removeSleepTimeouts() {
3208 mSleepTimeout = false;
3209 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3210 }
3211
3212 final void scheduleSleepTimeout() {
3213 removeSleepTimeouts();
3214 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3215 }
3216
Craig Mautner4a1cb222013-12-04 16:14:06 -08003217 @Override
3218 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003219 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003220 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3221 }
3222
3223 @Override
3224 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003225 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003226 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3227 }
3228
3229 @Override
3230 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003231 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003232 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3233 }
3234
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003235 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003236 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003237 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003238 newDisplay = mActivityDisplays.get(displayId) == null;
3239 if (newDisplay) {
3240 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003241 if (activityDisplay.mDisplay == null) {
3242 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3243 return;
3244 }
Craig Mautner4504de52013-12-20 09:06:56 -08003245 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003246 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08003247 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003248 }
Craig Mautner4504de52013-12-20 09:06:56 -08003249 if (newDisplay) {
3250 mWindowManager.onDisplayAdded(displayId);
3251 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003252 }
3253
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003254 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
3255 if (display.mDisplayId != Display.DEFAULT_DISPLAY) {
3256 return;
3257 }
3258 final float fraction = mService.mContext.getResources().getFraction(com.android.internal.R.
3259 fraction.config_displayFractionForDefaultMinimalSizeOfResizeableTask, 1, 1);
3260 mDefaultMinimalSizeOfResizeableTask = (int) (fraction * Math.min(
3261 display.mDisplayInfo.logicalWidth, display.mDisplayInfo.logicalHeight));
3262 }
3263
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003264 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003265 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003266 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3267 if (activityDisplay != null) {
3268 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003269 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003270 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003271 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003272 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003273 }
3274 }
3275 mWindowManager.onDisplayRemoved(displayId);
3276 }
3277
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003278 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003279 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003280 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3281 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003282 // TODO: Update the bounds.
3283 }
3284 }
3285 mWindowManager.onDisplayChanged(displayId);
3286 }
3287
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003288 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003289 StackInfo info = new StackInfo();
3290 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3291 info.displayId = Display.DEFAULT_DISPLAY;
3292 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003293 info.userId = stack.mCurrentUser;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003294
3295 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3296 final int numTasks = tasks.size();
3297 int[] taskIds = new int[numTasks];
3298 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003299 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003300 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003301 for (int i = 0; i < numTasks; ++i) {
3302 final TaskRecord task = tasks.get(i);
3303 taskIds[i] = task.taskId;
3304 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3305 : task.realActivity != null ? task.realActivity.flattenToString()
3306 : task.getTopActivity() != null ? task.getTopActivity().packageName
3307 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003308 taskBounds[i] = new Rect();
3309 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003310 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003311 }
3312 info.taskIds = taskIds;
3313 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003314 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003315 info.taskUserIds = taskUserIds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003316 return info;
3317 }
3318
3319 StackInfo getStackInfoLocked(int stackId) {
3320 ActivityStack stack = getStack(stackId);
3321 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003322 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003323 }
3324 return null;
3325 }
3326
3327 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003328 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003329 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3330 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003331 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003332 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003333 }
3334 }
3335 return list;
3336 }
3337
Craig Mautner15df08a2015-04-01 12:17:18 -07003338 TaskRecord getLockedTaskLocked() {
3339 final int top = mLockTaskModeTasks.size() - 1;
3340 if (top >= 0) {
3341 return mLockTaskModeTasks.get(top);
3342 }
3343 return null;
3344 }
3345
3346 boolean isLockedTask(TaskRecord task) {
3347 return mLockTaskModeTasks.contains(task);
3348 }
3349
3350 boolean isLastLockedTask(TaskRecord task) {
3351 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3352 }
3353
3354 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003355 if (!mLockTaskModeTasks.remove(task)) {
3356 return;
3357 }
3358 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3359 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003360 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003361 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3362 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003363 final Message lockTaskMsg = Message.obtain();
3364 lockTaskMsg.arg1 = task.userId;
3365 lockTaskMsg.what = LOCK_TASK_END_MSG;
3366 mHandler.sendMessage(lockTaskMsg);
3367 }
3368 }
3369
Jorim Jaggid53f0922016-04-06 22:16:23 -07003370 void handleNonResizableTaskIfNeeded(
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003371 TaskRecord task, int preferredStackId, int actualStackId) {
Jorim Jaggid53f0922016-04-06 22:16:23 -07003372 if ((!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID)
3373 || task.isHomeTask()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003374 return;
3375 }
3376
Jorim Jaggi2adba072016-03-03 13:43:39 +01003377 if (!task.canGoInDockedStack()) {
3378 // Display a warning toast that we tried to put a non-dockable task in the docked stack.
Jorim Jaggid53f0922016-04-06 22:16:23 -07003379 mService.mHandler.sendEmptyMessage(NOTIFY_ACTIVITY_DISMISSING_DOCKED_STACK_MSG);
3380
3381 // Dismiss docked stack.
3382 mService.moveTasksToFullscreenStack(DOCKED_STACK_ID, false);
Jorim Jaggi2adba072016-03-03 13:43:39 +01003383 } else if (task.mResizeMode == RESIZE_MODE_FORCE_RESIZEABLE) {
3384 String packageName = task.getTopActivity() != null
3385 ? task.getTopActivity().appInfo.packageName : null;
3386 mService.mHandler.obtainMessage(NOTIFY_FORCED_RESIZABLE_MSG, task.taskId, 0,
3387 packageName).sendToTarget();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003388 }
Chong Zhangb15758a2015-11-17 12:12:03 -08003389 }
3390
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003391 void showLockTaskToast() {
3392 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04003393 }
3394
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003395 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3396 if (mLockTaskModeTasks.contains(task)) {
3397 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3398 }
3399 }
3400
Craig Mautner432f64e2015-05-20 14:59:57 -07003401 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3402 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003403 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003404 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003405 final TaskRecord lockedTask = getLockedTaskLocked();
3406 if (lockedTask != null) {
3407 removeLockedTaskLocked(lockedTask);
3408 if (!mLockTaskModeTasks.isEmpty()) {
3409 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003410 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3411 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003412 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003413 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003414 return;
3415 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003416 }
Craig Mautnere0570202015-05-13 13:06:11 -07003417 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3418 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003419 return;
3420 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003421
3422 // Should have already been checked, but do it again.
3423 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003424 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3425 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003426 return;
3427 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003428 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003429 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003430 return;
3431 }
3432
3433 if (mLockTaskModeTasks.isEmpty()) {
3434 // First locktask.
3435 final Message lockTaskMsg = Message.obtain();
3436 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3437 lockTaskMsg.arg1 = task.userId;
3438 lockTaskMsg.what = LOCK_TASK_START_MSG;
3439 lockTaskMsg.arg2 = lockTaskModeState;
3440 mHandler.sendMessage(lockTaskMsg);
3441 }
3442 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003443 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3444 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003445 mLockTaskModeTasks.remove(task);
3446 mLockTaskModeTasks.add(task);
3447
3448 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07003449 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07003450 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003451
3452 if (andResume) {
3453 findTaskToMoveToFrontLocked(task, 0, null, reason);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003454 resumeFocusedStackTopActivityLocked();
Craig Mautner432f64e2015-05-20 14:59:57 -07003455 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003456 }
3457
3458 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003459 return isLockTaskModeViolation(task, false);
3460 }
3461
3462 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3463 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003464 return false;
3465 }
3466 final int lockTaskAuth = task.mLockTaskAuth;
3467 switch (lockTaskAuth) {
3468 case LOCK_TASK_AUTH_DONT_LOCK:
3469 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003470 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003471 case LOCK_TASK_AUTH_LAUNCHABLE:
3472 case LOCK_TASK_AUTH_WHITELISTED:
3473 return false;
3474 case LOCK_TASK_AUTH_PINNABLE:
3475 // Pinnable tasks can't be launched on top of locktask tasks.
3476 return !mLockTaskModeTasks.isEmpty();
3477 default:
3478 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3479 return true;
3480 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003481 }
3482
Craig Mautner15df08a2015-04-01 12:17:18 -07003483 void onLockTaskPackagesUpdatedLocked() {
3484 boolean didSomething = false;
3485 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3486 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01003487 final boolean wasWhitelisted =
3488 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3489 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07003490 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01003491 final boolean isWhitelisted =
3492 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3493 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3494 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003495 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003496 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3497 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003498 removeLockedTaskLocked(lockedTask);
3499 lockedTask.performClearTaskLocked();
3500 didSomething = true;
3501 }
3502 }
3503 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3504 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3505 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3506 final ActivityStack stack = stacks.get(stackNdx);
3507 stack.onLockTaskPackagesUpdatedLocked();
3508 }
3509 }
Craig Mautnere0570202015-05-13 13:06:11 -07003510 final ActivityRecord r = topRunningActivityLocked();
3511 final TaskRecord task = r != null ? r.task : null;
3512 if (mLockTaskModeTasks.isEmpty() && task != null
3513 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3514 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07003515 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
3516 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
3517 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
3518 false);
3519 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07003520 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003521 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003522 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003523 }
3524 }
3525
Benjamin Franz43261142015-02-11 15:59:44 +00003526 int getLockTaskModeState() {
3527 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003528 }
3529
Jorim Jaggife89d122015-12-22 16:28:44 +01003530 void activityRelaunchedLocked(IBinder token) {
3531 mWindowManager.notifyAppRelaunchingFinished(token);
3532 }
3533
3534 void activityRelaunchingLocked(ActivityRecord r) {
3535 mWindowManager.notifyAppRelaunching(r.appToken);
3536 }
3537
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003538 void logStackState() {
3539 mActivityMetricsLogger.logWindowState();
3540 }
3541
Andrii Kulian933076d2016-03-29 17:04:42 -07003542 void scheduleReportMultiWindowModeChanged(TaskRecord task) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003543 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3544 final ActivityRecord r = task.mActivities.get(i);
3545 if (r.app != null && r.app.thread != null) {
3546 mMultiWindowModeChangedActivities.add(r);
3547 }
3548 }
3549
3550 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
3551 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
3552 }
3553 }
3554
Andrii Kulian933076d2016-03-29 17:04:42 -07003555 void scheduleReportPictureInPictureModeChangedIfNeeded(TaskRecord task, ActivityStack prevStack) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003556 final ActivityStack stack = task.stack;
3557 if (prevStack == null || prevStack == stack
3558 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
3559 return;
3560 }
3561
3562 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 mPipModeChangedActivities.add(r);
3566 }
3567 }
3568
3569 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
3570 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
3571 }
3572 }
3573
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003574 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003575
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003576 public ActivityStackSupervisorHandler(Looper looper) {
3577 super(looper);
3578 }
3579
Craig Mautnerf3333272013-04-22 10:55:53 -07003580 void activityIdleInternal(ActivityRecord r) {
3581 synchronized (mService) {
3582 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3583 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003584 }
3585
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003586 @Override
3587 public void handleMessage(Message msg) {
3588 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003589 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
3590 synchronized (mService) {
3591 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
3592 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07003593 r.scheduleMultiWindowModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003594 }
3595 }
3596 } break;
3597 case REPORT_PIP_MODE_CHANGED_MSG: {
3598 synchronized (mService) {
3599 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
3600 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07003601 r.schedulePictureInPictureModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003602 }
3603 }
3604 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07003605 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003606 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3607 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003608 if (mService.mDidDexOpt) {
3609 mService.mDidDexOpt = false;
3610 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3611 nmsg.obj = msg.obj;
3612 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3613 return;
3614 }
3615 // We don't at this point know if the activity is fullscreen,
3616 // so we need to be conservative and assume it isn't.
3617 activityIdleInternal((ActivityRecord)msg.obj);
3618 } break;
3619 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003620 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003621 activityIdleInternal((ActivityRecord)msg.obj);
3622 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003623 case RESUME_TOP_ACTIVITY_MSG: {
3624 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003625 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07003626 }
3627 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003628 case SLEEP_TIMEOUT_MSG: {
3629 synchronized (mService) {
3630 if (mService.isSleepingOrShuttingDown()) {
3631 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3632 mSleepTimeout = true;
3633 checkReadyForSleepLocked();
3634 }
3635 }
3636 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003637 case LAUNCH_TIMEOUT_MSG: {
3638 if (mService.mDidDexOpt) {
3639 mService.mDidDexOpt = false;
3640 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3641 return;
3642 }
3643 synchronized (mService) {
3644 if (mLaunchingActivity.isHeld()) {
3645 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3646 if (VALIDATE_WAKE_LOCK_CALLER
3647 && Binder.getCallingUid() != Process.myUid()) {
3648 throw new IllegalStateException("Calling must be system uid");
3649 }
3650 mLaunchingActivity.release();
3651 }
3652 }
3653 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003654 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003655 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003656 } break;
3657 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003658 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003659 } break;
3660 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003661 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003662 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003663 case CONTAINER_CALLBACK_VISIBILITY: {
3664 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003665 final IActivityContainerCallback callback = container.mCallback;
3666 if (callback != null) {
3667 try {
3668 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3669 } catch (RemoteException e) {
3670 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003671 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003672 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003673 case LOCK_TASK_START_MSG: {
3674 // When lock task starts, we disable the status bars.
3675 try {
Jason Monk62515be2014-05-21 16:06:19 -04003676 if (mLockTaskNotify == null) {
3677 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003678 }
Jason Monk62515be2014-05-21 16:06:19 -04003679 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003680 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003681 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003682 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003683 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003684 flags = StatusBarManager.DISABLE_MASK
3685 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07003686 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003687 flags = StatusBarManager.DISABLE_MASK
3688 & (~StatusBarManager.DISABLE_BACK)
3689 & (~StatusBarManager.DISABLE_HOME)
3690 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003691 }
3692 getStatusBarService().disable(flags, mToken,
3693 mService.mContext.getPackageName());
3694 }
3695 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003696 if (getDevicePolicyManager() != null) {
3697 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003698 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003699 }
justinzhang5286d3f2014-05-12 17:06:01 -04003700 } catch (RemoteException ex) {
3701 throw new RuntimeException(ex);
3702 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003703 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003704 case LOCK_TASK_END_MSG: {
3705 // When lock task ends, we enable the status bars.
3706 try {
Jason Monk62515be2014-05-21 16:06:19 -04003707 if (getStatusBarService() != null) {
3708 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3709 mService.mContext.getPackageName());
3710 }
3711 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003712 if (getDevicePolicyManager() != null) {
3713 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3714 msg.arg1);
3715 }
Jason Monk62515be2014-05-21 16:06:19 -04003716 if (mLockTaskNotify == null) {
3717 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3718 }
3719 mLockTaskNotify.show(false);
3720 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003721 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003722 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003723 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003724 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003725 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003726 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003727 new LockPatternUtils(mService.mContext)
3728 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003729 }
3730 } catch (SettingNotFoundException e) {
3731 // No setting, don't lock.
3732 }
justinzhang5286d3f2014-05-12 17:06:01 -04003733 } catch (RemoteException ex) {
3734 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003735 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07003736 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003737 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003738 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003739 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
3740 if (mLockTaskNotify == null) {
3741 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3742 }
3743 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
3744 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003745 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3746 final ActivityContainer container = (ActivityContainer) msg.obj;
3747 final IActivityContainerCallback callback = container.mCallback;
3748 if (callback != null) {
3749 try {
3750 callback.onAllActivitiesComplete(container.asBinder());
3751 } catch (RemoteException e) {
3752 }
3753 }
3754 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003755 case LAUNCH_TASK_BEHIND_COMPLETE: {
3756 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003757 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07003758 if (r != null) {
3759 handleLaunchTaskBehindCompleteLocked(r);
3760 }
3761 }
3762 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08003763
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003764 }
3765 }
3766 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003767
Ying Wangb081a592014-04-22 15:20:16 -07003768 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08003769 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
3770 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003771 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003772 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003773 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003774 ActivityRecord mParentActivity = null;
3775 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003776
Craig Mautnere3a00d72014-04-16 08:31:19 -07003777 boolean mVisible = true;
3778
Craig Mautner4a1cb222013-12-04 16:14:06 -08003779 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003780 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003781
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003782 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3783 final static int CONTAINER_STATE_NO_SURFACE = 1;
3784 final static int CONTAINER_STATE_FINISHING = 2;
3785 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3786
3787 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003788 synchronized (mService) {
3789 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003790 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003791 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003792 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003793 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003794 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003795
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003796 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003797 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003798 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08003799 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003800 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003801 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003802 }
3803
3804 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003805 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003806 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003807 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3808 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003809 return;
3810 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003811 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003812 }
3813 }
3814
3815 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003816 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003817 synchronized (mService) {
3818 if (mActivityDisplay != null) {
3819 return mActivityDisplay.mDisplayId;
3820 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003821 }
3822 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003823 }
3824
Jeff Brownca9bc702014-02-11 14:32:56 -08003825 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08003826 public int getStackId() {
3827 synchronized (mService) {
3828 return mStackId;
3829 }
3830 }
3831
3832 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003833 public boolean injectEvent(InputEvent event) {
3834 final long origId = Binder.clearCallingIdentity();
3835 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003836 synchronized (mService) {
3837 if (mActivityDisplay != null) {
3838 return mInputManagerInternal.injectInputEvent(event,
3839 mActivityDisplay.mDisplayId,
3840 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3841 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003842 }
3843 return false;
3844 } finally {
3845 Binder.restoreCallingIdentity(origId);
3846 }
3847 }
3848
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003849 @Override
3850 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003851 synchronized (mService) {
3852 if (mContainerState == CONTAINER_STATE_FINISHING) {
3853 return;
3854 }
3855 mContainerState = CONTAINER_STATE_FINISHING;
3856
Craig Mautnerd163e752014-06-13 17:18:47 -07003857 long origId = Binder.clearCallingIdentity();
3858 try {
Craig Mautneree36c772014-07-16 14:56:05 -07003859 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003860 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07003861 } finally {
3862 Binder.restoreCallingIdentity(origId);
3863 }
3864 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003865 }
3866
Craig Mautner60257702014-09-17 15:02:33 -07003867 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003868 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08003869 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08003870 if (mActivityDisplay != null) {
3871 mActivityDisplay.detachActivitiesLocked(mStack);
3872 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003873 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003874 }
3875 }
3876
3877 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08003878 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003879 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003880 }
3881
3882 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07003883 public final int startActivityIntentSender(IIntentSender intentSender)
3884 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003885 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3886
3887 if (!(intentSender instanceof PendingIntentRecord)) {
3888 throw new IllegalArgumentException("Bad PendingIntent object");
3889 }
3890
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07003891 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003892 Binder.getCallingUid(), mCurrentUser, false,
3893 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08003894
3895 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3896 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
3897 pendingIntent.key.requestResolvedType);
3898
3899 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
3900 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
3901 }
3902
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003903 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07003904 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003905 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003906 throw new SecurityException(
3907 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3908 }
3909 }
3910
Craig Mautnerdf88d732014-01-27 09:21:32 -08003911 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003912 public IBinder asBinder() {
3913 return this;
3914 }
3915
Craig Mautner4504de52013-12-20 09:06:56 -08003916 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003917 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003918 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08003919 }
3920
Craig Mautner4a1cb222013-12-04 16:14:06 -08003921 ActivityStackSupervisor getOuter() {
3922 return ActivityStackSupervisor.this;
3923 }
3924
Craig Mautnerd163e752014-06-13 17:18:47 -07003925 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003926 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003927 }
3928
Craig Mautner6985bad2014-04-21 15:22:06 -07003929 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07003930 void setVisible(boolean visible) {
3931 if (mVisible != visible) {
3932 mVisible = visible;
3933 if (mCallback != null) {
3934 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
3935 0 /* unused */, this).sendToTarget();
3936 }
3937 }
3938 }
3939
Craig Mautner6985bad2014-04-21 15:22:06 -07003940 void setDrawn() {
3941 }
3942
Craig Mautner1b4bf852014-05-26 15:06:32 -07003943 // You can always start a new task on a regular ActivityStack.
3944 boolean isEligibleForNewTasks() {
3945 return true;
3946 }
3947
Craig Mautnerd163e752014-06-13 17:18:47 -07003948 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07003949 detachLocked();
3950 deleteActivityContainer(this);
3951 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003952 }
3953
Craig Mautner34b73df2014-01-12 21:11:08 -08003954 @Override
3955 public String toString() {
3956 return mIdString + (mActivityDisplay == null ? "N" : "A");
3957 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003958 }
3959
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003960 private class VirtualActivityContainer extends ActivityContainer {
3961 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07003962 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003963
3964 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
3965 super(getNextStackId());
3966 mParentActivity = parent;
3967 mCallback = callback;
3968 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07003969 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003970 }
3971
3972 @Override
3973 public void setSurface(Surface surface, int width, int height, int density) {
3974 super.setSurface(surface, width, height, density);
3975
3976 synchronized (mService) {
3977 final long origId = Binder.clearCallingIdentity();
3978 try {
3979 setSurfaceLocked(surface, width, height, density);
3980 } finally {
3981 Binder.restoreCallingIdentity(origId);
3982 }
3983 }
3984 }
3985
3986 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
3987 if (mContainerState == CONTAINER_STATE_FINISHING) {
3988 return;
3989 }
3990 VirtualActivityDisplay virtualActivityDisplay =
3991 (VirtualActivityDisplay) mActivityDisplay;
3992 if (virtualActivityDisplay == null) {
3993 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07003994 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003995 mActivityDisplay = virtualActivityDisplay;
3996 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003997 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003998 }
3999
4000 if (mSurface != null) {
4001 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004002 }
4003
Craig Mautner6985bad2014-04-21 15:22:06 -07004004 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004005 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004006 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004007 } else {
4008 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004009 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004010 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004011 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004012 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004013 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004014
Craig Mautnerd163e752014-06-13 17:18:47 -07004015 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004016
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004017 if (DEBUG_STACK) Slog.d(TAG_STACK,
4018 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004019 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004020
Craig Mautner6985bad2014-04-21 15:22:06 -07004021 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004022 boolean isAttachedLocked() {
4023 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004024 }
4025
4026 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004027 void setDrawn() {
4028 synchronized (mService) {
4029 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004030 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004031 }
4032 }
4033
Craig Mautner1b4bf852014-05-26 15:06:32 -07004034 // Never start a new task on an ActivityView if it isn't explicitly specified.
4035 @Override
4036 boolean isEligibleForNewTasks() {
4037 return false;
4038 }
4039
Craig Mautnerd163e752014-06-13 17:18:47 -07004040 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004041 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004042 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4043 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4044 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4045 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4046 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004047 }
4048 }
4049
Craig Mautner4a1cb222013-12-04 16:14:06 -08004050 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4051 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004052 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004053 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004054 int mDisplayId;
4055 Display mDisplay;
4056 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004057
Craig Mautner4a1cb222013-12-04 16:14:06 -08004058 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4059 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004060 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004061
Jose Lima4b6c6692014-08-12 17:41:12 -07004062 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004063
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004064 ActivityDisplay() {
4065 }
Craig Mautner4504de52013-12-20 09:06:56 -08004066
Craig Mautner1a70a162014-09-13 12:09:31 -07004067 // After instantiation, check that mDisplay is not null before using this. The alternative
4068 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004069 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004070 final Display display = mDisplayManager.getDisplay(displayId);
4071 if (display == null) {
4072 return;
4073 }
4074 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004075 }
4076
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004077 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004078 mDisplay = display;
4079 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004080 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004081 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004082
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004083 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004084 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004085 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
4086 + " onTop=" + onTop);
4087 if (onTop) {
4088 mStacks.add(stack);
4089 } else {
4090 mStacks.add(0, stack);
4091 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004092 }
4093
Craig Mautnere0a38842013-12-16 16:14:02 -08004094 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004095 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004096 + " from displayId=" + mDisplayId);
4097 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004098 }
4099
Jose Lima4b6c6692014-08-12 17:41:12 -07004100 void setVisibleBehindActivity(ActivityRecord r) {
4101 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004102 }
4103
Jose Lima4b6c6692014-08-12 17:41:12 -07004104 boolean hasVisibleBehindActivity() {
4105 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004106 }
4107
Craig Mautner34b73df2014-01-12 21:11:08 -08004108 @Override
4109 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004110 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4111 }
4112 }
4113
4114 class VirtualActivityDisplay extends ActivityDisplay {
4115 VirtualDisplay mVirtualDisplay;
4116
Craig Mautner6985bad2014-04-21 15:22:06 -07004117 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004118 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004119 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4120 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4121 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4122 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004123
4124 init(mVirtualDisplay.getDisplay());
4125
4126 mWindowManager.handleDisplayAdded(mDisplayId);
4127 }
4128
4129 void setSurface(Surface surface) {
4130 if (mVirtualDisplay != null) {
4131 mVirtualDisplay.setSurface(surface);
4132 }
4133 }
4134
4135 @Override
4136 void detachActivitiesLocked(ActivityStack stack) {
4137 super.detachActivitiesLocked(stack);
4138 if (mVirtualDisplay != null) {
4139 mVirtualDisplay.release();
4140 mVirtualDisplay = null;
4141 }
4142 }
4143
4144 @Override
4145 public String toString() {
4146 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004147 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004148 }
Jose Lima58e66d62014-05-27 20:00:27 -07004149
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07004150 /**
4151 * Adjust bounds to stay within stack bounds.
4152 *
4153 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
4154 * that keep them unchanged, but be contained within the stack bounds.
4155 *
4156 * @param bounds Bounds to be adjusted.
4157 * @param stackBounds Bounds within which the other bounds should remain.
4158 */
4159 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
4160 if (stackBounds == null || stackBounds.contains(bounds)) {
4161 return;
4162 }
4163
4164 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
4165 final int maxRight = stackBounds.right
4166 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
4167 int horizontalDiff = stackBounds.left - bounds.left;
4168 if ((horizontalDiff < 0 && bounds.left >= maxRight)
4169 || (bounds.left + horizontalDiff >= maxRight)) {
4170 horizontalDiff = maxRight - bounds.left;
4171 }
4172 bounds.left += horizontalDiff;
4173 bounds.right += horizontalDiff;
4174 }
4175
4176 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
4177 final int maxBottom = stackBounds.bottom
4178 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
4179 int verticalDiff = stackBounds.top - bounds.top;
4180 if ((verticalDiff < 0 && bounds.top >= maxBottom)
4181 || (bounds.top + verticalDiff >= maxBottom)) {
4182 verticalDiff = maxBottom - bounds.top;
4183 }
4184 bounds.top += verticalDiff;
4185 bounds.bottom += verticalDiff;
4186 }
4187 }
4188
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004189 ActivityStack findStackBehind(ActivityStack stack) {
4190 // TODO(multi-display): We are only looking for stacks on the default display.
4191 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
4192 if (display == null) {
4193 return null;
4194 }
4195 final ArrayList<ActivityStack> stacks = display.mStacks;
4196 for (int i = stacks.size() - 1; i >= 0; i--) {
4197 if (stacks.get(i) == stack && i > 0) {
4198 return stacks.get(i - 1);
4199 }
4200 }
4201 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4202 + " in=" + stacks);
4203 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004204
Jorim Jaggic69bd222016-03-15 14:38:37 +01004205 /**
4206 * Puts a task into resizing mode during the next app transition.
4207 *
4208 * @param taskId the id of the task to put into resizing mode
4209 */
4210 private void setResizingDuringAnimation(int taskId) {
4211 mResizingTasksDuringAnimation.add(taskId);
4212 mWindowManager.setTaskDockedResizing(taskId, true);
4213 }
4214
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004215 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
4216 final TaskRecord task;
4217 final int callingUid;
4218 final String callingPackage;
4219 final Intent intent;
4220 final int userId;
4221 final ActivityOptions activityOptions = (bOptions != null)
4222 ? new ActivityOptions(bOptions) : null;
4223 final int launchStackId = (activityOptions != null)
4224 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
4225
4226 if (launchStackId == HOME_STACK_ID) {
4227 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
4228 + taskId + " can't be launch in the home stack.");
4229 }
4230 task = anyTaskForIdLocked(taskId, RESTORE_FROM_RECENTS, launchStackId);
4231 if (task == null) {
4232 throw new IllegalArgumentException(
4233 "startActivityFromRecentsInner: Task " + taskId + " not found.");
4234 }
4235
4236 if (launchStackId != INVALID_STACK_ID) {
4237 if (launchStackId == DOCKED_STACK_ID) {
4238 mWindowManager.setDockedStackCreateState(
4239 activityOptions.getDockCreateMode(), null /* initialBounds */);
4240
4241 // Defer updating the stack in which recents is until the app transition is done, to
4242 // not run into issues where we still need to draw the task in recents but the
4243 // docked stack is already created.
4244 deferUpdateBounds(HOME_STACK_ID);
4245 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
4246 }
4247 if (task.stack.mStackId != launchStackId) {
4248 moveTaskToStackLocked(
4249 taskId, launchStackId, ON_TOP, FORCE_FOCUS, "startActivityFromRecents",
4250 ANIMATE);
4251 }
4252 }
4253
4254 // If the user must confirm credentials (e.g. when first launching a work app and the
4255 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4256 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4257 && task.getRootActivity() != null) {
4258 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions);
4259
4260 // If we are launching the task in the docked stack, put it into resizing mode so
4261 // the window renders full-screen with the background filling the void. Also only
4262 // call this at the end to make sure that tasks exists on the window manager side.
4263 if (launchStackId == DOCKED_STACK_ID) {
Jorim Jaggic69bd222016-03-15 14:38:37 +01004264 setResizingDuringAnimation(taskId);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004265 }
4266 return ActivityManager.START_TASK_TO_FRONT;
4267 }
4268 callingUid = task.mCallingUid;
4269 callingPackage = task.mCallingPackage;
4270 intent = task.intent;
4271 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4272 userId = task.userId;
Jorim Jaggic69bd222016-03-15 14:38:37 +01004273 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
4274 null, null, 0, 0, bOptions, userId, null, task);
4275 if (launchStackId == DOCKED_STACK_ID) {
4276 setResizingDuringAnimation(task.taskId);
4277 }
4278 return result;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004279 }
Craig Mautner27084302013-03-25 08:05:25 -07004280}