blob: c143474ce0c8080cbd7f008282170145ae7438d7 [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;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070055import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070056import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070057import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070058import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070059import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070060import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070061import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070062import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070063import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -040064import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070065import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -070066import android.os.Trace;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -070067import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -070068import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +010069import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -070070import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -070071import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -040072import android.provider.Settings;
73import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -070074import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -070075import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070076import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -070077import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -070078import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -080079import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -080080import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -080081import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -080082import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -080083import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -080084import android.view.Surface;
Chong Zhangb15758a2015-11-17 12:12:03 -080085
Dianne Hackborn85d558c2014-11-04 10:31:54 -080086import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -070087import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -040088import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -070089import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -040090import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -080091import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -070092import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070093import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -070094
Craig Mautner8d341ef2013-03-26 09:03:27 -070095import java.io.FileDescriptor;
96import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -070097import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -070098import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -070099import java.util.Arrays;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700100import java.util.List;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800101import java.util.Objects;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700102import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700103
Jorim Jaggife89d122015-12-22 16:28:44 +0100104import static android.Manifest.permission.START_ANY_ACTIVITY;
105import static android.app.ActivityManager.LOCK_TASK_MODE_LOCKED;
106import static android.app.ActivityManager.LOCK_TASK_MODE_NONE;
107import static android.app.ActivityManager.LOCK_TASK_MODE_PINNED;
108import static android.app.ActivityManager.RESIZE_MODE_FORCED;
109import static android.app.ActivityManager.RESIZE_MODE_SYSTEM;
110import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
111import static android.app.ActivityManager.StackId.FIRST_DYNAMIC_STACK_ID;
112import static android.app.ActivityManager.StackId.FIRST_STATIC_STACK_ID;
113import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
114import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
115import static android.app.ActivityManager.StackId.HOME_STACK_ID;
116import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
117import static android.app.ActivityManager.StackId.LAST_STATIC_STACK_ID;
118import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
119import static android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
120import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
121import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800122import static android.content.pm.ActivityInfo.RESIZE_MODE_UNRESIZEABLE;
Jorim Jaggife89d122015-12-22 16:28:44 +0100123import static android.content.pm.PackageManager.PERMISSION_GRANTED;
124import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
125import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
126import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONTAINERS;
127import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_IDLE;
128import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKSCREEN;
129import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKTASK;
130import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
131import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
132import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
133import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
134import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
135import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
136import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
137import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBLE_BEHIND;
138import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONTAINERS;
139import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
140import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_LOCKTASK;
141import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
142import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
143import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
144import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
145import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
146import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
147import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
148import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBLE_BEHIND;
149import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
150import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
Wale Ogunwale480dca02016-02-06 13:58:29 -0800151import static com.android.server.am.ActivityManagerService.ANIMATE;
Jorim Jaggife89d122015-12-22 16:28:44 +0100152import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
153import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
154import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
155import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
156import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
157import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
158import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
159import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
160import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
161import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
162import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
163import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
164import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
165import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
166import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
167import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
168import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
169
Craig Mautner4a1cb222013-12-04 16:14:06 -0800170public final class ActivityStackSupervisor implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800171 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700172 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700173 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700174 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700175 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700176 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700177 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700178 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700179 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700180 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800181 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700182 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800183
Craig Mautnerf3333272013-04-22 10:55:53 -0700184 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700185 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700186
Craig Mautner0eea92c2013-05-16 13:35:39 -0700187 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700188 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700189
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700190 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700191 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700192
Craig Mautner05d29032013-05-03 13:40:13 -0700193 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
194 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
195 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700196 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700197 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800198 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
199 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
200 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700201 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400202 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
203 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700204 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700205 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700206 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800207 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
208 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800209
Wale Ogunwale040b4702015-08-06 18:10:50 -0700210 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700211
Jason Monk62515be2014-05-21 16:06:19 -0400212 private static final String LOCK_TASK_TAG = "Lock-to-App";
213
Wale Ogunwale040b4702015-08-06 18:10:50 -0700214 // Used to indicate if an object (e.g. stack) that we are trying to get
215 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800216 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700217
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700218 // Used to indicate that windows of activities should be preserved during the resize.
219 static final boolean PRESERVE_WINDOWS = true;
220
Wale Ogunwale040b4702015-08-06 18:10:50 -0700221 // Used to indicate if an object (e.g. task) should be moved/created
222 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700223 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700224
225 // Used to indicate that an objects (e.g. task) removal from its container
226 // (e.g. stack) is due to it moving to another container.
227 static final boolean MOVING = true;
228
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700229 // Force the focus to change to the stack we are moving a task to..
230 static final boolean FORCE_FOCUS = true;
231
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700232 // Restore task from the saved recents if it can't be found in any live stack.
233 static final boolean RESTORE_FROM_RECENTS = true;
234
Svetoslav7008b512015-06-24 18:47:07 -0700235 // Activity actions an app cannot start if it uses a permission which is not granted.
236 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
237 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700238
Svetoslav7008b512015-06-24 18:47:07 -0700239 static {
240 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
241 Manifest.permission.CAMERA);
242 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
243 Manifest.permission.CAMERA);
244 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
245 Manifest.permission.CALL_PHONE);
246 }
247
Svet Ganov99b60432015-06-27 13:15:22 -0700248 /** Action restriction: launching the activity is not restricted. */
249 private static final int ACTIVITY_RESTRICTION_NONE = 0;
250 /** Action restriction: launching the activity is restricted by a permission. */
251 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
252 /** Action restriction: launching the activity is restricted by an app op. */
253 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700254
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700255 // The height/width divide used when fitting a task within a bounds with method
256 // {@link #fitWithinBounds}.
257 // We always want the task to to be visible in the bounds without affecting its size when
258 // fitting. To make sure this is the case, we don't adjust the task left or top side pass
259 // the input bounds right or bottom side minus the width or height divided by this value.
260 private static final int FIT_WITHIN_BOUNDS_DIVIDER = 3;
261
justinzhang5286d3f2014-05-12 17:06:01 -0400262 /** Status Bar Service **/
263 private IBinder mToken = new Binder();
264 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400265 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400266
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700267 // For debugging to make sure the caller when acquiring/releasing our
268 // wake lock is the system process.
269 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800270 /** The number of distinct task ids that can be assigned to the tasks of a single user */
271 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700272
Craig Mautner27084302013-03-25 08:05:25 -0700273 final ActivityManagerService mService;
274
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800275 private RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800276
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700277 final ActivityStackSupervisorHandler mHandler;
278
279 /** Short cut */
280 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800281 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700282
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700283 /** Counter for next free stack ID to use for dynamic activity stacks. */
284 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700285
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800286 /**
287 * Maps the task identifier that activities are currently being started in to the userId of the
288 * task. Each time a new task is created, the entry for the userId of the task is incremented
289 */
290 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700291
Craig Mautner2420ead2013-04-01 17:13:20 -0700292 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800293 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700294
Craig Mautnere0a38842013-12-16 16:14:02 -0800295 /** The stack containing the launcher app. Assumed to always be attached to
296 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800297 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700298
Craig Mautnere0a38842013-12-16 16:14:02 -0800299 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800300 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700301
Craig Mautner4a1cb222013-12-04 16:14:06 -0800302 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
303 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800304 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800305 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700306
307 /** List of activities that are waiting for a new activity to become visible before completing
308 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800309 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700310
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700311 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800312 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700313
314 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800315 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700316
Craig Mautnerde4ef022013-04-07 19:01:33 -0700317 /** List of activities that are ready to be stopped, but waiting for the next activity to
318 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800319 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700320
Craig Mautnerf3333272013-04-22 10:55:53 -0700321 /** List of activities that are ready to be finished, but waiting for the previous activity to
322 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800323 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700324
Craig Mautner0eea92c2013-05-16 13:35:39 -0700325 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800326 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700327
Wale Ogunwale22e25262016-02-01 10:32:02 -0800328 /** List of activities whose multi-window mode changed that we need to report to the
329 * application */
330 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
331
332 /** List of activities whose picture-in-picture mode changed that we need to report to the
333 * application */
334 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
335
Craig Mautnerf3333272013-04-22 10:55:53 -0700336 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700337 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700338
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700339 /** Used to queue up any background users being started */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700340 final ArrayList<UserState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700341
Craig Mautnerde4ef022013-04-07 19:01:33 -0700342 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
343 * is being brought in front of us. */
344 boolean mUserLeaving = false;
345
Craig Mautner0eea92c2013-05-16 13:35:39 -0700346 /** Set when we have taken too long waiting to go to sleep. */
347 boolean mSleepTimeout = false;
348
349 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700350 * We don't want to allow the device to go to sleep while in the process
351 * of launching an activity. This is primarily to allow alarm intent
352 * receivers to launch an activity and get that to run before the device
353 * goes back to sleep.
354 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700355 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700356
357 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700358 * Set when the system is going to sleep, until we have
359 * successfully paused the current activity and released our wake lock.
360 * At that point the system is allowed to actually sleep.
361 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700362 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700363
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700364 /** Stack id of the front stack when user switched, indexed by userId. */
365 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700366
Craig Mautner4504de52013-12-20 09:06:56 -0800367 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800368 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700369 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800370
371 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700372 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800373
Jeff Brownca9bc702014-02-11 14:32:56 -0800374 InputManagerInternal mInputManagerInternal;
375
Craig Mautner15df08a2015-04-01 12:17:18 -0700376 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
377 * may be finished until there is only one entry left. If this is empty the system is not
378 * in lockTask mode. */
379 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000380 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700381 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
382 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000383 */
384 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400385 /**
386 * Notifies the user when entering/exiting lock-task.
387 */
388 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800389
Wale Ogunwaled046a012015-12-24 13:05:59 -0800390 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800391 boolean inResumeTopActivity;
392
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700393 // 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 -0700394 private final Rect tempRect = new Rect();
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700395 private final Rect tempRect2 = new Rect();
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700396
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700397 private final SparseArray<Configuration> mTmpConfigs = new SparseArray<>();
398 private final SparseArray<Rect> mTmpBounds = new SparseArray<>();
Jorim Jaggidc249c42015-12-15 14:57:31 -0800399 private final SparseArray<Rect> mTmpInsetBounds = new SparseArray<>();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700400
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700401 // The default minimal size that will be used if the activity doesn't specify its minimal size.
402 // It will be calculated when the default display gets added.
403 private int mDefaultMinimalSizeOfResizeableTask = -1;
404
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700405 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
406 private boolean mTaskLayersChanged = true;
407
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800408 private final ActivityMetricsLogger mActivityMetricsLogger;
409
Jorim Jaggidc249c42015-12-15 14:57:31 -0800410 private final ResizeDockedStackTimeout mResizeDockedStackTimeout;
411
Wale Ogunwale39381972015-12-17 17:15:29 -0800412 static class FindTaskResult {
413 ActivityRecord r;
414 boolean matchedByRootAffinity;
415 }
416 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
417
Craig Mautneree36c772014-07-16 14:56:05 -0700418 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100419 * Used to keep track whether app visibilities got changed since the last pause. Useful to
420 * determine whether to invoke the task stack change listener after pausing.
421 */
422 boolean mAppVisibilitiesChangedSinceLastPause;
423
424 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700425 * Description of a request to start a new activity, which has been held
426 * due to app switches being disabled.
427 */
428 static class PendingActivityLaunch {
429 final ActivityRecord r;
430 final ActivityRecord sourceRecord;
431 final int startFlags;
432 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700433 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700434
435 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700436 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700437 r = _r;
438 sourceRecord = _sourceRecord;
439 startFlags = _startFlags;
440 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700441 callerApp = _callerApp;
442 }
443
444 void sendErrorResult(String message) {
445 try {
446 if (callerApp.thread != null) {
447 callerApp.thread.scheduleCrash(message);
448 }
449 } catch (RemoteException e) {
450 Slog.e(TAG, "Exception scheduling crash of failed "
451 + "activity launcher sourceRecord=" + sourceRecord, e);
452 }
Craig Mautneree36c772014-07-16 14:56:05 -0700453 }
454 }
455
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800456 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700457 mService = service;
Jeff Brown2c43c332014-06-12 22:38:59 -0700458 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800459 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggidc249c42015-12-15 14:57:31 -0800460 mResizeDockedStackTimeout = new ResizeDockedStackTimeout(service, this, mHandler);
Jeff Brown2c43c332014-06-12 22:38:59 -0700461 }
462
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800463 void setRecentTasks(RecentTasks recentTasks) {
464 mRecentTasks = recentTasks;
465 }
466
Jeff Brown2c43c332014-06-12 22:38:59 -0700467 /**
468 * At the time when the constructor runs, the power manager has not yet been
469 * initialized. So we initialize our wakelocks afterwards.
470 */
471 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800472 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700473 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700474 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700475 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700476 }
477
justinzhang5286d3f2014-05-12 17:06:01 -0400478 // This function returns a IStatusBarService. The value is from ServiceManager.
479 // getService and is cached.
480 private IStatusBarService getStatusBarService() {
481 synchronized (mService) {
482 if (mStatusBarService == null) {
483 mStatusBarService = IStatusBarService.Stub.asInterface(
484 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
485 if (mStatusBarService == null) {
486 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
487 }
488 }
489 return mStatusBarService;
490 }
491 }
492
Jason Monk35c62a42014-06-17 10:24:47 -0400493 private IDevicePolicyManager getDevicePolicyManager() {
494 synchronized (mService) {
495 if (mDevicePolicyManager == null) {
496 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
497 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
498 if (mDevicePolicyManager == null) {
499 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
500 }
501 }
502 return mDevicePolicyManager;
503 }
504 }
505
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700506 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800507 synchronized (mService) {
508 mWindowManager = wm;
509
510 mDisplayManager =
511 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
512 mDisplayManager.registerDisplayListener(this, null);
513
514 Display[] displays = mDisplayManager.getDisplays();
515 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
516 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800517 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700518 if (activityDisplay.mDisplay == null) {
519 throw new IllegalStateException("Default Display does not exist");
520 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800521 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700522 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800523 }
524
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800525 mHomeStack = mFocusedStack = mLastFocusedStack =
526 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800527
528 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800529 }
Craig Mautner27084302013-03-25 08:05:25 -0700530 }
531
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200532 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700533 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200534 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700535 }
536
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700537 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800538 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700539 }
540
Craig Mautnerde4ef022013-04-07 19:01:33 -0700541 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800542 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700543 }
544
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700545 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700546 if (stack == null) {
547 return false;
548 }
549
Craig Mautnerdf88d732014-01-27 09:21:32 -0800550 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
551 if (parent != null) {
552 stack = parent.task.stack;
553 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800554 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700555 }
556
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700557 /** The top most stack. */
558 boolean isFrontStack(ActivityStack stack) {
559 if (stack == null) {
560 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800561 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700562
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700563 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
564 if (parent != null) {
565 stack = parent.task.stack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800566 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700567 return stack == mHomeStack.mStacks.get((mHomeStack.mStacks.size() - 1));
568 }
569
Wale Ogunwaled046a012015-12-24 13:05:59 -0800570 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800571 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
572 if (!focusCandidate.isFocusable()) {
573 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
574 focusCandidate = focusCandidate.getNextFocusableStackLocked();
575 }
576
577 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800578 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800579 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800580
Wale Ogunwaled046a012015-12-24 13:05:59 -0800581 EventLogTags.writeAmFocusedStack(
582 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
583 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
584 }
585
586 final ActivityRecord r = topRunningActivityLocked();
Chong Zhang3aa28b22016-02-04 16:38:33 -0800587 if (!mService.mDoingSetFocusedActivity && mService.mFocusedActivity != r) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800588 // The focus activity should always be the top activity in the focused stack.
589 // There will be chaos and anarchy if it isn't...
590 mService.setFocusedActivityLocked(r, reason + " setFocusStack");
591 }
Craig Mautnerde313752015-01-22 14:28:03 -0800592
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800593 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800594 if (r != null && r.idle) {
595 checkFinishBootingLocked();
596 }
597 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700598 }
599
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700600 void moveHomeStackToFront(String reason) {
601 mHomeStack.moveToFront(reason);
602 }
603
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700604 /** Returns true if the focus activity was adjusted to the home stack top activity. */
605 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700606 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
607 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700608 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700609 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700610
Craig Mautner84984fa2014-06-19 11:19:20 -0700611 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700612
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700613 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700614 if (top == null) {
615 return false;
616 }
617 mService.setFocusedActivityLocked(top, reason);
618 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700619 }
620
Craig Mautner299f9602015-01-26 09:47:33 -0800621 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700622 if (!mService.mBooting && !mService.mBooted) {
623 // Not ready yet!
624 return false;
625 }
626
Craig Mautner84984fa2014-06-19 11:19:20 -0700627 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
628 mWindowManager.showRecentApps();
629 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700630 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700631
Craig Mautner84984fa2014-06-19 11:19:20 -0700632 if (prev != null) {
633 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
634 }
635
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700636 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
637 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800638 final String myReason = reason + " resumeHomeStackTask";
639
Mark Lua56ea122015-10-08 13:31:01 +0800640 // Only resume home activity if isn't finishing.
641 if (r != null && !r.finishing) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800642 mService.setFocusedActivityLocked(r, myReason);
643 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700644 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800645 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700646 }
647
Craig Mautner8d341ef2013-03-26 09:03:27 -0700648 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700649 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700650 }
651
652 /**
653 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
654 * @param id Id of the task we would like returned.
655 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
656 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700657 * @param stackId The stack to restore the task to (default launch stack will be used if
658 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700659 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700660 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800661 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800662 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800663 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800664 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
665 ActivityStack stack = stacks.get(stackNdx);
666 TaskRecord task = stack.taskForIdLocked(id);
667 if (task != null) {
668 return task;
669 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700670 }
671 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800672
673 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700674 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800675 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800676 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700677 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800678 return null;
679 }
680
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700681 if (!restoreFromRecents) {
682 return task;
683 }
684
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700685 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700686 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
687 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800688 return null;
689 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700690 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800691 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700692 }
693
Craig Mautner6170f732013-04-02 13:05:23 -0700694 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800695 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800696 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800697 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800698 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
699 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
700 if (r != null) {
701 return r;
702 }
Craig Mautner6170f732013-04-02 13:05:23 -0700703 }
704 }
705 return null;
706 }
707
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000708 boolean isFocusedUserLockedProfile() {
709 final int userId = mFocusedStack.topRunningActivityLocked().userId;
710 return userId != UserHandle.myUserId()
711 && mService.mUserController.shouldConfirmCredentials(userId);
712 }
713
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800714 void setNextTaskIdForUserLocked(int taskId, int userId) {
715 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
716 if (taskId > currentTaskId) {
717 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700718 }
719 }
720
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800721 int getNextTaskIdForUserLocked(int userId) {
722 mRecentTasks.loadUserRecentsLocked(userId);
723 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
724 // for a userId u, a taskId can only be in the range
725 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
726 // was 10, user 0 could only have taskIds 0 to 9, user 1: 10 to 19, user 2: 20 to 29, so on.
727 int candidateTaskId = currentTaskId;
728 while (anyTaskForIdLocked(candidateTaskId, !RESTORE_FROM_RECENTS,
729 INVALID_STACK_ID) != null) {
730 candidateTaskId++;
731 if (candidateTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
732 // Wrap around as there will be smaller task ids that are available now.
733 candidateTaskId -= MAX_TASK_IDS_PER_USER;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700734 }
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800735 if (candidateTaskId == currentTaskId) {
736 // Something wrong!
737 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
738 throw new IllegalStateException("Cannot get an available task id."
739 + " Reached limit of " + MAX_TASK_IDS_PER_USER
740 + " running tasks per user.");
741 }
742 }
743 mCurTaskIdForUser.put(userId, candidateTaskId);
744 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700745 }
746
Craig Mautnerde4ef022013-04-07 19:01:33 -0700747 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800748 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700749 if (stack == null) {
750 return null;
751 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700752 ActivityRecord resumedActivity = stack.mResumedActivity;
753 if (resumedActivity == null || resumedActivity.app == null) {
754 resumedActivity = stack.mPausingActivity;
755 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700756 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700757 }
758 }
759 return resumedActivity;
760 }
761
Dianne Hackbornff072722014-09-24 10:56:28 -0700762 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700763 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800764 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800765 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
766 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800767 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
768 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700769 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800770 continue;
771 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700772 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800773 if (hr != null) {
774 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
775 && processName.equals(hr.processName)) {
776 try {
George Mount2c92c972014-03-20 09:38:23 -0700777 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800778 didSomething = true;
779 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700780 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800781 Slog.w(TAG, "Exception in new application when starting activity "
782 + hr.intent.getComponent().flattenToShortString(), e);
783 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700784 }
Craig Mautner20e72272013-04-01 13:45:53 -0700785 }
Craig Mautner20e72272013-04-01 13:45:53 -0700786 }
787 }
788 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700789 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700790 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700791 }
Craig Mautner20e72272013-04-01 13:45:53 -0700792 return didSomething;
793 }
794
795 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800796 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
797 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800798 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
799 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700800 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800801 continue;
802 }
803 final ActivityRecord resumedActivity = stack.mResumedActivity;
804 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700805 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800806 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800807 return false;
808 }
Craig Mautner20e72272013-04-01 13:45:53 -0700809 }
810 }
811 return true;
812 }
813
Craig Mautnerde4ef022013-04-07 19:01:33 -0700814 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800815 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
816 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800817 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
818 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700819 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800820 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700821 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800822 return false;
823 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700824 }
825 }
826 }
827 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700828 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800829 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
830 mLastFocusedStack + " to=" + mFocusedStack);
831 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700832 return true;
833 }
834
835 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800836 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800837 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
838 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800839 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
840 final ActivityStack stack = stacks.get(stackNdx);
841 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800842 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700843 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800844 return false;
845 }
846 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800847 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700848 }
849 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800850 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700851 }
852
Craig Mautner2acc3892013-09-23 10:28:14 -0700853 /**
854 * Pause all activities in either all of the stacks or just the back stacks.
855 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700856 * @return true if any activity was paused as a result of this call.
857 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700858 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700859 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800860 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
861 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800862 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
863 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700864 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700865 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800866 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700867 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
868 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800869 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700870 }
871 }
872 return someActivityPaused;
873 }
874
Craig Mautnerde4ef022013-04-07 19:01:33 -0700875 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700876 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800877 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
878 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800879 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
880 final ActivityStack stack = stacks.get(stackNdx);
881 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700882 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800883 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700884 Slog.d(TAG_STATES,
885 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800886 pausing = false;
887 } else {
888 return false;
889 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700890 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700891 }
892 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700893 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700894 }
895
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700896 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
897 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500898 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800899 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
900 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
901 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
902 final ActivityStack stack = stacks.get(stackNdx);
903 if (stack.mResumedActivity != null &&
904 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700905 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800906 }
907 }
908 }
909 }
910
Wale Ogunwale2be760d2016-02-17 11:16:10 -0800911 void cancelInitializingActivities() {
912 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
913 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
914 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
915 stacks.get(stackNdx).cancelInitializingActivities();
916 }
917 }
918 }
919
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700920 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700921 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700922 }
923
924 void sendWaitingVisibleReportLocked(ActivityRecord r) {
925 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700926 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700927 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700928 if (w.who == null) {
929 changed = true;
930 w.timeout = false;
931 if (r != null) {
932 w.who = new ComponentName(r.info.packageName, r.info.name);
933 }
934 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
935 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700936 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700937 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700938 if (changed) {
939 mService.notifyAll();
940 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700941 }
942
943 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
944 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700945 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700946 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700947 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700948 if (w.who == null) {
949 changed = true;
950 w.timeout = timeout;
951 if (r != null) {
952 w.who = new ComponentName(r.info.packageName, r.info.name);
953 }
954 w.thisTime = thisTime;
955 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700956 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700957 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700958 if (changed) {
959 mService.notifyAll();
960 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700961 }
962
Craig Mautner29219d92013-04-16 20:19:12 -0700963 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800964 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700965 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -0700966 if (r != null) {
967 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700968 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700969
Craig Mautner4a1cb222013-12-04 16:14:06 -0800970 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800971 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800972 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
973 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800974 if (stack != focusedStack && isFrontStack(stack) && stack.isFocusable()) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700975 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -0700976 if (r != null) {
977 return r;
978 }
979 }
980 }
981 return null;
982 }
983
Dianne Hackborn09233282014-04-30 11:33:59 -0700984 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700985 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800986 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
987 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800988 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800989 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800990 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800991 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
992 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700993 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800994 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700995 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700996 }
997 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700998
999 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1000 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1001 while (maxNum > 0) {
1002 long mostRecentActiveTime = Long.MIN_VALUE;
1003 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001004 final int numTaskLists = runningTaskLists.size();
1005 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1006 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001007 if (!stackTaskList.isEmpty()) {
1008 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1009 if (lastActiveTime > mostRecentActiveTime) {
1010 mostRecentActiveTime = lastActiveTime;
1011 selectedStackList = stackTaskList;
1012 }
1013 }
1014 }
1015 if (selectedStackList != null) {
1016 list.add(selectedStackList.remove(0));
1017 --maxNum;
1018 } else {
1019 break;
1020 }
1021 }
Craig Mautner20e72272013-04-01 13:45:53 -07001022 }
1023
Todd Kennedy7440f172015-12-09 14:31:22 -08001024 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1025 ProfilerInfo profilerInfo) {
1026 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001027 if (aInfo != null) {
1028 // Store the found target back into the intent, because now that
1029 // we have it we never want to do this again. For example, if the
1030 // user navigates back to this point in the history, we should
1031 // always restart the exact same activity.
1032 intent.setComponent(new ComponentName(
1033 aInfo.applicationInfo.packageName, aInfo.name));
1034
1035 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001036 if (!aInfo.processName.equals("system")) {
1037 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001038 mService.setDebugApp(aInfo.processName, true, false);
1039 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001040
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001041 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1042 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1043 }
1044
Man Caocfa78b22015-06-11 20:14:34 -07001045 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1046 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1047 }
1048
1049 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001050 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001051 }
1052 }
1053 }
1054 return aInfo;
1055 }
1056
Todd Kennedy7440f172015-12-09 14:31:22 -08001057 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001058 return resolveIntent(intent, resolvedType, userId, 0);
1059 }
1060
1061 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy7440f172015-12-09 14:31:22 -08001062 try {
1063 return AppGlobals.getPackageManager().resolveIntent(intent, resolvedType,
Kenny Guyb1b30262016-02-09 16:02:35 +00001064 PackageManager.MATCH_DEFAULT_ONLY | flags
1065 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001066 } catch (RemoteException e) {
1067 }
1068 return null;
1069 }
1070
1071 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1072 ProfilerInfo profilerInfo, int userId) {
1073 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1074 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1075 }
1076
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001077 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1078 boolean andResume, boolean checkConfig) throws RemoteException {
1079
1080 if (!allPausedActivitiesComplete()) {
1081 // While there are activities pausing we skipping starting any new activities until
1082 // pauses are complete. NOTE: that we also do this for activities that are starting in
1083 // the paused state because they will first be resumed then paused on the client side.
1084 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1085 "realStartActivityLocked: Skipping start of r=" + r
1086 + " some activities pausing...");
1087 return false;
1088 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001089
Craig Mautner2568c3a2015-03-26 14:22:34 -07001090 if (andResume) {
1091 r.startFreezingScreenLocked(app, 0);
1092 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001093
Craig Mautner2568c3a2015-03-26 14:22:34 -07001094 // schedule launch ticks to collect information about slow apps.
1095 r.startLaunchTickingLocked();
1096 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001097
1098 // Have the window manager re-evaluate the orientation of
1099 // the screen based on the new activity order. Note that
1100 // as a result of this, it can call back into the activity
1101 // manager with a new orientation. We don't care about that,
1102 // because the activity is not currently running so we are
1103 // just restarting it anyway.
1104 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001105 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001106 mService.mConfiguration,
1107 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001108 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001109 }
1110
1111 r.app = app;
1112 app.waitingToKill = null;
1113 r.launchCount++;
1114 r.lastLaunchTime = SystemClock.uptimeMillis();
1115
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001116 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001117
1118 int idx = app.activities.indexOf(r);
1119 if (idx < 0) {
1120 app.activities.add(r);
1121 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001122 mService.updateLruProcessLocked(app, true, null);
1123 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001124
Craig Mautner15df08a2015-04-01 12:17:18 -07001125 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001126 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1127 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001128 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001129 }
1130
1131 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001132 try {
1133 if (app.thread == null) {
1134 throw new RemoteException();
1135 }
1136 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001137 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001138 if (andResume) {
1139 results = r.results;
1140 newIntents = r.newIntents;
1141 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001142 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1143 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1144 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001145 if (andResume) {
1146 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1147 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001148 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001149 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001150 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001151 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001152 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001153 }
Nicolas Geoffray27c07372015-11-05 16:54:09 +00001154 mService.notifyPackageUse(r.intent.getComponent().getPackageName());
Craig Mautner2420ead2013-04-01 17:13:20 -07001155 r.sleeping = false;
1156 r.forceNewConfig = false;
1157 mService.showAskCompatModeDialogLocked(r);
1158 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001159 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001160 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1161 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1162 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001163 final String profileFile = mService.mProfileFile;
1164 if (profileFile != null) {
1165 ParcelFileDescriptor profileFd = mService.mProfileFd;
1166 if (profileFd != null) {
1167 try {
1168 profileFd = profileFd.dup();
1169 } catch (IOException e) {
1170 if (profileFd != null) {
1171 try {
1172 profileFd.close();
1173 } catch (IOException o) {
1174 }
1175 profileFd = null;
1176 }
1177 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001178 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001179
1180 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1181 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001182 }
1183 }
1184 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001185
Craig Mautner2568c3a2015-03-26 14:22:34 -07001186 if (andResume) {
1187 app.hasShownUi = true;
1188 app.pendingUiClean = true;
1189 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001190 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001191 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001192 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001193 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001194 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001195 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001196
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001197 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001198 // This may be a heavy-weight process! Note that the package
1199 // manager will ensure that only activity can run in the main
1200 // process of the .apk, which is the only thing that will be
1201 // considered heavy-weight.
1202 if (app.processName.equals(app.info.packageName)) {
1203 if (mService.mHeavyWeightProcess != null
1204 && mService.mHeavyWeightProcess != app) {
1205 Slog.w(TAG, "Starting new heavy weight process " + app
1206 + " when already running "
1207 + mService.mHeavyWeightProcess);
1208 }
1209 mService.mHeavyWeightProcess = app;
1210 Message msg = mService.mHandler.obtainMessage(
1211 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1212 msg.obj = r;
1213 mService.mHandler.sendMessage(msg);
1214 }
1215 }
1216
1217 } catch (RemoteException e) {
1218 if (r.launchFailed) {
1219 // This is the second time we failed -- finish activity
1220 // and give up.
1221 Slog.e(TAG, "Second failure launching "
1222 + r.intent.getComponent().flattenToShortString()
1223 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001224 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001225 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1226 "2nd-crash", false);
1227 return false;
1228 }
1229
1230 // This is the first time we failed -- restart process and
1231 // retry.
1232 app.activities.remove(r);
1233 throw e;
1234 }
1235
1236 r.launchFailed = false;
1237 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001238 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001239 }
1240
1241 if (andResume) {
1242 // As part of the process of launching, ActivityThread also performs
1243 // a resume.
1244 stack.minimalResumeActivityLocked(r);
1245 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001246 // This activity is not starting in the resumed state... which should look like we asked
1247 // it to pause+stop (but remain visible), and it has done so and reported back the
1248 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001249 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001250 "Moving to PAUSED: " + r + " (starting in paused state)");
1251 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001252 }
1253
1254 // Launch the new version setup screen if needed. We do this -after-
1255 // launching the initial activity (that is, home), so that it can have
1256 // a chance to initialize itself while in the background, making the
1257 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001258 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001259 mService.startSetupActivityLocked();
1260 }
1261
Dianne Hackborn465fa392014-09-14 14:21:18 -07001262 // Update any services we are bound to that might care about whether
1263 // their client may have activities.
1264 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1265
Craig Mautner2420ead2013-04-01 17:13:20 -07001266 return true;
1267 }
1268
Craig Mautnere79d42682013-04-01 19:01:53 -07001269 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001270 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001271 // Is this activity's application already running?
1272 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001273 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001274
1275 r.task.stack.setLaunchTime(r);
1276
1277 if (app != null && app.thread != null) {
1278 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001279 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1280 || !"android".equals(r.info.packageName)) {
1281 // Don't add this if it is a platform component that is marked
1282 // to run in multiple processes, because this is actually
1283 // part of the framework so doesn't make sense to track as a
1284 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001285 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1286 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001287 }
George Mount2c92c972014-03-20 09:38:23 -07001288 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001289 return;
1290 } catch (RemoteException e) {
1291 Slog.w(TAG, "Exception when starting activity "
1292 + r.intent.getComponent().flattenToShortString(), e);
1293 }
1294
1295 // If a dead object exception was thrown -- fall through to
1296 // restart the application.
1297 }
1298
1299 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001300 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001301 }
1302
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001303 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001304 String resultWho, int requestCode, int callingPid, int callingUid,
1305 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
1306 ActivityRecord resultRecord, ActivityStack resultStack) {
1307 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1308 callingUid);
1309 if (startAnyPerm == PERMISSION_GRANTED) {
1310 return true;
1311 }
1312 final int componentRestriction = getComponentRestrictionForCallingPackage(
1313 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1314 final int actionRestriction = getActionRestrictionForCallingPackage(
1315 intent.getAction(), callingPackage, callingPid, callingUid);
1316 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1317 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1318 if (resultRecord != null) {
1319 resultStack.sendActivityResultLocked(-1,
1320 resultRecord, resultWho, requestCode,
1321 Activity.RESULT_CANCELED, null);
1322 }
1323 final String msg;
1324 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1325 msg = "Permission Denial: starting " + intent.toString()
1326 + " from " + callerApp + " (pid=" + callingPid
1327 + ", uid=" + callingUid + ")" + " with revoked permission "
1328 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1329 } else if (!aInfo.exported) {
1330 msg = "Permission Denial: starting " + intent.toString()
1331 + " from " + callerApp + " (pid=" + callingPid
1332 + ", uid=" + callingUid + ")"
1333 + " not exported from uid " + aInfo.applicationInfo.uid;
1334 } else {
1335 msg = "Permission Denial: starting " + intent.toString()
1336 + " from " + callerApp + " (pid=" + callingPid
1337 + ", uid=" + callingUid + ")"
1338 + " requires " + aInfo.permission;
1339 }
1340 Slog.w(TAG, msg);
1341 throw new SecurityException(msg);
1342 }
1343
1344 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1345 final String message = "Appop Denial: starting " + intent.toString()
1346 + " from " + callerApp + " (pid=" + callingPid
1347 + ", uid=" + callingUid + ")"
1348 + " requires " + AppOpsManager.permissionToOp(
1349 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1350 Slog.w(TAG, message);
1351 return false;
1352 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1353 final String message = "Appop Denial: starting " + intent.toString()
1354 + " from " + callerApp + " (pid=" + callingPid
1355 + ", uid=" + callingUid + ")"
1356 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1357 Slog.w(TAG, message);
1358 return false;
1359 }
1360 return true;
1361 }
1362
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001363 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001364 final long identity = Binder.clearCallingIdentity();
1365 try {
1366 return UserManager.get(mService.mContext).getUserInfo(userId);
1367 } finally {
1368 Binder.restoreCallingIdentity(identity);
1369 }
1370 }
1371
Svet Ganov99b60432015-06-27 13:15:22 -07001372 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001373 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001374 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1375 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001376 == PackageManager.PERMISSION_DENIED) {
1377 return ACTIVITY_RESTRICTION_PERMISSION;
1378 }
1379
Christopher Tateff7add02015-08-17 10:23:22 -07001380 if (activityInfo.permission == null) {
1381 return ACTIVITY_RESTRICTION_NONE;
1382 }
1383
Svet Ganov99b60432015-06-27 13:15:22 -07001384 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1385 if (opCode == AppOpsManager.OP_NONE) {
1386 return ACTIVITY_RESTRICTION_NONE;
1387 }
1388
1389 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1390 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001391 if (!ignoreTargetSecurity) {
1392 return ACTIVITY_RESTRICTION_APPOP;
1393 }
Svet Ganov99b60432015-06-27 13:15:22 -07001394 }
1395
1396 return ACTIVITY_RESTRICTION_NONE;
1397 }
1398
Svetoslav7008b512015-06-24 18:47:07 -07001399 private int getActionRestrictionForCallingPackage(String action,
1400 String callingPackage, int callingPid, int callingUid) {
1401 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001402 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001403 }
1404
1405 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1406 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001407 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001408 }
1409
1410 final PackageInfo packageInfo;
1411 try {
1412 packageInfo = mService.mContext.getPackageManager()
1413 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1414 } catch (PackageManager.NameNotFoundException e) {
1415 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001416 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001417 }
1418
1419 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001420 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001421 }
1422
1423 if (mService.checkPermission(permission, callingPid, callingUid) ==
1424 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001425 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001426 }
1427
1428 final int opCode = AppOpsManager.permissionToOpCode(permission);
1429 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001430 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001431 }
1432
1433 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1434 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001435 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001436 }
1437
Svet Ganov99b60432015-06-27 13:15:22 -07001438 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001439 }
1440
Wale Ogunwaled046a012015-12-24 13:05:59 -08001441 boolean moveActivityStackToFront(ActivityRecord r, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001442 if (r == null) {
1443 // Not sure what you are trying to do, but it is not going to work...
1444 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001445 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001446 final TaskRecord task = r.task;
1447 if (task == null || task.stack == null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001448 Slog.w(TAG, "Can't move stack to front for r=" + r + " task=" + task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001449 return false;
1450 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001451 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001452 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001453 }
1454
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001455 void setLaunchSource(int uid) {
1456 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1457 }
1458
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001459 void acquireLaunchWakelock() {
1460 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1461 throw new IllegalStateException("Calling must be system uid");
1462 }
1463 mLaunchingActivity.acquire();
1464 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1465 // To be safe, don't allow the wake lock to be held for too long.
1466 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1467 }
1468 }
1469
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001470 /**
1471 * Called when the frontmost task is idle.
1472 * @return the state of mService.mBooting before this was called.
1473 */
1474 private boolean checkFinishBootingLocked() {
1475 final boolean booting = mService.mBooting;
1476 boolean enableScreen = false;
1477 mService.mBooting = false;
1478 if (!mService.mBooted) {
1479 mService.mBooted = true;
1480 enableScreen = true;
1481 }
1482 if (booting || enableScreen) {
1483 mService.postFinishBooting(booting, enableScreen);
1484 }
1485 return booting;
1486 }
1487
Craig Mautnerf3333272013-04-22 10:55:53 -07001488 // Checked.
1489 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
1490 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001491 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001492
Craig Mautnerf3333272013-04-22 10:55:53 -07001493 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001494 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001495 int NS = 0;
1496 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001497 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001498 boolean activityRemoved = false;
1499
Wale Ogunwale7d701172015-03-11 15:36:30 -07001500 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001501 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001502 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1503 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001504 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1505 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001506 if (fromTimeout) {
1507 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001508 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001509
1510 // This is a hack to semi-deal with a race condition
1511 // in the client where it can be constructed with a
1512 // newer configuration from when we asked it to launch.
1513 // We'll update with whatever configuration it now says
1514 // it used to launch.
1515 if (config != null) {
1516 r.configuration = config;
1517 }
1518
1519 // We are now idle. If someone is waiting for a thumbnail from
1520 // us, we can now deliver.
1521 r.idle = true;
1522
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001523 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001524 if (isFocusedStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001525 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001526 }
1527 }
1528
1529 if (allResumedActivitiesIdle()) {
1530 if (r != null) {
1531 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001532 }
1533
1534 if (mLaunchingActivity.isHeld()) {
1535 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1536 if (VALIDATE_WAKE_LOCK_CALLER &&
1537 Binder.getCallingUid() != Process.myUid()) {
1538 throw new IllegalStateException("Calling must be system uid");
1539 }
1540 mLaunchingActivity.release();
1541 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001542 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001543 }
1544
1545 // Atomically retrieve all of the other things to do.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001546 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(true);
Craig Mautnerf3333272013-04-22 10:55:53 -07001547 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001548 if ((NF = mFinishingActivities.size()) > 0) {
1549 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001550 mFinishingActivities.clear();
1551 }
1552
Craig Mautnerf3333272013-04-22 10:55:53 -07001553 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001554 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001555 mStartingUsers.clear();
1556 }
1557
Craig Mautnerf3333272013-04-22 10:55:53 -07001558 // Stop any activities that are scheduled to do so but have been
1559 // waiting for the next one to start.
1560 for (int i = 0; i < NS; i++) {
1561 r = stops.get(i);
1562 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001563 if (stack != null) {
1564 if (r.finishing) {
1565 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1566 } else {
1567 stack.stopActivityLocked(r);
1568 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001569 }
1570 }
1571
1572 // Finish any activities that are scheduled to do so but have been
1573 // waiting for the next one to start.
1574 for (int i = 0; i < NF; i++) {
1575 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07001576 final ActivityStack stack = r.task.stack;
1577 if (stack != null) {
1578 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1579 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001580 }
1581
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001582 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001583 // Complete user switch
1584 if (startingUsers != null) {
1585 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001586 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001587 }
1588 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001589 }
1590
1591 mService.trimApplications();
1592 //dump();
1593 //mWindowManager.dump();
1594
Craig Mautnerf3333272013-04-22 10:55:53 -07001595 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001596 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001597 }
1598
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001599 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001600 }
1601
Craig Mautner8e569572013-10-11 17:36:59 -07001602 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001603 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001604 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1605 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001606 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1607 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1608 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001609 }
Craig Mautner19091252013-10-05 00:03:53 -07001610 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001611 }
1612
1613 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001614 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1615 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001616 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1617 stacks.get(stackNdx).closeSystemDialogsLocked();
1618 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001619 }
1620 }
1621
Craig Mautner93529a42013-10-04 15:03:13 -07001622 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001623 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001624 }
1625
Craig Mautner8d341ef2013-03-26 09:03:27 -07001626 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001627 * Update the last used stack id for non-current user (current user's last
1628 * used stack is the focused stack)
1629 */
1630 void updateUserStackLocked(int userId, ActivityStack stack) {
1631 if (userId != mCurrentUser) {
1632 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1633 }
1634 }
1635
1636 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001637 * @return true if some activity was finished (or would have finished if doit were true).
1638 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001639 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1640 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001641 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001642 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1643 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001644 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001645 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001646 if (stack.finishDisabledPackageActivitiesLocked(
1647 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001648 didSomething = true;
1649 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001650 }
1651 }
1652 return didSomething;
1653 }
1654
Dianne Hackborna413dc02013-07-12 12:02:55 -07001655 void updatePreviousProcessLocked(ActivityRecord r) {
1656 // Now that this process has stopped, we may want to consider
1657 // it to be the previous app to try to keep around in case
1658 // the user wants to return to it.
1659
1660 // First, found out what is currently the foreground app, so that
1661 // we don't blow away the previous app if this activity is being
1662 // hosted by the process that is actually still the foreground.
1663 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001664 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1665 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001666 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1667 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001668 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001669 if (stack.mResumedActivity != null) {
1670 fgApp = stack.mResumedActivity.app;
1671 } else if (stack.mPausingActivity != null) {
1672 fgApp = stack.mPausingActivity.app;
1673 }
1674 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001675 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001676 }
1677 }
1678
1679 // Now set this one as the previous process, only if that really
1680 // makes sense to.
1681 if (r.app != null && fgApp != null && r.app != fgApp
1682 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001683 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001684 mService.mPreviousProcess = r.app;
1685 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1686 }
1687 }
1688
Wale Ogunwaled046a012015-12-24 13:05:59 -08001689 boolean resumeFocusedStackTopActivityLocked() {
1690 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001691 }
1692
Wale Ogunwaled046a012015-12-24 13:05:59 -08001693 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001694 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001695 if (targetStack != null && isFocusedStack(targetStack)) {
1696 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001697 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001698 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
1699 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001700 }
1701
Adrian Roos20d7df32016-01-12 18:59:43 +01001702 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
1703 TaskRecord finishedTask = null;
1704 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08001705 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1706 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001707 final int numStacks = stacks.size();
1708 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1709 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01001710 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
1711 if (stack == focusedStack || finishedTask == null) {
1712 finishedTask = t;
1713 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001714 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001715 }
Adrian Roos20d7df32016-01-12 18:59:43 +01001716 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001717 }
1718
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07001719 void finishVoiceTask(IVoiceInteractionSession session) {
1720 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1721 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1722 final int numStacks = stacks.size();
1723 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1724 final ActivityStack stack = stacks.get(stackNdx);
1725 stack.finishVoiceTask(session);
1726 }
1727 }
1728 }
1729
Chong Zhang280d3322015-11-03 17:27:26 -08001730 void findTaskToMoveToFrontLocked(
1731 TaskRecord task, int flags, ActivityOptions options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001732 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
1733 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001734 }
Craig Mautneraea74a52014-03-08 14:23:10 -08001735 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
1736 // Caller wants the home activity moved with it. To accomplish this,
1737 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07001738 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08001739 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07001740 if (task.stack == null) {
1741 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
1742 + task + " to front. Stack is null");
1743 return;
1744 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001745
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001746 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001747 int stackId = options.getLaunchStackId();
1748 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001749 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07001750 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08001751 if (stackId == INVALID_STACK_ID) {
1752 stackId = task.getLaunchStackId();
1753 }
Chong Zhang112eb8c2015-11-02 11:17:00 -08001754 if (stackId != task.stack.mStackId) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08001755 final ActivityStack stack = moveTaskToStackUncheckedLocked(
1756 task, stackId, ON_TOP, !FORCE_FOCUS, reason);
1757 stackId = stack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08001758 // moveTaskToStackUncheckedLocked() should already placed the task on top,
1759 // still need moveTaskToFrontLocked() below for any transition settings.
1760 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001761 if (StackId.resizeStackWithLaunchBounds(stackId)) {
1762 resizeStackLocked(stackId, bounds,
1763 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
1764 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */);
1765 } else {
1766 // WM resizeTask must be done after the task is moved to the correct stack,
1767 // because Task's setBounds() also updates dim layer's bounds, but that has
1768 // dependency on the stack.
1769 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig,
1770 false /* relayout */, false /* forced */);
1771 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001772 }
1773 }
1774
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001775 final ActivityRecord r = task.getTopActivity();
1776 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
1777 r == null ? null : r.appTimeTracker, reason);
1778
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001779 if (DEBUG_STACK) Slog.d(TAG_STACK,
1780 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001781 }
1782
Wale Ogunwale854809c2015-12-27 16:18:19 -08001783 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001784 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08001785 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08001786 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001787 return false;
1788 }
1789 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
1790 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001791 }
1792
Craig Mautner967212c2013-04-13 21:10:58 -07001793 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07001794 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001795 }
1796
1797 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001798 ActivityContainer activityContainer = mActivityContainers.get(stackId);
1799 if (activityContainer != null) {
1800 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001801 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07001802 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001803 return null;
1804 }
1805 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001806 }
1807
Craig Mautner967212c2013-04-13 21:10:58 -07001808 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07001809 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001810 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1811 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001812 }
1813 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07001814 }
1815
Craig Mautner4a1cb222013-12-04 16:14:06 -08001816 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001817 ActivityRecord homeActivity = getHomeActivity();
1818 if (homeActivity != null) {
1819 return homeActivity.appToken;
1820 }
1821 return null;
1822 }
1823
1824 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07001825 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001826 }
1827
1828 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001829 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
1830 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1831 final TaskRecord task = tasks.get(taskNdx);
1832 if (task.isHomeTask()) {
1833 final ArrayList<ActivityRecord> activities = task.mActivities;
1834 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1835 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001836 if (r.isHomeActivity()
1837 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001838 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001839 }
1840 }
1841 }
1842 }
1843 return null;
1844 }
1845
Chong Zhangb15758a2015-11-17 12:12:03 -08001846 /**
1847 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
1848 * the docked stack itself, or if it's side-by-side to the docked stack.
1849 */
1850 boolean isStackDockedInEffect(int stackId) {
1851 return stackId == DOCKED_STACK_ID ||
1852 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
1853 }
1854
Todd Kennedyca4d8422015-01-15 15:19:22 -08001855 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08001856 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07001857 ActivityContainer activityContainer =
1858 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001859 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001860 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1861 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001862 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001863 return activityContainer;
1864 }
1865
Craig Mautner34b73df2014-01-12 21:11:08 -08001866 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001867 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
1868 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
1869 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001870 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
1871 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001872 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08001873 }
1874 }
1875
Craig Mautner95da1082014-02-24 17:54:35 -08001876 void deleteActivityContainer(IActivityContainer container) {
1877 ActivityContainer activityContainer = (ActivityContainer)container;
1878 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001879 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1880 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08001881 final int stackId = activityContainer.mStackId;
1882 mActivityContainers.remove(stackId);
1883 mWindowManager.removeStack(stackId);
1884 }
1885 }
1886
Jorim Jaggidc249c42015-12-15 14:57:31 -08001887 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
1888 boolean preserveWindows, boolean allowResizeInDockedMode) {
1889 if (stackId == DOCKED_STACK_ID) {
1890 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
1891 preserveWindows);
1892 return;
1893 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08001894 final ActivityStack stack = getStack(stackId);
1895 if (stack == null) {
1896 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
1897 return;
1898 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08001899
Jorim Jaggidc249c42015-12-15 14:57:31 -08001900 if (!allowResizeInDockedMode && getStack(DOCKED_STACK_ID) != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07001901 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
1902 // stack size changing so things don't get out of sync.
1903 return;
1904 }
1905
Wale Ogunwalecad05a02015-09-25 10:41:44 -07001906 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02001907 mWindowManager.deferSurfaceLayout();
1908 try {
Jorim Jaggidc249c42015-12-15 14:57:31 -08001909 resizeStackUncheckedLocked(stack, bounds, tempTaskBounds, tempTaskInsetBounds);
1910 ensureConfigurationAndResume(stack, stack.topRunningActivityLocked(), preserveWindows);
Jorim Jaggic4025202015-10-22 16:43:34 +02001911 } finally {
1912 mWindowManager.continueSurfaceLayout();
1913 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001914 }
1915 }
1916
Jorim Jaggidc249c42015-12-15 14:57:31 -08001917 private void resizeStackUncheckedLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
1918 Rect tempTaskInsetBounds) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001919 bounds = TaskRecord.validateBounds(bounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001920
1921 mTmpBounds.clear();
1922 mTmpConfigs.clear();
1923 mTmpInsetBounds.clear();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001924 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
Jorim Jaggidc249c42015-12-15 14:57:31 -08001925 for (int i = tasks.size() - 1; i >= 0; i--) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001926 final TaskRecord task = tasks.get(i);
1927 if (task.isResizeable()) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08001928 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1929 // For freeform stack we don't adjust the size of the tasks to match that
1930 // of the stack, but we do try to make sure the tasks are still contained
1931 // with the bounds of the stack.
1932 tempRect2.set(task.mBounds);
1933 fitWithinBounds(tempRect2, bounds);
1934 task.updateOverrideConfiguration(tempRect2);
1935 } else {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001936 task.updateOverrideConfiguration(
Jorim Jaggi0a932142016-02-01 17:42:25 -08001937 tempTaskBounds != null ? tempTaskBounds : bounds,
1938 tempTaskInsetBounds != null ? tempTaskInsetBounds : bounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001939 }
1940 }
1941
1942 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
1943 mTmpBounds.put(task.taskId, task.mBounds);
1944 if (tempTaskInsetBounds != null) {
1945 mTmpInsetBounds.put(task.taskId, tempTaskInsetBounds);
1946 }
1947 }
Jorim Jaggi0429f352015-12-22 16:29:16 +01001948
1949 // We might trigger a configuration change. Save the current task bounds for freezing.
1950 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001951 stack.mFullscreen = mWindowManager.resizeStack(stack.mStackId, bounds, mTmpConfigs,
1952 mTmpBounds, mTmpInsetBounds);
1953 stack.setBounds(bounds);
1954 }
1955
1956 private void ensureConfigurationAndResume(ActivityStack stack, ActivityRecord r,
1957 boolean preserveWindows) {
Jorim Jaggic3fb3142016-02-04 19:49:28 -08001958 if (r == null || !r.visible) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08001959 return;
1960 }
1961 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0,
1962 preserveWindows);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001963 if (!updated) {
1964 resumeFocusedStackTopActivityLocked();
1965 }
1966 }
1967
1968 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
1969 Rect tempDockedTaskInsetBounds,
1970 Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds, boolean preserveWindows) {
1971 final ActivityStack stack = getStack(DOCKED_STACK_ID);
1972 if (stack == null) {
1973 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
1974 return;
1975 }
1976
1977 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
1978 mWindowManager.deferSurfaceLayout();
1979 try {
1980 ActivityRecord r = stack.topRunningActivityLocked();
1981 resizeStackUncheckedLocked(stack, dockedBounds, tempDockedTaskBounds,
1982 tempDockedTaskInsetBounds);
1983
1984 if (stack.mFullscreen) {
1985 // The dock stack went fullscreen which is kinda like dismissing it.
1986 // In this case we make all other static stacks fullscreen and move all
1987 // docked stack tasks to the fullscreen stack.
1988 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
1989 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
1990 resizeStackLocked(i, null, null, null, preserveWindows,
1991 true /* allowResizeInDockedMode */);
1992 }
1993 }
1994
1995 ArrayList<TaskRecord> tasks = stack.getAllTasks();
1996 final int count = tasks.size();
1997 for (int i = 0; i < count; i++) {
1998 moveTaskToStackLocked(tasks.get(i).taskId,
1999 FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP, FORCE_FOCUS, "resizeStack",
2000 false /* animate */);
2001 }
2002
2003 // stack shouldn't contain anymore activities, so nothing to resume.
2004 r = null;
2005 } else {
2006 // Docked stacks occupy a dedicated region on screen so the size of all other
2007 // static stacks need to be adjusted so they don't overlap with the docked stack.
2008 // We get the bounds to use from window manager which has been adjusted for any
2009 // screen controls and is also the same for all stacks.
Wale Ogunwaleccb6ce22016-01-14 15:36:35 -08002010 mWindowManager.getStackDockedModeBounds(
Wale Ogunwale961f4852016-02-01 20:25:54 -08002011 HOME_STACK_ID, tempRect, true /* ignoreVisibility */);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002012 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2013 if (StackId.isResizeableByDockedStack(i)) {
2014 ActivityStack otherStack = getStack(i);
2015 if (otherStack != null) {
2016 resizeStackLocked(i, tempRect, tempOtherTaskBounds,
2017 tempOtherTaskInsetBounds, preserveWindows,
2018 true /* allowResizeInDockedMode */);
2019 }
2020 }
2021 }
2022 }
2023 ensureConfigurationAndResume(stack, r, preserveWindows);
2024 } finally {
2025 mWindowManager.continueSurfaceLayout();
2026 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2027 }
2028
2029 mResizeDockedStackTimeout.notifyResizing(dockedBounds,
2030 tempDockedTaskBounds != null
2031 || tempDockedTaskInsetBounds != null
2032 || tempOtherTaskBounds != null
2033 || tempOtherTaskInsetBounds != null);
2034 }
2035
Chong Zhangf596cd52016-01-05 13:42:44 -08002036 boolean resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002037 if (!task.isResizeable()) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002038 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
Chong Zhangf596cd52016-01-05 13:42:44 -08002039 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002040 }
2041
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07002042 adjustForMinimalTaskDimensions(task, bounds);
2043
Chong Zhang87b21722015-09-21 15:39:51 -07002044 // If this is a forced resize, let it go through even if the bounds is not changing,
2045 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07002046 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002047 if (Objects.equals(task.mBounds, bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002048 // Nothing to do here...
Chong Zhangf596cd52016-01-05 13:42:44 -08002049 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002050 }
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002051 bounds = TaskRecord.validateBounds(bounds);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002052
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002053 if (!mWindowManager.isValidTaskId(task.taskId)) {
2054 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07002055 // All we can do for now is update the bounds so it can be used when the task is
2056 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002057 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002058 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
2059 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07002060 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002061 }
Chong Zhangf596cd52016-01-05 13:42:44 -08002062 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002063 }
2064
Chong Zhang6de2ae82015-09-30 18:25:21 -07002065 // Do not move the task to another stack here.
2066 // This method assumes that the task is already placed in the right stack.
2067 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002068
Chong Zhang6de2ae82015-09-30 18:25:21 -07002069 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07002070
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07002071 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07002072 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002073 // way and the activity was kept the way it was. If it's false, it means the activity had
2074 // to be relaunched due to configuration change.
2075 boolean kept = true;
2076 if (overrideConfig != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002077 ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08002078 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002079 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07002080 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002081 // All other activities must be made visible with their correct configuration.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002082 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002083 if (!kept) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002084 resumeFocusedStackTopActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08002085 }
2086 }
2087 }
Chong Zhang87b21722015-09-21 15:39:51 -07002088 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002089
2090 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Chong Zhangf596cd52016-01-05 13:42:44 -08002091 return kept;
Wale Ogunwale60454db2015-01-23 16:05:07 -08002092 }
2093
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07002094 private void adjustForMinimalTaskDimensions(TaskRecord task, Rect bounds) {
2095 if (bounds == null) {
2096 return;
2097 }
2098 int minimalSize = task.mMinimalSize == -1 ? mDefaultMinimalSizeOfResizeableTask
2099 : task.mMinimalSize;
2100 final boolean adjustWidth = minimalSize > bounds.width();
2101 final boolean adjustHeight = minimalSize > bounds.height();
2102 if (!(adjustWidth || adjustHeight)) {
2103 return;
2104 }
2105 Rect taskBounds = task.mBounds;
2106 if (adjustWidth) {
2107 if (taskBounds != null && bounds.right == taskBounds.right) {
2108 bounds.left = bounds.right - minimalSize;
2109 } else {
2110 // Either left bounds match, or neither match, or the previous bounds were
2111 // fullscreen and we default to keeping left.
2112 bounds.right = bounds.left + minimalSize;
2113 }
2114 }
2115 if (adjustHeight) {
2116 if (taskBounds != null && bounds.bottom == taskBounds.bottom) {
2117 bounds.top = bounds.bottom - minimalSize;
2118 } else {
2119 // Either top bounds match, or neither match, or the previous bounds were
2120 // fullscreen and we default to keeping top.
2121 bounds.bottom = bounds.top + minimalSize;
2122 }
2123 }
2124 }
2125
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002126 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002127 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2128 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002129 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002130 }
2131
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002132 ActivityContainer activityContainer = new ActivityContainer(stackId);
2133 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002134 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002135 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002136 }
2137
2138 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002139 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002140 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2141 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002142 break;
2143 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002144 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002145 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002146 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002147 }
2148
Chong Zhang5dcb2752015-08-18 13:50:26 -07002149 /**
2150 * Restores a recent task to a stack
2151 * @param task The recent task to be restored.
2152 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale3797c222015-10-27 14:21:58 -07002153 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002154 * @return true if the task has been restored successfully.
2155 */
2156 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
2157 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002158 stackId = task.getLaunchStackId();
Wale Ogunwale513346d2016-01-27 10:55:01 -08002159 } else if (stackId == DOCKED_STACK_ID && !task.canGoInDockedStack()) {
2160 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2161 // Put it in the fullscreen stack.
2162 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002163 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002164
Wale Ogunwale706ed792015-08-02 10:29:44 -07002165 if (task.stack != null) {
2166 // Task has already been restored once. See if we need to do anything more
2167 if (task.stack.mStackId == stackId) {
2168 // Nothing else to do since it is already restored in the right stack.
2169 return true;
2170 }
2171 // Remove current stack association, so we can re-associate the task with the
2172 // right stack below.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002173 task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002174 }
2175
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002176 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002177 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002178
2179 if (stack == null) {
2180 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002181 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2182 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002183 return false;
2184 }
2185
Wale Ogunwale5f986092015-12-04 15:35:38 -08002186 stack.addTask(task, false, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002187 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2188 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002189 final ArrayList<ActivityRecord> activities = task.mActivities;
2190 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002191 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002192 }
2193 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002194 }
2195
Wale Ogunwale040b4702015-08-06 18:10:50 -07002196 /**
2197 * Moves the specified task record to the input stack id.
2198 * WARNING: This method performs an unchecked/raw move of the task and
2199 * can leave the system in an unstable state if used incorrectly.
Wale Ogunwale513346d2016-01-27 10:55:01 -08002200 * Use {@link #moveTaskToStackLocked} to perform safe task movement to a stack.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002201 * @param task Task to move.
2202 * @param stackId Id of stack to move task to.
2203 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07002204 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07002205 * @param reason Reason the task is been moved.
2206 * @return The stack the task was moved to.
2207 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07002208 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07002209 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
2210 final ActivityRecord r = task.getTopActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -08002211 final ActivityStack prevStack = task.stack;
2212 final boolean wasFocused = isFocusedStack(prevStack) && (topRunningActivityLocked() == r);
2213 final boolean wasResumed = wasFocused && (prevStack.mResumedActivity == r);
2214 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
2215 // Whenever we are moving the top activity from the front stack we want to make sure to move
2216 // the stack to the front.
2217 final boolean wasFront = isFrontStack(prevStack)
2218 && (prevStack.topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07002219
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002220 final int resizeMode = task.mResizeMode;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002221
2222 if (stackId == DOCKED_STACK_ID && resizeMode == RESIZE_MODE_UNRESIZEABLE) {
2223 // We don't allow moving a unresizeable task to the docked stack since the docked
2224 // stack is used for split-screen mode and will cause things like the docked divider to
2225 // show up. We instead leave the task in its current stack or move it to the fullscreen
2226 // stack if it isn't currently in a stack.
2227 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002228 Slog.w(TAG, "Can not move unresizeable task=" + task
2229 + " to docked stack. Moving to stackId=" + stackId + " instead.");
2230 }
2231
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002232 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
2233 // if a docked stack is created below which will lead to the stack we are moving from and
2234 // its resizeable tasks being resized.
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002235 task.mTemporarilyUnresizable = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002236 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002237 task.mTemporarilyUnresizable = false;
Wale Ogunwale040b4702015-08-06 18:10:50 -07002238 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002239 stack.addTask(task, toTop, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002240
2241 // If the task had focus before (or we're requested to move focus),
Wale Ogunwaled046a012015-12-24 13:05:59 -08002242 // move focus to the new stack by moving the stack to the front.
2243 stack.moveToFrontAndResumeStateIfNeeded(
2244 r, forceFocus || wasFocused || wasFront, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002245
Wale Ogunwale040b4702015-08-06 18:10:50 -07002246 return stack;
2247 }
2248
Filip Gruszczynski90186c62015-10-26 14:07:00 -07002249 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08002250 String reason, boolean animate) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002251 final TaskRecord task = anyTaskForIdLocked(taskId);
2252 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002253 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002254 return;
2255 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002256
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002257 if (task.stack != null && task.stack.mStackId == stackId) {
2258 // You are already in the right stack silly...
2259 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
2260 return;
2261 }
2262
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002263 final ActivityRecord topActivity = task.getTopActivity();
Jorim Jaggie9098022016-01-27 19:29:40 -08002264 final int sourceStackId = task.stack != null ? task.stack.mStackId : INVALID_STACK_ID;
Chong Zhangf596cd52016-01-05 13:42:44 -08002265 final boolean mightReplaceWindow =
Jorim Jaggie9098022016-01-27 19:29:40 -08002266 StackId.replaceWindowsOnTaskMove(sourceStackId, stackId) && topActivity != null;
Chong Zhangf596cd52016-01-05 13:42:44 -08002267 if (mightReplaceWindow) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002268 // We are about to relaunch the activity because its configuration changed due to
2269 // being maximized, i.e. size change. The activity will first remove the old window
2270 // and then add a new one. This call will tell window manager about this, so it can
2271 // preserve the old window until the new one is drawn. This prevents having a gap
2272 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07002273 // entrance of the new window to be properly animated.
Chong Zhangf596cd52016-01-05 13:42:44 -08002274 // Note here we always set the replacing window first, as the flags might be needed
2275 // during the relaunch. If we end up not doing any relaunch, we clear the flags later.
Jorim Jaggi030979c2015-11-20 15:14:43 -08002276 mWindowManager.setReplacingWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002277 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002278
2279 mWindowManager.deferSurfaceLayout();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002280 final int preferredLaunchStackId = stackId;
Chong Zhangf596cd52016-01-05 13:42:44 -08002281 boolean kept = true;
Wale Ogunwale961f4852016-02-01 20:25:54 -08002282 try {
2283 final ActivityStack stack = moveTaskToStackUncheckedLocked(
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002284 task, stackId, toTop, forceFocus, reason + " moveTaskToStack");
Wale Ogunwale961f4852016-02-01 20:25:54 -08002285 stackId = stack.mStackId;
Jorim Jaggie9098022016-01-27 19:29:40 -08002286
Wale Ogunwale961f4852016-02-01 20:25:54 -08002287 if (!animate) {
2288 stack.mNoAnimActivities.add(topActivity);
2289 }
Jorim Jaggie9098022016-01-27 19:29:40 -08002290
Wale Ogunwale961f4852016-02-01 20:25:54 -08002291 // We might trigger a configuration change. Save the current task bounds for freezing.
2292 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
2293
2294 // Make sure the task has the appropriate bounds/size for the stack it is in.
2295 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
2296 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM, !mightReplaceWindow);
2297 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID
2298 && task.mBounds == null && task.mLastNonFullscreenBounds != null) {
2299 kept = resizeTaskLocked(task, task.mLastNonFullscreenBounds,
2300 RESIZE_MODE_SYSTEM, !mightReplaceWindow);
2301 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
2302 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM, !mightReplaceWindow);
2303 }
2304 } finally {
2305 mWindowManager.continueSurfaceLayout();
Chong Zhangf596cd52016-01-05 13:42:44 -08002306 }
2307
2308 if (mightReplaceWindow) {
2309 // If we didn't actual do a relaunch (indicated by kept==true meaning we kept the old
2310 // window), we need to clear the replace window settings. Otherwise, we schedule a
2311 // timeout to remove the old window if the replacing window is not coming in time.
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002312 mWindowManager.scheduleClearReplacingWindowIfNeeded(topActivity.appToken, !kept);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002313 }
2314
Stefan Kuhne54714cd2015-05-12 13:42:18 -07002315 // The task might have already been running and its visibility needs to be synchronized with
2316 // the visibility of the stack / windows.
Jorim Jaggie9098022016-01-27 19:29:40 -08002317 ensureActivitiesVisibleLocked(null, 0, !mightReplaceWindow);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002318 resumeFocusedStackTopActivityLocked();
Chong Zhangb15758a2015-11-17 12:12:03 -08002319
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002320 showNonResizeableDockToastIfNeeded(task, preferredLaunchStackId, stackId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002321 }
2322
Wale Ogunwale079a0042015-10-24 11:44:07 -07002323 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2324 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2325 if (stack == null) {
2326 throw new IllegalArgumentException(
2327 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2328 }
2329
2330 final ActivityRecord r = stack.topRunningActivityLocked();
2331 if (r == null) {
2332 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2333 + " in stack=" + stack);
2334 return false;
2335 }
2336
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002337 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002338 Slog.w(TAG,
2339 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002340 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002341 return false;
2342 }
2343
Wale Ogunwale480dca02016-02-06 13:58:29 -08002344 moveActivityToPinnedStackLocked(r, "moveTopActivityToPinnedStack", bounds);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002345 return true;
2346 }
2347
Wale Ogunwale480dca02016-02-06 13:58:29 -08002348 void moveActivityToPinnedStackLocked(ActivityRecord r, String reason, Rect bounds) {
2349 mWindowManager.deferSurfaceLayout();
2350 try {
2351 final TaskRecord task = r.task;
2352
2353 // Need to make sure the pinned stack exist so we can resize it below...
2354 final ActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
2355
2356 // Resize the pinned stack to match the current size of the task the activity we are
2357 // going to be moving is currently contained in. We do this to have the right starting
2358 // animation bounds for the pinned stack to the desired bounds the caller wants.
2359 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2360 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
2361 true /* allowResizeInDockedMode */);
2362
2363 if (task.mActivities.size() == 1) {
2364 // There is only one activity in the task. So, we can just move the task over to
2365 // the stack without re-parenting the activity in a different task.
2366 moveTaskToStackLocked(
2367 task.taskId, PINNED_STACK_ID, ON_TOP, FORCE_FOCUS, reason, !ANIMATE);
2368 } else {
2369 stack.moveActivityToStack(r);
2370 }
2371 } finally {
2372 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002373 }
2374
Wale Ogunwale480dca02016-02-06 13:58:29 -08002375 // The task might have already been running and its visibility needs to be synchronized
2376 // with the visibility of the stack / windows.
Wale Ogunwale079a0042015-10-24 11:44:07 -07002377 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002378 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002379
Wale Ogunwale480dca02016-02-06 13:58:29 -08002380 mWindowManager.animateResizePinnedStack(bounds);
2381 mService.notifyActivityPinnedLocked();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002382 }
2383
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002384 void positionTaskInStackLocked(int taskId, int stackId, int position) {
2385 final TaskRecord task = anyTaskForIdLocked(taskId);
2386 if (task == null) {
2387 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
2388 return;
2389 }
Wale Ogunwale935e5022015-11-10 12:36:10 -08002390 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
2391
2392 task.updateOverrideConfigurationForStack(stack);
2393
2394 mWindowManager.positionTaskInStack(
2395 taskId, stackId, position, task.mBounds, task.mOverrideConfig);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002396 stack.positionTask(task, position);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002397 // The task might have already been running and its visibility needs to be synchronized with
2398 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002399 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002400 resumeFocusedStackTopActivityLocked();
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002401 }
2402
Craig Mautnerac6f8432013-07-17 13:24:59 -07002403 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002404 mTmpFindTaskResult.r = null;
2405 mTmpFindTaskResult.matchedByRootAffinity = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002406 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002407 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2408 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002409 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2410 final ActivityStack stack = stacks.get(stackNdx);
2411 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002412 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002413 continue;
2414 }
2415 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002416 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2417 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002418 continue;
2419 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002420 stack.findTaskLocked(r, mTmpFindTaskResult);
2421 // It is possible to have task in multiple stacks with the same root affinity.
2422 // If the match we found was based on root affinity we keep on looking to see if
2423 // there is a better match in another stack. We eventually return the match based
2424 // on root affinity if we don't find a better match.
2425 if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
2426 return mTmpFindTaskResult.r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002427 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002428 }
2429 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002430 if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
2431 return mTmpFindTaskResult.r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002432 }
2433
2434 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002435 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2436 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002437 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2438 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2439 if (ar != null) {
2440 return ar;
2441 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002442 }
2443 }
2444 return null;
2445 }
2446
Craig Mautner8d341ef2013-03-26 09:03:27 -07002447 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002448 scheduleSleepTimeout();
2449 if (!mGoingToSleep.isHeld()) {
2450 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002451 if (mLaunchingActivity.isHeld()) {
2452 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2453 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002454 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002455 mLaunchingActivity.release();
2456 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002457 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002458 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002459 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002460 }
2461
2462 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002463 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002464
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002465 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002466 final long endTime = System.currentTimeMillis() + timeout;
2467 while (true) {
2468 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002469 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2470 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002471 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2472 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2473 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002474 }
2475 if (cantShutdown) {
2476 long timeRemaining = endTime - System.currentTimeMillis();
2477 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002478 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002479 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002480 } catch (InterruptedException e) {
2481 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002482 } else {
2483 Slog.w(TAG, "Activity manager shutdown timed out");
2484 timedout = true;
2485 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002486 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002487 } else {
2488 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002489 }
2490 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002491
2492 // Force checkReadyForSleep to complete.
2493 mSleepTimeout = true;
2494 checkReadyForSleepLocked();
2495
Craig Mautner8d341ef2013-03-26 09:03:27 -07002496 return timedout;
2497 }
2498
2499 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002500 removeSleepTimeouts();
2501 if (mGoingToSleep.isHeld()) {
2502 mGoingToSleep.release();
2503 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002504 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2505 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002506 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2507 final ActivityStack stack = stacks.get(stackNdx);
2508 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002509 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002510 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002511 }
Craig Mautner5314a402013-09-26 12:40:16 -07002512 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002513 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002514 mGoingToSleepActivities.clear();
2515 }
2516
2517 void activitySleptLocked(ActivityRecord r) {
2518 mGoingToSleepActivities.remove(r);
2519 checkReadyForSleepLocked();
2520 }
2521
2522 void checkReadyForSleepLocked() {
2523 if (!mService.isSleepingOrShuttingDown()) {
2524 // Do not care.
2525 return;
2526 }
2527
2528 if (!mSleepTimeout) {
2529 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002530 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2531 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002532 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2533 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2534 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002535 }
2536
2537 if (mStoppingActivities.size() > 0) {
2538 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002539 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002540 + mStoppingActivities.size() + " activities");
2541 scheduleIdleLocked();
2542 dontSleep = true;
2543 }
2544
2545 if (mGoingToSleepActivities.size() > 0) {
2546 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002547 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002548 + mGoingToSleepActivities.size() + " activities");
2549 dontSleep = true;
2550 }
2551
2552 if (dontSleep) {
2553 return;
2554 }
2555 }
2556
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 stacks.get(stackNdx).goToSleep();
2561 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002562 }
2563
2564 removeSleepTimeouts();
2565
2566 if (mGoingToSleep.isHeld()) {
2567 mGoingToSleep.release();
2568 }
2569 if (mService.mShuttingDown) {
2570 mService.notifyAll();
2571 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002572 }
2573
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002574 boolean reportResumedActivityLocked(ActivityRecord r) {
2575 final ActivityStack stack = r.task.stack;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002576 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002577 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002578 }
2579 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002580 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002581 mWindowManager.executeAppTransition();
2582 return true;
2583 }
2584 return false;
2585 }
2586
Craig Mautner8d341ef2013-03-26 09:03:27 -07002587 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002588 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2589 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07002590 int stackNdx = stacks.size() - 1;
2591 while (stackNdx >= 0) {
2592 stacks.get(stackNdx).handleAppCrashLocked(app);
2593 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002594 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002595 }
2596 }
2597
Jose Lima4b6c6692014-08-12 17:41:12 -07002598 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002599 final ActivityStack stack = r.task.stack;
2600 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002601 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2602 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002603 return false;
2604 }
Jose Lima4b6c6692014-08-12 17:41:12 -07002605 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002606 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2607 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002608
2609 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002610 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002611 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07002612 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002613 return true;
2614 }
2615
2616 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07002617 if (visible && top.fullscreen) {
2618 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002619 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2620 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
2621 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
2622 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002623 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07002624 } else if (!visible && stack.getVisibleBehindActivity() != r) {
2625 // Only the activity set as currently visible behind should actively reset its
2626 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002627 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2628 "requestVisibleBehind: returning visible=" + visible
2629 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
2630 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07002631 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002632 }
2633
Jose Lima4b6c6692014-08-12 17:41:12 -07002634 stack.setVisibleBehindActivity(visible ? r : null);
2635 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002636 // If there is a translucent home activity, we need to force it stop being translucent,
2637 // because we can't depend on the application to necessarily perform that operation.
2638 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002639 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002640 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002641 mService.convertFromTranslucent(next.appToken);
2642 }
2643 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07002644 if (top.app != null && top.app.thread != null) {
2645 // Notify the top app of the change.
2646 try {
2647 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
2648 } catch (RemoteException e) {
2649 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07002650 }
2651 return true;
2652 }
2653
Craig Mautnerbb742462014-07-07 15:28:55 -07002654 // Called when WindowManager has finished animating the launchingBehind activity to the back.
2655 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
2656 r.mLaunchTaskBehind = false;
2657 final TaskRecord task = r.task;
Winson8b1871d2015-11-20 09:56:20 -08002658 task.setLastThumbnailLocked(task.stack.screenshotActivitiesLocked(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002659 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08002660 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07002661 mWindowManager.setAppVisibility(r.appToken, false);
2662 }
2663
2664 void scheduleLaunchTaskBehindComplete(IBinder token) {
2665 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2666 }
2667
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002668 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
2669 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07002670 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002671 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2672 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002673 final int topStackNdx = stacks.size() - 1;
2674 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2675 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002676 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07002677 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002678 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002679 }
2680
Chong Zhangfdcc4d42015-10-14 16:50:12 -07002681 void invalidateTaskLayers() {
2682 mTaskLayersChanged = true;
2683 }
2684
2685 void rankTaskLayersIfNeeded() {
2686 if (!mTaskLayersChanged) {
2687 return;
2688 }
2689 mTaskLayersChanged = false;
2690 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
2691 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2692 int baseLayer = 0;
2693 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2694 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
2695 }
2696 }
2697 }
2698
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002699 void clearOtherAppTimeTrackers(AppTimeTracker except) {
2700 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2701 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2702 final int topStackNdx = stacks.size() - 1;
2703 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2704 final ActivityStack stack = stacks.get(stackNdx);
2705 stack.clearOtherAppTimeTrackers(except);
2706 }
2707 }
2708 }
2709
Craig Mautner8d341ef2013-03-26 09:03:27 -07002710 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002711 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2712 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002713 final int numStacks = stacks.size();
2714 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2715 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002716 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002717 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002718 }
2719 }
2720
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002721 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
2722 // Examine all activities currently running in the process.
2723 TaskRecord firstTask = null;
2724 // Tasks is non-null only if two or more tasks are found.
2725 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002726 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
2727 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002728 ActivityRecord r = app.activities.get(i);
2729 // First, if we find an activity that is in the process of being destroyed,
2730 // then we just aren't going to do anything for now; we want things to settle
2731 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002732 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002733 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002734 return;
2735 }
2736 // Don't consider any activies that are currently not in a state where they
2737 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002738 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
2739 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002740 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002741 continue;
2742 }
2743 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002744 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002745 + " from " + r);
2746 if (firstTask == null) {
2747 firstTask = r.task;
2748 } else if (firstTask != r.task) {
2749 if (tasks == null) {
2750 tasks = new ArraySet<>();
2751 tasks.add(firstTask);
2752 }
2753 tasks.add(r.task);
2754 }
2755 }
2756 }
2757 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002758 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002759 return;
2760 }
2761 // If we have activities in multiple tasks that are in a position to be destroyed,
2762 // let's iterate through the tasks and release the oldest one.
2763 final int numDisplays = mActivityDisplays.size();
2764 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
2765 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2766 // Step through all stacks starting from behind, to hit the oldest things first.
2767 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
2768 final ActivityStack stack = stacks.get(stackNdx);
2769 // Try to release activities in this stack; if we manage to, we are done.
2770 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
2771 return;
2772 }
2773 }
2774 }
2775 }
2776
Amith Yamasani37a40c22015-06-17 13:25:42 -07002777 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002778 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002779 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07002780 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002781
Craig Mautner858d8a62013-04-23 17:08:34 -07002782 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08002783 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2784 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002785 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002786 final ActivityStack stack = stacks.get(stackNdx);
2787 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002788 TaskRecord task = stack.topTask();
2789 if (task != null) {
2790 mWindowManager.moveTaskToTop(task.taskId);
2791 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002792 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07002793 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002794
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002795 ActivityStack stack = getStack(restoreStackId);
2796 if (stack == null) {
2797 stack = mHomeStack;
2798 }
2799 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002800 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07002801 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08002802 } else {
2803 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08002804 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08002805 }
Craig Mautner93529a42013-10-04 15:03:13 -07002806 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07002807 }
2808
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002809 /** Checks whether the userid is a profile of the current user. */
2810 boolean isCurrentProfileLocked(int userId) {
2811 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002812 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002813 }
2814
Chong Zhang45c25ce2015-08-10 22:18:26 -07002815 /** Checks whether the activity should be shown for current user. */
2816 boolean okToShowLocked(ActivityRecord r) {
2817 return r != null && (isCurrentProfileLocked(r.userId)
2818 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
2819 }
2820
Craig Mautnerde4ef022013-04-07 19:01:33 -07002821 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002822 ArrayList<ActivityRecord> stops = null;
2823
2824 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08002825 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2826 ActivityRecord s = mStoppingActivities.get(activityNdx);
2827 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002828 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08002829 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
2830 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002831 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002832 if (s.finishing) {
2833 // If this activity is finishing, it is sitting on top of
2834 // everyone else but we now know it is no longer needed...
2835 // so get rid of it. Otherwise, we need to go through the
2836 // normal flow and hide it once we determine that it is
2837 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002838 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002839 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002840 }
2841 }
Craig Mautner8c14c152015-01-15 17:32:07 -08002842 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002843 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002844 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08002845 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002846 }
2847 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08002848 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002849 }
2850 }
2851
2852 return stops;
2853 }
2854
Craig Mautnercf910b02013-04-23 11:23:27 -07002855 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002856 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2857 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2858 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2859 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002860 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002861 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002862 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002863 if (r == null) Slog.e(TAG,
2864 "validateTop...: null top activity, stack=" + stack);
2865 else {
2866 final ActivityRecord pausing = stack.mPausingActivity;
2867 if (pausing != null && pausing == r) Slog.e(TAG,
2868 "validateTop...: top stack has pausing activity r=" + r
2869 + " state=" + state);
2870 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
2871 "validateTop...: activity in front not resumed r=" + r
2872 + " state=" + state);
2873 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002874 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002875 final ActivityRecord resumed = stack.mResumedActivity;
2876 if (resumed != null && resumed == r) Slog.e(TAG,
2877 "validateTop...: back stack has resumed activity r=" + r
2878 + " state=" + state);
2879 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
2880 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002881 }
2882 }
2883 }
Craig Mautner76ea2242013-05-15 11:40:05 -07002884 }
2885
Craig Mautnere0570202015-05-13 13:06:11 -07002886 private String lockTaskModeToString() {
2887 switch (mLockTaskModeState) {
2888 case LOCK_TASK_MODE_LOCKED:
2889 return "LOCKED";
2890 case LOCK_TASK_MODE_PINNED:
2891 return "PINNED";
2892 case LOCK_TASK_MODE_NONE:
2893 return "NONE";
2894 default: return "unknown=" + mLockTaskModeState;
2895 }
2896 }
2897
Craig Mautner27084302013-03-25 08:05:25 -07002898 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002899 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002900 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002901 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08002902 pw.print(prefix);
2903 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002904 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08002905 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07002906 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
2907 final SparseArray<String[]> packages = mService.mLockTaskPackages;
2908 if (packages.size() > 0) {
2909 pw.println(" mLockTaskPackages (userId:packages)=");
2910 for (int i = 0; i < packages.size(); ++i) {
2911 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
2912 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
2913 }
2914 }
2915 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07002916 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002917
Craig Mautner20e72272013-04-01 13:45:53 -07002918 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002919 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07002920 }
2921
Dianne Hackborn390517b2013-05-30 15:03:32 -07002922 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
2923 boolean needSep, String prefix) {
2924 if (activity != null) {
2925 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
2926 if (needSep) {
2927 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07002928 }
2929 pw.print(prefix);
2930 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002931 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002932 }
2933 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002934 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002935 }
2936
Craig Mautner8d341ef2013-03-26 09:03:27 -07002937 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
2938 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002939 boolean printed = false;
2940 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002941 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
2942 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002943 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07002944 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08002945 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07002946 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002947 final ActivityStack stack = stacks.get(stackNdx);
2948 StringBuilder stackHeader = new StringBuilder(128);
2949 stackHeader.append(" Stack #");
2950 stackHeader.append(stack.mStackId);
2951 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002952 stackHeader.append("\n");
2953 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
2954 stackHeader.append("\n");
2955 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002956 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
2957 needSep, stackHeader.toString());
2958 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
2959 !dumpAll, false, dumpPackage, true,
2960 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002961
Craig Mautner4a1cb222013-12-04 16:14:06 -08002962 needSep = printed;
2963 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
2964 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002965 if (pr) {
2966 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002967 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002968 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002969 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
2970 " mResumedActivity: ");
2971 if (pr) {
2972 printed = true;
2973 needSep = false;
2974 }
2975 if (dumpAll) {
2976 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
2977 " mLastPausedActivity: ");
2978 if (pr) {
2979 printed = true;
2980 needSep = true;
2981 }
2982 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
2983 needSep, " mLastNoHistoryActivity: ");
2984 }
2985 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002986 }
2987 }
2988
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002989 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
2990 false, dumpPackage, true, " Activities waiting to finish:", null);
2991 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
2992 false, dumpPackage, true, " Activities waiting to stop:", null);
2993 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
2994 false, dumpPackage, true, " Activities waiting for another to become visible:",
2995 null);
2996 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
2997 false, dumpPackage, true, " Activities waiting to sleep:", null);
2998 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
2999 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003000
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003001 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003002 }
3003
Dianne Hackborn390517b2013-05-30 15:03:32 -07003004 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003005 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003006 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003007 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003008 String innerPrefix = null;
3009 String[] args = null;
3010 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003011 for (int i=list.size()-1; i>=0; i--) {
3012 final ActivityRecord r = list.get(i);
3013 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3014 continue;
3015 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003016 if (innerPrefix == null) {
3017 innerPrefix = prefix + " ";
3018 args = new String[0];
3019 }
3020 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003021 final boolean full = !brief && (complete || !r.isInHistory());
3022 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003023 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003024 needNL = false;
3025 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003026 if (header1 != null) {
3027 pw.println(header1);
3028 header1 = null;
3029 }
3030 if (header2 != null) {
3031 pw.println(header2);
3032 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003033 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003034 if (lastTask != r.task) {
3035 lastTask = r.task;
3036 pw.print(prefix);
3037 pw.print(full ? "* " : " ");
3038 pw.println(lastTask);
3039 if (full) {
3040 lastTask.dump(pw, prefix + " ");
3041 } else if (complete) {
3042 // Complete + brief == give a summary. Isn't that obvious?!?
3043 if (lastTask.intent != null) {
3044 pw.print(prefix); pw.print(" ");
3045 pw.println(lastTask.intent.toInsecureStringWithClip());
3046 }
3047 }
3048 }
3049 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3050 pw.print(" #"); pw.print(i); pw.print(": ");
3051 pw.println(r);
3052 if (full) {
3053 r.dump(pw, innerPrefix);
3054 } else if (complete) {
3055 // Complete + brief == give a summary. Isn't that obvious?!?
3056 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3057 if (r.app != null) {
3058 pw.print(innerPrefix); pw.println(r.app);
3059 }
3060 }
3061 if (client && r.app != null && r.app.thread != null) {
3062 // flush anything that is already in the PrintWriter since the thread is going
3063 // to write to the file descriptor directly
3064 pw.flush();
3065 try {
3066 TransferPipe tp = new TransferPipe();
3067 try {
3068 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3069 r.appToken, innerPrefix, args);
3070 // Short timeout, since blocking here can
3071 // deadlock with the application.
3072 tp.go(fd, 2000);
3073 } finally {
3074 tp.kill();
3075 }
3076 } catch (IOException e) {
3077 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3078 } catch (RemoteException e) {
3079 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3080 }
3081 needNL = true;
3082 }
3083 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003084 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003085 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003086
Craig Mautnerf3333272013-04-22 10:55:53 -07003087 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003088 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3089 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003090 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3091 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003092 }
3093
3094 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003095 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003096 }
3097
3098 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003099 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3100 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003101 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3102 }
3103
Craig Mautner05d29032013-05-03 13:40:13 -07003104 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003105 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3106 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3107 }
Craig Mautner05d29032013-05-03 13:40:13 -07003108 }
3109
Craig Mautner0eea92c2013-05-16 13:35:39 -07003110 void removeSleepTimeouts() {
3111 mSleepTimeout = false;
3112 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3113 }
3114
3115 final void scheduleSleepTimeout() {
3116 removeSleepTimeouts();
3117 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3118 }
3119
Craig Mautner4a1cb222013-12-04 16:14:06 -08003120 @Override
3121 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003122 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003123 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3124 }
3125
3126 @Override
3127 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003128 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003129 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3130 }
3131
3132 @Override
3133 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003134 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003135 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3136 }
3137
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003138 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003139 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003140 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003141 newDisplay = mActivityDisplays.get(displayId) == null;
3142 if (newDisplay) {
3143 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003144 if (activityDisplay.mDisplay == null) {
3145 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3146 return;
3147 }
Craig Mautner4504de52013-12-20 09:06:56 -08003148 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003149 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08003150 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003151 }
Craig Mautner4504de52013-12-20 09:06:56 -08003152 if (newDisplay) {
3153 mWindowManager.onDisplayAdded(displayId);
3154 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003155 }
3156
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003157 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
3158 if (display.mDisplayId != Display.DEFAULT_DISPLAY) {
3159 return;
3160 }
3161 final float fraction = mService.mContext.getResources().getFraction(com.android.internal.R.
3162 fraction.config_displayFractionForDefaultMinimalSizeOfResizeableTask, 1, 1);
3163 mDefaultMinimalSizeOfResizeableTask = (int) (fraction * Math.min(
3164 display.mDisplayInfo.logicalWidth, display.mDisplayInfo.logicalHeight));
3165 }
3166
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003167 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003168 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003169 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3170 if (activityDisplay != null) {
3171 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003172 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003173 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003174 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003175 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003176 }
3177 }
3178 mWindowManager.onDisplayRemoved(displayId);
3179 }
3180
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003181 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003182 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003183 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3184 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003185 // TODO: Update the bounds.
3186 }
3187 }
3188 mWindowManager.onDisplayChanged(displayId);
3189 }
3190
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003191 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003192 StackInfo info = new StackInfo();
3193 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3194 info.displayId = Display.DEFAULT_DISPLAY;
3195 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003196 info.userId = stack.mCurrentUser;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003197
3198 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3199 final int numTasks = tasks.size();
3200 int[] taskIds = new int[numTasks];
3201 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003202 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003203 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003204 for (int i = 0; i < numTasks; ++i) {
3205 final TaskRecord task = tasks.get(i);
3206 taskIds[i] = task.taskId;
3207 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3208 : task.realActivity != null ? task.realActivity.flattenToString()
3209 : task.getTopActivity() != null ? task.getTopActivity().packageName
3210 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003211 taskBounds[i] = new Rect();
3212 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003213 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003214 }
3215 info.taskIds = taskIds;
3216 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003217 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003218 info.taskUserIds = taskUserIds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003219 return info;
3220 }
3221
3222 StackInfo getStackInfoLocked(int stackId) {
3223 ActivityStack stack = getStack(stackId);
3224 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003225 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003226 }
3227 return null;
3228 }
3229
3230 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003231 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003232 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3233 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003234 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003235 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003236 }
3237 }
3238 return list;
3239 }
3240
Craig Mautner15df08a2015-04-01 12:17:18 -07003241 TaskRecord getLockedTaskLocked() {
3242 final int top = mLockTaskModeTasks.size() - 1;
3243 if (top >= 0) {
3244 return mLockTaskModeTasks.get(top);
3245 }
3246 return null;
3247 }
3248
3249 boolean isLockedTask(TaskRecord task) {
3250 return mLockTaskModeTasks.contains(task);
3251 }
3252
3253 boolean isLastLockedTask(TaskRecord task) {
3254 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3255 }
3256
3257 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003258 if (!mLockTaskModeTasks.remove(task)) {
3259 return;
3260 }
3261 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3262 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003263 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003264 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3265 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003266 final Message lockTaskMsg = Message.obtain();
3267 lockTaskMsg.arg1 = task.userId;
3268 lockTaskMsg.what = LOCK_TASK_END_MSG;
3269 mHandler.sendMessage(lockTaskMsg);
3270 }
3271 }
3272
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003273 void showNonResizeableDockToastIfNeeded(
3274 TaskRecord task, int preferredStackId, int actualStackId) {
3275 if (!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID) {
3276 return;
3277 }
3278
3279 if (!task.canGoInDockedStack() || task.inCropWindowsResizeMode()) {
3280 // Display warning toast if we tried to put a non-dockable task in the docked stack or
3281 // the task is running in cropped window mode.
3282 mWindowManager.scheduleShowNonResizeableDockToast(task.taskId);
3283 }
Chong Zhangb15758a2015-11-17 12:12:03 -08003284 }
3285
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003286 void showLockTaskToast() {
3287 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04003288 }
3289
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003290 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3291 if (mLockTaskModeTasks.contains(task)) {
3292 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3293 }
3294 }
3295
Craig Mautner432f64e2015-05-20 14:59:57 -07003296 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3297 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003298 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003299 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003300 final TaskRecord lockedTask = getLockedTaskLocked();
3301 if (lockedTask != null) {
3302 removeLockedTaskLocked(lockedTask);
3303 if (!mLockTaskModeTasks.isEmpty()) {
3304 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003305 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3306 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003307 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003308 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003309 return;
3310 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003311 }
Craig Mautnere0570202015-05-13 13:06:11 -07003312 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3313 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003314 return;
3315 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003316
3317 // Should have already been checked, but do it again.
3318 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003319 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3320 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003321 return;
3322 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003323 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003324 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003325 return;
3326 }
3327
3328 if (mLockTaskModeTasks.isEmpty()) {
3329 // First locktask.
3330 final Message lockTaskMsg = Message.obtain();
3331 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3332 lockTaskMsg.arg1 = task.userId;
3333 lockTaskMsg.what = LOCK_TASK_START_MSG;
3334 lockTaskMsg.arg2 = lockTaskModeState;
3335 mHandler.sendMessage(lockTaskMsg);
3336 }
3337 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003338 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3339 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003340 mLockTaskModeTasks.remove(task);
3341 mLockTaskModeTasks.add(task);
3342
3343 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07003344 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07003345 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003346
3347 if (andResume) {
3348 findTaskToMoveToFrontLocked(task, 0, null, reason);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003349 resumeFocusedStackTopActivityLocked();
Craig Mautner432f64e2015-05-20 14:59:57 -07003350 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003351 }
3352
3353 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003354 return isLockTaskModeViolation(task, false);
3355 }
3356
3357 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3358 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003359 return false;
3360 }
3361 final int lockTaskAuth = task.mLockTaskAuth;
3362 switch (lockTaskAuth) {
3363 case LOCK_TASK_AUTH_DONT_LOCK:
3364 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003365 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003366 case LOCK_TASK_AUTH_LAUNCHABLE:
3367 case LOCK_TASK_AUTH_WHITELISTED:
3368 return false;
3369 case LOCK_TASK_AUTH_PINNABLE:
3370 // Pinnable tasks can't be launched on top of locktask tasks.
3371 return !mLockTaskModeTasks.isEmpty();
3372 default:
3373 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3374 return true;
3375 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003376 }
3377
Craig Mautner15df08a2015-04-01 12:17:18 -07003378 void onLockTaskPackagesUpdatedLocked() {
3379 boolean didSomething = false;
3380 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3381 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01003382 final boolean wasWhitelisted =
3383 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3384 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07003385 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01003386 final boolean isWhitelisted =
3387 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3388 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3389 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003390 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003391 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3392 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003393 removeLockedTaskLocked(lockedTask);
3394 lockedTask.performClearTaskLocked();
3395 didSomething = true;
3396 }
3397 }
3398 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3399 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3400 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3401 final ActivityStack stack = stacks.get(stackNdx);
3402 stack.onLockTaskPackagesUpdatedLocked();
3403 }
3404 }
Craig Mautnere0570202015-05-13 13:06:11 -07003405 final ActivityRecord r = topRunningActivityLocked();
3406 final TaskRecord task = r != null ? r.task : null;
3407 if (mLockTaskModeTasks.isEmpty() && task != null
3408 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3409 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07003410 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
3411 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
3412 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
3413 false);
3414 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07003415 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003416 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003417 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003418 }
3419 }
3420
Benjamin Franz43261142015-02-11 15:59:44 +00003421 int getLockTaskModeState() {
3422 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003423 }
3424
Jorim Jaggife89d122015-12-22 16:28:44 +01003425 void activityRelaunchedLocked(IBinder token) {
3426 mWindowManager.notifyAppRelaunchingFinished(token);
3427 }
3428
3429 void activityRelaunchingLocked(ActivityRecord r) {
3430 mWindowManager.notifyAppRelaunching(r.appToken);
3431 }
3432
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003433 void logStackState() {
3434 mActivityMetricsLogger.logWindowState();
3435 }
3436
Wale Ogunwale22e25262016-02-01 10:32:02 -08003437 void scheduleReportMultiWindowChanged(TaskRecord task) {
3438 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3439 final ActivityRecord r = task.mActivities.get(i);
3440 if (r.app != null && r.app.thread != null) {
3441 mMultiWindowModeChangedActivities.add(r);
3442 }
3443 }
3444
3445 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
3446 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
3447 }
3448 }
3449
3450 void scheduleReportPictureInPictureChangedIfNeeded(TaskRecord task, ActivityStack prevStack) {
3451 final ActivityStack stack = task.stack;
3452 if (prevStack == null || prevStack == stack
3453 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
3454 return;
3455 }
3456
3457 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3458 final ActivityRecord r = task.mActivities.get(i);
3459 if (r.app != null && r.app.thread != null) {
3460 mPipModeChangedActivities.add(r);
3461 }
3462 }
3463
3464 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
3465 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
3466 }
3467 }
3468
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003469 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003470
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003471 public ActivityStackSupervisorHandler(Looper looper) {
3472 super(looper);
3473 }
3474
Craig Mautnerf3333272013-04-22 10:55:53 -07003475 void activityIdleInternal(ActivityRecord r) {
3476 synchronized (mService) {
3477 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3478 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003479 }
3480
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003481 @Override
3482 public void handleMessage(Message msg) {
3483 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003484 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
3485 synchronized (mService) {
3486 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
3487 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
3488 r.scheduleMultiWindowChanged();
3489 }
3490 }
3491 } break;
3492 case REPORT_PIP_MODE_CHANGED_MSG: {
3493 synchronized (mService) {
3494 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
3495 final ActivityRecord r = mPipModeChangedActivities.remove(i);
3496 r.schedulePictureInPictureChanged();
3497 }
3498 }
3499 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07003500 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003501 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3502 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003503 if (mService.mDidDexOpt) {
3504 mService.mDidDexOpt = false;
3505 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3506 nmsg.obj = msg.obj;
3507 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3508 return;
3509 }
3510 // We don't at this point know if the activity is fullscreen,
3511 // so we need to be conservative and assume it isn't.
3512 activityIdleInternal((ActivityRecord)msg.obj);
3513 } break;
3514 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003515 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003516 activityIdleInternal((ActivityRecord)msg.obj);
3517 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003518 case RESUME_TOP_ACTIVITY_MSG: {
3519 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003520 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07003521 }
3522 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003523 case SLEEP_TIMEOUT_MSG: {
3524 synchronized (mService) {
3525 if (mService.isSleepingOrShuttingDown()) {
3526 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3527 mSleepTimeout = true;
3528 checkReadyForSleepLocked();
3529 }
3530 }
3531 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003532 case LAUNCH_TIMEOUT_MSG: {
3533 if (mService.mDidDexOpt) {
3534 mService.mDidDexOpt = false;
3535 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3536 return;
3537 }
3538 synchronized (mService) {
3539 if (mLaunchingActivity.isHeld()) {
3540 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3541 if (VALIDATE_WAKE_LOCK_CALLER
3542 && Binder.getCallingUid() != Process.myUid()) {
3543 throw new IllegalStateException("Calling must be system uid");
3544 }
3545 mLaunchingActivity.release();
3546 }
3547 }
3548 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003549 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003550 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003551 } break;
3552 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003553 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003554 } break;
3555 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003556 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003557 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003558 case CONTAINER_CALLBACK_VISIBILITY: {
3559 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003560 final IActivityContainerCallback callback = container.mCallback;
3561 if (callback != null) {
3562 try {
3563 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3564 } catch (RemoteException e) {
3565 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003566 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003567 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003568 case LOCK_TASK_START_MSG: {
3569 // When lock task starts, we disable the status bars.
3570 try {
Jason Monk62515be2014-05-21 16:06:19 -04003571 if (mLockTaskNotify == null) {
3572 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003573 }
Jason Monk62515be2014-05-21 16:06:19 -04003574 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003575 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003576 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003577 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003578 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003579 flags = StatusBarManager.DISABLE_MASK
3580 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07003581 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003582 flags = StatusBarManager.DISABLE_MASK
3583 & (~StatusBarManager.DISABLE_BACK)
3584 & (~StatusBarManager.DISABLE_HOME)
3585 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003586 }
3587 getStatusBarService().disable(flags, mToken,
3588 mService.mContext.getPackageName());
3589 }
3590 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003591 if (getDevicePolicyManager() != null) {
3592 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003593 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003594 }
justinzhang5286d3f2014-05-12 17:06:01 -04003595 } catch (RemoteException ex) {
3596 throw new RuntimeException(ex);
3597 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003598 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003599 case LOCK_TASK_END_MSG: {
3600 // When lock task ends, we enable the status bars.
3601 try {
Jason Monk62515be2014-05-21 16:06:19 -04003602 if (getStatusBarService() != null) {
3603 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3604 mService.mContext.getPackageName());
3605 }
3606 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003607 if (getDevicePolicyManager() != null) {
3608 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3609 msg.arg1);
3610 }
Jason Monk62515be2014-05-21 16:06:19 -04003611 if (mLockTaskNotify == null) {
3612 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3613 }
3614 mLockTaskNotify.show(false);
3615 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003616 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003617 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003618 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003619 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003620 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003621 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003622 new LockPatternUtils(mService.mContext)
3623 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003624 }
3625 } catch (SettingNotFoundException e) {
3626 // No setting, don't lock.
3627 }
justinzhang5286d3f2014-05-12 17:06:01 -04003628 } catch (RemoteException ex) {
3629 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003630 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07003631 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003632 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003633 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003634 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
3635 if (mLockTaskNotify == null) {
3636 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3637 }
3638 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
3639 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003640 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3641 final ActivityContainer container = (ActivityContainer) msg.obj;
3642 final IActivityContainerCallback callback = container.mCallback;
3643 if (callback != null) {
3644 try {
3645 callback.onAllActivitiesComplete(container.asBinder());
3646 } catch (RemoteException e) {
3647 }
3648 }
3649 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003650 case LAUNCH_TASK_BEHIND_COMPLETE: {
3651 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003652 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07003653 if (r != null) {
3654 handleLaunchTaskBehindCompleteLocked(r);
3655 }
3656 }
3657 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08003658
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003659 }
3660 }
3661 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003662
Ying Wangb081a592014-04-22 15:20:16 -07003663 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08003664 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
3665 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003666 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003667 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003668 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003669 ActivityRecord mParentActivity = null;
3670 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003671
Craig Mautnere3a00d72014-04-16 08:31:19 -07003672 boolean mVisible = true;
3673
Craig Mautner4a1cb222013-12-04 16:14:06 -08003674 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003675 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003676
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003677 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3678 final static int CONTAINER_STATE_NO_SURFACE = 1;
3679 final static int CONTAINER_STATE_FINISHING = 2;
3680 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3681
3682 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003683 synchronized (mService) {
3684 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003685 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003686 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003687 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003688 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003689 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003690
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003691 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003692 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003693 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08003694 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003695 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003696 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003697 }
3698
3699 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003700 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003701 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003702 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3703 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003704 return;
3705 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003706 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003707 }
3708 }
3709
3710 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003711 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003712 synchronized (mService) {
3713 if (mActivityDisplay != null) {
3714 return mActivityDisplay.mDisplayId;
3715 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003716 }
3717 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003718 }
3719
Jeff Brownca9bc702014-02-11 14:32:56 -08003720 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08003721 public int getStackId() {
3722 synchronized (mService) {
3723 return mStackId;
3724 }
3725 }
3726
3727 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003728 public boolean injectEvent(InputEvent event) {
3729 final long origId = Binder.clearCallingIdentity();
3730 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003731 synchronized (mService) {
3732 if (mActivityDisplay != null) {
3733 return mInputManagerInternal.injectInputEvent(event,
3734 mActivityDisplay.mDisplayId,
3735 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3736 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003737 }
3738 return false;
3739 } finally {
3740 Binder.restoreCallingIdentity(origId);
3741 }
3742 }
3743
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003744 @Override
3745 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003746 synchronized (mService) {
3747 if (mContainerState == CONTAINER_STATE_FINISHING) {
3748 return;
3749 }
3750 mContainerState = CONTAINER_STATE_FINISHING;
3751
Craig Mautnerd163e752014-06-13 17:18:47 -07003752 long origId = Binder.clearCallingIdentity();
3753 try {
Craig Mautneree36c772014-07-16 14:56:05 -07003754 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003755 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07003756 } finally {
3757 Binder.restoreCallingIdentity(origId);
3758 }
3759 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003760 }
3761
Craig Mautner60257702014-09-17 15:02:33 -07003762 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003763 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08003764 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08003765 if (mActivityDisplay != null) {
3766 mActivityDisplay.detachActivitiesLocked(mStack);
3767 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003768 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003769 }
3770 }
3771
3772 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08003773 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003774 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003775 }
3776
3777 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07003778 public final int startActivityIntentSender(IIntentSender intentSender)
3779 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003780 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3781
3782 if (!(intentSender instanceof PendingIntentRecord)) {
3783 throw new IllegalArgumentException("Bad PendingIntent object");
3784 }
3785
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07003786 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003787 Binder.getCallingUid(), mCurrentUser, false,
3788 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08003789
3790 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3791 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
3792 pendingIntent.key.requestResolvedType);
3793
3794 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
3795 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
3796 }
3797
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003798 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07003799 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003800 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003801 throw new SecurityException(
3802 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3803 }
3804 }
3805
Craig Mautnerdf88d732014-01-27 09:21:32 -08003806 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003807 public IBinder asBinder() {
3808 return this;
3809 }
3810
Craig Mautner4504de52013-12-20 09:06:56 -08003811 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003812 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003813 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08003814 }
3815
Craig Mautner4a1cb222013-12-04 16:14:06 -08003816 ActivityStackSupervisor getOuter() {
3817 return ActivityStackSupervisor.this;
3818 }
3819
Craig Mautnerd163e752014-06-13 17:18:47 -07003820 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003821 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003822 }
3823
Craig Mautner6985bad2014-04-21 15:22:06 -07003824 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07003825 void setVisible(boolean visible) {
3826 if (mVisible != visible) {
3827 mVisible = visible;
3828 if (mCallback != null) {
3829 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
3830 0 /* unused */, this).sendToTarget();
3831 }
3832 }
3833 }
3834
Craig Mautner6985bad2014-04-21 15:22:06 -07003835 void setDrawn() {
3836 }
3837
Craig Mautner1b4bf852014-05-26 15:06:32 -07003838 // You can always start a new task on a regular ActivityStack.
3839 boolean isEligibleForNewTasks() {
3840 return true;
3841 }
3842
Craig Mautnerd163e752014-06-13 17:18:47 -07003843 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07003844 detachLocked();
3845 deleteActivityContainer(this);
3846 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003847 }
3848
Craig Mautner34b73df2014-01-12 21:11:08 -08003849 @Override
3850 public String toString() {
3851 return mIdString + (mActivityDisplay == null ? "N" : "A");
3852 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003853 }
3854
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003855 private class VirtualActivityContainer extends ActivityContainer {
3856 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07003857 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003858
3859 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
3860 super(getNextStackId());
3861 mParentActivity = parent;
3862 mCallback = callback;
3863 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07003864 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003865 }
3866
3867 @Override
3868 public void setSurface(Surface surface, int width, int height, int density) {
3869 super.setSurface(surface, width, height, density);
3870
3871 synchronized (mService) {
3872 final long origId = Binder.clearCallingIdentity();
3873 try {
3874 setSurfaceLocked(surface, width, height, density);
3875 } finally {
3876 Binder.restoreCallingIdentity(origId);
3877 }
3878 }
3879 }
3880
3881 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
3882 if (mContainerState == CONTAINER_STATE_FINISHING) {
3883 return;
3884 }
3885 VirtualActivityDisplay virtualActivityDisplay =
3886 (VirtualActivityDisplay) mActivityDisplay;
3887 if (virtualActivityDisplay == null) {
3888 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07003889 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003890 mActivityDisplay = virtualActivityDisplay;
3891 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003892 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003893 }
3894
3895 if (mSurface != null) {
3896 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003897 }
3898
Craig Mautner6985bad2014-04-21 15:22:06 -07003899 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003900 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003901 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003902 } else {
3903 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07003904 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003905 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003906 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003907 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003908 }
Craig Mautner6985bad2014-04-21 15:22:06 -07003909
Craig Mautnerd163e752014-06-13 17:18:47 -07003910 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003911
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003912 if (DEBUG_STACK) Slog.d(TAG_STACK,
3913 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07003914 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003915
Craig Mautner6985bad2014-04-21 15:22:06 -07003916 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07003917 boolean isAttachedLocked() {
3918 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07003919 }
3920
3921 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07003922 void setDrawn() {
3923 synchronized (mService) {
3924 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07003925 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003926 }
3927 }
3928
Craig Mautner1b4bf852014-05-26 15:06:32 -07003929 // Never start a new task on an ActivityView if it isn't explicitly specified.
3930 @Override
3931 boolean isEligibleForNewTasks() {
3932 return false;
3933 }
3934
Craig Mautnerd163e752014-06-13 17:18:47 -07003935 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003936 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07003937 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
3938 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
3939 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
3940 mContainerState = CONTAINER_STATE_HAS_SURFACE;
3941 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003942 }
3943 }
3944
Craig Mautner4a1cb222013-12-04 16:14:06 -08003945 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
3946 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003947 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003948 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08003949 int mDisplayId;
3950 Display mDisplay;
3951 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08003952
Craig Mautner4a1cb222013-12-04 16:14:06 -08003953 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
3954 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08003955 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003956
Jose Lima4b6c6692014-08-12 17:41:12 -07003957 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003958
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003959 ActivityDisplay() {
3960 }
Craig Mautner4504de52013-12-20 09:06:56 -08003961
Craig Mautner1a70a162014-09-13 12:09:31 -07003962 // After instantiation, check that mDisplay is not null before using this. The alternative
3963 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08003964 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07003965 final Display display = mDisplayManager.getDisplay(displayId);
3966 if (display == null) {
3967 return;
3968 }
3969 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08003970 }
3971
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003972 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08003973 mDisplay = display;
3974 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003975 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08003976 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003977
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003978 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003979 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003980 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
3981 + " onTop=" + onTop);
3982 if (onTop) {
3983 mStacks.add(stack);
3984 } else {
3985 mStacks.add(0, stack);
3986 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003987 }
3988
Craig Mautnere0a38842013-12-16 16:14:02 -08003989 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003990 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08003991 + " from displayId=" + mDisplayId);
3992 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003993 }
3994
Jose Lima4b6c6692014-08-12 17:41:12 -07003995 void setVisibleBehindActivity(ActivityRecord r) {
3996 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003997 }
3998
Jose Lima4b6c6692014-08-12 17:41:12 -07003999 boolean hasVisibleBehindActivity() {
4000 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004001 }
4002
Craig Mautner34b73df2014-01-12 21:11:08 -08004003 @Override
4004 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004005 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4006 }
4007 }
4008
4009 class VirtualActivityDisplay extends ActivityDisplay {
4010 VirtualDisplay mVirtualDisplay;
4011
Craig Mautner6985bad2014-04-21 15:22:06 -07004012 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004013 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004014 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4015 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4016 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4017 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004018
4019 init(mVirtualDisplay.getDisplay());
4020
4021 mWindowManager.handleDisplayAdded(mDisplayId);
4022 }
4023
4024 void setSurface(Surface surface) {
4025 if (mVirtualDisplay != null) {
4026 mVirtualDisplay.setSurface(surface);
4027 }
4028 }
4029
4030 @Override
4031 void detachActivitiesLocked(ActivityStack stack) {
4032 super.detachActivitiesLocked(stack);
4033 if (mVirtualDisplay != null) {
4034 mVirtualDisplay.release();
4035 mVirtualDisplay = null;
4036 }
4037 }
4038
4039 @Override
4040 public String toString() {
4041 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004042 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004043 }
Jose Lima58e66d62014-05-27 20:00:27 -07004044
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07004045 /**
4046 * Adjust bounds to stay within stack bounds.
4047 *
4048 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
4049 * that keep them unchanged, but be contained within the stack bounds.
4050 *
4051 * @param bounds Bounds to be adjusted.
4052 * @param stackBounds Bounds within which the other bounds should remain.
4053 */
4054 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
4055 if (stackBounds == null || stackBounds.contains(bounds)) {
4056 return;
4057 }
4058
4059 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
4060 final int maxRight = stackBounds.right
4061 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
4062 int horizontalDiff = stackBounds.left - bounds.left;
4063 if ((horizontalDiff < 0 && bounds.left >= maxRight)
4064 || (bounds.left + horizontalDiff >= maxRight)) {
4065 horizontalDiff = maxRight - bounds.left;
4066 }
4067 bounds.left += horizontalDiff;
4068 bounds.right += horizontalDiff;
4069 }
4070
4071 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
4072 final int maxBottom = stackBounds.bottom
4073 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
4074 int verticalDiff = stackBounds.top - bounds.top;
4075 if ((verticalDiff < 0 && bounds.top >= maxBottom)
4076 || (bounds.top + verticalDiff >= maxBottom)) {
4077 verticalDiff = maxBottom - bounds.top;
4078 }
4079 bounds.top += verticalDiff;
4080 bounds.bottom += verticalDiff;
4081 }
4082 }
4083
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004084 ActivityStack findStackBehind(ActivityStack stack) {
4085 // TODO(multi-display): We are only looking for stacks on the default display.
4086 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
4087 if (display == null) {
4088 return null;
4089 }
4090 final ArrayList<ActivityStack> stacks = display.mStacks;
4091 for (int i = stacks.size() - 1; i >= 0; i--) {
4092 if (stacks.get(i) == stack && i > 0) {
4093 return stacks.get(i - 1);
4094 }
4095 }
4096 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4097 + " in=" + stacks);
4098 }
Craig Mautner27084302013-03-25 08:05:25 -07004099}