blob: 043a2e779428fc63c6d6774aa1db91842ca45b42 [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
Craig Mautner6170f732013-04-02 13:05:23 -070019import static android.Manifest.permission.START_ANY_ACTIVITY;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080020import static android.app.ActivityManager.LOCK_TASK_MODE_LOCKED;
21import static android.app.ActivityManager.LOCK_TASK_MODE_NONE;
22import static android.app.ActivityManager.LOCK_TASK_MODE_PINNED;
23import static android.app.ActivityManager.RESIZE_MODE_FORCED;
24import static android.app.ActivityManager.RESIZE_MODE_SYSTEM;
Wale Ogunwale3797c222015-10-27 14:21:58 -070025import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
26import static android.app.ActivityManager.StackId.FIRST_DYNAMIC_STACK_ID;
27import static android.app.ActivityManager.StackId.FIRST_STATIC_STACK_ID;
28import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
29import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
30import static android.app.ActivityManager.StackId.HOME_STACK_ID;
31import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
32import static android.app.ActivityManager.StackId.LAST_STATIC_STACK_ID;
33import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Filip Gruszczynskidc394902015-12-14 10:20:22 -080034import static android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
Craig Mautner29219d92013-04-16 20:19:12 -070035import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -070036import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Craig Mautner6170f732013-04-02 13:05:23 -070037import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Wale Ogunwalecad05a02015-09-25 10:41:44 -070038import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080039import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080040import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONTAINERS;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080041import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_IDLE;
42import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKSCREEN;
43import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKTASK;
44import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080045import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
46import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080047import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
48import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
49import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
50import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080051import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBLE_BEHIND;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080052import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONTAINERS;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080053import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
54import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_LOCKTASK;
55import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
56import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
57import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080058import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
59import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
60import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
61import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080062import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBLE_BEHIND;
63import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
64import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
Craig Mautner05d29032013-05-03 13:40:13 -070065import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080066import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner84984fa2014-06-19 11:19:20 -070067import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
68import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080069import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
70import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
71import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
72import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
73import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
74import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
75import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
76import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
Craig Mautner15df08a2015-04-01 12:17:18 -070077import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
78import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
Benjamin Franz469dd582015-06-09 14:24:36 +010079import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
Craig Mautner15df08a2015-04-01 12:17:18 -070080import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
81import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Craig Mautner8d341ef2013-03-26 09:03:27 -070082
Svetoslav7008b512015-06-24 18:47:07 -070083import android.Manifest;
Craig Mautner2420ead2013-04-01 17:13:20 -070084import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070085import android.app.ActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080086import android.app.ActivityManager.RunningTaskInfo;
Wale Ogunwale3797c222015-10-27 14:21:58 -070087import android.app.ActivityManager.StackId;
Craig Mautnered6649f2013-12-02 14:08:25 -080088import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070089import android.app.ActivityOptions;
90import android.app.AppGlobals;
Svetoslav7008b512015-06-24 18:47:07 -070091import android.app.AppOpsManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -080092import android.app.IActivityContainer;
93import android.app.IActivityContainerCallback;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070094import android.app.IActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080095import android.app.IActivityManager.WaitResult;
Jeff Hao1b012d32014-08-20 10:35:34 -070096import android.app.ProfilerInfo;
Craig Mautner2420ead2013-04-01 17:13:20 -070097import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -040098import android.app.StatusBarManager;
Jason Monk35c62a42014-06-17 10:24:47 -040099import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700100import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700101import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700102import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700103import android.content.Intent;
104import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700105import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -0700106import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700107import android.content.pm.PackageManager;
108import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100109import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700110import android.content.res.Configuration;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800111import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800112import android.hardware.display.DisplayManager;
113import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -0800114import android.hardware.display.DisplayManagerGlobal;
115import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -0800116import android.hardware.input.InputManager;
117import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700118import android.os.Binder;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700119import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700120import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700121import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700122import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -0700123import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700124import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700125import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700126import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700127import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -0400128import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700129import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -0700130import android.os.Trace;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -0700131import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -0700132import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100133import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700134import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -0700135import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -0400136import android.provider.Settings;
137import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700138import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -0700139import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700140import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -0700141import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700142import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800143import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800144import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800145import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800146import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -0800147import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800148import android.view.Surface;
Chong Zhangb15758a2015-11-17 12:12:03 -0800149
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800150import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700151import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400152import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -0700153import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -0400154import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800155import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700156import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700157import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700158
Craig Mautner8d341ef2013-03-26 09:03:27 -0700159import java.io.FileDescriptor;
160import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700161import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700162import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700163import java.util.Arrays;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700164import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700165import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700166
Craig Mautner4a1cb222013-12-04 16:14:06 -0800167public final class ActivityStackSupervisor implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800168 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700169 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700170 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700171 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700172 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700173 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700174 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700175 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700176 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700177 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800178 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700179 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800180
Craig Mautnerf3333272013-04-22 10:55:53 -0700181 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700182 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700183
Craig Mautner0eea92c2013-05-16 13:35:39 -0700184 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700185 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700186
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700187 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700188 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700189
Craig Mautner05d29032013-05-03 13:40:13 -0700190 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
191 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
192 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700193 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700194 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800195 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
196 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
197 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700198 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400199 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
200 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700201 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700202 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700203 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800204
Wale Ogunwale040b4702015-08-06 18:10:50 -0700205 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700206
Jason Monk62515be2014-05-21 16:06:19 -0400207 private static final String LOCK_TASK_TAG = "Lock-to-App";
208
Wale Ogunwale040b4702015-08-06 18:10:50 -0700209 // Used to indicate if an object (e.g. stack) that we are trying to get
210 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800211 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700212
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700213 // Used to indicate that windows of activities should be preserved during the resize.
214 static final boolean PRESERVE_WINDOWS = true;
215
Wale Ogunwale040b4702015-08-06 18:10:50 -0700216 // Used to indicate if an object (e.g. task) should be moved/created
217 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700218 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700219
220 // Used to indicate that an objects (e.g. task) removal from its container
221 // (e.g. stack) is due to it moving to another container.
222 static final boolean MOVING = true;
223
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700224 // Force the focus to change to the stack we are moving a task to..
225 static final boolean FORCE_FOCUS = true;
226
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700227 // Restore task from the saved recents if it can't be found in any live stack.
228 static final boolean RESTORE_FROM_RECENTS = true;
229
Svetoslav7008b512015-06-24 18:47:07 -0700230 // Activity actions an app cannot start if it uses a permission which is not granted.
231 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
232 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700233
Svetoslav7008b512015-06-24 18:47:07 -0700234 static {
235 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
236 Manifest.permission.CAMERA);
237 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
238 Manifest.permission.CAMERA);
239 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
240 Manifest.permission.CALL_PHONE);
241 }
242
Svet Ganov99b60432015-06-27 13:15:22 -0700243 /** Action restriction: launching the activity is not restricted. */
244 private static final int ACTIVITY_RESTRICTION_NONE = 0;
245 /** Action restriction: launching the activity is restricted by a permission. */
246 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
247 /** Action restriction: launching the activity is restricted by an app op. */
248 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700249
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700250 // The height/width divide used when fitting a task within a bounds with method
251 // {@link #fitWithinBounds}.
252 // We always want the task to to be visible in the bounds without affecting its size when
253 // fitting. To make sure this is the case, we don't adjust the task left or top side pass
254 // the input bounds right or bottom side minus the width or height divided by this value.
255 private static final int FIT_WITHIN_BOUNDS_DIVIDER = 3;
256
justinzhang5286d3f2014-05-12 17:06:01 -0400257 /** Status Bar Service **/
258 private IBinder mToken = new Binder();
259 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400260 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400261
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700262 // For debugging to make sure the caller when acquiring/releasing our
263 // wake lock is the system process.
264 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700265
Craig Mautner27084302013-03-25 08:05:25 -0700266 final ActivityManagerService mService;
267
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800268 private final RecentTasks mRecentTasks;
269
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700270 final ActivityStackSupervisorHandler mHandler;
271
272 /** Short cut */
273 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800274 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700275
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700276 /** Counter for next free stack ID to use for dynamic activity stacks. */
277 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700278
279 /** Task identifier that activities are currently being started in. Incremented each time a
280 * new task is created. */
281 private int mCurTaskId = 0;
282
Craig Mautner2420ead2013-04-01 17:13:20 -0700283 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800284 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700285
Craig Mautnere0a38842013-12-16 16:14:02 -0800286 /** The stack containing the launcher app. Assumed to always be attached to
287 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800288 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700289
Craig Mautnere0a38842013-12-16 16:14:02 -0800290 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800291 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700292
Craig Mautner4a1cb222013-12-04 16:14:06 -0800293 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
294 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800295 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800296 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700297
298 /** List of activities that are waiting for a new activity to become visible before completing
299 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800300 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700301
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700302 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800303 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700304
305 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800306 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700307
Craig Mautnerde4ef022013-04-07 19:01:33 -0700308 /** List of activities that are ready to be stopped, but waiting for the next activity to
309 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800310 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700311
Craig Mautnerf3333272013-04-22 10:55:53 -0700312 /** List of activities that are ready to be finished, but waiting for the previous activity to
313 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800314 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700315
Craig Mautner0eea92c2013-05-16 13:35:39 -0700316 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800317 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700318
Craig Mautnerf3333272013-04-22 10:55:53 -0700319 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700320 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700321
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700322 /** Used to queue up any background users being started */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700323 final ArrayList<UserState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700324
Craig Mautnerde4ef022013-04-07 19:01:33 -0700325 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
326 * is being brought in front of us. */
327 boolean mUserLeaving = false;
328
Craig Mautner0eea92c2013-05-16 13:35:39 -0700329 /** Set when we have taken too long waiting to go to sleep. */
330 boolean mSleepTimeout = false;
331
332 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700333 * We don't want to allow the device to go to sleep while in the process
334 * of launching an activity. This is primarily to allow alarm intent
335 * receivers to launch an activity and get that to run before the device
336 * goes back to sleep.
337 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700338 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700339
340 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700341 * Set when the system is going to sleep, until we have
342 * successfully paused the current activity and released our wake lock.
343 * At that point the system is allowed to actually sleep.
344 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700345 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700346
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700347 /** Stack id of the front stack when user switched, indexed by userId. */
348 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700349
Craig Mautner4504de52013-12-20 09:06:56 -0800350 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800351 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700352 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800353
354 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700355 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800356
Jeff Brownca9bc702014-02-11 14:32:56 -0800357 InputManagerInternal mInputManagerInternal;
358
Craig Mautner15df08a2015-04-01 12:17:18 -0700359 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
360 * may be finished until there is only one entry left. If this is empty the system is not
361 * in lockTask mode. */
362 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000363 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700364 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
365 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000366 */
367 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400368 /**
369 * Notifies the user when entering/exiting lock-task.
370 */
371 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800372
Wale Ogunwaled046a012015-12-24 13:05:59 -0800373 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800374 boolean inResumeTopActivity;
375
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700376 // 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 -0700377 private final Rect tempRect = new Rect();
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700378 private final Rect tempRect2 = new Rect();
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700379
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700380 private final SparseArray<Configuration> mTmpConfigs = new SparseArray<>();
381 private final SparseArray<Rect> mTmpBounds = new SparseArray<>();
382
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700383 // The default minimal size that will be used if the activity doesn't specify its minimal size.
384 // It will be calculated when the default display gets added.
385 private int mDefaultMinimalSizeOfResizeableTask = -1;
386
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700387 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
388 private boolean mTaskLayersChanged = true;
389
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800390 private final ActivityMetricsLogger mActivityMetricsLogger;
391
Wale Ogunwale39381972015-12-17 17:15:29 -0800392 static class FindTaskResult {
393 ActivityRecord r;
394 boolean matchedByRootAffinity;
395 }
396 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
397
Craig Mautneree36c772014-07-16 14:56:05 -0700398 /**
399 * Description of a request to start a new activity, which has been held
400 * due to app switches being disabled.
401 */
402 static class PendingActivityLaunch {
403 final ActivityRecord r;
404 final ActivityRecord sourceRecord;
405 final int startFlags;
406 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700407 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700408
409 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700410 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700411 r = _r;
412 sourceRecord = _sourceRecord;
413 startFlags = _startFlags;
414 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700415 callerApp = _callerApp;
416 }
417
418 void sendErrorResult(String message) {
419 try {
420 if (callerApp.thread != null) {
421 callerApp.thread.scheduleCrash(message);
422 }
423 } catch (RemoteException e) {
424 Slog.e(TAG, "Exception scheduling crash of failed "
425 + "activity launcher sourceRecord=" + sourceRecord, e);
426 }
Craig Mautneree36c772014-07-16 14:56:05 -0700427 }
428 }
429
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800430 public ActivityStackSupervisor(ActivityManagerService service, RecentTasks recentTasks) {
Craig Mautner27084302013-03-25 08:05:25 -0700431 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800432 mRecentTasks = recentTasks;
Jeff Brown2c43c332014-06-12 22:38:59 -0700433 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800434 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jeff Brown2c43c332014-06-12 22:38:59 -0700435 }
436
437 /**
438 * At the time when the constructor runs, the power manager has not yet been
439 * initialized. So we initialize our wakelocks afterwards.
440 */
441 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800442 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700443 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700444 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700445 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700446 }
447
justinzhang5286d3f2014-05-12 17:06:01 -0400448 // This function returns a IStatusBarService. The value is from ServiceManager.
449 // getService and is cached.
450 private IStatusBarService getStatusBarService() {
451 synchronized (mService) {
452 if (mStatusBarService == null) {
453 mStatusBarService = IStatusBarService.Stub.asInterface(
454 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
455 if (mStatusBarService == null) {
456 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
457 }
458 }
459 return mStatusBarService;
460 }
461 }
462
Jason Monk35c62a42014-06-17 10:24:47 -0400463 private IDevicePolicyManager getDevicePolicyManager() {
464 synchronized (mService) {
465 if (mDevicePolicyManager == null) {
466 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
467 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
468 if (mDevicePolicyManager == null) {
469 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
470 }
471 }
472 return mDevicePolicyManager;
473 }
474 }
475
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700476 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800477 synchronized (mService) {
478 mWindowManager = wm;
479
480 mDisplayManager =
481 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
482 mDisplayManager.registerDisplayListener(this, null);
483
484 Display[] displays = mDisplayManager.getDisplays();
485 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
486 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800487 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700488 if (activityDisplay.mDisplay == null) {
489 throw new IllegalStateException("Default Display does not exist");
490 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800491 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700492 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800493 }
494
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800495 mHomeStack = mFocusedStack = mLastFocusedStack =
496 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800497
498 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800499 }
Craig Mautner27084302013-03-25 08:05:25 -0700500 }
501
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200502 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700503 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200504 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700505 }
506
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700507 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800508 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700509 }
510
Craig Mautnerde4ef022013-04-07 19:01:33 -0700511 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800512 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700513 }
514
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700515 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700516 if (stack == null) {
517 return false;
518 }
519
Craig Mautnerdf88d732014-01-27 09:21:32 -0800520 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
521 if (parent != null) {
522 stack = parent.task.stack;
523 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800524 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700525 }
526
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700527 /** The top most stack. */
528 boolean isFrontStack(ActivityStack stack) {
529 if (stack == null) {
530 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800531 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700532
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700533 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
534 if (parent != null) {
535 stack = parent.task.stack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800536 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700537 return stack == mHomeStack.mStacks.get((mHomeStack.mStacks.size() - 1));
538 }
539
Wale Ogunwaled046a012015-12-24 13:05:59 -0800540 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800541 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
542 if (!focusCandidate.isFocusable()) {
543 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
544 focusCandidate = focusCandidate.getNextFocusableStackLocked();
545 }
546
547 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800548 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800549 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800550
Wale Ogunwaled046a012015-12-24 13:05:59 -0800551 EventLogTags.writeAmFocusedStack(
552 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
553 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
554 }
555
556 final ActivityRecord r = topRunningActivityLocked();
557 if (mService.mFocusedActivity != r) {
558 // The focus activity should always be the top activity in the focused stack.
559 // There will be chaos and anarchy if it isn't...
560 mService.setFocusedActivityLocked(r, reason + " setFocusStack");
561 }
Craig Mautnerde313752015-01-22 14:28:03 -0800562
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800563 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800564 if (r != null && r.idle) {
565 checkFinishBootingLocked();
566 }
567 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700568 }
569
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700570 void moveHomeStackToFront(String reason) {
571 mHomeStack.moveToFront(reason);
572 }
573
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700574 /** Returns true if the focus activity was adjusted to the home stack top activity. */
575 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700576 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
577 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700578 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700579 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700580
Craig Mautner84984fa2014-06-19 11:19:20 -0700581 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700582
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700583 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700584 if (top == null) {
585 return false;
586 }
587 mService.setFocusedActivityLocked(top, reason);
588 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700589 }
590
Craig Mautner299f9602015-01-26 09:47:33 -0800591 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700592 if (!mService.mBooting && !mService.mBooted) {
593 // Not ready yet!
594 return false;
595 }
596
Craig Mautner84984fa2014-06-19 11:19:20 -0700597 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
598 mWindowManager.showRecentApps();
599 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700600 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700601
Craig Mautner84984fa2014-06-19 11:19:20 -0700602 if (prev != null) {
603 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
604 }
605
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700606 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
607 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800608 final String myReason = reason + " resumeHomeStackTask";
609
Mark Lua56ea122015-10-08 13:31:01 +0800610 // Only resume home activity if isn't finishing.
611 if (r != null && !r.finishing) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800612 mService.setFocusedActivityLocked(r, myReason);
613 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700614 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800615 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700616 }
617
Craig Mautner8d341ef2013-03-26 09:03:27 -0700618 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700619 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700620 }
621
622 /**
623 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
624 * @param id Id of the task we would like returned.
625 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
626 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700627 * @param stackId The stack to restore the task to (default launch stack will be used if
628 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700629 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700630 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800631 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800632 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800633 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800634 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
635 ActivityStack stack = stacks.get(stackNdx);
636 TaskRecord task = stack.taskForIdLocked(id);
637 if (task != null) {
638 return task;
639 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700640 }
641 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800642
643 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700644 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800645 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800646 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700647 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800648 return null;
649 }
650
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700651 if (!restoreFromRecents) {
652 return task;
653 }
654
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700655 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700656 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
657 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800658 return null;
659 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700660 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800661 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700662 }
663
Craig Mautner6170f732013-04-02 13:05:23 -0700664 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800665 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800666 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800667 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800668 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
669 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
670 if (r != null) {
671 return r;
672 }
Craig Mautner6170f732013-04-02 13:05:23 -0700673 }
674 }
675 return null;
676 }
677
Craig Mautneref73ee12014-04-23 11:45:37 -0700678 void setNextTaskId(int taskId) {
679 if (taskId > mCurTaskId) {
680 mCurTaskId = taskId;
681 }
682 }
683
Craig Mautner8d341ef2013-03-26 09:03:27 -0700684 int getNextTaskId() {
685 do {
686 mCurTaskId++;
687 if (mCurTaskId <= 0) {
688 mCurTaskId = 1;
689 }
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700690 } while (anyTaskForIdLocked(mCurTaskId, !RESTORE_FROM_RECENTS, INVALID_STACK_ID) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700691 return mCurTaskId;
692 }
693
Craig Mautnerde4ef022013-04-07 19:01:33 -0700694 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800695 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700696 if (stack == null) {
697 return null;
698 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700699 ActivityRecord resumedActivity = stack.mResumedActivity;
700 if (resumedActivity == null || resumedActivity.app == null) {
701 resumedActivity = stack.mPausingActivity;
702 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700703 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700704 }
705 }
706 return resumedActivity;
707 }
708
Dianne Hackbornff072722014-09-24 10:56:28 -0700709 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700710 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800711 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800712 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
713 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800714 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
715 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700716 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800717 continue;
718 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700719 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800720 if (hr != null) {
721 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
722 && processName.equals(hr.processName)) {
723 try {
George Mount2c92c972014-03-20 09:38:23 -0700724 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800725 didSomething = true;
726 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700727 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800728 Slog.w(TAG, "Exception in new application when starting activity "
729 + hr.intent.getComponent().flattenToShortString(), e);
730 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700731 }
Craig Mautner20e72272013-04-01 13:45:53 -0700732 }
Craig Mautner20e72272013-04-01 13:45:53 -0700733 }
734 }
735 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700736 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700737 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700738 }
Craig Mautner20e72272013-04-01 13:45:53 -0700739 return didSomething;
740 }
741
742 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800743 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
744 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800745 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
746 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700747 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800748 continue;
749 }
750 final ActivityRecord resumedActivity = stack.mResumedActivity;
751 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700752 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800753 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800754 return false;
755 }
Craig Mautner20e72272013-04-01 13:45:53 -0700756 }
757 }
758 return true;
759 }
760
Craig Mautnerde4ef022013-04-07 19:01:33 -0700761 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800762 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
763 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800764 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
765 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700766 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800767 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700768 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800769 return false;
770 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700771 }
772 }
773 }
774 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700775 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800776 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
777 mLastFocusedStack + " to=" + mFocusedStack);
778 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700779 return true;
780 }
781
782 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800783 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800784 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
785 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800786 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
787 final ActivityStack stack = stacks.get(stackNdx);
788 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800789 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700790 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800791 return false;
792 }
793 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800794 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700795 }
796 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800797 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700798 }
799
Craig Mautner2acc3892013-09-23 10:28:14 -0700800 /**
801 * Pause all activities in either all of the stacks or just the back stacks.
802 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700803 * @return true if any activity was paused as a result of this call.
804 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700805 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700806 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800807 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
808 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800809 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
810 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700811 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700812 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800813 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700814 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
815 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800816 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700817 }
818 }
819 return someActivityPaused;
820 }
821
Craig Mautnerde4ef022013-04-07 19:01:33 -0700822 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700823 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800824 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
825 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800826 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
827 final ActivityStack stack = stacks.get(stackNdx);
828 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700829 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800830 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700831 Slog.d(TAG_STATES,
832 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800833 pausing = false;
834 } else {
835 return false;
836 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700837 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700838 }
839 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700840 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700841 }
842
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700843 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
844 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500845 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800846 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
847 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
848 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
849 final ActivityStack stack = stacks.get(stackNdx);
850 if (stack.mResumedActivity != null &&
851 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700852 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800853 }
854 }
855 }
856 }
857
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700858 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700859 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700860 }
861
862 void sendWaitingVisibleReportLocked(ActivityRecord r) {
863 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700864 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700865 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700866 if (w.who == null) {
867 changed = true;
868 w.timeout = false;
869 if (r != null) {
870 w.who = new ComponentName(r.info.packageName, r.info.name);
871 }
872 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
873 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700874 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700875 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700876 if (changed) {
877 mService.notifyAll();
878 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700879 }
880
881 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
882 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700883 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700884 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700885 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700886 if (w.who == null) {
887 changed = true;
888 w.timeout = timeout;
889 if (r != null) {
890 w.who = new ComponentName(r.info.packageName, r.info.name);
891 }
892 w.thisTime = thisTime;
893 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700894 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700895 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700896 if (changed) {
897 mService.notifyAll();
898 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700899 }
900
Craig Mautner29219d92013-04-16 20:19:12 -0700901 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800902 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700903 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -0700904 if (r != null) {
905 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700906 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700907
Craig Mautner4a1cb222013-12-04 16:14:06 -0800908 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800909 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800910 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
911 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800912 if (stack != focusedStack && isFrontStack(stack) && stack.isFocusable()) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700913 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -0700914 if (r != null) {
915 return r;
916 }
917 }
918 }
919 return null;
920 }
921
Dianne Hackborn09233282014-04-30 11:33:59 -0700922 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700923 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800924 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
925 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800926 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800927 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800928 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800929 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
930 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700931 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800932 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700933 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700934 }
935 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700936
937 // The lists are already sorted from most recent to oldest. Just pull the most recent off
938 // each list and add it to list. Stop when all lists are empty or maxNum reached.
939 while (maxNum > 0) {
940 long mostRecentActiveTime = Long.MIN_VALUE;
941 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800942 final int numTaskLists = runningTaskLists.size();
943 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
944 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700945 if (!stackTaskList.isEmpty()) {
946 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
947 if (lastActiveTime > mostRecentActiveTime) {
948 mostRecentActiveTime = lastActiveTime;
949 selectedStackList = stackTaskList;
950 }
951 }
952 }
953 if (selectedStackList != null) {
954 list.add(selectedStackList.remove(0));
955 --maxNum;
956 } else {
957 break;
958 }
959 }
Craig Mautner20e72272013-04-01 13:45:53 -0700960 }
961
Todd Kennedy7440f172015-12-09 14:31:22 -0800962 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
963 ProfilerInfo profilerInfo) {
964 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700965 if (aInfo != null) {
966 // Store the found target back into the intent, because now that
967 // we have it we never want to do this again. For example, if the
968 // user navigates back to this point in the history, we should
969 // always restart the exact same activity.
970 intent.setComponent(new ComponentName(
971 aInfo.applicationInfo.packageName, aInfo.name));
972
973 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -0700974 if (!aInfo.processName.equals("system")) {
975 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700976 mService.setDebugApp(aInfo.processName, true, false);
977 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700978
Man Caocfa78b22015-06-11 20:14:34 -0700979 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
980 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
981 }
982
983 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700984 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700985 }
986 }
987 }
988 return aInfo;
989 }
990
Todd Kennedy7440f172015-12-09 14:31:22 -0800991 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
992 try {
993 return AppGlobals.getPackageManager().resolveIntent(intent, resolvedType,
994 PackageManager.MATCH_DEFAULT_ONLY
995 | ActivityManagerService.STOCK_PM_FLAGS, userId);
996 } catch (RemoteException e) {
997 }
998 return null;
999 }
1000
1001 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1002 ProfilerInfo profilerInfo, int userId) {
1003 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1004 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1005 }
1006
Craig Mautner2420ead2013-04-01 17:13:20 -07001007 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001008 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001009 throws RemoteException {
1010
Craig Mautner2568c3a2015-03-26 14:22:34 -07001011 if (andResume) {
1012 r.startFreezingScreenLocked(app, 0);
1013 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001014
Craig Mautner2568c3a2015-03-26 14:22:34 -07001015 // schedule launch ticks to collect information about slow apps.
1016 r.startLaunchTickingLocked();
1017 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001018
1019 // Have the window manager re-evaluate the orientation of
1020 // the screen based on the new activity order. Note that
1021 // as a result of this, it can call back into the activity
1022 // manager with a new orientation. We don't care about that,
1023 // because the activity is not currently running so we are
1024 // just restarting it anyway.
1025 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001026 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001027 mService.mConfiguration,
1028 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001029 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001030 }
1031
1032 r.app = app;
1033 app.waitingToKill = null;
1034 r.launchCount++;
1035 r.lastLaunchTime = SystemClock.uptimeMillis();
1036
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001037 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001038
1039 int idx = app.activities.indexOf(r);
1040 if (idx < 0) {
1041 app.activities.add(r);
1042 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001043 mService.updateLruProcessLocked(app, true, null);
1044 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001045
Craig Mautner15df08a2015-04-01 12:17:18 -07001046 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001047 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1048 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001049 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001050 }
1051
1052 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001053 try {
1054 if (app.thread == null) {
1055 throw new RemoteException();
1056 }
1057 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001058 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001059 if (andResume) {
1060 results = r.results;
1061 newIntents = r.newIntents;
1062 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001063 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1064 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1065 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001066 if (andResume) {
1067 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1068 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001069 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001070 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001071 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001072 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001073 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001074 }
Nicolas Geoffray27c07372015-11-05 16:54:09 +00001075 mService.notifyPackageUse(r.intent.getComponent().getPackageName());
Craig Mautner2420ead2013-04-01 17:13:20 -07001076 r.sleeping = false;
1077 r.forceNewConfig = false;
1078 mService.showAskCompatModeDialogLocked(r);
1079 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001080 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001081 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1082 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1083 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001084 final String profileFile = mService.mProfileFile;
1085 if (profileFile != null) {
1086 ParcelFileDescriptor profileFd = mService.mProfileFd;
1087 if (profileFd != null) {
1088 try {
1089 profileFd = profileFd.dup();
1090 } catch (IOException e) {
1091 if (profileFd != null) {
1092 try {
1093 profileFd.close();
1094 } catch (IOException o) {
1095 }
1096 profileFd = null;
1097 }
1098 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001099 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001100
1101 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1102 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001103 }
1104 }
1105 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001106
Craig Mautner2568c3a2015-03-26 14:22:34 -07001107 if (andResume) {
1108 app.hasShownUi = true;
1109 app.pendingUiClean = true;
1110 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001111 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001112 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001113 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001114 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001115 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001116 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001117
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001118 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001119 // This may be a heavy-weight process! Note that the package
1120 // manager will ensure that only activity can run in the main
1121 // process of the .apk, which is the only thing that will be
1122 // considered heavy-weight.
1123 if (app.processName.equals(app.info.packageName)) {
1124 if (mService.mHeavyWeightProcess != null
1125 && mService.mHeavyWeightProcess != app) {
1126 Slog.w(TAG, "Starting new heavy weight process " + app
1127 + " when already running "
1128 + mService.mHeavyWeightProcess);
1129 }
1130 mService.mHeavyWeightProcess = app;
1131 Message msg = mService.mHandler.obtainMessage(
1132 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1133 msg.obj = r;
1134 mService.mHandler.sendMessage(msg);
1135 }
1136 }
1137
1138 } catch (RemoteException e) {
1139 if (r.launchFailed) {
1140 // This is the second time we failed -- finish activity
1141 // and give up.
1142 Slog.e(TAG, "Second failure launching "
1143 + r.intent.getComponent().flattenToShortString()
1144 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001145 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001146 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1147 "2nd-crash", false);
1148 return false;
1149 }
1150
1151 // This is the first time we failed -- restart process and
1152 // retry.
1153 app.activities.remove(r);
1154 throw e;
1155 }
1156
1157 r.launchFailed = false;
1158 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001159 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001160 }
1161
1162 if (andResume) {
1163 // As part of the process of launching, ActivityThread also performs
1164 // a resume.
1165 stack.minimalResumeActivityLocked(r);
1166 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001167 // This activity is not starting in the resumed state... which should look like we asked
1168 // it to pause+stop (but remain visible), and it has done so and reported back the
1169 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001170 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001171 "Moving to PAUSED: " + r + " (starting in paused state)");
1172 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001173 }
1174
1175 // Launch the new version setup screen if needed. We do this -after-
1176 // launching the initial activity (that is, home), so that it can have
1177 // a chance to initialize itself while in the background, making the
1178 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001179 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001180 mService.startSetupActivityLocked();
1181 }
1182
Dianne Hackborn465fa392014-09-14 14:21:18 -07001183 // Update any services we are bound to that might care about whether
1184 // their client may have activities.
1185 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1186
Craig Mautner2420ead2013-04-01 17:13:20 -07001187 return true;
1188 }
1189
Craig Mautnere79d42682013-04-01 19:01:53 -07001190 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001191 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001192 // Is this activity's application already running?
1193 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001194 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001195
1196 r.task.stack.setLaunchTime(r);
1197
1198 if (app != null && app.thread != null) {
1199 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001200 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1201 || !"android".equals(r.info.packageName)) {
1202 // Don't add this if it is a platform component that is marked
1203 // to run in multiple processes, because this is actually
1204 // part of the framework so doesn't make sense to track as a
1205 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001206 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1207 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001208 }
George Mount2c92c972014-03-20 09:38:23 -07001209 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001210 return;
1211 } catch (RemoteException e) {
1212 Slog.w(TAG, "Exception when starting activity "
1213 + r.intent.getComponent().flattenToShortString(), e);
1214 }
1215
1216 // If a dead object exception was thrown -- fall through to
1217 // restart the application.
1218 }
1219
1220 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001221 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001222 }
1223
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001224 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001225 String resultWho, int requestCode, int callingPid, int callingUid,
1226 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
1227 ActivityRecord resultRecord, ActivityStack resultStack) {
1228 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1229 callingUid);
1230 if (startAnyPerm == PERMISSION_GRANTED) {
1231 return true;
1232 }
1233 final int componentRestriction = getComponentRestrictionForCallingPackage(
1234 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1235 final int actionRestriction = getActionRestrictionForCallingPackage(
1236 intent.getAction(), callingPackage, callingPid, callingUid);
1237 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1238 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1239 if (resultRecord != null) {
1240 resultStack.sendActivityResultLocked(-1,
1241 resultRecord, resultWho, requestCode,
1242 Activity.RESULT_CANCELED, null);
1243 }
1244 final String msg;
1245 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1246 msg = "Permission Denial: starting " + intent.toString()
1247 + " from " + callerApp + " (pid=" + callingPid
1248 + ", uid=" + callingUid + ")" + " with revoked permission "
1249 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1250 } else if (!aInfo.exported) {
1251 msg = "Permission Denial: starting " + intent.toString()
1252 + " from " + callerApp + " (pid=" + callingPid
1253 + ", uid=" + callingUid + ")"
1254 + " not exported from uid " + aInfo.applicationInfo.uid;
1255 } else {
1256 msg = "Permission Denial: starting " + intent.toString()
1257 + " from " + callerApp + " (pid=" + callingPid
1258 + ", uid=" + callingUid + ")"
1259 + " requires " + aInfo.permission;
1260 }
1261 Slog.w(TAG, msg);
1262 throw new SecurityException(msg);
1263 }
1264
1265 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1266 final String message = "Appop Denial: starting " + intent.toString()
1267 + " from " + callerApp + " (pid=" + callingPid
1268 + ", uid=" + callingUid + ")"
1269 + " requires " + AppOpsManager.permissionToOp(
1270 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1271 Slog.w(TAG, message);
1272 return false;
1273 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1274 final String message = "Appop Denial: starting " + intent.toString()
1275 + " from " + callerApp + " (pid=" + callingPid
1276 + ", uid=" + callingUid + ")"
1277 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1278 Slog.w(TAG, message);
1279 return false;
1280 }
1281 return true;
1282 }
1283
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001284 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001285 final long identity = Binder.clearCallingIdentity();
1286 try {
1287 return UserManager.get(mService.mContext).getUserInfo(userId);
1288 } finally {
1289 Binder.restoreCallingIdentity(identity);
1290 }
1291 }
1292
Svet Ganov99b60432015-06-27 13:15:22 -07001293 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001294 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001295 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1296 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001297 == PackageManager.PERMISSION_DENIED) {
1298 return ACTIVITY_RESTRICTION_PERMISSION;
1299 }
1300
Christopher Tateff7add02015-08-17 10:23:22 -07001301 if (activityInfo.permission == null) {
1302 return ACTIVITY_RESTRICTION_NONE;
1303 }
1304
Svet Ganov99b60432015-06-27 13:15:22 -07001305 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1306 if (opCode == AppOpsManager.OP_NONE) {
1307 return ACTIVITY_RESTRICTION_NONE;
1308 }
1309
1310 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1311 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001312 if (!ignoreTargetSecurity) {
1313 return ACTIVITY_RESTRICTION_APPOP;
1314 }
Svet Ganov99b60432015-06-27 13:15:22 -07001315 }
1316
1317 return ACTIVITY_RESTRICTION_NONE;
1318 }
1319
Svetoslav7008b512015-06-24 18:47:07 -07001320 private int getActionRestrictionForCallingPackage(String action,
1321 String callingPackage, int callingPid, int callingUid) {
1322 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001323 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001324 }
1325
1326 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1327 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001328 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001329 }
1330
1331 final PackageInfo packageInfo;
1332 try {
1333 packageInfo = mService.mContext.getPackageManager()
1334 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1335 } catch (PackageManager.NameNotFoundException e) {
1336 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001337 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001338 }
1339
1340 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001341 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001342 }
1343
1344 if (mService.checkPermission(permission, callingPid, callingUid) ==
1345 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001346 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001347 }
1348
1349 final int opCode = AppOpsManager.permissionToOpCode(permission);
1350 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001351 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001352 }
1353
1354 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1355 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001356 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001357 }
1358
Svet Ganov99b60432015-06-27 13:15:22 -07001359 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001360 }
1361
Wale Ogunwaled046a012015-12-24 13:05:59 -08001362 boolean moveActivityStackToFront(ActivityRecord r, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001363 if (r == null) {
1364 // Not sure what you are trying to do, but it is not going to work...
1365 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001366 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001367 final TaskRecord task = r.task;
1368 if (task == null || task.stack == null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001369 Slog.w(TAG, "Can't move stack to front for r=" + r + " task=" + task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001370 return false;
1371 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001372 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001373 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001374 }
1375
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001376 Rect getOverrideBounds(ActivityRecord r, ActivityOptions options, TaskRecord inTask) {
Chong Zhang0fa656b2015-08-31 15:17:21 -07001377 Rect newBounds = null;
Wale Ogunwale6cae7652015-12-26 07:36:26 -08001378 if (options != null && (r.isResizeable() || (inTask != null && inTask.mResizeable))) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001379 if (canUseActivityOptionsLaunchBounds(options)) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001380 newBounds = options.getLaunchBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07001381 }
1382 }
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001383 return newBounds;
Craig Mautneree36c772014-07-16 14:56:05 -07001384 }
1385
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001386 void setLaunchSource(int uid) {
1387 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1388 }
1389
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001390 void acquireLaunchWakelock() {
1391 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1392 throw new IllegalStateException("Calling must be system uid");
1393 }
1394 mLaunchingActivity.acquire();
1395 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1396 // To be safe, don't allow the wake lock to be held for too long.
1397 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1398 }
1399 }
1400
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001401 /**
1402 * Called when the frontmost task is idle.
1403 * @return the state of mService.mBooting before this was called.
1404 */
1405 private boolean checkFinishBootingLocked() {
1406 final boolean booting = mService.mBooting;
1407 boolean enableScreen = false;
1408 mService.mBooting = false;
1409 if (!mService.mBooted) {
1410 mService.mBooted = true;
1411 enableScreen = true;
1412 }
1413 if (booting || enableScreen) {
1414 mService.postFinishBooting(booting, enableScreen);
1415 }
1416 return booting;
1417 }
1418
Craig Mautnerf3333272013-04-22 10:55:53 -07001419 // Checked.
1420 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
1421 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001422 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001423
Craig Mautnerf3333272013-04-22 10:55:53 -07001424 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001425 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001426 int NS = 0;
1427 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001428 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001429 boolean activityRemoved = false;
1430
Wale Ogunwale7d701172015-03-11 15:36:30 -07001431 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001432 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001433 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1434 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001435 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1436 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001437 if (fromTimeout) {
1438 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001439 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001440
1441 // This is a hack to semi-deal with a race condition
1442 // in the client where it can be constructed with a
1443 // newer configuration from when we asked it to launch.
1444 // We'll update with whatever configuration it now says
1445 // it used to launch.
1446 if (config != null) {
1447 r.configuration = config;
1448 }
1449
1450 // We are now idle. If someone is waiting for a thumbnail from
1451 // us, we can now deliver.
1452 r.idle = true;
1453
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001454 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001455 if (isFocusedStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001456 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001457 }
1458 }
1459
1460 if (allResumedActivitiesIdle()) {
1461 if (r != null) {
1462 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001463 }
1464
1465 if (mLaunchingActivity.isHeld()) {
1466 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1467 if (VALIDATE_WAKE_LOCK_CALLER &&
1468 Binder.getCallingUid() != Process.myUid()) {
1469 throw new IllegalStateException("Calling must be system uid");
1470 }
1471 mLaunchingActivity.release();
1472 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001473 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001474 }
1475
1476 // Atomically retrieve all of the other things to do.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001477 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(true);
Craig Mautnerf3333272013-04-22 10:55:53 -07001478 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001479 if ((NF = mFinishingActivities.size()) > 0) {
1480 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001481 mFinishingActivities.clear();
1482 }
1483
Craig Mautnerf3333272013-04-22 10:55:53 -07001484 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001485 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001486 mStartingUsers.clear();
1487 }
1488
Craig Mautnerf3333272013-04-22 10:55:53 -07001489 // Stop any activities that are scheduled to do so but have been
1490 // waiting for the next one to start.
1491 for (int i = 0; i < NS; i++) {
1492 r = stops.get(i);
1493 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001494 if (stack != null) {
1495 if (r.finishing) {
1496 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1497 } else {
1498 stack.stopActivityLocked(r);
1499 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001500 }
1501 }
1502
1503 // Finish any activities that are scheduled to do so but have been
1504 // waiting for the next one to start.
1505 for (int i = 0; i < NF; i++) {
1506 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07001507 final ActivityStack stack = r.task.stack;
1508 if (stack != null) {
1509 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1510 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001511 }
1512
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001513 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001514 // Complete user switch
1515 if (startingUsers != null) {
1516 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001517 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001518 }
1519 }
1520 // Complete starting up of background users
1521 if (mStartingBackgroundUsers.size() > 0) {
Amith Yamasani37a40c22015-06-17 13:25:42 -07001522 startingUsers = new ArrayList<UserState>(mStartingBackgroundUsers);
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001523 mStartingBackgroundUsers.clear();
1524 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001525 mService.mUserController.finishUserBoot(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001526 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001527 }
1528 }
1529
1530 mService.trimApplications();
1531 //dump();
1532 //mWindowManager.dump();
1533
Craig Mautnerf3333272013-04-22 10:55:53 -07001534 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001535 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001536 }
1537
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001538 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001539 }
1540
Craig Mautner8e569572013-10-11 17:36:59 -07001541 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001542 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001543 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1544 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001545 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1546 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1547 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001548 }
Craig Mautner19091252013-10-05 00:03:53 -07001549 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001550 }
1551
1552 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001553 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1554 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001555 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1556 stacks.get(stackNdx).closeSystemDialogsLocked();
1557 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001558 }
1559 }
1560
Craig Mautner93529a42013-10-04 15:03:13 -07001561 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001562 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001563 }
1564
Craig Mautner8d341ef2013-03-26 09:03:27 -07001565 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001566 * Update the last used stack id for non-current user (current user's last
1567 * used stack is the focused stack)
1568 */
1569 void updateUserStackLocked(int userId, ActivityStack stack) {
1570 if (userId != mCurrentUser) {
1571 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1572 }
1573 }
1574
1575 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001576 * @return true if some activity was finished (or would have finished if doit were true).
1577 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001578 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1579 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001580 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001581 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1582 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001583 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001584 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001585 if (stack.finishDisabledPackageActivitiesLocked(
1586 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001587 didSomething = true;
1588 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001589 }
1590 }
1591 return didSomething;
1592 }
1593
Dianne Hackborna413dc02013-07-12 12:02:55 -07001594 void updatePreviousProcessLocked(ActivityRecord r) {
1595 // Now that this process has stopped, we may want to consider
1596 // it to be the previous app to try to keep around in case
1597 // the user wants to return to it.
1598
1599 // First, found out what is currently the foreground app, so that
1600 // we don't blow away the previous app if this activity is being
1601 // hosted by the process that is actually still the foreground.
1602 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001603 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1604 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001605 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1606 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001607 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001608 if (stack.mResumedActivity != null) {
1609 fgApp = stack.mResumedActivity.app;
1610 } else if (stack.mPausingActivity != null) {
1611 fgApp = stack.mPausingActivity.app;
1612 }
1613 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001614 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001615 }
1616 }
1617
1618 // Now set this one as the previous process, only if that really
1619 // makes sense to.
1620 if (r.app != null && fgApp != null && r.app != fgApp
1621 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001622 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001623 mService.mPreviousProcess = r.app;
1624 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1625 }
1626 }
1627
Wale Ogunwaled046a012015-12-24 13:05:59 -08001628 boolean resumeFocusedStackTopActivityLocked() {
1629 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001630 }
1631
Wale Ogunwaled046a012015-12-24 13:05:59 -08001632 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001633 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001634 if (targetStack != null && isFocusedStack(targetStack)) {
1635 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001636 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001637 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
1638 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001639 }
1640
Todd Kennedy539db512014-12-15 09:57:55 -08001641 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
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;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001644 final int numStacks = stacks.size();
1645 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1646 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08001647 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001648 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001649 }
1650 }
1651
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07001652 void finishVoiceTask(IVoiceInteractionSession session) {
1653 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1654 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1655 final int numStacks = stacks.size();
1656 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1657 final ActivityStack stack = stacks.get(stackNdx);
1658 stack.finishVoiceTask(session);
1659 }
1660 }
1661 }
1662
Chong Zhang280d3322015-11-03 17:27:26 -08001663 void findTaskToMoveToFrontLocked(
1664 TaskRecord task, int flags, ActivityOptions options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001665 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
1666 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001667 }
Craig Mautneraea74a52014-03-08 14:23:10 -08001668 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
1669 // Caller wants the home activity moved with it. To accomplish this,
1670 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07001671 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08001672 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07001673 if (task.stack == null) {
1674 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
1675 + task + " to front. Stack is null");
1676 return;
1677 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001678
Chong Zhang0fa656b2015-08-31 15:17:21 -07001679 if (task.mResizeable && options != null) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001680 if (canUseActivityOptionsLaunchBounds(options)) {
1681 Rect bounds = options.getLaunchBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07001682 task.updateOverrideConfiguration(bounds);
Chong Zhang112eb8c2015-11-02 11:17:00 -08001683 final int stackId = task.getLaunchStackId();
1684 if (stackId != task.stack.mStackId) {
1685 moveTaskToStackUncheckedLocked(task, stackId, ON_TOP, !FORCE_FOCUS, reason);
1686 // moveTaskToStackUncheckedLocked() should already placed the task on top,
1687 // still need moveTaskToFrontLocked() below for any transition settings.
1688 }
1689 // WM resizeTask must be done after the task is moved to the correct stack,
1690 // because Task's setBounds() also updates dim layer's bounds, but that has
1691 // dependency on the stack.
Chong Zhang87b21722015-09-21 15:39:51 -07001692 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig,
1693 false /*relayout*/, false /*forced*/);
Chong Zhang0fa656b2015-08-31 15:17:21 -07001694 }
1695 }
1696
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001697 final ActivityRecord r = task.getTopActivity();
1698 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
1699 r == null ? null : r.appTimeTracker, reason);
1700
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001701 if (DEBUG_STACK) Slog.d(TAG_STACK,
1702 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001703 }
1704
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001705 private boolean canUseActivityOptionsLaunchBounds(ActivityOptions options) {
1706 // We use the launch bounds in the activity options is the device supports freeform
1707 // window management.
1708 return options.hasLaunchBounds() && mService.mSupportsFreeformWindowManagement;
1709 }
1710
Craig Mautner967212c2013-04-13 21:10:58 -07001711 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07001712 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001713 }
1714
1715 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001716 ActivityContainer activityContainer = mActivityContainers.get(stackId);
1717 if (activityContainer != null) {
1718 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001719 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07001720 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001721 return null;
1722 }
1723 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001724 }
1725
Craig Mautner967212c2013-04-13 21:10:58 -07001726 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07001727 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001728 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1729 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001730 }
1731 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07001732 }
1733
Craig Mautner4a1cb222013-12-04 16:14:06 -08001734 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001735 ActivityRecord homeActivity = getHomeActivity();
1736 if (homeActivity != null) {
1737 return homeActivity.appToken;
1738 }
1739 return null;
1740 }
1741
1742 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07001743 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001744 }
1745
1746 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001747 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
1748 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1749 final TaskRecord task = tasks.get(taskNdx);
1750 if (task.isHomeTask()) {
1751 final ArrayList<ActivityRecord> activities = task.mActivities;
1752 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1753 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001754 if (r.isHomeActivity()
1755 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001756 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001757 }
1758 }
1759 }
1760 }
1761 return null;
1762 }
1763
Chong Zhangb15758a2015-11-17 12:12:03 -08001764 /**
1765 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
1766 * the docked stack itself, or if it's side-by-side to the docked stack.
1767 */
1768 boolean isStackDockedInEffect(int stackId) {
1769 return stackId == DOCKED_STACK_ID ||
1770 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
1771 }
1772
Todd Kennedyca4d8422015-01-15 15:19:22 -08001773 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08001774 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07001775 ActivityContainer activityContainer =
1776 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001777 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001778 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1779 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001780 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001781 return activityContainer;
1782 }
1783
Craig Mautner34b73df2014-01-12 21:11:08 -08001784 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001785 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
1786 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
1787 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001788 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
1789 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001790 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08001791 }
1792 }
1793
Craig Mautner95da1082014-02-24 17:54:35 -08001794 void deleteActivityContainer(IActivityContainer container) {
1795 ActivityContainer activityContainer = (ActivityContainer)container;
1796 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001797 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1798 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08001799 final int stackId = activityContainer.mStackId;
1800 mActivityContainers.remove(stackId);
1801 mWindowManager.removeStack(stackId);
1802 }
1803 }
1804
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07001805 void resizeStackLocked(int stackId, Rect bounds, boolean preserveWindows,
1806 boolean allowResizeInDockedMode) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08001807 final ActivityStack stack = getStack(stackId);
1808 if (stack == null) {
1809 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
1810 return;
1811 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08001812
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07001813 if (!allowResizeInDockedMode
1814 && stackId != DOCKED_STACK_ID && getStack(DOCKED_STACK_ID) != null) {
1815 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
1816 // stack size changing so things don't get out of sync.
1817 return;
1818 }
1819
Wale Ogunwalecad05a02015-09-25 10:41:44 -07001820 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02001821 mWindowManager.deferSurfaceLayout();
1822 try {
Wale Ogunwaleb4ec0a32015-12-14 10:31:43 -08001823
1824 if (bounds != null && mWindowManager.isFullscreenBounds(stackId, bounds)) {
1825 // The bounds passed in corresponds to the fullscreen bounds which we normally
1826 // represent with null. Go ahead and set it to null so that all tasks configuration
1827 // can have the right fullscreen state.
1828 bounds = null;
1829 }
1830
Jorim Jaggic4025202015-10-22 16:43:34 +02001831 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwalecad05a02015-09-25 10:41:44 -07001832
Jorim Jaggic4025202015-10-22 16:43:34 +02001833 mTmpBounds.clear();
1834 mTmpConfigs.clear();
1835 ArrayList<TaskRecord> tasks = stack.getAllTasks();
1836 for (int i = tasks.size() - 1; i >= 0; i--) {
1837 TaskRecord task = tasks.get(i);
1838 if (task.mResizeable) {
1839 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1840 // For freeform stack we don't adjust the size of the tasks to match that
1841 // of the stack, but we do try to make sure the tasks are still contained
1842 // with the bounds of the stack.
1843 tempRect2.set(task.mBounds);
1844 fitWithinBounds(tempRect2, bounds);
1845 task.updateOverrideConfiguration(tempRect2);
1846 } else {
1847 task.updateOverrideConfiguration(bounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001848 }
1849 }
1850
Jorim Jaggic4025202015-10-22 16:43:34 +02001851 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
1852 mTmpBounds.put(task.taskId, task.mBounds);
1853 }
1854 stack.mFullscreen = mWindowManager.resizeStack(stackId, bounds, mTmpConfigs, mTmpBounds);
1855 if (stack.mStackId == DOCKED_STACK_ID) {
1856 // Dock stack funness...Yay!
1857 if (stack.mFullscreen) {
1858 // The dock stack went fullscreen which is kinda like dismissing it.
1859 // In this case we make all other static stacks fullscreen and move all
1860 // docked stack tasks to the fullscreen stack.
1861 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale3797c222015-10-27 14:21:58 -07001862 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
Jorim Jaggic4025202015-10-22 16:43:34 +02001863 resizeStackLocked(i, null, preserveWindows, true);
1864 }
1865 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001866
Jorim Jaggic4025202015-10-22 16:43:34 +02001867 final int count = tasks.size();
1868 for (int i = 0; i < count; i++) {
1869 moveTaskToStackLocked(tasks.get(i).taskId,
Jorim Jaggi030979c2015-11-20 15:14:43 -08001870 FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP, FORCE_FOCUS, "resizeStack",
Jorim Jaggi55387522015-11-24 18:21:10 -08001871 false /* animate */);
Jorim Jaggic4025202015-10-22 16:43:34 +02001872 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001873
Jorim Jaggic4025202015-10-22 16:43:34 +02001874 // stack shouldn't contain anymore activities, so nothing to resume.
1875 r = null;
1876 } else {
1877 // Docked stacks occupy a dedicated region on screen so the size of all other
1878 // static stacks need to be adjusted so they don't overlap with the docked stack.
1879 // We get the bounds to use from window manager which has been adjusted for any
1880 // screen controls and is also the same for all stacks.
1881 mWindowManager.getStackDockedModeBounds(HOME_STACK_ID, tempRect);
1882
1883 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale3797c222015-10-27 14:21:58 -07001884 if (StackId.isResizeableByDockedStack(i)) {
Jorim Jaggic4025202015-10-22 16:43:34 +02001885 ActivityStack otherStack = getStack(i);
1886 if (otherStack != null) {
1887 resizeStackLocked(i, tempRect, PRESERVE_WINDOWS, true);
1888 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001889 }
1890 }
1891 }
Jorim Jaggic4025202015-10-22 16:43:34 +02001892 // Since we are resizing the stack, all other operations should strive to preserve
1893 // windows.
1894 preserveWindows = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001895 }
Jorim Jaggic4025202015-10-22 16:43:34 +02001896 stack.setBounds(bounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001897
Jorim Jaggic4025202015-10-22 16:43:34 +02001898 if (r != null) {
1899 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0, preserveWindows);
1900 // And we need to make sure at this point that all other activities
1901 // are made visible with the correct configuration.
1902 ensureActivitiesVisibleLocked(r, 0, preserveWindows);
1903 if (!updated) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001904 resumeFocusedStackTopActivityLocked();
Jorim Jaggic4025202015-10-22 16:43:34 +02001905 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001906 }
Jorim Jaggic4025202015-10-22 16:43:34 +02001907 } finally {
1908 mWindowManager.continueSurfaceLayout();
1909 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001910 }
1911 }
1912
Chong Zhang6de2ae82015-09-30 18:25:21 -07001913 void resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001914 if (!task.mResizeable) {
1915 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
1916 return;
1917 }
1918
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07001919 adjustForMinimalTaskDimensions(task, bounds);
1920
Chong Zhang87b21722015-09-21 15:39:51 -07001921 // If this is a forced resize, let it go through even if the bounds is not changing,
1922 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07001923 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Chong Zhang87b21722015-09-21 15:39:51 -07001924 if (task.mBounds != null && task.mBounds.equals(bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001925 // Nothing to do here...
1926 return;
1927 }
1928
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001929 if (!mWindowManager.isValidTaskId(task.taskId)) {
1930 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07001931 // All we can do for now is update the bounds so it can be used when the task is
1932 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07001933 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07001934 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
1935 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07001936 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07001937 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001938 return;
1939 }
1940
Chong Zhang6de2ae82015-09-30 18:25:21 -07001941 // Do not move the task to another stack here.
1942 // This method assumes that the task is already placed in the right stack.
1943 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07001944
Chong Zhang6de2ae82015-09-30 18:25:21 -07001945 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07001946
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07001947 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07001948 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001949 // way and the activity was kept the way it was. If it's false, it means the activity had
1950 // to be relaunched due to configuration change.
1951 boolean kept = true;
1952 if (overrideConfig != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001953 ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08001954 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001955 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07001956 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001957 // All other activities must be made visible with their correct configuration.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001958 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001959 if (!kept) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001960 resumeFocusedStackTopActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08001961 }
1962 }
1963 }
Chong Zhang87b21722015-09-21 15:39:51 -07001964 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07001965
1966 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwale60454db2015-01-23 16:05:07 -08001967 }
1968
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07001969 private void adjustForMinimalTaskDimensions(TaskRecord task, Rect bounds) {
1970 if (bounds == null) {
1971 return;
1972 }
1973 int minimalSize = task.mMinimalSize == -1 ? mDefaultMinimalSizeOfResizeableTask
1974 : task.mMinimalSize;
1975 final boolean adjustWidth = minimalSize > bounds.width();
1976 final boolean adjustHeight = minimalSize > bounds.height();
1977 if (!(adjustWidth || adjustHeight)) {
1978 return;
1979 }
1980 Rect taskBounds = task.mBounds;
1981 if (adjustWidth) {
1982 if (taskBounds != null && bounds.right == taskBounds.right) {
1983 bounds.left = bounds.right - minimalSize;
1984 } else {
1985 // Either left bounds match, or neither match, or the previous bounds were
1986 // fullscreen and we default to keeping left.
1987 bounds.right = bounds.left + minimalSize;
1988 }
1989 }
1990 if (adjustHeight) {
1991 if (taskBounds != null && bounds.bottom == taskBounds.bottom) {
1992 bounds.top = bounds.bottom - minimalSize;
1993 } else {
1994 // Either top bounds match, or neither match, or the previous bounds were
1995 // fullscreen and we default to keeping top.
1996 bounds.bottom = bounds.top + minimalSize;
1997 }
1998 }
1999 }
2000
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002001 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002002 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2003 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002004 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002005 }
2006
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002007 ActivityContainer activityContainer = new ActivityContainer(stackId);
2008 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002009 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002010 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002011 }
2012
2013 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002014 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002015 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2016 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002017 break;
2018 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002019 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002020 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002021 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002022 }
2023
Chong Zhang5dcb2752015-08-18 13:50:26 -07002024 /**
2025 * Restores a recent task to a stack
2026 * @param task The recent task to be restored.
2027 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale3797c222015-10-27 14:21:58 -07002028 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002029 * @return true if the task has been restored successfully.
2030 */
2031 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
2032 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002033 stackId = task.getLaunchStackId();
Chong Zhang5dcb2752015-08-18 13:50:26 -07002034 }
Wale Ogunwale706ed792015-08-02 10:29:44 -07002035 if (task.stack != null) {
2036 // Task has already been restored once. See if we need to do anything more
2037 if (task.stack.mStackId == stackId) {
2038 // Nothing else to do since it is already restored in the right stack.
2039 return true;
2040 }
2041 // Remove current stack association, so we can re-associate the task with the
2042 // right stack below.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002043 task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002044 }
2045
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002046 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002047 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002048
2049 if (stack == null) {
2050 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002051 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2052 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002053 return false;
2054 }
2055
Wale Ogunwale5f986092015-12-04 15:35:38 -08002056 stack.addTask(task, false, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002057 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2058 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002059 final ArrayList<ActivityRecord> activities = task.mActivities;
2060 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002061 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002062 }
2063 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002064 }
2065
Wale Ogunwale040b4702015-08-06 18:10:50 -07002066 /**
2067 * Moves the specified task record to the input stack id.
2068 * WARNING: This method performs an unchecked/raw move of the task and
2069 * can leave the system in an unstable state if used incorrectly.
2070 * Use {@link #moveTaskToStackLocked} to perform safe task movement
2071 * to a stack.
2072 * @param task Task to move.
2073 * @param stackId Id of stack to move task to.
2074 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07002075 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07002076 * @param reason Reason the task is been moved.
2077 * @return The stack the task was moved to.
2078 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07002079 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07002080 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
2081 final ActivityRecord r = task.getTopActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -08002082 final ActivityStack prevStack = task.stack;
2083 final boolean wasFocused = isFocusedStack(prevStack) && (topRunningActivityLocked() == r);
2084 final boolean wasResumed = wasFocused && (prevStack.mResumedActivity == r);
2085 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
2086 // Whenever we are moving the top activity from the front stack we want to make sure to move
2087 // the stack to the front.
2088 final boolean wasFront = isFrontStack(prevStack)
2089 && (prevStack.topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07002090
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002091 final boolean resizeable = task.mResizeable;
2092 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
2093 // if a docked stack is created below which will lead to the stack we are moving from and
2094 // its resizeable tasks being resized.
2095 task.mResizeable = false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002096 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002097 task.mResizeable = resizeable;
Wale Ogunwale040b4702015-08-06 18:10:50 -07002098 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002099 stack.addTask(task, toTop, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002100
2101 // If the task had focus before (or we're requested to move focus),
Wale Ogunwaled046a012015-12-24 13:05:59 -08002102 // move focus to the new stack by moving the stack to the front.
2103 stack.moveToFrontAndResumeStateIfNeeded(
2104 r, forceFocus || wasFocused || wasFront, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002105
Wale Ogunwale040b4702015-08-06 18:10:50 -07002106 return stack;
2107 }
2108
Filip Gruszczynski90186c62015-10-26 14:07:00 -07002109 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08002110 String reason, boolean animate) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002111 final TaskRecord task = anyTaskForIdLocked(taskId);
2112 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002113 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002114 return;
2115 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002116
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002117 if (task.stack != null && task.stack.mStackId == stackId) {
2118 // You are already in the right stack silly...
2119 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
2120 return;
2121 }
2122
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002123 final ActivityRecord topActivity = task.getTopActivity();
2124 if (StackId.preserveWindowOnTaskMove(stackId) && topActivity != null) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002125 // We are about to relaunch the activity because its configuration changed due to
2126 // being maximized, i.e. size change. The activity will first remove the old window
2127 // and then add a new one. This call will tell window manager about this, so it can
2128 // preserve the old window until the new one is drawn. This prevents having a gap
2129 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07002130 // entrance of the new window to be properly animated.
Jorim Jaggi030979c2015-11-20 15:14:43 -08002131 mWindowManager.setReplacingWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002132 }
Wale Ogunwale3cd48042015-11-16 13:01:41 -08002133 final ActivityStack stack = moveTaskToStackUncheckedLocked(
2134 task, stackId, toTop, forceFocus, "moveTaskToStack:" + reason);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002135
Jorim Jaggi55387522015-11-24 18:21:10 -08002136 if (!animate) {
2137 stack.mNoAnimActivities.add(topActivity);
2138 }
2139
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002140 // Make sure the task has the appropriate bounds/size for the stack it is in.
2141 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002142 resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002143 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID
2144 && task.mBounds == null && task.mLastNonFullscreenBounds != null) {
Chong Zhang6de2ae82015-09-30 18:25:21 -07002145 resizeTaskLocked(task, task.mLastNonFullscreenBounds,
2146 RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale99db1862015-10-23 20:08:22 -07002147 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002148 resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002149 }
2150
Stefan Kuhne54714cd2015-05-12 13:42:18 -07002151 // The task might have already been running and its visibility needs to be synchronized with
2152 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002153 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002154 resumeFocusedStackTopActivityLocked();
Chong Zhangb15758a2015-11-17 12:12:03 -08002155
2156 if (!task.mResizeable && isStackDockedInEffect(stackId)) {
Chong Zhangc806d902015-11-30 09:44:27 -08002157 showNonResizeableDockToast(taskId);
Chong Zhangb15758a2015-11-17 12:12:03 -08002158 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002159 }
2160
Wale Ogunwale079a0042015-10-24 11:44:07 -07002161 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2162 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2163 if (stack == null) {
2164 throw new IllegalArgumentException(
2165 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2166 }
2167
2168 final ActivityRecord r = stack.topRunningActivityLocked();
2169 if (r == null) {
2170 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2171 + " in stack=" + stack);
2172 return false;
2173 }
2174
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002175 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002176 Slog.w(TAG,
2177 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002178 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002179 return false;
2180 }
2181
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002182 moveActivityToStackLocked(r, PINNED_STACK_ID, "moveTopActivityToPinnedStack", bounds);
2183 return true;
2184 }
2185
2186 void moveActivityToStackLocked(ActivityRecord r, int stackId, String reason, Rect bounds) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002187 final TaskRecord task = r.task;
Wale Ogunwale079a0042015-10-24 11:44:07 -07002188 if (task.mActivities.size() == 1) {
2189 // There is only one activity in the task. So, we can just move the task over to the
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002190 // stack without re-parenting the activity in a different task.
2191 moveTaskToStackLocked(
2192 task.taskId, stackId, ON_TOP, FORCE_FOCUS, reason, true /* animate */);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002193 } else {
Wale Ogunwaled88f6512015-12-06 19:39:01 -08002194 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, ON_TOP);
2195 stack.moveActivityToStack(r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002196 }
2197
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002198 if (bounds != null) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002199 resizeStackLocked(stackId, bounds, !PRESERVE_WINDOWS, true);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002200 }
Wale Ogunwale079a0042015-10-24 11:44:07 -07002201
2202 // The task might have already been running and its visibility needs to be synchronized with
2203 // the visibility of the stack / windows.
2204 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002205 resumeFocusedStackTopActivityLocked();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002206 }
2207
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002208 void positionTaskInStackLocked(int taskId, int stackId, int position) {
2209 final TaskRecord task = anyTaskForIdLocked(taskId);
2210 if (task == null) {
2211 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
2212 return;
2213 }
Wale Ogunwale935e5022015-11-10 12:36:10 -08002214 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
2215
2216 task.updateOverrideConfigurationForStack(stack);
2217
2218 mWindowManager.positionTaskInStack(
2219 taskId, stackId, position, task.mBounds, task.mOverrideConfig);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002220 stack.positionTask(task, position);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002221 // The task might have already been running and its visibility needs to be synchronized with
2222 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002223 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002224 resumeFocusedStackTopActivityLocked();
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002225 }
2226
Craig Mautnerac6f8432013-07-17 13:24:59 -07002227 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002228 mTmpFindTaskResult.r = null;
2229 mTmpFindTaskResult.matchedByRootAffinity = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002230 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002231 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2232 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002233 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2234 final ActivityStack stack = stacks.get(stackNdx);
2235 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002236 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002237 continue;
2238 }
2239 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002240 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2241 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002242 continue;
2243 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002244 stack.findTaskLocked(r, mTmpFindTaskResult);
2245 // It is possible to have task in multiple stacks with the same root affinity.
2246 // If the match we found was based on root affinity we keep on looking to see if
2247 // there is a better match in another stack. We eventually return the match based
2248 // on root affinity if we don't find a better match.
2249 if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
2250 return mTmpFindTaskResult.r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002251 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002252 }
2253 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002254 if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
2255 return mTmpFindTaskResult.r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002256 }
2257
2258 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002259 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2260 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002261 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2262 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2263 if (ar != null) {
2264 return ar;
2265 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002266 }
2267 }
2268 return null;
2269 }
2270
Craig Mautner8d341ef2013-03-26 09:03:27 -07002271 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002272 scheduleSleepTimeout();
2273 if (!mGoingToSleep.isHeld()) {
2274 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002275 if (mLaunchingActivity.isHeld()) {
2276 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2277 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002278 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002279 mLaunchingActivity.release();
2280 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002281 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002282 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002283 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002284 }
2285
2286 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002287 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002288
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002289 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002290 final long endTime = System.currentTimeMillis() + timeout;
2291 while (true) {
2292 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002293 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2294 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002295 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2296 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2297 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002298 }
2299 if (cantShutdown) {
2300 long timeRemaining = endTime - System.currentTimeMillis();
2301 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002302 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002303 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002304 } catch (InterruptedException e) {
2305 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002306 } else {
2307 Slog.w(TAG, "Activity manager shutdown timed out");
2308 timedout = true;
2309 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002310 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002311 } else {
2312 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002313 }
2314 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002315
2316 // Force checkReadyForSleep to complete.
2317 mSleepTimeout = true;
2318 checkReadyForSleepLocked();
2319
Craig Mautner8d341ef2013-03-26 09:03:27 -07002320 return timedout;
2321 }
2322
2323 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002324 removeSleepTimeouts();
2325 if (mGoingToSleep.isHeld()) {
2326 mGoingToSleep.release();
2327 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002328 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2329 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002330 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2331 final ActivityStack stack = stacks.get(stackNdx);
2332 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002333 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002334 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002335 }
Craig Mautner5314a402013-09-26 12:40:16 -07002336 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002337 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002338 mGoingToSleepActivities.clear();
2339 }
2340
2341 void activitySleptLocked(ActivityRecord r) {
2342 mGoingToSleepActivities.remove(r);
2343 checkReadyForSleepLocked();
2344 }
2345
2346 void checkReadyForSleepLocked() {
2347 if (!mService.isSleepingOrShuttingDown()) {
2348 // Do not care.
2349 return;
2350 }
2351
2352 if (!mSleepTimeout) {
2353 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002354 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2355 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002356 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2357 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2358 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002359 }
2360
2361 if (mStoppingActivities.size() > 0) {
2362 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002363 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002364 + mStoppingActivities.size() + " activities");
2365 scheduleIdleLocked();
2366 dontSleep = true;
2367 }
2368
2369 if (mGoingToSleepActivities.size() > 0) {
2370 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002371 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002372 + mGoingToSleepActivities.size() + " activities");
2373 dontSleep = true;
2374 }
2375
2376 if (dontSleep) {
2377 return;
2378 }
2379 }
2380
Craig Mautnere0a38842013-12-16 16:14:02 -08002381 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2382 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002383 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2384 stacks.get(stackNdx).goToSleep();
2385 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002386 }
2387
2388 removeSleepTimeouts();
2389
2390 if (mGoingToSleep.isHeld()) {
2391 mGoingToSleep.release();
2392 }
2393 if (mService.mShuttingDown) {
2394 mService.notifyAll();
2395 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002396 }
2397
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002398 boolean reportResumedActivityLocked(ActivityRecord r) {
2399 final ActivityStack stack = r.task.stack;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002400 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002401 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002402 }
2403 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002404 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002405 mWindowManager.executeAppTransition();
2406 return true;
2407 }
2408 return false;
2409 }
2410
Craig Mautner8d341ef2013-03-26 09:03:27 -07002411 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002412 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2413 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07002414 int stackNdx = stacks.size() - 1;
2415 while (stackNdx >= 0) {
2416 stacks.get(stackNdx).handleAppCrashLocked(app);
2417 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002418 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002419 }
2420 }
2421
Jose Lima4b6c6692014-08-12 17:41:12 -07002422 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002423 final ActivityStack stack = r.task.stack;
2424 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002425 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2426 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002427 return false;
2428 }
Jose Lima4b6c6692014-08-12 17:41:12 -07002429 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002430 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2431 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002432
2433 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002434 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002435 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07002436 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002437 return true;
2438 }
2439
2440 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07002441 if (visible && top.fullscreen) {
2442 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002443 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2444 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
2445 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
2446 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002447 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07002448 } else if (!visible && stack.getVisibleBehindActivity() != r) {
2449 // Only the activity set as currently visible behind should actively reset its
2450 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002451 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2452 "requestVisibleBehind: returning visible=" + visible
2453 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
2454 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07002455 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002456 }
2457
Jose Lima4b6c6692014-08-12 17:41:12 -07002458 stack.setVisibleBehindActivity(visible ? r : null);
2459 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002460 // Make the activity immediately above r opaque.
2461 final ActivityRecord next = stack.findNextTranslucentActivity(r);
2462 if (next != null) {
2463 mService.convertFromTranslucent(next.appToken);
2464 }
2465 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07002466 if (top.app != null && top.app.thread != null) {
2467 // Notify the top app of the change.
2468 try {
2469 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
2470 } catch (RemoteException e) {
2471 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07002472 }
2473 return true;
2474 }
2475
Craig Mautnerbb742462014-07-07 15:28:55 -07002476 // Called when WindowManager has finished animating the launchingBehind activity to the back.
2477 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
2478 r.mLaunchTaskBehind = false;
2479 final TaskRecord task = r.task;
Winson8b1871d2015-11-20 09:56:20 -08002480 task.setLastThumbnailLocked(task.stack.screenshotActivitiesLocked(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002481 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08002482 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07002483 mWindowManager.setAppVisibility(r.appToken, false);
2484 }
2485
2486 void scheduleLaunchTaskBehindComplete(IBinder token) {
2487 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2488 }
2489
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002490 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
2491 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07002492 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002493 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2494 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002495 final int topStackNdx = stacks.size() - 1;
2496 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2497 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002498 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07002499 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002500 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002501 }
2502
Chong Zhangfdcc4d42015-10-14 16:50:12 -07002503 void invalidateTaskLayers() {
2504 mTaskLayersChanged = true;
2505 }
2506
2507 void rankTaskLayersIfNeeded() {
2508 if (!mTaskLayersChanged) {
2509 return;
2510 }
2511 mTaskLayersChanged = false;
2512 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
2513 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2514 int baseLayer = 0;
2515 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2516 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
2517 }
2518 }
2519 }
2520
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002521 void clearOtherAppTimeTrackers(AppTimeTracker except) {
2522 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2523 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2524 final int topStackNdx = stacks.size() - 1;
2525 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2526 final ActivityStack stack = stacks.get(stackNdx);
2527 stack.clearOtherAppTimeTrackers(except);
2528 }
2529 }
2530 }
2531
Craig Mautner8d341ef2013-03-26 09:03:27 -07002532 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002533 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2534 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002535 final int numStacks = stacks.size();
2536 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2537 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002538 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002539 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002540 }
2541 }
2542
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002543 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
2544 // Examine all activities currently running in the process.
2545 TaskRecord firstTask = null;
2546 // Tasks is non-null only if two or more tasks are found.
2547 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002548 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
2549 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002550 ActivityRecord r = app.activities.get(i);
2551 // First, if we find an activity that is in the process of being destroyed,
2552 // then we just aren't going to do anything for now; we want things to settle
2553 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002554 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002555 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002556 return;
2557 }
2558 // Don't consider any activies that are currently not in a state where they
2559 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002560 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
2561 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002562 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002563 continue;
2564 }
2565 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002566 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002567 + " from " + r);
2568 if (firstTask == null) {
2569 firstTask = r.task;
2570 } else if (firstTask != r.task) {
2571 if (tasks == null) {
2572 tasks = new ArraySet<>();
2573 tasks.add(firstTask);
2574 }
2575 tasks.add(r.task);
2576 }
2577 }
2578 }
2579 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002580 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002581 return;
2582 }
2583 // If we have activities in multiple tasks that are in a position to be destroyed,
2584 // let's iterate through the tasks and release the oldest one.
2585 final int numDisplays = mActivityDisplays.size();
2586 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
2587 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2588 // Step through all stacks starting from behind, to hit the oldest things first.
2589 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
2590 final ActivityStack stack = stacks.get(stackNdx);
2591 // Try to release activities in this stack; if we manage to, we are done.
2592 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
2593 return;
2594 }
2595 }
2596 }
2597 }
2598
Amith Yamasani37a40c22015-06-17 13:25:42 -07002599 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002600 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002601 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07002602 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002603
Craig Mautner858d8a62013-04-23 17:08:34 -07002604 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08002605 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2606 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002607 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002608 final ActivityStack stack = stacks.get(stackNdx);
2609 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002610 TaskRecord task = stack.topTask();
2611 if (task != null) {
2612 mWindowManager.moveTaskToTop(task.taskId);
2613 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002614 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07002615 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002616
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002617 ActivityStack stack = getStack(restoreStackId);
2618 if (stack == null) {
2619 stack = mHomeStack;
2620 }
2621 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002622 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07002623 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08002624 } else {
2625 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08002626 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08002627 }
Craig Mautner93529a42013-10-04 15:03:13 -07002628 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07002629 }
2630
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002631 /**
2632 * Add background users to send boot completed events to.
2633 * @param userId The user being started in the background
2634 * @param uss The state object for the user.
2635 */
Amith Yamasani37a40c22015-06-17 13:25:42 -07002636 public void startBackgroundUserLocked(int userId, UserState uss) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002637 mStartingBackgroundUsers.add(uss);
2638 }
2639
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002640 /** Checks whether the userid is a profile of the current user. */
2641 boolean isCurrentProfileLocked(int userId) {
2642 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002643 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002644 }
2645
Chong Zhang45c25ce2015-08-10 22:18:26 -07002646 /** Checks whether the activity should be shown for current user. */
2647 boolean okToShowLocked(ActivityRecord r) {
2648 return r != null && (isCurrentProfileLocked(r.userId)
2649 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
2650 }
2651
Craig Mautnerde4ef022013-04-07 19:01:33 -07002652 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002653 ArrayList<ActivityRecord> stops = null;
2654
2655 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08002656 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2657 ActivityRecord s = mStoppingActivities.get(activityNdx);
2658 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002659 if (DEBUG_ALL) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08002660 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
2661 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002662 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002663 if (s.finishing) {
2664 // If this activity is finishing, it is sitting on top of
2665 // everyone else but we now know it is no longer needed...
2666 // so get rid of it. Otherwise, we need to go through the
2667 // normal flow and hide it once we determine that it is
2668 // hidden by the activities in front of it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002669 if (DEBUG_ALL) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002670 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002671 }
2672 }
Craig Mautner8c14c152015-01-15 17:32:07 -08002673 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002674 if (DEBUG_ALL) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002675 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08002676 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002677 }
2678 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08002679 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002680 }
2681 }
2682
2683 return stops;
2684 }
2685
Craig Mautnercf910b02013-04-23 11:23:27 -07002686 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002687 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2688 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2689 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2690 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002691 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002692 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002693 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002694 if (r == null) Slog.e(TAG,
2695 "validateTop...: null top activity, stack=" + stack);
2696 else {
2697 final ActivityRecord pausing = stack.mPausingActivity;
2698 if (pausing != null && pausing == r) Slog.e(TAG,
2699 "validateTop...: top stack has pausing activity r=" + r
2700 + " state=" + state);
2701 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
2702 "validateTop...: activity in front not resumed r=" + r
2703 + " state=" + state);
2704 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002705 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002706 final ActivityRecord resumed = stack.mResumedActivity;
2707 if (resumed != null && resumed == r) Slog.e(TAG,
2708 "validateTop...: back stack has resumed activity r=" + r
2709 + " state=" + state);
2710 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
2711 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002712 }
2713 }
2714 }
Craig Mautner76ea2242013-05-15 11:40:05 -07002715 }
2716
Craig Mautnere0570202015-05-13 13:06:11 -07002717 private String lockTaskModeToString() {
2718 switch (mLockTaskModeState) {
2719 case LOCK_TASK_MODE_LOCKED:
2720 return "LOCKED";
2721 case LOCK_TASK_MODE_PINNED:
2722 return "PINNED";
2723 case LOCK_TASK_MODE_NONE:
2724 return "NONE";
2725 default: return "unknown=" + mLockTaskModeState;
2726 }
2727 }
2728
Craig Mautner27084302013-03-25 08:05:25 -07002729 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002730 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002731 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002732 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
2733 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
2734 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08002735 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07002736 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
2737 final SparseArray<String[]> packages = mService.mLockTaskPackages;
2738 if (packages.size() > 0) {
2739 pw.println(" mLockTaskPackages (userId:packages)=");
2740 for (int i = 0; i < packages.size(); ++i) {
2741 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
2742 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
2743 }
2744 }
2745 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07002746 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002747
Craig Mautner20e72272013-04-01 13:45:53 -07002748 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002749 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07002750 }
2751
Dianne Hackborn390517b2013-05-30 15:03:32 -07002752 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
2753 boolean needSep, String prefix) {
2754 if (activity != null) {
2755 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
2756 if (needSep) {
2757 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07002758 }
2759 pw.print(prefix);
2760 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002761 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002762 }
2763 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002764 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002765 }
2766
Craig Mautner8d341ef2013-03-26 09:03:27 -07002767 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
2768 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002769 boolean printed = false;
2770 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002771 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
2772 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002773 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07002774 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08002775 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07002776 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002777 final ActivityStack stack = stacks.get(stackNdx);
2778 StringBuilder stackHeader = new StringBuilder(128);
2779 stackHeader.append(" Stack #");
2780 stackHeader.append(stack.mStackId);
2781 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002782 stackHeader.append("\n");
2783 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
2784 stackHeader.append("\n");
2785 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002786 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
2787 needSep, stackHeader.toString());
2788 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
2789 !dumpAll, false, dumpPackage, true,
2790 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002791
Craig Mautner4a1cb222013-12-04 16:14:06 -08002792 needSep = printed;
2793 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
2794 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002795 if (pr) {
2796 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002797 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002798 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002799 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
2800 " mResumedActivity: ");
2801 if (pr) {
2802 printed = true;
2803 needSep = false;
2804 }
2805 if (dumpAll) {
2806 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
2807 " mLastPausedActivity: ");
2808 if (pr) {
2809 printed = true;
2810 needSep = true;
2811 }
2812 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
2813 needSep, " mLastNoHistoryActivity: ");
2814 }
2815 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002816 }
2817 }
2818
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002819 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
2820 false, dumpPackage, true, " Activities waiting to finish:", null);
2821 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
2822 false, dumpPackage, true, " Activities waiting to stop:", null);
2823 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
2824 false, dumpPackage, true, " Activities waiting for another to become visible:",
2825 null);
2826 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
2827 false, dumpPackage, true, " Activities waiting to sleep:", null);
2828 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
2829 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07002830
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002831 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002832 }
2833
Dianne Hackborn390517b2013-05-30 15:03:32 -07002834 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07002835 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002836 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002837 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002838 String innerPrefix = null;
2839 String[] args = null;
2840 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002841 for (int i=list.size()-1; i>=0; i--) {
2842 final ActivityRecord r = list.get(i);
2843 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
2844 continue;
2845 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07002846 if (innerPrefix == null) {
2847 innerPrefix = prefix + " ";
2848 args = new String[0];
2849 }
2850 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002851 final boolean full = !brief && (complete || !r.isInHistory());
2852 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07002853 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07002854 needNL = false;
2855 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002856 if (header1 != null) {
2857 pw.println(header1);
2858 header1 = null;
2859 }
2860 if (header2 != null) {
2861 pw.println(header2);
2862 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002863 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002864 if (lastTask != r.task) {
2865 lastTask = r.task;
2866 pw.print(prefix);
2867 pw.print(full ? "* " : " ");
2868 pw.println(lastTask);
2869 if (full) {
2870 lastTask.dump(pw, prefix + " ");
2871 } else if (complete) {
2872 // Complete + brief == give a summary. Isn't that obvious?!?
2873 if (lastTask.intent != null) {
2874 pw.print(prefix); pw.print(" ");
2875 pw.println(lastTask.intent.toInsecureStringWithClip());
2876 }
2877 }
2878 }
2879 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
2880 pw.print(" #"); pw.print(i); pw.print(": ");
2881 pw.println(r);
2882 if (full) {
2883 r.dump(pw, innerPrefix);
2884 } else if (complete) {
2885 // Complete + brief == give a summary. Isn't that obvious?!?
2886 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
2887 if (r.app != null) {
2888 pw.print(innerPrefix); pw.println(r.app);
2889 }
2890 }
2891 if (client && r.app != null && r.app.thread != null) {
2892 // flush anything that is already in the PrintWriter since the thread is going
2893 // to write to the file descriptor directly
2894 pw.flush();
2895 try {
2896 TransferPipe tp = new TransferPipe();
2897 try {
2898 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
2899 r.appToken, innerPrefix, args);
2900 // Short timeout, since blocking here can
2901 // deadlock with the application.
2902 tp.go(fd, 2000);
2903 } finally {
2904 tp.kill();
2905 }
2906 } catch (IOException e) {
2907 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
2908 } catch (RemoteException e) {
2909 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
2910 }
2911 needNL = true;
2912 }
2913 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07002914 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002915 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002916
Craig Mautnerf3333272013-04-22 10:55:53 -07002917 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002918 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
2919 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07002920 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
2921 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07002922 }
2923
2924 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07002925 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07002926 }
2927
2928 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002929 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
2930 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002931 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2932 }
2933
Craig Mautner05d29032013-05-03 13:40:13 -07002934 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08002935 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
2936 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
2937 }
Craig Mautner05d29032013-05-03 13:40:13 -07002938 }
2939
Craig Mautner0eea92c2013-05-16 13:35:39 -07002940 void removeSleepTimeouts() {
2941 mSleepTimeout = false;
2942 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
2943 }
2944
2945 final void scheduleSleepTimeout() {
2946 removeSleepTimeouts();
2947 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
2948 }
2949
Craig Mautner4a1cb222013-12-04 16:14:06 -08002950 @Override
2951 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07002952 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002953 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
2954 }
2955
2956 @Override
2957 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07002958 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002959 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
2960 }
2961
2962 @Override
2963 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07002964 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002965 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
2966 }
2967
Wale Ogunwalef16a2812015-04-01 11:23:15 -07002968 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08002969 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002970 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08002971 newDisplay = mActivityDisplays.get(displayId) == null;
2972 if (newDisplay) {
2973 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07002974 if (activityDisplay.mDisplay == null) {
2975 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
2976 return;
2977 }
Craig Mautner4504de52013-12-20 09:06:56 -08002978 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07002979 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08002980 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002981 }
Craig Mautner4504de52013-12-20 09:06:56 -08002982 if (newDisplay) {
2983 mWindowManager.onDisplayAdded(displayId);
2984 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002985 }
2986
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07002987 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
2988 if (display.mDisplayId != Display.DEFAULT_DISPLAY) {
2989 return;
2990 }
2991 final float fraction = mService.mContext.getResources().getFraction(com.android.internal.R.
2992 fraction.config_displayFractionForDefaultMinimalSizeOfResizeableTask, 1, 1);
2993 mDefaultMinimalSizeOfResizeableTask = (int) (fraction * Math.min(
2994 display.mDisplayInfo.logicalWidth, display.mDisplayInfo.logicalHeight));
2995 }
2996
Wale Ogunwalef16a2812015-04-01 11:23:15 -07002997 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002998 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002999 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3000 if (activityDisplay != null) {
3001 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003002 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003003 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003004 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003005 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003006 }
3007 }
3008 mWindowManager.onDisplayRemoved(displayId);
3009 }
3010
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003011 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003012 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003013 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3014 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003015 // TODO: Update the bounds.
3016 }
3017 }
3018 mWindowManager.onDisplayChanged(displayId);
3019 }
3020
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003021 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003022 StackInfo info = new StackInfo();
3023 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3024 info.displayId = Display.DEFAULT_DISPLAY;
3025 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003026 info.userId = stack.mCurrentUser;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003027
3028 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3029 final int numTasks = tasks.size();
3030 int[] taskIds = new int[numTasks];
3031 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003032 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003033 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003034 for (int i = 0; i < numTasks; ++i) {
3035 final TaskRecord task = tasks.get(i);
3036 taskIds[i] = task.taskId;
3037 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3038 : task.realActivity != null ? task.realActivity.flattenToString()
3039 : task.getTopActivity() != null ? task.getTopActivity().packageName
3040 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003041 taskBounds[i] = new Rect();
3042 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003043 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003044 }
3045 info.taskIds = taskIds;
3046 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003047 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003048 info.taskUserIds = taskUserIds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003049 return info;
3050 }
3051
3052 StackInfo getStackInfoLocked(int stackId) {
3053 ActivityStack stack = getStack(stackId);
3054 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003055 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003056 }
3057 return null;
3058 }
3059
3060 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003061 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003062 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3063 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003064 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003065 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003066 }
3067 }
3068 return list;
3069 }
3070
Craig Mautner15df08a2015-04-01 12:17:18 -07003071 TaskRecord getLockedTaskLocked() {
3072 final int top = mLockTaskModeTasks.size() - 1;
3073 if (top >= 0) {
3074 return mLockTaskModeTasks.get(top);
3075 }
3076 return null;
3077 }
3078
3079 boolean isLockedTask(TaskRecord task) {
3080 return mLockTaskModeTasks.contains(task);
3081 }
3082
3083 boolean isLastLockedTask(TaskRecord task) {
3084 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3085 }
3086
3087 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003088 if (!mLockTaskModeTasks.remove(task)) {
3089 return;
3090 }
3091 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3092 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003093 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003094 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3095 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003096 final Message lockTaskMsg = Message.obtain();
3097 lockTaskMsg.arg1 = task.userId;
3098 lockTaskMsg.what = LOCK_TASK_END_MSG;
3099 mHandler.sendMessage(lockTaskMsg);
3100 }
3101 }
3102
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003103 void showNonResizeableDockToast(int taskId) {
Chong Zhangc806d902015-11-30 09:44:27 -08003104 mWindowManager.scheduleShowNonResizeableDockToast(taskId);
Chong Zhangb15758a2015-11-17 12:12:03 -08003105 }
3106
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003107 void showLockTaskToast() {
3108 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04003109 }
3110
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003111 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3112 if (mLockTaskModeTasks.contains(task)) {
3113 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3114 }
3115 }
3116
Craig Mautner432f64e2015-05-20 14:59:57 -07003117 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3118 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003119 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003120 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003121 final TaskRecord lockedTask = getLockedTaskLocked();
3122 if (lockedTask != null) {
3123 removeLockedTaskLocked(lockedTask);
3124 if (!mLockTaskModeTasks.isEmpty()) {
3125 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003126 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3127 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003128 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003129 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003130 return;
3131 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003132 }
Craig Mautnere0570202015-05-13 13:06:11 -07003133 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3134 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003135 return;
3136 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003137
3138 // Should have already been checked, but do it again.
3139 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003140 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3141 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003142 return;
3143 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003144 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003145 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003146 return;
3147 }
3148
3149 if (mLockTaskModeTasks.isEmpty()) {
3150 // First locktask.
3151 final Message lockTaskMsg = Message.obtain();
3152 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3153 lockTaskMsg.arg1 = task.userId;
3154 lockTaskMsg.what = LOCK_TASK_START_MSG;
3155 lockTaskMsg.arg2 = lockTaskModeState;
3156 mHandler.sendMessage(lockTaskMsg);
3157 }
3158 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003159 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3160 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003161 mLockTaskModeTasks.remove(task);
3162 mLockTaskModeTasks.add(task);
3163
3164 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07003165 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07003166 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003167
3168 if (andResume) {
3169 findTaskToMoveToFrontLocked(task, 0, null, reason);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003170 resumeFocusedStackTopActivityLocked();
Craig Mautner432f64e2015-05-20 14:59:57 -07003171 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003172 }
3173
3174 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003175 return isLockTaskModeViolation(task, false);
3176 }
3177
3178 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3179 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003180 return false;
3181 }
3182 final int lockTaskAuth = task.mLockTaskAuth;
3183 switch (lockTaskAuth) {
3184 case LOCK_TASK_AUTH_DONT_LOCK:
3185 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003186 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003187 case LOCK_TASK_AUTH_LAUNCHABLE:
3188 case LOCK_TASK_AUTH_WHITELISTED:
3189 return false;
3190 case LOCK_TASK_AUTH_PINNABLE:
3191 // Pinnable tasks can't be launched on top of locktask tasks.
3192 return !mLockTaskModeTasks.isEmpty();
3193 default:
3194 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3195 return true;
3196 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003197 }
3198
Craig Mautner15df08a2015-04-01 12:17:18 -07003199 void onLockTaskPackagesUpdatedLocked() {
3200 boolean didSomething = false;
3201 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3202 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01003203 final boolean wasWhitelisted =
3204 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3205 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07003206 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01003207 final boolean isWhitelisted =
3208 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3209 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3210 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003211 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003212 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3213 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003214 removeLockedTaskLocked(lockedTask);
3215 lockedTask.performClearTaskLocked();
3216 didSomething = true;
3217 }
3218 }
3219 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3220 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3221 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3222 final ActivityStack stack = stacks.get(stackNdx);
3223 stack.onLockTaskPackagesUpdatedLocked();
3224 }
3225 }
Craig Mautnere0570202015-05-13 13:06:11 -07003226 final ActivityRecord r = topRunningActivityLocked();
3227 final TaskRecord task = r != null ? r.task : null;
3228 if (mLockTaskModeTasks.isEmpty() && task != null
3229 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3230 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07003231 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
3232 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
3233 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
3234 false);
3235 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07003236 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003237 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003238 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003239 }
3240 }
3241
Benjamin Franz43261142015-02-11 15:59:44 +00003242 int getLockTaskModeState() {
3243 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003244 }
3245
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003246 void logStackState() {
3247 mActivityMetricsLogger.logWindowState();
3248 }
3249
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003250 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003251
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003252 public ActivityStackSupervisorHandler(Looper looper) {
3253 super(looper);
3254 }
3255
Craig Mautnerf3333272013-04-22 10:55:53 -07003256 void activityIdleInternal(ActivityRecord r) {
3257 synchronized (mService) {
3258 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3259 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003260 }
3261
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003262 @Override
3263 public void handleMessage(Message msg) {
3264 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003265 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003266 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3267 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003268 if (mService.mDidDexOpt) {
3269 mService.mDidDexOpt = false;
3270 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3271 nmsg.obj = msg.obj;
3272 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3273 return;
3274 }
3275 // We don't at this point know if the activity is fullscreen,
3276 // so we need to be conservative and assume it isn't.
3277 activityIdleInternal((ActivityRecord)msg.obj);
3278 } break;
3279 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003280 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003281 activityIdleInternal((ActivityRecord)msg.obj);
3282 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003283 case RESUME_TOP_ACTIVITY_MSG: {
3284 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003285 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07003286 }
3287 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003288 case SLEEP_TIMEOUT_MSG: {
3289 synchronized (mService) {
3290 if (mService.isSleepingOrShuttingDown()) {
3291 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3292 mSleepTimeout = true;
3293 checkReadyForSleepLocked();
3294 }
3295 }
3296 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003297 case LAUNCH_TIMEOUT_MSG: {
3298 if (mService.mDidDexOpt) {
3299 mService.mDidDexOpt = false;
3300 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3301 return;
3302 }
3303 synchronized (mService) {
3304 if (mLaunchingActivity.isHeld()) {
3305 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3306 if (VALIDATE_WAKE_LOCK_CALLER
3307 && Binder.getCallingUid() != Process.myUid()) {
3308 throw new IllegalStateException("Calling must be system uid");
3309 }
3310 mLaunchingActivity.release();
3311 }
3312 }
3313 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003314 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003315 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003316 } break;
3317 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003318 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003319 } break;
3320 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003321 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003322 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003323 case CONTAINER_CALLBACK_VISIBILITY: {
3324 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003325 final IActivityContainerCallback callback = container.mCallback;
3326 if (callback != null) {
3327 try {
3328 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3329 } catch (RemoteException e) {
3330 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003331 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003332 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003333 case LOCK_TASK_START_MSG: {
3334 // When lock task starts, we disable the status bars.
3335 try {
Jason Monk62515be2014-05-21 16:06:19 -04003336 if (mLockTaskNotify == null) {
3337 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003338 }
Jason Monk62515be2014-05-21 16:06:19 -04003339 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003340 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003341 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003342 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003343 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003344 flags = StatusBarManager.DISABLE_MASK
3345 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07003346 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003347 flags = StatusBarManager.DISABLE_MASK
3348 & (~StatusBarManager.DISABLE_BACK)
3349 & (~StatusBarManager.DISABLE_HOME)
3350 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003351 }
3352 getStatusBarService().disable(flags, mToken,
3353 mService.mContext.getPackageName());
3354 }
3355 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003356 if (getDevicePolicyManager() != null) {
3357 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003358 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003359 }
justinzhang5286d3f2014-05-12 17:06:01 -04003360 } catch (RemoteException ex) {
3361 throw new RuntimeException(ex);
3362 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003363 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003364 case LOCK_TASK_END_MSG: {
3365 // When lock task ends, we enable the status bars.
3366 try {
Jason Monk62515be2014-05-21 16:06:19 -04003367 if (getStatusBarService() != null) {
3368 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3369 mService.mContext.getPackageName());
3370 }
3371 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003372 if (getDevicePolicyManager() != null) {
3373 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3374 msg.arg1);
3375 }
Jason Monk62515be2014-05-21 16:06:19 -04003376 if (mLockTaskNotify == null) {
3377 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3378 }
3379 mLockTaskNotify.show(false);
3380 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003381 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003382 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003383 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003384 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003385 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003386 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003387 new LockPatternUtils(mService.mContext)
3388 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003389 }
3390 } catch (SettingNotFoundException e) {
3391 // No setting, don't lock.
3392 }
justinzhang5286d3f2014-05-12 17:06:01 -04003393 } catch (RemoteException ex) {
3394 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003395 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07003396 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003397 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003398 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003399 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
3400 if (mLockTaskNotify == null) {
3401 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3402 }
3403 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
3404 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003405 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3406 final ActivityContainer container = (ActivityContainer) msg.obj;
3407 final IActivityContainerCallback callback = container.mCallback;
3408 if (callback != null) {
3409 try {
3410 callback.onAllActivitiesComplete(container.asBinder());
3411 } catch (RemoteException e) {
3412 }
3413 }
3414 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003415 case LAUNCH_TASK_BEHIND_COMPLETE: {
3416 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003417 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07003418 if (r != null) {
3419 handleLaunchTaskBehindCompleteLocked(r);
3420 }
3421 }
3422 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08003423
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003424 }
3425 }
3426 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003427
Ying Wangb081a592014-04-22 15:20:16 -07003428 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08003429 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
3430 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003431 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003432 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003433 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003434 ActivityRecord mParentActivity = null;
3435 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003436
Craig Mautnere3a00d72014-04-16 08:31:19 -07003437 boolean mVisible = true;
3438
Craig Mautner4a1cb222013-12-04 16:14:06 -08003439 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003440 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003441
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003442 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3443 final static int CONTAINER_STATE_NO_SURFACE = 1;
3444 final static int CONTAINER_STATE_FINISHING = 2;
3445 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3446
3447 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003448 synchronized (mService) {
3449 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003450 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003451 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003452 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003453 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003454 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003455
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003456 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003457 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003458 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08003459 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003460 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003461 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003462 }
3463
3464 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003465 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003466 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003467 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3468 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003469 return;
3470 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003471 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003472 }
3473 }
3474
3475 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003476 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003477 synchronized (mService) {
3478 if (mActivityDisplay != null) {
3479 return mActivityDisplay.mDisplayId;
3480 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003481 }
3482 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003483 }
3484
Jeff Brownca9bc702014-02-11 14:32:56 -08003485 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08003486 public int getStackId() {
3487 synchronized (mService) {
3488 return mStackId;
3489 }
3490 }
3491
3492 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003493 public boolean injectEvent(InputEvent event) {
3494 final long origId = Binder.clearCallingIdentity();
3495 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003496 synchronized (mService) {
3497 if (mActivityDisplay != null) {
3498 return mInputManagerInternal.injectInputEvent(event,
3499 mActivityDisplay.mDisplayId,
3500 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3501 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003502 }
3503 return false;
3504 } finally {
3505 Binder.restoreCallingIdentity(origId);
3506 }
3507 }
3508
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003509 @Override
3510 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003511 synchronized (mService) {
3512 if (mContainerState == CONTAINER_STATE_FINISHING) {
3513 return;
3514 }
3515 mContainerState = CONTAINER_STATE_FINISHING;
3516
Craig Mautnerd163e752014-06-13 17:18:47 -07003517 long origId = Binder.clearCallingIdentity();
3518 try {
Craig Mautneree36c772014-07-16 14:56:05 -07003519 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003520 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07003521 } finally {
3522 Binder.restoreCallingIdentity(origId);
3523 }
3524 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003525 }
3526
Craig Mautner60257702014-09-17 15:02:33 -07003527 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003528 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08003529 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08003530 if (mActivityDisplay != null) {
3531 mActivityDisplay.detachActivitiesLocked(mStack);
3532 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003533 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003534 }
3535 }
3536
3537 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08003538 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003539 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003540 }
3541
3542 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07003543 public final int startActivityIntentSender(IIntentSender intentSender)
3544 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003545 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3546
3547 if (!(intentSender instanceof PendingIntentRecord)) {
3548 throw new IllegalArgumentException("Bad PendingIntent object");
3549 }
3550
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07003551 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003552 Binder.getCallingUid(), mCurrentUser, false,
3553 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08003554
3555 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3556 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
3557 pendingIntent.key.requestResolvedType);
3558
3559 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
3560 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
3561 }
3562
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003563 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07003564 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003565 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003566 throw new SecurityException(
3567 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3568 }
3569 }
3570
Craig Mautnerdf88d732014-01-27 09:21:32 -08003571 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003572 public IBinder asBinder() {
3573 return this;
3574 }
3575
Craig Mautner4504de52013-12-20 09:06:56 -08003576 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003577 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003578 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08003579 }
3580
Craig Mautner4a1cb222013-12-04 16:14:06 -08003581 ActivityStackSupervisor getOuter() {
3582 return ActivityStackSupervisor.this;
3583 }
3584
Craig Mautnerd163e752014-06-13 17:18:47 -07003585 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003586 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003587 }
3588
Craig Mautner6985bad2014-04-21 15:22:06 -07003589 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07003590 void setVisible(boolean visible) {
3591 if (mVisible != visible) {
3592 mVisible = visible;
3593 if (mCallback != null) {
3594 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
3595 0 /* unused */, this).sendToTarget();
3596 }
3597 }
3598 }
3599
Craig Mautner6985bad2014-04-21 15:22:06 -07003600 void setDrawn() {
3601 }
3602
Craig Mautner1b4bf852014-05-26 15:06:32 -07003603 // You can always start a new task on a regular ActivityStack.
3604 boolean isEligibleForNewTasks() {
3605 return true;
3606 }
3607
Craig Mautnerd163e752014-06-13 17:18:47 -07003608 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07003609 detachLocked();
3610 deleteActivityContainer(this);
3611 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003612 }
3613
Craig Mautner34b73df2014-01-12 21:11:08 -08003614 @Override
3615 public String toString() {
3616 return mIdString + (mActivityDisplay == null ? "N" : "A");
3617 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003618 }
3619
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003620 private class VirtualActivityContainer extends ActivityContainer {
3621 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07003622 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003623
3624 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
3625 super(getNextStackId());
3626 mParentActivity = parent;
3627 mCallback = callback;
3628 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07003629 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003630 }
3631
3632 @Override
3633 public void setSurface(Surface surface, int width, int height, int density) {
3634 super.setSurface(surface, width, height, density);
3635
3636 synchronized (mService) {
3637 final long origId = Binder.clearCallingIdentity();
3638 try {
3639 setSurfaceLocked(surface, width, height, density);
3640 } finally {
3641 Binder.restoreCallingIdentity(origId);
3642 }
3643 }
3644 }
3645
3646 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
3647 if (mContainerState == CONTAINER_STATE_FINISHING) {
3648 return;
3649 }
3650 VirtualActivityDisplay virtualActivityDisplay =
3651 (VirtualActivityDisplay) mActivityDisplay;
3652 if (virtualActivityDisplay == null) {
3653 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07003654 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003655 mActivityDisplay = virtualActivityDisplay;
3656 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003657 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003658 }
3659
3660 if (mSurface != null) {
3661 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003662 }
3663
Craig Mautner6985bad2014-04-21 15:22:06 -07003664 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003665 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003666 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003667 } else {
3668 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07003669 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003670 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003671 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003672 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003673 }
Craig Mautner6985bad2014-04-21 15:22:06 -07003674
Craig Mautnerd163e752014-06-13 17:18:47 -07003675 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003676
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003677 if (DEBUG_STACK) Slog.d(TAG_STACK,
3678 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07003679 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003680
Craig Mautner6985bad2014-04-21 15:22:06 -07003681 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07003682 boolean isAttachedLocked() {
3683 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07003684 }
3685
3686 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07003687 void setDrawn() {
3688 synchronized (mService) {
3689 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07003690 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003691 }
3692 }
3693
Craig Mautner1b4bf852014-05-26 15:06:32 -07003694 // Never start a new task on an ActivityView if it isn't explicitly specified.
3695 @Override
3696 boolean isEligibleForNewTasks() {
3697 return false;
3698 }
3699
Craig Mautnerd163e752014-06-13 17:18:47 -07003700 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003701 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07003702 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
3703 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
3704 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
3705 mContainerState = CONTAINER_STATE_HAS_SURFACE;
3706 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003707 }
3708 }
3709
Craig Mautner4a1cb222013-12-04 16:14:06 -08003710 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
3711 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003712 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003713 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08003714 int mDisplayId;
3715 Display mDisplay;
3716 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08003717
Craig Mautner4a1cb222013-12-04 16:14:06 -08003718 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
3719 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08003720 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003721
Jose Lima4b6c6692014-08-12 17:41:12 -07003722 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003723
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003724 ActivityDisplay() {
3725 }
Craig Mautner4504de52013-12-20 09:06:56 -08003726
Craig Mautner1a70a162014-09-13 12:09:31 -07003727 // After instantiation, check that mDisplay is not null before using this. The alternative
3728 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08003729 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07003730 final Display display = mDisplayManager.getDisplay(displayId);
3731 if (display == null) {
3732 return;
3733 }
3734 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08003735 }
3736
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003737 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08003738 mDisplay = display;
3739 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003740 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08003741 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003742
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003743 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003744 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003745 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
3746 + " onTop=" + onTop);
3747 if (onTop) {
3748 mStacks.add(stack);
3749 } else {
3750 mStacks.add(0, stack);
3751 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003752 }
3753
Craig Mautnere0a38842013-12-16 16:14:02 -08003754 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003755 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08003756 + " from displayId=" + mDisplayId);
3757 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003758 }
3759
Jose Lima4b6c6692014-08-12 17:41:12 -07003760 void setVisibleBehindActivity(ActivityRecord r) {
3761 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003762 }
3763
Jose Lima4b6c6692014-08-12 17:41:12 -07003764 boolean hasVisibleBehindActivity() {
3765 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003766 }
3767
Craig Mautner34b73df2014-01-12 21:11:08 -08003768 @Override
3769 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003770 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
3771 }
3772 }
3773
3774 class VirtualActivityDisplay extends ActivityDisplay {
3775 VirtualDisplay mVirtualDisplay;
3776
Craig Mautner6985bad2014-04-21 15:22:06 -07003777 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003778 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07003779 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
3780 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
3781 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
3782 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003783
3784 init(mVirtualDisplay.getDisplay());
3785
3786 mWindowManager.handleDisplayAdded(mDisplayId);
3787 }
3788
3789 void setSurface(Surface surface) {
3790 if (mVirtualDisplay != null) {
3791 mVirtualDisplay.setSurface(surface);
3792 }
3793 }
3794
3795 @Override
3796 void detachActivitiesLocked(ActivityStack stack) {
3797 super.detachActivitiesLocked(stack);
3798 if (mVirtualDisplay != null) {
3799 mVirtualDisplay.release();
3800 mVirtualDisplay = null;
3801 }
3802 }
3803
3804 @Override
3805 public String toString() {
3806 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08003807 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003808 }
Jose Lima58e66d62014-05-27 20:00:27 -07003809
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07003810 /**
3811 * Adjust bounds to stay within stack bounds.
3812 *
3813 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
3814 * that keep them unchanged, but be contained within the stack bounds.
3815 *
3816 * @param bounds Bounds to be adjusted.
3817 * @param stackBounds Bounds within which the other bounds should remain.
3818 */
3819 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
3820 if (stackBounds == null || stackBounds.contains(bounds)) {
3821 return;
3822 }
3823
3824 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
3825 final int maxRight = stackBounds.right
3826 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
3827 int horizontalDiff = stackBounds.left - bounds.left;
3828 if ((horizontalDiff < 0 && bounds.left >= maxRight)
3829 || (bounds.left + horizontalDiff >= maxRight)) {
3830 horizontalDiff = maxRight - bounds.left;
3831 }
3832 bounds.left += horizontalDiff;
3833 bounds.right += horizontalDiff;
3834 }
3835
3836 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
3837 final int maxBottom = stackBounds.bottom
3838 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
3839 int verticalDiff = stackBounds.top - bounds.top;
3840 if ((verticalDiff < 0 && bounds.top >= maxBottom)
3841 || (bounds.top + verticalDiff >= maxBottom)) {
3842 verticalDiff = maxBottom - bounds.top;
3843 }
3844 bounds.top += verticalDiff;
3845 bounds.bottom += verticalDiff;
3846 }
3847 }
3848
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08003849 ActivityStack findStackBehind(ActivityStack stack) {
3850 // TODO(multi-display): We are only looking for stacks on the default display.
3851 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
3852 if (display == null) {
3853 return null;
3854 }
3855 final ArrayList<ActivityStack> stacks = display.mStacks;
3856 for (int i = stacks.size() - 1; i >= 0; i--) {
3857 if (stacks.get(i) == stack && i > 0) {
3858 return stacks.get(i - 1);
3859 }
3860 }
3861 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
3862 + " in=" + stacks);
3863 }
Craig Mautner27084302013-03-25 08:05:25 -07003864}