blob: 1d589e6229473db27b0e0d6e8f4b5b083c214560 [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
Andrii Kuliane6ac20e2017-03-17 10:30:50 -070019import static android.Manifest.permission.MANAGE_ACTIVITY_STACKS;
Jorim Jaggife762342016-10-13 14:33:27 +020020import static android.Manifest.permission.START_ANY_ACTIVITY;
21import static android.Manifest.permission.START_TASKS_FROM_RECENTS;
22import static android.app.ActivityManager.LOCK_TASK_MODE_LOCKED;
23import static android.app.ActivityManager.LOCK_TASK_MODE_NONE;
24import static android.app.ActivityManager.LOCK_TASK_MODE_PINNED;
Jorim Jaggife762342016-10-13 14:33:27 +020025import static android.app.ActivityManager.START_TASK_TO_FRONT;
26import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
27import static android.app.ActivityManager.StackId.FIRST_DYNAMIC_STACK_ID;
28import static android.app.ActivityManager.StackId.FIRST_STATIC_STACK_ID;
29import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
30import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
31import static android.app.ActivityManager.StackId.HOME_STACK_ID;
32import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
33import static android.app.ActivityManager.StackId.LAST_STATIC_STACK_ID;
34import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Matthew Ng330757d2017-02-28 14:19:17 -080035import static android.app.ActivityManager.StackId.RECENTS_STACK_ID;
Jorim Jaggife762342016-10-13 14:33:27 +020036import static android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
37import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
38import static android.content.pm.PackageManager.PERMISSION_GRANTED;
39import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
40import static android.view.Display.DEFAULT_DISPLAY;
Andrii Kulian16802aa2016-11-02 12:21:33 -070041import static android.view.Display.FLAG_PRIVATE;
42import static android.view.Display.INVALID_DISPLAY;
Andrii Kulian250d6532017-02-08 23:30:45 -080043import static android.view.Display.REMOVE_MODE_DESTROY_CONTENT;
Jorim Jaggife762342016-10-13 14:33:27 +020044
Winson Chung47900652017-04-06 18:44:25 -070045import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN;
Jorim Jaggife762342016-10-13 14:33:27 +020046import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
47import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONTAINERS;
48import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_FOCUS;
49import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_IDLE;
50import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKTASK;
51import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
52import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
53import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
54import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
55import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
56import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
57import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
58import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBLE_BEHIND;
59import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONTAINERS;
60import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_FOCUS;
61import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
62import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_LOCKTASK;
63import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
64import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
65import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
66import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
67import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
68import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
69import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
70import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBLE_BEHIND;
71import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
72import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
73import static com.android.server.am.ActivityManagerService.ANIMATE;
74import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
75import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
76import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
Jorim Jaggife762342016-10-13 14:33:27 +020077import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
78import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
79import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
80import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
81import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
82import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
83import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
84import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
85import static com.android.server.am.ActivityStack.REMOVE_TASK_MODE_MOVING;
86import static com.android.server.am.ActivityStack.STACK_INVISIBLE;
87import static com.android.server.am.ActivityStack.STACK_VISIBLE;
88import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
89import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
90import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
91import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
92import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Winson Chung74666102017-02-22 17:49:24 -080093import static com.android.server.am.TaskRecord.REPARENT_KEEP_STACK_AT_FRONT;
94import static com.android.server.am.TaskRecord.REPARENT_LEAVE_STACK_IN_PLACE;
95import static com.android.server.am.TaskRecord.REPARENT_MOVE_STACK_TO_FRONT;
Jorim Jaggife762342016-10-13 14:33:27 +020096import static com.android.server.wm.AppTransition.TRANSIT_DOCK_TASK_FROM_RECENTS;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -080097import static java.lang.Integer.MAX_VALUE;
Jorim Jaggife762342016-10-13 14:33:27 +020098
Svetoslav7008b512015-06-24 18:47:07 -070099import android.Manifest;
Winson Chung7900bee2017-03-10 08:59:25 -0800100import android.annotation.IntDef;
Andrii Kulian16802aa2016-11-02 12:21:33 -0700101import android.annotation.NonNull;
Tony Mak853304c2016-04-18 15:17:41 +0100102import android.annotation.UserIdInt;
Craig Mautner2420ead2013-04-01 17:13:20 -0700103import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700104import android.app.ActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -0800105import android.app.ActivityManager.RunningTaskInfo;
Wale Ogunwale3797c222015-10-27 14:21:58 -0700106import android.app.ActivityManager.StackId;
Craig Mautnered6649f2013-12-02 14:08:25 -0800107import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700108import android.app.ActivityOptions;
Svetoslav7008b512015-06-24 18:47:07 -0700109import android.app.AppOpsManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800110import android.app.IActivityContainerCallback;
Jeff Hao1b012d32014-08-20 10:35:34 -0700111import android.app.ProfilerInfo;
Craig Mautner2420ead2013-04-01 17:13:20 -0700112import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -0400113import android.app.StatusBarManager;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700114import android.app.WaitResult;
Jason Monk35c62a42014-06-17 10:24:47 -0400115import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700116import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700117import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700118import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700119import android.content.Intent;
120import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700121import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -0700122import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700123import android.content.pm.PackageManager;
124import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100125import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700126import android.content.res.Configuration;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800127import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800128import android.hardware.display.DisplayManager;
129import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -0800130import android.hardware.display.DisplayManagerGlobal;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800131import android.hardware.display.DisplayManagerInternal;
Craig Mautner4504de52013-12-20 09:06:56 -0800132import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -0800133import android.hardware.input.InputManager;
134import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700135import android.os.Binder;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100136import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700137import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700138import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700139import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700140import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -0700141import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700142import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700143import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700144import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700145import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -0400146import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700147import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -0700148import android.os.Trace;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -0700149import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -0700150import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100151import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700152import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -0700153import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -0400154import android.provider.Settings;
155import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700156import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -0700157import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700158import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -0700159import android.util.EventLog;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800160import android.util.IntArray;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700161import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800162import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800163import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800164import android.view.Display;
Jeff Brownca9bc702014-02-11 14:32:56 -0800165import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800166import android.view.Surface;
Chong Zhangb15758a2015-11-17 12:12:03 -0800167
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800168import com.android.internal.content.ReferrerIntent;
Winson Chung14fbe142016-12-19 16:18:24 -0800169import com.android.internal.logging.MetricsLogger;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700170import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400171import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -0700172import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -0400173import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800174import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700175import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700176import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700177
Craig Mautner8d341ef2013-03-26 09:03:27 -0700178import java.io.FileDescriptor;
179import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700180import java.io.PrintWriter;
Winson Chung7900bee2017-03-10 08:59:25 -0800181import java.lang.annotation.Retention;
182import java.lang.annotation.RetentionPolicy;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700183import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700184import java.util.Arrays;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700185import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700186import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700187
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800188public class ActivityStackSupervisor extends ConfigurationContainer implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800189 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700190 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Chong Zhang6cda19c2016-06-14 19:07:56 -0700191 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700192 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700193 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700194 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700195 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700196 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700197 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700198 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700199 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800200 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700201 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800202
Craig Mautnerf3333272013-04-22 10:55:53 -0700203 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700204 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700205
Craig Mautner0eea92c2013-05-16 13:35:39 -0700206 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700207 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700208
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700209 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700210 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700211
Craig Mautner05d29032013-05-03 13:40:13 -0700212 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
213 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
214 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700215 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700216 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800217 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
218 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
219 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700220 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400221 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
222 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700223 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700224 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700225 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800226 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
227 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800228
Wale Ogunwale040b4702015-08-06 18:10:50 -0700229 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700230
Jason Monk62515be2014-05-21 16:06:19 -0400231 private static final String LOCK_TASK_TAG = "Lock-to-App";
232
Wale Ogunwale040b4702015-08-06 18:10:50 -0700233 // Used to indicate if an object (e.g. stack) that we are trying to get
234 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800235 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700236
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700237 // Used to indicate that windows of activities should be preserved during the resize.
238 static final boolean PRESERVE_WINDOWS = true;
239
Wale Ogunwale040b4702015-08-06 18:10:50 -0700240 // Used to indicate if an object (e.g. task) should be moved/created
241 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700242 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700243
244 // Used to indicate that an objects (e.g. task) removal from its container
245 // (e.g. stack) is due to it moving to another container.
246 static final boolean MOVING = true;
247
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700248 // Force the focus to change to the stack we are moving a task to..
249 static final boolean FORCE_FOCUS = true;
250
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700251 // Don't execute any calls to resume.
252 static final boolean DEFER_RESUME = true;
253
Winson Chung010927a2016-12-15 16:12:35 -0800254 // Used to indicate that a task is removed it should also be removed from recents.
255 static final boolean REMOVE_FROM_RECENTS = true;
256
Winson Chung6954fc92017-03-24 16:22:12 -0700257 // Used to indicate that pausing an activity should occur immediately without waiting for
258 // the activity callback indicating that it has completed pausing
259 static final boolean PAUSE_IMMEDIATELY = true;
260
Winson Chung7900bee2017-03-10 08:59:25 -0800261 /**
262 * The modes which affect which tasks are returned when calling
263 * {@link ActivityStackSupervisor#anyTaskForIdLocked(int)}.
264 */
265 @Retention(RetentionPolicy.SOURCE)
266 @IntDef({
267 MATCH_TASK_IN_STACKS_ONLY,
268 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
269 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
270 })
271 public @interface AnyTaskForIdMatchTaskMode {}
272 // Match only tasks in the current stacks
273 static final int MATCH_TASK_IN_STACKS_ONLY = 0;
274 // Match either tasks in the current stacks, or in the recent tasks if not found in the stacks
275 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS = 1;
276 // Match either tasks in the current stacks, or in the recent tasks, restoring it to the
277 // provided stack id
278 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE = 2;
279
Svetoslav7008b512015-06-24 18:47:07 -0700280 // Activity actions an app cannot start if it uses a permission which is not granted.
281 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
282 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700283
Svetoslav7008b512015-06-24 18:47:07 -0700284 static {
285 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
286 Manifest.permission.CAMERA);
287 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
288 Manifest.permission.CAMERA);
289 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
290 Manifest.permission.CALL_PHONE);
291 }
292
Svet Ganov99b60432015-06-27 13:15:22 -0700293 /** Action restriction: launching the activity is not restricted. */
294 private static final int ACTIVITY_RESTRICTION_NONE = 0;
295 /** Action restriction: launching the activity is restricted by a permission. */
296 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
297 /** Action restriction: launching the activity is restricted by an app op. */
298 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700299
justinzhang5286d3f2014-05-12 17:06:01 -0400300 /** Status Bar Service **/
301 private IBinder mToken = new Binder();
302 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400303 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400304
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700305 // For debugging to make sure the caller when acquiring/releasing our
306 // wake lock is the system process.
307 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800308 /** The number of distinct task ids that can be assigned to the tasks of a single user */
309 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700310
Craig Mautner27084302013-03-25 08:05:25 -0700311 final ActivityManagerService mService;
312
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800313 private RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800314
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700315 final ActivityStackSupervisorHandler mHandler;
316
317 /** Short cut */
318 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800319 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700320
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700321 /** Counter for next free stack ID to use for dynamic activity stacks. */
322 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700323
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800324 /**
325 * Maps the task identifier that activities are currently being started in to the userId of the
326 * task. Each time a new task is created, the entry for the userId of the task is incremented
327 */
328 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700329
Craig Mautner2420ead2013-04-01 17:13:20 -0700330 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800331 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700332
Craig Mautnere0a38842013-12-16 16:14:02 -0800333 /** The stack containing the launcher app. Assumed to always be attached to
334 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800335 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700336
Craig Mautnere0a38842013-12-16 16:14:02 -0800337 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800338 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700339
Craig Mautner4a1cb222013-12-04 16:14:06 -0800340 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
341 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800342 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800343 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700344
345 /** List of activities that are waiting for a new activity to become visible before completing
346 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800347 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700348
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700349 /** List of processes waiting to find out about the next visible activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700350 final ArrayList<WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700351
352 /** List of processes waiting to find out about the next launched activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700353 final ArrayList<WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700354
Craig Mautnerde4ef022013-04-07 19:01:33 -0700355 /** List of activities that are ready to be stopped, but waiting for the next activity to
356 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800357 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700358
Craig Mautnerf3333272013-04-22 10:55:53 -0700359 /** List of activities that are ready to be finished, but waiting for the previous activity to
360 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800361 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700362
Craig Mautner0eea92c2013-05-16 13:35:39 -0700363 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800364 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700365
Wale Ogunwale22e25262016-02-01 10:32:02 -0800366 /** List of activities whose multi-window mode changed that we need to report to the
367 * application */
368 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
369
370 /** List of activities whose picture-in-picture mode changed that we need to report to the
371 * application */
372 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
373
Winson Chung5af42fc2017-03-24 17:11:33 -0700374 /** The target stack bounds for the picture-in-picture mode changed that we need to report to
375 * the application */
376 Rect mPipModeChangedTargetStackBounds;
377
Craig Mautnerf3333272013-04-22 10:55:53 -0700378 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700379 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700380
Craig Mautnerde4ef022013-04-07 19:01:33 -0700381 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
382 * is being brought in front of us. */
383 boolean mUserLeaving = false;
384
Craig Mautner0eea92c2013-05-16 13:35:39 -0700385 /** Set when we have taken too long waiting to go to sleep. */
386 boolean mSleepTimeout = false;
387
388 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700389 * We don't want to allow the device to go to sleep while in the process
390 * of launching an activity. This is primarily to allow alarm intent
391 * receivers to launch an activity and get that to run before the device
392 * goes back to sleep.
393 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700394 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700395
396 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700397 * Set when the system is going to sleep, until we have
398 * successfully paused the current activity and released our wake lock.
399 * At that point the system is allowed to actually sleep.
400 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700401 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700402
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700403 /** Stack id of the front stack when user switched, indexed by userId. */
404 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700405
Craig Mautner4504de52013-12-20 09:06:56 -0800406 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800407 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Wale Ogunwale1666e312016-12-16 11:27:18 -0800408 SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800409
Bryce Leeaf3a4002017-03-20 10:37:12 -0700410 // TODO: There should be an ActivityDisplayController coordinating am/wm interaction.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800411 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700412 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800413
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800414 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
415
416 private DisplayManagerInternal mDisplayManagerInternal;
417 private InputManagerInternal mInputManagerInternal;
Jeff Brownca9bc702014-02-11 14:32:56 -0800418
Craig Mautner15df08a2015-04-01 12:17:18 -0700419 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
420 * may be finished until there is only one entry left. If this is empty the system is not
421 * in lockTask mode. */
422 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000423 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700424 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
425 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000426 */
427 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400428 /**
429 * Notifies the user when entering/exiting lock-task.
430 */
431 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800432
Wale Ogunwaled046a012015-12-24 13:05:59 -0800433 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800434 boolean inResumeTopActivity;
435
Andrii Kulian1e32e022016-09-16 15:29:34 -0700436 /**
437 * Temporary rect used during docked stack resize calculation so we don't need to create a new
438 * object each time.
439 */
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700440 private final Rect tempRect = new Rect();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700441
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700442 // The default minimal size that will be used if the activity doesn't specify its minimal size.
443 // It will be calculated when the default display gets added.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700444 int mDefaultMinSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700445
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700446 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
447 private boolean mTaskLayersChanged = true;
448
Jorim Jaggi275561a2016-02-23 10:11:02 -0500449 final ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800450
Andrii Kulian1779e612016-10-12 21:58:25 -0700451 @Override
452 protected int getChildCount() {
453 return mActivityDisplays.size();
454 }
455
456 @Override
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700457 protected ActivityDisplay getChildAt(int index) {
Andrii Kulian1779e612016-10-12 21:58:25 -0700458 return mActivityDisplays.valueAt(index);
459 }
460
461 @Override
462 protected ConfigurationContainer getParent() {
463 return null;
464 }
465
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700466 Configuration getDisplayOverrideConfiguration(int displayId) {
467 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
468 if (activityDisplay == null) {
469 throw new IllegalArgumentException("No display found with id: " + displayId);
470 }
471
472 return activityDisplay.getOverrideConfiguration();
473 }
474
475 void setDisplayOverrideConfiguration(Configuration overrideConfiguration, int displayId) {
476 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
477 if (activityDisplay == null) {
478 throw new IllegalArgumentException("No display found with id: " + displayId);
479 }
480
481 activityDisplay.onOverrideConfigurationChanged(overrideConfiguration);
482 }
483
Wale Ogunwale39381972015-12-17 17:15:29 -0800484 static class FindTaskResult {
485 ActivityRecord r;
486 boolean matchedByRootAffinity;
487 }
488 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
489
Craig Mautneree36c772014-07-16 14:56:05 -0700490 /**
Andrii Kulian7fc22812016-12-28 13:04:11 -0800491 * Temp storage for display ids sorted in focus order.
492 * Maps position to id. Using {@link SparseIntArray} instead of {@link ArrayList} because
493 * it's more efficient, as the number of displays is usually small.
494 */
495 private SparseIntArray mTmpOrderedDisplayIds = new SparseIntArray();
496
497 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100498 * Used to keep track whether app visibilities got changed since the last pause. Useful to
499 * determine whether to invoke the task stack change listener after pausing.
500 */
501 boolean mAppVisibilitiesChangedSinceLastPause;
502
503 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100504 * Set of tasks that are in resizing mode during an app transition to fill the "void".
505 */
506 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
507
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700508
509 /**
510 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
511 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
512 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
513 * like the docked stack going empty.
514 */
515 private boolean mAllowDockedStackResize = true;
516
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100517 /**
Tony Mak853304c2016-04-18 15:17:41 +0100518 * Is dock currently minimized.
519 */
520 boolean mIsDockMinimized;
521
Jorim Jaggife762342016-10-13 14:33:27 +0200522 final KeyguardController mKeyguardController;
523
Tony Mak853304c2016-04-18 15:17:41 +0100524 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700525 * Description of a request to start a new activity, which has been held
526 * due to app switches being disabled.
527 */
528 static class PendingActivityLaunch {
529 final ActivityRecord r;
530 final ActivityRecord sourceRecord;
531 final int startFlags;
532 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700533 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700534
535 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700536 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700537 r = _r;
538 sourceRecord = _sourceRecord;
539 startFlags = _startFlags;
540 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700541 callerApp = _callerApp;
542 }
543
544 void sendErrorResult(String message) {
545 try {
546 if (callerApp.thread != null) {
547 callerApp.thread.scheduleCrash(message);
548 }
549 } catch (RemoteException e) {
550 Slog.e(TAG, "Exception scheduling crash of failed "
551 + "activity launcher sourceRecord=" + sourceRecord, e);
552 }
Craig Mautneree36c772014-07-16 14:56:05 -0700553 }
554 }
555
Bryce Leeaf691c02017-03-20 14:20:22 -0700556 public ActivityStackSupervisor(ActivityManagerService service, Looper looper) {
Craig Mautner27084302013-03-25 08:05:25 -0700557 mService = service;
Bryce Leeaf691c02017-03-20 14:20:22 -0700558 mHandler = new ActivityStackSupervisorHandler(looper);
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800559 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggife762342016-10-13 14:33:27 +0200560 mKeyguardController = new KeyguardController(service, this);
Jeff Brown2c43c332014-06-12 22:38:59 -0700561 }
562
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800563 void setRecentTasks(RecentTasks recentTasks) {
564 mRecentTasks = recentTasks;
565 }
566
Jeff Brown2c43c332014-06-12 22:38:59 -0700567 /**
568 * At the time when the constructor runs, the power manager has not yet been
569 * initialized. So we initialize our wakelocks afterwards.
570 */
571 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800572 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700573 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700574 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700575 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700576 }
577
justinzhang5286d3f2014-05-12 17:06:01 -0400578 // This function returns a IStatusBarService. The value is from ServiceManager.
579 // getService and is cached.
580 private IStatusBarService getStatusBarService() {
581 synchronized (mService) {
582 if (mStatusBarService == null) {
583 mStatusBarService = IStatusBarService.Stub.asInterface(
584 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
585 if (mStatusBarService == null) {
586 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
587 }
588 }
589 return mStatusBarService;
590 }
591 }
592
Jason Monk35c62a42014-06-17 10:24:47 -0400593 private IDevicePolicyManager getDevicePolicyManager() {
594 synchronized (mService) {
595 if (mDevicePolicyManager == null) {
596 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
597 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
598 if (mDevicePolicyManager == null) {
599 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
600 }
601 }
602 return mDevicePolicyManager;
603 }
604 }
605
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700606 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800607 synchronized (mService) {
608 mWindowManager = wm;
Jorim Jaggife762342016-10-13 14:33:27 +0200609 mKeyguardController.setWindowManager(wm);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800610
611 mDisplayManager =
612 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
613 mDisplayManager.registerDisplayListener(this, null);
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800614 mDisplayManagerInternal = LocalServices.getService(DisplayManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800615
616 Display[] displays = mDisplayManager.getDisplays();
617 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
618 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800619 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700620 if (activityDisplay.mDisplay == null) {
621 throw new IllegalStateException("Default Display does not exist");
622 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800623 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700624 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800625 }
626
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800627 mHomeStack = mFocusedStack = mLastFocusedStack =
628 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800629
630 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800631 }
Craig Mautner27084302013-03-25 08:05:25 -0700632 }
633
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700634 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800635 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700636 }
637
Craig Mautnerde4ef022013-04-07 19:01:33 -0700638 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800639 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700640 }
641
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700642 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700643 if (stack == null) {
644 return false;
645 }
646
Craig Mautnerdf88d732014-01-27 09:21:32 -0800647 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
648 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700649 stack = parent.getStack();
Craig Mautnerdf88d732014-01-27 09:21:32 -0800650 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800651 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700652 }
653
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800654 /** The top most stack on its display. */
655 boolean isFrontStackOnDisplay(ActivityStack stack) {
656 return isFrontOfStackList(stack, stack.mActivityContainer.mActivityDisplay.mStacks);
657 }
658
659 private boolean isFrontOfStackList(ActivityStack stack, List<ActivityStack> stackList) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700660 if (stack == null) {
661 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800662 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700663
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700664 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
665 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700666 stack = parent.getStack();
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800667 }
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800668 return stack == stackList.get((stackList.size() - 1));
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700669 }
670
Wale Ogunwaled046a012015-12-24 13:05:59 -0800671 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800672 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
673 if (!focusCandidate.isFocusable()) {
674 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
Andrii Kulian7fc22812016-12-28 13:04:11 -0800675 focusCandidate = getNextFocusableStackLocked(focusCandidate);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800676 }
677
678 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800679 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800680 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800681
Wale Ogunwaled046a012015-12-24 13:05:59 -0800682 EventLogTags.writeAmFocusedStack(
683 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
684 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
685 }
686
687 final ActivityRecord r = topRunningActivityLocked();
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800688 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800689 if (r != null && r.idle) {
690 checkFinishBootingLocked();
691 }
692 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700693 }
694
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700695 void moveHomeStackToFront(String reason) {
696 mHomeStack.moveToFront(reason);
697 }
698
Matthew Ng330757d2017-02-28 14:19:17 -0800699 void moveRecentsStackToFront(String reason) {
700 final ActivityStack recentsStack = getStack(RECENTS_STACK_ID);
701 if (recentsStack != null) {
702 recentsStack.moveToFront(reason);
703 }
704 }
705
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700706 /** Returns true if the focus activity was adjusted to the home stack top activity. */
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800707 boolean moveHomeStackTaskToTop(String reason) {
708 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700709
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700710 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700711 if (top == null) {
712 return false;
713 }
Chong Zhang6cda19c2016-06-14 19:07:56 -0700714 moveFocusableActivityStackToFrontLocked(top, reason);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700715 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700716 }
717
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800718 boolean resumeHomeStackTask(ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700719 if (!mService.mBooting && !mService.mBooted) {
720 // Not ready yet!
721 return false;
722 }
723
Craig Mautner84984fa2014-06-19 11:19:20 -0700724 if (prev != null) {
Bryce Leeaf691c02017-03-20 14:20:22 -0700725 prev.getTask().setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner84984fa2014-06-19 11:19:20 -0700726 }
727
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800728 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700729 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800730 final String myReason = reason + " resumeHomeStackTask";
731
Mark Lua56ea122015-10-08 13:31:01 +0800732 // Only resume home activity if isn't finishing.
733 if (r != null && !r.finishing) {
Chong Zhang6cda19c2016-06-14 19:07:56 -0700734 moveFocusableActivityStackToFrontLocked(r, myReason);
Wale Ogunwaled046a012015-12-24 13:05:59 -0800735 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700736 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800737 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700738 }
739
Craig Mautner8d341ef2013-03-26 09:03:27 -0700740 TaskRecord anyTaskForIdLocked(int id) {
Winson Chung7900bee2017-03-10 08:59:25 -0800741 return anyTaskForIdLocked(id, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
742 INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700743 }
744
745 /**
Bryce Lee92b7b652017-04-03 08:33:11 -0700746 * Returns a {@link TaskRecord} for the input id if available. {@code null} otherwise.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700747 * @param id Id of the task we would like returned.
Winson Chung7900bee2017-03-10 08:59:25 -0800748 * @param matchMode The mode to match the given task id in.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700749 * @param stackId The stack to restore the task to (default launch stack will be used if
Winson Chung7900bee2017-03-10 08:59:25 -0800750 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}). Only
751 * valid if the matchMode is
752 * {@link #MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE}.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700753 */
Winson Chung7900bee2017-03-10 08:59:25 -0800754 TaskRecord anyTaskForIdLocked(int id, @AnyTaskForIdMatchTaskMode int matchMode, int stackId) {
755 // If there is a stack id set, ensure that we are attempting to actually restore a task
756 if (matchMode != MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE &&
757 stackId != INVALID_STACK_ID) {
758 throw new IllegalArgumentException("Should not specify stackId for non-restore lookup");
759 }
760
Craig Mautnere0a38842013-12-16 16:14:02 -0800761 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800762 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800763 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800764 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
765 ActivityStack stack = stacks.get(stackNdx);
Bryce Lee92b7b652017-04-03 08:33:11 -0700766 final TaskRecord task = stack.taskForIdLocked(id);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800767 if (task != null) {
768 return task;
769 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700770 }
771 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800772
Winson Chung7900bee2017-03-10 08:59:25 -0800773 // If we are matching stack tasks only, return now
774 if (matchMode == MATCH_TASK_IN_STACKS_ONLY) {
Wale Ogunwale7de05352014-12-12 15:21:33 -0800775 return null;
776 }
777
Winson Chung7900bee2017-03-10 08:59:25 -0800778 // Otherwise, check the recent tasks and return if we find it there and we are not restoring
779 // the task from recents
780 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Bryce Lee92b7b652017-04-03 08:33:11 -0700781 final TaskRecord task = mRecentTasks.taskForIdLocked(id);
782
783 if (task == null) {
784 if (DEBUG_RECENTS) {
Winson Chung7900bee2017-03-10 08:59:25 -0800785 Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
786 }
Bryce Lee92b7b652017-04-03 08:33:11 -0700787
788 return null;
789 }
790
791 if (matchMode == MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700792 return task;
793 }
794
Winson Chung7900bee2017-03-10 08:59:25 -0800795 // Implicitly, this case is MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700796 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700797 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
798 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800799 return null;
800 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700801 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800802 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700803 }
804
Craig Mautner6170f732013-04-02 13:05:23 -0700805 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800806 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800807 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800808 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800809 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
810 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
811 if (r != null) {
812 return r;
813 }
Craig Mautner6170f732013-04-02 13:05:23 -0700814 }
815 }
816 return null;
817 }
818
Tony Mak853304c2016-04-18 15:17:41 +0100819 /**
Robin Lee3fef1f22016-12-20 14:50:13 +0000820 * Detects whether we should show a lock screen in front of this task for a locked user.
821 * <p>
822 * We'll do this if either of the following holds:
823 * <ul>
824 * <li>The top activity explicitly belongs to {@param userId}.</li>
825 * <li>The top activity returns a result to an activity belonging to {@param userId}.</li>
826 * </ul>
827 *
828 * @return {@code true} if the top activity looks like it belongs to {@param userId}.
Tony Mak853304c2016-04-18 15:17:41 +0100829 */
Robin Lee3fef1f22016-12-20 14:50:13 +0000830 private boolean taskTopActivityIsUser(TaskRecord task, @UserIdInt int userId) {
831 // To handle the case that work app is in the task but just is not the top one.
832 final ActivityRecord activityRecord = task.getTopActivity();
833 final ActivityRecord resultTo = (activityRecord != null ? activityRecord.resultTo : null);
834
835 return (activityRecord != null && activityRecord.userId == userId)
836 || (resultTo != null && resultTo.userId == userId);
837 }
838
839 /**
840 * Find all visible task stacks containing {@param userId} and intercept them with an activity
841 * to block out the contents and possibly start a credential-confirming intent.
842 *
843 * @param userId user handle for the locked managed profile.
844 */
845 void lockAllProfileTasks(@UserIdInt int userId) {
846 mWindowManager.deferSurfaceLayout();
847 try {
848 final List<ActivityStack> stacks = getStacks();
849 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; stackNdx--) {
850 final List<TaskRecord> tasks = stacks.get(stackNdx).getAllTasks();
851 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; taskNdx--) {
852 final TaskRecord task = tasks.get(taskNdx);
853
854 // Check the task for a top activity belonging to userId, or returning a result
855 // to an activity belonging to userId. Example case: a document picker for
856 // personal files, opened by a work app, should still get locked.
857 if (taskTopActivityIsUser(task, userId)) {
Robin Leec41f6ec2017-01-10 17:02:34 +0000858 mService.mTaskChangeNotificationController.notifyTaskProfileLocked(
859 task.taskId, userId);
Robin Lee3fef1f22016-12-20 14:50:13 +0000860 }
Tony Mak853304c2016-04-18 15:17:41 +0100861 }
862 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000863 } finally {
864 mWindowManager.continueSurfaceLayout();
Tony Mak853304c2016-04-18 15:17:41 +0100865 }
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000866 }
867
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800868 void setNextTaskIdForUserLocked(int taskId, int userId) {
869 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
870 if (taskId > currentTaskId) {
871 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700872 }
873 }
874
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700875 static int nextTaskIdForUser(int taskId, int userId) {
876 int nextTaskId = taskId + 1;
877 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
878 // Wrap around as there will be smaller task ids that are available now.
879 nextTaskId -= MAX_TASK_IDS_PER_USER;
880 }
881 return nextTaskId;
882 }
883
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800884 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800885 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
886 // for a userId u, a taskId can only be in the range
887 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
888 // was 10, user 0 could only have taskIds 0 to 9, user 1: 10 to 19, user 2: 20 to 29, so on.
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700889 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800890 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
Winson Chung7900bee2017-03-10 08:59:25 -0800891 || anyTaskForIdLocked(candidateTaskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800892 INVALID_STACK_ID) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700893 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800894 if (candidateTaskId == currentTaskId) {
895 // Something wrong!
896 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
897 throw new IllegalStateException("Cannot get an available task id."
898 + " Reached limit of " + MAX_TASK_IDS_PER_USER
899 + " running tasks per user.");
900 }
901 }
902 mCurTaskIdForUser.put(userId, candidateTaskId);
903 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700904 }
905
Chong Zhang6cda19c2016-06-14 19:07:56 -0700906 ActivityRecord getResumedActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800907 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700908 if (stack == null) {
909 return null;
910 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700911 ActivityRecord resumedActivity = stack.mResumedActivity;
912 if (resumedActivity == null || resumedActivity.app == null) {
913 resumedActivity = stack.mPausingActivity;
914 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700915 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700916 }
917 }
918 return resumedActivity;
919 }
920
Dianne Hackbornff072722014-09-24 10:56:28 -0700921 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700922 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800923 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800924 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
925 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800926 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
927 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700928 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800929 continue;
930 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700931 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800932 if (hr != null) {
933 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
934 && processName.equals(hr.processName)) {
935 try {
George Mount2c92c972014-03-20 09:38:23 -0700936 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800937 didSomething = true;
938 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700939 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800940 Slog.w(TAG, "Exception in new application when starting activity "
941 + hr.intent.getComponent().flattenToShortString(), e);
942 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700943 }
Craig Mautner20e72272013-04-01 13:45:53 -0700944 }
Craig Mautner20e72272013-04-01 13:45:53 -0700945 }
946 }
947 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700948 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700949 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700950 }
Craig Mautner20e72272013-04-01 13:45:53 -0700951 return didSomething;
952 }
953
954 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800955 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
956 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800957 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
958 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700959 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800960 continue;
961 }
962 final ActivityRecord resumedActivity = stack.mResumedActivity;
963 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700964 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800965 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800966 return false;
967 }
Craig Mautner20e72272013-04-01 13:45:53 -0700968 }
969 }
Wei Wang65c7a152016-06-02 18:51:22 -0700970 // Send launch end powerhint when idle
971 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
Craig Mautner20e72272013-04-01 13:45:53 -0700972 return true;
973 }
974
Craig Mautnerde4ef022013-04-07 19:01:33 -0700975 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800976 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
977 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800978 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
979 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700980 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800981 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700982 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800983 return false;
984 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700985 }
986 }
987 }
988 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700989 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800990 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
991 mLastFocusedStack + " to=" + mFocusedStack);
992 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700993 return true;
994 }
995
996 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800997 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800998 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
999 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001000 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1001 final ActivityStack stack = stacks.get(stackNdx);
1002 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +08001003 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -07001004 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +08001005 return false;
1006 }
1007 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001008 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001009 }
1010 }
riddle_hsudb46d6b2015-04-01 18:58:07 +08001011 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001012 }
1013
Craig Mautner2acc3892013-09-23 10:28:14 -07001014 /**
1015 * Pause all activities in either all of the stacks or just the back stacks.
1016 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Wale Ogunwale950faff2016-08-08 09:51:04 -07001017 * @param resuming The resuming activity.
1018 * @param dontWait The resuming activity isn't going to wait for all activities to be paused
1019 * before resuming.
Craig Mautner2acc3892013-09-23 10:28:14 -07001020 * @return true if any activity was paused as a result of this call.
1021 */
Wale Ogunwale950faff2016-08-08 09:51:04 -07001022 boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001023 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001024 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1025 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001026 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1027 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001028 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001029 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -08001030 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001031 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
1032 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001033 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001034 }
1035 }
1036 return someActivityPaused;
1037 }
1038
Craig Mautnerde4ef022013-04-07 19:01:33 -07001039 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001040 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -08001041 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1042 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001043 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1044 final ActivityStack stack = stacks.get(stackNdx);
1045 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001046 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001047 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001048 Slog.d(TAG_STATES,
1049 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001050 pausing = false;
1051 } else {
1052 return false;
1053 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001054 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001055 }
1056 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001057 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001058 }
1059
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001060 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
Wale Ogunwale950faff2016-08-08 09:51:04 -07001061 ActivityRecord resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -05001062 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -08001063 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1064 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1065 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1066 final ActivityStack stack = stacks.get(stackNdx);
1067 if (stack.mResumedActivity != null &&
1068 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001069 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -08001070 }
1071 }
1072 }
1073 }
1074
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001075 void cancelInitializingActivities() {
1076 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1077 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1078 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1079 stacks.get(stackNdx).cancelInitializingActivities();
1080 }
1081 }
1082 }
1083
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001084 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001085 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001086 }
1087
1088 void sendWaitingVisibleReportLocked(ActivityRecord r) {
1089 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -07001090 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001091 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001092 if (w.who == null) {
1093 changed = true;
1094 w.timeout = false;
1095 if (r != null) {
1096 w.who = new ComponentName(r.info.packageName, r.info.name);
1097 }
1098 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
1099 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001100 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001101 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001102 if (changed) {
1103 mService.notifyAll();
1104 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001105 }
1106
Chong Zhang5022da32016-06-21 16:31:37 -07001107 void reportTaskToFrontNoLaunch(ActivityRecord r) {
1108 boolean changed = false;
1109 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1110 WaitResult w = mWaitingActivityLaunched.remove(i);
1111 if (w.who == null) {
1112 changed = true;
1113 // Set result to START_TASK_TO_FRONT so that startActivityMayWait() knows that
1114 // the starting activity ends up moving another activity to front, and it should
1115 // wait for this new activity to become visible instead.
1116 // Do not modify other fields.
1117 w.result = START_TASK_TO_FRONT;
1118 }
1119 }
1120 if (changed) {
1121 mService.notifyAll();
1122 }
1123 }
1124
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001125 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1126 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001127 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001128 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001129 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001130 if (w.who == null) {
1131 changed = true;
1132 w.timeout = timeout;
1133 if (r != null) {
1134 w.who = new ComponentName(r.info.packageName, r.info.name);
1135 }
1136 w.thisTime = thisTime;
1137 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001138 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001139 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001140 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001141 if (changed) {
1142 mService.notifyAll();
1143 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001144 }
1145
Craig Mautner29219d92013-04-16 20:19:12 -07001146 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001147 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001148 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001149 if (r != null) {
1150 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001151 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001152
Andrii Kulian7318d632016-07-20 18:59:28 -07001153 // Look in other non-focused and non-home stacks.
Andrii Kulian7d95df42017-02-15 10:11:48 -08001154 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
1155
1156 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
1157 final int displayId = mTmpOrderedDisplayIds.get(i);
1158 final List<ActivityStack> stacks = mActivityDisplays.get(displayId).mStacks;
1159 if (stacks == null) {
1160 continue;
1161 }
1162 for (int j = stacks.size() - 1; j >= 0; --j) {
1163 final ActivityStack stack = stacks.get(j);
1164 if (stack != focusedStack && isFrontStackOnDisplay(stack) && stack.isFocusable()) {
1165 r = stack.topRunningActivityLocked();
1166 if (r != null) {
1167 return r;
1168 }
Craig Mautner29219d92013-04-16 20:19:12 -07001169 }
1170 }
1171 }
1172 return null;
1173 }
1174
Dianne Hackborn09233282014-04-30 11:33:59 -07001175 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001176 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001177 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
1178 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001179 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001180 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001181 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001182 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1183 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001184 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001185 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001186 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001187 }
1188 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001189
1190 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1191 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1192 while (maxNum > 0) {
1193 long mostRecentActiveTime = Long.MIN_VALUE;
1194 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001195 final int numTaskLists = runningTaskLists.size();
1196 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1197 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001198 if (!stackTaskList.isEmpty()) {
1199 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1200 if (lastActiveTime > mostRecentActiveTime) {
1201 mostRecentActiveTime = lastActiveTime;
1202 selectedStackList = stackTaskList;
1203 }
1204 }
1205 }
1206 if (selectedStackList != null) {
1207 list.add(selectedStackList.remove(0));
1208 --maxNum;
1209 } else {
1210 break;
1211 }
1212 }
Craig Mautner20e72272013-04-01 13:45:53 -07001213 }
1214
Todd Kennedy7440f172015-12-09 14:31:22 -08001215 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1216 ProfilerInfo profilerInfo) {
1217 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001218 if (aInfo != null) {
1219 // Store the found target back into the intent, because now that
1220 // we have it we never want to do this again. For example, if the
1221 // user navigates back to this point in the history, we should
1222 // always restart the exact same activity.
1223 intent.setComponent(new ComponentName(
1224 aInfo.applicationInfo.packageName, aInfo.name));
1225
1226 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001227 if (!aInfo.processName.equals("system")) {
1228 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001229 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001230 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001231
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001232 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1233 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1234 }
1235
Man Caocfa78b22015-06-11 20:14:34 -07001236 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1237 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1238 }
1239
1240 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001241 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001242 }
1243 }
Todd Kennedyb3b431302017-03-20 16:05:48 -07001244 final String intentLaunchToken = intent.getLaunchToken();
1245 if (aInfo.launchToken == null && intentLaunchToken != null) {
1246 aInfo.launchToken = intentLaunchToken;
1247 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001248 }
1249 return aInfo;
1250 }
1251
Todd Kennedy7440f172015-12-09 14:31:22 -08001252 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001253 return resolveIntent(intent, resolvedType, userId, 0);
1254 }
1255
1256 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy4d1de7d2017-02-23 10:32:18 -08001257 synchronized (mService) {
1258 return mService.getPackageManagerInternalLocked().resolveIntent(intent, resolvedType,
Todd Kennedybe0b8892017-02-15 14:13:52 -08001259 PackageManager.MATCH_INSTANT | PackageManager.MATCH_DEFAULT_ONLY | flags
Kenny Guyb1b30262016-02-09 16:02:35 +00001260 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001261 }
Todd Kennedy7440f172015-12-09 14:31:22 -08001262 }
1263
1264 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1265 ProfilerInfo profilerInfo, int userId) {
1266 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1267 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1268 }
1269
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001270 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1271 boolean andResume, boolean checkConfig) throws RemoteException {
1272
1273 if (!allPausedActivitiesComplete()) {
1274 // While there are activities pausing we skipping starting any new activities until
1275 // pauses are complete. NOTE: that we also do this for activities that are starting in
1276 // the paused state because they will first be resumed then paused on the client side.
1277 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1278 "realStartActivityLocked: Skipping start of r=" + r
1279 + " some activities pausing...");
1280 return false;
1281 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001282
Andrii Kulianada10292017-02-09 23:19:29 -08001283 r.startFreezingScreenLocked(app, 0);
1284 r.setVisibility(true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001285
Andrii Kulianada10292017-02-09 23:19:29 -08001286 // schedule launch ticks to collect information about slow apps.
1287 r.startLaunchTickingLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001288
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001289 // Have the window manager re-evaluate the orientation of the screen based on the new
1290 // activity order. Note that as a result of this, it can call back into the activity
1291 // manager with a new orientation. We don't care about that, because the activity is not
1292 // currently running so we are just restarting it anyway.
Craig Mautner2420ead2013-04-01 17:13:20 -07001293 if (checkConfig) {
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001294 final int displayId = r.getDisplayId();
1295 final Configuration config = mWindowManager.updateOrientationFromAppTokens(
1296 getDisplayOverrideConfiguration(displayId),
1297 r.mayFreezeScreenLocked(app) ? r.appToken : null, displayId);
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07001298 // Deferring resume here because we're going to launch new activity shortly.
1299 // We don't want to perform a redundant launch of the same record while ensuring
1300 // configurations and trying to resume top activity of focused stack.
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001301 mService.updateDisplayOverrideConfigurationLocked(config, r, true /* deferResume */,
1302 displayId);
Craig Mautner2420ead2013-04-01 17:13:20 -07001303 }
1304
Jorim Jaggi5a108c22016-10-13 14:33:27 +02001305 if (mKeyguardController.isKeyguardLocked()) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001306 r.notifyUnknownVisibilityLaunched();
Jorim Jaggi5a108c22016-10-13 14:33:27 +02001307 }
1308
Craig Mautner2420ead2013-04-01 17:13:20 -07001309 r.app = app;
1310 app.waitingToKill = null;
1311 r.launchCount++;
1312 r.lastLaunchTime = SystemClock.uptimeMillis();
1313
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001314 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001315
1316 int idx = app.activities.indexOf(r);
1317 if (idx < 0) {
1318 app.activities.add(r);
1319 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001320 mService.updateLruProcessLocked(app, true, null);
1321 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001322
Bryce Leeaf691c02017-03-20 14:20:22 -07001323 final TaskRecord task = r.getTask();
Benjamin Franz469dd582015-06-09 14:24:36 +01001324 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1325 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001326 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001327 }
1328
Andrii Kulian02b7a832016-10-06 23:11:56 -07001329 final ActivityStack stack = task.getStack();
Craig Mautner2420ead2013-04-01 17:13:20 -07001330 try {
1331 if (app.thread == null) {
1332 throw new RemoteException();
1333 }
1334 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001335 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001336 if (andResume) {
Andrii Kulianada10292017-02-09 23:19:29 -08001337 // We don't need to deliver new intents and/or set results if activity is going
1338 // to pause immediately after launch.
Craig Mautner2420ead2013-04-01 17:13:20 -07001339 results = r.results;
1340 newIntents = r.newIntents;
1341 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001342 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1343 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1344 + " newIntents=" + newIntents + " andResume=" + andResume);
Andrii Kulianada10292017-02-09 23:19:29 -08001345 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY, r.userId,
1346 System.identityHashCode(r), task.taskId, r.shortComponentName);
Chong Zhang85ee6542015-10-02 13:36:38 -07001347 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001348 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001349 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001350 }
Brian Carlstromca82e612016-04-19 23:16:08 -07001351 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1352 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
Craig Mautner2420ead2013-04-01 17:13:20 -07001353 r.sleeping = false;
1354 r.forceNewConfig = false;
Alan Viverette7df9b452016-06-16 12:47:58 -04001355 mService.showUnsupportedZoomDialogIfNeededLocked(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001356 mService.showAskCompatModeDialogLocked(r);
1357 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001358 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001359 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1360 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1361 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001362 final String profileFile = mService.mProfileFile;
1363 if (profileFile != null) {
1364 ParcelFileDescriptor profileFd = mService.mProfileFd;
1365 if (profileFd != null) {
1366 try {
1367 profileFd = profileFd.dup();
1368 } catch (IOException e) {
1369 if (profileFd != null) {
1370 try {
1371 profileFd.close();
1372 } catch (IOException o) {
1373 }
1374 profileFd = null;
1375 }
1376 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001377 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001378
1379 profilerInfo = new ProfilerInfo(profileFile, profileFd,
Shukang Zhou6ffd4f92017-01-25 16:07:57 -08001380 mService.mSamplingInterval, mService.mAutoStopProfiler,
1381 mService.mStreamingOutput);
Craig Mautner2420ead2013-04-01 17:13:20 -07001382 }
1383 }
1384 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001385
Andrii Kulianada10292017-02-09 23:19:29 -08001386 app.hasShownUi = true;
1387 app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001388 app.forceProcessStateUpTo(mService.mTopProcessState);
Andrii Kulian1779e612016-10-12 21:58:25 -07001389 // Because we could be starting an Activity in the system process this may not go across
1390 // a Binder interface which would create a new Configuration. Consequently we have to
1391 // always create a new Configuration here.
Bryce Leea163b762017-01-24 11:05:01 -08001392
1393 final Configuration globalConfiguration =
1394 new Configuration(mService.getGlobalConfiguration());
1395 r.setLastReportedGlobalConfiguration(globalConfiguration);
1396 final Configuration mergedOverrideConfiguration =
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07001397 new Configuration(r.getMergedOverrideConfiguration());
Bryce Leea163b762017-01-24 11:05:01 -08001398 r.setLastReportedMergedOverrideConfiguration(mergedOverrideConfiguration);
1399
Craig Mautner2420ead2013-04-01 17:13:20 -07001400 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Andrii Kulian8072d112016-09-16 11:11:01 -07001401 System.identityHashCode(r), r.info,
Bryce Leea163b762017-01-24 11:05:01 -08001402 globalConfiguration,
1403 mergedOverrideConfiguration, r.compat,
Andrii Kulian1779e612016-10-12 21:58:25 -07001404 r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
1405 r.persistentState, results, newIntents, !andResume,
1406 mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001407
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001408 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001409 // This may be a heavy-weight process! Note that the package
1410 // manager will ensure that only activity can run in the main
1411 // process of the .apk, which is the only thing that will be
1412 // considered heavy-weight.
1413 if (app.processName.equals(app.info.packageName)) {
1414 if (mService.mHeavyWeightProcess != null
1415 && mService.mHeavyWeightProcess != app) {
1416 Slog.w(TAG, "Starting new heavy weight process " + app
1417 + " when already running "
1418 + mService.mHeavyWeightProcess);
1419 }
1420 mService.mHeavyWeightProcess = app;
1421 Message msg = mService.mHandler.obtainMessage(
1422 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1423 msg.obj = r;
1424 mService.mHandler.sendMessage(msg);
1425 }
1426 }
1427
1428 } catch (RemoteException e) {
1429 if (r.launchFailed) {
1430 // This is the second time we failed -- finish activity
1431 // and give up.
1432 Slog.e(TAG, "Second failure launching "
1433 + r.intent.getComponent().flattenToShortString()
1434 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001435 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001436 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1437 "2nd-crash", false);
1438 return false;
1439 }
1440
1441 // This is the first time we failed -- restart process and
1442 // retry.
1443 app.activities.remove(r);
1444 throw e;
1445 }
1446
1447 r.launchFailed = false;
1448 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001449 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001450 }
1451
1452 if (andResume) {
1453 // As part of the process of launching, ActivityThread also performs
1454 // a resume.
1455 stack.minimalResumeActivityLocked(r);
1456 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001457 // This activity is not starting in the resumed state... which should look like we asked
1458 // it to pause+stop (but remain visible), and it has done so and reported back the
1459 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001460 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001461 "Moving to PAUSED: " + r + " (starting in paused state)");
1462 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001463 }
1464
1465 // Launch the new version setup screen if needed. We do this -after-
1466 // launching the initial activity (that is, home), so that it can have
1467 // a chance to initialize itself while in the background, making the
1468 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001469 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001470 mService.startSetupActivityLocked();
1471 }
1472
Dianne Hackborn465fa392014-09-14 14:21:18 -07001473 // Update any services we are bound to that might care about whether
1474 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001475 if (r.app != null) {
1476 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1477 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001478
Craig Mautner2420ead2013-04-01 17:13:20 -07001479 return true;
1480 }
1481
Craig Mautnere79d42682013-04-01 19:01:53 -07001482 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001483 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001484 // Is this activity's application already running?
1485 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001486 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001487
Andrii Kulian02b7a832016-10-06 23:11:56 -07001488 r.getStack().setLaunchTime(r);
Craig Mautnere79d42682013-04-01 19:01:53 -07001489
1490 if (app != null && app.thread != null) {
1491 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001492 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1493 || !"android".equals(r.info.packageName)) {
1494 // Don't add this if it is a platform component that is marked
1495 // to run in multiple processes, because this is actually
1496 // part of the framework so doesn't make sense to track as a
1497 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001498 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1499 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001500 }
George Mount2c92c972014-03-20 09:38:23 -07001501 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001502 return;
1503 } catch (RemoteException e) {
1504 Slog.w(TAG, "Exception when starting activity "
1505 + r.intent.getComponent().flattenToShortString(), e);
1506 }
1507
1508 // If a dead object exception was thrown -- fall through to
1509 // restart the application.
1510 }
1511
1512 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001513 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001514 }
1515
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001516 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001517 String resultWho, int requestCode, int callingPid, int callingUid,
1518 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001519 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001520 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1521 callingUid);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001522 if (startAnyPerm == PERMISSION_GRANTED) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001523 return true;
1524 }
1525 final int componentRestriction = getComponentRestrictionForCallingPackage(
1526 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1527 final int actionRestriction = getActionRestrictionForCallingPackage(
1528 intent.getAction(), callingPackage, callingPid, callingUid);
1529 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1530 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1531 if (resultRecord != null) {
1532 resultStack.sendActivityResultLocked(-1,
1533 resultRecord, resultWho, requestCode,
1534 Activity.RESULT_CANCELED, null);
1535 }
1536 final String msg;
1537 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1538 msg = "Permission Denial: starting " + intent.toString()
1539 + " from " + callerApp + " (pid=" + callingPid
1540 + ", uid=" + callingUid + ")" + " with revoked permission "
1541 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1542 } else if (!aInfo.exported) {
1543 msg = "Permission Denial: starting " + intent.toString()
1544 + " from " + callerApp + " (pid=" + callingPid
1545 + ", uid=" + callingUid + ")"
1546 + " not exported from uid " + aInfo.applicationInfo.uid;
1547 } else {
1548 msg = "Permission Denial: starting " + intent.toString()
1549 + " from " + callerApp + " (pid=" + callingPid
1550 + ", uid=" + callingUid + ")"
1551 + " requires " + aInfo.permission;
1552 }
1553 Slog.w(TAG, msg);
1554 throw new SecurityException(msg);
1555 }
1556
1557 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1558 final String message = "Appop Denial: starting " + intent.toString()
1559 + " from " + callerApp + " (pid=" + callingPid
1560 + ", uid=" + callingUid + ")"
1561 + " requires " + AppOpsManager.permissionToOp(
1562 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1563 Slog.w(TAG, message);
1564 return false;
1565 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1566 final String message = "Appop Denial: starting " + intent.toString()
1567 + " from " + callerApp + " (pid=" + callingPid
1568 + ", uid=" + callingUid + ")"
1569 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1570 Slog.w(TAG, message);
1571 return false;
1572 }
Andrii Kulian16802aa2016-11-02 12:21:33 -07001573 if (options != null) {
1574 if (options.getLaunchTaskId() != INVALID_STACK_ID) {
1575 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1576 callingPid, callingUid);
1577 if (startInTaskPerm != PERMISSION_GRANTED) {
1578 final String msg = "Permission Denial: starting " + intent.toString()
1579 + " from " + callerApp + " (pid=" + callingPid
1580 + ", uid=" + callingUid + ") with launchTaskId="
1581 + options.getLaunchTaskId();
1582 Slog.w(TAG, msg);
1583 throw new SecurityException(msg);
1584 }
1585 }
1586 // Check if someone tries to launch an activity on a private display with a different
1587 // owner.
1588 final int launchDisplayId = options.getLaunchDisplayId();
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001589 if (launchDisplayId != INVALID_DISPLAY
1590 && !isCallerAllowedToLaunchOnDisplay(callingPid, callingUid, launchDisplayId)) {
1591 final String msg = "Permission Denial: starting " + intent.toString()
1592 + " from " + callerApp + " (pid=" + callingPid
1593 + ", uid=" + callingUid + ") with launchDisplayId="
1594 + launchDisplayId;
1595 Slog.w(TAG, msg);
1596 throw new SecurityException(msg);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001597 }
1598 }
1599
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001600 return true;
1601 }
1602
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001603 /** Check if caller is allowed to launch activities on specified display. */
1604 boolean isCallerAllowedToLaunchOnDisplay(int callingPid, int callingUid, int launchDisplayId) {
1605 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: displayId=" + launchDisplayId
1606 + " callingPid=" + callingPid + " callingUid=" + callingUid);
1607
1608 final ActivityDisplay activityDisplay = mActivityDisplays.get(launchDisplayId);
1609 if (activityDisplay == null) {
1610 Slog.w(TAG, "Launch on display check: display not found");
1611 return false;
1612 }
1613
1614 if (!activityDisplay.isPrivate()) {
1615 // Anyone can launch on a public display.
1616 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1617 + " allow launch on public display");
1618 return true;
1619 }
1620
1621 // Check if the caller is the owner of the display.
1622 if (activityDisplay.mDisplay.getOwnerUid() == callingUid) {
1623 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1624 + " allow launch for owner of the display");
1625 return true;
1626 }
1627
1628 // Check if caller is present on display
1629 if (activityDisplay.isUidPresent(callingUid)) {
1630 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1631 + " allow launch for caller present on the display");
1632 return true;
1633 }
1634
Andrii Kuliane6ac20e2017-03-17 10:30:50 -07001635 // Check if the caller can manage activity stacks.
1636 final int startAnyPerm = mService.checkPermission(MANAGE_ACTIVITY_STACKS, callingPid,
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001637 callingUid);
1638 if (startAnyPerm == PERMISSION_GRANTED) {
1639 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1640 + " allow launch any on display");
1641 return true;
1642 }
1643
1644 Slog.w(TAG, "Launch on display check: denied");
1645 return false;
1646 }
1647
1648 /** Update lists of UIDs that are present on displays and have access to them. */
1649 void updateUIDsPresentOnDisplay() {
1650 mDisplayAccessUIDs.clear();
1651 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1652 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
David Stevens82ea6cb2017-03-03 16:18:50 -08001653 // Only bother calculating the whitelist for private displays
1654 if (activityDisplay.isPrivate()) {
1655 mDisplayAccessUIDs.append(
1656 activityDisplay.mDisplayId, activityDisplay.getPresentUIDs());
1657 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001658 }
1659 // Store updated lists in DisplayManager. Callers from outside of AM should get them there.
1660 mDisplayManagerInternal.setDisplayAccessUIDs(mDisplayAccessUIDs);
1661 }
1662
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001663 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001664 final long identity = Binder.clearCallingIdentity();
1665 try {
1666 return UserManager.get(mService.mContext).getUserInfo(userId);
1667 } finally {
1668 Binder.restoreCallingIdentity(identity);
1669 }
1670 }
1671
Svet Ganov99b60432015-06-27 13:15:22 -07001672 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001673 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001674 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1675 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001676 == PackageManager.PERMISSION_DENIED) {
1677 return ACTIVITY_RESTRICTION_PERMISSION;
1678 }
1679
Christopher Tateff7add02015-08-17 10:23:22 -07001680 if (activityInfo.permission == null) {
1681 return ACTIVITY_RESTRICTION_NONE;
1682 }
1683
Svet Ganov99b60432015-06-27 13:15:22 -07001684 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1685 if (opCode == AppOpsManager.OP_NONE) {
1686 return ACTIVITY_RESTRICTION_NONE;
1687 }
1688
1689 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1690 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001691 if (!ignoreTargetSecurity) {
1692 return ACTIVITY_RESTRICTION_APPOP;
1693 }
Svet Ganov99b60432015-06-27 13:15:22 -07001694 }
1695
1696 return ACTIVITY_RESTRICTION_NONE;
1697 }
1698
Svetoslav7008b512015-06-24 18:47:07 -07001699 private int getActionRestrictionForCallingPackage(String action,
1700 String callingPackage, int callingPid, int callingUid) {
1701 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001702 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001703 }
1704
1705 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1706 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001707 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001708 }
1709
1710 final PackageInfo packageInfo;
1711 try {
1712 packageInfo = mService.mContext.getPackageManager()
1713 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1714 } catch (PackageManager.NameNotFoundException e) {
1715 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001716 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001717 }
1718
1719 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001720 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001721 }
1722
1723 if (mService.checkPermission(permission, callingPid, callingUid) ==
1724 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001725 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001726 }
1727
1728 final int opCode = AppOpsManager.permissionToOpCode(permission);
1729 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001730 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001731 }
1732
1733 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1734 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001735 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001736 }
1737
Svet Ganov99b60432015-06-27 13:15:22 -07001738 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001739 }
1740
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001741 void setLaunchSource(int uid) {
1742 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1743 }
1744
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001745 void acquireLaunchWakelock() {
1746 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1747 throw new IllegalStateException("Calling must be system uid");
1748 }
1749 mLaunchingActivity.acquire();
1750 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1751 // To be safe, don't allow the wake lock to be held for too long.
1752 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1753 }
1754 }
1755
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001756 /**
1757 * Called when the frontmost task is idle.
1758 * @return the state of mService.mBooting before this was called.
1759 */
1760 private boolean checkFinishBootingLocked() {
1761 final boolean booting = mService.mBooting;
1762 boolean enableScreen = false;
1763 mService.mBooting = false;
1764 if (!mService.mBooted) {
1765 mService.mBooted = true;
1766 enableScreen = true;
1767 }
1768 if (booting || enableScreen) {
1769 mService.postFinishBooting(booting, enableScreen);
1770 }
1771 return booting;
1772 }
1773
Craig Mautnerf3333272013-04-22 10:55:53 -07001774 // Checked.
1775 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
Winson Chung4dabf232017-01-25 13:25:22 -08001776 boolean processPausingActivities, Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001777 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001778
Craig Mautnerf3333272013-04-22 10:55:53 -07001779 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001780 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001781 int NS = 0;
1782 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001783 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001784 boolean activityRemoved = false;
1785
Wale Ogunwale7d701172015-03-11 15:36:30 -07001786 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001787 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001788 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1789 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001790 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1791 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001792 if (fromTimeout) {
1793 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001794 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001795
1796 // This is a hack to semi-deal with a race condition
1797 // in the client where it can be constructed with a
1798 // newer configuration from when we asked it to launch.
1799 // We'll update with whatever configuration it now says
1800 // it used to launch.
1801 if (config != null) {
Bryce Leea163b762017-01-24 11:05:01 -08001802 r.setLastReportedGlobalConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001803 }
1804
1805 // We are now idle. If someone is waiting for a thumbnail from
1806 // us, we can now deliver.
1807 r.idle = true;
1808
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001809 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001810 if (isFocusedStack(r.getStack()) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001811 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001812 }
1813 }
1814
1815 if (allResumedActivitiesIdle()) {
1816 if (r != null) {
1817 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001818 }
1819
1820 if (mLaunchingActivity.isHeld()) {
1821 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1822 if (VALIDATE_WAKE_LOCK_CALLER &&
1823 Binder.getCallingUid() != Process.myUid()) {
1824 throw new IllegalStateException("Calling must be system uid");
1825 }
1826 mLaunchingActivity.release();
1827 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001828 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001829 }
1830
1831 // Atomically retrieve all of the other things to do.
Winson Chung4dabf232017-01-25 13:25:22 -08001832 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(r,
1833 true /* remove */, processPausingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001834 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001835 if ((NF = mFinishingActivities.size()) > 0) {
1836 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001837 mFinishingActivities.clear();
1838 }
1839
Craig Mautnerf3333272013-04-22 10:55:53 -07001840 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001841 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001842 mStartingUsers.clear();
1843 }
1844
Craig Mautnerf3333272013-04-22 10:55:53 -07001845 // Stop any activities that are scheduled to do so but have been
1846 // waiting for the next one to start.
1847 for (int i = 0; i < NS; i++) {
1848 r = stops.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001849 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001850 if (stack != null) {
1851 if (r.finishing) {
1852 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1853 } else {
1854 stack.stopActivityLocked(r);
1855 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001856 }
1857 }
1858
1859 // Finish any activities that are scheduled to do so but have been
1860 // waiting for the next one to start.
1861 for (int i = 0; i < NF; i++) {
1862 r = finishes.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001863 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001864 if (stack != null) {
1865 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1866 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001867 }
1868
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001869 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001870 // Complete user switch
1871 if (startingUsers != null) {
1872 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001873 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001874 }
1875 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001876 }
1877
1878 mService.trimApplications();
1879 //dump();
1880 //mWindowManager.dump();
1881
Craig Mautnerf3333272013-04-22 10:55:53 -07001882 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001883 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001884 }
1885
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001886 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001887 }
1888
Craig Mautner8e569572013-10-11 17:36:59 -07001889 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001890 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001891 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1892 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001893 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1894 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1895 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001896 }
Craig Mautner19091252013-10-05 00:03:53 -07001897 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001898 }
1899
1900 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001901 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1902 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001903 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1904 stacks.get(stackNdx).closeSystemDialogsLocked();
1905 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001906 }
1907 }
1908
Craig Mautner93529a42013-10-04 15:03:13 -07001909 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001910 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001911 }
1912
Craig Mautner8d341ef2013-03-26 09:03:27 -07001913 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001914 * Update the last used stack id for non-current user (current user's last
1915 * used stack is the focused stack)
1916 */
1917 void updateUserStackLocked(int userId, ActivityStack stack) {
1918 if (userId != mCurrentUser) {
1919 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1920 }
1921 }
1922
1923 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001924 * @return true if some activity was finished (or would have finished if doit were true).
1925 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001926 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1927 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001928 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001929 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1930 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001931 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001932 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001933 if (stack.finishDisabledPackageActivitiesLocked(
1934 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001935 didSomething = true;
1936 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001937 }
1938 }
1939 return didSomething;
1940 }
1941
Dianne Hackborna413dc02013-07-12 12:02:55 -07001942 void updatePreviousProcessLocked(ActivityRecord r) {
1943 // Now that this process has stopped, we may want to consider
1944 // it to be the previous app to try to keep around in case
1945 // the user wants to return to it.
1946
1947 // First, found out what is currently the foreground app, so that
1948 // we don't blow away the previous app if this activity is being
1949 // hosted by the process that is actually still the foreground.
1950 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001951 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1952 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001953 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1954 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001955 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001956 if (stack.mResumedActivity != null) {
1957 fgApp = stack.mResumedActivity.app;
1958 } else if (stack.mPausingActivity != null) {
1959 fgApp = stack.mPausingActivity.app;
1960 }
1961 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001962 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001963 }
1964 }
1965
1966 // Now set this one as the previous process, only if that really
1967 // makes sense to.
1968 if (r.app != null && fgApp != null && r.app != fgApp
1969 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001970 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001971 mService.mPreviousProcess = r.app;
1972 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1973 }
1974 }
1975
Wale Ogunwaled046a012015-12-24 13:05:59 -08001976 boolean resumeFocusedStackTopActivityLocked() {
1977 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001978 }
1979
Wale Ogunwaled046a012015-12-24 13:05:59 -08001980 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001981 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001982 if (targetStack != null && isFocusedStack(targetStack)) {
1983 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001984 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07001985 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
1986 if (r == null || r.state != RESUMED) {
1987 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
skuhne@google.com1b974dc2016-12-09 13:41:29 -08001988 } else if (r.state == RESUMED) {
1989 // Kick off any lingering app transitions form the MoveTaskToFront operation.
1990 mFocusedStack.executeAppTransition(targetOptions);
Wale Ogunwale06579d62016-04-30 15:29:06 -07001991 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001992 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001993 }
1994
Todd Kennedy39bfee52016-02-24 10:28:21 -08001995 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
1996 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1997 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1998 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1999 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
2000 }
2001 }
2002 }
2003
Adrian Roos20d7df32016-01-12 18:59:43 +01002004 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
2005 TaskRecord finishedTask = null;
2006 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002007 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2008 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002009 final int numStacks = stacks.size();
2010 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2011 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01002012 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
2013 if (stack == focusedStack || finishedTask == null) {
2014 finishedTask = t;
2015 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002016 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002017 }
Adrian Roos20d7df32016-01-12 18:59:43 +01002018 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002019 }
2020
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002021 void finishVoiceTask(IVoiceInteractionSession session) {
2022 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2023 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2024 final int numStacks = stacks.size();
2025 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2026 final ActivityStack stack = stacks.get(stackNdx);
2027 stack.finishVoiceTask(session);
2028 }
2029 }
2030 }
2031
Andrii Kulianc27916642016-04-12 17:59:27 -07002032 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
2033 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002034 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2035 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002036 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002037 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2038 // Caller wants the home activity moved with it. To accomplish this,
2039 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002040 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002041 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002042 ActivityStack currentStack = task.getStack();
2043 if (currentStack == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002044 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2045 + task + " to front. Stack is null");
2046 return;
2047 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002048
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002049 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002050 int stackId = options.getLaunchStackId();
2051 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002052 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07002053 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08002054 if (stackId == INVALID_STACK_ID) {
2055 stackId = task.getLaunchStackId();
2056 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002057 if (stackId != currentStack.mStackId) {
Winson Chung74666102017-02-22 17:49:24 -08002058 task.reparent(stackId, ON_TOP, REPARENT_KEEP_STACK_AT_FRONT, !ANIMATE,
2059 DEFER_RESUME, "findTaskToMoveToFrontLocked");
Andrii Kulian02b7a832016-10-06 23:11:56 -07002060 stackId = currentStack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08002061 // moveTaskToStackUncheckedLocked() should already placed the task on top,
2062 // still need moveTaskToFrontLocked() below for any transition settings.
2063 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002064 if (StackId.resizeStackWithLaunchBounds(stackId)) {
2065 resizeStackLocked(stackId, bounds,
2066 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002067 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002068 } else {
2069 // WM resizeTask must be done after the task is moved to the correct stack,
2070 // because Task's setBounds() also updates dim layer's bounds, but that has
2071 // dependency on the stack.
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002072 task.resizeWindowContainer();
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002073 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002074 }
2075 }
2076
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002077 final ActivityRecord r = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002078 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002079 r == null ? null : r.appTimeTracker, reason);
2080
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002081 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07002082 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01002083
Andrii Kulian02b7a832016-10-06 23:11:56 -07002084 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, currentStack.mStackId,
Andrii Kulianc27916642016-04-12 17:59:27 -07002085 forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002086 }
2087
Wale Ogunwale854809c2015-12-27 16:18:19 -08002088 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002089 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08002090 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08002091 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002092 return false;
2093 }
2094 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
2095 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002096 }
2097
Winson Chung55893332017-02-17 17:13:10 -08002098 protected <T extends ActivityStack> T getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07002099 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002100 }
2101
Winson Chung55893332017-02-17 17:13:10 -08002102 protected <T extends ActivityStack> T getStack(int stackId, boolean createStaticStackIfNeeded,
2103 boolean createOnTop) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002104 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002105 if (activityContainer != null) {
Winson Chung55893332017-02-17 17:13:10 -08002106 return (T) activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002107 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07002108 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002109 return null;
2110 }
Matthew Ng330757d2017-02-28 14:19:17 -08002111 if (stackId == DOCKED_STACK_ID) {
2112 // Make sure recents stack exist when creating a dock stack as it normally need to be on
2113 // the other side of the docked stack and we make visibility decisions based on that.
2114 getStack(RECENTS_STACK_ID, CREATE_IF_NEEDED, createOnTop);
2115 }
Winson Chung55893332017-02-17 17:13:10 -08002116 return (T) createStackOnDisplay(stackId, DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002117 }
2118
Andrii Kulian16802aa2016-11-02 12:21:33 -07002119 /**
2120 * Get a topmost stack on the display, that is a valid launch stack for specified activity.
2121 * If there is no such stack, new dynamic stack can be created.
2122 * @param displayId Target display.
2123 * @param r Activity that should be launched there.
2124 * @return Existing stack if there is a valid one, new dynamic stack if it is valid or null.
2125 */
2126 ActivityStack getValidLaunchStackOnDisplay(int displayId, @NonNull ActivityRecord r) {
2127 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2128 if (activityDisplay == null) {
2129 throw new IllegalArgumentException(
2130 "Display with displayId=" + displayId + " not found.");
2131 }
2132
2133 // Return the topmost valid stack on the display.
2134 for (int i = activityDisplay.mStacks.size() - 1; i >= 0; --i) {
2135 final ActivityStack stack = activityDisplay.mStacks.get(i);
2136 if (mService.mActivityStarter.isValidLaunchStackId(stack.mStackId, r)) {
2137 return stack;
2138 }
2139 }
2140
2141 // If there is no valid stack on the external display - check if new dynamic stack will do.
2142 if (displayId != Display.DEFAULT_DISPLAY) {
2143 final int newDynamicStackId = getNextStackId();
2144 if (mService.mActivityStarter.isValidLaunchStackId(newDynamicStackId, r)) {
2145 return createStackOnDisplay(newDynamicStackId, displayId, true /*onTop*/);
2146 }
2147 }
2148
2149 Slog.w(TAG, "getValidLaunchStackOnDisplay: can't launch on displayId " + displayId);
2150 return null;
2151 }
2152
Craig Mautner967212c2013-04-13 21:10:58 -07002153 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07002154 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002155 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2156 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002157 }
2158 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002159 }
2160
Jorim Jaggife762342016-10-13 14:33:27 +02002161 ArrayList<ActivityStack> getStacksOnDefaultDisplay() {
2162 return mActivityDisplays.valueAt(DEFAULT_DISPLAY).mStacks;
2163 }
2164
Andrii Kulian7fc22812016-12-28 13:04:11 -08002165 /**
2166 * Get next focusable stack in the system. This will search across displays and stacks
2167 * in last-focused order for a focusable and visible stack, different from the target stack.
2168 *
2169 * @param currentFocus The stack that previously had focus and thus needs to be ignored when
2170 * searching for next candidate.
2171 * @return Next focusable {@link ActivityStack}, null if not found.
2172 */
2173 ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus) {
2174 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2175
2176 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2177 final int displayId = mTmpOrderedDisplayIds.get(i);
2178 final List<ActivityStack> stacks = mActivityDisplays.get(displayId).mStacks;
2179 if (stacks == null) {
2180 continue;
2181 }
2182 for (int j = stacks.size() - 1; j >= 0; --j) {
2183 final ActivityStack stack = stacks.get(j);
2184 if (stack != currentFocus && stack.isFocusable()
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07002185 && stack.shouldBeVisible(null) != STACK_INVISIBLE) {
Andrii Kulian7fc22812016-12-28 13:04:11 -08002186 return stack;
2187 }
2188 }
2189 }
2190
2191 return null;
2192 }
2193
Craig Mautneree2e45a2014-06-27 12:10:03 -07002194 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002195 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002196 }
2197
2198 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002199 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2200 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2201 final TaskRecord task = tasks.get(taskNdx);
2202 if (task.isHomeTask()) {
2203 final ArrayList<ActivityRecord> activities = task.mActivities;
2204 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2205 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002206 if (r.isHomeActivity()
2207 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002208 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002209 }
2210 }
2211 }
2212 }
2213 return null;
2214 }
2215
Chong Zhangb15758a2015-11-17 12:12:03 -08002216 /**
2217 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
2218 * the docked stack itself, or if it's side-by-side to the docked stack.
2219 */
2220 boolean isStackDockedInEffect(int stackId) {
2221 return stackId == DOCKED_STACK_ID ||
2222 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
2223 }
2224
Todd Kennedyca4d8422015-01-15 15:19:22 -08002225 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002226 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002227 ActivityContainer activityContainer =
2228 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002229 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002230 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2231 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002232 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002233 return activityContainer;
2234 }
2235
Craig Mautner34b73df2014-01-12 21:11:08 -08002236 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002237 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2238 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2239 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002240 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2241 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002242 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002243 }
2244 }
2245
Andrii Kulian6d6fb402016-10-26 16:15:25 -07002246 void deleteActivityContainerRecord(int stackId) {
2247 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2248 "deleteActivityContainerRecord: callers=" + Debug.getCallers(4));
2249 mActivityContainers.remove(stackId);
Craig Mautner95da1082014-02-24 17:54:35 -08002250 }
2251
Jorim Jaggidc249c42015-12-15 14:57:31 -08002252 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002253 boolean preserveWindows, boolean allowResizeInDockedMode, boolean deferResume) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002254 if (stackId == DOCKED_STACK_ID) {
2255 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002256 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002257 return;
2258 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002259 final ActivityStack stack = getStack(stackId);
2260 if (stack == null) {
2261 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2262 return;
2263 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002264
Winson Chunga2249ac2017-03-30 18:15:30 -07002265 if (!allowResizeInDockedMode && !StackId.tasksAreFloating(stackId) &&
2266 getStack(DOCKED_STACK_ID) != null) {
2267 // If the docked stack exists, don't resize non-floating stacks independently of the
2268 // size computed from the docked stack size (otherwise they will be out of sync)
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002269 return;
2270 }
2271
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002272 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002273 mWindowManager.deferSurfaceLayout();
2274 try {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002275 stack.resize(bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002276 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002277 stack.ensureVisibleActivitiesConfigurationLocked(
2278 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002279 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002280 } finally {
2281 mWindowManager.continueSurfaceLayout();
2282 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002283 }
2284 }
2285
Jorim Jaggi192086e2016-03-11 17:17:03 +01002286 void deferUpdateBounds(int stackId) {
2287 final ActivityStack stack = getStack(stackId);
2288 if (stack != null) {
2289 stack.deferUpdateBounds();
2290 }
2291 }
2292
2293 void continueUpdateBounds(int stackId) {
2294 final ActivityStack stack = getStack(stackId);
2295 if (stack != null) {
2296 stack.continueUpdateBounds();
2297 }
2298 }
2299
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002300 void notifyAppTransitionDone() {
2301 continueUpdateBounds(HOME_STACK_ID);
2302 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2303 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002304 final TaskRecord task =
Winson Chung7900bee2017-03-10 08:59:25 -08002305 anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_ONLY, INVALID_STACK_ID);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002306 if (task != null) {
2307 task.setTaskDockedResizing(false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002308 }
2309 }
2310 mResizingTasksDuringAnimation.clear();
2311 }
2312
Robert Carre7cc44d2017-03-20 19:04:30 -07002313 private void moveTasksToFullscreenStackInSurfaceTransaction(int fromStackId,
2314 boolean onTop) {
Robert Carr6914f082017-03-20 19:04:30 -07002315
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002316 final ActivityStack stack = getStack(fromStackId);
2317 if (stack == null) {
2318 return;
2319 }
2320
2321 mWindowManager.deferSurfaceLayout();
2322 try {
2323 if (fromStackId == DOCKED_STACK_ID) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002324 // We are moving all tasks from the docked stack to the fullscreen stack,
2325 // which is dismissing the docked stack, so resize all other stacks to
2326 // fullscreen here already so we don't end up with resize trashing.
2327 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2328 if (StackId.isResizeableByDockedStack(i)) {
2329 ActivityStack otherStack = getStack(i);
2330 if (otherStack != null) {
2331 resizeStackLocked(i, null, null, null, PRESERVE_WINDOWS,
2332 true /* allowResizeInDockedMode */, DEFER_RESUME);
2333 }
2334 }
2335 }
2336
2337 // Also disable docked stack resizing since we have manually adjusted the
2338 // size of other stacks above and we don't want to trigger a docked stack
2339 // resize when we remove task from it below and it is detached from the
2340 // display because it no longer contains any tasks.
2341 mAllowDockedStackResize = false;
Winson Chung47900652017-04-06 18:44:25 -07002342 } else if (fromStackId == PINNED_STACK_ID) {
2343 if (onTop) {
2344 // Log if we are expanding the PiP to fullscreen
2345 MetricsLogger.action(mService.mContext,
2346 ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN);
2347 }
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002348 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002349 ActivityStack fullscreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
Winson Chungc85d9ce2017-01-03 11:59:52 -08002350 final boolean isFullscreenStackVisible = fullscreenStack != null &&
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07002351 fullscreenStack.shouldBeVisible(null) == STACK_VISIBLE;
Winson Chung5af42fc2017-03-24 17:11:33 -07002352 // If we are moving from the pinned stack, then the animation takes care of updating
2353 // the picture-in-picture mode.
Winson Chung47900652017-04-06 18:44:25 -07002354 final boolean schedulePictureInPictureModeChange = (fromStackId == PINNED_STACK_ID);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002355 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2356 final int size = tasks.size();
2357 if (onTop) {
2358 for (int i = 0; i < size; i++) {
Winson Chung0c1ca092016-11-10 17:40:34 -08002359 final TaskRecord task = tasks.get(i);
Winson Chung74666102017-02-22 17:49:24 -08002360 final boolean isTopTask = i == (size - 1);
Winson Chung0c1ca092016-11-10 17:40:34 -08002361 if (fromStackId == PINNED_STACK_ID) {
2362 // Update the return-to to reflect where the pinned stack task was moved
2363 // from so that we retain the stack that was previously visible if the
2364 // pinned stack is recreated. See moveActivityToPinnedStackLocked().
Winson Chungc85d9ce2017-01-03 11:59:52 -08002365 task.setTaskToReturnTo(isFullscreenStackVisible && onTop ?
2366 APPLICATION_ACTIVITY_TYPE : HOME_ACTIVITY_TYPE);
Winson Chung0c1ca092016-11-10 17:40:34 -08002367 }
Winson Chung74666102017-02-22 17:49:24 -08002368 // Defer resume until all the tasks have been moved to the fullscreen stack
2369 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP,
2370 REPARENT_MOVE_STACK_TO_FRONT, isTopTask /* animate */, DEFER_RESUME,
Winson Chung5af42fc2017-03-24 17:11:33 -07002371 schedulePictureInPictureModeChange,
Winson Chung74666102017-02-22 17:49:24 -08002372 "moveTasksToFullscreenStack - onTop");
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002373 }
2374 } else {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002375 for (int i = 0; i < size; i++) {
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002376 final TaskRecord task = tasks.get(i);
Winson Chung74666102017-02-22 17:49:24 -08002377 final int position = fullscreenStack != null
2378 ? Math.max(fullscreenStack.getAllTasks().size() - 1, 0) : 0;
2379 // Defer resume until all the tasks have been moved to the fullscreen stack
Matthew Ng47fa6ae2017-02-07 13:15:10 -08002380 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, position,
Winson Chung5af42fc2017-03-24 17:11:33 -07002381 REPARENT_LEAVE_STACK_IN_PLACE, !ANIMATE, DEFER_RESUME,
2382 schedulePictureInPictureModeChange,
2383 "moveTasksToFullscreenStack - NOT_onTop");
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002384 }
2385 }
Winson Chung74666102017-02-22 17:49:24 -08002386
2387 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2388 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002389 } finally {
2390 mAllowDockedStackResize = true;
2391 mWindowManager.continueSurfaceLayout();
2392 }
2393 }
2394
Robert Carr6914f082017-03-20 19:04:30 -07002395 void moveTasksToFullscreenStackLocked(int fromStackId, boolean onTop) {
Robert Carre7cc44d2017-03-20 19:04:30 -07002396 mWindowManager.inSurfaceTransaction(
2397 () -> moveTasksToFullscreenStackInSurfaceTransaction(fromStackId, onTop));
Robert Carr6914f082017-03-20 19:04:30 -07002398 }
2399
Jorim Jaggidc249c42015-12-15 14:57:31 -08002400 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002401 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2402 boolean preserveWindows) {
2403 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2404 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2405 false /* deferResume */);
2406 }
2407
2408 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2409 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2410 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002411
2412 if (!mAllowDockedStackResize) {
2413 // Docked stack resize currently disabled.
2414 return;
2415 }
2416
Jorim Jaggidc249c42015-12-15 14:57:31 -08002417 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2418 if (stack == null) {
2419 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2420 return;
2421 }
2422
2423 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2424 mWindowManager.deferSurfaceLayout();
2425 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002426 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2427 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002428 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002429 stack.resize(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002430
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002431 // TODO: Checking for isAttached might not be needed as if the user passes in null
2432 // dockedBounds then they want the docked stack to be dismissed.
2433 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2434 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002435 // In this case we make all other static stacks fullscreen and move all
2436 // docked stack tasks to the fullscreen stack.
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002437 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002438
2439 // stack shouldn't contain anymore activities, so nothing to resume.
2440 r = null;
2441 } else {
2442 // Docked stacks occupy a dedicated region on screen so the size of all other
2443 // static stacks need to be adjusted so they don't overlap with the docked stack.
2444 // We get the bounds to use from window manager which has been adjusted for any
2445 // screen controls and is also the same for all stacks.
Matthew Ngaa2b6202017-02-10 14:48:21 -08002446 final Rect otherTaskRect = new Rect();
Jorim Jaggidc249c42015-12-15 14:57:31 -08002447 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002448 final ActivityStack current = getStack(i);
2449 if (current != null && StackId.isResizeableByDockedStack(i)) {
Matthew Ngaa2b6202017-02-10 14:48:21 -08002450 current.getStackDockedModeBounds(
2451 tempOtherTaskBounds /* currentTempTaskBounds */,
2452 tempRect /* outStackBounds */,
2453 otherTaskRect /* outTempTaskBounds */, true /* ignoreVisibility */);
2454
Matthew Nge15352e2016-12-20 15:36:29 -08002455 resizeStackLocked(i, tempRect,
Matthew Ngaa2b6202017-02-10 14:48:21 -08002456 !otherTaskRect.isEmpty() ? otherTaskRect : tempOtherTaskBounds,
2457 tempOtherTaskInsetBounds, preserveWindows,
2458 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002459 }
2460 }
2461 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002462 if (!deferResume) {
2463 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2464 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002465 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002466 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002467 mWindowManager.continueSurfaceLayout();
2468 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2469 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002470 }
2471
Robert Carr0d00c2e2016-02-29 17:45:02 -08002472 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
2473 final ActivityStack stack = getStack(PINNED_STACK_ID);
2474 if (stack == null) {
2475 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2476 return;
2477 }
2478 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2479 mWindowManager.deferSurfaceLayout();
2480 try {
2481 ActivityRecord r = stack.topRunningActivityLocked();
Robert Carr7e4c90e2017-02-15 19:52:38 -08002482 Rect insetBounds = null;
2483 if (tempPinnedTaskBounds != null) {
2484 // We always use 0,0 as the position for the inset rect because
2485 // if we are getting insets at all in the pinned stack it must mean
2486 // we are headed for fullscreen.
2487 insetBounds = tempRect;
2488 insetBounds.top = 0;
2489 insetBounds.left = 0;
2490 insetBounds.right = tempPinnedTaskBounds.width();
2491 insetBounds.bottom = tempPinnedTaskBounds.height();
2492 }
2493 stack.resize(pinnedBounds, tempPinnedTaskBounds, insetBounds);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002494 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002495 } finally {
2496 mWindowManager.continueSurfaceLayout();
2497 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2498 }
2499 }
2500
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002501 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002502 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
Craig Mautnere0a38842013-12-16 16:14:02 -08002503 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002504 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002505 }
2506
Wale Ogunwale1666e312016-12-16 11:27:18 -08002507 final ActivityContainer activityContainer =
2508 new ActivityContainer(stackId, activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002509 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002510 }
2511
Robert Carr6914f082017-03-20 19:04:30 -07002512
Robert Carre7cc44d2017-03-20 19:04:30 -07002513 void removeStackInSurfaceTransaction(int stackId) {
Winson Chung010927a2016-12-15 16:12:35 -08002514 final ActivityStack stack = getStack(stackId);
2515 if (stack == null) {
2516 return;
2517 }
2518
2519 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2520 if (stack.getStackId() == PINNED_STACK_ID) {
Winson Chung47900652017-04-06 18:44:25 -07002521 /**
2522 * Workaround: Force-stop all the activities in the pinned stack before we reparent them
2523 * to the fullscreen stack. This is to guarantee that when we are removing a stack,
2524 * that the client receives onStop() before it is reparented. We do this by detaching
2525 * the stack from the display so that it will be considered invisible when
2526 * ensureActivitiesVisibleLocked() is called, and all of its activitys will be marked
2527 * invisible as well and added to the stopping list. After which we process the
2528 * stopping list by handling the idle.
2529 */
2530 final PinnedActivityStack pinnedStack = (PinnedActivityStack) stack;
2531 pinnedStack.mForceHidden = true;
2532 pinnedStack.ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2533 pinnedStack.mForceHidden = false;
2534 activityIdleInternalLocked(null, false /* fromTimeout */,
2535 true /* processPausingActivites */, null /* configuration */);
2536
2537 // Move all the tasks to the bottom of the fullscreen stack
2538 moveTasksToFullscreenStackLocked(PINNED_STACK_ID, !ON_TOP);
Winson Chung010927a2016-12-15 16:12:35 -08002539 } else {
2540 for (int i = tasks.size() - 1; i >= 0; i--) {
2541 removeTaskByIdLocked(tasks.get(i).taskId, true /* killProcess */,
2542 REMOVE_FROM_RECENTS);
2543 }
2544 }
2545 }
2546
2547 /**
Robert Carr6914f082017-03-20 19:04:30 -07002548 * Removes the stack associated with the given {@param stackId}. If the {@param stackId} is the
2549 * pinned stack, then its tasks are not explicitly removed when the stack is destroyed, but
2550 * instead moved back onto the fullscreen stack.
2551 */
2552 void removeStackLocked(int stackId) {
Robert Carre7cc44d2017-03-20 19:04:30 -07002553 mWindowManager.inSurfaceTransaction(
2554 () -> removeStackInSurfaceTransaction(stackId));
Robert Carr6914f082017-03-20 19:04:30 -07002555 }
2556
2557 /**
Winson Chung6954fc92017-03-24 16:22:12 -07002558 * See {@link #removeTaskByIdLocked(int, boolean, boolean, boolean)}
2559 */
2560 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents) {
2561 return removeTaskByIdLocked(taskId, killProcess, removeFromRecents, !PAUSE_IMMEDIATELY);
2562 }
2563
2564 /**
Winson Chung010927a2016-12-15 16:12:35 -08002565 * Removes the task with the specified task id.
2566 *
2567 * @param taskId Identifier of the task to be removed.
2568 * @param killProcess Kill any process associated with the task if possible.
2569 * @param removeFromRecents Whether to also remove the task from recents.
Winson Chung6954fc92017-03-24 16:22:12 -07002570 * @param pauseImmediately Pauses all task activities immediately without waiting for the
2571 * pause-complete callback from the activity.
Winson Chung010927a2016-12-15 16:12:35 -08002572 * @return Returns true if the given task was found and removed.
2573 */
Winson Chung6954fc92017-03-24 16:22:12 -07002574 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents,
2575 boolean pauseImmediately) {
Winson Chung7900bee2017-03-10 08:59:25 -08002576 final TaskRecord tr = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
2577 INVALID_STACK_ID);
Winson Chung010927a2016-12-15 16:12:35 -08002578 if (tr != null) {
Winson Chung6954fc92017-03-24 16:22:12 -07002579 tr.removeTaskActivitiesLocked(pauseImmediately);
Winson Chung010927a2016-12-15 16:12:35 -08002580 cleanUpRemovedTaskLocked(tr, killProcess, removeFromRecents);
2581 if (tr.isPersistable) {
2582 mService.notifyTaskPersisterLocked(null, true);
2583 }
2584 return true;
2585 }
2586 Slog.w(TAG, "Request to remove task ignored for non-existent task " + taskId);
2587 return false;
2588 }
2589
2590 void cleanUpRemovedTaskLocked(TaskRecord tr, boolean killProcess, boolean removeFromRecents) {
2591 if (removeFromRecents) {
2592 mRecentTasks.remove(tr);
2593 tr.removedFromRecents();
2594 }
2595 ComponentName component = tr.getBaseIntent().getComponent();
2596 if (component == null) {
2597 Slog.w(TAG, "No component for base intent of task: " + tr);
2598 return;
2599 }
2600
2601 // Find any running services associated with this app and stop if needed.
2602 mService.mServices.cleanUpRemovedTaskLocked(tr, component, new Intent(tr.getBaseIntent()));
2603
2604 if (!killProcess) {
2605 return;
2606 }
2607
2608 // Determine if the process(es) for this task should be killed.
2609 final String pkg = component.getPackageName();
2610 ArrayList<ProcessRecord> procsToKill = new ArrayList<>();
2611 ArrayMap<String, SparseArray<ProcessRecord>> pmap = mService.mProcessNames.getMap();
2612 for (int i = 0; i < pmap.size(); i++) {
2613
2614 SparseArray<ProcessRecord> uids = pmap.valueAt(i);
2615 for (int j = 0; j < uids.size(); j++) {
2616 ProcessRecord proc = uids.valueAt(j);
2617 if (proc.userId != tr.userId) {
2618 // Don't kill process for a different user.
2619 continue;
2620 }
2621 if (proc == mService.mHomeProcess) {
2622 // Don't kill the home process along with tasks from the same package.
2623 continue;
2624 }
2625 if (!proc.pkgList.containsKey(pkg)) {
2626 // Don't kill process that is not associated with this task.
2627 continue;
2628 }
2629
2630 for (int k = 0; k < proc.activities.size(); k++) {
Bryce Leeaf691c02017-03-20 14:20:22 -07002631 TaskRecord otherTask = proc.activities.get(k).getTask();
Winson Chung010927a2016-12-15 16:12:35 -08002632 if (tr.taskId != otherTask.taskId && otherTask.inRecents) {
2633 // Don't kill process(es) that has an activity in a different task that is
2634 // also in recents.
2635 return;
2636 }
2637 }
2638
2639 if (proc.foregroundServices) {
2640 // Don't kill process(es) with foreground service.
2641 return;
2642 }
2643
2644 // Add process to kill list.
2645 procsToKill.add(proc);
2646 }
2647 }
2648
2649 // Kill the running processes.
2650 for (int i = 0; i < procsToKill.size(); i++) {
2651 ProcessRecord pr = procsToKill.get(i);
2652 if (pr.setSchedGroup == ProcessList.SCHED_GROUP_BACKGROUND
2653 && pr.curReceivers.isEmpty()) {
2654 pr.kill("remove task", true);
2655 } else {
2656 // We delay killing processes that are not in the background or running a receiver.
2657 pr.waitingToKill = "remove task";
2658 }
2659 }
2660 }
2661
Craig Mautner4a1cb222013-12-04 16:14:06 -08002662 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002663 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002664 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2665 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002666 break;
2667 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002668 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002669 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002670 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002671 }
2672
Chong Zhang5dcb2752015-08-18 13:50:26 -07002673 /**
2674 * Restores a recent task to a stack
2675 * @param task The recent task to be restored.
2676 * @param stackId The stack to restore the task to (default launch stack will be used
Andrii Kulian16802aa2016-11-02 12:21:33 -07002677 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}
2678 * or is not a static stack).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002679 * @return true if the task has been restored successfully.
2680 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002681 boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002682 if (!StackId.isStaticStack(stackId)) {
2683 // If stack is not static (or stack id is invalid) - use the default one.
2684 // This means that tasks that were on external displays will be restored on the
2685 // primary display.
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002686 stackId = task.getLaunchStackId();
Winson Chungd3395382016-12-13 11:49:09 -08002687 } else if (stackId == DOCKED_STACK_ID && !task.supportsSplitScreen()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002688 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2689 // Put it in the fullscreen stack.
2690 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002691 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002692
Andrii Kulian02b7a832016-10-06 23:11:56 -07002693 final ActivityStack currentStack = task.getStack();
2694 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002695 // Task has already been restored once. See if we need to do anything more
Andrii Kulian02b7a832016-10-06 23:11:56 -07002696 if (currentStack.mStackId == stackId) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002697 // Nothing else to do since it is already restored in the right stack.
2698 return true;
2699 }
2700 // Remove current stack association, so we can re-associate the task with the
2701 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07002702 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002703 }
2704
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002705 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002706 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002707
2708 if (stack == null) {
2709 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002710 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2711 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002712 return false;
2713 }
2714
Wale Ogunwale72919d22016-12-08 18:58:50 -08002715 stack.addTask(task, false /* toTop */, "restoreRecentTask");
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002716 // TODO: move call for creation here and other place into Stack.addTask()
2717 task.createWindowContainer(false /* toTop */, true /* showForAllUsers */);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002718 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2719 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002720 final ArrayList<ActivityRecord> activities = task.mActivities;
2721 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002722 activities.get(activityNdx).createWindowContainer();
Wale Ogunwale7de05352014-12-12 15:21:33 -08002723 }
2724 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002725 }
2726
Wale Ogunwale040b4702015-08-06 18:10:50 -07002727 /**
Andrii Kulian839def92016-11-02 10:58:58 -07002728 * Move stack with all its existing content to specified display.
2729 * @param stackId Id of stack to move.
2730 * @param displayId Id of display to move stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08002731 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07002732 */
Andrii Kulian250d6532017-02-08 23:30:45 -08002733 void moveStackToDisplayLocked(int stackId, int displayId, boolean onTop) {
Andrii Kulian839def92016-11-02 10:58:58 -07002734 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2735 if (activityDisplay == null) {
2736 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown displayId="
2737 + displayId);
2738 }
2739 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
2740 if (activityContainer != null) {
2741 if (activityContainer.isAttachedLocked()) {
2742 if (activityContainer.getDisplayId() == displayId) {
2743 throw new IllegalArgumentException("Trying to move stackId=" + stackId
2744 + " to its current displayId=" + displayId);
2745 }
2746
Andrii Kulian250d6532017-02-08 23:30:45 -08002747 activityContainer.moveToDisplayLocked(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07002748 } else {
2749 throw new IllegalStateException("moveStackToDisplayLocked: Stack with stackId="
2750 + stackId + " is not attached to any display.");
2751 }
2752 } else {
2753 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown stackId="
2754 + stackId);
2755 }
2756
2757 ensureActivitiesVisibleLocked(null /* starting */, 0 /* configChanges */,
2758 !PRESERVE_WINDOWS);
2759 // TODO(multi-display): resize stacks properly if moved from split-screen.
2760 }
2761
2762 /**
Winson Chung74666102017-02-22 17:49:24 -08002763 * Returns the reparent target stack, creating the stack if necessary. This call also enforces
2764 * the various checks on tasks that are going to be reparented from one stack to another.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002765 */
Winson Chung74666102017-02-22 17:49:24 -08002766 ActivityStack getReparentTargetStack(TaskRecord task, int stackId, boolean toTop) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002767 final ActivityStack prevStack = task.getStack();
Chong Zhang02898352015-08-21 17:27:14 -07002768
Winson Chung74666102017-02-22 17:49:24 -08002769 // Check that we aren't reparenting to the same stack that the task is already in
2770 if (prevStack != null && prevStack.mStackId == stackId) {
2771 Slog.w(TAG, "Can not reparent to same stack, task=" + task
2772 + " already in stackId=" + stackId);
2773 return prevStack;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002774 }
2775
Winson Chung74666102017-02-22 17:49:24 -08002776 // Ensure that we aren't trying to move into a multi-window stack without multi-window
2777 // support
2778 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2779 throw new IllegalArgumentException("Device doesn't support multi-window, can not"
2780 + " reparent task=" + task + " to stackId=" + stackId);
Winson Chungc2baac02017-01-11 13:34:47 -08002781 }
2782
Winson Chung74666102017-02-22 17:49:24 -08002783 // Ensure that we aren't trying to move into a freeform stack without freeform
2784 // support
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002785 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
Winson Chung74666102017-02-22 17:49:24 -08002786 throw new IllegalArgumentException("Device doesn't support freeform, can not reparent"
2787 + " task=" + task);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002788 }
2789
Winson Chung74666102017-02-22 17:49:24 -08002790 // We don't allow moving a unresizeable task to the docked stack since the docked stack is
2791 // used for split-screen mode and will cause things like the docked divider to show up. We
2792 // instead leave the task in its current stack or move it to the fullscreen stack if it
2793 // isn't currently in a stack.
2794 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
2795 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
2796 Slog.w(TAG, "Can not move unresizeable task=" + task + " to docked stack."
2797 + " Moving to stackId=" + stackId + " instead.");
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002798 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002799
Winson Chung74666102017-02-22 17:49:24 -08002800 // Temporarily disable resizeablility of the task as we don't want it to be resized if, for
2801 // example, a docked stack is created which will lead to the stack we are moving from being
2802 // resized and and its resizeable tasks being resized.
Wale Ogunwale961f4852016-02-01 20:25:54 -08002803 try {
Winson Chung74666102017-02-22 17:49:24 -08002804 task.mTemporarilyUnresizable = true;
2805 return getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002806 } finally {
Winson Chung74666102017-02-22 17:49:24 -08002807 task.mTemporarilyUnresizable = false;
Chong Zhangf596cd52016-01-05 13:42:44 -08002808 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002809 }
2810
Winson Chung08f81892017-03-02 15:40:51 -08002811 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect destBounds) {
Wale Ogunwale079a0042015-10-24 11:44:07 -07002812 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2813 if (stack == null) {
2814 throw new IllegalArgumentException(
2815 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2816 }
2817
2818 final ActivityRecord r = stack.topRunningActivityLocked();
2819 if (r == null) {
2820 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2821 + " in stack=" + stack);
2822 return false;
2823 }
2824
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002825 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002826 Slog.w(TAG,
2827 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002828 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002829 return false;
2830 }
2831
Winson Chung08f81892017-03-02 15:40:51 -08002832 moveActivityToPinnedStackLocked(r, null /* sourceBounds */, destBounds,
2833 true /* moveHomeStackToFront */, "moveTopActivityToPinnedStack");
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002834 return true;
2835 }
2836
Winson Chung08f81892017-03-02 15:40:51 -08002837 void moveActivityToPinnedStackLocked(ActivityRecord r, Rect sourceBounds, Rect destBounds,
2838 boolean moveHomeStackToFront, String reason) {
2839
Wale Ogunwale480dca02016-02-06 13:58:29 -08002840 mWindowManager.deferSurfaceLayout();
Winson Chung74666102017-02-22 17:49:24 -08002841
Wale Ogunwale1666e312016-12-16 11:27:18 -08002842 // Need to make sure the pinned stack exist so we can resize it below...
Winson Chung55893332017-02-17 17:13:10 -08002843 final PinnedActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Wale Ogunwale1666e312016-12-16 11:27:18 -08002844
Wale Ogunwale480dca02016-02-06 13:58:29 -08002845 try {
Bryce Leeaf691c02017-03-20 14:20:22 -07002846 final TaskRecord task = r.getTask();
Wale Ogunwale480dca02016-02-06 13:58:29 -08002847
Andrii Kulian02b7a832016-10-06 23:11:56 -07002848 if (r == task.getStack().getVisibleBehindActivity()) {
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002849 // An activity can't be pinned and visible behind at the same time. Go ahead and
2850 // release it from been visible behind before pinning.
2851 requestVisibleBehindLocked(r, false);
2852 }
2853
Wale Ogunwale480dca02016-02-06 13:58:29 -08002854 // Resize the pinned stack to match the current size of the task the activity we are
2855 // going to be moving is currently contained in. We do this to have the right starting
2856 // animation bounds for the pinned stack to the desired bounds the caller wants.
2857 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2858 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002859 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002860
2861 if (task.mActivities.size() == 1) {
2862 // There is only one activity in the task. So, we can just move the task over to
Winson Chungc2baac02017-01-11 13:34:47 -08002863 // the stack without re-parenting the activity in a different task. We don't
2864 // move the home stack forward if we are currently entering picture-in-picture
2865 // while pausing because that changes the focused stack and may prevent the new
2866 // starting activity from resuming.
2867 if (moveHomeStackToFront && task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE
Winson Chung105ae5f2017-03-06 15:06:28 -08002868 && (r.state == RESUMED || !r.supportsPictureInPictureWhilePausing)) {
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08002869 // Move the home stack forward if the task we just moved to the pinned stack
2870 // was launched from home so home should be visible behind it.
2871 moveHomeStackToFront(reason);
2872 }
Winson Chung5af42fc2017-03-24 17:11:33 -07002873 // Defer resume until below, and do not schedule PiP changes until we animate below
Winson Chung74666102017-02-22 17:49:24 -08002874 task.reparent(PINNED_STACK_ID, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
Winson Chung5af42fc2017-03-24 17:11:33 -07002875 DEFER_RESUME, false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002876 } else {
Winson Chungb5c41b72016-12-07 15:00:47 -08002877 // There are multiple activities in the task and moving the top activity should
Winson Chung74666102017-02-22 17:49:24 -08002878 // reveal/leave the other activities in their original task.
2879
2880 // Currently, we don't support reparenting activities across tasks in two different
2881 // stacks, so instead, just create a new task in the same stack, reparent the
2882 // activity into that task, and then reparent the whole task to the new stack. This
2883 // ensures that all the necessary work to migrate states in the old and new stacks
2884 // is also done.
2885 final TaskRecord newTask = task.getStack().createTaskRecord(
2886 getNextTaskIdForUserLocked(r.userId), r.info, r.intent, null, null, true,
2887 r.mActivityType);
2888 r.reparent(newTask, MAX_VALUE, "moveActivityToStack");
2889
Winson Chung5af42fc2017-03-24 17:11:33 -07002890 // Defer resume until below, and do not schedule PiP changes until we animate below
Winson Chung74666102017-02-22 17:49:24 -08002891 newTask.reparent(PINNED_STACK_ID, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
Winson Chung5af42fc2017-03-24 17:11:33 -07002892 DEFER_RESUME, false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002893 }
Winson Chungc2baac02017-01-11 13:34:47 -08002894
2895 // Reset the state that indicates it can enter PiP while pausing after we've moved it
2896 // to the pinned stack
2897 r.supportsPictureInPictureWhilePausing = false;
Wale Ogunwale480dca02016-02-06 13:58:29 -08002898 } finally {
2899 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002900 }
2901
Wale Ogunwale480dca02016-02-06 13:58:29 -08002902 // The task might have already been running and its visibility needs to be synchronized
2903 // with the visibility of the stack / windows.
Wale Ogunwale079a0042015-10-24 11:44:07 -07002904 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002905 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002906
Winson Chung47900652017-04-06 18:44:25 -07002907 // TODO(b/36099777): Schedule the PiP mode change here immediately until we can defer all
2908 // callbacks until after the bounds animation
2909 scheduleUpdatePictureInPictureModeIfNeeded(r.getTask(), destBounds, true /* immediate */);
2910
Winson Chung08f81892017-03-02 15:40:51 -08002911 stack.animateResizePinnedStack(sourceBounds, destBounds, -1 /* animationDuration */);
Winson Chungc81c0ce2017-03-17 12:27:30 -07002912 mService.mTaskChangeNotificationController.notifyActivityPinned(r.packageName);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002913 }
2914
Andrii Kulian0864bbb2017-02-16 15:45:58 -08002915 /** Move activity with its stack to front and make the stack focused. */
Chong Zhang6cda19c2016-06-14 19:07:56 -07002916 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
2917 if (r == null || !r.isFocusable()) {
2918 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2919 "moveActivityStackToFront: unfocusable r=" + r);
2920 return false;
2921 }
2922
Bryce Leeaf691c02017-03-20 14:20:22 -07002923 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002924 final ActivityStack stack = r.getStack();
2925 if (stack == null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07002926 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
2927 + r + " task=" + task);
2928 return false;
2929 }
2930
Chong Zhang6cda19c2016-06-14 19:07:56 -07002931 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
2932 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2933 "moveActivityStackToFront: already on top, r=" + r);
2934 return false;
2935 }
2936
2937 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2938 "moveActivityStackToFront: r=" + r);
2939
2940 stack.moveToFront(reason, task);
2941 return true;
2942 }
2943
David Stevensc6b91c62017-02-08 14:23:58 -08002944 ActivityRecord findTaskLocked(ActivityRecord r, int displayId) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002945 mTmpFindTaskResult.r = null;
2946 mTmpFindTaskResult.matchedByRootAffinity = false;
David Stevensc6b91c62017-02-08 14:23:58 -08002947 ActivityRecord affinityMatch = null;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002948 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002949 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2950 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002951 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2952 final ActivityStack stack = stacks.get(stackNdx);
Matthew Ngae1ff4f2016-11-10 15:49:14 -08002953 if (!r.isApplicationActivity() && !stack.isHomeOrRecentsStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002954 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002955 continue;
2956 }
2957 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002958 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2959 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002960 continue;
2961 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002962 stack.findTaskLocked(r, mTmpFindTaskResult);
David Stevensc6b91c62017-02-08 14:23:58 -08002963 // It is possible to have tasks in multiple stacks with the same root affinity, so
2964 // we should keep looking after finding an affinity match to see if there is a
2965 // better match in another stack. Also, task affinity isn't a good enough reason
2966 // to target a display which isn't the source of the intent, so skip any affinity
2967 // matches not on the specified display.
2968 if (mTmpFindTaskResult.r != null) {
2969 if (!mTmpFindTaskResult.matchedByRootAffinity) {
2970 return mTmpFindTaskResult.r;
2971 } else if (mTmpFindTaskResult.r.getDisplayId() == displayId) {
2972 affinityMatch = mTmpFindTaskResult.r;
2973 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002974 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002975 }
2976 }
David Stevensc6b91c62017-02-08 14:23:58 -08002977 if (DEBUG_TASKS && affinityMatch == null) Slog.d(TAG_TASKS, "No task found");
2978 return affinityMatch;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002979 }
2980
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002981 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
2982 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002983 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2984 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002985 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002986 final ActivityRecord ar = stacks.get(stackNdx)
2987 .findActivityLocked(intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002988 if (ar != null) {
2989 return ar;
2990 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002991 }
2992 }
2993 return null;
2994 }
2995
Craig Mautner8d341ef2013-03-26 09:03:27 -07002996 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002997 scheduleSleepTimeout();
2998 if (!mGoingToSleep.isHeld()) {
2999 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003000 if (mLaunchingActivity.isHeld()) {
3001 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3002 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003003 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003004 mLaunchingActivity.release();
3005 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003006 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003007 }
Amith Yamasanice15e152013-09-19 12:30:32 -07003008 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003009 }
3010
3011 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003012 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003013
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003014 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003015 final long endTime = System.currentTimeMillis() + timeout;
3016 while (true) {
3017 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003018 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3019 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003020 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3021 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
3022 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003023 }
3024 if (cantShutdown) {
3025 long timeRemaining = endTime - System.currentTimeMillis();
3026 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003027 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003028 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003029 } catch (InterruptedException e) {
3030 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003031 } else {
3032 Slog.w(TAG, "Activity manager shutdown timed out");
3033 timedout = true;
3034 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003035 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003036 } else {
3037 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003038 }
3039 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003040
3041 // Force checkReadyForSleep to complete.
3042 mSleepTimeout = true;
3043 checkReadyForSleepLocked();
3044
Craig Mautner8d341ef2013-03-26 09:03:27 -07003045 return timedout;
3046 }
3047
3048 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003049 removeSleepTimeouts();
3050 if (mGoingToSleep.isHeld()) {
3051 mGoingToSleep.release();
3052 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003053 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3054 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003055 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3056 final ActivityStack stack = stacks.get(stackNdx);
3057 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003058 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003059 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003060 }
Craig Mautner5314a402013-09-26 12:40:16 -07003061 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003062 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003063 mGoingToSleepActivities.clear();
3064 }
3065
3066 void activitySleptLocked(ActivityRecord r) {
3067 mGoingToSleepActivities.remove(r);
3068 checkReadyForSleepLocked();
3069 }
3070
3071 void checkReadyForSleepLocked() {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003072 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003073 // Do not care.
3074 return;
3075 }
3076
3077 if (!mSleepTimeout) {
3078 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003079 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3080 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003081 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3082 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3083 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003084 }
3085
3086 if (mStoppingActivities.size() > 0) {
3087 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003088 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003089 + mStoppingActivities.size() + " activities");
3090 scheduleIdleLocked();
3091 dontSleep = true;
3092 }
3093
3094 if (mGoingToSleepActivities.size() > 0) {
3095 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003096 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003097 + mGoingToSleepActivities.size() + " activities");
3098 dontSleep = true;
3099 }
3100
3101 if (dontSleep) {
3102 return;
3103 }
3104 }
3105
Wei Wang65c7a152016-06-02 18:51:22 -07003106 // Send launch end powerhint before going sleep
3107 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
3108
Craig Mautnere0a38842013-12-16 16:14:02 -08003109 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3110 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003111 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3112 stacks.get(stackNdx).goToSleep();
3113 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003114 }
3115
3116 removeSleepTimeouts();
3117
3118 if (mGoingToSleep.isHeld()) {
3119 mGoingToSleep.release();
3120 }
3121 if (mService.mShuttingDown) {
3122 mService.notifyAll();
3123 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003124 }
3125
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003126 boolean reportResumedActivityLocked(ActivityRecord r) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003127 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003128 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003129 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003130 }
3131 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003132 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003133 mWindowManager.executeAppTransition();
3134 return true;
3135 }
3136 return false;
3137 }
3138
Craig Mautner8d341ef2013-03-26 09:03:27 -07003139 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003140 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3141 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003142 int stackNdx = stacks.size() - 1;
3143 while (stackNdx >= 0) {
3144 stacks.get(stackNdx).handleAppCrashLocked(app);
3145 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003146 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003147 }
3148 }
3149
Jose Lima4b6c6692014-08-12 17:41:12 -07003150 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003151 final ActivityStack stack = r.getStack();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003152 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003153 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3154 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003155 return false;
3156 }
Wale Ogunwale43896cf2016-04-16 10:54:30 -07003157
3158 if (visible && !StackId.activitiesCanRequestVisibleBehind(stack.mStackId)) {
3159 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: r=" + r
3160 + " visible=" + visible + " stackId=" + stack.mStackId
3161 + " can't contain visible behind activities");
3162 return false;
3163 }
3164
Jose Lima4b6c6692014-08-12 17:41:12 -07003165 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003166 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3167 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003168
3169 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003170 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003171 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003172 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003173 return true;
3174 }
3175
3176 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003177 if (visible && top.fullscreen) {
3178 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003179 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3180 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3181 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3182 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003183 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003184 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3185 // Only the activity set as currently visible behind should actively reset its
3186 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003187 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3188 "requestVisibleBehind: returning visible=" + visible
3189 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3190 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003191 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003192 }
3193
Jose Lima4b6c6692014-08-12 17:41:12 -07003194 stack.setVisibleBehindActivity(visible ? r : null);
3195 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003196 // If there is a translucent home activity, we need to force it stop being translucent,
3197 // because we can't depend on the application to necessarily perform that operation.
3198 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003199 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003200 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003201 mService.convertFromTranslucent(next.appToken);
3202 }
3203 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003204 if (top.app != null && top.app.thread != null) {
3205 // Notify the top app of the change.
3206 try {
3207 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3208 } catch (RemoteException e) {
3209 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003210 }
3211 return true;
3212 }
3213
Craig Mautnerbb742462014-07-07 15:28:55 -07003214 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07003215 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003216 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003217 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07003218
3219 r.mLaunchTaskBehind = false;
Andrii Kulian21713ac2016-10-12 22:05:05 -07003220 task.setLastThumbnailLocked(r.screenshotActivityLocked());
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003221 mRecentTasks.addLocked(task);
Yorke Leebd54c2a2016-10-25 13:49:23 -07003222 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003223 r.setVisibility(false);
Winson730bf062016-03-31 18:04:56 -07003224
3225 // When launching tasks behind, update the last active time of the top task after the new
3226 // task has been shown briefly
3227 final ActivityRecord top = stack.topActivity();
3228 if (top != null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003229 top.getTask().touchActiveTime();
Winson730bf062016-03-31 18:04:56 -07003230 }
Craig Mautnerbb742462014-07-07 15:28:55 -07003231 }
3232
3233 void scheduleLaunchTaskBehindComplete(IBinder token) {
3234 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3235 }
3236
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003237 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3238 boolean preserveWindows) {
Jorim Jaggife762342016-10-13 14:33:27 +02003239 mKeyguardController.beginActivityVisibilityUpdate();
3240 try {
3241 // First the front stacks. In case any are not fullscreen and are in front of home.
3242 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3243 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3244 final int topStackNdx = stacks.size() - 1;
3245 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3246 final ActivityStack stack = stacks.get(stackNdx);
3247 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
3248 }
Craig Mautner580ea812013-04-25 12:58:38 -07003249 }
Jorim Jaggife762342016-10-13 14:33:27 +02003250 } finally {
3251 mKeyguardController.endActivityVisibilityUpdate();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003252 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003253 }
3254
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003255 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
3256 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3257 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3258 final int topStackNdx = stacks.size() - 1;
3259 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3260 final ActivityStack stack = stacks.get(stackNdx);
3261 stack.addStartingWindowsForVisibleActivities(taskSwitch);
3262 }
3263 }
3264 }
3265
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003266 void invalidateTaskLayers() {
3267 mTaskLayersChanged = true;
3268 }
3269
3270 void rankTaskLayersIfNeeded() {
3271 if (!mTaskLayersChanged) {
3272 return;
3273 }
3274 mTaskLayersChanged = false;
3275 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
3276 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3277 int baseLayer = 0;
3278 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3279 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
3280 }
3281 }
3282 }
3283
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003284 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3285 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3286 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3287 final int topStackNdx = stacks.size() - 1;
3288 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3289 final ActivityStack stack = stacks.get(stackNdx);
3290 stack.clearOtherAppTimeTrackers(except);
3291 }
3292 }
3293 }
3294
Craig Mautner8d341ef2013-03-26 09:03:27 -07003295 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003296 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3297 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003298 final int numStacks = stacks.size();
3299 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3300 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003301 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003302 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003303 }
3304 }
3305
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003306 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3307 // Examine all activities currently running in the process.
3308 TaskRecord firstTask = null;
3309 // Tasks is non-null only if two or more tasks are found.
3310 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003311 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3312 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003313 ActivityRecord r = app.activities.get(i);
3314 // First, if we find an activity that is in the process of being destroyed,
3315 // then we just aren't going to do anything for now; we want things to settle
3316 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003317 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003318 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003319 return;
3320 }
3321 // Don't consider any activies that are currently not in a state where they
3322 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003323 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3324 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003325 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003326 continue;
3327 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003328
3329 final TaskRecord task = r.getTask();
3330 if (task != null) {
3331 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003332 + " from " + r);
3333 if (firstTask == null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003334 firstTask = task;
3335 } else if (firstTask != task) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003336 if (tasks == null) {
3337 tasks = new ArraySet<>();
3338 tasks.add(firstTask);
3339 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003340 tasks.add(task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003341 }
3342 }
3343 }
3344 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003345 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003346 return;
3347 }
3348 // If we have activities in multiple tasks that are in a position to be destroyed,
3349 // let's iterate through the tasks and release the oldest one.
3350 final int numDisplays = mActivityDisplays.size();
3351 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3352 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3353 // Step through all stacks starting from behind, to hit the oldest things first.
3354 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3355 final ActivityStack stack = stacks.get(stackNdx);
3356 // Try to release activities in this stack; if we manage to, we are done.
3357 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3358 return;
3359 }
3360 }
3361 }
3362 }
3363
Amith Yamasani37a40c22015-06-17 13:25:42 -07003364 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003365 final int focusStackId = mFocusedStack.getStackId();
3366 // We dismiss the docked stack whenever we switch users.
3367 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, focusStackId == DOCKED_STACK_ID);
Winson Chungc2b23a52017-01-09 15:44:55 -08003368 // Also dismiss the pinned stack whenever we switch users. Removing the pinned stack will
3369 // also cause all tasks to be moved to the fullscreen stack at a position that is
3370 // appropriate.
3371 removeStackLocked(PINNED_STACK_ID);
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003372
3373 mUserStackInFront.put(mCurrentUser, focusStackId);
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003374 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003375 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003376
Craig Mautner858d8a62013-04-23 17:08:34 -07003377 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003378 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3379 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003380 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003381 final ActivityStack stack = stacks.get(stackNdx);
3382 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003383 TaskRecord task = stack.topTask();
3384 if (task != null) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003385 stack.positionChildWindowContainerAtTop(task);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003386 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003387 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003388 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003389
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003390 ActivityStack stack = getStack(restoreStackId);
3391 if (stack == null) {
3392 stack = mHomeStack;
3393 }
3394 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003395 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003396 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003397 } else {
3398 // Stack was moved to another display while user was swapped out.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003399 resumeHomeStackTask(null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003400 }
Craig Mautner93529a42013-10-04 15:03:13 -07003401 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003402 }
3403
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003404 /** Checks whether the userid is a profile of the current user. */
3405 boolean isCurrentProfileLocked(int userId) {
3406 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003407 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003408 }
3409
Winson Chung4dabf232017-01-25 13:25:22 -08003410 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(ActivityRecord idleActivity,
3411 boolean remove, boolean processPausingActivities) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003412 ArrayList<ActivityRecord> stops = null;
3413
3414 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003415 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3416 ActivityRecord s = mStoppingActivities.get(activityNdx);
Andrii Kulianee056812016-09-21 15:34:45 -07003417 // TODO: Remove mWaitingVisibleActivities list and just remove activity from
3418 // mStoppingActivities when something else comes up.
Wale Ogunwale3c519302016-07-14 09:17:59 -07003419 boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003420 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003421 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3422 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003423 mWaitingVisibleActivities.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003424 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003425 if (s.finishing) {
3426 // If this activity is finishing, it is sitting on top of
3427 // everyone else but we now know it is no longer needed...
3428 // so get rid of it. Otherwise, we need to go through the
3429 // normal flow and hide it once we determine that it is
3430 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003431 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003432 s.setVisibility(false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003433 }
3434 }
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003435 if ((!waitingVisible || mService.isSleepingOrShuttingDownLocked()) && remove) {
Winson Chung4dabf232017-01-25 13:25:22 -08003436 if (!processPausingActivities && s.state == PAUSING) {
3437 // Defer processing pausing activities in this iteration and reschedule
3438 // a delayed idle to reprocess it again
3439 removeTimeoutsForActivityLocked(idleActivity);
3440 scheduleIdleTimeoutLocked(idleActivity);
3441 continue;
3442 }
3443
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003444 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003445 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003446 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003447 }
3448 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003449 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003450 }
3451 }
3452
3453 return stops;
3454 }
3455
Craig Mautnercf910b02013-04-23 11:23:27 -07003456 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003457 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3458 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3459 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3460 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003461 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003462 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003463 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003464 if (r == null) Slog.e(TAG,
3465 "validateTop...: null top activity, stack=" + stack);
3466 else {
3467 final ActivityRecord pausing = stack.mPausingActivity;
3468 if (pausing != null && pausing == r) Slog.e(TAG,
3469 "validateTop...: top stack has pausing activity r=" + r
3470 + " state=" + state);
3471 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3472 "validateTop...: activity in front not resumed r=" + r
3473 + " state=" + state);
3474 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003475 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003476 final ActivityRecord resumed = stack.mResumedActivity;
3477 if (resumed != null && resumed == r) Slog.e(TAG,
3478 "validateTop...: back stack has resumed activity r=" + r
3479 + " state=" + state);
3480 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3481 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003482 }
3483 }
3484 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003485 }
3486
Craig Mautnere0570202015-05-13 13:06:11 -07003487 private String lockTaskModeToString() {
3488 switch (mLockTaskModeState) {
3489 case LOCK_TASK_MODE_LOCKED:
3490 return "LOCKED";
3491 case LOCK_TASK_MODE_PINNED:
3492 return "PINNED";
3493 case LOCK_TASK_MODE_NONE:
3494 return "NONE";
3495 default: return "unknown=" + mLockTaskModeState;
3496 }
3497 }
3498
Craig Mautner27084302013-03-25 08:05:25 -07003499 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003500 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003501 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003502 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003503 pw.print(prefix);
3504 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003505 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003506 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003507 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
Jorim Jaggi8d786932016-10-26 19:08:36 -07003508 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3509 if (packages.size() > 0) {
3510 pw.print(prefix); pw.println("mLockTaskPackages (userId:packages)=");
3511 for (int i = 0; i < packages.size(); ++i) {
3512 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3513 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3514 }
3515 }
3516 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
3517 mKeyguardController.dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07003518 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003519
Winson43d1f262016-06-14 16:05:55 -07003520 /**
Andrii Kulian5406e7a2016-10-21 11:55:23 -07003521 * Dump all connected displays' configurations.
3522 * @param prefix Prefix to apply to each line of the dump.
3523 */
3524 void dumpDisplayConfigs(PrintWriter pw, String prefix) {
3525 pw.print(prefix); pw.println("Display override configurations:");
3526 final int displayCount = mActivityDisplays.size();
3527 for (int i = 0; i < displayCount; i++) {
3528 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(i);
3529 pw.print(prefix); pw.print(" "); pw.print(activityDisplay.mDisplayId); pw.print(": ");
3530 pw.println(activityDisplay.getOverrideConfiguration());
3531 }
3532 }
3533
3534 /**
Winson43d1f262016-06-14 16:05:55 -07003535 * Dumps the activities matching the given {@param name} in the either the focused stack
3536 * or all visible stacks if {@param dumpVisibleStacks} is true.
3537 */
Winson Chung6998bc42017-02-28 17:07:05 -08003538 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacksOnly,
3539 boolean dumpFocusedStackOnly) {
3540 if (dumpFocusedStackOnly) {
3541 return mFocusedStack.getDumpActivitiesLocked(name);
3542 } else {
Winson43d1f262016-06-14 16:05:55 -07003543 ArrayList<ActivityRecord> activities = new ArrayList<>();
3544 int numDisplays = mActivityDisplays.size();
3545 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3546 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3547 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3548 ActivityStack stack = stacks.get(stackNdx);
Winson Chung6998bc42017-02-28 17:07:05 -08003549 if (!dumpVisibleStacksOnly ||
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07003550 stack.shouldBeVisible(null) == STACK_VISIBLE) {
Winson43d1f262016-06-14 16:05:55 -07003551 activities.addAll(stack.getDumpActivitiesLocked(name));
3552 }
3553 }
3554 }
3555 return activities;
Winson43d1f262016-06-14 16:05:55 -07003556 }
Craig Mautner20e72272013-04-01 13:45:53 -07003557 }
3558
Dianne Hackborn390517b2013-05-30 15:03:32 -07003559 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3560 boolean needSep, String prefix) {
3561 if (activity != null) {
3562 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3563 if (needSep) {
3564 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003565 }
3566 pw.print(prefix);
3567 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003568 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003569 }
3570 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003571 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003572 }
3573
Craig Mautner8d341ef2013-03-26 09:03:27 -07003574 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3575 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003576 boolean printed = false;
3577 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003578 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3579 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003580 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003581 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003582 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003583 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003584 final ActivityStack stack = stacks.get(stackNdx);
3585 StringBuilder stackHeader = new StringBuilder(128);
3586 stackHeader.append(" Stack #");
3587 stackHeader.append(stack.mStackId);
3588 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003589 stackHeader.append("\n");
3590 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3591 stackHeader.append("\n");
3592 stackHeader.append(" mBounds=" + stack.mBounds);
Winson Chungabb433b2017-03-24 09:35:42 -07003593
3594 final boolean printedStackHeader = stack.dumpActivitiesLocked(fd, pw, dumpAll,
3595 dumpClient, dumpPackage, needSep, stackHeader.toString());
3596 printed |= printedStackHeader;
3597 if (!printedStackHeader) {
3598 // Ensure we always dump the stack header even if there are no activities
3599 pw.println();
3600 pw.println(stackHeader);
3601 }
3602
Craig Mautner4a1cb222013-12-04 16:14:06 -08003603 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3604 !dumpAll, false, dumpPackage, true,
3605 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003606
Craig Mautner4a1cb222013-12-04 16:14:06 -08003607 needSep = printed;
3608 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3609 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003610 if (pr) {
3611 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003612 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003613 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003614 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3615 " mResumedActivity: ");
3616 if (pr) {
3617 printed = true;
3618 needSep = false;
3619 }
3620 if (dumpAll) {
3621 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3622 " mLastPausedActivity: ");
3623 if (pr) {
3624 printed = true;
3625 needSep = true;
3626 }
3627 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3628 needSep, " mLastNoHistoryActivity: ");
3629 }
3630 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003631 }
3632 }
3633
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003634 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3635 false, dumpPackage, true, " Activities waiting to finish:", null);
3636 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3637 false, dumpPackage, true, " Activities waiting to stop:", null);
3638 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3639 false, dumpPackage, true, " Activities waiting for another to become visible:",
3640 null);
3641 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3642 false, dumpPackage, true, " Activities waiting to sleep:", null);
3643 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3644 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003645
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003646 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003647 }
3648
Dianne Hackborn390517b2013-05-30 15:03:32 -07003649 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003650 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003651 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003652 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003653 String innerPrefix = null;
3654 String[] args = null;
3655 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003656 for (int i=list.size()-1; i>=0; i--) {
3657 final ActivityRecord r = list.get(i);
3658 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3659 continue;
3660 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003661 if (innerPrefix == null) {
3662 innerPrefix = prefix + " ";
3663 args = new String[0];
3664 }
3665 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003666 final boolean full = !brief && (complete || !r.isInHistory());
3667 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003668 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003669 needNL = false;
3670 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003671 if (header1 != null) {
3672 pw.println(header1);
3673 header1 = null;
3674 }
3675 if (header2 != null) {
3676 pw.println(header2);
3677 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003678 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003679 if (lastTask != r.getTask()) {
3680 lastTask = r.getTask();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003681 pw.print(prefix);
3682 pw.print(full ? "* " : " ");
3683 pw.println(lastTask);
3684 if (full) {
3685 lastTask.dump(pw, prefix + " ");
3686 } else if (complete) {
3687 // Complete + brief == give a summary. Isn't that obvious?!?
3688 if (lastTask.intent != null) {
3689 pw.print(prefix); pw.print(" ");
3690 pw.println(lastTask.intent.toInsecureStringWithClip());
3691 }
3692 }
3693 }
3694 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3695 pw.print(" #"); pw.print(i); pw.print(": ");
3696 pw.println(r);
3697 if (full) {
3698 r.dump(pw, innerPrefix);
3699 } else if (complete) {
3700 // Complete + brief == give a summary. Isn't that obvious?!?
3701 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3702 if (r.app != null) {
3703 pw.print(innerPrefix); pw.println(r.app);
3704 }
3705 }
3706 if (client && r.app != null && r.app.thread != null) {
3707 // flush anything that is already in the PrintWriter since the thread is going
3708 // to write to the file descriptor directly
3709 pw.flush();
3710 try {
3711 TransferPipe tp = new TransferPipe();
3712 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07003713 r.app.thread.dumpActivity(tp.getWriteFd(), r.appToken, innerPrefix, args);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003714 // Short timeout, since blocking here can
3715 // deadlock with the application.
3716 tp.go(fd, 2000);
3717 } finally {
3718 tp.kill();
3719 }
3720 } catch (IOException e) {
3721 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3722 } catch (RemoteException e) {
3723 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3724 }
3725 needNL = true;
3726 }
3727 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003728 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003729 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003730
Craig Mautnerf3333272013-04-22 10:55:53 -07003731 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003732 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3733 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003734 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3735 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003736 }
3737
3738 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003739 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003740 }
3741
3742 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003743 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3744 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003745 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3746 }
3747
Craig Mautner05d29032013-05-03 13:40:13 -07003748 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003749 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3750 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3751 }
Craig Mautner05d29032013-05-03 13:40:13 -07003752 }
3753
Craig Mautner0eea92c2013-05-16 13:35:39 -07003754 void removeSleepTimeouts() {
3755 mSleepTimeout = false;
3756 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3757 }
3758
3759 final void scheduleSleepTimeout() {
3760 removeSleepTimeouts();
3761 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3762 }
3763
Craig Mautner4a1cb222013-12-04 16:14:06 -08003764 @Override
3765 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003766 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003767 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3768 }
3769
3770 @Override
3771 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003772 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003773 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3774 }
3775
3776 @Override
3777 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003778 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003779 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3780 }
3781
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003782 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003783 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003784 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003785 newDisplay = mActivityDisplays.get(displayId) == null;
3786 if (newDisplay) {
3787 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003788 if (activityDisplay.mDisplay == null) {
3789 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3790 return;
3791 }
Craig Mautner4504de52013-12-20 09:06:56 -08003792 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003793 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Bryce Leeaf3a4002017-03-20 10:37:12 -07003794 mWindowManager.onDisplayAdded(displayId);
Craig Mautner4504de52013-12-20 09:06:56 -08003795 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003796 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003797 }
3798
Andrii Kulianca007a62016-11-22 16:33:28 -08003799 /** Check if display with specified id is added to the list. */
3800 boolean isDisplayAdded(int displayId) {
3801 return mActivityDisplays.get(displayId) != null;
3802 }
3803
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003804 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07003805 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07003806 mService.mContext.getResources().getDimensionPixelSize(
3807 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003808 }
3809
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003810 private void handleDisplayRemoved(int displayId) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08003811 if (displayId == DEFAULT_DISPLAY) {
3812 throw new IllegalArgumentException("Can't remove the primary display.");
3813 }
3814
Craig Mautner4a1cb222013-12-04 16:14:06 -08003815 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003816 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3817 if (activityDisplay != null) {
Andrii Kulian250d6532017-02-08 23:30:45 -08003818 final boolean destroyContentOnRemoval
3819 = activityDisplay.shouldDestroyContentOnRemove();
Andrii Kuliana33818c2017-02-16 16:11:30 -08003820 final ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
3821 while (!stacks.isEmpty()) {
3822 final ActivityStack stack = stacks.get(0);
Andrii Kulian250d6532017-02-08 23:30:45 -08003823 if (destroyContentOnRemoval) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08003824 moveStackToDisplayLocked(stack.mStackId, DEFAULT_DISPLAY,
3825 false /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08003826 stack.finishAllActivitiesLocked(true /* immediately */);
Andrii Kuliana33818c2017-02-16 16:11:30 -08003827 } else {
3828 // Moving all tasks to fullscreen stack, because it's guaranteed to be
3829 // a valid launch stack for all activities. This way the task history from
3830 // external display will be preserved on primary after move.
3831 moveTasksToFullscreenStackLocked(stack.getStackId(), true /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08003832 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003833 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003834 mActivityDisplays.remove(displayId);
Bryce Leeaf3a4002017-03-20 10:37:12 -07003835 mWindowManager.onDisplayRemoved(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003836 }
3837 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003838 }
3839
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003840 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003841 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003842 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3843 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003844 // TODO: Update the bounds.
3845 }
Bryce Leeaf3a4002017-03-20 10:37:12 -07003846 mWindowManager.onDisplayChanged(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003847 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003848 }
3849
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003850 private StackInfo getStackInfoLocked(ActivityStack stack) {
Jorim Jaggife762342016-10-13 14:33:27 +02003851 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003852 StackInfo info = new StackInfo();
Wale Ogunwale1666e312016-12-16 11:27:18 -08003853 stack.getWindowContainerBounds(info.bounds);
Jorim Jaggife762342016-10-13 14:33:27 +02003854 info.displayId = DEFAULT_DISPLAY;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003855 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003856 info.userId = stack.mCurrentUser;
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07003857 info.visible = stack.shouldBeVisible(null) == STACK_VISIBLE;
Winson529c8e42016-05-17 11:08:40 -07003858 info.position = display != null
3859 ? display.mStacks.indexOf(stack)
3860 : 0;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003861
3862 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3863 final int numTasks = tasks.size();
3864 int[] taskIds = new int[numTasks];
3865 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003866 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003867 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003868 for (int i = 0; i < numTasks; ++i) {
3869 final TaskRecord task = tasks.get(i);
3870 taskIds[i] = task.taskId;
3871 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3872 : task.realActivity != null ? task.realActivity.flattenToString()
3873 : task.getTopActivity() != null ? task.getTopActivity().packageName
3874 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003875 taskBounds[i] = new Rect();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08003876 task.getWindowContainerBounds(taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003877 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003878 }
3879 info.taskIds = taskIds;
3880 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003881 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003882 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07003883
3884 final ActivityRecord top = stack.topRunningActivityLocked();
3885 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003886 return info;
3887 }
3888
3889 StackInfo getStackInfoLocked(int stackId) {
3890 ActivityStack stack = getStack(stackId);
3891 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003892 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003893 }
3894 return null;
3895 }
3896
3897 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003898 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003899 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3900 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003901 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003902 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003903 }
3904 }
3905 return list;
3906 }
3907
Craig Mautner15df08a2015-04-01 12:17:18 -07003908 TaskRecord getLockedTaskLocked() {
3909 final int top = mLockTaskModeTasks.size() - 1;
3910 if (top >= 0) {
3911 return mLockTaskModeTasks.get(top);
3912 }
3913 return null;
3914 }
3915
3916 boolean isLockedTask(TaskRecord task) {
3917 return mLockTaskModeTasks.contains(task);
3918 }
3919
3920 boolean isLastLockedTask(TaskRecord task) {
3921 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3922 }
3923
3924 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003925 if (!mLockTaskModeTasks.remove(task)) {
3926 return;
3927 }
3928 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3929 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003930 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003931 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3932 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003933 final Message lockTaskMsg = Message.obtain();
3934 lockTaskMsg.arg1 = task.userId;
3935 lockTaskMsg.what = LOCK_TASK_END_MSG;
3936 mHandler.sendMessage(lockTaskMsg);
3937 }
3938 }
3939
Andrii Kulianc27916642016-04-12 17:59:27 -07003940 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId, int actualStackId) {
3941 handleNonResizableTaskIfNeeded(task, preferredStackId, actualStackId,
3942 false /* forceNonResizable */);
3943 }
3944
Jorim Jaggid53f0922016-04-06 22:16:23 -07003945 void handleNonResizableTaskIfNeeded(
Andrii Kulianc27916642016-04-12 17:59:27 -07003946 TaskRecord task, int preferredStackId, int actualStackId, boolean forceNonResizable) {
Jorim Jaggid53f0922016-04-06 22:16:23 -07003947 if ((!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID)
3948 || task.isHomeTask()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003949 return;
3950 }
3951
Jorim Jaggicd13d332016-04-27 15:40:20 -07003952 final ActivityRecord topActivity = task.getTopActivity();
Winson Chungd3395382016-12-13 11:49:09 -08003953 if (!task.supportsSplitScreen() || forceNonResizable) {
Jorim Jaggi2adba072016-03-03 13:43:39 +01003954 // Display a warning toast that we tried to put a non-dockable task in the docked stack.
Yorke Leebd54c2a2016-10-25 13:49:23 -07003955 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
Jorim Jaggid53f0922016-04-06 22:16:23 -07003956
Andrii Kulianc27916642016-04-12 17:59:27 -07003957 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
3958 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003959 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, actualStackId == DOCKED_STACK_ID);
Winson Chungd3395382016-12-13 11:49:09 -08003960 } else if (topActivity != null && topActivity.isNonResizableOrForcedResizable()
Jorim Jaggicd13d332016-04-27 15:40:20 -07003961 && !topActivity.noDisplay) {
3962 String packageName = topActivity.appInfo.packageName;
Yorke Leebd54c2a2016-10-25 13:49:23 -07003963 mService.mTaskChangeNotificationController.notifyActivityForcedResizable(
3964 task.taskId, packageName);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003965 }
Chong Zhangb15758a2015-11-17 12:12:03 -08003966 }
3967
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003968 void showLockTaskToast() {
Hall Liu45784f12016-05-31 15:50:48 -07003969 if (mLockTaskNotify != null) {
3970 mLockTaskNotify.showToast(mLockTaskModeState);
3971 }
Jason Monka8f569c2014-07-07 12:15:21 -04003972 }
3973
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003974 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3975 if (mLockTaskModeTasks.contains(task)) {
3976 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3977 }
3978 }
3979
Craig Mautner432f64e2015-05-20 14:59:57 -07003980 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3981 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003982 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003983 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003984 final TaskRecord lockedTask = getLockedTaskLocked();
3985 if (lockedTask != null) {
3986 removeLockedTaskLocked(lockedTask);
3987 if (!mLockTaskModeTasks.isEmpty()) {
3988 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003989 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3990 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003991 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003992 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003993 return;
3994 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003995 }
Craig Mautnere0570202015-05-13 13:06:11 -07003996 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3997 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003998 return;
3999 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004000
4001 // Should have already been checked, but do it again.
4002 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07004003 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4004 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08004005 return;
4006 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004007 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07004008 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07004009 return;
4010 }
4011
4012 if (mLockTaskModeTasks.isEmpty()) {
4013 // First locktask.
4014 final Message lockTaskMsg = Message.obtain();
4015 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
4016 lockTaskMsg.arg1 = task.userId;
4017 lockTaskMsg.what = LOCK_TASK_START_MSG;
4018 lockTaskMsg.arg2 = lockTaskModeState;
4019 mHandler.sendMessage(lockTaskMsg);
4020 }
4021 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07004022 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
4023 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07004024 mLockTaskModeTasks.remove(task);
4025 mLockTaskModeTasks.add(task);
4026
4027 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07004028 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07004029 }
Craig Mautner432f64e2015-05-20 14:59:57 -07004030
4031 if (andResume) {
Andrii Kulianc27916642016-04-12 17:59:27 -07004032 findTaskToMoveToFrontLocked(task, 0, null, reason,
4033 lockTaskModeState != LOCK_TASK_MODE_NONE);
Wale Ogunwaled046a012015-12-24 13:05:59 -08004034 resumeFocusedStackTopActivityLocked();
Jorim Jaggia42938e2016-11-22 14:31:38 +01004035 mWindowManager.executeAppTransition();
Andrii Kulianc27916642016-04-12 17:59:27 -07004036 } else if (lockTaskModeState != LOCK_TASK_MODE_NONE) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004037 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.getStackId(),
Andrii Kulianc27916642016-04-12 17:59:27 -07004038 true /* forceNonResizable */);
Craig Mautner432f64e2015-05-20 14:59:57 -07004039 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004040 }
4041
4042 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04004043 return isLockTaskModeViolation(task, false);
4044 }
4045
4046 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
4047 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004048 return false;
4049 }
4050 final int lockTaskAuth = task.mLockTaskAuth;
4051 switch (lockTaskAuth) {
4052 case LOCK_TASK_AUTH_DONT_LOCK:
4053 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01004054 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07004055 case LOCK_TASK_AUTH_LAUNCHABLE:
4056 case LOCK_TASK_AUTH_WHITELISTED:
4057 return false;
4058 case LOCK_TASK_AUTH_PINNABLE:
4059 // Pinnable tasks can't be launched on top of locktask tasks.
4060 return !mLockTaskModeTasks.isEmpty();
4061 default:
4062 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
4063 return true;
4064 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004065 }
4066
Craig Mautner15df08a2015-04-01 12:17:18 -07004067 void onLockTaskPackagesUpdatedLocked() {
4068 boolean didSomething = false;
4069 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
4070 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01004071 final boolean wasWhitelisted =
4072 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4073 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07004074 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01004075 final boolean isWhitelisted =
4076 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4077 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
4078 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004079 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07004080 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
4081 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07004082 removeLockedTaskLocked(lockedTask);
4083 lockedTask.performClearTaskLocked();
4084 didSomething = true;
4085 }
4086 }
4087 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4088 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4089 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4090 final ActivityStack stack = stacks.get(stackNdx);
4091 stack.onLockTaskPackagesUpdatedLocked();
4092 }
4093 }
Craig Mautnere0570202015-05-13 13:06:11 -07004094 final ActivityRecord r = topRunningActivityLocked();
Bryce Leeaf691c02017-03-20 14:20:22 -07004095 final TaskRecord task = r != null ? r.getTask() : null;
Craig Mautnere0570202015-05-13 13:06:11 -07004096 if (mLockTaskModeTasks.isEmpty() && task != null
4097 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4098 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004099 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4100 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4101 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4102 false);
4103 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004104 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004105 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004106 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004107 }
4108 }
4109
Benjamin Franz43261142015-02-11 15:59:44 +00004110 int getLockTaskModeState() {
4111 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004112 }
4113
Jorim Jaggife89d122015-12-22 16:28:44 +01004114 void activityRelaunchedLocked(IBinder token) {
4115 mWindowManager.notifyAppRelaunchingFinished(token);
Wale Ogunwale3e997362016-09-06 10:37:56 -07004116 if (mService.isSleepingOrShuttingDownLocked()) {
4117 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
4118 if (r != null) {
4119 r.setSleeping(true, true);
4120 }
4121 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004122 }
4123
4124 void activityRelaunchingLocked(ActivityRecord r) {
4125 mWindowManager.notifyAppRelaunching(r.appToken);
4126 }
4127
Filip Gruszczynski77d94482015-12-11 13:59:52 -08004128 void logStackState() {
4129 mActivityMetricsLogger.logWindowState();
4130 }
4131
Winson Chung5af42fc2017-03-24 17:11:33 -07004132 void scheduleUpdateMultiWindowMode(TaskRecord task) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004133 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4134 final ActivityRecord r = task.mActivities.get(i);
4135 if (r.app != null && r.app.thread != null) {
4136 mMultiWindowModeChangedActivities.add(r);
4137 }
4138 }
4139
4140 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
4141 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
4142 }
4143 }
4144
Winson Chung5af42fc2017-03-24 17:11:33 -07004145 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004146 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004147 if (prevStack == null || prevStack == stack
4148 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
4149 return;
4150 }
4151
Winson Chung5af42fc2017-03-24 17:11:33 -07004152 scheduleUpdatePictureInPictureModeIfNeeded(task, stack.mBounds, false /* immediate */);
4153 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004154
Winson Chung5af42fc2017-03-24 17:11:33 -07004155 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, Rect targetStackBounds,
4156 boolean immediate) {
4157
4158 if (immediate) {
4159 mHandler.removeMessages(REPORT_PIP_MODE_CHANGED_MSG);
4160 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4161 final ActivityRecord r = task.mActivities.get(i);
4162 if (r.app != null && r.app.thread != null) {
4163 r.updatePictureInPictureMode(targetStackBounds);
4164 }
4165 }
4166 } else {
4167 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4168 final ActivityRecord r = task.mActivities.get(i);
4169 if (r.app != null && r.app.thread != null) {
4170 mPipModeChangedActivities.add(r);
4171 }
4172 }
4173 mPipModeChangedTargetStackBounds = targetStackBounds;
4174
4175 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
4176 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
4177 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004178 }
4179 }
4180
Tony Mak853304c2016-04-18 15:17:41 +01004181 void setDockedStackMinimized(boolean minimized) {
4182 mIsDockMinimized = minimized;
Tony Mak853304c2016-04-18 15:17:41 +01004183 }
4184
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004185 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004186
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004187 public ActivityStackSupervisorHandler(Looper looper) {
4188 super(looper);
4189 }
4190
Winson Chung4dabf232017-01-25 13:25:22 -08004191 void activityIdleInternal(ActivityRecord r, boolean processPausingActivities) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004192 synchronized (mService) {
Winson Chung4dabf232017-01-25 13:25:22 -08004193 activityIdleInternalLocked(r != null ? r.appToken : null, true /* fromTimeout */,
4194 processPausingActivities, null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004195 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004196 }
4197
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004198 @Override
4199 public void handleMessage(Message msg) {
4200 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004201 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
4202 synchronized (mService) {
4203 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
4204 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Winson Chung5af42fc2017-03-24 17:11:33 -07004205 r.updateMultiWindowMode();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004206 }
4207 }
4208 } break;
4209 case REPORT_PIP_MODE_CHANGED_MSG: {
4210 synchronized (mService) {
4211 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
4212 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Winson Chung5af42fc2017-03-24 17:11:33 -07004213 r.updatePictureInPictureMode(mPipModeChangedTargetStackBounds);
Wale Ogunwale22e25262016-02-01 10:32:02 -08004214 }
4215 }
4216 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07004217 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004218 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4219 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004220 if (mService.mDidDexOpt) {
4221 mService.mDidDexOpt = false;
4222 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4223 nmsg.obj = msg.obj;
4224 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4225 return;
4226 }
4227 // We don't at this point know if the activity is fullscreen,
4228 // so we need to be conservative and assume it isn't.
Winson Chung4dabf232017-01-25 13:25:22 -08004229 activityIdleInternal((ActivityRecord) msg.obj,
4230 true /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004231 } break;
4232 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004233 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Winson Chung4dabf232017-01-25 13:25:22 -08004234 activityIdleInternal((ActivityRecord) msg.obj,
4235 false /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004236 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004237 case RESUME_TOP_ACTIVITY_MSG: {
4238 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004239 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07004240 }
4241 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004242 case SLEEP_TIMEOUT_MSG: {
4243 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07004244 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004245 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4246 mSleepTimeout = true;
4247 checkReadyForSleepLocked();
4248 }
4249 }
4250 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004251 case LAUNCH_TIMEOUT_MSG: {
4252 if (mService.mDidDexOpt) {
4253 mService.mDidDexOpt = false;
4254 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4255 return;
4256 }
4257 synchronized (mService) {
4258 if (mLaunchingActivity.isHeld()) {
4259 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4260 if (VALIDATE_WAKE_LOCK_CALLER
4261 && Binder.getCallingUid() != Process.myUid()) {
4262 throw new IllegalStateException("Calling must be system uid");
4263 }
4264 mLaunchingActivity.release();
4265 }
4266 }
4267 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004268 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004269 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004270 } break;
4271 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004272 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004273 } break;
4274 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004275 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004276 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004277 case CONTAINER_CALLBACK_VISIBILITY: {
4278 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004279 final IActivityContainerCallback callback = container.mCallback;
4280 if (callback != null) {
4281 try {
4282 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4283 } catch (RemoteException e) {
4284 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004285 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004286 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004287 case LOCK_TASK_START_MSG: {
4288 // When lock task starts, we disable the status bars.
4289 try {
Jason Monk62515be2014-05-21 16:06:19 -04004290 if (mLockTaskNotify == null) {
4291 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004292 }
Jason Monk62515be2014-05-21 16:06:19 -04004293 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004294 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004295 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004296 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004297 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004298 flags = StatusBarManager.DISABLE_MASK
4299 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004300 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004301 flags = StatusBarManager.DISABLE_MASK
4302 & (~StatusBarManager.DISABLE_BACK)
4303 & (~StatusBarManager.DISABLE_HOME)
4304 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004305 }
4306 getStatusBarService().disable(flags, mToken,
4307 mService.mContext.getPackageName());
4308 }
4309 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004310 if (getDevicePolicyManager() != null) {
4311 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004312 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004313 }
justinzhang5286d3f2014-05-12 17:06:01 -04004314 } catch (RemoteException ex) {
4315 throw new RuntimeException(ex);
4316 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004317 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004318 case LOCK_TASK_END_MSG: {
4319 // When lock task ends, we enable the status bars.
4320 try {
Jason Monk62515be2014-05-21 16:06:19 -04004321 if (getStatusBarService() != null) {
4322 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4323 mService.mContext.getPackageName());
4324 }
4325 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004326 if (getDevicePolicyManager() != null) {
4327 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4328 msg.arg1);
4329 }
Jason Monk62515be2014-05-21 16:06:19 -04004330 if (mLockTaskNotify == null) {
4331 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4332 }
4333 mLockTaskNotify.show(false);
4334 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004335 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004336 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004337 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004338 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004339 mWindowManager.lockNow(null);
Jorim Jaggi241ae102016-11-02 21:57:33 -07004340 mWindowManager.dismissKeyguard(null /* callback */);
Jason Monke0697792014-08-04 16:28:09 -04004341 new LockPatternUtils(mService.mContext)
4342 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004343 }
4344 } catch (SettingNotFoundException e) {
4345 // No setting, don't lock.
4346 }
justinzhang5286d3f2014-05-12 17:06:01 -04004347 } catch (RemoteException ex) {
4348 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004349 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004350 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004351 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004352 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004353 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4354 if (mLockTaskNotify == null) {
4355 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4356 }
4357 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4358 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004359 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4360 final ActivityContainer container = (ActivityContainer) msg.obj;
4361 final IActivityContainerCallback callback = container.mCallback;
4362 if (callback != null) {
4363 try {
4364 callback.onAllActivitiesComplete(container.asBinder());
4365 } catch (RemoteException e) {
4366 }
4367 }
4368 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004369 case LAUNCH_TASK_BEHIND_COMPLETE: {
4370 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004371 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004372 if (r != null) {
4373 handleLaunchTaskBehindCompleteLocked(r);
4374 }
4375 }
4376 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004377
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004378 }
4379 }
4380 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004381
Ying Wangb081a592014-04-22 15:20:16 -07004382 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08004383 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
4384 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004385 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004386 IActivityContainerCallback mCallback = null;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004387 ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004388 ActivityRecord mParentActivity = null;
4389 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004390
Craig Mautnere3a00d72014-04-16 08:31:19 -07004391 boolean mVisible = true;
4392
Craig Mautner4a1cb222013-12-04 16:14:06 -08004393 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004394 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004395
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004396 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4397 final static int CONTAINER_STATE_NO_SURFACE = 1;
4398 final static int CONTAINER_STATE_FINISHING = 2;
4399 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4400
Wale Ogunwale1666e312016-12-16 11:27:18 -08004401 ActivityContainer(int stackId, ActivityDisplay activityDisplay, boolean onTop) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004402 synchronized (mService) {
4403 mStackId = stackId;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004404 mActivityDisplay = activityDisplay;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004405 mIdString = "ActivtyContainer{" + mStackId + "}";
Bryce Leeaf691c02017-03-20 14:20:22 -07004406
4407 createStack(stackId, onTop);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004408 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004409 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004410 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004411
Bryce Leeaf691c02017-03-20 14:20:22 -07004412 protected void createStack(int stackId, boolean onTop) {
4413 switch (stackId) {
4414 case PINNED_STACK_ID:
4415 new PinnedActivityStack(this, mRecentTasks, onTop);
4416 break;
4417 default:
4418 new ActivityStack(this, mRecentTasks, onTop);
4419 break;
4420 }
4421 }
4422
Andrii Kulian839def92016-11-02 10:58:58 -07004423 /**
4424 * Adds the stack to specified display. Also calls WindowManager to do the same from
Wale Ogunwale1666e312016-12-16 11:27:18 -08004425 * {@link ActivityStack#reparent(ActivityDisplay, boolean)}.
Andrii Kulian839def92016-11-02 10:58:58 -07004426 * @param activityDisplay The display to add the stack to.
Andrii Kulian839def92016-11-02 10:58:58 -07004427 */
Wale Ogunwale1666e312016-12-16 11:27:18 -08004428 void addToDisplayLocked(ActivityDisplay activityDisplay) {
Andrii Kulian839def92016-11-02 10:58:58 -07004429 if (DEBUG_STACK) Slog.d(TAG_STACK, "addToDisplayLocked: " + this
Wale Ogunwale1666e312016-12-16 11:27:18 -08004430 + " to display=" + activityDisplay);
Andrii Kulian839def92016-11-02 10:58:58 -07004431 if (mActivityDisplay != null) {
4432 throw new IllegalStateException("ActivityContainer is already attached, " +
4433 "displayId=" + mActivityDisplay.mDisplayId);
4434 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004435 mActivityDisplay = activityDisplay;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004436 mStack.reparent(activityDisplay, true /* onTop */);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004437 }
4438
4439 @Override
Andrii Kulian839def92016-11-02 10:58:58 -07004440 public void addToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004441 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004442 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4443 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004444 return;
4445 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08004446 addToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004447 }
4448 }
4449
4450 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004451 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004452 synchronized (mService) {
4453 if (mActivityDisplay != null) {
4454 return mActivityDisplay.mDisplayId;
4455 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004456 }
4457 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004458 }
4459
Jeff Brownca9bc702014-02-11 14:32:56 -08004460 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004461 public int getStackId() {
4462 synchronized (mService) {
4463 return mStackId;
4464 }
4465 }
4466
4467 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004468 public boolean injectEvent(InputEvent event) {
4469 final long origId = Binder.clearCallingIdentity();
4470 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004471 synchronized (mService) {
4472 if (mActivityDisplay != null) {
4473 return mInputManagerInternal.injectInputEvent(event,
4474 mActivityDisplay.mDisplayId,
4475 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4476 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004477 }
4478 return false;
4479 } finally {
4480 Binder.restoreCallingIdentity(origId);
4481 }
4482 }
4483
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004484 @Override
4485 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004486 synchronized (mService) {
4487 if (mContainerState == CONTAINER_STATE_FINISHING) {
4488 return;
4489 }
4490 mContainerState = CONTAINER_STATE_FINISHING;
4491
Craig Mautnerd163e752014-06-13 17:18:47 -07004492 long origId = Binder.clearCallingIdentity();
4493 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004494 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004495 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004496 } finally {
4497 Binder.restoreCallingIdentity(origId);
4498 }
4499 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004500 }
4501
Andrii Kulian03c403d2016-11-11 11:14:12 -08004502 /**
4503 * Remove the stack completely. Must be called only when there are no tasks left in it,
4504 * as this method does not finish running activities.
4505 */
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004506 void removeLocked() {
4507 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004508 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004509 if (mActivityDisplay != null) {
Andrii Kulian839def92016-11-02 10:58:58 -07004510 removeFromDisplayLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004511 }
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004512 mStack.remove();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004513 }
4514
Andrii Kulian839def92016-11-02 10:58:58 -07004515 /**
4516 * Remove the stack from its current {@link ActivityDisplay}, so it can be either destroyed
4517 * completely or re-parented.
4518 */
4519 private void removeFromDisplayLocked() {
4520 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeFromDisplayLocked: " + this
4521 + " current displayId=" + mActivityDisplay.mDisplayId);
4522
Wale Ogunwale1666e312016-12-16 11:27:18 -08004523 mActivityDisplay.detachStack(mStack);
Andrii Kulian839def92016-11-02 10:58:58 -07004524 mActivityDisplay = null;
4525 }
4526
4527 /**
4528 * Move the stack to specified display.
4529 * @param activityDisplay Target display to move the stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08004530 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07004531 */
Andrii Kulian250d6532017-02-08 23:30:45 -08004532 void moveToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Andrii Kulian839def92016-11-02 10:58:58 -07004533 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveToDisplayLocked: " + this + " from display="
4534 + mActivityDisplay + " to display=" + activityDisplay
4535 + " Callers=" + Debug.getCallers(2));
4536
4537 removeFromDisplayLocked();
4538
4539 mActivityDisplay = activityDisplay;
Andrii Kulian250d6532017-02-08 23:30:45 -08004540 mStack.reparent(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07004541 }
4542
Craig Mautner4a1cb222013-12-04 16:14:06 -08004543 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004544 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004545 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004546 }
4547
4548 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004549 public final int startActivityIntentSender(IIntentSender intentSender)
4550 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004551 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4552
4553 if (!(intentSender instanceof PendingIntentRecord)) {
4554 throw new IllegalArgumentException("Bad PendingIntent object");
4555 }
4556
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004557 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004558 Binder.getCallingUid(), mCurrentUser, false,
4559 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004560
4561 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4562 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4563 pendingIntent.key.requestResolvedType);
4564
4565 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4566 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4567 }
4568
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004569 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004570 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004571 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004572 throw new SecurityException(
4573 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4574 }
4575 }
4576
Craig Mautnerdf88d732014-01-27 09:21:32 -08004577 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004578 public IBinder asBinder() {
4579 return this;
4580 }
4581
Craig Mautner4504de52013-12-20 09:06:56 -08004582 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004583 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004584 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004585 }
4586
Craig Mautner4a1cb222013-12-04 16:14:06 -08004587 ActivityStackSupervisor getOuter() {
4588 return ActivityStackSupervisor.this;
4589 }
4590
Craig Mautnerd163e752014-06-13 17:18:47 -07004591 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004592 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004593 }
4594
Craig Mautner6985bad2014-04-21 15:22:06 -07004595 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004596 void setVisible(boolean visible) {
4597 if (mVisible != visible) {
4598 mVisible = visible;
4599 if (mCallback != null) {
4600 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4601 0 /* unused */, this).sendToTarget();
4602 }
4603 }
4604 }
4605
Craig Mautner6985bad2014-04-21 15:22:06 -07004606 void setDrawn() {
4607 }
4608
Craig Mautner1b4bf852014-05-26 15:06:32 -07004609 // You can always start a new task on a regular ActivityStack.
4610 boolean isEligibleForNewTasks() {
4611 return true;
4612 }
4613
Craig Mautnerd163e752014-06-13 17:18:47 -07004614 void onTaskListEmptyLocked() {
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004615 removeLocked();
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004616 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004617 }
4618
Craig Mautner34b73df2014-01-12 21:11:08 -08004619 @Override
4620 public String toString() {
4621 return mIdString + (mActivityDisplay == null ? "N" : "A");
4622 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004623 }
4624
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004625 private class VirtualActivityContainer extends ActivityContainer {
4626 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004627 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004628
4629 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08004630 super(getNextStackId(), parent.getStack().mActivityContainer.mActivityDisplay,
4631 true /* onTop */);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004632 mParentActivity = parent;
4633 mCallback = callback;
4634 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004635 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004636 }
4637
4638 @Override
4639 public void setSurface(Surface surface, int width, int height, int density) {
4640 super.setSurface(surface, width, height, density);
4641
4642 synchronized (mService) {
4643 final long origId = Binder.clearCallingIdentity();
4644 try {
4645 setSurfaceLocked(surface, width, height, density);
4646 } finally {
4647 Binder.restoreCallingIdentity(origId);
4648 }
4649 }
4650 }
4651
4652 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4653 if (mContainerState == CONTAINER_STATE_FINISHING) {
4654 return;
4655 }
4656 VirtualActivityDisplay virtualActivityDisplay =
4657 (VirtualActivityDisplay) mActivityDisplay;
4658 if (virtualActivityDisplay == null) {
4659 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004660 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004661 mActivityDisplay = virtualActivityDisplay;
4662 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwale1666e312016-12-16 11:27:18 -08004663 addToDisplayLocked(virtualActivityDisplay);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004664 }
4665
4666 if (mSurface != null) {
4667 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004668 }
4669
Craig Mautner6985bad2014-04-21 15:22:06 -07004670 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004671 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004672 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004673 } else {
4674 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004675 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004676 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Wale Ogunwale950faff2016-08-08 09:51:04 -07004677 mStack.startPausingLocked(false, true, null, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004678 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004679 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004680
Craig Mautnerd163e752014-06-13 17:18:47 -07004681 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004682
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004683 if (DEBUG_STACK) Slog.d(TAG_STACK,
4684 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004685 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004686
Craig Mautner6985bad2014-04-21 15:22:06 -07004687 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004688 boolean isAttachedLocked() {
4689 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004690 }
4691
4692 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004693 void setDrawn() {
4694 synchronized (mService) {
4695 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004696 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004697 }
4698 }
4699
Craig Mautner1b4bf852014-05-26 15:06:32 -07004700 // Never start a new task on an ActivityView if it isn't explicitly specified.
4701 @Override
4702 boolean isEligibleForNewTasks() {
4703 return false;
4704 }
4705
Craig Mautnerd163e752014-06-13 17:18:47 -07004706 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004707 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004708 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4709 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4710 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4711 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4712 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004713 }
4714 }
4715
Craig Mautner4a1cb222013-12-04 16:14:06 -08004716 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4717 * attached {@link ActivityStack}s */
Andrii Kulian1779e612016-10-12 21:58:25 -07004718 class ActivityDisplay extends ConfigurationContainer {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004719 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004720 int mDisplayId;
4721 Display mDisplay;
Craig Mautnered6649f2013-12-02 14:08:25 -08004722
Craig Mautner4a1cb222013-12-04 16:14:06 -08004723 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4724 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004725 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004726
Jose Lima4b6c6692014-08-12 17:41:12 -07004727 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004728
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004729 /** Array of all UIDs that are present on the display. */
4730 private IntArray mDisplayAccessUIDs = new IntArray();
4731
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004732 ActivityDisplay() {
4733 }
Craig Mautner4504de52013-12-20 09:06:56 -08004734
Craig Mautner1a70a162014-09-13 12:09:31 -07004735 // After instantiation, check that mDisplay is not null before using this. The alternative
4736 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004737 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004738 final Display display = mDisplayManager.getDisplay(displayId);
4739 if (display == null) {
4740 return;
4741 }
4742 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004743 }
4744
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004745 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004746 mDisplay = display;
4747 mDisplayId = display.getDisplayId();
Craig Mautnered6649f2013-12-02 14:08:25 -08004748 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004749
Winson Chung7c3ede32017-03-14 14:18:34 -07004750 void attachStack(ActivityStack stack, int position) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08004751 if (DEBUG_STACK) Slog.v(TAG_STACK, "attachStack: attaching " + stack
Winson Chung7c3ede32017-03-14 14:18:34 -07004752 + " to displayId=" + mDisplayId + " position=" + position);
4753 mStacks.add(position, stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004754 }
4755
Wale Ogunwale1666e312016-12-16 11:27:18 -08004756 void detachStack(ActivityStack stack) {
4757 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachStack: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004758 + " from displayId=" + mDisplayId);
4759 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004760 }
4761
Jose Lima4b6c6692014-08-12 17:41:12 -07004762 void setVisibleBehindActivity(ActivityRecord r) {
4763 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004764 }
4765
Jose Lima4b6c6692014-08-12 17:41:12 -07004766 boolean hasVisibleBehindActivity() {
4767 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004768 }
4769
Craig Mautner34b73df2014-01-12 21:11:08 -08004770 @Override
4771 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004772 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4773 }
Andrii Kulian1779e612016-10-12 21:58:25 -07004774
4775 @Override
4776 protected int getChildCount() {
4777 return mStacks.size();
4778 }
4779
4780 @Override
4781 protected ConfigurationContainer getChildAt(int index) {
4782 return mStacks.get(index);
4783 }
4784
4785 @Override
4786 protected ConfigurationContainer getParent() {
4787 return ActivityStackSupervisor.this;
4788 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004789
4790 boolean isPrivate() {
4791 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
4792 }
4793
4794 boolean isUidPresent(int uid) {
4795 for (ActivityStack stack : mStacks) {
4796 if (stack.isUidPresent(uid)) {
4797 return true;
4798 }
4799 }
4800 return false;
4801 }
4802
4803 /** Update and get all UIDs that are present on the display and have access to it. */
4804 private IntArray getPresentUIDs() {
4805 mDisplayAccessUIDs.clear();
4806 for (ActivityStack stack : mStacks) {
4807 stack.getPresentUIDs(mDisplayAccessUIDs);
4808 }
4809 return mDisplayAccessUIDs;
4810 }
Andrii Kulian250d6532017-02-08 23:30:45 -08004811
4812 boolean shouldDestroyContentOnRemove() {
4813 return mDisplay.getRemoveMode() == REMOVE_MODE_DESTROY_CONTENT;
4814 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004815 }
4816
4817 class VirtualActivityDisplay extends ActivityDisplay {
4818 VirtualDisplay mVirtualDisplay;
4819
Craig Mautner6985bad2014-04-21 15:22:06 -07004820 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004821 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004822 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4823 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4824 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4825 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004826
4827 init(mVirtualDisplay.getDisplay());
4828
Bryce Leeaf3a4002017-03-20 10:37:12 -07004829 mWindowManager.onDisplayAdded(mDisplayId);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004830 }
4831
4832 void setSurface(Surface surface) {
4833 if (mVirtualDisplay != null) {
4834 mVirtualDisplay.setSurface(surface);
4835 }
4836 }
4837
4838 @Override
Wale Ogunwale1666e312016-12-16 11:27:18 -08004839 void detachStack(ActivityStack stack) {
4840 super.detachStack(stack);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004841 if (mVirtualDisplay != null) {
4842 mVirtualDisplay.release();
4843 mVirtualDisplay = null;
4844 }
4845 }
4846
4847 @Override
4848 public String toString() {
4849 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004850 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004851 }
Jose Lima58e66d62014-05-27 20:00:27 -07004852
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004853 ActivityStack findStackBehind(ActivityStack stack) {
4854 // TODO(multi-display): We are only looking for stacks on the default display.
Jorim Jaggife762342016-10-13 14:33:27 +02004855 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004856 if (display == null) {
4857 return null;
4858 }
4859 final ArrayList<ActivityStack> stacks = display.mStacks;
4860 for (int i = stacks.size() - 1; i >= 0; i--) {
4861 if (stacks.get(i) == stack && i > 0) {
4862 return stacks.get(i - 1);
4863 }
4864 }
4865 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4866 + " in=" + stacks);
4867 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004868
Jorim Jaggic69bd222016-03-15 14:38:37 +01004869 /**
4870 * Puts a task into resizing mode during the next app transition.
4871 *
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004872 * @param task The task to put into resizing mode
Jorim Jaggic69bd222016-03-15 14:38:37 +01004873 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004874 private void setResizingDuringAnimation(TaskRecord task) {
4875 mResizingTasksDuringAnimation.add(task.taskId);
4876 task.setTaskDockedResizing(true);
Jorim Jaggic69bd222016-03-15 14:38:37 +01004877 }
4878
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004879 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
4880 final TaskRecord task;
4881 final int callingUid;
4882 final String callingPackage;
4883 final Intent intent;
4884 final int userId;
4885 final ActivityOptions activityOptions = (bOptions != null)
4886 ? new ActivityOptions(bOptions) : null;
4887 final int launchStackId = (activityOptions != null)
4888 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004889 if (StackId.isHomeOrRecentsStack(launchStackId)) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004890 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004891 + taskId + " can't be launch in the home/recents stack.");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004892 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004893
4894 if (launchStackId == DOCKED_STACK_ID) {
4895 mWindowManager.setDockedStackCreateState(
4896 activityOptions.getDockCreateMode(), null /* initialBounds */);
4897
4898 // Defer updating the stack in which recents is until the app transition is done, to
4899 // not run into issues where we still need to draw the task in recents but the
4900 // docked stack is already created.
Matthew Ng299a01f2016-12-05 11:39:23 -08004901 deferUpdateBounds(HOME_STACK_ID);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004902 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
4903 }
4904
Winson Chung7900bee2017-03-10 08:59:25 -08004905 task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
4906 launchStackId);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004907 if (task == null) {
Matthew Ng299a01f2016-12-05 11:39:23 -08004908 continueUpdateBounds(HOME_STACK_ID);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004909 mWindowManager.executeAppTransition();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004910 throw new IllegalArgumentException(
4911 "startActivityFromRecentsInner: Task " + taskId + " not found.");
4912 }
4913
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004914 // Since we don't have an actual source record here, we assume that the currently focused
4915 // activity was the source.
4916 final ActivityStack focusedStack = getFocusedStack();
4917 final ActivityRecord sourceRecord =
4918 focusedStack != null ? focusedStack.topActivity() : null;
4919
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004920 if (launchStackId != INVALID_STACK_ID) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004921 if (task.getStackId() != launchStackId) {
Winson Chung74666102017-02-22 17:49:24 -08004922 task.reparent(launchStackId, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, ANIMATE,
4923 DEFER_RESUME, "startActivityFromRecents");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004924 }
4925 }
4926
4927 // If the user must confirm credentials (e.g. when first launching a work app and the
4928 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4929 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4930 && task.getRootActivity() != null) {
Wei Wang65c7a152016-06-02 18:51:22 -07004931 mService.mActivityStarter.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */);
Jorim Jaggi09c49542016-04-09 01:39:40 -07004932 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004933 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions);
Jorim Jaggi1e630c02016-05-16 12:13:13 -07004934 mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
4935 task.getTopActivity());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004936
4937 // If we are launching the task in the docked stack, put it into resizing mode so
4938 // the window renders full-screen with the background filling the void. Also only
4939 // call this at the end to make sure that tasks exists on the window manager side.
4940 if (launchStackId == DOCKED_STACK_ID) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004941 setResizingDuringAnimation(task);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004942 }
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004943
Bryce Leeaa5e8c32017-03-01 16:01:06 -08004944 mService.mActivityStarter.postStartActivityProcessing(task.getTopActivity(),
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004945 ActivityManager.START_TASK_TO_FRONT,
Bryce Leeaf691c02017-03-20 14:20:22 -07004946 sourceRecord != null ? sourceRecord.getTask().getStackId() : INVALID_STACK_ID,
Andrii Kulian02b7a832016-10-06 23:11:56 -07004947 sourceRecord, task.getStack());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004948 return ActivityManager.START_TASK_TO_FRONT;
4949 }
4950 callingUid = task.mCallingUid;
4951 callingPackage = task.mCallingPackage;
4952 intent = task.intent;
4953 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4954 userId = task.userId;
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004955 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
4956 null, null, 0, 0, bOptions, userId, null, task);
4957 if (launchStackId == DOCKED_STACK_ID) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004958 setResizingDuringAnimation(task);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004959 }
4960 return result;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004961 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004962
4963 /**
4964 * @return a list of activities which are the top ones in each visible stack. The first
4965 * entry will be the focused activity.
4966 */
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004967 List<IBinder> getTopVisibleActivities() {
4968 final ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4969 // Traverse all displays.
Robert Carr22374c02017-02-21 13:01:47 -08004970 for (int i = mActivityDisplays.size() - 1; i >= 0; i--) {
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004971 final ActivityDisplay display = mActivityDisplays.valueAt(i);
4972 // Traverse all stacks on a display.
4973 for (int j = display.mStacks.size() - 1; j >= 0; j--) {
4974 final ActivityStack stack = display.mStacks.get(j);
4975 // Get top activity from a visible stack and add it to the list.
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07004976 if (stack.shouldBeVisible(null /* starting */)
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004977 == ActivityStack.STACK_VISIBLE) {
4978 final ActivityRecord top = stack.topActivity();
4979 if (top != null) {
4980 if (stack == mFocusedStack) {
4981 topActivityTokens.add(0, top.appToken);
4982 } else {
4983 topActivityTokens.add(top.appToken);
4984 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004985 }
4986 }
4987 }
4988 }
4989 return topActivityTokens;
4990 }
Craig Mautner27084302013-03-25 08:05:25 -07004991}