blob: 32671acfcc30e14a8100cd43fffb6d390063b8ec [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
Craig Mautner42d04db2014-11-06 12:13:23 -0800373 /** Used to keep resumeTopActivityLocked() from being entered recursively */
374 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 Ogunwaleddc1cb22015-07-25 19:23:04 -0700495 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800496 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
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
540 void setFocusStack(String reason, ActivityStack focusedStack) {
541 mLastFocusedStack = mFocusedStack;
542 mFocusedStack = focusedStack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800543
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700544 EventLogTags.writeAmFocusedStack(
545 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
546 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
Craig Mautnerde313752015-01-22 14:28:03 -0800547
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800548 if (mService.mBooting || !mService.mBooted) {
549 final ActivityRecord r = topRunningActivityLocked();
550 if (r != null && r.idle) {
551 checkFinishBootingLocked();
552 }
553 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700554 }
555
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700556 void moveHomeStackToFront(String reason) {
557 mHomeStack.moveToFront(reason);
558 }
559
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700560 /** Returns true if the focus activity was adjusted to the home stack top activity. */
561 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700562 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
563 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700564 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700565 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700566
Craig Mautner84984fa2014-06-19 11:19:20 -0700567 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700568
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700569 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700570 if (top == null) {
571 return false;
572 }
573 mService.setFocusedActivityLocked(top, reason);
574 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700575 }
576
Craig Mautner299f9602015-01-26 09:47:33 -0800577 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700578 if (!mService.mBooting && !mService.mBooted) {
579 // Not ready yet!
580 return false;
581 }
582
Craig Mautner84984fa2014-06-19 11:19:20 -0700583 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
584 mWindowManager.showRecentApps();
585 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700586 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700587
Craig Mautner84984fa2014-06-19 11:19:20 -0700588 if (prev != null) {
589 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
590 }
591
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700592 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
593 ActivityRecord r = getHomeActivity();
Mark Lua56ea122015-10-08 13:31:01 +0800594 // Only resume home activity if isn't finishing.
595 if (r != null && !r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -0800596 mService.setFocusedActivityLocked(r, reason);
Craig Mautner05d29032013-05-03 13:40:13 -0700597 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700598 }
Craig Mautner299f9602015-01-26 09:47:33 -0800599 return mService.startHomeActivityLocked(mCurrentUser, reason);
Craig Mautner69ada552013-04-18 13:51:51 -0700600 }
601
Craig Mautner8d341ef2013-03-26 09:03:27 -0700602 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700603 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700604 }
605
606 /**
607 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
608 * @param id Id of the task we would like returned.
609 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
610 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700611 * @param stackId The stack to restore the task to (default launch stack will be used if
612 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700613 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700614 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800615 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800616 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800617 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800618 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
619 ActivityStack stack = stacks.get(stackNdx);
620 TaskRecord task = stack.taskForIdLocked(id);
621 if (task != null) {
622 return task;
623 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700624 }
625 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800626
627 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700628 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800629 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800630 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700631 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800632 return null;
633 }
634
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700635 if (!restoreFromRecents) {
636 return task;
637 }
638
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700639 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700640 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
641 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800642 return null;
643 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700644 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800645 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700646 }
647
Craig Mautner6170f732013-04-02 13:05:23 -0700648 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800649 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800650 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800651 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800652 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
653 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
654 if (r != null) {
655 return r;
656 }
Craig Mautner6170f732013-04-02 13:05:23 -0700657 }
658 }
659 return null;
660 }
661
Craig Mautneref73ee12014-04-23 11:45:37 -0700662 void setNextTaskId(int taskId) {
663 if (taskId > mCurTaskId) {
664 mCurTaskId = taskId;
665 }
666 }
667
Craig Mautner8d341ef2013-03-26 09:03:27 -0700668 int getNextTaskId() {
669 do {
670 mCurTaskId++;
671 if (mCurTaskId <= 0) {
672 mCurTaskId = 1;
673 }
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700674 } while (anyTaskForIdLocked(mCurTaskId, !RESTORE_FROM_RECENTS, INVALID_STACK_ID) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700675 return mCurTaskId;
676 }
677
Craig Mautnerde4ef022013-04-07 19:01:33 -0700678 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800679 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700680 if (stack == null) {
681 return null;
682 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700683 ActivityRecord resumedActivity = stack.mResumedActivity;
684 if (resumedActivity == null || resumedActivity.app == null) {
685 resumedActivity = stack.mPausingActivity;
686 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700687 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700688 }
689 }
690 return resumedActivity;
691 }
692
Dianne Hackbornff072722014-09-24 10:56:28 -0700693 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700694 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800695 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800696 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
697 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800698 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
699 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700700 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800701 continue;
702 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700703 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800704 if (hr != null) {
705 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
706 && processName.equals(hr.processName)) {
707 try {
George Mount2c92c972014-03-20 09:38:23 -0700708 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800709 didSomething = true;
710 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700711 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800712 Slog.w(TAG, "Exception in new application when starting activity "
713 + hr.intent.getComponent().flattenToShortString(), e);
714 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700715 }
Craig Mautner20e72272013-04-01 13:45:53 -0700716 }
Craig Mautner20e72272013-04-01 13:45:53 -0700717 }
718 }
719 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700720 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700721 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700722 }
Craig Mautner20e72272013-04-01 13:45:53 -0700723 return didSomething;
724 }
725
726 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800727 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
728 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800729 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
730 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700731 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800732 continue;
733 }
734 final ActivityRecord resumedActivity = stack.mResumedActivity;
735 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700736 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800737 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800738 return false;
739 }
Craig Mautner20e72272013-04-01 13:45:53 -0700740 }
741 }
742 return true;
743 }
744
Craig Mautnerde4ef022013-04-07 19:01:33 -0700745 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800746 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
747 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800748 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
749 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700750 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800751 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700752 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800753 return false;
754 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700755 }
756 }
757 }
758 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700759 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800760 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
761 mLastFocusedStack + " to=" + mFocusedStack);
762 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700763 return true;
764 }
765
766 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800767 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800768 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
769 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800770 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
771 final ActivityStack stack = stacks.get(stackNdx);
772 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800773 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700774 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800775 return false;
776 }
777 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800778 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700779 }
780 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800781 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700782 }
783
Craig Mautner2acc3892013-09-23 10:28:14 -0700784 /**
785 * Pause all activities in either all of the stacks or just the back stacks.
786 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700787 * @return true if any activity was paused as a result of this call.
788 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700789 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700790 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800791 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
792 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800793 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
794 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700795 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700796 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800797 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700798 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
799 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800800 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700801 }
802 }
803 return someActivityPaused;
804 }
805
Craig Mautnerde4ef022013-04-07 19:01:33 -0700806 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700807 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800808 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
809 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800810 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
811 final ActivityStack stack = stacks.get(stackNdx);
812 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700813 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800814 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700815 Slog.d(TAG_STATES,
816 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800817 pausing = false;
818 } else {
819 return false;
820 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700821 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700822 }
823 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700824 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700825 }
826
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700827 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
828 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500829 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800830 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
831 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
832 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
833 final ActivityStack stack = stacks.get(stackNdx);
834 if (stack.mResumedActivity != null &&
835 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700836 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800837 }
838 }
839 }
840 }
841
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700842 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700843 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700844 }
845
846 void sendWaitingVisibleReportLocked(ActivityRecord r) {
847 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700848 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700849 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700850 if (w.who == null) {
851 changed = true;
852 w.timeout = false;
853 if (r != null) {
854 w.who = new ComponentName(r.info.packageName, r.info.name);
855 }
856 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
857 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700858 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700859 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700860 if (changed) {
861 mService.notifyAll();
862 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700863 }
864
865 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
866 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700867 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700868 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700869 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700870 if (w.who == null) {
871 changed = true;
872 w.timeout = timeout;
873 if (r != null) {
874 w.who = new ComponentName(r.info.packageName, r.info.name);
875 }
876 w.thisTime = thisTime;
877 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700878 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700879 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700880 if (changed) {
881 mService.notifyAll();
882 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700883 }
884
Craig Mautner29219d92013-04-16 20:19:12 -0700885 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800886 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700887 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -0700888 if (r != null) {
889 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700890 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700891
Craig Mautner4a1cb222013-12-04 16:14:06 -0800892 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800893 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800894 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
895 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700896 if (stack != focusedStack && isFrontStack(stack)) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700897 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -0700898 if (r != null) {
899 return r;
900 }
901 }
902 }
903 return null;
904 }
905
Dianne Hackborn09233282014-04-30 11:33:59 -0700906 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700907 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800908 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
909 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800910 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800911 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800912 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800913 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
914 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700915 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800916 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700917 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700918 }
919 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700920
921 // The lists are already sorted from most recent to oldest. Just pull the most recent off
922 // each list and add it to list. Stop when all lists are empty or maxNum reached.
923 while (maxNum > 0) {
924 long mostRecentActiveTime = Long.MIN_VALUE;
925 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800926 final int numTaskLists = runningTaskLists.size();
927 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
928 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700929 if (!stackTaskList.isEmpty()) {
930 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
931 if (lastActiveTime > mostRecentActiveTime) {
932 mostRecentActiveTime = lastActiveTime;
933 selectedStackList = stackTaskList;
934 }
935 }
936 }
937 if (selectedStackList != null) {
938 list.add(selectedStackList.remove(0));
939 --maxNum;
940 } else {
941 break;
942 }
943 }
Craig Mautner20e72272013-04-01 13:45:53 -0700944 }
945
Todd Kennedy7440f172015-12-09 14:31:22 -0800946 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
947 ProfilerInfo profilerInfo) {
948 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700949 if (aInfo != null) {
950 // Store the found target back into the intent, because now that
951 // we have it we never want to do this again. For example, if the
952 // user navigates back to this point in the history, we should
953 // always restart the exact same activity.
954 intent.setComponent(new ComponentName(
955 aInfo.applicationInfo.packageName, aInfo.name));
956
957 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -0700958 if (!aInfo.processName.equals("system")) {
959 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700960 mService.setDebugApp(aInfo.processName, true, false);
961 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700962
Man Caocfa78b22015-06-11 20:14:34 -0700963 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
964 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
965 }
966
967 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700968 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700969 }
970 }
971 }
972 return aInfo;
973 }
974
Todd Kennedy7440f172015-12-09 14:31:22 -0800975 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
976 try {
977 return AppGlobals.getPackageManager().resolveIntent(intent, resolvedType,
978 PackageManager.MATCH_DEFAULT_ONLY
979 | ActivityManagerService.STOCK_PM_FLAGS, userId);
980 } catch (RemoteException e) {
981 }
982 return null;
983 }
984
985 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
986 ProfilerInfo profilerInfo, int userId) {
987 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
988 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
989 }
990
Craig Mautner2420ead2013-04-01 17:13:20 -0700991 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -0700992 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -0700993 throws RemoteException {
994
Craig Mautner2568c3a2015-03-26 14:22:34 -0700995 if (andResume) {
996 r.startFreezingScreenLocked(app, 0);
997 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -0700998
Craig Mautner2568c3a2015-03-26 14:22:34 -0700999 // schedule launch ticks to collect information about slow apps.
1000 r.startLaunchTickingLocked();
1001 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001002
1003 // Have the window manager re-evaluate the orientation of
1004 // the screen based on the new activity order. Note that
1005 // as a result of this, it can call back into the activity
1006 // manager with a new orientation. We don't care about that,
1007 // because the activity is not currently running so we are
1008 // just restarting it anyway.
1009 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001010 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001011 mService.mConfiguration,
1012 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001013 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001014 }
1015
1016 r.app = app;
1017 app.waitingToKill = null;
1018 r.launchCount++;
1019 r.lastLaunchTime = SystemClock.uptimeMillis();
1020
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001021 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001022
1023 int idx = app.activities.indexOf(r);
1024 if (idx < 0) {
1025 app.activities.add(r);
1026 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001027 mService.updateLruProcessLocked(app, true, null);
1028 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001029
Craig Mautner15df08a2015-04-01 12:17:18 -07001030 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001031 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1032 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001033 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001034 }
1035
1036 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001037 try {
1038 if (app.thread == null) {
1039 throw new RemoteException();
1040 }
1041 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001042 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001043 if (andResume) {
1044 results = r.results;
1045 newIntents = r.newIntents;
1046 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001047 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1048 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1049 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001050 if (andResume) {
1051 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1052 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001053 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001054 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001055 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001056 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001057 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001058 }
Nicolas Geoffray27c07372015-11-05 16:54:09 +00001059 mService.notifyPackageUse(r.intent.getComponent().getPackageName());
Craig Mautner2420ead2013-04-01 17:13:20 -07001060 r.sleeping = false;
1061 r.forceNewConfig = false;
1062 mService.showAskCompatModeDialogLocked(r);
1063 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001064 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001065 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1066 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1067 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001068 final String profileFile = mService.mProfileFile;
1069 if (profileFile != null) {
1070 ParcelFileDescriptor profileFd = mService.mProfileFd;
1071 if (profileFd != null) {
1072 try {
1073 profileFd = profileFd.dup();
1074 } catch (IOException e) {
1075 if (profileFd != null) {
1076 try {
1077 profileFd.close();
1078 } catch (IOException o) {
1079 }
1080 profileFd = null;
1081 }
1082 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001083 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001084
1085 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1086 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001087 }
1088 }
1089 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001090
Craig Mautner2568c3a2015-03-26 14:22:34 -07001091 if (andResume) {
1092 app.hasShownUi = true;
1093 app.pendingUiClean = true;
1094 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001095 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001096 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001097 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001098 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001099 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001100 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001101
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001102 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001103 // This may be a heavy-weight process! Note that the package
1104 // manager will ensure that only activity can run in the main
1105 // process of the .apk, which is the only thing that will be
1106 // considered heavy-weight.
1107 if (app.processName.equals(app.info.packageName)) {
1108 if (mService.mHeavyWeightProcess != null
1109 && mService.mHeavyWeightProcess != app) {
1110 Slog.w(TAG, "Starting new heavy weight process " + app
1111 + " when already running "
1112 + mService.mHeavyWeightProcess);
1113 }
1114 mService.mHeavyWeightProcess = app;
1115 Message msg = mService.mHandler.obtainMessage(
1116 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1117 msg.obj = r;
1118 mService.mHandler.sendMessage(msg);
1119 }
1120 }
1121
1122 } catch (RemoteException e) {
1123 if (r.launchFailed) {
1124 // This is the second time we failed -- finish activity
1125 // and give up.
1126 Slog.e(TAG, "Second failure launching "
1127 + r.intent.getComponent().flattenToShortString()
1128 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001129 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001130 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1131 "2nd-crash", false);
1132 return false;
1133 }
1134
1135 // This is the first time we failed -- restart process and
1136 // retry.
1137 app.activities.remove(r);
1138 throw e;
1139 }
1140
1141 r.launchFailed = false;
1142 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001143 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001144 }
1145
1146 if (andResume) {
1147 // As part of the process of launching, ActivityThread also performs
1148 // a resume.
1149 stack.minimalResumeActivityLocked(r);
1150 } else {
1151 // This activity is not starting in the resumed state... which
1152 // should look like we asked it to pause+stop (but remain visible),
1153 // and it has done so and reported back the current icicle and
1154 // other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001155 if (DEBUG_STATES) Slog.v(TAG_STATES,
1156 "Moving to STOPPED: " + r + " (starting in stopped state)");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001157 r.state = STOPPED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001158 r.stopped = true;
1159 }
1160
1161 // Launch the new version setup screen if needed. We do this -after-
1162 // launching the initial activity (that is, home), so that it can have
1163 // a chance to initialize itself while in the background, making the
1164 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001165 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001166 mService.startSetupActivityLocked();
1167 }
1168
Dianne Hackborn465fa392014-09-14 14:21:18 -07001169 // Update any services we are bound to that might care about whether
1170 // their client may have activities.
1171 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1172
Craig Mautner2420ead2013-04-01 17:13:20 -07001173 return true;
1174 }
1175
Craig Mautnere79d42682013-04-01 19:01:53 -07001176 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001177 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001178 // Is this activity's application already running?
1179 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001180 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001181
1182 r.task.stack.setLaunchTime(r);
1183
1184 if (app != null && app.thread != null) {
1185 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001186 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1187 || !"android".equals(r.info.packageName)) {
1188 // Don't add this if it is a platform component that is marked
1189 // to run in multiple processes, because this is actually
1190 // part of the framework so doesn't make sense to track as a
1191 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001192 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1193 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001194 }
George Mount2c92c972014-03-20 09:38:23 -07001195 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001196 return;
1197 } catch (RemoteException e) {
1198 Slog.w(TAG, "Exception when starting activity "
1199 + r.intent.getComponent().flattenToShortString(), e);
1200 }
1201
1202 // If a dead object exception was thrown -- fall through to
1203 // restart the application.
1204 }
1205
1206 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001207 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001208 }
1209
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001210 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001211 String resultWho, int requestCode, int callingPid, int callingUid,
1212 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
1213 ActivityRecord resultRecord, ActivityStack resultStack) {
1214 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1215 callingUid);
1216 if (startAnyPerm == PERMISSION_GRANTED) {
1217 return true;
1218 }
1219 final int componentRestriction = getComponentRestrictionForCallingPackage(
1220 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1221 final int actionRestriction = getActionRestrictionForCallingPackage(
1222 intent.getAction(), callingPackage, callingPid, callingUid);
1223 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1224 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1225 if (resultRecord != null) {
1226 resultStack.sendActivityResultLocked(-1,
1227 resultRecord, resultWho, requestCode,
1228 Activity.RESULT_CANCELED, null);
1229 }
1230 final String msg;
1231 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1232 msg = "Permission Denial: starting " + intent.toString()
1233 + " from " + callerApp + " (pid=" + callingPid
1234 + ", uid=" + callingUid + ")" + " with revoked permission "
1235 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1236 } else if (!aInfo.exported) {
1237 msg = "Permission Denial: starting " + intent.toString()
1238 + " from " + callerApp + " (pid=" + callingPid
1239 + ", uid=" + callingUid + ")"
1240 + " not exported from uid " + aInfo.applicationInfo.uid;
1241 } else {
1242 msg = "Permission Denial: starting " + intent.toString()
1243 + " from " + callerApp + " (pid=" + callingPid
1244 + ", uid=" + callingUid + ")"
1245 + " requires " + aInfo.permission;
1246 }
1247 Slog.w(TAG, msg);
1248 throw new SecurityException(msg);
1249 }
1250
1251 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1252 final String message = "Appop Denial: starting " + intent.toString()
1253 + " from " + callerApp + " (pid=" + callingPid
1254 + ", uid=" + callingUid + ")"
1255 + " requires " + AppOpsManager.permissionToOp(
1256 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1257 Slog.w(TAG, message);
1258 return false;
1259 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1260 final String message = "Appop Denial: starting " + intent.toString()
1261 + " from " + callerApp + " (pid=" + callingPid
1262 + ", uid=" + callingUid + ")"
1263 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1264 Slog.w(TAG, message);
1265 return false;
1266 }
1267 return true;
1268 }
1269
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001270 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001271 final long identity = Binder.clearCallingIdentity();
1272 try {
1273 return UserManager.get(mService.mContext).getUserInfo(userId);
1274 } finally {
1275 Binder.restoreCallingIdentity(identity);
1276 }
1277 }
1278
Svet Ganov99b60432015-06-27 13:15:22 -07001279 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001280 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001281 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1282 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001283 == PackageManager.PERMISSION_DENIED) {
1284 return ACTIVITY_RESTRICTION_PERMISSION;
1285 }
1286
Christopher Tateff7add02015-08-17 10:23:22 -07001287 if (activityInfo.permission == null) {
1288 return ACTIVITY_RESTRICTION_NONE;
1289 }
1290
Svet Ganov99b60432015-06-27 13:15:22 -07001291 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1292 if (opCode == AppOpsManager.OP_NONE) {
1293 return ACTIVITY_RESTRICTION_NONE;
1294 }
1295
1296 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1297 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001298 if (!ignoreTargetSecurity) {
1299 return ACTIVITY_RESTRICTION_APPOP;
1300 }
Svet Ganov99b60432015-06-27 13:15:22 -07001301 }
1302
1303 return ACTIVITY_RESTRICTION_NONE;
1304 }
1305
Svetoslav7008b512015-06-24 18:47:07 -07001306 private int getActionRestrictionForCallingPackage(String action,
1307 String callingPackage, int callingPid, int callingUid) {
1308 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001309 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001310 }
1311
1312 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1313 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001314 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001315 }
1316
1317 final PackageInfo packageInfo;
1318 try {
1319 packageInfo = mService.mContext.getPackageManager()
1320 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1321 } catch (PackageManager.NameNotFoundException e) {
1322 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001323 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001324 }
1325
1326 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001327 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001328 }
1329
1330 if (mService.checkPermission(permission, callingPid, callingUid) ==
1331 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001332 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001333 }
1334
1335 final int opCode = AppOpsManager.permissionToOpCode(permission);
1336 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001337 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001338 }
1339
1340 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1341 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001342 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001343 }
1344
Svet Ganov99b60432015-06-27 13:15:22 -07001345 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001346 }
1347
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001348 boolean setFocusedStack(ActivityRecord r, String reason) {
1349 if (r == null) {
1350 // Not sure what you are trying to do, but it is not going to work...
1351 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001352 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001353 final TaskRecord task = r.task;
1354 if (task == null || task.stack == null) {
1355 Slog.w(TAG, "Can't set focus stack for r=" + r + " task=" + task);
1356 return false;
1357 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001358 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001359 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001360 }
1361
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001362 Rect getOverrideBounds(ActivityRecord r, ActivityOptions options, TaskRecord inTask) {
Chong Zhang0fa656b2015-08-31 15:17:21 -07001363 Rect newBounds = null;
Chong Zhang56f171d2015-10-08 10:39:23 -07001364 if (options != null && (r.info.resizeable || (inTask != null && inTask.mResizeable))) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001365 if (canUseActivityOptionsLaunchBounds(options)) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001366 newBounds = options.getLaunchBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07001367 }
1368 }
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001369 return newBounds;
Craig Mautneree36c772014-07-16 14:56:05 -07001370 }
1371
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001372 void setLaunchSource(int uid) {
1373 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1374 }
1375
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001376 void acquireLaunchWakelock() {
1377 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1378 throw new IllegalStateException("Calling must be system uid");
1379 }
1380 mLaunchingActivity.acquire();
1381 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1382 // To be safe, don't allow the wake lock to be held for too long.
1383 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1384 }
1385 }
1386
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001387 /**
1388 * Called when the frontmost task is idle.
1389 * @return the state of mService.mBooting before this was called.
1390 */
1391 private boolean checkFinishBootingLocked() {
1392 final boolean booting = mService.mBooting;
1393 boolean enableScreen = false;
1394 mService.mBooting = false;
1395 if (!mService.mBooted) {
1396 mService.mBooted = true;
1397 enableScreen = true;
1398 }
1399 if (booting || enableScreen) {
1400 mService.postFinishBooting(booting, enableScreen);
1401 }
1402 return booting;
1403 }
1404
Craig Mautnerf3333272013-04-22 10:55:53 -07001405 // Checked.
1406 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
1407 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001408 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001409
Craig Mautnerf3333272013-04-22 10:55:53 -07001410 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001411 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001412 int NS = 0;
1413 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001414 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001415 boolean activityRemoved = false;
1416
Wale Ogunwale7d701172015-03-11 15:36:30 -07001417 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001418 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001419 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1420 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001421 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1422 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001423 if (fromTimeout) {
1424 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001425 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001426
1427 // This is a hack to semi-deal with a race condition
1428 // in the client where it can be constructed with a
1429 // newer configuration from when we asked it to launch.
1430 // We'll update with whatever configuration it now says
1431 // it used to launch.
1432 if (config != null) {
1433 r.configuration = config;
1434 }
1435
1436 // We are now idle. If someone is waiting for a thumbnail from
1437 // us, we can now deliver.
1438 r.idle = true;
1439
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001440 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001441 if (isFocusedStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001442 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001443 }
1444 }
1445
1446 if (allResumedActivitiesIdle()) {
1447 if (r != null) {
1448 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001449 }
1450
1451 if (mLaunchingActivity.isHeld()) {
1452 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1453 if (VALIDATE_WAKE_LOCK_CALLER &&
1454 Binder.getCallingUid() != Process.myUid()) {
1455 throw new IllegalStateException("Calling must be system uid");
1456 }
1457 mLaunchingActivity.release();
1458 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001459 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001460 }
1461
1462 // Atomically retrieve all of the other things to do.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001463 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(true);
Craig Mautnerf3333272013-04-22 10:55:53 -07001464 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001465 if ((NF = mFinishingActivities.size()) > 0) {
1466 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001467 mFinishingActivities.clear();
1468 }
1469
Craig Mautnerf3333272013-04-22 10:55:53 -07001470 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001471 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001472 mStartingUsers.clear();
1473 }
1474
Craig Mautnerf3333272013-04-22 10:55:53 -07001475 // Stop any activities that are scheduled to do so but have been
1476 // waiting for the next one to start.
1477 for (int i = 0; i < NS; i++) {
1478 r = stops.get(i);
1479 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001480 if (stack != null) {
1481 if (r.finishing) {
1482 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1483 } else {
1484 stack.stopActivityLocked(r);
1485 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001486 }
1487 }
1488
1489 // Finish 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 < NF; i++) {
1492 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07001493 final ActivityStack stack = r.task.stack;
1494 if (stack != null) {
1495 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1496 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001497 }
1498
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001499 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001500 // Complete user switch
1501 if (startingUsers != null) {
1502 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001503 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001504 }
1505 }
1506 // Complete starting up of background users
1507 if (mStartingBackgroundUsers.size() > 0) {
Amith Yamasani37a40c22015-06-17 13:25:42 -07001508 startingUsers = new ArrayList<UserState>(mStartingBackgroundUsers);
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001509 mStartingBackgroundUsers.clear();
1510 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001511 mService.mUserController.finishUserBoot(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001512 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001513 }
1514 }
1515
1516 mService.trimApplications();
1517 //dump();
1518 //mWindowManager.dump();
1519
Craig Mautnerf3333272013-04-22 10:55:53 -07001520 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07001521 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001522 }
1523
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001524 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001525 }
1526
Craig Mautner8e569572013-10-11 17:36:59 -07001527 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001528 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001529 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1530 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001531 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1532 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1533 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001534 }
Craig Mautner19091252013-10-05 00:03:53 -07001535 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001536 }
1537
1538 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001539 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1540 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001541 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1542 stacks.get(stackNdx).closeSystemDialogsLocked();
1543 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001544 }
1545 }
1546
Craig Mautner93529a42013-10-04 15:03:13 -07001547 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001548 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001549 }
1550
Craig Mautner8d341ef2013-03-26 09:03:27 -07001551 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001552 * Update the last used stack id for non-current user (current user's last
1553 * used stack is the focused stack)
1554 */
1555 void updateUserStackLocked(int userId, ActivityStack stack) {
1556 if (userId != mCurrentUser) {
1557 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1558 }
1559 }
1560
1561 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001562 * @return true if some activity was finished (or would have finished if doit were true).
1563 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001564 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1565 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001566 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001567 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1568 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001569 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001570 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001571 if (stack.finishDisabledPackageActivitiesLocked(
1572 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001573 didSomething = true;
1574 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001575 }
1576 }
1577 return didSomething;
1578 }
1579
Dianne Hackborna413dc02013-07-12 12:02:55 -07001580 void updatePreviousProcessLocked(ActivityRecord r) {
1581 // Now that this process has stopped, we may want to consider
1582 // it to be the previous app to try to keep around in case
1583 // the user wants to return to it.
1584
1585 // First, found out what is currently the foreground app, so that
1586 // we don't blow away the previous app if this activity is being
1587 // hosted by the process that is actually still the foreground.
1588 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001589 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1590 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001591 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1592 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001593 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001594 if (stack.mResumedActivity != null) {
1595 fgApp = stack.mResumedActivity.app;
1596 } else if (stack.mPausingActivity != null) {
1597 fgApp = stack.mPausingActivity.app;
1598 }
1599 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001600 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001601 }
1602 }
1603
1604 // Now set this one as the previous process, only if that really
1605 // makes sense to.
1606 if (r.app != null && fgApp != null && r.app != fgApp
1607 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001608 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001609 mService.mPreviousProcess = r.app;
1610 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1611 }
1612 }
1613
Craig Mautner05d29032013-05-03 13:40:13 -07001614 boolean resumeTopActivitiesLocked() {
1615 return resumeTopActivitiesLocked(null, null, null);
1616 }
1617
Chong Zhang280d3322015-11-03 17:27:26 -08001618 boolean resumeTopActivitiesLocked(
1619 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Craig Mautner05d29032013-05-03 13:40:13 -07001620 if (targetStack == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001621 targetStack = mFocusedStack;
Craig Mautner05d29032013-05-03 13:40:13 -07001622 }
Craig Mautner12ff7392014-02-21 21:08:00 -08001623 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07001624 boolean result = false;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001625 if (isFocusedStack(targetStack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08001626 result = targetStack.resumeTopActivityLocked(target, targetOptions);
1627 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001628
Craig Mautnere0a38842013-12-16 16:14:02 -08001629 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1630 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001631 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1632 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08001633 if (stack == targetStack) {
1634 // Already started above.
1635 continue;
1636 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001637 if (isFocusedStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08001638 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07001639 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001640 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001641 }
Craig Mautner05d29032013-05-03 13:40:13 -07001642 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001643 }
1644
Todd Kennedy539db512014-12-15 09:57:55 -08001645 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001646 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1647 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001648 final int numStacks = stacks.size();
1649 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1650 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08001651 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001652 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001653 }
1654 }
1655
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07001656 void finishVoiceTask(IVoiceInteractionSession session) {
1657 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1658 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1659 final int numStacks = stacks.size();
1660 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1661 final ActivityStack stack = stacks.get(stackNdx);
1662 stack.finishVoiceTask(session);
1663 }
1664 }
1665 }
1666
Chong Zhang280d3322015-11-03 17:27:26 -08001667 void findTaskToMoveToFrontLocked(
1668 TaskRecord task, int flags, ActivityOptions options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001669 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
1670 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001671 }
Craig Mautneraea74a52014-03-08 14:23:10 -08001672 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
1673 // Caller wants the home activity moved with it. To accomplish this,
1674 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07001675 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08001676 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07001677 if (task.stack == null) {
1678 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
1679 + task + " to front. Stack is null");
1680 return;
1681 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001682
Chong Zhang0fa656b2015-08-31 15:17:21 -07001683 if (task.mResizeable && options != null) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001684 if (canUseActivityOptionsLaunchBounds(options)) {
1685 Rect bounds = options.getLaunchBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07001686 task.updateOverrideConfiguration(bounds);
Chong Zhang112eb8c2015-11-02 11:17:00 -08001687 final int stackId = task.getLaunchStackId();
1688 if (stackId != task.stack.mStackId) {
1689 moveTaskToStackUncheckedLocked(task, stackId, ON_TOP, !FORCE_FOCUS, reason);
1690 // moveTaskToStackUncheckedLocked() should already placed the task on top,
1691 // still need moveTaskToFrontLocked() below for any transition settings.
1692 }
1693 // WM resizeTask must be done after the task is moved to the correct stack,
1694 // because Task's setBounds() also updates dim layer's bounds, but that has
1695 // dependency on the stack.
Chong Zhang87b21722015-09-21 15:39:51 -07001696 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig,
1697 false /*relayout*/, false /*forced*/);
Chong Zhang0fa656b2015-08-31 15:17:21 -07001698 }
1699 }
1700
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001701 final ActivityRecord r = task.getTopActivity();
1702 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
1703 r == null ? null : r.appTimeTracker, reason);
1704
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001705 if (DEBUG_STACK) Slog.d(TAG_STACK,
1706 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001707 }
1708
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001709 private boolean canUseActivityOptionsLaunchBounds(ActivityOptions options) {
1710 // We use the launch bounds in the activity options is the device supports freeform
1711 // window management.
1712 return options.hasLaunchBounds() && mService.mSupportsFreeformWindowManagement;
1713 }
1714
Craig Mautner967212c2013-04-13 21:10:58 -07001715 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07001716 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001717 }
1718
1719 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001720 ActivityContainer activityContainer = mActivityContainers.get(stackId);
1721 if (activityContainer != null) {
1722 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001723 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07001724 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001725 return null;
1726 }
1727 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001728 }
1729
Craig Mautner967212c2013-04-13 21:10:58 -07001730 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07001731 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001732 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1733 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001734 }
1735 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07001736 }
1737
Craig Mautner4a1cb222013-12-04 16:14:06 -08001738 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001739 ActivityRecord homeActivity = getHomeActivity();
1740 if (homeActivity != null) {
1741 return homeActivity.appToken;
1742 }
1743 return null;
1744 }
1745
1746 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07001747 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001748 }
1749
1750 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001751 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
1752 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1753 final TaskRecord task = tasks.get(taskNdx);
1754 if (task.isHomeTask()) {
1755 final ArrayList<ActivityRecord> activities = task.mActivities;
1756 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1757 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001758 if (r.isHomeActivity()
1759 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001760 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001761 }
1762 }
1763 }
1764 }
1765 return null;
1766 }
1767
Chong Zhangb15758a2015-11-17 12:12:03 -08001768 /**
1769 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
1770 * the docked stack itself, or if it's side-by-side to the docked stack.
1771 */
1772 boolean isStackDockedInEffect(int stackId) {
1773 return stackId == DOCKED_STACK_ID ||
1774 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
1775 }
1776
Todd Kennedyca4d8422015-01-15 15:19:22 -08001777 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08001778 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07001779 ActivityContainer activityContainer =
1780 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001781 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001782 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1783 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001784 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001785 return activityContainer;
1786 }
1787
Craig Mautner34b73df2014-01-12 21:11:08 -08001788 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001789 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
1790 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
1791 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001792 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
1793 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001794 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08001795 }
1796 }
1797
Craig Mautner95da1082014-02-24 17:54:35 -08001798 void deleteActivityContainer(IActivityContainer container) {
1799 ActivityContainer activityContainer = (ActivityContainer)container;
1800 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001801 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1802 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08001803 final int stackId = activityContainer.mStackId;
1804 mActivityContainers.remove(stackId);
1805 mWindowManager.removeStack(stackId);
1806 }
1807 }
1808
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07001809 void resizeStackLocked(int stackId, Rect bounds, boolean preserveWindows,
1810 boolean allowResizeInDockedMode) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08001811 final ActivityStack stack = getStack(stackId);
1812 if (stack == null) {
1813 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
1814 return;
1815 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08001816
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07001817 if (!allowResizeInDockedMode
1818 && stackId != DOCKED_STACK_ID && getStack(DOCKED_STACK_ID) != null) {
1819 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
1820 // stack size changing so things don't get out of sync.
1821 return;
1822 }
1823
Wale Ogunwalecad05a02015-09-25 10:41:44 -07001824 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02001825 mWindowManager.deferSurfaceLayout();
1826 try {
Wale Ogunwaleb4ec0a32015-12-14 10:31:43 -08001827
1828 if (bounds != null && mWindowManager.isFullscreenBounds(stackId, bounds)) {
1829 // The bounds passed in corresponds to the fullscreen bounds which we normally
1830 // represent with null. Go ahead and set it to null so that all tasks configuration
1831 // can have the right fullscreen state.
1832 bounds = null;
1833 }
1834
Jorim Jaggic4025202015-10-22 16:43:34 +02001835 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwalecad05a02015-09-25 10:41:44 -07001836
Jorim Jaggic4025202015-10-22 16:43:34 +02001837 mTmpBounds.clear();
1838 mTmpConfigs.clear();
1839 ArrayList<TaskRecord> tasks = stack.getAllTasks();
1840 for (int i = tasks.size() - 1; i >= 0; i--) {
1841 TaskRecord task = tasks.get(i);
1842 if (task.mResizeable) {
1843 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1844 // For freeform stack we don't adjust the size of the tasks to match that
1845 // of the stack, but we do try to make sure the tasks are still contained
1846 // with the bounds of the stack.
1847 tempRect2.set(task.mBounds);
1848 fitWithinBounds(tempRect2, bounds);
1849 task.updateOverrideConfiguration(tempRect2);
1850 } else {
1851 task.updateOverrideConfiguration(bounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001852 }
1853 }
1854
Jorim Jaggic4025202015-10-22 16:43:34 +02001855 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
1856 mTmpBounds.put(task.taskId, task.mBounds);
1857 }
1858 stack.mFullscreen = mWindowManager.resizeStack(stackId, bounds, mTmpConfigs, mTmpBounds);
1859 if (stack.mStackId == DOCKED_STACK_ID) {
1860 // Dock stack funness...Yay!
1861 if (stack.mFullscreen) {
1862 // The dock stack went fullscreen which is kinda like dismissing it.
1863 // In this case we make all other static stacks fullscreen and move all
1864 // docked stack tasks to the fullscreen stack.
1865 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale3797c222015-10-27 14:21:58 -07001866 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
Jorim Jaggic4025202015-10-22 16:43:34 +02001867 resizeStackLocked(i, null, preserveWindows, true);
1868 }
1869 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001870
Jorim Jaggic4025202015-10-22 16:43:34 +02001871 final int count = tasks.size();
1872 for (int i = 0; i < count; i++) {
1873 moveTaskToStackLocked(tasks.get(i).taskId,
Jorim Jaggi030979c2015-11-20 15:14:43 -08001874 FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP, FORCE_FOCUS, "resizeStack",
Jorim Jaggi55387522015-11-24 18:21:10 -08001875 false /* animate */);
Jorim Jaggic4025202015-10-22 16:43:34 +02001876 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001877
Jorim Jaggic4025202015-10-22 16:43:34 +02001878 // stack shouldn't contain anymore activities, so nothing to resume.
1879 r = null;
1880 } else {
1881 // Docked stacks occupy a dedicated region on screen so the size of all other
1882 // static stacks need to be adjusted so they don't overlap with the docked stack.
1883 // We get the bounds to use from window manager which has been adjusted for any
1884 // screen controls and is also the same for all stacks.
1885 mWindowManager.getStackDockedModeBounds(HOME_STACK_ID, tempRect);
1886
1887 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale3797c222015-10-27 14:21:58 -07001888 if (StackId.isResizeableByDockedStack(i)) {
Jorim Jaggic4025202015-10-22 16:43:34 +02001889 ActivityStack otherStack = getStack(i);
1890 if (otherStack != null) {
1891 resizeStackLocked(i, tempRect, PRESERVE_WINDOWS, true);
1892 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001893 }
1894 }
1895 }
Jorim Jaggic4025202015-10-22 16:43:34 +02001896 // Since we are resizing the stack, all other operations should strive to preserve
1897 // windows.
1898 preserveWindows = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001899 }
Jorim Jaggic4025202015-10-22 16:43:34 +02001900 stack.setBounds(bounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001901
Jorim Jaggic4025202015-10-22 16:43:34 +02001902 if (r != null) {
1903 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0, preserveWindows);
1904 // And we need to make sure at this point that all other activities
1905 // are made visible with the correct configuration.
1906 ensureActivitiesVisibleLocked(r, 0, preserveWindows);
1907 if (!updated) {
1908 resumeTopActivitiesLocked(stack, null, null);
1909 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001910 }
Jorim Jaggic4025202015-10-22 16:43:34 +02001911 } finally {
1912 mWindowManager.continueSurfaceLayout();
1913 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001914 }
1915 }
1916
Chong Zhang6de2ae82015-09-30 18:25:21 -07001917 void resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001918 if (!task.mResizeable) {
1919 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
1920 return;
1921 }
1922
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07001923 adjustForMinimalTaskDimensions(task, bounds);
1924
Chong Zhang87b21722015-09-21 15:39:51 -07001925 // If this is a forced resize, let it go through even if the bounds is not changing,
1926 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07001927 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Chong Zhang87b21722015-09-21 15:39:51 -07001928 if (task.mBounds != null && task.mBounds.equals(bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001929 // Nothing to do here...
1930 return;
1931 }
1932
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001933 if (!mWindowManager.isValidTaskId(task.taskId)) {
1934 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07001935 // All we can do for now is update the bounds so it can be used when the task is
1936 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07001937 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07001938 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
1939 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07001940 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07001941 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001942 return;
1943 }
1944
Chong Zhang6de2ae82015-09-30 18:25:21 -07001945 // Do not move the task to another stack here.
1946 // This method assumes that the task is already placed in the right stack.
1947 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07001948
Chong Zhang6de2ae82015-09-30 18:25:21 -07001949 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07001950
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07001951 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07001952 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001953 // way and the activity was kept the way it was. If it's false, it means the activity had
1954 // to be relaunched due to configuration change.
1955 boolean kept = true;
1956 if (overrideConfig != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001957 ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08001958 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001959 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07001960 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001961 // All other activities must be made visible with their correct configuration.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001962 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001963 if (!kept) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08001964 resumeTopActivitiesLocked(stack, null, null);
1965 }
1966 }
1967 }
Chong Zhang87b21722015-09-21 15:39:51 -07001968 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07001969
1970 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwale60454db2015-01-23 16:05:07 -08001971 }
1972
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07001973 private void adjustForMinimalTaskDimensions(TaskRecord task, Rect bounds) {
1974 if (bounds == null) {
1975 return;
1976 }
1977 int minimalSize = task.mMinimalSize == -1 ? mDefaultMinimalSizeOfResizeableTask
1978 : task.mMinimalSize;
1979 final boolean adjustWidth = minimalSize > bounds.width();
1980 final boolean adjustHeight = minimalSize > bounds.height();
1981 if (!(adjustWidth || adjustHeight)) {
1982 return;
1983 }
1984 Rect taskBounds = task.mBounds;
1985 if (adjustWidth) {
1986 if (taskBounds != null && bounds.right == taskBounds.right) {
1987 bounds.left = bounds.right - minimalSize;
1988 } else {
1989 // Either left bounds match, or neither match, or the previous bounds were
1990 // fullscreen and we default to keeping left.
1991 bounds.right = bounds.left + minimalSize;
1992 }
1993 }
1994 if (adjustHeight) {
1995 if (taskBounds != null && bounds.bottom == taskBounds.bottom) {
1996 bounds.top = bounds.bottom - minimalSize;
1997 } else {
1998 // Either top bounds match, or neither match, or the previous bounds were
1999 // fullscreen and we default to keeping top.
2000 bounds.bottom = bounds.top + minimalSize;
2001 }
2002 }
2003 }
2004
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002005 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002006 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2007 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002008 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002009 }
2010
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002011 ActivityContainer activityContainer = new ActivityContainer(stackId);
2012 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002013 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002014 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002015 }
2016
2017 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002018 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002019 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2020 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002021 break;
2022 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002023 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002024 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002025 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002026 }
2027
Chong Zhang5dcb2752015-08-18 13:50:26 -07002028 /**
2029 * Restores a recent task to a stack
2030 * @param task The recent task to be restored.
2031 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale3797c222015-10-27 14:21:58 -07002032 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002033 * @return true if the task has been restored successfully.
2034 */
2035 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
2036 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002037 stackId = task.getLaunchStackId();
Chong Zhang5dcb2752015-08-18 13:50:26 -07002038 }
Wale Ogunwale706ed792015-08-02 10:29:44 -07002039 if (task.stack != null) {
2040 // Task has already been restored once. See if we need to do anything more
2041 if (task.stack.mStackId == stackId) {
2042 // Nothing else to do since it is already restored in the right stack.
2043 return true;
2044 }
2045 // Remove current stack association, so we can re-associate the task with the
2046 // right stack below.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002047 task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002048 }
2049
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002050 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002051 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002052
2053 if (stack == null) {
2054 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002055 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2056 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002057 return false;
2058 }
2059
Wale Ogunwale5f986092015-12-04 15:35:38 -08002060 stack.addTask(task, false, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002061 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2062 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002063 final ArrayList<ActivityRecord> activities = task.mActivities;
2064 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002065 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002066 }
2067 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002068 }
2069
Wale Ogunwale040b4702015-08-06 18:10:50 -07002070 /**
2071 * Moves the specified task record to the input stack id.
2072 * WARNING: This method performs an unchecked/raw move of the task and
2073 * can leave the system in an unstable state if used incorrectly.
2074 * Use {@link #moveTaskToStackLocked} to perform safe task movement
2075 * to a stack.
2076 * @param task Task to move.
2077 * @param stackId Id of stack to move task to.
2078 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07002079 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07002080 * @param reason Reason the task is been moved.
2081 * @return The stack the task was moved to.
2082 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07002083 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07002084 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
2085 final ActivityRecord r = task.getTopActivity();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002086 final boolean wasFocused = isFocusedStack(task.stack) && (topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07002087 final boolean wasResumed = wasFocused && (task.stack.mResumedActivity == r);
2088
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002089 final boolean resizeable = task.mResizeable;
2090 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
2091 // if a docked stack is created below which will lead to the stack we are moving from and
2092 // its resizeable tasks being resized.
2093 task.mResizeable = false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002094 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002095 task.mResizeable = resizeable;
Wale Ogunwale040b4702015-08-06 18:10:50 -07002096 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002097 stack.addTask(task, toTop, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002098
2099 // If the task had focus before (or we're requested to move focus),
2100 // move focus to the new stack.
Wale Ogunwale079a0042015-10-24 11:44:07 -07002101 stack.setFocusAndResumeStateIfNeeded(
2102 r, forceFocus || wasFocused, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002103
Wale Ogunwale040b4702015-08-06 18:10:50 -07002104 return stack;
2105 }
2106
Filip Gruszczynski90186c62015-10-26 14:07:00 -07002107 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08002108 String reason, boolean animate) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002109 final TaskRecord task = anyTaskForIdLocked(taskId);
2110 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002111 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002112 return;
2113 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002114
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002115 if (task.stack != null && task.stack.mStackId == stackId) {
2116 // You are already in the right stack silly...
2117 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
2118 return;
2119 }
2120
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002121 final ActivityRecord topActivity = task.getTopActivity();
2122 if (StackId.preserveWindowOnTaskMove(stackId) && topActivity != null) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002123 // We are about to relaunch the activity because its configuration changed due to
2124 // being maximized, i.e. size change. The activity will first remove the old window
2125 // and then add a new one. This call will tell window manager about this, so it can
2126 // preserve the old window until the new one is drawn. This prevents having a gap
2127 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07002128 // entrance of the new window to be properly animated.
Jorim Jaggi030979c2015-11-20 15:14:43 -08002129 mWindowManager.setReplacingWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002130 }
Wale Ogunwale3cd48042015-11-16 13:01:41 -08002131 final ActivityStack stack = moveTaskToStackUncheckedLocked(
2132 task, stackId, toTop, forceFocus, "moveTaskToStack:" + reason);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002133
Jorim Jaggi55387522015-11-24 18:21:10 -08002134 if (!animate) {
2135 stack.mNoAnimActivities.add(topActivity);
2136 }
2137
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002138 // Make sure the task has the appropriate bounds/size for the stack it is in.
2139 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002140 resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002141 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID
2142 && task.mBounds == null && task.mLastNonFullscreenBounds != null) {
Chong Zhang6de2ae82015-09-30 18:25:21 -07002143 resizeTaskLocked(task, task.mLastNonFullscreenBounds,
2144 RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale99db1862015-10-23 20:08:22 -07002145 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002146 resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002147 }
2148
Stefan Kuhne54714cd2015-05-12 13:42:18 -07002149 // The task might have already been running and its visibility needs to be synchronized with
2150 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002151 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautner05d29032013-05-03 13:40:13 -07002152 resumeTopActivitiesLocked();
Chong Zhangb15758a2015-11-17 12:12:03 -08002153
2154 if (!task.mResizeable && isStackDockedInEffect(stackId)) {
Chong Zhangc806d902015-11-30 09:44:27 -08002155 showNonResizeableDockToast(taskId);
Chong Zhangb15758a2015-11-17 12:12:03 -08002156 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002157 }
2158
Wale Ogunwale079a0042015-10-24 11:44:07 -07002159 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2160 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2161 if (stack == null) {
2162 throw new IllegalArgumentException(
2163 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2164 }
2165
2166 final ActivityRecord r = stack.topRunningActivityLocked();
2167 if (r == null) {
2168 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2169 + " in stack=" + stack);
2170 return false;
2171 }
2172
Youngsang Choba80ba52015-12-11 14:32:35 -08002173 if (!mService.mForceResizableActivities && !r.info.supportsPip) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002174 Slog.w(TAG,
2175 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002176 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002177 return false;
2178 }
2179
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002180 moveActivityToStackLocked(r, PINNED_STACK_ID, "moveTopActivityToPinnedStack", bounds);
2181 return true;
2182 }
2183
2184 void moveActivityToStackLocked(ActivityRecord r, int stackId, String reason, Rect bounds) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002185 final TaskRecord task = r.task;
Wale Ogunwale079a0042015-10-24 11:44:07 -07002186 if (task.mActivities.size() == 1) {
2187 // 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 -08002188 // stack without re-parenting the activity in a different task.
2189 moveTaskToStackLocked(
2190 task.taskId, stackId, ON_TOP, FORCE_FOCUS, reason, true /* animate */);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002191 } else {
Wale Ogunwaled88f6512015-12-06 19:39:01 -08002192 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, ON_TOP);
2193 stack.moveActivityToStack(r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002194 }
2195
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002196 if (bounds != null) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002197 resizeStackLocked(stackId, bounds, !PRESERVE_WINDOWS, true);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002198 }
Wale Ogunwale079a0042015-10-24 11:44:07 -07002199
2200 // The task might have already been running and its visibility needs to be synchronized with
2201 // the visibility of the stack / windows.
2202 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
2203 resumeTopActivitiesLocked();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002204 }
2205
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002206 void positionTaskInStackLocked(int taskId, int stackId, int position) {
2207 final TaskRecord task = anyTaskForIdLocked(taskId);
2208 if (task == null) {
2209 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
2210 return;
2211 }
Wale Ogunwale935e5022015-11-10 12:36:10 -08002212 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
2213
2214 task.updateOverrideConfigurationForStack(stack);
2215
2216 mWindowManager.positionTaskInStack(
2217 taskId, stackId, position, task.mBounds, task.mOverrideConfig);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002218 stack.positionTask(task, position);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002219 // The task might have already been running and its visibility needs to be synchronized with
2220 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002221 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002222 resumeTopActivitiesLocked();
2223 }
2224
Craig Mautnerac6f8432013-07-17 13:24:59 -07002225 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002226 mTmpFindTaskResult.r = null;
2227 mTmpFindTaskResult.matchedByRootAffinity = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002228 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002229 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2230 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002231 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2232 final ActivityStack stack = stacks.get(stackNdx);
2233 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002234 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002235 continue;
2236 }
2237 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002238 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2239 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002240 continue;
2241 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002242 stack.findTaskLocked(r, mTmpFindTaskResult);
2243 // It is possible to have task in multiple stacks with the same root affinity.
2244 // If the match we found was based on root affinity we keep on looking to see if
2245 // there is a better match in another stack. We eventually return the match based
2246 // on root affinity if we don't find a better match.
2247 if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
2248 return mTmpFindTaskResult.r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002249 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002250 }
2251 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002252 if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
2253 return mTmpFindTaskResult.r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002254 }
2255
2256 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002257 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2258 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002259 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2260 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2261 if (ar != null) {
2262 return ar;
2263 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002264 }
2265 }
2266 return null;
2267 }
2268
Craig Mautner8d341ef2013-03-26 09:03:27 -07002269 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002270 scheduleSleepTimeout();
2271 if (!mGoingToSleep.isHeld()) {
2272 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002273 if (mLaunchingActivity.isHeld()) {
2274 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2275 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002276 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002277 mLaunchingActivity.release();
2278 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002279 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002280 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002281 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002282 }
2283
2284 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002285 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002286
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002287 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002288 final long endTime = System.currentTimeMillis() + timeout;
2289 while (true) {
2290 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002291 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2292 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002293 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2294 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2295 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002296 }
2297 if (cantShutdown) {
2298 long timeRemaining = endTime - System.currentTimeMillis();
2299 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002300 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002301 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002302 } catch (InterruptedException e) {
2303 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002304 } else {
2305 Slog.w(TAG, "Activity manager shutdown timed out");
2306 timedout = true;
2307 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002308 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002309 } else {
2310 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002311 }
2312 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002313
2314 // Force checkReadyForSleep to complete.
2315 mSleepTimeout = true;
2316 checkReadyForSleepLocked();
2317
Craig Mautner8d341ef2013-03-26 09:03:27 -07002318 return timedout;
2319 }
2320
2321 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002322 removeSleepTimeouts();
2323 if (mGoingToSleep.isHeld()) {
2324 mGoingToSleep.release();
2325 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002326 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2327 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002328 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2329 final ActivityStack stack = stacks.get(stackNdx);
2330 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002331 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002332 resumeTopActivitiesLocked();
2333 }
Craig Mautner5314a402013-09-26 12:40:16 -07002334 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002335 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002336 mGoingToSleepActivities.clear();
2337 }
2338
2339 void activitySleptLocked(ActivityRecord r) {
2340 mGoingToSleepActivities.remove(r);
2341 checkReadyForSleepLocked();
2342 }
2343
2344 void checkReadyForSleepLocked() {
2345 if (!mService.isSleepingOrShuttingDown()) {
2346 // Do not care.
2347 return;
2348 }
2349
2350 if (!mSleepTimeout) {
2351 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002352 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2353 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002354 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2355 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2356 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002357 }
2358
2359 if (mStoppingActivities.size() > 0) {
2360 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002361 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002362 + mStoppingActivities.size() + " activities");
2363 scheduleIdleLocked();
2364 dontSleep = true;
2365 }
2366
2367 if (mGoingToSleepActivities.size() > 0) {
2368 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002369 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002370 + mGoingToSleepActivities.size() + " activities");
2371 dontSleep = true;
2372 }
2373
2374 if (dontSleep) {
2375 return;
2376 }
2377 }
2378
Craig Mautnere0a38842013-12-16 16:14:02 -08002379 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2380 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002381 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2382 stacks.get(stackNdx).goToSleep();
2383 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002384 }
2385
2386 removeSleepTimeouts();
2387
2388 if (mGoingToSleep.isHeld()) {
2389 mGoingToSleep.release();
2390 }
2391 if (mService.mShuttingDown) {
2392 mService.notifyAll();
2393 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002394 }
2395
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002396 boolean reportResumedActivityLocked(ActivityRecord r) {
2397 final ActivityStack stack = r.task.stack;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002398 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002399 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002400 }
2401 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002402 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002403 mWindowManager.executeAppTransition();
2404 return true;
2405 }
2406 return false;
2407 }
2408
Craig Mautner8d341ef2013-03-26 09:03:27 -07002409 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002410 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2411 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07002412 int stackNdx = stacks.size() - 1;
2413 while (stackNdx >= 0) {
2414 stacks.get(stackNdx).handleAppCrashLocked(app);
2415 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002416 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002417 }
2418 }
2419
Jose Lima4b6c6692014-08-12 17:41:12 -07002420 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002421 final ActivityStack stack = r.task.stack;
2422 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002423 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2424 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002425 return false;
2426 }
Jose Lima4b6c6692014-08-12 17:41:12 -07002427 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002428 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2429 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002430
2431 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002432 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002433 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07002434 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002435 return true;
2436 }
2437
2438 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07002439 if (visible && top.fullscreen) {
2440 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002441 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2442 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
2443 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
2444 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002445 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07002446 } else if (!visible && stack.getVisibleBehindActivity() != r) {
2447 // Only the activity set as currently visible behind should actively reset its
2448 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002449 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2450 "requestVisibleBehind: returning visible=" + visible
2451 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
2452 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07002453 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002454 }
2455
Jose Lima4b6c6692014-08-12 17:41:12 -07002456 stack.setVisibleBehindActivity(visible ? r : null);
2457 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002458 // Make the activity immediately above r opaque.
2459 final ActivityRecord next = stack.findNextTranslucentActivity(r);
2460 if (next != null) {
2461 mService.convertFromTranslucent(next.appToken);
2462 }
2463 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07002464 if (top.app != null && top.app.thread != null) {
2465 // Notify the top app of the change.
2466 try {
2467 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
2468 } catch (RemoteException e) {
2469 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07002470 }
2471 return true;
2472 }
2473
Craig Mautnerbb742462014-07-07 15:28:55 -07002474 // Called when WindowManager has finished animating the launchingBehind activity to the back.
2475 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
2476 r.mLaunchTaskBehind = false;
2477 final TaskRecord task = r.task;
Winson8b1871d2015-11-20 09:56:20 -08002478 task.setLastThumbnailLocked(task.stack.screenshotActivitiesLocked(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002479 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08002480 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07002481 mWindowManager.setAppVisibility(r.appToken, false);
2482 }
2483
2484 void scheduleLaunchTaskBehindComplete(IBinder token) {
2485 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2486 }
2487
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002488 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
2489 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07002490 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002491 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2492 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002493 final int topStackNdx = stacks.size() - 1;
2494 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2495 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002496 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07002497 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002498 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002499 }
2500
Chong Zhangfdcc4d42015-10-14 16:50:12 -07002501 void invalidateTaskLayers() {
2502 mTaskLayersChanged = true;
2503 }
2504
2505 void rankTaskLayersIfNeeded() {
2506 if (!mTaskLayersChanged) {
2507 return;
2508 }
2509 mTaskLayersChanged = false;
2510 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
2511 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2512 int baseLayer = 0;
2513 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2514 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
2515 }
2516 }
2517 }
2518
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002519 void clearOtherAppTimeTrackers(AppTimeTracker except) {
2520 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2521 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2522 final int topStackNdx = stacks.size() - 1;
2523 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2524 final ActivityStack stack = stacks.get(stackNdx);
2525 stack.clearOtherAppTimeTrackers(except);
2526 }
2527 }
2528 }
2529
Craig Mautner8d341ef2013-03-26 09:03:27 -07002530 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002531 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2532 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002533 final int numStacks = stacks.size();
2534 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2535 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002536 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002537 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002538 }
2539 }
2540
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002541 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
2542 // Examine all activities currently running in the process.
2543 TaskRecord firstTask = null;
2544 // Tasks is non-null only if two or more tasks are found.
2545 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002546 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
2547 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002548 ActivityRecord r = app.activities.get(i);
2549 // First, if we find an activity that is in the process of being destroyed,
2550 // then we just aren't going to do anything for now; we want things to settle
2551 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002552 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002553 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002554 return;
2555 }
2556 // Don't consider any activies that are currently not in a state where they
2557 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002558 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
2559 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002560 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002561 continue;
2562 }
2563 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002564 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002565 + " from " + r);
2566 if (firstTask == null) {
2567 firstTask = r.task;
2568 } else if (firstTask != r.task) {
2569 if (tasks == null) {
2570 tasks = new ArraySet<>();
2571 tasks.add(firstTask);
2572 }
2573 tasks.add(r.task);
2574 }
2575 }
2576 }
2577 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002578 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002579 return;
2580 }
2581 // If we have activities in multiple tasks that are in a position to be destroyed,
2582 // let's iterate through the tasks and release the oldest one.
2583 final int numDisplays = mActivityDisplays.size();
2584 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
2585 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2586 // Step through all stacks starting from behind, to hit the oldest things first.
2587 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
2588 final ActivityStack stack = stacks.get(stackNdx);
2589 // Try to release activities in this stack; if we manage to, we are done.
2590 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
2591 return;
2592 }
2593 }
2594 }
2595 }
2596
Amith Yamasani37a40c22015-06-17 13:25:42 -07002597 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002598 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002599 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07002600 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002601
Craig Mautner858d8a62013-04-23 17:08:34 -07002602 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08002603 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2604 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002605 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002606 final ActivityStack stack = stacks.get(stackNdx);
2607 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002608 TaskRecord task = stack.topTask();
2609 if (task != null) {
2610 mWindowManager.moveTaskToTop(task.taskId);
2611 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002612 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07002613 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002614
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002615 ActivityStack stack = getStack(restoreStackId);
2616 if (stack == null) {
2617 stack = mHomeStack;
2618 }
2619 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002620 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07002621 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08002622 } else {
2623 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08002624 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08002625 }
Craig Mautner93529a42013-10-04 15:03:13 -07002626 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07002627 }
2628
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002629 /**
2630 * Add background users to send boot completed events to.
2631 * @param userId The user being started in the background
2632 * @param uss The state object for the user.
2633 */
Amith Yamasani37a40c22015-06-17 13:25:42 -07002634 public void startBackgroundUserLocked(int userId, UserState uss) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002635 mStartingBackgroundUsers.add(uss);
2636 }
2637
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002638 /** Checks whether the userid is a profile of the current user. */
2639 boolean isCurrentProfileLocked(int userId) {
2640 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002641 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002642 }
2643
Chong Zhang45c25ce2015-08-10 22:18:26 -07002644 /** Checks whether the activity should be shown for current user. */
2645 boolean okToShowLocked(ActivityRecord r) {
2646 return r != null && (isCurrentProfileLocked(r.userId)
2647 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
2648 }
2649
Craig Mautnerde4ef022013-04-07 19:01:33 -07002650 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002651 ArrayList<ActivityRecord> stops = null;
2652
2653 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08002654 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2655 ActivityRecord s = mStoppingActivities.get(activityNdx);
2656 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002657 if (DEBUG_ALL) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08002658 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
2659 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002660 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002661 if (s.finishing) {
2662 // If this activity is finishing, it is sitting on top of
2663 // everyone else but we now know it is no longer needed...
2664 // so get rid of it. Otherwise, we need to go through the
2665 // normal flow and hide it once we determine that it is
2666 // hidden by the activities in front of it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002667 if (DEBUG_ALL) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002668 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002669 }
2670 }
Craig Mautner8c14c152015-01-15 17:32:07 -08002671 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002672 if (DEBUG_ALL) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002673 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08002674 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002675 }
2676 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08002677 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002678 }
2679 }
2680
2681 return stops;
2682 }
2683
Craig Mautnercf910b02013-04-23 11:23:27 -07002684 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002685 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2686 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2687 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2688 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002689 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002690 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002691 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002692 if (r == null) Slog.e(TAG,
2693 "validateTop...: null top activity, stack=" + stack);
2694 else {
2695 final ActivityRecord pausing = stack.mPausingActivity;
2696 if (pausing != null && pausing == r) Slog.e(TAG,
2697 "validateTop...: top stack has pausing activity r=" + r
2698 + " state=" + state);
2699 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
2700 "validateTop...: activity in front not resumed r=" + r
2701 + " state=" + state);
2702 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002703 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002704 final ActivityRecord resumed = stack.mResumedActivity;
2705 if (resumed != null && resumed == r) Slog.e(TAG,
2706 "validateTop...: back stack has resumed activity r=" + r
2707 + " state=" + state);
2708 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
2709 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002710 }
2711 }
2712 }
Craig Mautner76ea2242013-05-15 11:40:05 -07002713 }
2714
Craig Mautnere0570202015-05-13 13:06:11 -07002715 private String lockTaskModeToString() {
2716 switch (mLockTaskModeState) {
2717 case LOCK_TASK_MODE_LOCKED:
2718 return "LOCKED";
2719 case LOCK_TASK_MODE_PINNED:
2720 return "PINNED";
2721 case LOCK_TASK_MODE_NONE:
2722 return "NONE";
2723 default: return "unknown=" + mLockTaskModeState;
2724 }
2725 }
2726
Craig Mautner27084302013-03-25 08:05:25 -07002727 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002728 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002729 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002730 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
2731 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
2732 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08002733 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07002734 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
2735 final SparseArray<String[]> packages = mService.mLockTaskPackages;
2736 if (packages.size() > 0) {
2737 pw.println(" mLockTaskPackages (userId:packages)=");
2738 for (int i = 0; i < packages.size(); ++i) {
2739 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
2740 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
2741 }
2742 }
2743 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07002744 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002745
Craig Mautner20e72272013-04-01 13:45:53 -07002746 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002747 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07002748 }
2749
Dianne Hackborn390517b2013-05-30 15:03:32 -07002750 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
2751 boolean needSep, String prefix) {
2752 if (activity != null) {
2753 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
2754 if (needSep) {
2755 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07002756 }
2757 pw.print(prefix);
2758 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002759 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002760 }
2761 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002762 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002763 }
2764
Craig Mautner8d341ef2013-03-26 09:03:27 -07002765 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
2766 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002767 boolean printed = false;
2768 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002769 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
2770 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002771 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07002772 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08002773 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07002774 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002775 final ActivityStack stack = stacks.get(stackNdx);
2776 StringBuilder stackHeader = new StringBuilder(128);
2777 stackHeader.append(" Stack #");
2778 stackHeader.append(stack.mStackId);
2779 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002780 stackHeader.append("\n");
2781 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
2782 stackHeader.append("\n");
2783 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002784 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
2785 needSep, stackHeader.toString());
2786 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
2787 !dumpAll, false, dumpPackage, true,
2788 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002789
Craig Mautner4a1cb222013-12-04 16:14:06 -08002790 needSep = printed;
2791 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
2792 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002793 if (pr) {
2794 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002795 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002796 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002797 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
2798 " mResumedActivity: ");
2799 if (pr) {
2800 printed = true;
2801 needSep = false;
2802 }
2803 if (dumpAll) {
2804 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
2805 " mLastPausedActivity: ");
2806 if (pr) {
2807 printed = true;
2808 needSep = true;
2809 }
2810 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
2811 needSep, " mLastNoHistoryActivity: ");
2812 }
2813 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002814 }
2815 }
2816
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002817 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
2818 false, dumpPackage, true, " Activities waiting to finish:", null);
2819 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
2820 false, dumpPackage, true, " Activities waiting to stop:", null);
2821 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
2822 false, dumpPackage, true, " Activities waiting for another to become visible:",
2823 null);
2824 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
2825 false, dumpPackage, true, " Activities waiting to sleep:", null);
2826 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
2827 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07002828
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002829 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002830 }
2831
Dianne Hackborn390517b2013-05-30 15:03:32 -07002832 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07002833 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002834 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002835 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002836 String innerPrefix = null;
2837 String[] args = null;
2838 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002839 for (int i=list.size()-1; i>=0; i--) {
2840 final ActivityRecord r = list.get(i);
2841 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
2842 continue;
2843 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07002844 if (innerPrefix == null) {
2845 innerPrefix = prefix + " ";
2846 args = new String[0];
2847 }
2848 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002849 final boolean full = !brief && (complete || !r.isInHistory());
2850 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07002851 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07002852 needNL = false;
2853 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002854 if (header1 != null) {
2855 pw.println(header1);
2856 header1 = null;
2857 }
2858 if (header2 != null) {
2859 pw.println(header2);
2860 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002861 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002862 if (lastTask != r.task) {
2863 lastTask = r.task;
2864 pw.print(prefix);
2865 pw.print(full ? "* " : " ");
2866 pw.println(lastTask);
2867 if (full) {
2868 lastTask.dump(pw, prefix + " ");
2869 } else if (complete) {
2870 // Complete + brief == give a summary. Isn't that obvious?!?
2871 if (lastTask.intent != null) {
2872 pw.print(prefix); pw.print(" ");
2873 pw.println(lastTask.intent.toInsecureStringWithClip());
2874 }
2875 }
2876 }
2877 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
2878 pw.print(" #"); pw.print(i); pw.print(": ");
2879 pw.println(r);
2880 if (full) {
2881 r.dump(pw, innerPrefix);
2882 } else if (complete) {
2883 // Complete + brief == give a summary. Isn't that obvious?!?
2884 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
2885 if (r.app != null) {
2886 pw.print(innerPrefix); pw.println(r.app);
2887 }
2888 }
2889 if (client && r.app != null && r.app.thread != null) {
2890 // flush anything that is already in the PrintWriter since the thread is going
2891 // to write to the file descriptor directly
2892 pw.flush();
2893 try {
2894 TransferPipe tp = new TransferPipe();
2895 try {
2896 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
2897 r.appToken, innerPrefix, args);
2898 // Short timeout, since blocking here can
2899 // deadlock with the application.
2900 tp.go(fd, 2000);
2901 } finally {
2902 tp.kill();
2903 }
2904 } catch (IOException e) {
2905 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
2906 } catch (RemoteException e) {
2907 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
2908 }
2909 needNL = true;
2910 }
2911 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07002912 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002913 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002914
Craig Mautnerf3333272013-04-22 10:55:53 -07002915 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002916 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
2917 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07002918 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
2919 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07002920 }
2921
2922 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07002923 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07002924 }
2925
2926 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002927 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
2928 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002929 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2930 }
2931
Craig Mautner05d29032013-05-03 13:40:13 -07002932 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08002933 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
2934 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
2935 }
Craig Mautner05d29032013-05-03 13:40:13 -07002936 }
2937
Craig Mautner0eea92c2013-05-16 13:35:39 -07002938 void removeSleepTimeouts() {
2939 mSleepTimeout = false;
2940 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
2941 }
2942
2943 final void scheduleSleepTimeout() {
2944 removeSleepTimeouts();
2945 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
2946 }
2947
Craig Mautner4a1cb222013-12-04 16:14:06 -08002948 @Override
2949 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07002950 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002951 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
2952 }
2953
2954 @Override
2955 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07002956 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002957 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
2958 }
2959
2960 @Override
2961 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07002962 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002963 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
2964 }
2965
Wale Ogunwalef16a2812015-04-01 11:23:15 -07002966 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08002967 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002968 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08002969 newDisplay = mActivityDisplays.get(displayId) == null;
2970 if (newDisplay) {
2971 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07002972 if (activityDisplay.mDisplay == null) {
2973 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
2974 return;
2975 }
Craig Mautner4504de52013-12-20 09:06:56 -08002976 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07002977 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08002978 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002979 }
Craig Mautner4504de52013-12-20 09:06:56 -08002980 if (newDisplay) {
2981 mWindowManager.onDisplayAdded(displayId);
2982 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002983 }
2984
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07002985 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
2986 if (display.mDisplayId != Display.DEFAULT_DISPLAY) {
2987 return;
2988 }
2989 final float fraction = mService.mContext.getResources().getFraction(com.android.internal.R.
2990 fraction.config_displayFractionForDefaultMinimalSizeOfResizeableTask, 1, 1);
2991 mDefaultMinimalSizeOfResizeableTask = (int) (fraction * Math.min(
2992 display.mDisplayInfo.logicalWidth, display.mDisplayInfo.logicalHeight));
2993 }
2994
Wale Ogunwalef16a2812015-04-01 11:23:15 -07002995 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002996 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002997 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2998 if (activityDisplay != null) {
2999 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003000 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003001 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003002 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003003 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003004 }
3005 }
3006 mWindowManager.onDisplayRemoved(displayId);
3007 }
3008
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003009 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003010 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003011 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3012 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003013 // TODO: Update the bounds.
3014 }
3015 }
3016 mWindowManager.onDisplayChanged(displayId);
3017 }
3018
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003019 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003020 StackInfo info = new StackInfo();
3021 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3022 info.displayId = Display.DEFAULT_DISPLAY;
3023 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003024 info.userId = stack.mCurrentUser;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003025
3026 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3027 final int numTasks = tasks.size();
3028 int[] taskIds = new int[numTasks];
3029 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003030 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003031 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003032 for (int i = 0; i < numTasks; ++i) {
3033 final TaskRecord task = tasks.get(i);
3034 taskIds[i] = task.taskId;
3035 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3036 : task.realActivity != null ? task.realActivity.flattenToString()
3037 : task.getTopActivity() != null ? task.getTopActivity().packageName
3038 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003039 taskBounds[i] = new Rect();
3040 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003041 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003042 }
3043 info.taskIds = taskIds;
3044 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003045 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003046 info.taskUserIds = taskUserIds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003047 return info;
3048 }
3049
3050 StackInfo getStackInfoLocked(int stackId) {
3051 ActivityStack stack = getStack(stackId);
3052 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003053 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003054 }
3055 return null;
3056 }
3057
3058 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003059 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003060 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3061 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003062 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003063 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003064 }
3065 }
3066 return list;
3067 }
3068
Craig Mautner15df08a2015-04-01 12:17:18 -07003069 TaskRecord getLockedTaskLocked() {
3070 final int top = mLockTaskModeTasks.size() - 1;
3071 if (top >= 0) {
3072 return mLockTaskModeTasks.get(top);
3073 }
3074 return null;
3075 }
3076
3077 boolean isLockedTask(TaskRecord task) {
3078 return mLockTaskModeTasks.contains(task);
3079 }
3080
3081 boolean isLastLockedTask(TaskRecord task) {
3082 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3083 }
3084
3085 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003086 if (!mLockTaskModeTasks.remove(task)) {
3087 return;
3088 }
3089 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3090 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003091 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003092 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3093 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003094 final Message lockTaskMsg = Message.obtain();
3095 lockTaskMsg.arg1 = task.userId;
3096 lockTaskMsg.what = LOCK_TASK_END_MSG;
3097 mHandler.sendMessage(lockTaskMsg);
3098 }
3099 }
3100
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003101 void showNonResizeableDockToast(int taskId) {
Chong Zhangc806d902015-11-30 09:44:27 -08003102 mWindowManager.scheduleShowNonResizeableDockToast(taskId);
Chong Zhangb15758a2015-11-17 12:12:03 -08003103 }
3104
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003105 void showLockTaskToast() {
3106 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04003107 }
3108
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003109 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3110 if (mLockTaskModeTasks.contains(task)) {
3111 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3112 }
3113 }
3114
Craig Mautner432f64e2015-05-20 14:59:57 -07003115 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3116 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003117 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003118 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003119 final TaskRecord lockedTask = getLockedTaskLocked();
3120 if (lockedTask != null) {
3121 removeLockedTaskLocked(lockedTask);
3122 if (!mLockTaskModeTasks.isEmpty()) {
3123 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003124 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3125 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003126 lockedTask.performClearTaskLocked();
3127 resumeTopActivitiesLocked();
3128 return;
3129 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003130 }
Craig Mautnere0570202015-05-13 13:06:11 -07003131 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3132 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003133 return;
3134 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003135
3136 // Should have already been checked, but do it again.
3137 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003138 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3139 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003140 return;
3141 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003142 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003143 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003144 return;
3145 }
3146
3147 if (mLockTaskModeTasks.isEmpty()) {
3148 // First locktask.
3149 final Message lockTaskMsg = Message.obtain();
3150 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3151 lockTaskMsg.arg1 = task.userId;
3152 lockTaskMsg.what = LOCK_TASK_START_MSG;
3153 lockTaskMsg.arg2 = lockTaskModeState;
3154 mHandler.sendMessage(lockTaskMsg);
3155 }
3156 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003157 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3158 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003159 mLockTaskModeTasks.remove(task);
3160 mLockTaskModeTasks.add(task);
3161
3162 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07003163 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07003164 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003165
3166 if (andResume) {
3167 findTaskToMoveToFrontLocked(task, 0, null, reason);
3168 resumeTopActivitiesLocked();
3169 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003170 }
3171
3172 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003173 return isLockTaskModeViolation(task, false);
3174 }
3175
3176 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3177 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003178 return false;
3179 }
3180 final int lockTaskAuth = task.mLockTaskAuth;
3181 switch (lockTaskAuth) {
3182 case LOCK_TASK_AUTH_DONT_LOCK:
3183 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003184 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003185 case LOCK_TASK_AUTH_LAUNCHABLE:
3186 case LOCK_TASK_AUTH_WHITELISTED:
3187 return false;
3188 case LOCK_TASK_AUTH_PINNABLE:
3189 // Pinnable tasks can't be launched on top of locktask tasks.
3190 return !mLockTaskModeTasks.isEmpty();
3191 default:
3192 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3193 return true;
3194 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003195 }
3196
Craig Mautner15df08a2015-04-01 12:17:18 -07003197 void onLockTaskPackagesUpdatedLocked() {
3198 boolean didSomething = false;
3199 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3200 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01003201 final boolean wasWhitelisted =
3202 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3203 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07003204 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01003205 final boolean isWhitelisted =
3206 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3207 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3208 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003209 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003210 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3211 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003212 removeLockedTaskLocked(lockedTask);
3213 lockedTask.performClearTaskLocked();
3214 didSomething = true;
3215 }
3216 }
3217 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3218 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3219 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3220 final ActivityStack stack = stacks.get(stackNdx);
3221 stack.onLockTaskPackagesUpdatedLocked();
3222 }
3223 }
Craig Mautnere0570202015-05-13 13:06:11 -07003224 final ActivityRecord r = topRunningActivityLocked();
3225 final TaskRecord task = r != null ? r.task : null;
3226 if (mLockTaskModeTasks.isEmpty() && task != null
3227 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3228 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07003229 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
3230 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
3231 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
3232 false);
3233 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07003234 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003235 if (didSomething) {
3236 resumeTopActivitiesLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003237 }
3238 }
3239
Benjamin Franz43261142015-02-11 15:59:44 +00003240 int getLockTaskModeState() {
3241 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003242 }
3243
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003244 void logStackState() {
3245 mActivityMetricsLogger.logWindowState();
3246 }
3247
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003248 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003249
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003250 public ActivityStackSupervisorHandler(Looper looper) {
3251 super(looper);
3252 }
3253
Craig Mautnerf3333272013-04-22 10:55:53 -07003254 void activityIdleInternal(ActivityRecord r) {
3255 synchronized (mService) {
3256 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3257 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003258 }
3259
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003260 @Override
3261 public void handleMessage(Message msg) {
3262 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003263 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003264 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3265 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003266 if (mService.mDidDexOpt) {
3267 mService.mDidDexOpt = false;
3268 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3269 nmsg.obj = msg.obj;
3270 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3271 return;
3272 }
3273 // We don't at this point know if the activity is fullscreen,
3274 // so we need to be conservative and assume it isn't.
3275 activityIdleInternal((ActivityRecord)msg.obj);
3276 } break;
3277 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003278 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003279 activityIdleInternal((ActivityRecord)msg.obj);
3280 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003281 case RESUME_TOP_ACTIVITY_MSG: {
3282 synchronized (mService) {
3283 resumeTopActivitiesLocked();
3284 }
3285 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003286 case SLEEP_TIMEOUT_MSG: {
3287 synchronized (mService) {
3288 if (mService.isSleepingOrShuttingDown()) {
3289 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3290 mSleepTimeout = true;
3291 checkReadyForSleepLocked();
3292 }
3293 }
3294 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003295 case LAUNCH_TIMEOUT_MSG: {
3296 if (mService.mDidDexOpt) {
3297 mService.mDidDexOpt = false;
3298 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3299 return;
3300 }
3301 synchronized (mService) {
3302 if (mLaunchingActivity.isHeld()) {
3303 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3304 if (VALIDATE_WAKE_LOCK_CALLER
3305 && Binder.getCallingUid() != Process.myUid()) {
3306 throw new IllegalStateException("Calling must be system uid");
3307 }
3308 mLaunchingActivity.release();
3309 }
3310 }
3311 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003312 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003313 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003314 } break;
3315 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003316 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003317 } break;
3318 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003319 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003320 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003321 case CONTAINER_CALLBACK_VISIBILITY: {
3322 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003323 final IActivityContainerCallback callback = container.mCallback;
3324 if (callback != null) {
3325 try {
3326 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3327 } catch (RemoteException e) {
3328 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003329 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003330 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003331 case LOCK_TASK_START_MSG: {
3332 // When lock task starts, we disable the status bars.
3333 try {
Jason Monk62515be2014-05-21 16:06:19 -04003334 if (mLockTaskNotify == null) {
3335 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003336 }
Jason Monk62515be2014-05-21 16:06:19 -04003337 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003338 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003339 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003340 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003341 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003342 flags = StatusBarManager.DISABLE_MASK
3343 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07003344 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003345 flags = StatusBarManager.DISABLE_MASK
3346 & (~StatusBarManager.DISABLE_BACK)
3347 & (~StatusBarManager.DISABLE_HOME)
3348 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003349 }
3350 getStatusBarService().disable(flags, mToken,
3351 mService.mContext.getPackageName());
3352 }
3353 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003354 if (getDevicePolicyManager() != null) {
3355 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003356 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003357 }
justinzhang5286d3f2014-05-12 17:06:01 -04003358 } catch (RemoteException ex) {
3359 throw new RuntimeException(ex);
3360 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003361 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003362 case LOCK_TASK_END_MSG: {
3363 // When lock task ends, we enable the status bars.
3364 try {
Jason Monk62515be2014-05-21 16:06:19 -04003365 if (getStatusBarService() != null) {
3366 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3367 mService.mContext.getPackageName());
3368 }
3369 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003370 if (getDevicePolicyManager() != null) {
3371 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3372 msg.arg1);
3373 }
Jason Monk62515be2014-05-21 16:06:19 -04003374 if (mLockTaskNotify == null) {
3375 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3376 }
3377 mLockTaskNotify.show(false);
3378 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003379 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003380 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003381 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003382 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003383 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003384 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003385 new LockPatternUtils(mService.mContext)
3386 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003387 }
3388 } catch (SettingNotFoundException e) {
3389 // No setting, don't lock.
3390 }
justinzhang5286d3f2014-05-12 17:06:01 -04003391 } catch (RemoteException ex) {
3392 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003393 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07003394 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003395 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003396 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003397 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
3398 if (mLockTaskNotify == null) {
3399 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3400 }
3401 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
3402 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003403 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3404 final ActivityContainer container = (ActivityContainer) msg.obj;
3405 final IActivityContainerCallback callback = container.mCallback;
3406 if (callback != null) {
3407 try {
3408 callback.onAllActivitiesComplete(container.asBinder());
3409 } catch (RemoteException e) {
3410 }
3411 }
3412 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003413 case LAUNCH_TASK_BEHIND_COMPLETE: {
3414 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003415 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07003416 if (r != null) {
3417 handleLaunchTaskBehindCompleteLocked(r);
3418 }
3419 }
3420 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08003421
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003422 }
3423 }
3424 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003425
Ying Wangb081a592014-04-22 15:20:16 -07003426 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08003427 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
3428 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003429 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003430 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003431 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003432 ActivityRecord mParentActivity = null;
3433 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003434
Craig Mautnere3a00d72014-04-16 08:31:19 -07003435 boolean mVisible = true;
3436
Craig Mautner4a1cb222013-12-04 16:14:06 -08003437 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003438 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003439
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003440 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3441 final static int CONTAINER_STATE_NO_SURFACE = 1;
3442 final static int CONTAINER_STATE_FINISHING = 2;
3443 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3444
3445 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003446 synchronized (mService) {
3447 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003448 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003449 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003450 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003451 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003452 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003453
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003454 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003455 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003456 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08003457 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003458 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003459 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003460 }
3461
3462 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003463 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003464 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003465 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3466 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003467 return;
3468 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003469 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003470 }
3471 }
3472
3473 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003474 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003475 synchronized (mService) {
3476 if (mActivityDisplay != null) {
3477 return mActivityDisplay.mDisplayId;
3478 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003479 }
3480 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003481 }
3482
Jeff Brownca9bc702014-02-11 14:32:56 -08003483 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08003484 public int getStackId() {
3485 synchronized (mService) {
3486 return mStackId;
3487 }
3488 }
3489
3490 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003491 public boolean injectEvent(InputEvent event) {
3492 final long origId = Binder.clearCallingIdentity();
3493 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003494 synchronized (mService) {
3495 if (mActivityDisplay != null) {
3496 return mInputManagerInternal.injectInputEvent(event,
3497 mActivityDisplay.mDisplayId,
3498 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3499 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003500 }
3501 return false;
3502 } finally {
3503 Binder.restoreCallingIdentity(origId);
3504 }
3505 }
3506
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003507 @Override
3508 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003509 synchronized (mService) {
3510 if (mContainerState == CONTAINER_STATE_FINISHING) {
3511 return;
3512 }
3513 mContainerState = CONTAINER_STATE_FINISHING;
3514
Craig Mautnerd163e752014-06-13 17:18:47 -07003515 long origId = Binder.clearCallingIdentity();
3516 try {
Craig Mautneree36c772014-07-16 14:56:05 -07003517 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003518 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07003519 } finally {
3520 Binder.restoreCallingIdentity(origId);
3521 }
3522 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003523 }
3524
Craig Mautner60257702014-09-17 15:02:33 -07003525 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003526 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08003527 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08003528 if (mActivityDisplay != null) {
3529 mActivityDisplay.detachActivitiesLocked(mStack);
3530 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003531 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003532 }
3533 }
3534
3535 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08003536 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003537 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003538 }
3539
3540 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07003541 public final int startActivityIntentSender(IIntentSender intentSender)
3542 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003543 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3544
3545 if (!(intentSender instanceof PendingIntentRecord)) {
3546 throw new IllegalArgumentException("Bad PendingIntent object");
3547 }
3548
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07003549 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003550 Binder.getCallingUid(), mCurrentUser, false,
3551 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08003552
3553 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3554 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
3555 pendingIntent.key.requestResolvedType);
3556
3557 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
3558 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
3559 }
3560
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003561 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07003562 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003563 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003564 throw new SecurityException(
3565 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3566 }
3567 }
3568
Craig Mautnerdf88d732014-01-27 09:21:32 -08003569 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003570 public IBinder asBinder() {
3571 return this;
3572 }
3573
Craig Mautner4504de52013-12-20 09:06:56 -08003574 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003575 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003576 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08003577 }
3578
Craig Mautner4a1cb222013-12-04 16:14:06 -08003579 ActivityStackSupervisor getOuter() {
3580 return ActivityStackSupervisor.this;
3581 }
3582
Craig Mautnerd163e752014-06-13 17:18:47 -07003583 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003584 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003585 }
3586
Craig Mautner6985bad2014-04-21 15:22:06 -07003587 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07003588 void setVisible(boolean visible) {
3589 if (mVisible != visible) {
3590 mVisible = visible;
3591 if (mCallback != null) {
3592 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
3593 0 /* unused */, this).sendToTarget();
3594 }
3595 }
3596 }
3597
Craig Mautner6985bad2014-04-21 15:22:06 -07003598 void setDrawn() {
3599 }
3600
Craig Mautner1b4bf852014-05-26 15:06:32 -07003601 // You can always start a new task on a regular ActivityStack.
3602 boolean isEligibleForNewTasks() {
3603 return true;
3604 }
3605
Craig Mautnerd163e752014-06-13 17:18:47 -07003606 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07003607 detachLocked();
3608 deleteActivityContainer(this);
3609 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003610 }
3611
Craig Mautner34b73df2014-01-12 21:11:08 -08003612 @Override
3613 public String toString() {
3614 return mIdString + (mActivityDisplay == null ? "N" : "A");
3615 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003616 }
3617
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003618 private class VirtualActivityContainer extends ActivityContainer {
3619 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07003620 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003621
3622 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
3623 super(getNextStackId());
3624 mParentActivity = parent;
3625 mCallback = callback;
3626 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07003627 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003628 }
3629
3630 @Override
3631 public void setSurface(Surface surface, int width, int height, int density) {
3632 super.setSurface(surface, width, height, density);
3633
3634 synchronized (mService) {
3635 final long origId = Binder.clearCallingIdentity();
3636 try {
3637 setSurfaceLocked(surface, width, height, density);
3638 } finally {
3639 Binder.restoreCallingIdentity(origId);
3640 }
3641 }
3642 }
3643
3644 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
3645 if (mContainerState == CONTAINER_STATE_FINISHING) {
3646 return;
3647 }
3648 VirtualActivityDisplay virtualActivityDisplay =
3649 (VirtualActivityDisplay) mActivityDisplay;
3650 if (virtualActivityDisplay == null) {
3651 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07003652 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003653 mActivityDisplay = virtualActivityDisplay;
3654 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003655 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003656 }
3657
3658 if (mSurface != null) {
3659 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003660 }
3661
Craig Mautner6985bad2014-04-21 15:22:06 -07003662 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003663 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003664 mStack.resumeTopActivityLocked(null);
3665 } else {
3666 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07003667 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003668 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003669 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003670 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003671 }
Craig Mautner6985bad2014-04-21 15:22:06 -07003672
Craig Mautnerd163e752014-06-13 17:18:47 -07003673 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003674
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003675 if (DEBUG_STACK) Slog.d(TAG_STACK,
3676 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07003677 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003678
Craig Mautner6985bad2014-04-21 15:22:06 -07003679 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07003680 boolean isAttachedLocked() {
3681 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07003682 }
3683
3684 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07003685 void setDrawn() {
3686 synchronized (mService) {
3687 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07003688 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003689 }
3690 }
3691
Craig Mautner1b4bf852014-05-26 15:06:32 -07003692 // Never start a new task on an ActivityView if it isn't explicitly specified.
3693 @Override
3694 boolean isEligibleForNewTasks() {
3695 return false;
3696 }
3697
Craig Mautnerd163e752014-06-13 17:18:47 -07003698 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003699 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07003700 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
3701 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
3702 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
3703 mContainerState = CONTAINER_STATE_HAS_SURFACE;
3704 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003705 }
3706 }
3707
Craig Mautner4a1cb222013-12-04 16:14:06 -08003708 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
3709 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003710 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003711 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08003712 int mDisplayId;
3713 Display mDisplay;
3714 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08003715
Craig Mautner4a1cb222013-12-04 16:14:06 -08003716 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
3717 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08003718 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003719
Jose Lima4b6c6692014-08-12 17:41:12 -07003720 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003721
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003722 ActivityDisplay() {
3723 }
Craig Mautner4504de52013-12-20 09:06:56 -08003724
Craig Mautner1a70a162014-09-13 12:09:31 -07003725 // After instantiation, check that mDisplay is not null before using this. The alternative
3726 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08003727 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07003728 final Display display = mDisplayManager.getDisplay(displayId);
3729 if (display == null) {
3730 return;
3731 }
3732 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08003733 }
3734
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003735 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08003736 mDisplay = display;
3737 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003738 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08003739 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003740
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003741 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003742 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003743 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
3744 + " onTop=" + onTop);
3745 if (onTop) {
3746 mStacks.add(stack);
3747 } else {
3748 mStacks.add(0, stack);
3749 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003750 }
3751
Craig Mautnere0a38842013-12-16 16:14:02 -08003752 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003753 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08003754 + " from displayId=" + mDisplayId);
3755 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003756 }
3757
Jose Lima4b6c6692014-08-12 17:41:12 -07003758 void setVisibleBehindActivity(ActivityRecord r) {
3759 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003760 }
3761
Jose Lima4b6c6692014-08-12 17:41:12 -07003762 boolean hasVisibleBehindActivity() {
3763 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003764 }
3765
Craig Mautner34b73df2014-01-12 21:11:08 -08003766 @Override
3767 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003768 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
3769 }
3770 }
3771
3772 class VirtualActivityDisplay extends ActivityDisplay {
3773 VirtualDisplay mVirtualDisplay;
3774
Craig Mautner6985bad2014-04-21 15:22:06 -07003775 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003776 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07003777 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
3778 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
3779 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
3780 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003781
3782 init(mVirtualDisplay.getDisplay());
3783
3784 mWindowManager.handleDisplayAdded(mDisplayId);
3785 }
3786
3787 void setSurface(Surface surface) {
3788 if (mVirtualDisplay != null) {
3789 mVirtualDisplay.setSurface(surface);
3790 }
3791 }
3792
3793 @Override
3794 void detachActivitiesLocked(ActivityStack stack) {
3795 super.detachActivitiesLocked(stack);
3796 if (mVirtualDisplay != null) {
3797 mVirtualDisplay.release();
3798 mVirtualDisplay = null;
3799 }
3800 }
3801
3802 @Override
3803 public String toString() {
3804 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08003805 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003806 }
Jose Lima58e66d62014-05-27 20:00:27 -07003807
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07003808 /**
3809 * Adjust bounds to stay within stack bounds.
3810 *
3811 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
3812 * that keep them unchanged, but be contained within the stack bounds.
3813 *
3814 * @param bounds Bounds to be adjusted.
3815 * @param stackBounds Bounds within which the other bounds should remain.
3816 */
3817 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
3818 if (stackBounds == null || stackBounds.contains(bounds)) {
3819 return;
3820 }
3821
3822 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
3823 final int maxRight = stackBounds.right
3824 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
3825 int horizontalDiff = stackBounds.left - bounds.left;
3826 if ((horizontalDiff < 0 && bounds.left >= maxRight)
3827 || (bounds.left + horizontalDiff >= maxRight)) {
3828 horizontalDiff = maxRight - bounds.left;
3829 }
3830 bounds.left += horizontalDiff;
3831 bounds.right += horizontalDiff;
3832 }
3833
3834 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
3835 final int maxBottom = stackBounds.bottom
3836 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
3837 int verticalDiff = stackBounds.top - bounds.top;
3838 if ((verticalDiff < 0 && bounds.top >= maxBottom)
3839 || (bounds.top + verticalDiff >= maxBottom)) {
3840 verticalDiff = maxBottom - bounds.top;
3841 }
3842 bounds.top += verticalDiff;
3843 bounds.bottom += verticalDiff;
3844 }
3845 }
3846
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08003847 ActivityStack findStackBehind(ActivityStack stack) {
3848 // TODO(multi-display): We are only looking for stacks on the default display.
3849 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
3850 if (display == null) {
3851 return null;
3852 }
3853 final ArrayList<ActivityStack> stacks = display.mStacks;
3854 for (int i = stacks.size() - 1; i >= 0; i--) {
3855 if (stacks.get(i) == stack && i > 0) {
3856 return stacks.get(i - 1);
3857 }
3858 }
3859 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
3860 + " in=" + stacks);
3861 }
Craig Mautner27084302013-03-25 08:05:25 -07003862}