blob: 18b3e62f33c57774c1fef3bdc868eab8f72d0e9f [file] [log] [blame]
Craig Mautner27084302013-03-25 08:05:25 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Craig Mautner6170f732013-04-02 13:05:23 -070019import static android.Manifest.permission.START_ANY_ACTIVITY;
Wale Ogunwale868a5e12015-08-02 16:19:20 -070020import static android.app.ActivityManager.*;
Wale Ogunwale3797c222015-10-27 14:21:58 -070021import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
22import static android.app.ActivityManager.StackId.FIRST_DYNAMIC_STACK_ID;
23import static android.app.ActivityManager.StackId.FIRST_STATIC_STACK_ID;
24import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
25import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
26import static android.app.ActivityManager.StackId.HOME_STACK_ID;
27import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
28import static android.app.ActivityManager.StackId.LAST_STATIC_STACK_ID;
29import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Wale Ogunwaled80c2632015-03-13 10:26:26 -070030import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK;
31import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
Craig Mautner29219d92013-04-16 20:19:12 -070032import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
33import static android.content.Intent.FLAG_ACTIVITY_TASK_ON_HOME;
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -070034import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Craig Mautner6170f732013-04-02 13:05:23 -070035import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Wale Ogunwalecad05a02015-09-25 10:41:44 -070036import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
Chong Zhangb15758a2015-11-17 12:12:03 -080037import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Wale Ogunwaled57969f2014-11-15 19:37:29 -080038import static com.android.server.am.ActivityManagerDebugConfig.*;
Craig Mautner05d29032013-05-03 13:40:13 -070039import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Craig Mautner84984fa2014-06-19 11:19:20 -070040import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
41import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
42import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -070043import static com.android.server.am.ActivityStack.ActivityState.*;
Craig Mautner15df08a2015-04-01 12:17:18 -070044import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
45import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
Benjamin Franz469dd582015-06-09 14:24:36 +010046import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
Craig Mautner15df08a2015-04-01 12:17:18 -070047import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
48import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Craig Mautner8d341ef2013-03-26 09:03:27 -070049
Svetoslav7008b512015-06-24 18:47:07 -070050import android.Manifest;
Craig Mautner2420ead2013-04-01 17:13:20 -070051import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070052import android.app.ActivityManager;
Wale Ogunwale3797c222015-10-27 14:21:58 -070053import android.app.ActivityManager.StackId;
Craig Mautnered6649f2013-12-02 14:08:25 -080054import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070055import android.app.ActivityOptions;
56import android.app.AppGlobals;
Svetoslav7008b512015-06-24 18:47:07 -070057import android.app.AppOpsManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -080058import android.app.IActivityContainer;
59import android.app.IActivityContainerCallback;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070060import android.app.IActivityManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070061import android.app.IApplicationThread;
Clara Bayarrif7fea162015-10-22 16:09:52 +010062import android.app.KeyguardManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070063import android.app.PendingIntent;
Jeff Hao1b012d32014-08-20 10:35:34 -070064import android.app.ProfilerInfo;
Craig Mautner20e72272013-04-01 13:45:53 -070065import android.app.ActivityManager.RunningTaskInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070066import android.app.IActivityManager.WaitResult;
Craig Mautner2420ead2013-04-01 17:13:20 -070067import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -040068import android.app.StatusBarManager;
Jason Monk35c62a42014-06-17 10:24:47 -040069import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070070import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -070071import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -070072import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070073import android.content.Intent;
Craig Mautner23ac33b2013-04-01 16:26:35 -070074import android.content.IntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070075import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070076import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -070077import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070078import android.content.pm.PackageManager;
Svet Ganov9c165d72015-12-01 19:52:26 -080079import android.content.pm.PackageManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -070080import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +010081import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070082import android.content.res.Configuration;
Wale Ogunwale60454db2015-01-23 16:05:07 -080083import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -080084import android.hardware.display.DisplayManager;
85import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080086import android.hardware.display.DisplayManagerGlobal;
87import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080088import android.hardware.input.InputManager;
89import android.hardware.input.InputManagerInternal;
Craig Mautnerb9168362015-02-26 20:40:19 -080090import android.net.Uri;
Craig Mautner23ac33b2013-04-01 16:26:35 -070091import android.os.Binder;
Svet Ganov9c165d72015-12-01 19:52:26 -080092import android.os.Build;
Craig Mautner8d341ef2013-03-26 09:03:27 -070093import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070094import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070095import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070096import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070097import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070098import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070099import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700100import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700101import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700102import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -0400103import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700104import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -0700105import android.os.Trace;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -0700106import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -0700107import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100108import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700109import android.os.WorkSource;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100110import android.os.storage.StorageManager;
Svetoslav7008b512015-06-24 18:47:07 -0700111import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -0400112import android.provider.Settings;
113import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700114import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -0700115import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700116import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -0700117import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700118import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800119import android.util.SparseArray;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700120
Craig Mautner4a1cb222013-12-04 16:14:06 -0800121import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800122import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800123import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -0800124import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800125import android.view.Surface;
Chong Zhangb15758a2015-11-17 12:12:03 -0800126import android.widget.Toast;
127
Craig Mautner23ac33b2013-04-01 16:26:35 -0700128import com.android.internal.app.HeavyWeightSwitcherActivity;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700129import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800130import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700131import com.android.internal.os.TransferPipe;
Chong Zhangb15758a2015-11-17 12:12:03 -0800132import com.android.internal.R;
justinzhang5286d3f2014-05-12 17:06:01 -0400133import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -0700134import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -0400135import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800136import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700137import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700138import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700139
Craig Mautner8d341ef2013-03-26 09:03:27 -0700140import java.io.FileDescriptor;
141import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700142import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700143import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700144import java.util.Arrays;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700145import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700146import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700147
Craig Mautner4a1cb222013-12-04 16:14:06 -0800148public final class ActivityStackSupervisor implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800149 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700150 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700151 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700152 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700153 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700154 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700155 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700156 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700157 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
158 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700159 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700160 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700161 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
162 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700163 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700164 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800165
Craig Mautnerf3333272013-04-22 10:55:53 -0700166 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700167 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700168
Craig Mautner0eea92c2013-05-16 13:35:39 -0700169 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700170 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700171
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700172 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700173 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700174
Craig Mautner05d29032013-05-03 13:40:13 -0700175 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
176 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
177 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700178 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700179 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800180 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
181 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
182 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700183 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400184 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
185 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700186 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700187 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700188 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800189
Wale Ogunwale040b4702015-08-06 18:10:50 -0700190 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700191
Jason Monk62515be2014-05-21 16:06:19 -0400192 private static final String LOCK_TASK_TAG = "Lock-to-App";
193
Wale Ogunwale040b4702015-08-06 18:10:50 -0700194 // Used to indicate if an object (e.g. stack) that we are trying to get
195 // should be created if it doesn't exist already.
196 private static final boolean CREATE_IF_NEEDED = true;
197
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700198 // Used to indicate that windows of activities should be preserved during the resize.
199 static final boolean PRESERVE_WINDOWS = true;
200
Wale Ogunwale040b4702015-08-06 18:10:50 -0700201 // Used to indicate if an object (e.g. task) should be moved/created
202 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700203 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700204
205 // Used to indicate that an objects (e.g. task) removal from its container
206 // (e.g. stack) is due to it moving to another container.
207 static final boolean MOVING = true;
208
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700209 // Force the focus to change to the stack we are moving a task to..
210 static final boolean FORCE_FOCUS = true;
211
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700212 // Restore task from the saved recents if it can't be found in any live stack.
213 static final boolean RESTORE_FROM_RECENTS = true;
214
Svetoslav7008b512015-06-24 18:47:07 -0700215 // Activity actions an app cannot start if it uses a permission which is not granted.
216 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
217 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700218
Svetoslav7008b512015-06-24 18:47:07 -0700219 static {
220 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
221 Manifest.permission.CAMERA);
222 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
223 Manifest.permission.CAMERA);
224 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
225 Manifest.permission.CALL_PHONE);
226 }
227
Svet Ganov99b60432015-06-27 13:15:22 -0700228 /** Action restriction: launching the activity is not restricted. */
229 private static final int ACTIVITY_RESTRICTION_NONE = 0;
230 /** Action restriction: launching the activity is restricted by a permission. */
231 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
232 /** Action restriction: launching the activity is restricted by an app op. */
233 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700234
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700235 // The height/width divide used when fitting a task within a bounds with method
236 // {@link #fitWithinBounds}.
237 // We always want the task to to be visible in the bounds without affecting its size when
238 // fitting. To make sure this is the case, we don't adjust the task left or top side pass
239 // the input bounds right or bottom side minus the width or height divided by this value.
240 private static final int FIT_WITHIN_BOUNDS_DIVIDER = 3;
241
justinzhang5286d3f2014-05-12 17:06:01 -0400242 /** Status Bar Service **/
243 private IBinder mToken = new Binder();
244 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400245 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400246
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700247 // For debugging to make sure the caller when acquiring/releasing our
248 // wake lock is the system process.
249 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700250
Craig Mautner27084302013-03-25 08:05:25 -0700251 final ActivityManagerService mService;
252
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800253 private final RecentTasks mRecentTasks;
254
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700255 final ActivityStackSupervisorHandler mHandler;
256
257 /** Short cut */
258 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800259 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700260
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700261 /** Counter for next free stack ID to use for dynamic activity stacks. */
262 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700263
264 /** Task identifier that activities are currently being started in. Incremented each time a
265 * new task is created. */
266 private int mCurTaskId = 0;
267
Craig Mautner2420ead2013-04-01 17:13:20 -0700268 /** The current user */
269 private int mCurrentUser;
270
Craig Mautnere0a38842013-12-16 16:14:02 -0800271 /** The stack containing the launcher app. Assumed to always be attached to
272 * Display.DEFAULT_DISPLAY. */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700273 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700274
Craig Mautnere0a38842013-12-16 16:14:02 -0800275 /** The stack currently receiving input or launching the next activity. */
Craig Mautner29219d92013-04-16 20:19:12 -0700276 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700277
Craig Mautner4a1cb222013-12-04 16:14:06 -0800278 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
279 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800280 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800281 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700282
283 /** List of activities that are waiting for a new activity to become visible before completing
284 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800285 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700286
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700287 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800288 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700289
290 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800291 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700292
Craig Mautnerde4ef022013-04-07 19:01:33 -0700293 /** List of activities that are ready to be stopped, but waiting for the next activity to
294 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800295 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700296
Craig Mautnerf3333272013-04-22 10:55:53 -0700297 /** List of activities that are ready to be finished, but waiting for the previous activity to
298 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800299 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700300
Craig Mautner0eea92c2013-05-16 13:35:39 -0700301 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800302 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700303
Craig Mautnerf3333272013-04-22 10:55:53 -0700304 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700305 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700306
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700307 /** Used to queue up any background users being started */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700308 final ArrayList<UserState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700309
Craig Mautnerde4ef022013-04-07 19:01:33 -0700310 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
311 * is being brought in front of us. */
312 boolean mUserLeaving = false;
313
Craig Mautner0eea92c2013-05-16 13:35:39 -0700314 /** Set when we have taken too long waiting to go to sleep. */
315 boolean mSleepTimeout = false;
316
317 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700318 * We don't want to allow the device to go to sleep while in the process
319 * of launching an activity. This is primarily to allow alarm intent
320 * receivers to launch an activity and get that to run before the device
321 * goes back to sleep.
322 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700323 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700324
325 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700326 * Set when the system is going to sleep, until we have
327 * successfully paused the current activity and released our wake lock.
328 * At that point the system is allowed to actually sleep.
329 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700330 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700331
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700332 /** Stack id of the front stack when user switched, indexed by userId. */
333 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700334
Craig Mautner4504de52013-12-20 09:06:56 -0800335 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800336 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700337 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800338
339 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700340 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800341
Jeff Brownca9bc702014-02-11 14:32:56 -0800342 InputManagerInternal mInputManagerInternal;
343
Craig Mautner15df08a2015-04-01 12:17:18 -0700344 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
345 * may be finished until there is only one entry left. If this is empty the system is not
346 * in lockTask mode. */
347 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000348 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700349 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
350 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000351 */
352 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400353 /**
354 * Notifies the user when entering/exiting lock-task.
355 */
356 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800357
Craig Mautner15df08a2015-04-01 12:17:18 -0700358 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches = new ArrayList<>();
Craig Mautneree36c772014-07-16 14:56:05 -0700359
Craig Mautner42d04db2014-11-06 12:13:23 -0800360 /** Used to keep resumeTopActivityLocked() from being entered recursively */
361 boolean inResumeTopActivity;
362
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700363 // temp. rects used during resize calculation so we don't need to create a new object each time.
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700364 private final Rect tempRect = new Rect();
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700365 private final Rect tempRect2 = new Rect();
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700366
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700367 private final SparseArray<Configuration> mTmpConfigs = new SparseArray<>();
368 private final SparseArray<Rect> mTmpBounds = new SparseArray<>();
369
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700370 // The default minimal size that will be used if the activity doesn't specify its minimal size.
371 // It will be calculated when the default display gets added.
372 private int mDefaultMinimalSizeOfResizeableTask = -1;
373
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700374 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
375 private boolean mTaskLayersChanged = true;
376
Craig Mautneree36c772014-07-16 14:56:05 -0700377 /**
378 * Description of a request to start a new activity, which has been held
379 * due to app switches being disabled.
380 */
381 static class PendingActivityLaunch {
382 final ActivityRecord r;
383 final ActivityRecord sourceRecord;
384 final int startFlags;
385 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700386 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700387
388 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700389 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700390 r = _r;
391 sourceRecord = _sourceRecord;
392 startFlags = _startFlags;
393 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700394 callerApp = _callerApp;
395 }
396
397 void sendErrorResult(String message) {
398 try {
399 if (callerApp.thread != null) {
400 callerApp.thread.scheduleCrash(message);
401 }
402 } catch (RemoteException e) {
403 Slog.e(TAG, "Exception scheduling crash of failed "
404 + "activity launcher sourceRecord=" + sourceRecord, e);
405 }
Craig Mautneree36c772014-07-16 14:56:05 -0700406 }
407 }
408
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800409 public ActivityStackSupervisor(ActivityManagerService service, RecentTasks recentTasks) {
Craig Mautner27084302013-03-25 08:05:25 -0700410 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800411 mRecentTasks = recentTasks;
Jeff Brown2c43c332014-06-12 22:38:59 -0700412 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
413 }
414
415 /**
416 * At the time when the constructor runs, the power manager has not yet been
417 * initialized. So we initialize our wakelocks afterwards.
418 */
419 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800420 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700421 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700422 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700423 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700424 }
425
justinzhang5286d3f2014-05-12 17:06:01 -0400426 // This function returns a IStatusBarService. The value is from ServiceManager.
427 // getService and is cached.
428 private IStatusBarService getStatusBarService() {
429 synchronized (mService) {
430 if (mStatusBarService == null) {
431 mStatusBarService = IStatusBarService.Stub.asInterface(
432 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
433 if (mStatusBarService == null) {
434 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
435 }
436 }
437 return mStatusBarService;
438 }
439 }
440
Jason Monk35c62a42014-06-17 10:24:47 -0400441 private IDevicePolicyManager getDevicePolicyManager() {
442 synchronized (mService) {
443 if (mDevicePolicyManager == null) {
444 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
445 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
446 if (mDevicePolicyManager == null) {
447 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
448 }
449 }
450 return mDevicePolicyManager;
451 }
452 }
453
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700454 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800455 synchronized (mService) {
456 mWindowManager = wm;
457
458 mDisplayManager =
459 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
460 mDisplayManager.registerDisplayListener(this, null);
461
462 Display[] displays = mDisplayManager.getDisplays();
463 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
464 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800465 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700466 if (activityDisplay.mDisplay == null) {
467 throw new IllegalStateException("Default Display does not exist");
468 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800469 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700470 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800471 }
472
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700473 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800474 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800475
476 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800477 }
Craig Mautner27084302013-03-25 08:05:25 -0700478 }
479
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200480 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700481 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200482 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700483 }
484
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700485 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800486 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700487 }
488
Craig Mautnerde4ef022013-04-07 19:01:33 -0700489 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800490 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700491 }
492
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700493 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700494 if (stack == null) {
495 return false;
496 }
497
Craig Mautnerdf88d732014-01-27 09:21:32 -0800498 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
499 if (parent != null) {
500 stack = parent.task.stack;
501 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800502 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700503 }
504
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700505 /** The top most stack. */
506 boolean isFrontStack(ActivityStack stack) {
507 if (stack == null) {
508 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800509 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700510
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700511 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
512 if (parent != null) {
513 stack = parent.task.stack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800514 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700515 return stack == mHomeStack.mStacks.get((mHomeStack.mStacks.size() - 1));
516 }
517
518 void setFocusStack(String reason, ActivityStack focusedStack) {
519 mLastFocusedStack = mFocusedStack;
520 mFocusedStack = focusedStack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800521
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700522 EventLogTags.writeAmFocusedStack(
523 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
524 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
Craig Mautnerde313752015-01-22 14:28:03 -0800525
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800526 if (mService.mBooting || !mService.mBooted) {
527 final ActivityRecord r = topRunningActivityLocked();
528 if (r != null && r.idle) {
529 checkFinishBootingLocked();
530 }
531 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700532 }
533
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700534 void moveHomeStackToFront(String reason) {
535 mHomeStack.moveToFront(reason);
536 }
537
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700538 /** Returns true if the focus activity was adjusted to the home stack top activity. */
539 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700540 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
541 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700542 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700543 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700544
Craig Mautner84984fa2014-06-19 11:19:20 -0700545 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700546
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700547 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700548 if (top == null) {
549 return false;
550 }
551 mService.setFocusedActivityLocked(top, reason);
552 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700553 }
554
Craig Mautner299f9602015-01-26 09:47:33 -0800555 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700556 if (!mService.mBooting && !mService.mBooted) {
557 // Not ready yet!
558 return false;
559 }
560
Craig Mautner84984fa2014-06-19 11:19:20 -0700561 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
562 mWindowManager.showRecentApps();
563 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700564 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700565
Craig Mautner84984fa2014-06-19 11:19:20 -0700566 if (prev != null) {
567 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
568 }
569
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700570 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
571 ActivityRecord r = getHomeActivity();
Mark Lua56ea122015-10-08 13:31:01 +0800572 // Only resume home activity if isn't finishing.
573 if (r != null && !r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -0800574 mService.setFocusedActivityLocked(r, reason);
Craig Mautner05d29032013-05-03 13:40:13 -0700575 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700576 }
Craig Mautner299f9602015-01-26 09:47:33 -0800577 return mService.startHomeActivityLocked(mCurrentUser, reason);
Craig Mautner69ada552013-04-18 13:51:51 -0700578 }
579
Craig Mautner8d341ef2013-03-26 09:03:27 -0700580 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700581 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700582 }
583
584 /**
585 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
586 * @param id Id of the task we would like returned.
587 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
588 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700589 * @param stackId The stack to restore the task to (default launch stack will be used if
590 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700591 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700592 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800593 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800594 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800595 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800596 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
597 ActivityStack stack = stacks.get(stackNdx);
598 TaskRecord task = stack.taskForIdLocked(id);
599 if (task != null) {
600 return task;
601 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700602 }
603 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800604
605 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700606 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800607 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800608 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700609 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800610 return null;
611 }
612
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700613 if (!restoreFromRecents) {
614 return task;
615 }
616
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700617 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700618 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
619 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800620 return null;
621 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700622 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800623 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700624 }
625
Craig Mautner6170f732013-04-02 13:05:23 -0700626 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800627 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800628 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800629 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800630 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
631 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
632 if (r != null) {
633 return r;
634 }
Craig Mautner6170f732013-04-02 13:05:23 -0700635 }
636 }
637 return null;
638 }
639
Craig Mautneref73ee12014-04-23 11:45:37 -0700640 void setNextTaskId(int taskId) {
641 if (taskId > mCurTaskId) {
642 mCurTaskId = taskId;
643 }
644 }
645
Craig Mautner8d341ef2013-03-26 09:03:27 -0700646 int getNextTaskId() {
647 do {
648 mCurTaskId++;
649 if (mCurTaskId <= 0) {
650 mCurTaskId = 1;
651 }
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700652 } while (anyTaskForIdLocked(mCurTaskId, !RESTORE_FROM_RECENTS, INVALID_STACK_ID) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700653 return mCurTaskId;
654 }
655
Craig Mautnerde4ef022013-04-07 19:01:33 -0700656 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800657 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700658 if (stack == null) {
659 return null;
660 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700661 ActivityRecord resumedActivity = stack.mResumedActivity;
662 if (resumedActivity == null || resumedActivity.app == null) {
663 resumedActivity = stack.mPausingActivity;
664 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700665 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700666 }
667 }
668 return resumedActivity;
669 }
670
Dianne Hackbornff072722014-09-24 10:56:28 -0700671 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700672 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800673 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800674 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
675 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800676 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
677 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700678 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800679 continue;
680 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700681 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800682 if (hr != null) {
683 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
684 && processName.equals(hr.processName)) {
685 try {
George Mount2c92c972014-03-20 09:38:23 -0700686 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800687 didSomething = true;
688 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700689 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800690 Slog.w(TAG, "Exception in new application when starting activity "
691 + hr.intent.getComponent().flattenToShortString(), e);
692 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700693 }
Craig Mautner20e72272013-04-01 13:45:53 -0700694 }
Craig Mautner20e72272013-04-01 13:45:53 -0700695 }
696 }
697 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700698 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700699 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700700 }
Craig Mautner20e72272013-04-01 13:45:53 -0700701 return didSomething;
702 }
703
704 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800705 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
706 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800707 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
708 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700709 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800710 continue;
711 }
712 final ActivityRecord resumedActivity = stack.mResumedActivity;
713 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700714 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800715 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800716 return false;
717 }
Craig Mautner20e72272013-04-01 13:45:53 -0700718 }
719 }
720 return true;
721 }
722
Craig Mautnerde4ef022013-04-07 19:01:33 -0700723 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800724 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
725 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800726 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
727 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700728 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800729 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700730 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800731 return false;
732 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700733 }
734 }
735 }
736 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700737 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800738 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
739 mLastFocusedStack + " to=" + mFocusedStack);
740 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700741 return true;
742 }
743
744 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800745 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800746 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
747 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800748 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
749 final ActivityStack stack = stacks.get(stackNdx);
750 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800751 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700752 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800753 return false;
754 }
755 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800756 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700757 }
758 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800759 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700760 }
761
Craig Mautner2acc3892013-09-23 10:28:14 -0700762 /**
763 * Pause all activities in either all of the stacks or just the back stacks.
764 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700765 * @return true if any activity was paused as a result of this call.
766 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700767 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700768 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800769 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
770 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800771 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
772 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700773 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700774 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800775 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700776 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
777 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800778 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700779 }
780 }
781 return someActivityPaused;
782 }
783
Craig Mautnerde4ef022013-04-07 19:01:33 -0700784 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700785 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800786 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
787 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800788 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
789 final ActivityStack stack = stacks.get(stackNdx);
790 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700791 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800792 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700793 Slog.d(TAG_STATES,
794 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800795 pausing = false;
796 } else {
797 return false;
798 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700799 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700800 }
801 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700802 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700803 }
804
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700805 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
806 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500807 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800808 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
809 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
810 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
811 final ActivityStack stack = stacks.get(stackNdx);
812 if (stack.mResumedActivity != null &&
813 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700814 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800815 }
816 }
817 }
818 }
819
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700820 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700821 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700822 }
823
824 void sendWaitingVisibleReportLocked(ActivityRecord r) {
825 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700826 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700827 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700828 if (w.who == null) {
829 changed = true;
830 w.timeout = false;
831 if (r != null) {
832 w.who = new ComponentName(r.info.packageName, r.info.name);
833 }
834 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
835 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700836 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700837 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700838 if (changed) {
839 mService.notifyAll();
840 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700841 }
842
843 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
844 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700845 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700846 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700847 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700848 if (w.who == null) {
849 changed = true;
850 w.timeout = timeout;
851 if (r != null) {
852 w.who = new ComponentName(r.info.packageName, r.info.name);
853 }
854 w.thisTime = thisTime;
855 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700856 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700857 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700858 if (changed) {
859 mService.notifyAll();
860 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700861 }
862
Craig Mautner29219d92013-04-16 20:19:12 -0700863 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800864 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700865 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -0700866 if (r != null) {
867 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700868 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700869
Craig Mautner4a1cb222013-12-04 16:14:06 -0800870 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800871 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800872 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
873 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700874 if (stack != focusedStack && isFrontStack(stack)) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700875 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -0700876 if (r != null) {
877 return r;
878 }
879 }
880 }
881 return null;
882 }
883
Dianne Hackborn09233282014-04-30 11:33:59 -0700884 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700885 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800886 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
887 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800888 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800889 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800890 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800891 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
892 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700893 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800894 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700895 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700896 }
897 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700898
899 // The lists are already sorted from most recent to oldest. Just pull the most recent off
900 // each list and add it to list. Stop when all lists are empty or maxNum reached.
901 while (maxNum > 0) {
902 long mostRecentActiveTime = Long.MIN_VALUE;
903 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800904 final int numTaskLists = runningTaskLists.size();
905 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
906 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700907 if (!stackTaskList.isEmpty()) {
908 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
909 if (lastActiveTime > mostRecentActiveTime) {
910 mostRecentActiveTime = lastActiveTime;
911 selectedStackList = stackTaskList;
912 }
913 }
914 }
915 if (selectedStackList != null) {
916 list.add(selectedStackList.remove(0));
917 --maxNum;
918 } else {
919 break;
920 }
921 }
Craig Mautner20e72272013-04-01 13:45:53 -0700922 }
923
Craig Mautner23ac33b2013-04-01 16:26:35 -0700924 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700925 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700926 // Collect information about the target of the Intent.
927 ActivityInfo aInfo;
928 try {
929 ResolveInfo rInfo =
930 AppGlobals.getPackageManager().resolveIntent(
931 intent, resolvedType,
932 PackageManager.MATCH_DEFAULT_ONLY
933 | ActivityManagerService.STOCK_PM_FLAGS, userId);
934 aInfo = rInfo != null ? rInfo.activityInfo : null;
935 } catch (RemoteException e) {
936 aInfo = null;
937 }
938
939 if (aInfo != null) {
940 // Store the found target back into the intent, because now that
941 // we have it we never want to do this again. For example, if the
942 // user navigates back to this point in the history, we should
943 // always restart the exact same activity.
944 intent.setComponent(new ComponentName(
945 aInfo.applicationInfo.packageName, aInfo.name));
946
947 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -0700948 if (!aInfo.processName.equals("system")) {
949 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700950 mService.setDebugApp(aInfo.processName, true, false);
951 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700952
Man Caocfa78b22015-06-11 20:14:34 -0700953 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
954 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
955 }
956
957 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700958 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700959 }
960 }
961 }
962 return aInfo;
963 }
964
Craig Mautner299f9602015-01-26 09:47:33 -0800965 void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
966 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700967 startActivityLocked(null /* caller */, intent, null /* resolvedType */, aInfo,
968 null /* voiceSession */, null /* voiceInteractor */, null /* resultTo */,
969 null /* resultWho */, 0 /* requestCode */, 0 /* callingPid */, 0 /* callingUid */,
970 null /* callingPackage */, 0 /* realCallingPid */, 0 /* realCallingUid */,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700971 0 /* startFlags */, null /* options */, false /* ignoreTargetSecurity */,
972 false /* componentSpecified */,
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700973 null /* outActivity */, null /* container */, null /* inTask */);
974 if (inResumeTopActivity) {
975 // If we are in resume section already, home activity will be initialized, but not
976 // resumed (to avoid recursive resume) and will stay that way until something pokes it
977 // again. We need to schedule another resume.
978 scheduleResumeTopActivities();
979 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700980 }
981
Craig Mautner23ac33b2013-04-01 16:26:35 -0700982 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700983 String callingPackage, Intent intent, String resolvedType,
984 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700985 IBinder resultTo, String resultWho, int requestCode, int startFlags,
986 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Chong Zhang280d3322015-11-03 17:27:26 -0800987 Bundle bOptions, boolean ignoreTargetSecurity, int userId,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700988 IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700989 // Refuse possible leaked file descriptors
990 if (intent != null && intent.hasFileDescriptors()) {
991 throw new IllegalArgumentException("File descriptors passed in Intent");
992 }
993 boolean componentSpecified = intent.getComponent() != null;
994
995 // Don't modify the client's object!
996 intent = new Intent(intent);
997
998 // Collect information about the target of the Intent.
Craig Mautner15df08a2015-04-01 12:17:18 -0700999 ActivityInfo aInfo =
1000 resolveActivity(intent, resolvedType, startFlags, profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001001
Chong Zhang280d3322015-11-03 17:27:26 -08001002 ActivityOptions options = ActivityOptions.fromBundle(bOptions);
Craig Mautnere0a38842013-12-16 16:14:02 -08001003 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001004 synchronized (mService) {
Craig Mautnerb9168362015-02-26 20:40:19 -08001005 if (container != null && container.mParentActivity != null &&
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001006 container.mParentActivity.state != RESUMED) {
Craig Mautnerb9168362015-02-26 20:40:19 -08001007 // Cannot start a child activity if the parent is not resumed.
1008 return ActivityManager.START_CANCELED;
1009 }
Dianne Hackborn95465202014-09-15 16:21:55 -07001010 final int realCallingPid = Binder.getCallingPid();
1011 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -07001012 int callingPid;
1013 if (callingUid >= 0) {
1014 callingPid = -1;
1015 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001016 callingPid = realCallingPid;
1017 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001018 } else {
1019 callingPid = callingUid = -1;
1020 }
1021
Craig Mautnere0a38842013-12-16 16:14:02 -08001022 final ActivityStack stack;
1023 if (container == null || container.mStack.isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001024 stack = mFocusedStack;
Craig Mautnere0a38842013-12-16 16:14:02 -08001025 } else {
1026 stack = container.mStack;
1027 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08001028 stack.mConfigWillChange = config != null && mService.mConfiguration.diff(config) != 0;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001029 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautnerde4ef022013-04-07 19:01:33 -07001030 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001031
1032 final long origId = Binder.clearCallingIdentity();
1033
1034 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001035 (aInfo.applicationInfo.privateFlags
1036 &ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001037 // This may be a heavy-weight process! Check to see if we already
1038 // have another, different heavy-weight process running.
1039 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
1040 if (mService.mHeavyWeightProcess != null &&
1041 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
1042 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001043 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001044 if (caller != null) {
1045 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
1046 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001047 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001048 } else {
1049 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -07001050 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -07001051 + intent.toString());
1052 ActivityOptions.abort(options);
1053 return ActivityManager.START_PERMISSION_DENIED;
1054 }
1055 }
1056
1057 IIntentSender target = mService.getIntentSenderLocked(
1058 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -07001059 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -07001060 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
1061 | PendingIntent.FLAG_ONE_SHOT, null);
1062
1063 Intent newIntent = new Intent();
1064 if (requestCode >= 0) {
1065 // Caller is requesting a result.
1066 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
1067 }
1068 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
1069 new IntentSender(target));
1070 if (mService.mHeavyWeightProcess.activities.size() > 0) {
1071 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
1072 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
1073 hist.packageName);
1074 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
1075 hist.task.taskId);
1076 }
1077 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
1078 aInfo.packageName);
1079 newIntent.setFlags(intent.getFlags());
1080 newIntent.setClassName("android",
1081 HeavyWeightSwitcherActivity.class.getName());
1082 intent = newIntent;
1083 resolvedType = null;
1084 caller = null;
1085 callingUid = Binder.getCallingUid();
1086 callingPid = Binder.getCallingPid();
1087 componentSpecified = true;
1088 try {
1089 ResolveInfo rInfo =
1090 AppGlobals.getPackageManager().resolveIntent(
1091 intent, null,
1092 PackageManager.MATCH_DEFAULT_ONLY
1093 | ActivityManagerService.STOCK_PM_FLAGS, userId);
1094 aInfo = rInfo != null ? rInfo.activityInfo : null;
1095 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1096 } catch (RemoteException e) {
1097 aInfo = null;
1098 }
1099 }
1100 }
1101 }
1102
Dianne Hackborn91097de2014-04-04 18:02:06 -07001103 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
1104 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -07001105 requestCode, callingPid, callingUid, callingPackage,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001106 realCallingPid, realCallingUid, startFlags, options, ignoreTargetSecurity,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001107 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001108
Craig Mautner85c11a82014-07-17 12:38:18 -07001109 Binder.restoreCallingIdentity(origId);
1110
Craig Mautnerde4ef022013-04-07 19:01:33 -07001111 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001112 // If the caller also wants to switch to a new configuration,
1113 // do so now. This allows a clean switch, as we are waiting
1114 // for the current activity to pause (so we will not destroy
1115 // it), and have not yet started the next activity.
1116 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
1117 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -07001118 stack.mConfigWillChange = false;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001119 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautner23ac33b2013-04-01 16:26:35 -07001120 "Updating to new configuration after starting activity.");
Maxim Bogatov05075302015-05-19 18:33:08 -07001121 mService.updateConfigurationLocked(config, null, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001122 }
1123
Craig Mautner23ac33b2013-04-01 16:26:35 -07001124 if (outResult != null) {
1125 outResult.result = res;
1126 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001127 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001128 do {
1129 try {
1130 mService.wait();
1131 } catch (InterruptedException e) {
1132 }
1133 } while (!outResult.timeout && outResult.who == null);
1134 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001135 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001136 if (r.nowVisible && r.state == RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001137 outResult.timeout = false;
1138 outResult.who = new ComponentName(r.info.packageName, r.info.name);
1139 outResult.totalTime = 0;
1140 outResult.thisTime = 0;
1141 } else {
1142 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001143 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001144 do {
1145 try {
1146 mService.wait();
1147 } catch (InterruptedException e) {
1148 }
1149 } while (!outResult.timeout && outResult.who == null);
1150 }
1151 }
1152 }
1153
1154 return res;
1155 }
1156 }
1157
1158 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1159 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
Chong Zhang280d3322015-11-03 17:27:26 -08001160 Bundle bOptions, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001161 if (intents == null) {
1162 throw new NullPointerException("intents is null");
1163 }
1164 if (resolvedTypes == null) {
1165 throw new NullPointerException("resolvedTypes is null");
1166 }
1167 if (intents.length != resolvedTypes.length) {
1168 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1169 }
1170
Craig Mautner23ac33b2013-04-01 16:26:35 -07001171
1172 int callingPid;
1173 if (callingUid >= 0) {
1174 callingPid = -1;
1175 } else if (caller == null) {
1176 callingPid = Binder.getCallingPid();
1177 callingUid = Binder.getCallingUid();
1178 } else {
1179 callingPid = callingUid = -1;
1180 }
1181 final long origId = Binder.clearCallingIdentity();
1182 try {
1183 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001184 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001185 for (int i=0; i<intents.length; i++) {
1186 Intent intent = intents[i];
1187 if (intent == null) {
1188 continue;
1189 }
1190
1191 // Refuse possible leaked file descriptors
1192 if (intent != null && intent.hasFileDescriptors()) {
1193 throw new IllegalArgumentException("File descriptors passed in Intent");
1194 }
1195
1196 boolean componentSpecified = intent.getComponent() != null;
1197
1198 // Don't modify the client's object!
1199 intent = new Intent(intent);
1200
1201 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001202 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001203 // TODO: New, check if this is correct
1204 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1205
1206 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001207 (aInfo.applicationInfo.privateFlags
1208 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001209 throw new IllegalArgumentException(
1210 "FLAG_CANT_SAVE_STATE not supported here");
1211 }
1212
Chong Zhang280d3322015-11-03 17:27:26 -08001213 ActivityOptions options = ActivityOptions.fromBundle(
1214 i == intents.length - 1 ? bOptions : null);
Craig Mautner6170f732013-04-02 13:05:23 -07001215 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001216 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1217 callingPackage, callingPid, callingUid,
Chong Zhang280d3322015-11-03 17:27:26 -08001218 0, options, false, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001219 if (res < 0) {
1220 return res;
1221 }
1222
1223 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1224 }
1225 }
1226 } finally {
1227 Binder.restoreCallingIdentity(origId);
1228 }
1229
1230 return ActivityManager.START_SUCCESS;
1231 }
1232
Craig Mautner2420ead2013-04-01 17:13:20 -07001233 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001234 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001235 throws RemoteException {
1236
Craig Mautner2568c3a2015-03-26 14:22:34 -07001237 if (andResume) {
1238 r.startFreezingScreenLocked(app, 0);
1239 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001240
Craig Mautner2568c3a2015-03-26 14:22:34 -07001241 // schedule launch ticks to collect information about slow apps.
1242 r.startLaunchTickingLocked();
1243 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001244
1245 // Have the window manager re-evaluate the orientation of
1246 // the screen based on the new activity order. Note that
1247 // as a result of this, it can call back into the activity
1248 // manager with a new orientation. We don't care about that,
1249 // because the activity is not currently running so we are
1250 // just restarting it anyway.
1251 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001252 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001253 mService.mConfiguration,
1254 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001255 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001256 }
1257
1258 r.app = app;
1259 app.waitingToKill = null;
1260 r.launchCount++;
1261 r.lastLaunchTime = SystemClock.uptimeMillis();
1262
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001263 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001264
1265 int idx = app.activities.indexOf(r);
1266 if (idx < 0) {
1267 app.activities.add(r);
1268 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001269 mService.updateLruProcessLocked(app, true, null);
1270 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001271
Craig Mautner15df08a2015-04-01 12:17:18 -07001272 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001273 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1274 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001275 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001276 }
1277
1278 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001279 try {
1280 if (app.thread == null) {
1281 throw new RemoteException();
1282 }
1283 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001284 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001285 if (andResume) {
1286 results = r.results;
1287 newIntents = r.newIntents;
1288 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001289 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1290 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1291 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001292 if (andResume) {
1293 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1294 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001295 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001296 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001297 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001298 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001299 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001300 }
Nicolas Geoffray27c07372015-11-05 16:54:09 +00001301 mService.notifyPackageUse(r.intent.getComponent().getPackageName());
Craig Mautner2420ead2013-04-01 17:13:20 -07001302 r.sleeping = false;
1303 r.forceNewConfig = false;
1304 mService.showAskCompatModeDialogLocked(r);
1305 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001306 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001307 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1308 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1309 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001310 final String profileFile = mService.mProfileFile;
1311 if (profileFile != null) {
1312 ParcelFileDescriptor profileFd = mService.mProfileFd;
1313 if (profileFd != null) {
1314 try {
1315 profileFd = profileFd.dup();
1316 } catch (IOException e) {
1317 if (profileFd != null) {
1318 try {
1319 profileFd.close();
1320 } catch (IOException o) {
1321 }
1322 profileFd = null;
1323 }
1324 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001325 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001326
1327 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1328 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001329 }
1330 }
1331 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001332
Craig Mautner2568c3a2015-03-26 14:22:34 -07001333 if (andResume) {
1334 app.hasShownUi = true;
1335 app.pendingUiClean = true;
1336 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001337 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001338 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001339 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001340 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001341 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001342 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001343
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001344 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001345 // This may be a heavy-weight process! Note that the package
1346 // manager will ensure that only activity can run in the main
1347 // process of the .apk, which is the only thing that will be
1348 // considered heavy-weight.
1349 if (app.processName.equals(app.info.packageName)) {
1350 if (mService.mHeavyWeightProcess != null
1351 && mService.mHeavyWeightProcess != app) {
1352 Slog.w(TAG, "Starting new heavy weight process " + app
1353 + " when already running "
1354 + mService.mHeavyWeightProcess);
1355 }
1356 mService.mHeavyWeightProcess = app;
1357 Message msg = mService.mHandler.obtainMessage(
1358 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1359 msg.obj = r;
1360 mService.mHandler.sendMessage(msg);
1361 }
1362 }
1363
1364 } catch (RemoteException e) {
1365 if (r.launchFailed) {
1366 // This is the second time we failed -- finish activity
1367 // and give up.
1368 Slog.e(TAG, "Second failure launching "
1369 + r.intent.getComponent().flattenToShortString()
1370 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001371 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001372 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1373 "2nd-crash", false);
1374 return false;
1375 }
1376
1377 // This is the first time we failed -- restart process and
1378 // retry.
1379 app.activities.remove(r);
1380 throw e;
1381 }
1382
1383 r.launchFailed = false;
1384 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001385 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001386 }
1387
1388 if (andResume) {
1389 // As part of the process of launching, ActivityThread also performs
1390 // a resume.
1391 stack.minimalResumeActivityLocked(r);
1392 } else {
1393 // This activity is not starting in the resumed state... which
1394 // should look like we asked it to pause+stop (but remain visible),
1395 // and it has done so and reported back the current icicle and
1396 // other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001397 if (DEBUG_STATES) Slog.v(TAG_STATES,
1398 "Moving to STOPPED: " + r + " (starting in stopped state)");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001399 r.state = STOPPED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001400 r.stopped = true;
1401 }
1402
1403 // Launch the new version setup screen if needed. We do this -after-
1404 // launching the initial activity (that is, home), so that it can have
1405 // a chance to initialize itself while in the background, making the
1406 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001407 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001408 mService.startSetupActivityLocked();
1409 }
1410
Dianne Hackborn465fa392014-09-14 14:21:18 -07001411 // Update any services we are bound to that might care about whether
1412 // their client may have activities.
1413 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1414
Craig Mautner2420ead2013-04-01 17:13:20 -07001415 return true;
1416 }
1417
Craig Mautnere79d42682013-04-01 19:01:53 -07001418 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001419 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001420 // Is this activity's application already running?
1421 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001422 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001423
1424 r.task.stack.setLaunchTime(r);
1425
1426 if (app != null && app.thread != null) {
1427 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001428 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1429 || !"android".equals(r.info.packageName)) {
1430 // Don't add this if it is a platform component that is marked
1431 // to run in multiple processes, because this is actually
1432 // part of the framework so doesn't make sense to track as a
1433 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001434 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1435 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001436 }
George Mount2c92c972014-03-20 09:38:23 -07001437 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001438 return;
1439 } catch (RemoteException e) {
1440 Slog.w(TAG, "Exception when starting activity "
1441 + r.intent.getComponent().flattenToShortString(), e);
1442 }
1443
1444 // If a dead object exception was thrown -- fall through to
1445 // restart the application.
1446 }
1447
1448 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001449 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001450 }
1451
Craig Mautner6170f732013-04-02 13:05:23 -07001452 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001453 Intent intent, String resolvedType, ActivityInfo aInfo,
1454 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1455 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001456 int callingPid, int callingUid, String callingPackage,
Chong Zhang280d3322015-11-03 17:27:26 -08001457 int realCallingPid, int realCallingUid, int startFlags, ActivityOptions options,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001458 boolean ignoreTargetSecurity, boolean componentSpecified, ActivityRecord[] outActivity,
1459 ActivityContainer container, TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001460 int err = ActivityManager.START_SUCCESS;
1461
1462 ProcessRecord callerApp = null;
1463 if (caller != null) {
1464 callerApp = mService.getRecordForAppLocked(caller);
1465 if (callerApp != null) {
1466 callingPid = callerApp.pid;
1467 callingUid = callerApp.info.uid;
1468 } else {
1469 Slog.w(TAG, "Unable to find app for caller " + caller
1470 + " (pid=" + callingPid + ") when starting: "
1471 + intent.toString());
1472 err = ActivityManager.START_PERMISSION_DENIED;
1473 }
1474 }
1475
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001476 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1477
Craig Mautner6170f732013-04-02 13:05:23 -07001478 if (err == ActivityManager.START_SUCCESS) {
Craig Mautner6170f732013-04-02 13:05:23 -07001479 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001480 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001481 + " on display " + (container == null ? (mFocusedStack == null ?
1482 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1483 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1484 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001485 }
1486
1487 ActivityRecord sourceRecord = null;
1488 ActivityRecord resultRecord = null;
1489 if (resultTo != null) {
1490 sourceRecord = isInAnyStackLocked(resultTo);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001491 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
1492 "Will send result to " + resultTo + " " + sourceRecord);
Craig Mautner6170f732013-04-02 13:05:23 -07001493 if (sourceRecord != null) {
1494 if (requestCode >= 0 && !sourceRecord.finishing) {
1495 resultRecord = sourceRecord;
1496 }
1497 }
1498 }
Craig Mautner6170f732013-04-02 13:05:23 -07001499
Dianne Hackborn91097de2014-04-04 18:02:06 -07001500 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001501
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001502 if ((launchFlags & Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001503 // Transfer the result target from the source activity to the new
1504 // one being started, including any failures.
1505 if (requestCode >= 0) {
1506 ActivityOptions.abort(options);
1507 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1508 }
1509 resultRecord = sourceRecord.resultTo;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001510 if (resultRecord != null && !resultRecord.isInStackLocked()) {
1511 resultRecord = null;
1512 }
Craig Mautner6170f732013-04-02 13:05:23 -07001513 resultWho = sourceRecord.resultWho;
1514 requestCode = sourceRecord.requestCode;
1515 sourceRecord.resultTo = null;
1516 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001517 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001518 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001519 if (sourceRecord.launchedFromUid == callingUid) {
1520 // The new activity is being launched from the same uid as the previous
1521 // activity in the flow, and asking to forward its result back to the
1522 // previous. In this case the activity is serving as a trampoline between
1523 // the two, so we also want to update its launchedFromPackage to be the
1524 // same as the previous activity. Note that this is safe, since we know
1525 // these two packages come from the same uid; the caller could just as
1526 // well have supplied that same package name itself. This specifially
1527 // deals with the case of an intent picker/chooser being launched in the app
1528 // flow to redirect to an activity picked by the user, where we want the final
1529 // activity to consider it to have been launched by the previous app activity.
1530 callingPackage = sourceRecord.launchedFromPackage;
1531 }
Craig Mautner6170f732013-04-02 13:05:23 -07001532 }
1533
1534 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1535 // We couldn't find a class that can handle the given Intent.
1536 // That's the end of that!
1537 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1538 }
1539
1540 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1541 // We couldn't find the specific class specified in the Intent.
1542 // Also the end of the line.
1543 err = ActivityManager.START_CLASS_NOT_FOUND;
1544 }
1545
Dianne Hackborn91097de2014-04-04 18:02:06 -07001546 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1547 && sourceRecord.task.voiceSession != null) {
1548 // If this activity is being launched as part of a voice session, we need
1549 // to ensure that it is safe to do so. If the upcoming activity will also
1550 // be part of the voice session, we can only launch it if it has explicitly
1551 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001552 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001553 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1554 try {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001555 intent.addCategory(Intent.CATEGORY_VOICE);
Dianne Hackborn95465202014-09-15 16:21:55 -07001556 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1557 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001558 Slog.w(TAG,
1559 "Activity being started in current voice task does not support voice: "
1560 + intent);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001561 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1562 }
1563 } catch (RemoteException e) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001564 Slog.w(TAG, "Failure checking voice capabilities", e);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001565 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1566 }
1567 }
1568 }
1569
1570 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1571 // If the caller is starting a new voice session, just make sure the target
1572 // is actually allowing it to run this way.
1573 try {
1574 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1575 intent, resolvedType)) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001576 Slog.w(TAG,
1577 "Activity being started in new voice task does not support: "
1578 + intent);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001579 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1580 }
1581 } catch (RemoteException e) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001582 Slog.w(TAG, "Failure checking voice capabilities", e);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001583 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1584 }
1585 }
1586
louis_changcd5d1982014-08-01 10:09:08 +08001587 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1588
Craig Mautner6170f732013-04-02 13:05:23 -07001589 if (err != ActivityManager.START_SUCCESS) {
1590 if (resultRecord != null) {
1591 resultStack.sendActivityResultLocked(-1,
1592 resultRecord, resultWho, requestCode,
1593 Activity.RESULT_CANCELED, null);
1594 }
Craig Mautner6170f732013-04-02 13:05:23 -07001595 ActivityOptions.abort(options);
1596 return err;
1597 }
1598
Svetoslav7008b512015-06-24 18:47:07 -07001599 boolean abort = false;
1600
Svet Ganov99b60432015-06-27 13:15:22 -07001601 final int startAnyPerm = mService.checkPermission(
1602 START_ANY_ACTIVITY, callingPid, callingUid);
1603
1604 if (startAnyPerm != PERMISSION_GRANTED) {
1605 final int componentRestriction = getComponentRestrictionForCallingPackage(
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001606 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
Svet Ganov99b60432015-06-27 13:15:22 -07001607 final int actionRestriction = getActionRestrictionForCallingPackage(
1608 intent.getAction(), callingPackage, callingPid, callingUid);
1609
1610 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1611 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1612 if (resultRecord != null) {
1613 resultStack.sendActivityResultLocked(-1,
1614 resultRecord, resultWho, requestCode,
1615 Activity.RESULT_CANCELED, null);
1616 }
1617 String msg;
1618 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1619 msg = "Permission Denial: starting " + intent.toString()
1620 + " from " + callerApp + " (pid=" + callingPid
1621 + ", uid=" + callingUid + ")" + " with revoked permission "
1622 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1623 } else if (!aInfo.exported) {
1624 msg = "Permission Denial: starting " + intent.toString()
1625 + " from " + callerApp + " (pid=" + callingPid
1626 + ", uid=" + callingUid + ")"
1627 + " not exported from uid " + aInfo.applicationInfo.uid;
1628 } else {
1629 msg = "Permission Denial: starting " + intent.toString()
1630 + " from " + callerApp + " (pid=" + callingPid
1631 + ", uid=" + callingUid + ")"
1632 + " requires " + aInfo.permission;
1633 }
1634 Slog.w(TAG, msg);
1635 throw new SecurityException(msg);
1636 }
1637
1638 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1639 String message = "Appop Denial: starting " + intent.toString()
1640 + " from " + callerApp + " (pid=" + callingPid
1641 + ", uid=" + callingUid + ")"
1642 + " requires " + AppOpsManager.permissionToOp(
1643 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1644 Slog.w(TAG, message);
1645 abort = true;
1646 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1647 String message = "Appop Denial: starting " + intent.toString()
1648 + " from " + callerApp + " (pid=" + callingPid
1649 + ", uid=" + callingUid + ")"
1650 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1651 Slog.w(TAG, message);
1652 abort = true;
1653 }
Svetoslav7008b512015-06-24 18:47:07 -07001654 }
1655
1656 abort |= !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001657 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001658
Craig Mautner6170f732013-04-02 13:05:23 -07001659 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001660 try {
1661 // The Intent we give to the watcher has the extra data
1662 // stripped off, since it can contain private information.
1663 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001664 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001665 aInfo.applicationInfo.packageName);
1666 } catch (RemoteException e) {
1667 mService.mController = null;
1668 }
Ben Gruver5e207332013-04-03 17:41:37 -07001669 }
Craig Mautner6170f732013-04-02 13:05:23 -07001670
Clara Bayarrif7fea162015-10-22 16:09:52 +01001671 UserInfo user = getUserInfo(userId);
Clara Bayarri56878a92015-10-29 15:43:55 +00001672 KeyguardManager km = (KeyguardManager) mService.mContext
1673 .getSystemService(Context.KEYGUARD_SERVICE);
1674 if (user.isManagedProfile()
Clara Bayarri10ad84a2015-12-01 17:38:05 +00001675 && LockPatternUtils.isSeparateWorkChallengeEnabled()
Clara Bayarri56878a92015-10-29 15:43:55 +00001676 && km.isDeviceLocked(userId)) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001677 IIntentSender target = mService.getIntentSenderLocked(
1678 ActivityManager.INTENT_SENDER_ACTIVITY, callingPackage,
1679 Binder.getCallingUid(), userId, null, null, 0, new Intent[]{ intent },
1680 new String[]{ resolvedType },
1681 PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT
1682 | PendingIntent.FLAG_IMMUTABLE, null);
1683 int flags = intent.getFlags();
1684 intent = km.createConfirmDeviceCredentialIntent(null, null);
1685 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1686 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1687 intent.putExtra(Intent.EXTRA_PACKAGE_NAME, aInfo.packageName);
1688 intent.putExtra(Intent.EXTRA_INTENT, new IntentSender(target));
1689 intent.putExtra(Intent.EXTRA_USER_ID, userId);
1690 intent.setFlags(flags);
1691
1692 resolvedType = null;
1693 callingUid = realCallingUid;
1694 callingPid = realCallingPid;
1695
1696 UserInfo parent = UserManager.get(mService.mContext).getProfileParent(userId);
1697 aInfo = resolveActivity(intent, null, PackageManager.MATCH_DEFAULT_ONLY
1698 | ActivityManagerService.STOCK_PM_FLAGS, null, parent.id);
1699 }
1700
Ben Gruver5e207332013-04-03 17:41:37 -07001701 if (abort) {
1702 if (resultRecord != null) {
1703 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001704 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001705 }
Ben Gruver5e207332013-04-03 17:41:37 -07001706 // We pretend to the caller that it was really started, but
1707 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001708 ActivityOptions.abort(options);
1709 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001710 }
1711
Svet Ganov9c165d72015-12-01 19:52:26 -08001712 // If permissions need a review before any of the app components can run, we
1713 // launch the review activity and pass a pending intent to start the activity
1714 // we are to launching now after the review is completed.
1715 if (Build.PERMISSIONS_REVIEW_REQUIRED && aInfo != null) {
1716 if (mService.getPackageManagerInternalLocked().isPermissionsReviewRequired(
1717 aInfo.packageName, userId)) {
1718 IIntentSender target = mService.getIntentSenderLocked(
1719 ActivityManager.INTENT_SENDER_ACTIVITY, callingPackage,
1720 callingUid, userId, null, null, 0, new Intent[]{intent},
1721 new String[]{resolvedType}, PendingIntent.FLAG_CANCEL_CURRENT
1722 | PendingIntent.FLAG_ONE_SHOT, null);
1723
1724 Intent newIntent = new Intent(Intent.ACTION_REVIEW_PERMISSIONS);
1725 newIntent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1726 newIntent.putExtra(Intent.EXTRA_PACKAGE_NAME, aInfo.packageName);
1727 newIntent.putExtra(Intent.EXTRA_INTENT, new IntentSender(target));
1728 if (resultRecord != null) {
1729 newIntent.putExtra(Intent.EXTRA_RESULT_NEEDED, true);
1730 }
1731 newIntent.setFlags(intent.getFlags());
1732 intent = newIntent;
1733
1734 resolvedType = null;
1735 callingUid = realCallingUid;
1736 callingPid = realCallingPid;
1737
1738 aInfo = resolveActivity(intent, null, PackageManager.MATCH_DEFAULT_ONLY
1739 | ActivityManagerService.STOCK_PM_FLAGS, null, userId);
1740
1741 if (DEBUG_PERMISSIONS_REVIEW) {
1742 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true,
1743 true, false) + "} from uid " + callingUid + " on display "
1744 + (container == null ? (mFocusedStack == null ?
1745 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1746 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1747 container.mActivityDisplay.mDisplayId)));
1748 }
1749 }
1750 }
1751
Craig Mautner6170f732013-04-02 13:05:23 -07001752 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001753 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001754 requestCode, componentSpecified, voiceSession != null, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001755 if (outActivity != null) {
1756 outActivity[0] = r;
1757 }
1758
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001759 if (r.appTimeTracker == null && sourceRecord != null) {
1760 // If the caller didn't specify an explicit time tracker, we want to continue
1761 // tracking under any it has.
1762 r.appTimeTracker = sourceRecord.appTimeTracker;
1763 }
1764
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001765 final ActivityStack stack = mFocusedStack;
Dianne Hackborn91097de2014-04-04 18:02:06 -07001766 if (voiceSession == null && (stack.mResumedActivity == null
1767 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001768 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1769 realCallingPid, realCallingUid, "Activity start")) {
Chong Zhang280d3322015-11-03 17:27:26 -08001770 PendingActivityLaunch pal = new PendingActivityLaunch(r,
Robert Carr13997f52015-10-23 13:13:39 -07001771 sourceRecord, startFlags, stack, callerApp);
Craig Mautneree36c772014-07-16 14:56:05 -07001772 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001773 ActivityOptions.abort(options);
1774 return ActivityManager.START_SWITCHES_CANCELED;
1775 }
1776 }
1777
1778 if (mService.mDidAppSwitch) {
1779 // This is the second allowed switch since we stopped switches,
1780 // so now just generally allow switches. Use case: user presses
1781 // home (switches disabled, switch to home, mDidAppSwitch now true);
1782 // user taps a home icon (coming from home so allowed, we hit here
1783 // and now allow anyone to switch again).
1784 mService.mAppSwitchesAllowedTime = 0;
1785 } else {
1786 mService.mDidAppSwitch = true;
1787 }
1788
Craig Mautneree36c772014-07-16 14:56:05 -07001789 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001790
Dianne Hackborn91097de2014-04-04 18:02:06 -07001791 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001792 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001793
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001794 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001795 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001796 // activity start, but we are not actually doing an activity
1797 // switch... just dismiss the keyguard now, because we
1798 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001799 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001800 }
1801 return err;
1802 }
1803
Clara Bayarrif7fea162015-10-22 16:09:52 +01001804 private UserInfo getUserInfo(int userId) {
1805 final long identity = Binder.clearCallingIdentity();
1806 try {
1807 return UserManager.get(mService.mContext).getUserInfo(userId);
1808 } finally {
1809 Binder.restoreCallingIdentity(identity);
1810 }
1811 }
1812
Svet Ganov99b60432015-06-27 13:15:22 -07001813 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001814 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001815 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1816 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001817 == PackageManager.PERMISSION_DENIED) {
1818 return ACTIVITY_RESTRICTION_PERMISSION;
1819 }
1820
Christopher Tateff7add02015-08-17 10:23:22 -07001821 if (activityInfo.permission == null) {
1822 return ACTIVITY_RESTRICTION_NONE;
1823 }
1824
Svet Ganov99b60432015-06-27 13:15:22 -07001825 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1826 if (opCode == AppOpsManager.OP_NONE) {
1827 return ACTIVITY_RESTRICTION_NONE;
1828 }
1829
1830 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1831 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001832 if (!ignoreTargetSecurity) {
1833 return ACTIVITY_RESTRICTION_APPOP;
1834 }
Svet Ganov99b60432015-06-27 13:15:22 -07001835 }
1836
1837 return ACTIVITY_RESTRICTION_NONE;
1838 }
1839
Svetoslav7008b512015-06-24 18:47:07 -07001840 private int getActionRestrictionForCallingPackage(String action,
1841 String callingPackage, int callingPid, int callingUid) {
1842 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001843 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001844 }
1845
1846 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1847 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001848 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001849 }
1850
1851 final PackageInfo packageInfo;
1852 try {
1853 packageInfo = mService.mContext.getPackageManager()
1854 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1855 } catch (PackageManager.NameNotFoundException e) {
1856 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001857 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001858 }
1859
1860 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001861 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001862 }
1863
1864 if (mService.checkPermission(permission, callingPid, callingUid) ==
1865 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001866 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001867 }
1868
1869 final int opCode = AppOpsManager.permissionToOpCode(permission);
1870 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001871 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001872 }
1873
1874 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1875 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001876 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001877 }
1878
Svet Ganov99b60432015-06-27 13:15:22 -07001879 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001880 }
1881
Chong Zhang0fa656b2015-08-31 15:17:21 -07001882 ActivityStack computeStackFocus(ActivityRecord r, boolean newTask, Rect bounds) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001883 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001884
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001885 if (!(r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
1886 return mHomeStack;
1887 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001888
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001889 ActivityStack stack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001890
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001891 if (task != null && task.stack != null) {
1892 stack = task.stack;
1893 if (stack.isOnHomeDisplay()) {
1894 if (mFocusedStack != stack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001895 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001896 "computeStackFocus: Setting " + "focused stack to r=" + r
1897 + " task=" + task);
1898 } else {
1899 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1900 "computeStackFocus: Focused stack already=" + mFocusedStack);
Craig Mautner858d8a62013-04-23 17:08:34 -07001901 }
1902 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001903 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001904 }
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001905
1906 final ActivityContainer container = r.mInitialActivityContainer;
1907 if (container != null) {
1908 // The first time put it on the desired stack, after this put on task stack.
1909 r.mInitialActivityContainer = null;
1910 return container.mStack;
1911 }
1912
1913 // The fullscreen stack can contain any task regardless of if the task is resizeable
1914 // or not. So, we let the task go in the fullscreen task if it is the focus stack.
1915 // If the freeform stack has focus, and the activity to be launched is resizeable,
1916 // we can also put it in the focused stack.
1917 final boolean canUseFocusedStack =
1918 mFocusedStack.mStackId == FULLSCREEN_WORKSPACE_STACK_ID
1919 || mFocusedStack.mStackId == FREEFORM_WORKSPACE_STACK_ID && r.info.resizeable;
1920 if (canUseFocusedStack
1921 && (!newTask || mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
1922 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1923 "computeStackFocus: Have a focused stack=" + mFocusedStack);
1924 return mFocusedStack;
1925 }
1926
1927 // We first try to put the task in the first dynamic stack.
1928 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1929 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
1930 stack = homeDisplayStacks.get(stackNdx);
Wale Ogunwale3797c222015-10-27 14:21:58 -07001931 if (!StackId.isStaticStack(stack.mStackId)) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001932 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1933 "computeStackFocus: Setting focused stack=" + stack);
1934 return stack;
1935 }
1936 }
1937
1938 // If there is no suitable dynamic stack then we figure out which static stack to use.
1939 final int stackId = task != null ? task.getLaunchStackId() :
1940 bounds != null ? FREEFORM_WORKSPACE_STACK_ID :
1941 FULLSCREEN_WORKSPACE_STACK_ID;
1942 stack = getStack(stackId, CREATE_IF_NEEDED, ON_TOP);
1943 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS, "computeStackFocus: New stack r="
1944 + r + " stackId=" + stack.mStackId);
1945 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001946 }
1947
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001948 boolean setFocusedStack(ActivityRecord r, String reason) {
1949 if (r == null) {
1950 // Not sure what you are trying to do, but it is not going to work...
1951 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001952 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001953 final TaskRecord task = r.task;
1954 if (task == null || task.stack == null) {
1955 Slog.w(TAG, "Can't set focus stack for r=" + r + " task=" + task);
1956 return false;
1957 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001958 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001959 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001960 }
1961
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001962 final int startActivityUncheckedLocked(final ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001963 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Chong Zhang280d3322015-11-03 17:27:26 -08001964 boolean doResume, ActivityOptions options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001965 final Intent intent = r.intent;
1966 final int callingUid = r.launchedFromUid;
1967
Chong Zhang0fa656b2015-08-31 15:17:21 -07001968 boolean overrideBounds = false;
1969 Rect newBounds = null;
Chong Zhang56f171d2015-10-08 10:39:23 -07001970 if (options != null && (r.info.resizeable || (inTask != null && inTask.mResizeable))) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001971 if (canUseActivityOptionsLaunchBounds(options)) {
Chong Zhang0fa656b2015-08-31 15:17:21 -07001972 overrideBounds = true;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001973 newBounds = options.getLaunchBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07001974 }
1975 }
1976
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001977 // In some flows in to this function, we retrieve the task record and hold on to it
1978 // without a lock before calling back in to here... so the task at this point may
1979 // not actually be in recents. Check for that, and if it isn't in recents just
1980 // consider it invalid.
1981 if (inTask != null && !inTask.inRecents) {
1982 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1983 inTask = null;
1984 }
1985
Craig Mautnerad400af2014-08-13 16:41:36 -07001986 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001987 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1988 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001989
Craig Mautnera228ae92014-07-09 05:44:55 -07001990 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001991 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001992 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001993 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1994 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1995 "\"singleInstance\" or \"singleTask\"");
1996 launchFlags &=
1997 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1998 } else {
1999 switch (r.info.documentLaunchMode) {
2000 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
2001 break;
2002 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
2003 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
2004 break;
2005 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
2006 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
2007 break;
2008 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
2009 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
2010 break;
2011 }
2012 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002013
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002014 final boolean launchTaskBehind = r.mLaunchTaskBehind
2015 && !launchSingleTask && !launchSingleInstance
2016 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002017
Wale Ogunwale7d701172015-03-11 15:36:30 -07002018 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0
2019 && r.resultTo.task.stack != null) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002020 // For whatever reason this activity is being launched into a new
2021 // task... yet the caller has requested a result back. Well, that
2022 // is pretty messed up, so instead immediately send back a cancel
2023 // and let the new task continue launched as normal without a
2024 // dependency on its originator.
2025 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
2026 r.resultTo.task.stack.sendActivityResultLocked(-1,
2027 r.resultTo, r.resultWho, r.requestCode,
2028 Activity.RESULT_CANCELED, null);
2029 r.resultTo = null;
2030 }
2031
2032 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
2033 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2034 }
2035
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002036 // If we are actually going to launch in to a new task, there are some cases where
2037 // we further want to do multiple task.
2038 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
2039 if (launchTaskBehind
2040 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
2041 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
2042 }
2043 }
2044
Craig Mautner8849a5e2013-04-02 16:41:03 -07002045 // We'll invoke onUserLeaving before onPause only if the launching
2046 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07002047 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002048 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
2049 "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002050
2051 // If the caller has asked not to resume at this point, we make note
2052 // of this in the record so that we can skip it when trying to find
2053 // the top running activity.
Chong Zhang45c25ce2015-08-10 22:18:26 -07002054 if (!doResume || !okToShowLocked(r)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002055 r.delayedResume = true;
Chong Zhang45c25ce2015-08-10 22:18:26 -07002056 doResume = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002057 }
2058
Craig Mautnera254cd72014-05-25 16:47:39 -07002059 ActivityRecord notTop =
2060 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002061
2062 // If the onlyIfNeeded flag is set, then we can do this if the activity
2063 // being launched is the same as the one making the call... or, as
2064 // a special case, if we do not know the caller then we count the
2065 // current top activity as the caller.
2066 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2067 ActivityRecord checkedCaller = sourceRecord;
2068 if (checkedCaller == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002069 checkedCaller = mFocusedStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002070 }
2071 if (!checkedCaller.realActivity.equals(r.realActivity)) {
2072 // Caller is not the same as launcher, so always needed.
2073 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
2074 }
2075 }
2076
Craig Mautner8849a5e2013-04-02 16:41:03 -07002077 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002078 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002079
2080 // If the caller is not coming from another activity, but has given us an
2081 // explicit task into which they would like us to launch the new activity,
2082 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002083 if (sourceRecord == null && inTask != null && inTask.stack != null) {
2084 final Intent baseIntent = inTask.getBaseIntent();
2085 final ActivityRecord root = inTask.getRootActivity();
2086 if (baseIntent == null) {
2087 ActivityOptions.abort(options);
2088 throw new IllegalArgumentException("Launching into task without base intent: "
2089 + inTask);
2090 }
2091
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002092 // If this task is empty, then we are adding the first activity -- it
2093 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002094 if (launchSingleInstance || launchSingleTask) {
2095 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
2096 ActivityOptions.abort(options);
2097 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
2098 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002099 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002100 if (root != null) {
2101 ActivityOptions.abort(options);
2102 throw new IllegalArgumentException("Caller with inTask " + inTask
2103 + " has root " + root + " but target is singleInstance/Task");
2104 }
2105 }
2106
2107 // If task is empty, then adopt the interesting intent launch flags in to the
2108 // activity being started.
2109 if (root == null) {
2110 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
2111 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
2112 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
2113 launchFlags = (launchFlags&~flagsOfInterest)
2114 | (baseIntent.getFlags()&flagsOfInterest);
2115 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002116 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07002117 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002118
Dianne Hackborn962d5352014-08-29 16:27:40 -07002119 // If the task is not empty and the caller is asking to start it as the root
2120 // of a new task, then we don't actually want to start this on the task. We
2121 // will bring the task to the front, and possibly give it a new intent.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002122 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07002123 addingToTask = false;
2124
2125 } else {
2126 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002127 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07002128
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002129 reuseTask = inTask;
2130 } else {
2131 inTask = null;
Chong Zhang85ee6542015-10-02 13:36:38 -07002132 // Launch ResolverActivity in the source task, so that it stays in the task
2133 // bounds when in freeform workspace.
2134 // Also put noDisplay activities in the source task. These by itself can
2135 // be placed in any task/stack, however it could launch other activities
2136 // like ResolverActivity, and we want those to stay in the original task.
2137 if (r.isResolverActivity() || r.noDisplay) {
2138 addingToTask = true;
2139 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002140 }
2141
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002142 if (inTask == null) {
2143 if (sourceRecord == null) {
2144 // This activity is not being started from another... in this
2145 // case we -always- start a new task.
2146 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
2147 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
2148 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
2149 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2150 }
2151 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
2152 // The original activity who is starting us is running as a single
2153 // instance... this new activity it is starting must go on its
2154 // own task.
2155 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2156 } else if (launchSingleInstance || launchSingleTask) {
2157 // The activity being started is a single instance... it always
2158 // gets launched into its own task.
2159 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2160 }
2161 }
2162
2163 ActivityInfo newTaskInfo = null;
2164 Intent newTaskIntent = null;
2165 ActivityStack sourceStack;
2166 if (sourceRecord != null) {
2167 if (sourceRecord.finishing) {
2168 // If the source is finishing, we can't further count it as our source. This
2169 // is because the task it is associated with may now be empty and on its way out,
2170 // so we don't want to blindly throw it in to that task. Instead we will take
2171 // the NEW_TASK flow and try to find a task for it. But save the task information
2172 // so it can be used when creating the new task.
2173 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
2174 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
2175 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
2176 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2177 newTaskInfo = sourceRecord.info;
2178 newTaskIntent = sourceRecord.task.intent;
2179 }
2180 sourceRecord = null;
2181 sourceStack = null;
2182 } else {
2183 sourceStack = sourceRecord.task.stack;
2184 }
2185 } else {
2186 sourceStack = null;
2187 }
2188
2189 boolean movedHome = false;
2190 ActivityStack targetStack;
2191
2192 intent.setFlags(launchFlags);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002193 final boolean noAnimation = (launchFlags & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002194
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002195 // We may want to try to place the new activity in to an existing task. We always
2196 // do this if the target activity is singleTask or singleInstance; we will also do
2197 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
2198 // us to still place it in a new task: multi task, always doc mode, or being asked to
2199 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07002200 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
2201 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07002202 || launchSingleInstance || launchSingleTask) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002203 // If bring to front is requested, and no result is requested and we have not
2204 // been given an explicit task to launch in to, and
Craig Mautner8849a5e2013-04-02 16:41:03 -07002205 // we can find a task that was started with this same
2206 // component, then instead of launching bring that one to the front.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002207 if (inTask == null && r.resultTo == null) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002208 // See if there is a task to bring to the front. If this is
2209 // a SINGLE_INSTANCE activity, there can be one and only one
2210 // instance of it in the history, and it is always in its own
2211 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07002212 ActivityRecord intentActivity = !launchSingleInstance ?
2213 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002214 if (intentActivity != null) {
Jason Monk25d237b2015-06-19 10:39:39 -04002215 // When the flags NEW_TASK and CLEAR_TASK are set, then the task gets reused
2216 // but still needs to be a lock task mode violation since the task gets
2217 // cleared out and the device would otherwise leave the locked task.
2218 if (isLockTaskModeViolation(intentActivity.task,
2219 (launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
2220 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))) {
Craig Mautner6cd6cec2015-04-01 00:02:12 -07002221 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07002222 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08002223 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2224 }
Craig Mautner29219d92013-04-16 20:19:12 -07002225 if (r.task == null) {
2226 r.task = intentActivity.task;
2227 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002228 if (intentActivity.task.intent == null) {
2229 // This task was started because of movement of
2230 // the activity based on affinity... now that we
2231 // are actually launching it, we can assign the
2232 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07002233 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002234 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002235 targetStack = intentActivity.task.stack;
2236 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002237 // If the target task is not in the front, then we need
2238 // to bring it to the front... except... well, with
2239 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
2240 // to have the same behavior as if a new instance was
2241 // being started, which means not bringing it to the front
2242 // if the caller is not itself in the front.
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002243 final ActivityStack focusStack = getFocusedStack();
2244 ActivityRecord curTop = (focusStack == null)
2245 ? null : focusStack.topRunningNonDelayedActivityLocked(notTop);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002246 boolean movedToFront = false;
Craig Mautner7504d7b2013-09-17 10:50:53 -07002247 if (curTop != null && (curTop.task != intentActivity.task ||
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002248 curTop.task != focusStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002249 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07002250 if (sourceRecord == null || (sourceStack.topActivity() != null &&
2251 sourceStack.topActivity().task == sourceRecord.task)) {
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002252 // We really do want to push this one into the user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07002253 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002254 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
2255 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002256 movedHome = true;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002257 targetStack.moveTaskToFrontLocked(intentActivity.task, noAnimation,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002258 options, r.appTimeTracker, "bringingFoundTaskToFront");
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002259 movedToFront = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002260 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07002261 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2262 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07002263 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07002264 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002265 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002266 options = null;
2267 }
2268 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002269 if (!movedToFront && doResume) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002270 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Bring to front target: " + targetStack
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002271 + " from " + intentActivity);
2272 targetStack.moveToFront("intentActivityFound");
2273 }
2274
Craig Mautner8849a5e2013-04-02 16:41:03 -07002275 // If the caller has requested that the target task be
2276 // reset, then do so.
2277 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
2278 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
2279 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002280 if ((startFlags & ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002281 // We don't need to start a new activity, and
2282 // the client said not to do anything if that
2283 // is the case, so this is it! And for paranoia, make
2284 // sure we have correctly resumed the top activity.
2285 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002286 resumeTopActivitiesLocked(targetStack, null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002287
2288 // Make sure to notify Keyguard as well if we are not running an app
2289 // transition later.
2290 if (!movedToFront) {
2291 notifyActivityDrawnForKeyguard();
2292 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002293 } else {
2294 ActivityOptions.abort(options);
2295 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002296 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002297 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2298 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002299 if ((launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002300 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002301 // The caller has requested to completely replace any
2302 // existing task with its new activity. Well that should
2303 // not be too hard...
2304 reuseTask = intentActivity.task;
2305 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07002306 reuseTask.setIntent(r);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002307 } else if ((launchFlags & FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07002308 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002309 // In this situation we want to remove all activities
2310 // from the task up to the one being started. In most
2311 // cases this means we are resetting the task to its
2312 // initial state.
2313 ActivityRecord top =
2314 intentActivity.task.performClearTaskLocked(r, launchFlags);
2315 if (top != null) {
2316 if (top.frontOfTask) {
2317 // Activity aliases may mean we use different
2318 // intents for the top activity, so make sure
2319 // the task now has the identity of the new
2320 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07002321 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002322 }
2323 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
2324 r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002325 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002326 } else {
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002327 // A special case: we need to start the activity because it is not
2328 // currently running, and the caller has asked to clear the current
2329 // task to have this activity at the top.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002330 addingToTask = true;
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002331 // Now pretend like this activity is being started by the top of its
2332 // task, so it is put in the right place.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002333 sourceRecord = intentActivity;
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002334 TaskRecord task = sourceRecord.task;
2335 if (task != null && task.stack == null) {
2336 // Target stack got cleared when we all activities were removed
2337 // above. Go ahead and reset it.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002338 targetStack = computeStackFocus(
2339 sourceRecord, false /* newTask */, null /* bounds */);
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002340 targetStack.addTask(
2341 task, !launchTaskBehind /* toTop */, false /* moving */);
2342 }
2343
Craig Mautner8849a5e2013-04-02 16:41:03 -07002344 }
2345 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
2346 // In this case the top activity on the task is the
2347 // same as the one being launched, so we take that
2348 // as a request to bring the task to the foreground.
2349 // If the top activity in the task is the root
2350 // activity, deliver this new intent to it if it
2351 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07002352 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07002353 && intentActivity.realActivity.equals(r.realActivity)) {
2354 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
2355 intentActivity.task);
2356 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07002357 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002358 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002359 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
2360 r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002361 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
2362 // In this case we are launching the root activity
2363 // of the task, but with a different intent. We
2364 // should start a new instance on top.
2365 addingToTask = true;
2366 sourceRecord = intentActivity;
2367 }
2368 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
2369 // In this case an activity is being launched in to an
2370 // existing task, without resetting that task. This
2371 // is typically the situation of launching an activity
2372 // from a notification or shortcut. We want to place
2373 // the new activity on top of the current task.
2374 addingToTask = true;
2375 sourceRecord = intentActivity;
2376 } else if (!intentActivity.task.rootWasReset) {
2377 // In this case we are launching in to an existing task
2378 // that has not yet been started from its front door.
2379 // The current task has been brought to the front.
2380 // Ideally, we'd probably like to place this new task
2381 // at the bottom of its stack, but that's a little hard
2382 // to do with the current organization of the code so
2383 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07002384 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002385 }
2386 if (!addingToTask && reuseTask == null) {
2387 // We didn't do anything... but it was needed (a.k.a., client
2388 // don't use that intent!) And for paranoia, make
2389 // sure we have correctly resumed the top activity.
2390 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002391 targetStack.resumeTopActivityLocked(null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002392 if (!movedToFront) {
2393 // Make sure to notify Keyguard as well if we are not running an app
2394 // transition later.
2395 notifyActivityDrawnForKeyguard();
2396 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002397 } else {
2398 ActivityOptions.abort(options);
2399 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002400 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002401 return ActivityManager.START_TASK_TO_FRONT;
2402 }
2403 }
2404 }
2405 }
2406
2407 //String uri = r.intent.toURI();
2408 //Intent intent2 = new Intent(uri);
2409 //Slog.i(TAG, "Given intent: " + r.intent);
2410 //Slog.i(TAG, "URI is: " + uri);
2411 //Slog.i(TAG, "To intent: " + intent2);
2412
2413 if (r.packageName != null) {
2414 // If the activity being launched is the same as the one currently
2415 // at the top, then we need to check if it should only be launched
2416 // once.
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002417 ActivityStack topStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002418 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002419 if (top != null && r.resultTo == null) {
2420 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2421 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07002422 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07002423 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002424 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
2425 top.task);
2426 // For paranoia, make sure we have correctly
2427 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002428 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002429 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002430 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002431 }
2432 ActivityOptions.abort(options);
2433 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2434 // We don't need to start a new activity, and
2435 // the client said not to do anything if that
2436 // is the case, so this is it!
2437 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2438 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002439 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002440 return ActivityManager.START_DELIVERED_TO_TOP;
2441 }
2442 }
2443 }
2444 }
2445
2446 } else {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002447 if (r.resultTo != null && r.resultTo.task.stack != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002448 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2449 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002450 }
2451 ActivityOptions.abort(options);
2452 return ActivityManager.START_CLASS_NOT_FOUND;
2453 }
2454
2455 boolean newTask = false;
2456 boolean keepCurTransition = false;
2457
Craig Mautnerbb742462014-07-07 15:28:55 -07002458 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002459 sourceRecord.task : null;
2460
Craig Mautner8849a5e2013-04-02 16:41:03 -07002461 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002462 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002463 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002464 newTask = true;
Chong Zhang0fa656b2015-08-31 15:17:21 -07002465 targetStack = computeStackFocus(r, newTask, newBounds);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002466 if (doResume) {
2467 targetStack.moveToFront("startingNewTask");
2468 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002469
Craig Mautner8849a5e2013-04-02 16:41:03 -07002470 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002471 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2472 newTaskInfo != null ? newTaskInfo : r.info,
2473 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002474 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2475 taskToAffiliate);
Chong Zhang0fa656b2015-08-31 15:17:21 -07002476 if (overrideBounds) {
2477 r.task.updateOverrideConfiguration(newBounds);
2478 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002479 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2480 "Starting new activity " + r + " in new task " + r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002481 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002482 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002483 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002484 if (isLockTaskModeViolation(r.task)) {
2485 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2486 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2487 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002488 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002489 if ((launchFlags &
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002490 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2491 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002492 // Caller wants to appear on home activity, so before starting
2493 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002494 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002495 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002496 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002497 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002498 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002499 if (isLockTaskModeViolation(sourceTask)) {
2500 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2501 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2502 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002503 targetStack = sourceTask.stack;
Chong Zhang45c25ce2015-08-10 22:18:26 -07002504 if (doResume) {
2505 targetStack.moveToFront("sourceStackToFront");
2506 }
Craig Mautner737fae22014-10-15 12:52:10 -07002507 final TaskRecord topTask = targetStack.topTask();
2508 if (topTask != sourceTask) {
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002509 targetStack.moveTaskToFrontLocked(sourceTask, noAnimation, options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002510 r.appTimeTracker, "sourceTaskToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002511 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002512 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002513 // In this case, we are adding the activity to an existing
2514 // task, but the caller has asked to clear that task if the
2515 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002516 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002517 keepCurTransition = true;
2518 if (top != null) {
2519 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002520 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002521 // For paranoia, make sure we have correctly
2522 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002523 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002524 if (doResume) {
2525 targetStack.resumeTopActivityLocked(null);
2526 }
2527 ActivityOptions.abort(options);
2528 return ActivityManager.START_DELIVERED_TO_TOP;
2529 }
2530 } else if (!addingToTask &&
2531 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2532 // In this case, we are launching an activity in our own task
2533 // that may already be running somewhere in the history, and
2534 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002535 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002536 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002537 final TaskRecord task = top.task;
2538 task.moveActivityToFrontLocked(top);
2539 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002540 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002541 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002542 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002543 if (doResume) {
2544 targetStack.resumeTopActivityLocked(null);
2545 }
2546 return ActivityManager.START_DELIVERED_TO_TOP;
2547 }
2548 }
2549 // An existing activity is starting this new activity, so we want
2550 // to keep the new one in the same task as the one that is starting
2551 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002552 r.setTask(sourceTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002553 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002554 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002555
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002556 } else if (inTask != null) {
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002557 // The caller is asking that the new activity be started in an explicit
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002558 // task it has provided to us.
2559 if (isLockTaskModeViolation(inTask)) {
2560 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2561 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2562 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002563 if (overrideBounds) {
2564 inTask.updateOverrideConfiguration(newBounds);
2565 int stackId = inTask.getLaunchStackId();
2566 if (stackId != inTask.stack.mStackId) {
2567 moveTaskToStackUncheckedLocked(
2568 inTask, stackId, ON_TOP, !FORCE_FOCUS, "inTaskToFront");
2569 }
2570 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002571 targetStack = inTask.stack;
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002572 targetStack.moveTaskToFrontLocked(inTask, noAnimation, options, r.appTimeTracker,
2573 "inTaskToFront");
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002574
2575 // Check whether we should actually launch the new activity in to the task,
2576 // or just reuse the current activity on top.
2577 ActivityRecord top = inTask.getTopActivity();
2578 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2579 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2580 || launchSingleTop || launchSingleTask) {
2581 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2582 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2583 // We don't need to start a new activity, and
2584 // the client said not to do anything if that
2585 // is the case, so this is it!
2586 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2587 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002588 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002589 return ActivityManager.START_DELIVERED_TO_TOP;
2590 }
2591 }
2592
Dianne Hackborn962d5352014-08-29 16:27:40 -07002593 if (!addingToTask) {
2594 // We don't actually want to have this activity added to the task, so just
2595 // stop here but still tell the caller that we consumed the intent.
2596 ActivityOptions.abort(options);
2597 return ActivityManager.START_TASK_TO_FRONT;
2598 }
2599
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002600 r.setTask(inTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002601 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002602 + " in explicit task " + r.task);
2603
Craig Mautner8849a5e2013-04-02 16:41:03 -07002604 } else {
2605 // This not being started from an existing activity, and not part
2606 // of a new task... just put it in the top task, though these days
2607 // this case should never happen.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002608 targetStack = computeStackFocus(r, newTask, null /* bounds */);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002609 if (doResume) {
2610 targetStack.moveToFront("addingToTopTask");
2611 }
Craig Mautner1602ec22013-05-12 10:24:27 -07002612 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002613 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002614 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002615 mWindowManager.moveTaskToTop(r.task.taskId);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002616 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Craig Mautner8849a5e2013-04-02 16:41:03 -07002617 + " in new guessed " + r.task);
2618 }
2619
2620 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002621 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002622
Craig Mautner76e2a762014-06-24 09:05:43 -07002623 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2624 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2625 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002626 if (newTask) {
2627 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2628 }
2629 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002630 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002631 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002632 if (!launchTaskBehind && doResume) {
Craig Mautner299f9602015-01-26 09:47:33 -08002633 mService.setFocusedActivityLocked(r, "startedActivity");
Craig Mautnerbb742462014-07-07 15:28:55 -07002634 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002635 updateUserStackLocked(r.userId, targetStack);
Chong Zhangc806d902015-11-30 09:44:27 -08002636
2637 if (!r.task.mResizeable && isStackDockedInEffect(targetStack.mStackId)) {
2638 showNonResizeableDockToast(r.task.taskId);
2639 }
2640
Craig Mautner8849a5e2013-04-02 16:41:03 -07002641 return ActivityManager.START_SUCCESS;
2642 }
2643
Craig Mautneree36c772014-07-16 14:56:05 -07002644 final void doPendingActivityLaunchesLocked(boolean doResume) {
2645 while (!mPendingActivityLaunches.isEmpty()) {
2646 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Robert Carrf1ddb5e2015-10-20 14:44:45 -07002647
2648 try {
2649 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Robert Carr13997f52015-10-23 13:13:39 -07002650 doResume && mPendingActivityLaunches.isEmpty(), null, null);
Robert Carrf1ddb5e2015-10-20 14:44:45 -07002651 } catch (Exception e) {
Robert Carr13997f52015-10-23 13:13:39 -07002652 Slog.e(TAG, "Exception during pending activity launch pal=" + pal, e);
2653 pal.sendErrorResult(e.getMessage());
Robert Carrf1ddb5e2015-10-20 14:44:45 -07002654 }
Craig Mautneree36c772014-07-16 14:56:05 -07002655 }
2656 }
2657
Craig Mautner7f13ed32014-07-28 14:00:35 -07002658 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002659 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2660 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002661 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002662 mPendingActivityLaunches.remove(palNdx);
2663 }
2664 }
2665 }
2666
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002667 void setLaunchSource(int uid) {
2668 mLaunchingActivity.setWorkSource(new WorkSource(uid));
2669 }
2670
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002671 void acquireLaunchWakelock() {
2672 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2673 throw new IllegalStateException("Calling must be system uid");
2674 }
2675 mLaunchingActivity.acquire();
2676 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2677 // To be safe, don't allow the wake lock to be held for too long.
2678 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2679 }
2680 }
2681
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002682 /**
2683 * Called when the frontmost task is idle.
2684 * @return the state of mService.mBooting before this was called.
2685 */
2686 private boolean checkFinishBootingLocked() {
2687 final boolean booting = mService.mBooting;
2688 boolean enableScreen = false;
2689 mService.mBooting = false;
2690 if (!mService.mBooted) {
2691 mService.mBooted = true;
2692 enableScreen = true;
2693 }
2694 if (booting || enableScreen) {
2695 mService.postFinishBooting(booting, enableScreen);
2696 }
2697 return booting;
2698 }
2699
Craig Mautnerf3333272013-04-22 10:55:53 -07002700 // Checked.
2701 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2702 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002703 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002704
Craig Mautnerf3333272013-04-22 10:55:53 -07002705 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07002706 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07002707 int NS = 0;
2708 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002709 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002710 boolean activityRemoved = false;
2711
Wale Ogunwale7d701172015-03-11 15:36:30 -07002712 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002713 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002714 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
2715 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002716 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2717 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002718 if (fromTimeout) {
2719 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002720 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002721
2722 // This is a hack to semi-deal with a race condition
2723 // in the client where it can be constructed with a
2724 // newer configuration from when we asked it to launch.
2725 // We'll update with whatever configuration it now says
2726 // it used to launch.
2727 if (config != null) {
2728 r.configuration = config;
2729 }
2730
2731 // We are now idle. If someone is waiting for a thumbnail from
2732 // us, we can now deliver.
2733 r.idle = true;
2734
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002735 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002736 if (isFocusedStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002737 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002738 }
2739 }
2740
2741 if (allResumedActivitiesIdle()) {
2742 if (r != null) {
2743 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002744 }
2745
2746 if (mLaunchingActivity.isHeld()) {
2747 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2748 if (VALIDATE_WAKE_LOCK_CALLER &&
2749 Binder.getCallingUid() != Process.myUid()) {
2750 throw new IllegalStateException("Calling must be system uid");
2751 }
2752 mLaunchingActivity.release();
2753 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002754 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07002755 }
2756
2757 // Atomically retrieve all of the other things to do.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08002758 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(true);
Craig Mautnerf3333272013-04-22 10:55:53 -07002759 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002760 if ((NF = mFinishingActivities.size()) > 0) {
2761 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002762 mFinishingActivities.clear();
2763 }
2764
Craig Mautnerf3333272013-04-22 10:55:53 -07002765 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002766 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07002767 mStartingUsers.clear();
2768 }
2769
Craig Mautnerf3333272013-04-22 10:55:53 -07002770 // Stop any activities that are scheduled to do so but have been
2771 // waiting for the next one to start.
2772 for (int i = 0; i < NS; i++) {
2773 r = stops.get(i);
2774 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002775 if (stack != null) {
2776 if (r.finishing) {
2777 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2778 } else {
2779 stack.stopActivityLocked(r);
2780 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002781 }
2782 }
2783
2784 // Finish any activities that are scheduled to do so but have been
2785 // waiting for the next one to start.
2786 for (int i = 0; i < NF; i++) {
2787 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07002788 final ActivityStack stack = r.task.stack;
2789 if (stack != null) {
2790 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
2791 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002792 }
2793
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002794 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002795 // Complete user switch
2796 if (startingUsers != null) {
2797 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002798 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002799 }
2800 }
2801 // Complete starting up of background users
2802 if (mStartingBackgroundUsers.size() > 0) {
Amith Yamasani37a40c22015-06-17 13:25:42 -07002803 startingUsers = new ArrayList<UserState>(mStartingBackgroundUsers);
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002804 mStartingBackgroundUsers.clear();
2805 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002806 mService.mUserController.finishUserBoot(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002807 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002808 }
2809 }
2810
2811 mService.trimApplications();
2812 //dump();
2813 //mWindowManager.dump();
2814
Craig Mautnerf3333272013-04-22 10:55:53 -07002815 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002816 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002817 }
2818
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002819 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002820 }
2821
Craig Mautner8e569572013-10-11 17:36:59 -07002822 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002823 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002824 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2825 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002826 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2827 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2828 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002829 }
Craig Mautner19091252013-10-05 00:03:53 -07002830 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002831 }
2832
2833 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002834 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2835 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002836 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2837 stacks.get(stackNdx).closeSystemDialogsLocked();
2838 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002839 }
2840 }
2841
Craig Mautner93529a42013-10-04 15:03:13 -07002842 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002843 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002844 }
2845
Craig Mautner8d341ef2013-03-26 09:03:27 -07002846 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07002847 * Update the last used stack id for non-current user (current user's last
2848 * used stack is the focused stack)
2849 */
2850 void updateUserStackLocked(int userId, ActivityStack stack) {
2851 if (userId != mCurrentUser) {
2852 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
2853 }
2854 }
2855
2856 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07002857 * @return true if some activity was finished (or would have finished if doit were true).
2858 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07002859 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
2860 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002861 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002862 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2863 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08002864 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002865 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07002866 if (stack.finishDisabledPackageActivitiesLocked(
2867 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002868 didSomething = true;
2869 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002870 }
2871 }
2872 return didSomething;
2873 }
2874
Dianne Hackborna413dc02013-07-12 12:02:55 -07002875 void updatePreviousProcessLocked(ActivityRecord r) {
2876 // Now that this process has stopped, we may want to consider
2877 // it to be the previous app to try to keep around in case
2878 // the user wants to return to it.
2879
2880 // First, found out what is currently the foreground app, so that
2881 // we don't blow away the previous app if this activity is being
2882 // hosted by the process that is actually still the foreground.
2883 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002884 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2885 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002886 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2887 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002888 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002889 if (stack.mResumedActivity != null) {
2890 fgApp = stack.mResumedActivity.app;
2891 } else if (stack.mPausingActivity != null) {
2892 fgApp = stack.mPausingActivity.app;
2893 }
2894 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002895 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002896 }
2897 }
2898
2899 // Now set this one as the previous process, only if that really
2900 // makes sense to.
2901 if (r.app != null && fgApp != null && r.app != fgApp
2902 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002903 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002904 mService.mPreviousProcess = r.app;
2905 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2906 }
2907 }
2908
Craig Mautner05d29032013-05-03 13:40:13 -07002909 boolean resumeTopActivitiesLocked() {
2910 return resumeTopActivitiesLocked(null, null, null);
2911 }
2912
Chong Zhang280d3322015-11-03 17:27:26 -08002913 boolean resumeTopActivitiesLocked(
2914 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Craig Mautner05d29032013-05-03 13:40:13 -07002915 if (targetStack == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002916 targetStack = mFocusedStack;
Craig Mautner05d29032013-05-03 13:40:13 -07002917 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002918 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002919 boolean result = false;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002920 if (isFocusedStack(targetStack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002921 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2922 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002923
Craig Mautnere0a38842013-12-16 16:14:02 -08002924 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2925 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002926 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2927 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002928 if (stack == targetStack) {
2929 // Already started above.
2930 continue;
2931 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002932 if (isFocusedStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002933 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002934 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002935 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002936 }
Craig Mautner05d29032013-05-03 13:40:13 -07002937 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002938 }
2939
Todd Kennedy539db512014-12-15 09:57:55 -08002940 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002941 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2942 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002943 final int numStacks = stacks.size();
2944 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2945 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08002946 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002947 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002948 }
2949 }
2950
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002951 void finishVoiceTask(IVoiceInteractionSession session) {
2952 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2953 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2954 final int numStacks = stacks.size();
2955 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2956 final ActivityStack stack = stacks.get(stackNdx);
2957 stack.finishVoiceTask(session);
2958 }
2959 }
2960 }
2961
Chong Zhang280d3322015-11-03 17:27:26 -08002962 void findTaskToMoveToFrontLocked(
2963 TaskRecord task, int flags, ActivityOptions options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002964 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2965 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002966 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002967 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2968 // Caller wants the home activity moved with it. To accomplish this,
2969 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002970 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002971 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07002972 if (task.stack == null) {
2973 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2974 + task + " to front. Stack is null");
2975 return;
2976 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002977
Chong Zhang0fa656b2015-08-31 15:17:21 -07002978 if (task.mResizeable && options != null) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002979 if (canUseActivityOptionsLaunchBounds(options)) {
2980 Rect bounds = options.getLaunchBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07002981 task.updateOverrideConfiguration(bounds);
Chong Zhang112eb8c2015-11-02 11:17:00 -08002982 final int stackId = task.getLaunchStackId();
2983 if (stackId != task.stack.mStackId) {
2984 moveTaskToStackUncheckedLocked(task, stackId, ON_TOP, !FORCE_FOCUS, reason);
2985 // moveTaskToStackUncheckedLocked() should already placed the task on top,
2986 // still need moveTaskToFrontLocked() below for any transition settings.
2987 }
2988 // WM resizeTask must be done after the task is moved to the correct stack,
2989 // because Task's setBounds() also updates dim layer's bounds, but that has
2990 // dependency on the stack.
Chong Zhang87b21722015-09-21 15:39:51 -07002991 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig,
2992 false /*relayout*/, false /*forced*/);
Chong Zhang0fa656b2015-08-31 15:17:21 -07002993 }
2994 }
2995
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002996 final ActivityRecord r = task.getTopActivity();
2997 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
2998 r == null ? null : r.appTimeTracker, reason);
2999
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003000 if (DEBUG_STACK) Slog.d(TAG_STACK,
3001 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003002 }
3003
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08003004 private boolean canUseActivityOptionsLaunchBounds(ActivityOptions options) {
3005 // We use the launch bounds in the activity options is the device supports freeform
3006 // window management.
3007 return options.hasLaunchBounds() && mService.mSupportsFreeformWindowManagement;
3008 }
3009
Craig Mautner967212c2013-04-13 21:10:58 -07003010 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07003011 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003012 }
3013
3014 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003015 ActivityContainer activityContainer = mActivityContainers.get(stackId);
3016 if (activityContainer != null) {
3017 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003018 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07003019 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003020 return null;
3021 }
3022 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003023 }
3024
Craig Mautner967212c2013-04-13 21:10:58 -07003025 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07003026 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003027 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3028 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003029 }
3030 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07003031 }
3032
Craig Mautner4a1cb222013-12-04 16:14:06 -08003033 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003034 ActivityRecord homeActivity = getHomeActivity();
3035 if (homeActivity != null) {
3036 return homeActivity.appToken;
3037 }
3038 return null;
3039 }
3040
3041 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07003042 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07003043 }
3044
3045 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003046 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
3047 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
3048 final TaskRecord task = tasks.get(taskNdx);
3049 if (task.isHomeTask()) {
3050 final ArrayList<ActivityRecord> activities = task.mActivities;
3051 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3052 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07003053 if (r.isHomeActivity()
3054 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003055 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003056 }
3057 }
3058 }
3059 }
3060 return null;
3061 }
3062
Chong Zhangb15758a2015-11-17 12:12:03 -08003063 /**
3064 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
3065 * the docked stack itself, or if it's side-by-side to the docked stack.
3066 */
3067 boolean isStackDockedInEffect(int stackId) {
3068 return stackId == DOCKED_STACK_ID ||
3069 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
3070 }
3071
Todd Kennedyca4d8422015-01-15 15:19:22 -08003072 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08003073 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003074 ActivityContainer activityContainer =
3075 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003076 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003077 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
3078 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003079 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003080 return activityContainer;
3081 }
3082
Craig Mautner34b73df2014-01-12 21:11:08 -08003083 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003084 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
3085 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
3086 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003087 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
3088 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003089 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08003090 }
3091 }
3092
Craig Mautner95da1082014-02-24 17:54:35 -08003093 void deleteActivityContainer(IActivityContainer container) {
3094 ActivityContainer activityContainer = (ActivityContainer)container;
3095 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003096 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
3097 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08003098 final int stackId = activityContainer.mStackId;
3099 mActivityContainers.remove(stackId);
3100 mWindowManager.removeStack(stackId);
3101 }
3102 }
3103
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07003104 void resizeStackLocked(int stackId, Rect bounds, boolean preserveWindows,
3105 boolean allowResizeInDockedMode) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08003106 final ActivityStack stack = getStack(stackId);
3107 if (stack == null) {
3108 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
3109 return;
3110 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003111
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07003112 if (!allowResizeInDockedMode
3113 && stackId != DOCKED_STACK_ID && getStack(DOCKED_STACK_ID) != null) {
3114 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
3115 // stack size changing so things don't get out of sync.
3116 return;
3117 }
3118
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003119 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02003120 mWindowManager.deferSurfaceLayout();
3121 try {
3122 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003123
Jorim Jaggic4025202015-10-22 16:43:34 +02003124 mTmpBounds.clear();
3125 mTmpConfigs.clear();
3126 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3127 for (int i = tasks.size() - 1; i >= 0; i--) {
3128 TaskRecord task = tasks.get(i);
3129 if (task.mResizeable) {
3130 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
3131 // For freeform stack we don't adjust the size of the tasks to match that
3132 // of the stack, but we do try to make sure the tasks are still contained
3133 // with the bounds of the stack.
3134 tempRect2.set(task.mBounds);
3135 fitWithinBounds(tempRect2, bounds);
3136 task.updateOverrideConfiguration(tempRect2);
3137 } else {
3138 task.updateOverrideConfiguration(bounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003139 }
3140 }
3141
Jorim Jaggic4025202015-10-22 16:43:34 +02003142 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
3143 mTmpBounds.put(task.taskId, task.mBounds);
3144 }
3145 stack.mFullscreen = mWindowManager.resizeStack(stackId, bounds, mTmpConfigs, mTmpBounds);
3146 if (stack.mStackId == DOCKED_STACK_ID) {
3147 // Dock stack funness...Yay!
3148 if (stack.mFullscreen) {
3149 // The dock stack went fullscreen which is kinda like dismissing it.
3150 // In this case we make all other static stacks fullscreen and move all
3151 // docked stack tasks to the fullscreen stack.
3152 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale3797c222015-10-27 14:21:58 -07003153 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
Jorim Jaggic4025202015-10-22 16:43:34 +02003154 resizeStackLocked(i, null, preserveWindows, true);
3155 }
3156 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003157
Jorim Jaggic4025202015-10-22 16:43:34 +02003158 final int count = tasks.size();
3159 for (int i = 0; i < count; i++) {
3160 moveTaskToStackLocked(tasks.get(i).taskId,
Jorim Jaggi030979c2015-11-20 15:14:43 -08003161 FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP, FORCE_FOCUS, "resizeStack",
Jorim Jaggi55387522015-11-24 18:21:10 -08003162 false /* animate */);
Jorim Jaggic4025202015-10-22 16:43:34 +02003163 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003164
Jorim Jaggic4025202015-10-22 16:43:34 +02003165 // stack shouldn't contain anymore activities, so nothing to resume.
3166 r = null;
3167 } else {
3168 // Docked stacks occupy a dedicated region on screen so the size of all other
3169 // static stacks need to be adjusted so they don't overlap with the docked stack.
3170 // We get the bounds to use from window manager which has been adjusted for any
3171 // screen controls and is also the same for all stacks.
3172 mWindowManager.getStackDockedModeBounds(HOME_STACK_ID, tempRect);
3173
3174 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale3797c222015-10-27 14:21:58 -07003175 if (StackId.isResizeableByDockedStack(i)) {
Jorim Jaggic4025202015-10-22 16:43:34 +02003176 ActivityStack otherStack = getStack(i);
3177 if (otherStack != null) {
3178 resizeStackLocked(i, tempRect, PRESERVE_WINDOWS, true);
3179 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003180 }
3181 }
3182 }
Jorim Jaggic4025202015-10-22 16:43:34 +02003183 // Since we are resizing the stack, all other operations should strive to preserve
3184 // windows.
3185 preserveWindows = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003186 }
Jorim Jaggic4025202015-10-22 16:43:34 +02003187 stack.setBounds(bounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003188
Jorim Jaggic4025202015-10-22 16:43:34 +02003189 if (r != null) {
3190 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0, preserveWindows);
3191 // And we need to make sure at this point that all other activities
3192 // are made visible with the correct configuration.
3193 ensureActivitiesVisibleLocked(r, 0, preserveWindows);
3194 if (!updated) {
3195 resumeTopActivitiesLocked(stack, null, null);
3196 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003197 }
Jorim Jaggic4025202015-10-22 16:43:34 +02003198 } finally {
3199 mWindowManager.continueSurfaceLayout();
3200 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003201 }
3202 }
3203
Chong Zhang6de2ae82015-09-30 18:25:21 -07003204 void resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003205 if (!task.mResizeable) {
3206 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
3207 return;
3208 }
3209
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003210 adjustForMinimalTaskDimensions(task, bounds);
3211
Chong Zhang87b21722015-09-21 15:39:51 -07003212 // If this is a forced resize, let it go through even if the bounds is not changing,
3213 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07003214 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Chong Zhang87b21722015-09-21 15:39:51 -07003215 if (task.mBounds != null && task.mBounds.equals(bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003216 // Nothing to do here...
3217 return;
3218 }
3219
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003220 if (!mWindowManager.isValidTaskId(task.taskId)) {
3221 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07003222 // All we can do for now is update the bounds so it can be used when the task is
3223 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07003224 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003225 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
3226 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07003227 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003228 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003229 return;
3230 }
3231
Chong Zhang6de2ae82015-09-30 18:25:21 -07003232 // Do not move the task to another stack here.
3233 // This method assumes that the task is already placed in the right stack.
3234 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003235
Chong Zhang6de2ae82015-09-30 18:25:21 -07003236 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07003237
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07003238 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07003239 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003240 // way and the activity was kept the way it was. If it's false, it means the activity had
3241 // to be relaunched due to configuration change.
3242 boolean kept = true;
3243 if (overrideConfig != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003244 ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08003245 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003246 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003247 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003248 // All other activities must be made visible with their correct configuration.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003249 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003250 if (!kept) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08003251 resumeTopActivitiesLocked(stack, null, null);
3252 }
3253 }
3254 }
Chong Zhang87b21722015-09-21 15:39:51 -07003255 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003256
3257 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwale60454db2015-01-23 16:05:07 -08003258 }
3259
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003260 private void adjustForMinimalTaskDimensions(TaskRecord task, Rect bounds) {
3261 if (bounds == null) {
3262 return;
3263 }
3264 int minimalSize = task.mMinimalSize == -1 ? mDefaultMinimalSizeOfResizeableTask
3265 : task.mMinimalSize;
3266 final boolean adjustWidth = minimalSize > bounds.width();
3267 final boolean adjustHeight = minimalSize > bounds.height();
3268 if (!(adjustWidth || adjustHeight)) {
3269 return;
3270 }
3271 Rect taskBounds = task.mBounds;
3272 if (adjustWidth) {
3273 if (taskBounds != null && bounds.right == taskBounds.right) {
3274 bounds.left = bounds.right - minimalSize;
3275 } else {
3276 // Either left bounds match, or neither match, or the previous bounds were
3277 // fullscreen and we default to keeping left.
3278 bounds.right = bounds.left + minimalSize;
3279 }
3280 }
3281 if (adjustHeight) {
3282 if (taskBounds != null && bounds.bottom == taskBounds.bottom) {
3283 bounds.top = bounds.bottom - minimalSize;
3284 } else {
3285 // Either top bounds match, or neither match, or the previous bounds were
3286 // fullscreen and we default to keeping top.
3287 bounds.bottom = bounds.top + minimalSize;
3288 }
3289 }
3290 }
3291
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003292 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003293 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3294 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08003295 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003296 }
3297
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003298 ActivityContainer activityContainer = new ActivityContainer(stackId);
3299 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003300 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08003301 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003302 }
3303
3304 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07003305 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003306 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
3307 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07003308 break;
3309 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003310 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003311 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003312 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003313 }
3314
Chong Zhang5dcb2752015-08-18 13:50:26 -07003315 /**
3316 * Restores a recent task to a stack
3317 * @param task The recent task to be restored.
3318 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale3797c222015-10-27 14:21:58 -07003319 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07003320 * @return true if the task has been restored successfully.
3321 */
3322 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
3323 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07003324 stackId = task.getLaunchStackId();
Chong Zhang5dcb2752015-08-18 13:50:26 -07003325 }
Wale Ogunwale706ed792015-08-02 10:29:44 -07003326 if (task.stack != null) {
3327 // Task has already been restored once. See if we need to do anything more
3328 if (task.stack.mStackId == stackId) {
3329 // Nothing else to do since it is already restored in the right stack.
3330 return true;
3331 }
3332 // Remove current stack association, so we can re-associate the task with the
3333 // right stack below.
Wale Ogunwale040b4702015-08-06 18:10:50 -07003334 task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003335 }
3336
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08003337 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07003338 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003339
3340 if (stack == null) {
3341 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003342 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3343 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003344 return false;
3345 }
3346
3347 stack.addTask(task, false, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003348 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3349 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003350 final ArrayList<ActivityRecord> activities = task.mActivities;
3351 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003352 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003353 }
3354 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07003355 }
3356
Wale Ogunwale040b4702015-08-06 18:10:50 -07003357 /**
3358 * Moves the specified task record to the input stack id.
3359 * WARNING: This method performs an unchecked/raw move of the task and
3360 * can leave the system in an unstable state if used incorrectly.
3361 * Use {@link #moveTaskToStackLocked} to perform safe task movement
3362 * to a stack.
3363 * @param task Task to move.
3364 * @param stackId Id of stack to move task to.
3365 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07003366 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07003367 * @param reason Reason the task is been moved.
3368 * @return The stack the task was moved to.
3369 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07003370 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07003371 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
3372 final ActivityRecord r = task.getTopActivity();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003373 final boolean wasFocused = isFocusedStack(task.stack) && (topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07003374 final boolean wasResumed = wasFocused && (task.stack.mResumedActivity == r);
3375
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07003376 final boolean resizeable = task.mResizeable;
3377 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
3378 // if a docked stack is created below which will lead to the stack we are moving from and
3379 // its resizeable tasks being resized.
3380 task.mResizeable = false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003381 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07003382 task.mResizeable = resizeable;
Wale Ogunwale040b4702015-08-06 18:10:50 -07003383 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
3384 if (task.stack != null) {
3385 task.stack.removeTask(task, reason, MOVING);
3386 }
3387 stack.addTask(task, toTop, MOVING);
Chong Zhang02898352015-08-21 17:27:14 -07003388
3389 // If the task had focus before (or we're requested to move focus),
3390 // move focus to the new stack.
Wale Ogunwale079a0042015-10-24 11:44:07 -07003391 stack.setFocusAndResumeStateIfNeeded(
3392 r, forceFocus || wasFocused, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07003393
Wale Ogunwale040b4702015-08-06 18:10:50 -07003394 return stack;
3395 }
3396
Filip Gruszczynski90186c62015-10-26 14:07:00 -07003397 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08003398 String reason, boolean animate) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003399 final TaskRecord task = anyTaskForIdLocked(taskId);
3400 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08003401 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003402 return;
3403 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08003404
Wale Ogunwale70c65c82015-11-13 13:25:16 -08003405 if (task.stack != null && task.stack.mStackId == stackId) {
3406 // You are already in the right stack silly...
3407 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
3408 return;
3409 }
3410
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08003411 final ActivityRecord topActivity = task.getTopActivity();
3412 if (StackId.preserveWindowOnTaskMove(stackId) && topActivity != null) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07003413 // We are about to relaunch the activity because its configuration changed due to
3414 // being maximized, i.e. size change. The activity will first remove the old window
3415 // and then add a new one. This call will tell window manager about this, so it can
3416 // preserve the old window until the new one is drawn. This prevents having a gap
3417 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07003418 // entrance of the new window to be properly animated.
Jorim Jaggi030979c2015-11-20 15:14:43 -08003419 mWindowManager.setReplacingWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07003420 }
Wale Ogunwale3cd48042015-11-16 13:01:41 -08003421 final ActivityStack stack = moveTaskToStackUncheckedLocked(
3422 task, stackId, toTop, forceFocus, "moveTaskToStack:" + reason);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003423
Jorim Jaggi55387522015-11-24 18:21:10 -08003424 if (!animate) {
3425 stack.mNoAnimActivities.add(topActivity);
3426 }
3427
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003428 // Make sure the task has the appropriate bounds/size for the stack it is in.
3429 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Wale Ogunwale70c65c82015-11-13 13:25:16 -08003430 resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003431 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID
3432 && task.mBounds == null && task.mLastNonFullscreenBounds != null) {
Chong Zhang6de2ae82015-09-30 18:25:21 -07003433 resizeTaskLocked(task, task.mLastNonFullscreenBounds,
3434 RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale99db1862015-10-23 20:08:22 -07003435 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Wale Ogunwale70c65c82015-11-13 13:25:16 -08003436 resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003437 }
3438
Stefan Kuhne54714cd2015-05-12 13:42:18 -07003439 // The task might have already been running and its visibility needs to be synchronized with
3440 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003441 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautner05d29032013-05-03 13:40:13 -07003442 resumeTopActivitiesLocked();
Chong Zhangb15758a2015-11-17 12:12:03 -08003443
3444 if (!task.mResizeable && isStackDockedInEffect(stackId)) {
Chong Zhangc806d902015-11-30 09:44:27 -08003445 showNonResizeableDockToast(taskId);
Chong Zhangb15758a2015-11-17 12:12:03 -08003446 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003447 }
3448
Wale Ogunwale079a0042015-10-24 11:44:07 -07003449 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
3450 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
3451 if (stack == null) {
3452 throw new IllegalArgumentException(
3453 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
3454 }
3455
3456 final ActivityRecord r = stack.topRunningActivityLocked();
3457 if (r == null) {
3458 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
3459 + " in stack=" + stack);
3460 return false;
3461 }
3462
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07003463 if (!r.info.supportsPip) {
3464 Slog.w(TAG,
3465 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Wale Ogunwale079a0042015-10-24 11:44:07 -07003466 + " r=" + r);
3467 return false;
3468 }
3469
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07003470 final TaskRecord task = r.task;
Wale Ogunwale079a0042015-10-24 11:44:07 -07003471 if (task.mActivities.size() == 1) {
3472 // There is only one activity in the task. So, we can just move the task over to the
3473 // pinned stack without re-parenting the activity in a different task.
Filip Gruszczynski90186c62015-10-26 14:07:00 -07003474 moveTaskToStackLocked(task.taskId, PINNED_STACK_ID, ON_TOP, FORCE_FOCUS,
Jorim Jaggi030979c2015-11-20 15:14:43 -08003475 "moveTopActivityToPinnedStack", true /* animate */);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003476 } else {
3477 final ActivityStack pinnedStack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
3478 pinnedStack.moveActivityToStack(r);
3479 }
3480
Filip Gruszczynskia21136c2015-11-24 14:43:31 -08003481 resizeStackLocked(PINNED_STACK_ID, bounds, !PRESERVE_WINDOWS, true);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003482
3483 // The task might have already been running and its visibility needs to be synchronized with
3484 // the visibility of the stack / windows.
3485 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
3486 resumeTopActivitiesLocked();
3487 return true;
3488 }
3489
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003490 void positionTaskInStackLocked(int taskId, int stackId, int position) {
3491 final TaskRecord task = anyTaskForIdLocked(taskId);
3492 if (task == null) {
3493 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
3494 return;
3495 }
Wale Ogunwale935e5022015-11-10 12:36:10 -08003496 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
3497
3498 task.updateOverrideConfigurationForStack(stack);
3499
3500 mWindowManager.positionTaskInStack(
3501 taskId, stackId, position, task.mBounds, task.mOverrideConfig);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003502 final boolean stackChanged = task.stack != null && task.stack != stack;
3503 if (stackChanged) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07003504 task.stack.removeTask(task, "moveTaskToStack", MOVING);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003505 }
3506 stack.positionTask(task, position, stackChanged);
3507 // The task might have already been running and its visibility needs to be synchronized with
3508 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003509 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003510 resumeTopActivitiesLocked();
3511 }
3512
Craig Mautnerac6f8432013-07-17 13:24:59 -07003513 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003514 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003515 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3516 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003517 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3518 final ActivityStack stack = stacks.get(stackNdx);
3519 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003520 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003521 continue;
3522 }
3523 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003524 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
3525 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003526 continue;
3527 }
3528 final ActivityRecord ar = stack.findTaskLocked(r);
3529 if (ar != null) {
3530 return ar;
3531 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003532 }
3533 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003534 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07003535 return null;
3536 }
3537
3538 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003539 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3540 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003541 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3542 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
3543 if (ar != null) {
3544 return ar;
3545 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003546 }
3547 }
3548 return null;
3549 }
3550
Craig Mautner8d341ef2013-03-26 09:03:27 -07003551 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003552 scheduleSleepTimeout();
3553 if (!mGoingToSleep.isHeld()) {
3554 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003555 if (mLaunchingActivity.isHeld()) {
3556 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3557 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003558 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003559 mLaunchingActivity.release();
3560 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003561 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003562 }
Amith Yamasanice15e152013-09-19 12:30:32 -07003563 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003564 }
3565
3566 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003567 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003568
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003569 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003570 final long endTime = System.currentTimeMillis() + timeout;
3571 while (true) {
3572 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003573 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3574 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003575 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3576 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
3577 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003578 }
3579 if (cantShutdown) {
3580 long timeRemaining = endTime - System.currentTimeMillis();
3581 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003582 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003583 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003584 } catch (InterruptedException e) {
3585 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003586 } else {
3587 Slog.w(TAG, "Activity manager shutdown timed out");
3588 timedout = true;
3589 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003590 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003591 } else {
3592 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003593 }
3594 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003595
3596 // Force checkReadyForSleep to complete.
3597 mSleepTimeout = true;
3598 checkReadyForSleepLocked();
3599
Craig Mautner8d341ef2013-03-26 09:03:27 -07003600 return timedout;
3601 }
3602
3603 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003604 removeSleepTimeouts();
3605 if (mGoingToSleep.isHeld()) {
3606 mGoingToSleep.release();
3607 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003608 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3609 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003610 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3611 final ActivityStack stack = stacks.get(stackNdx);
3612 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003613 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003614 resumeTopActivitiesLocked();
3615 }
Craig Mautner5314a402013-09-26 12:40:16 -07003616 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003617 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003618 mGoingToSleepActivities.clear();
3619 }
3620
3621 void activitySleptLocked(ActivityRecord r) {
3622 mGoingToSleepActivities.remove(r);
3623 checkReadyForSleepLocked();
3624 }
3625
3626 void checkReadyForSleepLocked() {
3627 if (!mService.isSleepingOrShuttingDown()) {
3628 // Do not care.
3629 return;
3630 }
3631
3632 if (!mSleepTimeout) {
3633 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003634 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3635 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003636 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3637 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3638 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003639 }
3640
3641 if (mStoppingActivities.size() > 0) {
3642 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003643 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003644 + mStoppingActivities.size() + " activities");
3645 scheduleIdleLocked();
3646 dontSleep = true;
3647 }
3648
3649 if (mGoingToSleepActivities.size() > 0) {
3650 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003651 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003652 + mGoingToSleepActivities.size() + " activities");
3653 dontSleep = true;
3654 }
3655
3656 if (dontSleep) {
3657 return;
3658 }
3659 }
3660
Craig Mautnere0a38842013-12-16 16:14:02 -08003661 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3662 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003663 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3664 stacks.get(stackNdx).goToSleep();
3665 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003666 }
3667
3668 removeSleepTimeouts();
3669
3670 if (mGoingToSleep.isHeld()) {
3671 mGoingToSleep.release();
3672 }
3673 if (mService.mShuttingDown) {
3674 mService.notifyAll();
3675 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003676 }
3677
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003678 boolean reportResumedActivityLocked(ActivityRecord r) {
3679 final ActivityStack stack = r.task.stack;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003680 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003681 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003682 }
3683 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003684 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003685 mWindowManager.executeAppTransition();
3686 return true;
3687 }
3688 return false;
3689 }
3690
Craig Mautner8d341ef2013-03-26 09:03:27 -07003691 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003692 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3693 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003694 int stackNdx = stacks.size() - 1;
3695 while (stackNdx >= 0) {
3696 stacks.get(stackNdx).handleAppCrashLocked(app);
3697 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003698 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003699 }
3700 }
3701
Jose Lima4b6c6692014-08-12 17:41:12 -07003702 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003703 final ActivityStack stack = r.task.stack;
3704 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003705 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3706 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003707 return false;
3708 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003709 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003710 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3711 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003712
3713 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003714 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003715 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003716 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003717 return true;
3718 }
3719
3720 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003721 if (visible && top.fullscreen) {
3722 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003723 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3724 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3725 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3726 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003727 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003728 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3729 // Only the activity set as currently visible behind should actively reset its
3730 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003731 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3732 "requestVisibleBehind: returning visible=" + visible
3733 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3734 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003735 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003736 }
3737
Jose Lima4b6c6692014-08-12 17:41:12 -07003738 stack.setVisibleBehindActivity(visible ? r : null);
3739 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003740 // Make the activity immediately above r opaque.
3741 final ActivityRecord next = stack.findNextTranslucentActivity(r);
3742 if (next != null) {
3743 mService.convertFromTranslucent(next.appToken);
3744 }
3745 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003746 if (top.app != null && top.app.thread != null) {
3747 // Notify the top app of the change.
3748 try {
3749 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3750 } catch (RemoteException e) {
3751 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003752 }
3753 return true;
3754 }
3755
Craig Mautnerbb742462014-07-07 15:28:55 -07003756 // Called when WindowManager has finished animating the launchingBehind activity to the back.
3757 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
3758 r.mLaunchTaskBehind = false;
3759 final TaskRecord task = r.task;
Winson8b1871d2015-11-20 09:56:20 -08003760 task.setLastThumbnailLocked(task.stack.screenshotActivitiesLocked(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003761 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08003762 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07003763 mWindowManager.setAppVisibility(r.appToken, false);
3764 }
3765
3766 void scheduleLaunchTaskBehindComplete(IBinder token) {
3767 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3768 }
3769
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003770 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3771 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07003772 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08003773 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3774 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003775 final int topStackNdx = stacks.size() - 1;
3776 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3777 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003778 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07003779 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003780 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003781 }
3782
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003783 void invalidateTaskLayers() {
3784 mTaskLayersChanged = true;
3785 }
3786
3787 void rankTaskLayersIfNeeded() {
3788 if (!mTaskLayersChanged) {
3789 return;
3790 }
3791 mTaskLayersChanged = false;
3792 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
3793 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3794 int baseLayer = 0;
3795 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3796 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
3797 }
3798 }
3799 }
3800
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003801 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3802 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3803 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3804 final int topStackNdx = stacks.size() - 1;
3805 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3806 final ActivityStack stack = stacks.get(stackNdx);
3807 stack.clearOtherAppTimeTrackers(except);
3808 }
3809 }
3810 }
3811
Craig Mautner8d341ef2013-03-26 09:03:27 -07003812 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003813 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3814 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003815 final int numStacks = stacks.size();
3816 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3817 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003818 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003819 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003820 }
3821 }
3822
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003823 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3824 // Examine all activities currently running in the process.
3825 TaskRecord firstTask = null;
3826 // Tasks is non-null only if two or more tasks are found.
3827 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003828 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3829 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003830 ActivityRecord r = app.activities.get(i);
3831 // First, if we find an activity that is in the process of being destroyed,
3832 // then we just aren't going to do anything for now; we want things to settle
3833 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003834 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003835 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003836 return;
3837 }
3838 // Don't consider any activies that are currently not in a state where they
3839 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003840 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3841 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003842 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003843 continue;
3844 }
3845 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003846 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003847 + " from " + r);
3848 if (firstTask == null) {
3849 firstTask = r.task;
3850 } else if (firstTask != r.task) {
3851 if (tasks == null) {
3852 tasks = new ArraySet<>();
3853 tasks.add(firstTask);
3854 }
3855 tasks.add(r.task);
3856 }
3857 }
3858 }
3859 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003860 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003861 return;
3862 }
3863 // If we have activities in multiple tasks that are in a position to be destroyed,
3864 // let's iterate through the tasks and release the oldest one.
3865 final int numDisplays = mActivityDisplays.size();
3866 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3867 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3868 // Step through all stacks starting from behind, to hit the oldest things first.
3869 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3870 final ActivityStack stack = stacks.get(stackNdx);
3871 // Try to release activities in this stack; if we manage to, we are done.
3872 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3873 return;
3874 }
3875 }
3876 }
3877 }
3878
Amith Yamasani37a40c22015-06-17 13:25:42 -07003879 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003880 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003881 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003882 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003883
Craig Mautner858d8a62013-04-23 17:08:34 -07003884 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003885 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3886 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003887 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003888 final ActivityStack stack = stacks.get(stackNdx);
3889 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003890 TaskRecord task = stack.topTask();
3891 if (task != null) {
3892 mWindowManager.moveTaskToTop(task.taskId);
3893 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003894 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003895 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003896
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003897 ActivityStack stack = getStack(restoreStackId);
3898 if (stack == null) {
3899 stack = mHomeStack;
3900 }
3901 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003902 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003903 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003904 } else {
3905 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003906 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003907 }
Craig Mautner93529a42013-10-04 15:03:13 -07003908 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003909 }
3910
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003911 /**
3912 * Add background users to send boot completed events to.
3913 * @param userId The user being started in the background
3914 * @param uss The state object for the user.
3915 */
Amith Yamasani37a40c22015-06-17 13:25:42 -07003916 public void startBackgroundUserLocked(int userId, UserState uss) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003917 mStartingBackgroundUsers.add(uss);
3918 }
3919
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003920 /** Checks whether the userid is a profile of the current user. */
3921 boolean isCurrentProfileLocked(int userId) {
3922 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003923 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003924 }
3925
Chong Zhang45c25ce2015-08-10 22:18:26 -07003926 /** Checks whether the activity should be shown for current user. */
3927 boolean okToShowLocked(ActivityRecord r) {
3928 return r != null && (isCurrentProfileLocked(r.userId)
3929 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
3930 }
3931
Craig Mautnerde4ef022013-04-07 19:01:33 -07003932 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003933 ArrayList<ActivityRecord> stops = null;
3934
3935 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003936 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3937 ActivityRecord s = mStoppingActivities.get(activityNdx);
3938 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003939 if (DEBUG_ALL) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003940 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3941 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003942 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003943 if (s.finishing) {
3944 // If this activity is finishing, it is sitting on top of
3945 // everyone else but we now know it is no longer needed...
3946 // so get rid of it. Otherwise, we need to go through the
3947 // normal flow and hide it once we determine that it is
3948 // hidden by the activities in front of it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003949 if (DEBUG_ALL) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003950 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003951 }
3952 }
Craig Mautner8c14c152015-01-15 17:32:07 -08003953 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003954 if (DEBUG_ALL) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003955 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003956 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003957 }
3958 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003959 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003960 }
3961 }
3962
3963 return stops;
3964 }
3965
Craig Mautnercf910b02013-04-23 11:23:27 -07003966 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003967 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3968 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3969 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3970 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003971 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003972 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003973 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003974 if (r == null) Slog.e(TAG,
3975 "validateTop...: null top activity, stack=" + stack);
3976 else {
3977 final ActivityRecord pausing = stack.mPausingActivity;
3978 if (pausing != null && pausing == r) Slog.e(TAG,
3979 "validateTop...: top stack has pausing activity r=" + r
3980 + " state=" + state);
3981 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3982 "validateTop...: activity in front not resumed r=" + r
3983 + " state=" + state);
3984 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003985 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003986 final ActivityRecord resumed = stack.mResumedActivity;
3987 if (resumed != null && resumed == r) Slog.e(TAG,
3988 "validateTop...: back stack has resumed activity r=" + r
3989 + " state=" + state);
3990 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3991 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003992 }
3993 }
3994 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003995 }
3996
Craig Mautnere0570202015-05-13 13:06:11 -07003997 private String lockTaskModeToString() {
3998 switch (mLockTaskModeState) {
3999 case LOCK_TASK_MODE_LOCKED:
4000 return "LOCKED";
4001 case LOCK_TASK_MODE_PINNED:
4002 return "PINNED";
4003 case LOCK_TASK_MODE_NONE:
4004 return "NONE";
4005 default: return "unknown=" + mLockTaskModeState;
4006 }
4007 }
4008
Craig Mautner27084302013-03-25 08:05:25 -07004009 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07004010 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004011 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07004012 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
4013 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
4014 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08004015 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07004016 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
4017 final SparseArray<String[]> packages = mService.mLockTaskPackages;
4018 if (packages.size() > 0) {
4019 pw.println(" mLockTaskPackages (userId:packages)=");
4020 for (int i = 0; i < packages.size(); ++i) {
4021 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
4022 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
4023 }
4024 }
4025 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07004026 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07004027
Craig Mautner20e72272013-04-01 13:45:53 -07004028 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004029 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07004030 }
4031
Dianne Hackborn390517b2013-05-30 15:03:32 -07004032 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
4033 boolean needSep, String prefix) {
4034 if (activity != null) {
4035 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
4036 if (needSep) {
4037 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07004038 }
4039 pw.print(prefix);
4040 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004041 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07004042 }
4043 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004044 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07004045 }
4046
Craig Mautner8d341ef2013-03-26 09:03:27 -07004047 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
4048 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004049 boolean printed = false;
4050 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08004051 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
4052 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004053 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07004054 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08004055 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07004056 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004057 final ActivityStack stack = stacks.get(stackNdx);
4058 StringBuilder stackHeader = new StringBuilder(128);
4059 stackHeader.append(" Stack #");
4060 stackHeader.append(stack.mStackId);
4061 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07004062 stackHeader.append("\n");
4063 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
4064 stackHeader.append("\n");
4065 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004066 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
4067 needSep, stackHeader.toString());
4068 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
4069 !dumpAll, false, dumpPackage, true,
4070 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07004071
Craig Mautner4a1cb222013-12-04 16:14:06 -08004072 needSep = printed;
4073 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
4074 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004075 if (pr) {
4076 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004077 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004078 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004079 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
4080 " mResumedActivity: ");
4081 if (pr) {
4082 printed = true;
4083 needSep = false;
4084 }
4085 if (dumpAll) {
4086 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
4087 " mLastPausedActivity: ");
4088 if (pr) {
4089 printed = true;
4090 needSep = true;
4091 }
4092 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
4093 needSep, " mLastNoHistoryActivity: ");
4094 }
4095 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004096 }
4097 }
4098
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004099 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
4100 false, dumpPackage, true, " Activities waiting to finish:", null);
4101 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
4102 false, dumpPackage, true, " Activities waiting to stop:", null);
4103 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
4104 false, dumpPackage, true, " Activities waiting for another to become visible:",
4105 null);
4106 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
4107 false, dumpPackage, true, " Activities waiting to sleep:", null);
4108 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
4109 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004110
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004111 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004112 }
4113
Dianne Hackborn390517b2013-05-30 15:03:32 -07004114 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07004115 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004116 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07004117 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07004118 String innerPrefix = null;
4119 String[] args = null;
4120 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004121 for (int i=list.size()-1; i>=0; i--) {
4122 final ActivityRecord r = list.get(i);
4123 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
4124 continue;
4125 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004126 if (innerPrefix == null) {
4127 innerPrefix = prefix + " ";
4128 args = new String[0];
4129 }
4130 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004131 final boolean full = !brief && (complete || !r.isInHistory());
4132 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004133 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07004134 needNL = false;
4135 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004136 if (header1 != null) {
4137 pw.println(header1);
4138 header1 = null;
4139 }
4140 if (header2 != null) {
4141 pw.println(header2);
4142 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07004143 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07004144 if (lastTask != r.task) {
4145 lastTask = r.task;
4146 pw.print(prefix);
4147 pw.print(full ? "* " : " ");
4148 pw.println(lastTask);
4149 if (full) {
4150 lastTask.dump(pw, prefix + " ");
4151 } else if (complete) {
4152 // Complete + brief == give a summary. Isn't that obvious?!?
4153 if (lastTask.intent != null) {
4154 pw.print(prefix); pw.print(" ");
4155 pw.println(lastTask.intent.toInsecureStringWithClip());
4156 }
4157 }
4158 }
4159 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
4160 pw.print(" #"); pw.print(i); pw.print(": ");
4161 pw.println(r);
4162 if (full) {
4163 r.dump(pw, innerPrefix);
4164 } else if (complete) {
4165 // Complete + brief == give a summary. Isn't that obvious?!?
4166 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
4167 if (r.app != null) {
4168 pw.print(innerPrefix); pw.println(r.app);
4169 }
4170 }
4171 if (client && r.app != null && r.app.thread != null) {
4172 // flush anything that is already in the PrintWriter since the thread is going
4173 // to write to the file descriptor directly
4174 pw.flush();
4175 try {
4176 TransferPipe tp = new TransferPipe();
4177 try {
4178 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
4179 r.appToken, innerPrefix, args);
4180 // Short timeout, since blocking here can
4181 // deadlock with the application.
4182 tp.go(fd, 2000);
4183 } finally {
4184 tp.kill();
4185 }
4186 } catch (IOException e) {
4187 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
4188 } catch (RemoteException e) {
4189 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
4190 }
4191 needNL = true;
4192 }
4193 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004194 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004195 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004196
Craig Mautnerf3333272013-04-22 10:55:53 -07004197 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004198 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4199 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07004200 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
4201 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07004202 }
4203
4204 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07004205 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07004206 }
4207
4208 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004209 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
4210 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07004211 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
4212 }
4213
Craig Mautner05d29032013-05-03 13:40:13 -07004214 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08004215 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
4216 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
4217 }
Craig Mautner05d29032013-05-03 13:40:13 -07004218 }
4219
Craig Mautner0eea92c2013-05-16 13:35:39 -07004220 void removeSleepTimeouts() {
4221 mSleepTimeout = false;
4222 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
4223 }
4224
4225 final void scheduleSleepTimeout() {
4226 removeSleepTimeouts();
4227 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
4228 }
4229
Craig Mautner4a1cb222013-12-04 16:14:06 -08004230 @Override
4231 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004232 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004233 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
4234 }
4235
4236 @Override
4237 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004238 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004239 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
4240 }
4241
4242 @Override
4243 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004244 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004245 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
4246 }
4247
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004248 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08004249 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004250 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08004251 newDisplay = mActivityDisplays.get(displayId) == null;
4252 if (newDisplay) {
4253 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07004254 if (activityDisplay.mDisplay == null) {
4255 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
4256 return;
4257 }
Craig Mautner4504de52013-12-20 09:06:56 -08004258 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004259 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08004260 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004261 }
Craig Mautner4504de52013-12-20 09:06:56 -08004262 if (newDisplay) {
4263 mWindowManager.onDisplayAdded(displayId);
4264 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004265 }
4266
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004267 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
4268 if (display.mDisplayId != Display.DEFAULT_DISPLAY) {
4269 return;
4270 }
4271 final float fraction = mService.mContext.getResources().getFraction(com.android.internal.R.
4272 fraction.config_displayFractionForDefaultMinimalSizeOfResizeableTask, 1, 1);
4273 mDefaultMinimalSizeOfResizeableTask = (int) (fraction * Math.min(
4274 display.mDisplayInfo.logicalWidth, display.mDisplayInfo.logicalHeight));
4275 }
4276
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004277 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004278 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004279 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4280 if (activityDisplay != null) {
4281 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004282 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004283 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004284 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004285 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004286 }
4287 }
4288 mWindowManager.onDisplayRemoved(displayId);
4289 }
4290
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004291 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004292 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004293 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4294 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004295 // TODO: Update the bounds.
4296 }
4297 }
4298 mWindowManager.onDisplayChanged(displayId);
4299 }
4300
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004301 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004302 StackInfo info = new StackInfo();
4303 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
4304 info.displayId = Display.DEFAULT_DISPLAY;
4305 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004306 info.userId = stack.mCurrentUser;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004307
4308 ArrayList<TaskRecord> tasks = stack.getAllTasks();
4309 final int numTasks = tasks.size();
4310 int[] taskIds = new int[numTasks];
4311 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004312 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004313 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08004314 for (int i = 0; i < numTasks; ++i) {
4315 final TaskRecord task = tasks.get(i);
4316 taskIds[i] = task.taskId;
4317 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
4318 : task.realActivity != null ? task.realActivity.flattenToString()
4319 : task.getTopActivity() != null ? task.getTopActivity().packageName
4320 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004321 taskBounds[i] = new Rect();
4322 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004323 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004324 }
4325 info.taskIds = taskIds;
4326 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07004327 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004328 info.taskUserIds = taskUserIds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004329 return info;
4330 }
4331
4332 StackInfo getStackInfoLocked(int stackId) {
4333 ActivityStack stack = getStack(stackId);
4334 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004335 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004336 }
4337 return null;
4338 }
4339
4340 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004341 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08004342 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
4343 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004344 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004345 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08004346 }
4347 }
4348 return list;
4349 }
4350
Craig Mautner15df08a2015-04-01 12:17:18 -07004351 TaskRecord getLockedTaskLocked() {
4352 final int top = mLockTaskModeTasks.size() - 1;
4353 if (top >= 0) {
4354 return mLockTaskModeTasks.get(top);
4355 }
4356 return null;
4357 }
4358
4359 boolean isLockedTask(TaskRecord task) {
4360 return mLockTaskModeTasks.contains(task);
4361 }
4362
4363 boolean isLastLockedTask(TaskRecord task) {
4364 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
4365 }
4366
4367 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07004368 if (!mLockTaskModeTasks.remove(task)) {
4369 return;
4370 }
4371 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
4372 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004373 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07004374 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
4375 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07004376 final Message lockTaskMsg = Message.obtain();
4377 lockTaskMsg.arg1 = task.userId;
4378 lockTaskMsg.what = LOCK_TASK_END_MSG;
4379 mHandler.sendMessage(lockTaskMsg);
4380 }
4381 }
4382
Chong Zhangc806d902015-11-30 09:44:27 -08004383 private void showNonResizeableDockToast(int taskId) {
4384 mWindowManager.scheduleShowNonResizeableDockToast(taskId);
Chong Zhangb15758a2015-11-17 12:12:03 -08004385 }
4386
Craig Mautner6cd6cec2015-04-01 00:02:12 -07004387 void showLockTaskToast() {
4388 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04004389 }
4390
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004391 void showLockTaskEscapeMessageLocked(TaskRecord task) {
4392 if (mLockTaskModeTasks.contains(task)) {
4393 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
4394 }
4395 }
4396
Craig Mautner432f64e2015-05-20 14:59:57 -07004397 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
4398 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004399 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07004400 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07004401 final TaskRecord lockedTask = getLockedTaskLocked();
4402 if (lockedTask != null) {
4403 removeLockedTaskLocked(lockedTask);
4404 if (!mLockTaskModeTasks.isEmpty()) {
4405 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07004406 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4407 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07004408 lockedTask.performClearTaskLocked();
4409 resumeTopActivitiesLocked();
4410 return;
4411 }
Christopher Tate3bd90612014-06-18 16:37:52 -07004412 }
Craig Mautnere0570202015-05-13 13:06:11 -07004413 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4414 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08004415 return;
4416 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004417
4418 // Should have already been checked, but do it again.
4419 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07004420 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4421 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08004422 return;
4423 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004424 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07004425 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07004426 return;
4427 }
4428
4429 if (mLockTaskModeTasks.isEmpty()) {
4430 // First locktask.
4431 final Message lockTaskMsg = Message.obtain();
4432 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
4433 lockTaskMsg.arg1 = task.userId;
4434 lockTaskMsg.what = LOCK_TASK_START_MSG;
4435 lockTaskMsg.arg2 = lockTaskModeState;
4436 mHandler.sendMessage(lockTaskMsg);
4437 }
4438 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07004439 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
4440 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07004441 mLockTaskModeTasks.remove(task);
4442 mLockTaskModeTasks.add(task);
4443
4444 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07004445 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07004446 }
Craig Mautner432f64e2015-05-20 14:59:57 -07004447
4448 if (andResume) {
4449 findTaskToMoveToFrontLocked(task, 0, null, reason);
4450 resumeTopActivitiesLocked();
4451 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004452 }
4453
4454 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04004455 return isLockTaskModeViolation(task, false);
4456 }
4457
4458 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
4459 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004460 return false;
4461 }
4462 final int lockTaskAuth = task.mLockTaskAuth;
4463 switch (lockTaskAuth) {
4464 case LOCK_TASK_AUTH_DONT_LOCK:
4465 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01004466 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07004467 case LOCK_TASK_AUTH_LAUNCHABLE:
4468 case LOCK_TASK_AUTH_WHITELISTED:
4469 return false;
4470 case LOCK_TASK_AUTH_PINNABLE:
4471 // Pinnable tasks can't be launched on top of locktask tasks.
4472 return !mLockTaskModeTasks.isEmpty();
4473 default:
4474 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
4475 return true;
4476 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004477 }
4478
Craig Mautner15df08a2015-04-01 12:17:18 -07004479 void onLockTaskPackagesUpdatedLocked() {
4480 boolean didSomething = false;
4481 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
4482 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01004483 final boolean wasWhitelisted =
4484 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4485 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07004486 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01004487 final boolean isWhitelisted =
4488 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4489 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
4490 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004491 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07004492 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
4493 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07004494 removeLockedTaskLocked(lockedTask);
4495 lockedTask.performClearTaskLocked();
4496 didSomething = true;
4497 }
4498 }
4499 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4500 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4501 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4502 final ActivityStack stack = stacks.get(stackNdx);
4503 stack.onLockTaskPackagesUpdatedLocked();
4504 }
4505 }
Craig Mautnere0570202015-05-13 13:06:11 -07004506 final ActivityRecord r = topRunningActivityLocked();
4507 final TaskRecord task = r != null ? r.task : null;
4508 if (mLockTaskModeTasks.isEmpty() && task != null
4509 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4510 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004511 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4512 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4513 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4514 false);
4515 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004516 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004517 if (didSomething) {
4518 resumeTopActivitiesLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004519 }
4520 }
4521
Benjamin Franz43261142015-02-11 15:59:44 +00004522 int getLockTaskModeState() {
4523 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004524 }
4525
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004526 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004527
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004528 public ActivityStackSupervisorHandler(Looper looper) {
4529 super(looper);
4530 }
4531
Craig Mautnerf3333272013-04-22 10:55:53 -07004532 void activityIdleInternal(ActivityRecord r) {
4533 synchronized (mService) {
4534 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
4535 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004536 }
4537
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004538 @Override
4539 public void handleMessage(Message msg) {
4540 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004541 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004542 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4543 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004544 if (mService.mDidDexOpt) {
4545 mService.mDidDexOpt = false;
4546 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4547 nmsg.obj = msg.obj;
4548 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4549 return;
4550 }
4551 // We don't at this point know if the activity is fullscreen,
4552 // so we need to be conservative and assume it isn't.
4553 activityIdleInternal((ActivityRecord)msg.obj);
4554 } break;
4555 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004556 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004557 activityIdleInternal((ActivityRecord)msg.obj);
4558 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004559 case RESUME_TOP_ACTIVITY_MSG: {
4560 synchronized (mService) {
4561 resumeTopActivitiesLocked();
4562 }
4563 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004564 case SLEEP_TIMEOUT_MSG: {
4565 synchronized (mService) {
4566 if (mService.isSleepingOrShuttingDown()) {
4567 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4568 mSleepTimeout = true;
4569 checkReadyForSleepLocked();
4570 }
4571 }
4572 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004573 case LAUNCH_TIMEOUT_MSG: {
4574 if (mService.mDidDexOpt) {
4575 mService.mDidDexOpt = false;
4576 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4577 return;
4578 }
4579 synchronized (mService) {
4580 if (mLaunchingActivity.isHeld()) {
4581 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4582 if (VALIDATE_WAKE_LOCK_CALLER
4583 && Binder.getCallingUid() != Process.myUid()) {
4584 throw new IllegalStateException("Calling must be system uid");
4585 }
4586 mLaunchingActivity.release();
4587 }
4588 }
4589 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004590 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004591 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004592 } break;
4593 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004594 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004595 } break;
4596 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004597 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004598 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004599 case CONTAINER_CALLBACK_VISIBILITY: {
4600 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004601 final IActivityContainerCallback callback = container.mCallback;
4602 if (callback != null) {
4603 try {
4604 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4605 } catch (RemoteException e) {
4606 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004607 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004608 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004609 case LOCK_TASK_START_MSG: {
4610 // When lock task starts, we disable the status bars.
4611 try {
Jason Monk62515be2014-05-21 16:06:19 -04004612 if (mLockTaskNotify == null) {
4613 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004614 }
Jason Monk62515be2014-05-21 16:06:19 -04004615 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004616 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004617 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004618 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004619 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004620 flags = StatusBarManager.DISABLE_MASK
4621 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004622 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004623 flags = StatusBarManager.DISABLE_MASK
4624 & (~StatusBarManager.DISABLE_BACK)
4625 & (~StatusBarManager.DISABLE_HOME)
4626 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004627 }
4628 getStatusBarService().disable(flags, mToken,
4629 mService.mContext.getPackageName());
4630 }
4631 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004632 if (getDevicePolicyManager() != null) {
4633 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004634 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004635 }
justinzhang5286d3f2014-05-12 17:06:01 -04004636 } catch (RemoteException ex) {
4637 throw new RuntimeException(ex);
4638 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004639 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004640 case LOCK_TASK_END_MSG: {
4641 // When lock task ends, we enable the status bars.
4642 try {
Jason Monk62515be2014-05-21 16:06:19 -04004643 if (getStatusBarService() != null) {
4644 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4645 mService.mContext.getPackageName());
4646 }
4647 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004648 if (getDevicePolicyManager() != null) {
4649 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4650 msg.arg1);
4651 }
Jason Monk62515be2014-05-21 16:06:19 -04004652 if (mLockTaskNotify == null) {
4653 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4654 }
4655 mLockTaskNotify.show(false);
4656 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004657 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004658 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004659 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004660 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004661 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04004662 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04004663 new LockPatternUtils(mService.mContext)
4664 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004665 }
4666 } catch (SettingNotFoundException e) {
4667 // No setting, don't lock.
4668 }
justinzhang5286d3f2014-05-12 17:06:01 -04004669 } catch (RemoteException ex) {
4670 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004671 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004672 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004673 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004674 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004675 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4676 if (mLockTaskNotify == null) {
4677 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4678 }
4679 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4680 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004681 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4682 final ActivityContainer container = (ActivityContainer) msg.obj;
4683 final IActivityContainerCallback callback = container.mCallback;
4684 if (callback != null) {
4685 try {
4686 callback.onAllActivitiesComplete(container.asBinder());
4687 } catch (RemoteException e) {
4688 }
4689 }
4690 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004691 case LAUNCH_TASK_BEHIND_COMPLETE: {
4692 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004693 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004694 if (r != null) {
4695 handleLaunchTaskBehindCompleteLocked(r);
4696 }
4697 }
4698 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004699
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004700 }
4701 }
4702 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004703
Ying Wangb081a592014-04-22 15:20:16 -07004704 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07004705 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07004706 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004707 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004708 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004709 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004710 ActivityRecord mParentActivity = null;
4711 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004712
Craig Mautnere3a00d72014-04-16 08:31:19 -07004713 boolean mVisible = true;
4714
Craig Mautner4a1cb222013-12-04 16:14:06 -08004715 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004716 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004717
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004718 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4719 final static int CONTAINER_STATE_NO_SURFACE = 1;
4720 final static int CONTAINER_STATE_FINISHING = 2;
4721 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4722
4723 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004724 synchronized (mService) {
4725 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004726 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004727 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004728 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004729 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004730 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004731
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004732 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004733 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004734 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08004735 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004736 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004737 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004738 }
4739
4740 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004741 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004742 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004743 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4744 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004745 return;
4746 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004747 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004748 }
4749 }
4750
4751 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004752 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004753 synchronized (mService) {
4754 if (mActivityDisplay != null) {
4755 return mActivityDisplay.mDisplayId;
4756 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004757 }
4758 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004759 }
4760
Jeff Brownca9bc702014-02-11 14:32:56 -08004761 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004762 public int getStackId() {
4763 synchronized (mService) {
4764 return mStackId;
4765 }
4766 }
4767
4768 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004769 public boolean injectEvent(InputEvent event) {
4770 final long origId = Binder.clearCallingIdentity();
4771 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004772 synchronized (mService) {
4773 if (mActivityDisplay != null) {
4774 return mInputManagerInternal.injectInputEvent(event,
4775 mActivityDisplay.mDisplayId,
4776 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4777 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004778 }
4779 return false;
4780 } finally {
4781 Binder.restoreCallingIdentity(origId);
4782 }
4783 }
4784
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004785 @Override
4786 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004787 synchronized (mService) {
4788 if (mContainerState == CONTAINER_STATE_FINISHING) {
4789 return;
4790 }
4791 mContainerState = CONTAINER_STATE_FINISHING;
4792
Craig Mautnerd163e752014-06-13 17:18:47 -07004793 long origId = Binder.clearCallingIdentity();
4794 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004795 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07004796 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004797 } finally {
4798 Binder.restoreCallingIdentity(origId);
4799 }
4800 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004801 }
4802
Craig Mautner60257702014-09-17 15:02:33 -07004803 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004804 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004805 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004806 if (mActivityDisplay != null) {
4807 mActivityDisplay.detachActivitiesLocked(mStack);
4808 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004809 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004810 }
4811 }
4812
4813 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004814 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004815 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004816 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004817 Binder.getCallingUid(), mCurrentUser, false,
4818 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004819
Craig Mautnere0a38842013-12-16 16:14:02 -08004820 // TODO: Switch to user app stacks here.
4821 String mimeType = intent.getType();
Craig Mautnerb9168362015-02-26 20:40:19 -08004822 final Uri data = intent.getData();
4823 if (mimeType == null && data != null && "content".equals(data.getScheme())) {
4824 mimeType = mService.getProviderMimeType(data, userId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004825 }
Craig Mautnerb9168362015-02-26 20:40:19 -08004826 checkEmbeddedAllowedInner(userId, intent, mimeType);
4827
4828 intent.addFlags(FORCE_NEW_TASK_FLAGS);
4829 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07004830 0, 0, null, null, null, null, false, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004831 }
4832
4833 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004834 public final int startActivityIntentSender(IIntentSender intentSender)
4835 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004836 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4837
4838 if (!(intentSender instanceof PendingIntentRecord)) {
4839 throw new IllegalArgumentException("Bad PendingIntent object");
4840 }
4841
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004842 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004843 Binder.getCallingUid(), mCurrentUser, false,
4844 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004845
4846 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4847 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4848 pendingIntent.key.requestResolvedType);
4849
4850 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4851 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4852 }
4853
4854 private void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004855 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004856 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004857 throw new SecurityException(
4858 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4859 }
4860 }
4861
Craig Mautnerdf88d732014-01-27 09:21:32 -08004862 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004863 public IBinder asBinder() {
4864 return this;
4865 }
4866
Craig Mautner4504de52013-12-20 09:06:56 -08004867 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004868 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004869 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004870 }
4871
Craig Mautner4a1cb222013-12-04 16:14:06 -08004872 ActivityStackSupervisor getOuter() {
4873 return ActivityStackSupervisor.this;
4874 }
4875
Craig Mautnerd163e752014-06-13 17:18:47 -07004876 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004877 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004878 }
4879
Craig Mautner6985bad2014-04-21 15:22:06 -07004880 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004881 void setVisible(boolean visible) {
4882 if (mVisible != visible) {
4883 mVisible = visible;
4884 if (mCallback != null) {
4885 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4886 0 /* unused */, this).sendToTarget();
4887 }
4888 }
4889 }
4890
Craig Mautner6985bad2014-04-21 15:22:06 -07004891 void setDrawn() {
4892 }
4893
Craig Mautner1b4bf852014-05-26 15:06:32 -07004894 // You can always start a new task on a regular ActivityStack.
4895 boolean isEligibleForNewTasks() {
4896 return true;
4897 }
4898
Craig Mautnerd163e752014-06-13 17:18:47 -07004899 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004900 detachLocked();
4901 deleteActivityContainer(this);
4902 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004903 }
4904
Craig Mautner34b73df2014-01-12 21:11:08 -08004905 @Override
4906 public String toString() {
4907 return mIdString + (mActivityDisplay == null ? "N" : "A");
4908 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004909 }
4910
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004911 private class VirtualActivityContainer extends ActivityContainer {
4912 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004913 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004914
4915 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4916 super(getNextStackId());
4917 mParentActivity = parent;
4918 mCallback = callback;
4919 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004920 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004921 }
4922
4923 @Override
4924 public void setSurface(Surface surface, int width, int height, int density) {
4925 super.setSurface(surface, width, height, density);
4926
4927 synchronized (mService) {
4928 final long origId = Binder.clearCallingIdentity();
4929 try {
4930 setSurfaceLocked(surface, width, height, density);
4931 } finally {
4932 Binder.restoreCallingIdentity(origId);
4933 }
4934 }
4935 }
4936
4937 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4938 if (mContainerState == CONTAINER_STATE_FINISHING) {
4939 return;
4940 }
4941 VirtualActivityDisplay virtualActivityDisplay =
4942 (VirtualActivityDisplay) mActivityDisplay;
4943 if (virtualActivityDisplay == null) {
4944 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004945 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004946 mActivityDisplay = virtualActivityDisplay;
4947 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004948 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004949 }
4950
4951 if (mSurface != null) {
4952 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004953 }
4954
Craig Mautner6985bad2014-04-21 15:22:06 -07004955 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004956 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004957 mStack.resumeTopActivityLocked(null);
4958 } else {
4959 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004960 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004961 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004962 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004963 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004964 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004965
Craig Mautnerd163e752014-06-13 17:18:47 -07004966 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004967
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004968 if (DEBUG_STACK) Slog.d(TAG_STACK,
4969 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004970 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004971
Craig Mautner6985bad2014-04-21 15:22:06 -07004972 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004973 boolean isAttachedLocked() {
4974 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004975 }
4976
4977 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004978 void setDrawn() {
4979 synchronized (mService) {
4980 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004981 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004982 }
4983 }
4984
Craig Mautner1b4bf852014-05-26 15:06:32 -07004985 // Never start a new task on an ActivityView if it isn't explicitly specified.
4986 @Override
4987 boolean isEligibleForNewTasks() {
4988 return false;
4989 }
4990
Craig Mautnerd163e752014-06-13 17:18:47 -07004991 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004992 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004993 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4994 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4995 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4996 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4997 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004998 }
4999 }
5000
Craig Mautner4a1cb222013-12-04 16:14:06 -08005001 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
5002 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005003 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08005004 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08005005 int mDisplayId;
5006 Display mDisplay;
5007 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08005008
Craig Mautner4a1cb222013-12-04 16:14:06 -08005009 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
5010 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08005011 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08005012
Jose Lima4b6c6692014-08-12 17:41:12 -07005013 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07005014
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005015 ActivityDisplay() {
5016 }
Craig Mautner4504de52013-12-20 09:06:56 -08005017
Craig Mautner1a70a162014-09-13 12:09:31 -07005018 // After instantiation, check that mDisplay is not null before using this. The alternative
5019 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08005020 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07005021 final Display display = mDisplayManager.getDisplay(displayId);
5022 if (display == null) {
5023 return;
5024 }
5025 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08005026 }
5027
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005028 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08005029 mDisplay = display;
5030 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08005031 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08005032 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08005033
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07005034 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07005035 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07005036 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
5037 + " onTop=" + onTop);
5038 if (onTop) {
5039 mStacks.add(stack);
5040 } else {
5041 mStacks.add(0, stack);
5042 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08005043 }
5044
Craig Mautnere0a38842013-12-16 16:14:02 -08005045 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07005046 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08005047 + " from displayId=" + mDisplayId);
5048 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08005049 }
5050
Jose Lima4b6c6692014-08-12 17:41:12 -07005051 void setVisibleBehindActivity(ActivityRecord r) {
5052 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07005053 }
5054
Jose Lima4b6c6692014-08-12 17:41:12 -07005055 boolean hasVisibleBehindActivity() {
5056 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07005057 }
5058
Craig Mautner34b73df2014-01-12 21:11:08 -08005059 @Override
5060 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005061 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
5062 }
5063 }
5064
5065 class VirtualActivityDisplay extends ActivityDisplay {
5066 VirtualDisplay mVirtualDisplay;
5067
Craig Mautner6985bad2014-04-21 15:22:06 -07005068 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005069 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07005070 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
5071 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
5072 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
5073 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005074
5075 init(mVirtualDisplay.getDisplay());
5076
5077 mWindowManager.handleDisplayAdded(mDisplayId);
5078 }
5079
5080 void setSurface(Surface surface) {
5081 if (mVirtualDisplay != null) {
5082 mVirtualDisplay.setSurface(surface);
5083 }
5084 }
5085
5086 @Override
5087 void detachActivitiesLocked(ActivityStack stack) {
5088 super.detachActivitiesLocked(stack);
5089 if (mVirtualDisplay != null) {
5090 mVirtualDisplay.release();
5091 mVirtualDisplay = null;
5092 }
5093 }
5094
5095 @Override
5096 public String toString() {
5097 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08005098 }
Craig Mautnered6649f2013-12-02 14:08:25 -08005099 }
Jose Lima58e66d62014-05-27 20:00:27 -07005100
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07005101 /**
5102 * Adjust bounds to stay within stack bounds.
5103 *
5104 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
5105 * that keep them unchanged, but be contained within the stack bounds.
5106 *
5107 * @param bounds Bounds to be adjusted.
5108 * @param stackBounds Bounds within which the other bounds should remain.
5109 */
5110 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
5111 if (stackBounds == null || stackBounds.contains(bounds)) {
5112 return;
5113 }
5114
5115 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
5116 final int maxRight = stackBounds.right
5117 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
5118 int horizontalDiff = stackBounds.left - bounds.left;
5119 if ((horizontalDiff < 0 && bounds.left >= maxRight)
5120 || (bounds.left + horizontalDiff >= maxRight)) {
5121 horizontalDiff = maxRight - bounds.left;
5122 }
5123 bounds.left += horizontalDiff;
5124 bounds.right += horizontalDiff;
5125 }
5126
5127 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
5128 final int maxBottom = stackBounds.bottom
5129 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
5130 int verticalDiff = stackBounds.top - bounds.top;
5131 if ((verticalDiff < 0 && bounds.top >= maxBottom)
5132 || (bounds.top + verticalDiff >= maxBottom)) {
5133 verticalDiff = maxBottom - bounds.top;
5134 }
5135 bounds.top += verticalDiff;
5136 bounds.bottom += verticalDiff;
5137 }
5138 }
5139
Craig Mautner27084302013-03-25 08:05:25 -07005140}