blob: 119f42d307b2513de64048e9e99ec6e2bc46d9e2 [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;
Wale Ogunwaled57969f2014-11-15 19:37:29 -080037import static com.android.server.am.ActivityManagerDebugConfig.*;
Craig Mautner05d29032013-05-03 13:40:13 -070038import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Craig Mautner84984fa2014-06-19 11:19:20 -070039import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
40import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
41import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -070042import static com.android.server.am.ActivityStack.ActivityState.*;
Craig Mautner15df08a2015-04-01 12:17:18 -070043import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
44import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
Benjamin Franz469dd582015-06-09 14:24:36 +010045import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
Craig Mautner15df08a2015-04-01 12:17:18 -070046import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
47import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Craig Mautner8d341ef2013-03-26 09:03:27 -070048
Svetoslav7008b512015-06-24 18:47:07 -070049import android.Manifest;
Craig Mautner2420ead2013-04-01 17:13:20 -070050import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070051import android.app.ActivityManager;
Wale Ogunwale3797c222015-10-27 14:21:58 -070052import android.app.ActivityManager.StackId;
Craig Mautnered6649f2013-12-02 14:08:25 -080053import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070054import android.app.ActivityOptions;
55import android.app.AppGlobals;
Svetoslav7008b512015-06-24 18:47:07 -070056import android.app.AppOpsManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -080057import android.app.IActivityContainer;
58import android.app.IActivityContainerCallback;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070059import android.app.IActivityManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070060import android.app.IApplicationThread;
Clara Bayarrif7fea162015-10-22 16:09:52 +010061import android.app.KeyguardManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070062import android.app.PendingIntent;
Jeff Hao1b012d32014-08-20 10:35:34 -070063import android.app.ProfilerInfo;
Craig Mautner20e72272013-04-01 13:45:53 -070064import android.app.ActivityManager.RunningTaskInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070065import android.app.IActivityManager.WaitResult;
Craig Mautner2420ead2013-04-01 17:13:20 -070066import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -040067import android.app.StatusBarManager;
Jason Monk35c62a42014-06-17 10:24:47 -040068import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070069import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -070070import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -070071import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070072import android.content.Intent;
Craig Mautner23ac33b2013-04-01 16:26:35 -070073import android.content.IntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070074import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070075import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -070076import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070077import android.content.pm.PackageManager;
78import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +010079import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070080import android.content.res.Configuration;
Wale Ogunwale60454db2015-01-23 16:05:07 -080081import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -080082import android.hardware.display.DisplayManager;
83import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080084import android.hardware.display.DisplayManagerGlobal;
85import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080086import android.hardware.input.InputManager;
87import android.hardware.input.InputManagerInternal;
Craig Mautnerb9168362015-02-26 20:40:19 -080088import android.net.Uri;
Craig Mautner23ac33b2013-04-01 16:26:35 -070089import android.os.Binder;
Svet Ganov9c165d72015-12-01 19:52:26 -080090import android.os.Build;
Craig Mautner8d341ef2013-03-26 09:03:27 -070091import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070092import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070093import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070094import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070095import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070096import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070097import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070098import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070099import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700100import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -0400101import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700102import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -0700103import android.os.Trace;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -0700104import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -0700105import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100106import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700107import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -0700108import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -0400109import android.provider.Settings;
110import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700111import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -0700112import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700113import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -0700114import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700115import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800116import android.util.SparseArray;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700117
Craig Mautner4a1cb222013-12-04 16:14:06 -0800118import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800119import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800120import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -0800121import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800122import android.view.Surface;
Chong Zhangb15758a2015-11-17 12:12:03 -0800123
Craig Mautner23ac33b2013-04-01 16:26:35 -0700124import com.android.internal.app.HeavyWeightSwitcherActivity;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700125import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800126import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700127import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400128import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -0700129import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -0400130import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800131import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700132import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700133import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700134
Craig Mautner8d341ef2013-03-26 09:03:27 -0700135import java.io.FileDescriptor;
136import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700137import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700138import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700139import java.util.Arrays;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700140import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700141import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700142
Craig Mautner4a1cb222013-12-04 16:14:06 -0800143public final class ActivityStackSupervisor implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800144 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700145 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700146 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700147 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700148 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700149 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700150 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700151 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700152 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
153 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700154 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700155 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700156 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
157 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700158 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700159 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800160
Craig Mautnerf3333272013-04-22 10:55:53 -0700161 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700162 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700163
Craig Mautner0eea92c2013-05-16 13:35:39 -0700164 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700165 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700166
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700167 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700168 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700169
Craig Mautner05d29032013-05-03 13:40:13 -0700170 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
171 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
172 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700173 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700174 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800175 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
176 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
177 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700178 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400179 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
180 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700181 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700182 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700183 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800184
Wale Ogunwale040b4702015-08-06 18:10:50 -0700185 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700186
Jason Monk62515be2014-05-21 16:06:19 -0400187 private static final String LOCK_TASK_TAG = "Lock-to-App";
188
Wale Ogunwale040b4702015-08-06 18:10:50 -0700189 // Used to indicate if an object (e.g. stack) that we are trying to get
190 // should be created if it doesn't exist already.
191 private static final boolean CREATE_IF_NEEDED = true;
192
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700193 // Used to indicate that windows of activities should be preserved during the resize.
194 static final boolean PRESERVE_WINDOWS = true;
195
Wale Ogunwale040b4702015-08-06 18:10:50 -0700196 // Used to indicate if an object (e.g. task) should be moved/created
197 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700198 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700199
200 // Used to indicate that an objects (e.g. task) removal from its container
201 // (e.g. stack) is due to it moving to another container.
202 static final boolean MOVING = true;
203
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700204 // Force the focus to change to the stack we are moving a task to..
205 static final boolean FORCE_FOCUS = true;
206
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700207 // Restore task from the saved recents if it can't be found in any live stack.
208 static final boolean RESTORE_FROM_RECENTS = true;
209
Svetoslav7008b512015-06-24 18:47:07 -0700210 // Activity actions an app cannot start if it uses a permission which is not granted.
211 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
212 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700213
Svetoslav7008b512015-06-24 18:47:07 -0700214 static {
215 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
216 Manifest.permission.CAMERA);
217 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
218 Manifest.permission.CAMERA);
219 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
220 Manifest.permission.CALL_PHONE);
221 }
222
Svet Ganov99b60432015-06-27 13:15:22 -0700223 /** Action restriction: launching the activity is not restricted. */
224 private static final int ACTIVITY_RESTRICTION_NONE = 0;
225 /** Action restriction: launching the activity is restricted by a permission. */
226 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
227 /** Action restriction: launching the activity is restricted by an app op. */
228 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700229
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700230 // The height/width divide used when fitting a task within a bounds with method
231 // {@link #fitWithinBounds}.
232 // We always want the task to to be visible in the bounds without affecting its size when
233 // fitting. To make sure this is the case, we don't adjust the task left or top side pass
234 // the input bounds right or bottom side minus the width or height divided by this value.
235 private static final int FIT_WITHIN_BOUNDS_DIVIDER = 3;
236
justinzhang5286d3f2014-05-12 17:06:01 -0400237 /** Status Bar Service **/
238 private IBinder mToken = new Binder();
239 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400240 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400241
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700242 // For debugging to make sure the caller when acquiring/releasing our
243 // wake lock is the system process.
244 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700245
Craig Mautner27084302013-03-25 08:05:25 -0700246 final ActivityManagerService mService;
247
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800248 private final RecentTasks mRecentTasks;
249
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700250 final ActivityStackSupervisorHandler mHandler;
251
252 /** Short cut */
253 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800254 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700255
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700256 /** Counter for next free stack ID to use for dynamic activity stacks. */
257 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700258
259 /** Task identifier that activities are currently being started in. Incremented each time a
260 * new task is created. */
261 private int mCurTaskId = 0;
262
Craig Mautner2420ead2013-04-01 17:13:20 -0700263 /** The current user */
264 private int mCurrentUser;
265
Craig Mautnere0a38842013-12-16 16:14:02 -0800266 /** The stack containing the launcher app. Assumed to always be attached to
267 * Display.DEFAULT_DISPLAY. */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700268 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700269
Craig Mautnere0a38842013-12-16 16:14:02 -0800270 /** The stack currently receiving input or launching the next activity. */
Craig Mautner29219d92013-04-16 20:19:12 -0700271 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700272
Craig Mautner4a1cb222013-12-04 16:14:06 -0800273 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
274 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800275 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800276 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700277
278 /** List of activities that are waiting for a new activity to become visible before completing
279 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800280 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700281
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700282 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800283 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700284
285 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800286 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700287
Craig Mautnerde4ef022013-04-07 19:01:33 -0700288 /** List of activities that are ready to be stopped, but waiting for the next activity to
289 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800290 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700291
Craig Mautnerf3333272013-04-22 10:55:53 -0700292 /** List of activities that are ready to be finished, but waiting for the previous activity to
293 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800294 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700295
Craig Mautner0eea92c2013-05-16 13:35:39 -0700296 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800297 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700298
Craig Mautnerf3333272013-04-22 10:55:53 -0700299 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700300 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700301
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700302 /** Used to queue up any background users being started */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700303 final ArrayList<UserState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700304
Craig Mautnerde4ef022013-04-07 19:01:33 -0700305 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
306 * is being brought in front of us. */
307 boolean mUserLeaving = false;
308
Craig Mautner0eea92c2013-05-16 13:35:39 -0700309 /** Set when we have taken too long waiting to go to sleep. */
310 boolean mSleepTimeout = false;
311
312 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700313 * We don't want to allow the device to go to sleep while in the process
314 * of launching an activity. This is primarily to allow alarm intent
315 * receivers to launch an activity and get that to run before the device
316 * goes back to sleep.
317 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700318 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700319
320 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700321 * Set when the system is going to sleep, until we have
322 * successfully paused the current activity and released our wake lock.
323 * At that point the system is allowed to actually sleep.
324 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700325 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700326
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700327 /** Stack id of the front stack when user switched, indexed by userId. */
328 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700329
Craig Mautner4504de52013-12-20 09:06:56 -0800330 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800331 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700332 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800333
334 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700335 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800336
Jeff Brownca9bc702014-02-11 14:32:56 -0800337 InputManagerInternal mInputManagerInternal;
338
Craig Mautner15df08a2015-04-01 12:17:18 -0700339 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
340 * may be finished until there is only one entry left. If this is empty the system is not
341 * in lockTask mode. */
342 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000343 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700344 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
345 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000346 */
347 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400348 /**
349 * Notifies the user when entering/exiting lock-task.
350 */
351 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800352
Craig Mautner15df08a2015-04-01 12:17:18 -0700353 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches = new ArrayList<>();
Craig Mautneree36c772014-07-16 14:56:05 -0700354
Craig Mautner42d04db2014-11-06 12:13:23 -0800355 /** Used to keep resumeTopActivityLocked() from being entered recursively */
356 boolean inResumeTopActivity;
357
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700358 // 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 -0700359 private final Rect tempRect = new Rect();
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700360 private final Rect tempRect2 = new Rect();
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700361
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700362 private final SparseArray<Configuration> mTmpConfigs = new SparseArray<>();
363 private final SparseArray<Rect> mTmpBounds = new SparseArray<>();
364
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700365 // The default minimal size that will be used if the activity doesn't specify its minimal size.
366 // It will be calculated when the default display gets added.
367 private int mDefaultMinimalSizeOfResizeableTask = -1;
368
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700369 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
370 private boolean mTaskLayersChanged = true;
371
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800372 private final ActivityMetricsLogger mActivityMetricsLogger;
373
Craig Mautneree36c772014-07-16 14:56:05 -0700374 /**
375 * Description of a request to start a new activity, which has been held
376 * due to app switches being disabled.
377 */
378 static class PendingActivityLaunch {
379 final ActivityRecord r;
380 final ActivityRecord sourceRecord;
381 final int startFlags;
382 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700383 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700384
385 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700386 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700387 r = _r;
388 sourceRecord = _sourceRecord;
389 startFlags = _startFlags;
390 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700391 callerApp = _callerApp;
392 }
393
394 void sendErrorResult(String message) {
395 try {
396 if (callerApp.thread != null) {
397 callerApp.thread.scheduleCrash(message);
398 }
399 } catch (RemoteException e) {
400 Slog.e(TAG, "Exception scheduling crash of failed "
401 + "activity launcher sourceRecord=" + sourceRecord, e);
402 }
Craig Mautneree36c772014-07-16 14:56:05 -0700403 }
404 }
405
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800406 public ActivityStackSupervisor(ActivityManagerService service, RecentTasks recentTasks) {
Craig Mautner27084302013-03-25 08:05:25 -0700407 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800408 mRecentTasks = recentTasks;
Jeff Brown2c43c332014-06-12 22:38:59 -0700409 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800410 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jeff Brown2c43c332014-06-12 22:38:59 -0700411 }
412
413 /**
414 * At the time when the constructor runs, the power manager has not yet been
415 * initialized. So we initialize our wakelocks afterwards.
416 */
417 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800418 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700419 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700420 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700421 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700422 }
423
justinzhang5286d3f2014-05-12 17:06:01 -0400424 // This function returns a IStatusBarService. The value is from ServiceManager.
425 // getService and is cached.
426 private IStatusBarService getStatusBarService() {
427 synchronized (mService) {
428 if (mStatusBarService == null) {
429 mStatusBarService = IStatusBarService.Stub.asInterface(
430 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
431 if (mStatusBarService == null) {
432 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
433 }
434 }
435 return mStatusBarService;
436 }
437 }
438
Jason Monk35c62a42014-06-17 10:24:47 -0400439 private IDevicePolicyManager getDevicePolicyManager() {
440 synchronized (mService) {
441 if (mDevicePolicyManager == null) {
442 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
443 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
444 if (mDevicePolicyManager == null) {
445 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
446 }
447 }
448 return mDevicePolicyManager;
449 }
450 }
451
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700452 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800453 synchronized (mService) {
454 mWindowManager = wm;
455
456 mDisplayManager =
457 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
458 mDisplayManager.registerDisplayListener(this, null);
459
460 Display[] displays = mDisplayManager.getDisplays();
461 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
462 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800463 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700464 if (activityDisplay.mDisplay == null) {
465 throw new IllegalStateException("Default Display does not exist");
466 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800467 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700468 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800469 }
470
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700471 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800472 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800473
474 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800475 }
Craig Mautner27084302013-03-25 08:05:25 -0700476 }
477
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200478 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700479 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200480 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700481 }
482
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700483 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800484 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700485 }
486
Craig Mautnerde4ef022013-04-07 19:01:33 -0700487 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800488 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700489 }
490
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700491 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700492 if (stack == null) {
493 return false;
494 }
495
Craig Mautnerdf88d732014-01-27 09:21:32 -0800496 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
497 if (parent != null) {
498 stack = parent.task.stack;
499 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800500 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700501 }
502
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700503 /** The top most stack. */
504 boolean isFrontStack(ActivityStack stack) {
505 if (stack == null) {
506 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800507 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700508
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700509 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
510 if (parent != null) {
511 stack = parent.task.stack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800512 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700513 return stack == mHomeStack.mStacks.get((mHomeStack.mStacks.size() - 1));
514 }
515
516 void setFocusStack(String reason, ActivityStack focusedStack) {
517 mLastFocusedStack = mFocusedStack;
518 mFocusedStack = focusedStack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800519
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700520 EventLogTags.writeAmFocusedStack(
521 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
522 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
Craig Mautnerde313752015-01-22 14:28:03 -0800523
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800524 if (mService.mBooting || !mService.mBooted) {
525 final ActivityRecord r = topRunningActivityLocked();
526 if (r != null && r.idle) {
527 checkFinishBootingLocked();
528 }
529 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700530 }
531
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700532 void moveHomeStackToFront(String reason) {
533 mHomeStack.moveToFront(reason);
534 }
535
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700536 /** Returns true if the focus activity was adjusted to the home stack top activity. */
537 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700538 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
539 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700540 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700541 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700542
Craig Mautner84984fa2014-06-19 11:19:20 -0700543 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700544
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700545 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700546 if (top == null) {
547 return false;
548 }
549 mService.setFocusedActivityLocked(top, reason);
550 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700551 }
552
Craig Mautner299f9602015-01-26 09:47:33 -0800553 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700554 if (!mService.mBooting && !mService.mBooted) {
555 // Not ready yet!
556 return false;
557 }
558
Craig Mautner84984fa2014-06-19 11:19:20 -0700559 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
560 mWindowManager.showRecentApps();
561 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700562 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700563
Craig Mautner84984fa2014-06-19 11:19:20 -0700564 if (prev != null) {
565 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
566 }
567
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700568 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
569 ActivityRecord r = getHomeActivity();
Mark Lua56ea122015-10-08 13:31:01 +0800570 // Only resume home activity if isn't finishing.
571 if (r != null && !r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -0800572 mService.setFocusedActivityLocked(r, reason);
Craig Mautner05d29032013-05-03 13:40:13 -0700573 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700574 }
Craig Mautner299f9602015-01-26 09:47:33 -0800575 return mService.startHomeActivityLocked(mCurrentUser, reason);
Craig Mautner69ada552013-04-18 13:51:51 -0700576 }
577
Craig Mautner8d341ef2013-03-26 09:03:27 -0700578 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700579 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700580 }
581
582 /**
583 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
584 * @param id Id of the task we would like returned.
585 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
586 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700587 * @param stackId The stack to restore the task to (default launch stack will be used if
588 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700589 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700590 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800591 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800592 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800593 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800594 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
595 ActivityStack stack = stacks.get(stackNdx);
596 TaskRecord task = stack.taskForIdLocked(id);
597 if (task != null) {
598 return task;
599 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700600 }
601 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800602
603 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700604 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800605 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800606 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700607 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800608 return null;
609 }
610
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700611 if (!restoreFromRecents) {
612 return task;
613 }
614
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700615 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700616 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
617 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800618 return null;
619 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700620 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800621 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700622 }
623
Craig Mautner6170f732013-04-02 13:05:23 -0700624 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800625 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800626 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800627 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800628 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
629 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
630 if (r != null) {
631 return r;
632 }
Craig Mautner6170f732013-04-02 13:05:23 -0700633 }
634 }
635 return null;
636 }
637
Craig Mautneref73ee12014-04-23 11:45:37 -0700638 void setNextTaskId(int taskId) {
639 if (taskId > mCurTaskId) {
640 mCurTaskId = taskId;
641 }
642 }
643
Craig Mautner8d341ef2013-03-26 09:03:27 -0700644 int getNextTaskId() {
645 do {
646 mCurTaskId++;
647 if (mCurTaskId <= 0) {
648 mCurTaskId = 1;
649 }
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700650 } while (anyTaskForIdLocked(mCurTaskId, !RESTORE_FROM_RECENTS, INVALID_STACK_ID) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700651 return mCurTaskId;
652 }
653
Craig Mautnerde4ef022013-04-07 19:01:33 -0700654 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800655 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700656 if (stack == null) {
657 return null;
658 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700659 ActivityRecord resumedActivity = stack.mResumedActivity;
660 if (resumedActivity == null || resumedActivity.app == null) {
661 resumedActivity = stack.mPausingActivity;
662 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700663 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700664 }
665 }
666 return resumedActivity;
667 }
668
Dianne Hackbornff072722014-09-24 10:56:28 -0700669 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700670 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800671 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800672 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
673 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800674 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
675 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700676 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800677 continue;
678 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700679 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800680 if (hr != null) {
681 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
682 && processName.equals(hr.processName)) {
683 try {
George Mount2c92c972014-03-20 09:38:23 -0700684 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800685 didSomething = true;
686 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700687 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800688 Slog.w(TAG, "Exception in new application when starting activity "
689 + hr.intent.getComponent().flattenToShortString(), e);
690 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700691 }
Craig Mautner20e72272013-04-01 13:45:53 -0700692 }
Craig Mautner20e72272013-04-01 13:45:53 -0700693 }
694 }
695 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700696 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700697 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700698 }
Craig Mautner20e72272013-04-01 13:45:53 -0700699 return didSomething;
700 }
701
702 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800703 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
704 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800705 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
706 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700707 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800708 continue;
709 }
710 final ActivityRecord resumedActivity = stack.mResumedActivity;
711 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700712 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800713 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800714 return false;
715 }
Craig Mautner20e72272013-04-01 13:45:53 -0700716 }
717 }
718 return true;
719 }
720
Craig Mautnerde4ef022013-04-07 19:01:33 -0700721 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800722 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
723 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800724 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
725 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700726 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800727 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700728 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800729 return false;
730 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700731 }
732 }
733 }
734 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700735 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800736 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
737 mLastFocusedStack + " to=" + mFocusedStack);
738 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700739 return true;
740 }
741
742 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800743 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800744 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
745 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800746 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
747 final ActivityStack stack = stacks.get(stackNdx);
748 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800749 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700750 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800751 return false;
752 }
753 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800754 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700755 }
756 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800757 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700758 }
759
Craig Mautner2acc3892013-09-23 10:28:14 -0700760 /**
761 * Pause all activities in either all of the stacks or just the back stacks.
762 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700763 * @return true if any activity was paused as a result of this call.
764 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700765 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700766 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800767 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
768 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800769 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
770 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700771 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700772 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800773 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700774 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
775 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800776 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700777 }
778 }
779 return someActivityPaused;
780 }
781
Craig Mautnerde4ef022013-04-07 19:01:33 -0700782 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700783 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800784 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
785 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800786 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
787 final ActivityStack stack = stacks.get(stackNdx);
788 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700789 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800790 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700791 Slog.d(TAG_STATES,
792 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800793 pausing = false;
794 } else {
795 return false;
796 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700797 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700798 }
799 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700800 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700801 }
802
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700803 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
804 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500805 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800806 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
807 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
808 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
809 final ActivityStack stack = stacks.get(stackNdx);
810 if (stack.mResumedActivity != null &&
811 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700812 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800813 }
814 }
815 }
816 }
817
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700818 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700819 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700820 }
821
822 void sendWaitingVisibleReportLocked(ActivityRecord r) {
823 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700824 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700825 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700826 if (w.who == null) {
827 changed = true;
828 w.timeout = false;
829 if (r != null) {
830 w.who = new ComponentName(r.info.packageName, r.info.name);
831 }
832 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
833 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700834 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700835 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700836 if (changed) {
837 mService.notifyAll();
838 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700839 }
840
841 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
842 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700843 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700844 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700845 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700846 if (w.who == null) {
847 changed = true;
848 w.timeout = timeout;
849 if (r != null) {
850 w.who = new ComponentName(r.info.packageName, r.info.name);
851 }
852 w.thisTime = thisTime;
853 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700854 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700855 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700856 if (changed) {
857 mService.notifyAll();
858 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700859 }
860
Craig Mautner29219d92013-04-16 20:19:12 -0700861 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800862 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700863 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -0700864 if (r != null) {
865 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700866 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700867
Craig Mautner4a1cb222013-12-04 16:14:06 -0800868 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800869 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800870 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
871 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700872 if (stack != focusedStack && isFrontStack(stack)) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700873 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -0700874 if (r != null) {
875 return r;
876 }
877 }
878 }
879 return null;
880 }
881
Dianne Hackborn09233282014-04-30 11:33:59 -0700882 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700883 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800884 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
885 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800886 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800887 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800888 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800889 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
890 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700891 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800892 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700893 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700894 }
895 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700896
897 // The lists are already sorted from most recent to oldest. Just pull the most recent off
898 // each list and add it to list. Stop when all lists are empty or maxNum reached.
899 while (maxNum > 0) {
900 long mostRecentActiveTime = Long.MIN_VALUE;
901 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800902 final int numTaskLists = runningTaskLists.size();
903 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
904 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700905 if (!stackTaskList.isEmpty()) {
906 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
907 if (lastActiveTime > mostRecentActiveTime) {
908 mostRecentActiveTime = lastActiveTime;
909 selectedStackList = stackTaskList;
910 }
911 }
912 }
913 if (selectedStackList != null) {
914 list.add(selectedStackList.remove(0));
915 --maxNum;
916 } else {
917 break;
918 }
919 }
Craig Mautner20e72272013-04-01 13:45:53 -0700920 }
921
Craig Mautner23ac33b2013-04-01 16:26:35 -0700922 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700923 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700924 // Collect information about the target of the Intent.
925 ActivityInfo aInfo;
926 try {
927 ResolveInfo rInfo =
928 AppGlobals.getPackageManager().resolveIntent(
929 intent, resolvedType,
930 PackageManager.MATCH_DEFAULT_ONLY
931 | ActivityManagerService.STOCK_PM_FLAGS, userId);
932 aInfo = rInfo != null ? rInfo.activityInfo : null;
933 } catch (RemoteException e) {
934 aInfo = null;
935 }
936
937 if (aInfo != null) {
938 // Store the found target back into the intent, because now that
939 // we have it we never want to do this again. For example, if the
940 // user navigates back to this point in the history, we should
941 // always restart the exact same activity.
942 intent.setComponent(new ComponentName(
943 aInfo.applicationInfo.packageName, aInfo.name));
944
945 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -0700946 if (!aInfo.processName.equals("system")) {
947 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700948 mService.setDebugApp(aInfo.processName, true, false);
949 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700950
Man Caocfa78b22015-06-11 20:14:34 -0700951 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
952 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
953 }
954
955 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700956 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700957 }
958 }
959 }
960 return aInfo;
961 }
962
Craig Mautner299f9602015-01-26 09:47:33 -0800963 void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
964 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700965 startActivityLocked(null /* caller */, intent, null /* resolvedType */, aInfo,
966 null /* voiceSession */, null /* voiceInteractor */, null /* resultTo */,
967 null /* resultWho */, 0 /* requestCode */, 0 /* callingPid */, 0 /* callingUid */,
968 null /* callingPackage */, 0 /* realCallingPid */, 0 /* realCallingUid */,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700969 0 /* startFlags */, null /* options */, false /* ignoreTargetSecurity */,
970 false /* componentSpecified */,
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700971 null /* outActivity */, null /* container */, null /* inTask */);
972 if (inResumeTopActivity) {
973 // If we are in resume section already, home activity will be initialized, but not
974 // resumed (to avoid recursive resume) and will stay that way until something pokes it
975 // again. We need to schedule another resume.
976 scheduleResumeTopActivities();
977 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700978 }
979
Craig Mautner23ac33b2013-04-01 16:26:35 -0700980 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700981 String callingPackage, Intent intent, String resolvedType,
982 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700983 IBinder resultTo, String resultWho, int requestCode, int startFlags,
984 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Chong Zhang280d3322015-11-03 17:27:26 -0800985 Bundle bOptions, boolean ignoreTargetSecurity, int userId,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700986 IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700987 // Refuse possible leaked file descriptors
988 if (intent != null && intent.hasFileDescriptors()) {
989 throw new IllegalArgumentException("File descriptors passed in Intent");
990 }
991 boolean componentSpecified = intent.getComponent() != null;
992
993 // Don't modify the client's object!
994 intent = new Intent(intent);
995
996 // Collect information about the target of the Intent.
Craig Mautner15df08a2015-04-01 12:17:18 -0700997 ActivityInfo aInfo =
998 resolveActivity(intent, resolvedType, startFlags, profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700999
Chong Zhang280d3322015-11-03 17:27:26 -08001000 ActivityOptions options = ActivityOptions.fromBundle(bOptions);
Craig Mautnere0a38842013-12-16 16:14:02 -08001001 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001002 synchronized (mService) {
Craig Mautnerb9168362015-02-26 20:40:19 -08001003 if (container != null && container.mParentActivity != null &&
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001004 container.mParentActivity.state != RESUMED) {
Craig Mautnerb9168362015-02-26 20:40:19 -08001005 // Cannot start a child activity if the parent is not resumed.
1006 return ActivityManager.START_CANCELED;
1007 }
Dianne Hackborn95465202014-09-15 16:21:55 -07001008 final int realCallingPid = Binder.getCallingPid();
1009 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -07001010 int callingPid;
1011 if (callingUid >= 0) {
1012 callingPid = -1;
1013 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001014 callingPid = realCallingPid;
1015 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001016 } else {
1017 callingPid = callingUid = -1;
1018 }
1019
Craig Mautnere0a38842013-12-16 16:14:02 -08001020 final ActivityStack stack;
1021 if (container == null || container.mStack.isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001022 stack = mFocusedStack;
Craig Mautnere0a38842013-12-16 16:14:02 -08001023 } else {
1024 stack = container.mStack;
1025 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08001026 stack.mConfigWillChange = config != null && mService.mConfiguration.diff(config) != 0;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001027 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautnerde4ef022013-04-07 19:01:33 -07001028 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001029
1030 final long origId = Binder.clearCallingIdentity();
1031
1032 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001033 (aInfo.applicationInfo.privateFlags
1034 &ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001035 // This may be a heavy-weight process! Check to see if we already
1036 // have another, different heavy-weight process running.
1037 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
1038 if (mService.mHeavyWeightProcess != null &&
1039 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
1040 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001041 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001042 if (caller != null) {
1043 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
1044 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001045 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001046 } else {
1047 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -07001048 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -07001049 + intent.toString());
1050 ActivityOptions.abort(options);
1051 return ActivityManager.START_PERMISSION_DENIED;
1052 }
1053 }
1054
1055 IIntentSender target = mService.getIntentSenderLocked(
1056 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -07001057 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -07001058 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
1059 | PendingIntent.FLAG_ONE_SHOT, null);
1060
1061 Intent newIntent = new Intent();
1062 if (requestCode >= 0) {
1063 // Caller is requesting a result.
1064 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
1065 }
1066 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
1067 new IntentSender(target));
1068 if (mService.mHeavyWeightProcess.activities.size() > 0) {
1069 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
1070 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
1071 hist.packageName);
1072 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
1073 hist.task.taskId);
1074 }
1075 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
1076 aInfo.packageName);
1077 newIntent.setFlags(intent.getFlags());
1078 newIntent.setClassName("android",
1079 HeavyWeightSwitcherActivity.class.getName());
1080 intent = newIntent;
1081 resolvedType = null;
1082 caller = null;
1083 callingUid = Binder.getCallingUid();
1084 callingPid = Binder.getCallingPid();
1085 componentSpecified = true;
1086 try {
1087 ResolveInfo rInfo =
1088 AppGlobals.getPackageManager().resolveIntent(
1089 intent, null,
1090 PackageManager.MATCH_DEFAULT_ONLY
1091 | ActivityManagerService.STOCK_PM_FLAGS, userId);
1092 aInfo = rInfo != null ? rInfo.activityInfo : null;
1093 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1094 } catch (RemoteException e) {
1095 aInfo = null;
1096 }
1097 }
1098 }
1099 }
1100
Dianne Hackborn91097de2014-04-04 18:02:06 -07001101 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
1102 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -07001103 requestCode, callingPid, callingUid, callingPackage,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001104 realCallingPid, realCallingUid, startFlags, options, ignoreTargetSecurity,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001105 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001106
Craig Mautner85c11a82014-07-17 12:38:18 -07001107 Binder.restoreCallingIdentity(origId);
1108
Craig Mautnerde4ef022013-04-07 19:01:33 -07001109 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001110 // If the caller also wants to switch to a new configuration,
1111 // do so now. This allows a clean switch, as we are waiting
1112 // for the current activity to pause (so we will not destroy
1113 // it), and have not yet started the next activity.
1114 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
1115 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -07001116 stack.mConfigWillChange = false;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001117 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautner23ac33b2013-04-01 16:26:35 -07001118 "Updating to new configuration after starting activity.");
Maxim Bogatov05075302015-05-19 18:33:08 -07001119 mService.updateConfigurationLocked(config, null, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001120 }
1121
Craig Mautner23ac33b2013-04-01 16:26:35 -07001122 if (outResult != null) {
1123 outResult.result = res;
1124 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001125 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001126 do {
1127 try {
1128 mService.wait();
1129 } catch (InterruptedException e) {
1130 }
1131 } while (!outResult.timeout && outResult.who == null);
1132 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001133 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001134 if (r.nowVisible && r.state == RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001135 outResult.timeout = false;
1136 outResult.who = new ComponentName(r.info.packageName, r.info.name);
1137 outResult.totalTime = 0;
1138 outResult.thisTime = 0;
1139 } else {
1140 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001141 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001142 do {
1143 try {
1144 mService.wait();
1145 } catch (InterruptedException e) {
1146 }
1147 } while (!outResult.timeout && outResult.who == null);
1148 }
1149 }
1150 }
1151
1152 return res;
1153 }
1154 }
1155
1156 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1157 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
Chong Zhang280d3322015-11-03 17:27:26 -08001158 Bundle bOptions, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001159 if (intents == null) {
1160 throw new NullPointerException("intents is null");
1161 }
1162 if (resolvedTypes == null) {
1163 throw new NullPointerException("resolvedTypes is null");
1164 }
1165 if (intents.length != resolvedTypes.length) {
1166 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1167 }
1168
Craig Mautner23ac33b2013-04-01 16:26:35 -07001169
1170 int callingPid;
1171 if (callingUid >= 0) {
1172 callingPid = -1;
1173 } else if (caller == null) {
1174 callingPid = Binder.getCallingPid();
1175 callingUid = Binder.getCallingUid();
1176 } else {
1177 callingPid = callingUid = -1;
1178 }
1179 final long origId = Binder.clearCallingIdentity();
1180 try {
1181 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001182 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001183 for (int i=0; i<intents.length; i++) {
1184 Intent intent = intents[i];
1185 if (intent == null) {
1186 continue;
1187 }
1188
1189 // Refuse possible leaked file descriptors
1190 if (intent != null && intent.hasFileDescriptors()) {
1191 throw new IllegalArgumentException("File descriptors passed in Intent");
1192 }
1193
1194 boolean componentSpecified = intent.getComponent() != null;
1195
1196 // Don't modify the client's object!
1197 intent = new Intent(intent);
1198
1199 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001200 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001201 // TODO: New, check if this is correct
1202 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1203
1204 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001205 (aInfo.applicationInfo.privateFlags
1206 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001207 throw new IllegalArgumentException(
1208 "FLAG_CANT_SAVE_STATE not supported here");
1209 }
1210
Chong Zhang280d3322015-11-03 17:27:26 -08001211 ActivityOptions options = ActivityOptions.fromBundle(
1212 i == intents.length - 1 ? bOptions : null);
Craig Mautner6170f732013-04-02 13:05:23 -07001213 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001214 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1215 callingPackage, callingPid, callingUid,
Chong Zhang280d3322015-11-03 17:27:26 -08001216 0, options, false, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001217 if (res < 0) {
1218 return res;
1219 }
1220
1221 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1222 }
1223 }
1224 } finally {
1225 Binder.restoreCallingIdentity(origId);
1226 }
1227
1228 return ActivityManager.START_SUCCESS;
1229 }
1230
Craig Mautner2420ead2013-04-01 17:13:20 -07001231 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001232 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001233 throws RemoteException {
1234
Craig Mautner2568c3a2015-03-26 14:22:34 -07001235 if (andResume) {
1236 r.startFreezingScreenLocked(app, 0);
1237 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001238
Craig Mautner2568c3a2015-03-26 14:22:34 -07001239 // schedule launch ticks to collect information about slow apps.
1240 r.startLaunchTickingLocked();
1241 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001242
1243 // Have the window manager re-evaluate the orientation of
1244 // the screen based on the new activity order. Note that
1245 // as a result of this, it can call back into the activity
1246 // manager with a new orientation. We don't care about that,
1247 // because the activity is not currently running so we are
1248 // just restarting it anyway.
1249 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001250 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001251 mService.mConfiguration,
1252 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001253 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001254 }
1255
1256 r.app = app;
1257 app.waitingToKill = null;
1258 r.launchCount++;
1259 r.lastLaunchTime = SystemClock.uptimeMillis();
1260
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001261 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001262
1263 int idx = app.activities.indexOf(r);
1264 if (idx < 0) {
1265 app.activities.add(r);
1266 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001267 mService.updateLruProcessLocked(app, true, null);
1268 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001269
Craig Mautner15df08a2015-04-01 12:17:18 -07001270 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001271 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1272 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001273 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001274 }
1275
1276 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001277 try {
1278 if (app.thread == null) {
1279 throw new RemoteException();
1280 }
1281 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001282 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001283 if (andResume) {
1284 results = r.results;
1285 newIntents = r.newIntents;
1286 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001287 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1288 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1289 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001290 if (andResume) {
1291 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1292 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001293 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001294 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001295 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001296 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001297 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001298 }
Nicolas Geoffray27c07372015-11-05 16:54:09 +00001299 mService.notifyPackageUse(r.intent.getComponent().getPackageName());
Craig Mautner2420ead2013-04-01 17:13:20 -07001300 r.sleeping = false;
1301 r.forceNewConfig = false;
1302 mService.showAskCompatModeDialogLocked(r);
1303 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001304 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001305 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1306 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1307 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001308 final String profileFile = mService.mProfileFile;
1309 if (profileFile != null) {
1310 ParcelFileDescriptor profileFd = mService.mProfileFd;
1311 if (profileFd != null) {
1312 try {
1313 profileFd = profileFd.dup();
1314 } catch (IOException e) {
1315 if (profileFd != null) {
1316 try {
1317 profileFd.close();
1318 } catch (IOException o) {
1319 }
1320 profileFd = null;
1321 }
1322 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001323 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001324
1325 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1326 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001327 }
1328 }
1329 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001330
Craig Mautner2568c3a2015-03-26 14:22:34 -07001331 if (andResume) {
1332 app.hasShownUi = true;
1333 app.pendingUiClean = true;
1334 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001335 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001336 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001337 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001338 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001339 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001340 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001341
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001342 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001343 // This may be a heavy-weight process! Note that the package
1344 // manager will ensure that only activity can run in the main
1345 // process of the .apk, which is the only thing that will be
1346 // considered heavy-weight.
1347 if (app.processName.equals(app.info.packageName)) {
1348 if (mService.mHeavyWeightProcess != null
1349 && mService.mHeavyWeightProcess != app) {
1350 Slog.w(TAG, "Starting new heavy weight process " + app
1351 + " when already running "
1352 + mService.mHeavyWeightProcess);
1353 }
1354 mService.mHeavyWeightProcess = app;
1355 Message msg = mService.mHandler.obtainMessage(
1356 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1357 msg.obj = r;
1358 mService.mHandler.sendMessage(msg);
1359 }
1360 }
1361
1362 } catch (RemoteException e) {
1363 if (r.launchFailed) {
1364 // This is the second time we failed -- finish activity
1365 // and give up.
1366 Slog.e(TAG, "Second failure launching "
1367 + r.intent.getComponent().flattenToShortString()
1368 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001369 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001370 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1371 "2nd-crash", false);
1372 return false;
1373 }
1374
1375 // This is the first time we failed -- restart process and
1376 // retry.
1377 app.activities.remove(r);
1378 throw e;
1379 }
1380
1381 r.launchFailed = false;
1382 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001383 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001384 }
1385
1386 if (andResume) {
1387 // As part of the process of launching, ActivityThread also performs
1388 // a resume.
1389 stack.minimalResumeActivityLocked(r);
1390 } else {
1391 // This activity is not starting in the resumed state... which
1392 // should look like we asked it to pause+stop (but remain visible),
1393 // and it has done so and reported back the current icicle and
1394 // other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001395 if (DEBUG_STATES) Slog.v(TAG_STATES,
1396 "Moving to STOPPED: " + r + " (starting in stopped state)");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001397 r.state = STOPPED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001398 r.stopped = true;
1399 }
1400
1401 // Launch the new version setup screen if needed. We do this -after-
1402 // launching the initial activity (that is, home), so that it can have
1403 // a chance to initialize itself while in the background, making the
1404 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001405 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001406 mService.startSetupActivityLocked();
1407 }
1408
Dianne Hackborn465fa392014-09-14 14:21:18 -07001409 // Update any services we are bound to that might care about whether
1410 // their client may have activities.
1411 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1412
Craig Mautner2420ead2013-04-01 17:13:20 -07001413 return true;
1414 }
1415
Craig Mautnere79d42682013-04-01 19:01:53 -07001416 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001417 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001418 // Is this activity's application already running?
1419 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001420 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001421
1422 r.task.stack.setLaunchTime(r);
1423
1424 if (app != null && app.thread != null) {
1425 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001426 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1427 || !"android".equals(r.info.packageName)) {
1428 // Don't add this if it is a platform component that is marked
1429 // to run in multiple processes, because this is actually
1430 // part of the framework so doesn't make sense to track as a
1431 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001432 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1433 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001434 }
George Mount2c92c972014-03-20 09:38:23 -07001435 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001436 return;
1437 } catch (RemoteException e) {
1438 Slog.w(TAG, "Exception when starting activity "
1439 + r.intent.getComponent().flattenToShortString(), e);
1440 }
1441
1442 // If a dead object exception was thrown -- fall through to
1443 // restart the application.
1444 }
1445
1446 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001447 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001448 }
1449
Craig Mautner6170f732013-04-02 13:05:23 -07001450 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001451 Intent intent, String resolvedType, ActivityInfo aInfo,
1452 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1453 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001454 int callingPid, int callingUid, String callingPackage,
Chong Zhang280d3322015-11-03 17:27:26 -08001455 int realCallingPid, int realCallingUid, int startFlags, ActivityOptions options,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001456 boolean ignoreTargetSecurity, boolean componentSpecified, ActivityRecord[] outActivity,
1457 ActivityContainer container, TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001458 int err = ActivityManager.START_SUCCESS;
1459
1460 ProcessRecord callerApp = null;
1461 if (caller != null) {
1462 callerApp = mService.getRecordForAppLocked(caller);
1463 if (callerApp != null) {
1464 callingPid = callerApp.pid;
1465 callingUid = callerApp.info.uid;
1466 } else {
1467 Slog.w(TAG, "Unable to find app for caller " + caller
1468 + " (pid=" + callingPid + ") when starting: "
1469 + intent.toString());
1470 err = ActivityManager.START_PERMISSION_DENIED;
1471 }
1472 }
1473
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001474 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1475
Craig Mautner6170f732013-04-02 13:05:23 -07001476 if (err == ActivityManager.START_SUCCESS) {
Craig Mautner6170f732013-04-02 13:05:23 -07001477 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001478 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001479 + " on display " + (container == null ? (mFocusedStack == null ?
1480 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1481 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1482 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001483 }
1484
1485 ActivityRecord sourceRecord = null;
1486 ActivityRecord resultRecord = null;
1487 if (resultTo != null) {
1488 sourceRecord = isInAnyStackLocked(resultTo);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001489 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
1490 "Will send result to " + resultTo + " " + sourceRecord);
Craig Mautner6170f732013-04-02 13:05:23 -07001491 if (sourceRecord != null) {
1492 if (requestCode >= 0 && !sourceRecord.finishing) {
1493 resultRecord = sourceRecord;
1494 }
1495 }
1496 }
Craig Mautner6170f732013-04-02 13:05:23 -07001497
Dianne Hackborn91097de2014-04-04 18:02:06 -07001498 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001499
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001500 if ((launchFlags & Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001501 // Transfer the result target from the source activity to the new
1502 // one being started, including any failures.
1503 if (requestCode >= 0) {
1504 ActivityOptions.abort(options);
1505 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1506 }
1507 resultRecord = sourceRecord.resultTo;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001508 if (resultRecord != null && !resultRecord.isInStackLocked()) {
1509 resultRecord = null;
1510 }
Craig Mautner6170f732013-04-02 13:05:23 -07001511 resultWho = sourceRecord.resultWho;
1512 requestCode = sourceRecord.requestCode;
1513 sourceRecord.resultTo = null;
1514 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001515 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001516 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001517 if (sourceRecord.launchedFromUid == callingUid) {
1518 // The new activity is being launched from the same uid as the previous
1519 // activity in the flow, and asking to forward its result back to the
1520 // previous. In this case the activity is serving as a trampoline between
1521 // the two, so we also want to update its launchedFromPackage to be the
1522 // same as the previous activity. Note that this is safe, since we know
1523 // these two packages come from the same uid; the caller could just as
1524 // well have supplied that same package name itself. This specifially
1525 // deals with the case of an intent picker/chooser being launched in the app
1526 // flow to redirect to an activity picked by the user, where we want the final
1527 // activity to consider it to have been launched by the previous app activity.
1528 callingPackage = sourceRecord.launchedFromPackage;
1529 }
Craig Mautner6170f732013-04-02 13:05:23 -07001530 }
1531
1532 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1533 // We couldn't find a class that can handle the given Intent.
1534 // That's the end of that!
1535 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1536 }
1537
1538 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1539 // We couldn't find the specific class specified in the Intent.
1540 // Also the end of the line.
1541 err = ActivityManager.START_CLASS_NOT_FOUND;
1542 }
1543
Dianne Hackborn91097de2014-04-04 18:02:06 -07001544 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1545 && sourceRecord.task.voiceSession != null) {
1546 // If this activity is being launched as part of a voice session, we need
1547 // to ensure that it is safe to do so. If the upcoming activity will also
1548 // be part of the voice session, we can only launch it if it has explicitly
1549 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001550 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001551 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1552 try {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001553 intent.addCategory(Intent.CATEGORY_VOICE);
Dianne Hackborn95465202014-09-15 16:21:55 -07001554 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1555 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001556 Slog.w(TAG,
1557 "Activity being started in current voice task does not support voice: "
1558 + intent);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001559 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1560 }
1561 } catch (RemoteException e) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001562 Slog.w(TAG, "Failure checking voice capabilities", e);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001563 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1564 }
1565 }
1566 }
1567
1568 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1569 // If the caller is starting a new voice session, just make sure the target
1570 // is actually allowing it to run this way.
1571 try {
1572 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1573 intent, resolvedType)) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001574 Slog.w(TAG,
1575 "Activity being started in new voice task does not support: "
1576 + intent);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001577 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1578 }
1579 } catch (RemoteException e) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001580 Slog.w(TAG, "Failure checking voice capabilities", e);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001581 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1582 }
1583 }
1584
louis_changcd5d1982014-08-01 10:09:08 +08001585 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1586
Craig Mautner6170f732013-04-02 13:05:23 -07001587 if (err != ActivityManager.START_SUCCESS) {
1588 if (resultRecord != null) {
1589 resultStack.sendActivityResultLocked(-1,
1590 resultRecord, resultWho, requestCode,
1591 Activity.RESULT_CANCELED, null);
1592 }
Craig Mautner6170f732013-04-02 13:05:23 -07001593 ActivityOptions.abort(options);
1594 return err;
1595 }
1596
Svetoslav7008b512015-06-24 18:47:07 -07001597 boolean abort = false;
1598
Svet Ganov99b60432015-06-27 13:15:22 -07001599 final int startAnyPerm = mService.checkPermission(
1600 START_ANY_ACTIVITY, callingPid, callingUid);
1601
1602 if (startAnyPerm != PERMISSION_GRANTED) {
1603 final int componentRestriction = getComponentRestrictionForCallingPackage(
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001604 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
Svet Ganov99b60432015-06-27 13:15:22 -07001605 final int actionRestriction = getActionRestrictionForCallingPackage(
1606 intent.getAction(), callingPackage, callingPid, callingUid);
1607
1608 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1609 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1610 if (resultRecord != null) {
1611 resultStack.sendActivityResultLocked(-1,
1612 resultRecord, resultWho, requestCode,
1613 Activity.RESULT_CANCELED, null);
1614 }
1615 String msg;
1616 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1617 msg = "Permission Denial: starting " + intent.toString()
1618 + " from " + callerApp + " (pid=" + callingPid
1619 + ", uid=" + callingUid + ")" + " with revoked permission "
1620 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1621 } else if (!aInfo.exported) {
1622 msg = "Permission Denial: starting " + intent.toString()
1623 + " from " + callerApp + " (pid=" + callingPid
1624 + ", uid=" + callingUid + ")"
1625 + " not exported from uid " + aInfo.applicationInfo.uid;
1626 } else {
1627 msg = "Permission Denial: starting " + intent.toString()
1628 + " from " + callerApp + " (pid=" + callingPid
1629 + ", uid=" + callingUid + ")"
1630 + " requires " + aInfo.permission;
1631 }
1632 Slog.w(TAG, msg);
1633 throw new SecurityException(msg);
1634 }
1635
1636 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1637 String message = "Appop Denial: starting " + intent.toString()
1638 + " from " + callerApp + " (pid=" + callingPid
1639 + ", uid=" + callingUid + ")"
1640 + " requires " + AppOpsManager.permissionToOp(
1641 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1642 Slog.w(TAG, message);
1643 abort = true;
1644 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1645 String message = "Appop Denial: starting " + intent.toString()
1646 + " from " + callerApp + " (pid=" + callingPid
1647 + ", uid=" + callingUid + ")"
1648 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1649 Slog.w(TAG, message);
1650 abort = true;
1651 }
Svetoslav7008b512015-06-24 18:47:07 -07001652 }
1653
1654 abort |= !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001655 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001656
Craig Mautner6170f732013-04-02 13:05:23 -07001657 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001658 try {
1659 // The Intent we give to the watcher has the extra data
1660 // stripped off, since it can contain private information.
1661 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001662 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001663 aInfo.applicationInfo.packageName);
1664 } catch (RemoteException e) {
1665 mService.mController = null;
1666 }
Ben Gruver5e207332013-04-03 17:41:37 -07001667 }
Craig Mautner6170f732013-04-02 13:05:23 -07001668
Clara Bayarrif7fea162015-10-22 16:09:52 +01001669 UserInfo user = getUserInfo(userId);
Clara Bayarri56878a92015-10-29 15:43:55 +00001670 KeyguardManager km = (KeyguardManager) mService.mContext
1671 .getSystemService(Context.KEYGUARD_SERVICE);
1672 if (user.isManagedProfile()
Clara Bayarri10ad84a2015-12-01 17:38:05 +00001673 && LockPatternUtils.isSeparateWorkChallengeEnabled()
Clara Bayarri56878a92015-10-29 15:43:55 +00001674 && km.isDeviceLocked(userId)) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001675 IIntentSender target = mService.getIntentSenderLocked(
1676 ActivityManager.INTENT_SENDER_ACTIVITY, callingPackage,
1677 Binder.getCallingUid(), userId, null, null, 0, new Intent[]{ intent },
1678 new String[]{ resolvedType },
1679 PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT
1680 | PendingIntent.FLAG_IMMUTABLE, null);
1681 int flags = intent.getFlags();
1682 intent = km.createConfirmDeviceCredentialIntent(null, null);
1683 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1684 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1685 intent.putExtra(Intent.EXTRA_PACKAGE_NAME, aInfo.packageName);
1686 intent.putExtra(Intent.EXTRA_INTENT, new IntentSender(target));
1687 intent.putExtra(Intent.EXTRA_USER_ID, userId);
1688 intent.setFlags(flags);
1689
1690 resolvedType = null;
1691 callingUid = realCallingUid;
1692 callingPid = realCallingPid;
1693
1694 UserInfo parent = UserManager.get(mService.mContext).getProfileParent(userId);
1695 aInfo = resolveActivity(intent, null, PackageManager.MATCH_DEFAULT_ONLY
1696 | ActivityManagerService.STOCK_PM_FLAGS, null, parent.id);
1697 }
1698
Ben Gruver5e207332013-04-03 17:41:37 -07001699 if (abort) {
1700 if (resultRecord != null) {
1701 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001702 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001703 }
Ben Gruver5e207332013-04-03 17:41:37 -07001704 // We pretend to the caller that it was really started, but
1705 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001706 ActivityOptions.abort(options);
1707 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001708 }
1709
Svet Ganov9c165d72015-12-01 19:52:26 -08001710 // If permissions need a review before any of the app components can run, we
1711 // launch the review activity and pass a pending intent to start the activity
1712 // we are to launching now after the review is completed.
1713 if (Build.PERMISSIONS_REVIEW_REQUIRED && aInfo != null) {
1714 if (mService.getPackageManagerInternalLocked().isPermissionsReviewRequired(
1715 aInfo.packageName, userId)) {
1716 IIntentSender target = mService.getIntentSenderLocked(
1717 ActivityManager.INTENT_SENDER_ACTIVITY, callingPackage,
1718 callingUid, userId, null, null, 0, new Intent[]{intent},
1719 new String[]{resolvedType}, PendingIntent.FLAG_CANCEL_CURRENT
1720 | PendingIntent.FLAG_ONE_SHOT, null);
1721
1722 Intent newIntent = new Intent(Intent.ACTION_REVIEW_PERMISSIONS);
1723 newIntent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1724 newIntent.putExtra(Intent.EXTRA_PACKAGE_NAME, aInfo.packageName);
1725 newIntent.putExtra(Intent.EXTRA_INTENT, new IntentSender(target));
1726 if (resultRecord != null) {
1727 newIntent.putExtra(Intent.EXTRA_RESULT_NEEDED, true);
1728 }
1729 newIntent.setFlags(intent.getFlags());
1730 intent = newIntent;
1731
1732 resolvedType = null;
1733 callingUid = realCallingUid;
1734 callingPid = realCallingPid;
1735
1736 aInfo = resolveActivity(intent, null, PackageManager.MATCH_DEFAULT_ONLY
1737 | ActivityManagerService.STOCK_PM_FLAGS, null, userId);
1738
1739 if (DEBUG_PERMISSIONS_REVIEW) {
1740 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true,
1741 true, false) + "} from uid " + callingUid + " on display "
1742 + (container == null ? (mFocusedStack == null ?
1743 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1744 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1745 container.mActivityDisplay.mDisplayId)));
1746 }
1747 }
1748 }
1749
Craig Mautner6170f732013-04-02 13:05:23 -07001750 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001751 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001752 requestCode, componentSpecified, voiceSession != null, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001753 if (outActivity != null) {
1754 outActivity[0] = r;
1755 }
1756
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001757 if (r.appTimeTracker == null && sourceRecord != null) {
1758 // If the caller didn't specify an explicit time tracker, we want to continue
1759 // tracking under any it has.
1760 r.appTimeTracker = sourceRecord.appTimeTracker;
1761 }
1762
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001763 final ActivityStack stack = mFocusedStack;
Dianne Hackborn91097de2014-04-04 18:02:06 -07001764 if (voiceSession == null && (stack.mResumedActivity == null
1765 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001766 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1767 realCallingPid, realCallingUid, "Activity start")) {
Chong Zhang280d3322015-11-03 17:27:26 -08001768 PendingActivityLaunch pal = new PendingActivityLaunch(r,
Robert Carr13997f52015-10-23 13:13:39 -07001769 sourceRecord, startFlags, stack, callerApp);
Craig Mautneree36c772014-07-16 14:56:05 -07001770 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001771 ActivityOptions.abort(options);
1772 return ActivityManager.START_SWITCHES_CANCELED;
1773 }
1774 }
1775
1776 if (mService.mDidAppSwitch) {
1777 // This is the second allowed switch since we stopped switches,
1778 // so now just generally allow switches. Use case: user presses
1779 // home (switches disabled, switch to home, mDidAppSwitch now true);
1780 // user taps a home icon (coming from home so allowed, we hit here
1781 // and now allow anyone to switch again).
1782 mService.mAppSwitchesAllowedTime = 0;
1783 } else {
1784 mService.mDidAppSwitch = true;
1785 }
1786
Craig Mautneree36c772014-07-16 14:56:05 -07001787 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001788
Dianne Hackborn91097de2014-04-04 18:02:06 -07001789 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001790 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001791
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001792 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001793 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001794 // activity start, but we are not actually doing an activity
1795 // switch... just dismiss the keyguard now, because we
1796 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001797 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001798 }
1799 return err;
1800 }
1801
Clara Bayarrif7fea162015-10-22 16:09:52 +01001802 private UserInfo getUserInfo(int userId) {
1803 final long identity = Binder.clearCallingIdentity();
1804 try {
1805 return UserManager.get(mService.mContext).getUserInfo(userId);
1806 } finally {
1807 Binder.restoreCallingIdentity(identity);
1808 }
1809 }
1810
Svet Ganov99b60432015-06-27 13:15:22 -07001811 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001812 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001813 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1814 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001815 == PackageManager.PERMISSION_DENIED) {
1816 return ACTIVITY_RESTRICTION_PERMISSION;
1817 }
1818
Christopher Tateff7add02015-08-17 10:23:22 -07001819 if (activityInfo.permission == null) {
1820 return ACTIVITY_RESTRICTION_NONE;
1821 }
1822
Svet Ganov99b60432015-06-27 13:15:22 -07001823 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1824 if (opCode == AppOpsManager.OP_NONE) {
1825 return ACTIVITY_RESTRICTION_NONE;
1826 }
1827
1828 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1829 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001830 if (!ignoreTargetSecurity) {
1831 return ACTIVITY_RESTRICTION_APPOP;
1832 }
Svet Ganov99b60432015-06-27 13:15:22 -07001833 }
1834
1835 return ACTIVITY_RESTRICTION_NONE;
1836 }
1837
Svetoslav7008b512015-06-24 18:47:07 -07001838 private int getActionRestrictionForCallingPackage(String action,
1839 String callingPackage, int callingPid, int callingUid) {
1840 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001841 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001842 }
1843
1844 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1845 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001846 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001847 }
1848
1849 final PackageInfo packageInfo;
1850 try {
1851 packageInfo = mService.mContext.getPackageManager()
1852 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1853 } catch (PackageManager.NameNotFoundException e) {
1854 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001855 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001856 }
1857
1858 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001859 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001860 }
1861
1862 if (mService.checkPermission(permission, callingPid, callingUid) ==
1863 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001864 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001865 }
1866
1867 final int opCode = AppOpsManager.permissionToOpCode(permission);
1868 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001869 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001870 }
1871
1872 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1873 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001874 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001875 }
1876
Svet Ganov99b60432015-06-27 13:15:22 -07001877 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001878 }
1879
Chong Zhang0fa656b2015-08-31 15:17:21 -07001880 ActivityStack computeStackFocus(ActivityRecord r, boolean newTask, Rect bounds) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001881 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001882
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001883 if (!(r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
1884 return mHomeStack;
1885 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001886
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001887 ActivityStack stack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001888
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001889 if (task != null && task.stack != null) {
1890 stack = task.stack;
1891 if (stack.isOnHomeDisplay()) {
1892 if (mFocusedStack != stack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001893 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001894 "computeStackFocus: Setting " + "focused stack to r=" + r
1895 + " task=" + task);
1896 } else {
1897 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1898 "computeStackFocus: Focused stack already=" + mFocusedStack);
Craig Mautner858d8a62013-04-23 17:08:34 -07001899 }
1900 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001901 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001902 }
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001903
1904 final ActivityContainer container = r.mInitialActivityContainer;
1905 if (container != null) {
1906 // The first time put it on the desired stack, after this put on task stack.
1907 r.mInitialActivityContainer = null;
1908 return container.mStack;
1909 }
1910
1911 // The fullscreen stack can contain any task regardless of if the task is resizeable
1912 // or not. So, we let the task go in the fullscreen task if it is the focus stack.
1913 // If the freeform stack has focus, and the activity to be launched is resizeable,
1914 // we can also put it in the focused stack.
1915 final boolean canUseFocusedStack =
1916 mFocusedStack.mStackId == FULLSCREEN_WORKSPACE_STACK_ID
1917 || mFocusedStack.mStackId == FREEFORM_WORKSPACE_STACK_ID && r.info.resizeable;
1918 if (canUseFocusedStack
1919 && (!newTask || mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
1920 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1921 "computeStackFocus: Have a focused stack=" + mFocusedStack);
1922 return mFocusedStack;
1923 }
1924
1925 // We first try to put the task in the first dynamic stack.
1926 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1927 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
1928 stack = homeDisplayStacks.get(stackNdx);
Wale Ogunwale3797c222015-10-27 14:21:58 -07001929 if (!StackId.isStaticStack(stack.mStackId)) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001930 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1931 "computeStackFocus: Setting focused stack=" + stack);
1932 return stack;
1933 }
1934 }
1935
1936 // If there is no suitable dynamic stack then we figure out which static stack to use.
1937 final int stackId = task != null ? task.getLaunchStackId() :
1938 bounds != null ? FREEFORM_WORKSPACE_STACK_ID :
1939 FULLSCREEN_WORKSPACE_STACK_ID;
1940 stack = getStack(stackId, CREATE_IF_NEEDED, ON_TOP);
1941 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS, "computeStackFocus: New stack r="
1942 + r + " stackId=" + stack.mStackId);
1943 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001944 }
1945
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001946 boolean setFocusedStack(ActivityRecord r, String reason) {
1947 if (r == null) {
1948 // Not sure what you are trying to do, but it is not going to work...
1949 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001950 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001951 final TaskRecord task = r.task;
1952 if (task == null || task.stack == null) {
1953 Slog.w(TAG, "Can't set focus stack for r=" + r + " task=" + task);
1954 return false;
1955 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001956 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001957 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001958 }
1959
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001960 final int startActivityUncheckedLocked(final ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001961 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Chong Zhang280d3322015-11-03 17:27:26 -08001962 boolean doResume, ActivityOptions options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001963 final Intent intent = r.intent;
1964 final int callingUid = r.launchedFromUid;
1965
Chong Zhang0fa656b2015-08-31 15:17:21 -07001966 boolean overrideBounds = false;
1967 Rect newBounds = null;
Chong Zhang56f171d2015-10-08 10:39:23 -07001968 if (options != null && (r.info.resizeable || (inTask != null && inTask.mResizeable))) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001969 if (canUseActivityOptionsLaunchBounds(options)) {
Chong Zhang0fa656b2015-08-31 15:17:21 -07001970 overrideBounds = true;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001971 newBounds = options.getLaunchBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07001972 }
1973 }
1974
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001975 // In some flows in to this function, we retrieve the task record and hold on to it
1976 // without a lock before calling back in to here... so the task at this point may
1977 // not actually be in recents. Check for that, and if it isn't in recents just
1978 // consider it invalid.
1979 if (inTask != null && !inTask.inRecents) {
1980 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1981 inTask = null;
1982 }
1983
Craig Mautnerad400af2014-08-13 16:41:36 -07001984 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001985 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1986 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001987
Craig Mautnera228ae92014-07-09 05:44:55 -07001988 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001989 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001990 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001991 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1992 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1993 "\"singleInstance\" or \"singleTask\"");
1994 launchFlags &=
1995 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1996 } else {
1997 switch (r.info.documentLaunchMode) {
1998 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1999 break;
2000 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
2001 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
2002 break;
2003 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
2004 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
2005 break;
2006 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
2007 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
2008 break;
2009 }
2010 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002011
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002012 final boolean launchTaskBehind = r.mLaunchTaskBehind
2013 && !launchSingleTask && !launchSingleInstance
2014 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002015
Wale Ogunwale7d701172015-03-11 15:36:30 -07002016 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0
2017 && r.resultTo.task.stack != null) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002018 // For whatever reason this activity is being launched into a new
2019 // task... yet the caller has requested a result back. Well, that
2020 // is pretty messed up, so instead immediately send back a cancel
2021 // and let the new task continue launched as normal without a
2022 // dependency on its originator.
2023 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
2024 r.resultTo.task.stack.sendActivityResultLocked(-1,
2025 r.resultTo, r.resultWho, r.requestCode,
2026 Activity.RESULT_CANCELED, null);
2027 r.resultTo = null;
2028 }
2029
2030 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
2031 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2032 }
2033
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002034 // If we are actually going to launch in to a new task, there are some cases where
2035 // we further want to do multiple task.
2036 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
2037 if (launchTaskBehind
2038 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
2039 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
2040 }
2041 }
2042
Craig Mautner8849a5e2013-04-02 16:41:03 -07002043 // We'll invoke onUserLeaving before onPause only if the launching
2044 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07002045 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002046 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
2047 "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002048
2049 // If the caller has asked not to resume at this point, we make note
2050 // of this in the record so that we can skip it when trying to find
2051 // the top running activity.
Chong Zhang45c25ce2015-08-10 22:18:26 -07002052 if (!doResume || !okToShowLocked(r)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002053 r.delayedResume = true;
Chong Zhang45c25ce2015-08-10 22:18:26 -07002054 doResume = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002055 }
2056
Craig Mautnera254cd72014-05-25 16:47:39 -07002057 ActivityRecord notTop =
2058 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002059
2060 // If the onlyIfNeeded flag is set, then we can do this if the activity
2061 // being launched is the same as the one making the call... or, as
2062 // a special case, if we do not know the caller then we count the
2063 // current top activity as the caller.
2064 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2065 ActivityRecord checkedCaller = sourceRecord;
2066 if (checkedCaller == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002067 checkedCaller = mFocusedStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002068 }
2069 if (!checkedCaller.realActivity.equals(r.realActivity)) {
2070 // Caller is not the same as launcher, so always needed.
2071 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
2072 }
2073 }
2074
Craig Mautner8849a5e2013-04-02 16:41:03 -07002075 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002076 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002077
2078 // If the caller is not coming from another activity, but has given us an
2079 // explicit task into which they would like us to launch the new activity,
2080 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002081 if (sourceRecord == null && inTask != null && inTask.stack != null) {
2082 final Intent baseIntent = inTask.getBaseIntent();
2083 final ActivityRecord root = inTask.getRootActivity();
2084 if (baseIntent == null) {
2085 ActivityOptions.abort(options);
2086 throw new IllegalArgumentException("Launching into task without base intent: "
2087 + inTask);
2088 }
2089
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002090 // If this task is empty, then we are adding the first activity -- it
2091 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002092 if (launchSingleInstance || launchSingleTask) {
2093 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
2094 ActivityOptions.abort(options);
2095 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
2096 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002097 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002098 if (root != null) {
2099 ActivityOptions.abort(options);
2100 throw new IllegalArgumentException("Caller with inTask " + inTask
2101 + " has root " + root + " but target is singleInstance/Task");
2102 }
2103 }
2104
2105 // If task is empty, then adopt the interesting intent launch flags in to the
2106 // activity being started.
2107 if (root == null) {
2108 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
2109 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
2110 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
2111 launchFlags = (launchFlags&~flagsOfInterest)
2112 | (baseIntent.getFlags()&flagsOfInterest);
2113 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002114 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07002115 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002116
Dianne Hackborn962d5352014-08-29 16:27:40 -07002117 // If the task is not empty and the caller is asking to start it as the root
2118 // of a new task, then we don't actually want to start this on the task. We
2119 // will bring the task to the front, and possibly give it a new intent.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002120 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07002121 addingToTask = false;
2122
2123 } else {
2124 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002125 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07002126
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002127 reuseTask = inTask;
2128 } else {
2129 inTask = null;
Chong Zhang85ee6542015-10-02 13:36:38 -07002130 // Launch ResolverActivity in the source task, so that it stays in the task
2131 // bounds when in freeform workspace.
2132 // Also put noDisplay activities in the source task. These by itself can
2133 // be placed in any task/stack, however it could launch other activities
2134 // like ResolverActivity, and we want those to stay in the original task.
2135 if (r.isResolverActivity() || r.noDisplay) {
2136 addingToTask = true;
2137 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002138 }
2139
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002140 if (inTask == null) {
2141 if (sourceRecord == null) {
2142 // This activity is not being started from another... in this
2143 // case we -always- start a new task.
2144 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
2145 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
2146 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
2147 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2148 }
2149 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
2150 // The original activity who is starting us is running as a single
2151 // instance... this new activity it is starting must go on its
2152 // own task.
2153 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2154 } else if (launchSingleInstance || launchSingleTask) {
2155 // The activity being started is a single instance... it always
2156 // gets launched into its own task.
2157 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2158 }
2159 }
2160
2161 ActivityInfo newTaskInfo = null;
2162 Intent newTaskIntent = null;
2163 ActivityStack sourceStack;
2164 if (sourceRecord != null) {
2165 if (sourceRecord.finishing) {
2166 // If the source is finishing, we can't further count it as our source. This
2167 // is because the task it is associated with may now be empty and on its way out,
2168 // so we don't want to blindly throw it in to that task. Instead we will take
2169 // the NEW_TASK flow and try to find a task for it. But save the task information
2170 // so it can be used when creating the new task.
2171 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
2172 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
2173 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
2174 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2175 newTaskInfo = sourceRecord.info;
2176 newTaskIntent = sourceRecord.task.intent;
2177 }
2178 sourceRecord = null;
2179 sourceStack = null;
2180 } else {
2181 sourceStack = sourceRecord.task.stack;
2182 }
2183 } else {
2184 sourceStack = null;
2185 }
2186
2187 boolean movedHome = false;
2188 ActivityStack targetStack;
2189
2190 intent.setFlags(launchFlags);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002191 final boolean noAnimation = (launchFlags & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002192
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002193 // We may want to try to place the new activity in to an existing task. We always
2194 // do this if the target activity is singleTask or singleInstance; we will also do
2195 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
2196 // us to still place it in a new task: multi task, always doc mode, or being asked to
2197 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07002198 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
2199 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07002200 || launchSingleInstance || launchSingleTask) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002201 // If bring to front is requested, and no result is requested and we have not
2202 // been given an explicit task to launch in to, and
Craig Mautner8849a5e2013-04-02 16:41:03 -07002203 // we can find a task that was started with this same
2204 // component, then instead of launching bring that one to the front.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002205 if (inTask == null && r.resultTo == null) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002206 // See if there is a task to bring to the front. If this is
2207 // a SINGLE_INSTANCE activity, there can be one and only one
2208 // instance of it in the history, and it is always in its own
2209 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07002210 ActivityRecord intentActivity = !launchSingleInstance ?
2211 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002212 if (intentActivity != null) {
Jason Monk25d237b2015-06-19 10:39:39 -04002213 // When the flags NEW_TASK and CLEAR_TASK are set, then the task gets reused
2214 // but still needs to be a lock task mode violation since the task gets
2215 // cleared out and the device would otherwise leave the locked task.
2216 if (isLockTaskModeViolation(intentActivity.task,
2217 (launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
2218 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))) {
Craig Mautner6cd6cec2015-04-01 00:02:12 -07002219 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07002220 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08002221 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2222 }
Craig Mautner29219d92013-04-16 20:19:12 -07002223 if (r.task == null) {
2224 r.task = intentActivity.task;
2225 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002226 if (intentActivity.task.intent == null) {
2227 // This task was started because of movement of
2228 // the activity based on affinity... now that we
2229 // are actually launching it, we can assign the
2230 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07002231 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002232 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002233 targetStack = intentActivity.task.stack;
2234 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002235 // If the target task is not in the front, then we need
2236 // to bring it to the front... except... well, with
2237 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
2238 // to have the same behavior as if a new instance was
2239 // being started, which means not bringing it to the front
2240 // if the caller is not itself in the front.
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002241 final ActivityStack focusStack = getFocusedStack();
2242 ActivityRecord curTop = (focusStack == null)
2243 ? null : focusStack.topRunningNonDelayedActivityLocked(notTop);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002244 boolean movedToFront = false;
Craig Mautner7504d7b2013-09-17 10:50:53 -07002245 if (curTop != null && (curTop.task != intentActivity.task ||
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002246 curTop.task != focusStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002247 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07002248 if (sourceRecord == null || (sourceStack.topActivity() != null &&
2249 sourceStack.topActivity().task == sourceRecord.task)) {
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002250 // We really do want to push this one into the user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07002251 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002252 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
2253 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002254 movedHome = true;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002255 targetStack.moveTaskToFrontLocked(intentActivity.task, noAnimation,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002256 options, r.appTimeTracker, "bringingFoundTaskToFront");
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002257 movedToFront = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002258 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07002259 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2260 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07002261 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07002262 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002263 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002264 options = null;
2265 }
2266 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002267 if (!movedToFront && doResume) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002268 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Bring to front target: " + targetStack
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002269 + " from " + intentActivity);
2270 targetStack.moveToFront("intentActivityFound");
2271 }
2272
Craig Mautner8849a5e2013-04-02 16:41:03 -07002273 // If the caller has requested that the target task be
2274 // reset, then do so.
2275 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
2276 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
2277 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002278 if ((startFlags & ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002279 // We don't need to start a new activity, and
2280 // the client said not to do anything if that
2281 // is the case, so this is it! And for paranoia, make
2282 // sure we have correctly resumed the top activity.
2283 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002284 resumeTopActivitiesLocked(targetStack, null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002285
2286 // Make sure to notify Keyguard as well if we are not running an app
2287 // transition later.
2288 if (!movedToFront) {
2289 notifyActivityDrawnForKeyguard();
2290 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002291 } else {
2292 ActivityOptions.abort(options);
2293 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002294 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002295 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2296 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002297 if ((launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002298 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002299 // The caller has requested to completely replace any
2300 // existing task with its new activity. Well that should
2301 // not be too hard...
2302 reuseTask = intentActivity.task;
2303 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07002304 reuseTask.setIntent(r);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002305 } else if ((launchFlags & FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07002306 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002307 // In this situation we want to remove all activities
2308 // from the task up to the one being started. In most
2309 // cases this means we are resetting the task to its
2310 // initial state.
2311 ActivityRecord top =
2312 intentActivity.task.performClearTaskLocked(r, launchFlags);
2313 if (top != null) {
2314 if (top.frontOfTask) {
2315 // Activity aliases may mean we use different
2316 // intents for the top activity, so make sure
2317 // the task now has the identity of the new
2318 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07002319 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002320 }
2321 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
2322 r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002323 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002324 } else {
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002325 // A special case: we need to start the activity because it is not
2326 // currently running, and the caller has asked to clear the current
2327 // task to have this activity at the top.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002328 addingToTask = true;
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002329 // Now pretend like this activity is being started by the top of its
2330 // task, so it is put in the right place.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002331 sourceRecord = intentActivity;
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002332 TaskRecord task = sourceRecord.task;
2333 if (task != null && task.stack == null) {
2334 // Target stack got cleared when we all activities were removed
2335 // above. Go ahead and reset it.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002336 targetStack = computeStackFocus(
2337 sourceRecord, false /* newTask */, null /* bounds */);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002338 targetStack.addTask(task,
2339 !launchTaskBehind /* toTop */, "startActivityUnchecked");
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002340 }
2341
Craig Mautner8849a5e2013-04-02 16:41:03 -07002342 }
2343 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
2344 // In this case the top activity on the task is the
2345 // same as the one being launched, so we take that
2346 // as a request to bring the task to the foreground.
2347 // If the top activity in the task is the root
2348 // activity, deliver this new intent to it if it
2349 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07002350 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07002351 && intentActivity.realActivity.equals(r.realActivity)) {
2352 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
2353 intentActivity.task);
2354 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07002355 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002356 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002357 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
2358 r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002359 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
2360 // In this case we are launching the root activity
2361 // of the task, but with a different intent. We
2362 // should start a new instance on top.
2363 addingToTask = true;
2364 sourceRecord = intentActivity;
2365 }
2366 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
2367 // In this case an activity is being launched in to an
2368 // existing task, without resetting that task. This
2369 // is typically the situation of launching an activity
2370 // from a notification or shortcut. We want to place
2371 // the new activity on top of the current task.
2372 addingToTask = true;
2373 sourceRecord = intentActivity;
2374 } else if (!intentActivity.task.rootWasReset) {
2375 // In this case we are launching in to an existing task
2376 // that has not yet been started from its front door.
2377 // The current task has been brought to the front.
2378 // Ideally, we'd probably like to place this new task
2379 // at the bottom of its stack, but that's a little hard
2380 // to do with the current organization of the code so
2381 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07002382 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002383 }
2384 if (!addingToTask && reuseTask == null) {
2385 // We didn't do anything... but it was needed (a.k.a., client
2386 // don't use that intent!) And for paranoia, make
2387 // sure we have correctly resumed the top activity.
2388 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002389 targetStack.resumeTopActivityLocked(null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002390 if (!movedToFront) {
2391 // Make sure to notify Keyguard as well if we are not running an app
2392 // transition later.
2393 notifyActivityDrawnForKeyguard();
2394 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002395 } else {
2396 ActivityOptions.abort(options);
2397 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002398 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002399 return ActivityManager.START_TASK_TO_FRONT;
2400 }
2401 }
2402 }
2403 }
2404
2405 //String uri = r.intent.toURI();
2406 //Intent intent2 = new Intent(uri);
2407 //Slog.i(TAG, "Given intent: " + r.intent);
2408 //Slog.i(TAG, "URI is: " + uri);
2409 //Slog.i(TAG, "To intent: " + intent2);
2410
2411 if (r.packageName != null) {
2412 // If the activity being launched is the same as the one currently
2413 // at the top, then we need to check if it should only be launched
2414 // once.
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002415 ActivityStack topStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002416 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002417 if (top != null && r.resultTo == null) {
2418 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2419 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07002420 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07002421 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002422 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
2423 top.task);
2424 // For paranoia, make sure we have correctly
2425 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002426 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002427 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002428 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002429 }
2430 ActivityOptions.abort(options);
2431 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2432 // We don't need to start a new activity, and
2433 // the client said not to do anything if that
2434 // is the case, so this is it!
2435 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2436 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002437 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002438 return ActivityManager.START_DELIVERED_TO_TOP;
2439 }
2440 }
2441 }
2442 }
2443
2444 } else {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002445 if (r.resultTo != null && r.resultTo.task.stack != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002446 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2447 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002448 }
2449 ActivityOptions.abort(options);
2450 return ActivityManager.START_CLASS_NOT_FOUND;
2451 }
2452
2453 boolean newTask = false;
2454 boolean keepCurTransition = false;
2455
Craig Mautnerbb742462014-07-07 15:28:55 -07002456 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002457 sourceRecord.task : null;
2458
Craig Mautner8849a5e2013-04-02 16:41:03 -07002459 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002460 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002461 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002462 newTask = true;
Chong Zhang0fa656b2015-08-31 15:17:21 -07002463 targetStack = computeStackFocus(r, newTask, newBounds);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002464 if (doResume) {
2465 targetStack.moveToFront("startingNewTask");
2466 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002467
Craig Mautner8849a5e2013-04-02 16:41:03 -07002468 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002469 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2470 newTaskInfo != null ? newTaskInfo : r.info,
2471 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002472 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2473 taskToAffiliate);
Chong Zhang0fa656b2015-08-31 15:17:21 -07002474 if (overrideBounds) {
2475 r.task.updateOverrideConfiguration(newBounds);
2476 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002477 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2478 "Starting new activity " + r + " in new task " + r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002479 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002480 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002481 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002482 if (isLockTaskModeViolation(r.task)) {
2483 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2484 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2485 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002486 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002487 if ((launchFlags &
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002488 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2489 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002490 // Caller wants to appear on home activity, so before starting
2491 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002492 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002493 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002494 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002495 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002496 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002497 if (isLockTaskModeViolation(sourceTask)) {
2498 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2499 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2500 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002501 targetStack = sourceTask.stack;
Chong Zhang45c25ce2015-08-10 22:18:26 -07002502 if (doResume) {
2503 targetStack.moveToFront("sourceStackToFront");
2504 }
Craig Mautner737fae22014-10-15 12:52:10 -07002505 final TaskRecord topTask = targetStack.topTask();
2506 if (topTask != sourceTask) {
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002507 targetStack.moveTaskToFrontLocked(sourceTask, noAnimation, options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002508 r.appTimeTracker, "sourceTaskToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002509 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002510 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002511 // In this case, we are adding the activity to an existing
2512 // task, but the caller has asked to clear that task if the
2513 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002514 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002515 keepCurTransition = true;
2516 if (top != null) {
2517 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002518 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002519 // For paranoia, make sure we have correctly
2520 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002521 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002522 if (doResume) {
2523 targetStack.resumeTopActivityLocked(null);
2524 }
2525 ActivityOptions.abort(options);
2526 return ActivityManager.START_DELIVERED_TO_TOP;
2527 }
2528 } else if (!addingToTask &&
2529 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2530 // In this case, we are launching an activity in our own task
2531 // that may already be running somewhere in the history, and
2532 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002533 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002534 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002535 final TaskRecord task = top.task;
2536 task.moveActivityToFrontLocked(top);
2537 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002538 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002539 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002540 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002541 if (doResume) {
2542 targetStack.resumeTopActivityLocked(null);
2543 }
2544 return ActivityManager.START_DELIVERED_TO_TOP;
2545 }
2546 }
2547 // An existing activity is starting this new activity, so we want
2548 // to keep the new one in the same task as the one that is starting
2549 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002550 r.setTask(sourceTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002551 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002552 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002553
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002554 } else if (inTask != null) {
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002555 // The caller is asking that the new activity be started in an explicit
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002556 // task it has provided to us.
2557 if (isLockTaskModeViolation(inTask)) {
2558 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2559 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2560 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002561 if (overrideBounds) {
2562 inTask.updateOverrideConfiguration(newBounds);
2563 int stackId = inTask.getLaunchStackId();
2564 if (stackId != inTask.stack.mStackId) {
2565 moveTaskToStackUncheckedLocked(
2566 inTask, stackId, ON_TOP, !FORCE_FOCUS, "inTaskToFront");
2567 }
2568 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002569 targetStack = inTask.stack;
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002570 targetStack.moveTaskToFrontLocked(inTask, noAnimation, options, r.appTimeTracker,
2571 "inTaskToFront");
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002572
2573 // Check whether we should actually launch the new activity in to the task,
2574 // or just reuse the current activity on top.
2575 ActivityRecord top = inTask.getTopActivity();
2576 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2577 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2578 || launchSingleTop || launchSingleTask) {
2579 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2580 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2581 // We don't need to start a new activity, and
2582 // the client said not to do anything if that
2583 // is the case, so this is it!
2584 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2585 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002586 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002587 return ActivityManager.START_DELIVERED_TO_TOP;
2588 }
2589 }
2590
Dianne Hackborn962d5352014-08-29 16:27:40 -07002591 if (!addingToTask) {
2592 // We don't actually want to have this activity added to the task, so just
2593 // stop here but still tell the caller that we consumed the intent.
2594 ActivityOptions.abort(options);
2595 return ActivityManager.START_TASK_TO_FRONT;
2596 }
2597
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002598 r.setTask(inTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002599 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002600 + " in explicit task " + r.task);
2601
Craig Mautner8849a5e2013-04-02 16:41:03 -07002602 } else {
2603 // This not being started from an existing activity, and not part
2604 // of a new task... just put it in the top task, though these days
2605 // this case should never happen.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002606 targetStack = computeStackFocus(r, newTask, null /* bounds */);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002607 if (doResume) {
2608 targetStack.moveToFront("addingToTopTask");
2609 }
Craig Mautner1602ec22013-05-12 10:24:27 -07002610 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002611 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002612 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002613 mWindowManager.moveTaskToTop(r.task.taskId);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002614 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Craig Mautner8849a5e2013-04-02 16:41:03 -07002615 + " in new guessed " + r.task);
2616 }
2617
2618 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002619 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002620
Craig Mautner76e2a762014-06-24 09:05:43 -07002621 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2622 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2623 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002624 if (newTask) {
2625 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2626 }
2627 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002628 targetStack.mLastPausedActivity = null;
Filip Gruszczynski3d82ed62015-12-10 10:41:39 -08002629 targetStack.startActivityLocked(r, newTask, keepCurTransition, options);
2630 if (doResume) {
2631 if (!launchTaskBehind) {
2632 mService.setFocusedActivityLocked(r, "startedActivity");
2633 }
2634 resumeTopActivitiesLocked(targetStack, r, options);
2635 } else {
2636 targetStack.addRecentActivityLocked(r);
Craig Mautnerbb742462014-07-07 15:28:55 -07002637 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002638 updateUserStackLocked(r.userId, targetStack);
Chong Zhangc806d902015-11-30 09:44:27 -08002639
2640 if (!r.task.mResizeable && isStackDockedInEffect(targetStack.mStackId)) {
2641 showNonResizeableDockToast(r.task.taskId);
2642 }
2643
Craig Mautner8849a5e2013-04-02 16:41:03 -07002644 return ActivityManager.START_SUCCESS;
2645 }
2646
Craig Mautneree36c772014-07-16 14:56:05 -07002647 final void doPendingActivityLaunchesLocked(boolean doResume) {
2648 while (!mPendingActivityLaunches.isEmpty()) {
2649 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Robert Carrf1ddb5e2015-10-20 14:44:45 -07002650
2651 try {
2652 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Robert Carr13997f52015-10-23 13:13:39 -07002653 doResume && mPendingActivityLaunches.isEmpty(), null, null);
Robert Carrf1ddb5e2015-10-20 14:44:45 -07002654 } catch (Exception e) {
Robert Carr13997f52015-10-23 13:13:39 -07002655 Slog.e(TAG, "Exception during pending activity launch pal=" + pal, e);
2656 pal.sendErrorResult(e.getMessage());
Robert Carrf1ddb5e2015-10-20 14:44:45 -07002657 }
Craig Mautneree36c772014-07-16 14:56:05 -07002658 }
2659 }
2660
Craig Mautner7f13ed32014-07-28 14:00:35 -07002661 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002662 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2663 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002664 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002665 mPendingActivityLaunches.remove(palNdx);
2666 }
2667 }
2668 }
2669
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002670 void setLaunchSource(int uid) {
2671 mLaunchingActivity.setWorkSource(new WorkSource(uid));
2672 }
2673
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002674 void acquireLaunchWakelock() {
2675 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2676 throw new IllegalStateException("Calling must be system uid");
2677 }
2678 mLaunchingActivity.acquire();
2679 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2680 // To be safe, don't allow the wake lock to be held for too long.
2681 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2682 }
2683 }
2684
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002685 /**
2686 * Called when the frontmost task is idle.
2687 * @return the state of mService.mBooting before this was called.
2688 */
2689 private boolean checkFinishBootingLocked() {
2690 final boolean booting = mService.mBooting;
2691 boolean enableScreen = false;
2692 mService.mBooting = false;
2693 if (!mService.mBooted) {
2694 mService.mBooted = true;
2695 enableScreen = true;
2696 }
2697 if (booting || enableScreen) {
2698 mService.postFinishBooting(booting, enableScreen);
2699 }
2700 return booting;
2701 }
2702
Craig Mautnerf3333272013-04-22 10:55:53 -07002703 // Checked.
2704 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2705 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002706 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002707
Craig Mautnerf3333272013-04-22 10:55:53 -07002708 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07002709 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07002710 int NS = 0;
2711 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002712 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002713 boolean activityRemoved = false;
2714
Wale Ogunwale7d701172015-03-11 15:36:30 -07002715 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002716 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002717 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
2718 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002719 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2720 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002721 if (fromTimeout) {
2722 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002723 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002724
2725 // This is a hack to semi-deal with a race condition
2726 // in the client where it can be constructed with a
2727 // newer configuration from when we asked it to launch.
2728 // We'll update with whatever configuration it now says
2729 // it used to launch.
2730 if (config != null) {
2731 r.configuration = config;
2732 }
2733
2734 // We are now idle. If someone is waiting for a thumbnail from
2735 // us, we can now deliver.
2736 r.idle = true;
2737
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002738 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002739 if (isFocusedStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002740 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002741 }
2742 }
2743
2744 if (allResumedActivitiesIdle()) {
2745 if (r != null) {
2746 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002747 }
2748
2749 if (mLaunchingActivity.isHeld()) {
2750 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2751 if (VALIDATE_WAKE_LOCK_CALLER &&
2752 Binder.getCallingUid() != Process.myUid()) {
2753 throw new IllegalStateException("Calling must be system uid");
2754 }
2755 mLaunchingActivity.release();
2756 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002757 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07002758 }
2759
2760 // Atomically retrieve all of the other things to do.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08002761 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(true);
Craig Mautnerf3333272013-04-22 10:55:53 -07002762 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002763 if ((NF = mFinishingActivities.size()) > 0) {
2764 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002765 mFinishingActivities.clear();
2766 }
2767
Craig Mautnerf3333272013-04-22 10:55:53 -07002768 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002769 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07002770 mStartingUsers.clear();
2771 }
2772
Craig Mautnerf3333272013-04-22 10:55:53 -07002773 // Stop any activities that are scheduled to do so but have been
2774 // waiting for the next one to start.
2775 for (int i = 0; i < NS; i++) {
2776 r = stops.get(i);
2777 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002778 if (stack != null) {
2779 if (r.finishing) {
2780 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2781 } else {
2782 stack.stopActivityLocked(r);
2783 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002784 }
2785 }
2786
2787 // Finish any activities that are scheduled to do so but have been
2788 // waiting for the next one to start.
2789 for (int i = 0; i < NF; i++) {
2790 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07002791 final ActivityStack stack = r.task.stack;
2792 if (stack != null) {
2793 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
2794 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002795 }
2796
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002797 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002798 // Complete user switch
2799 if (startingUsers != null) {
2800 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002801 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002802 }
2803 }
2804 // Complete starting up of background users
2805 if (mStartingBackgroundUsers.size() > 0) {
Amith Yamasani37a40c22015-06-17 13:25:42 -07002806 startingUsers = new ArrayList<UserState>(mStartingBackgroundUsers);
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002807 mStartingBackgroundUsers.clear();
2808 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002809 mService.mUserController.finishUserBoot(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002810 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002811 }
2812 }
2813
2814 mService.trimApplications();
2815 //dump();
2816 //mWindowManager.dump();
2817
Craig Mautnerf3333272013-04-22 10:55:53 -07002818 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002819 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002820 }
2821
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002822 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002823 }
2824
Craig Mautner8e569572013-10-11 17:36:59 -07002825 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002826 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002827 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2828 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002829 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2830 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2831 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002832 }
Craig Mautner19091252013-10-05 00:03:53 -07002833 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002834 }
2835
2836 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002837 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2838 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002839 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2840 stacks.get(stackNdx).closeSystemDialogsLocked();
2841 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002842 }
2843 }
2844
Craig Mautner93529a42013-10-04 15:03:13 -07002845 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002846 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002847 }
2848
Craig Mautner8d341ef2013-03-26 09:03:27 -07002849 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07002850 * Update the last used stack id for non-current user (current user's last
2851 * used stack is the focused stack)
2852 */
2853 void updateUserStackLocked(int userId, ActivityStack stack) {
2854 if (userId != mCurrentUser) {
2855 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
2856 }
2857 }
2858
2859 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07002860 * @return true if some activity was finished (or would have finished if doit were true).
2861 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07002862 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
2863 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002864 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002865 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2866 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08002867 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002868 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07002869 if (stack.finishDisabledPackageActivitiesLocked(
2870 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002871 didSomething = true;
2872 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002873 }
2874 }
2875 return didSomething;
2876 }
2877
Dianne Hackborna413dc02013-07-12 12:02:55 -07002878 void updatePreviousProcessLocked(ActivityRecord r) {
2879 // Now that this process has stopped, we may want to consider
2880 // it to be the previous app to try to keep around in case
2881 // the user wants to return to it.
2882
2883 // First, found out what is currently the foreground app, so that
2884 // we don't blow away the previous app if this activity is being
2885 // hosted by the process that is actually still the foreground.
2886 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002887 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2888 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002889 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2890 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002891 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002892 if (stack.mResumedActivity != null) {
2893 fgApp = stack.mResumedActivity.app;
2894 } else if (stack.mPausingActivity != null) {
2895 fgApp = stack.mPausingActivity.app;
2896 }
2897 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002898 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002899 }
2900 }
2901
2902 // Now set this one as the previous process, only if that really
2903 // makes sense to.
2904 if (r.app != null && fgApp != null && r.app != fgApp
2905 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002906 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002907 mService.mPreviousProcess = r.app;
2908 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2909 }
2910 }
2911
Craig Mautner05d29032013-05-03 13:40:13 -07002912 boolean resumeTopActivitiesLocked() {
2913 return resumeTopActivitiesLocked(null, null, null);
2914 }
2915
Chong Zhang280d3322015-11-03 17:27:26 -08002916 boolean resumeTopActivitiesLocked(
2917 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Craig Mautner05d29032013-05-03 13:40:13 -07002918 if (targetStack == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002919 targetStack = mFocusedStack;
Craig Mautner05d29032013-05-03 13:40:13 -07002920 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002921 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002922 boolean result = false;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002923 if (isFocusedStack(targetStack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002924 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2925 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002926
Craig Mautnere0a38842013-12-16 16:14:02 -08002927 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2928 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002929 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2930 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002931 if (stack == targetStack) {
2932 // Already started above.
2933 continue;
2934 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002935 if (isFocusedStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002936 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002937 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002938 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002939 }
Craig Mautner05d29032013-05-03 13:40:13 -07002940 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002941 }
2942
Todd Kennedy539db512014-12-15 09:57:55 -08002943 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002944 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2945 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002946 final int numStacks = stacks.size();
2947 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2948 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08002949 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002950 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002951 }
2952 }
2953
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002954 void finishVoiceTask(IVoiceInteractionSession session) {
2955 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2956 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2957 final int numStacks = stacks.size();
2958 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2959 final ActivityStack stack = stacks.get(stackNdx);
2960 stack.finishVoiceTask(session);
2961 }
2962 }
2963 }
2964
Chong Zhang280d3322015-11-03 17:27:26 -08002965 void findTaskToMoveToFrontLocked(
2966 TaskRecord task, int flags, ActivityOptions options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002967 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2968 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002969 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002970 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2971 // Caller wants the home activity moved with it. To accomplish this,
2972 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002973 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002974 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07002975 if (task.stack == null) {
2976 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2977 + task + " to front. Stack is null");
2978 return;
2979 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002980
Chong Zhang0fa656b2015-08-31 15:17:21 -07002981 if (task.mResizeable && options != null) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002982 if (canUseActivityOptionsLaunchBounds(options)) {
2983 Rect bounds = options.getLaunchBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07002984 task.updateOverrideConfiguration(bounds);
Chong Zhang112eb8c2015-11-02 11:17:00 -08002985 final int stackId = task.getLaunchStackId();
2986 if (stackId != task.stack.mStackId) {
2987 moveTaskToStackUncheckedLocked(task, stackId, ON_TOP, !FORCE_FOCUS, reason);
2988 // moveTaskToStackUncheckedLocked() should already placed the task on top,
2989 // still need moveTaskToFrontLocked() below for any transition settings.
2990 }
2991 // WM resizeTask must be done after the task is moved to the correct stack,
2992 // because Task's setBounds() also updates dim layer's bounds, but that has
2993 // dependency on the stack.
Chong Zhang87b21722015-09-21 15:39:51 -07002994 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig,
2995 false /*relayout*/, false /*forced*/);
Chong Zhang0fa656b2015-08-31 15:17:21 -07002996 }
2997 }
2998
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002999 final ActivityRecord r = task.getTopActivity();
3000 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
3001 r == null ? null : r.appTimeTracker, reason);
3002
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003003 if (DEBUG_STACK) Slog.d(TAG_STACK,
3004 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003005 }
3006
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08003007 private boolean canUseActivityOptionsLaunchBounds(ActivityOptions options) {
3008 // We use the launch bounds in the activity options is the device supports freeform
3009 // window management.
3010 return options.hasLaunchBounds() && mService.mSupportsFreeformWindowManagement;
3011 }
3012
Craig Mautner967212c2013-04-13 21:10:58 -07003013 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07003014 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003015 }
3016
3017 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003018 ActivityContainer activityContainer = mActivityContainers.get(stackId);
3019 if (activityContainer != null) {
3020 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003021 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07003022 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003023 return null;
3024 }
3025 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003026 }
3027
Craig Mautner967212c2013-04-13 21:10:58 -07003028 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07003029 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003030 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3031 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003032 }
3033 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07003034 }
3035
Craig Mautner4a1cb222013-12-04 16:14:06 -08003036 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003037 ActivityRecord homeActivity = getHomeActivity();
3038 if (homeActivity != null) {
3039 return homeActivity.appToken;
3040 }
3041 return null;
3042 }
3043
3044 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07003045 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07003046 }
3047
3048 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003049 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
3050 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
3051 final TaskRecord task = tasks.get(taskNdx);
3052 if (task.isHomeTask()) {
3053 final ArrayList<ActivityRecord> activities = task.mActivities;
3054 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3055 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07003056 if (r.isHomeActivity()
3057 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003058 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003059 }
3060 }
3061 }
3062 }
3063 return null;
3064 }
3065
Chong Zhangb15758a2015-11-17 12:12:03 -08003066 /**
3067 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
3068 * the docked stack itself, or if it's side-by-side to the docked stack.
3069 */
3070 boolean isStackDockedInEffect(int stackId) {
3071 return stackId == DOCKED_STACK_ID ||
3072 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
3073 }
3074
Todd Kennedyca4d8422015-01-15 15:19:22 -08003075 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08003076 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003077 ActivityContainer activityContainer =
3078 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003079 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003080 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
3081 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003082 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003083 return activityContainer;
3084 }
3085
Craig Mautner34b73df2014-01-12 21:11:08 -08003086 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003087 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
3088 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
3089 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003090 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
3091 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003092 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08003093 }
3094 }
3095
Craig Mautner95da1082014-02-24 17:54:35 -08003096 void deleteActivityContainer(IActivityContainer container) {
3097 ActivityContainer activityContainer = (ActivityContainer)container;
3098 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003099 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
3100 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08003101 final int stackId = activityContainer.mStackId;
3102 mActivityContainers.remove(stackId);
3103 mWindowManager.removeStack(stackId);
3104 }
3105 }
3106
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07003107 void resizeStackLocked(int stackId, Rect bounds, boolean preserveWindows,
3108 boolean allowResizeInDockedMode) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08003109 final ActivityStack stack = getStack(stackId);
3110 if (stack == null) {
3111 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
3112 return;
3113 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003114
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07003115 if (!allowResizeInDockedMode
3116 && stackId != DOCKED_STACK_ID && getStack(DOCKED_STACK_ID) != null) {
3117 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
3118 // stack size changing so things don't get out of sync.
3119 return;
3120 }
3121
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003122 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02003123 mWindowManager.deferSurfaceLayout();
3124 try {
3125 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003126
Jorim Jaggic4025202015-10-22 16:43:34 +02003127 mTmpBounds.clear();
3128 mTmpConfigs.clear();
3129 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3130 for (int i = tasks.size() - 1; i >= 0; i--) {
3131 TaskRecord task = tasks.get(i);
3132 if (task.mResizeable) {
3133 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
3134 // For freeform stack we don't adjust the size of the tasks to match that
3135 // of the stack, but we do try to make sure the tasks are still contained
3136 // with the bounds of the stack.
3137 tempRect2.set(task.mBounds);
3138 fitWithinBounds(tempRect2, bounds);
3139 task.updateOverrideConfiguration(tempRect2);
3140 } else {
3141 task.updateOverrideConfiguration(bounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003142 }
3143 }
3144
Jorim Jaggic4025202015-10-22 16:43:34 +02003145 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
3146 mTmpBounds.put(task.taskId, task.mBounds);
3147 }
3148 stack.mFullscreen = mWindowManager.resizeStack(stackId, bounds, mTmpConfigs, mTmpBounds);
3149 if (stack.mStackId == DOCKED_STACK_ID) {
3150 // Dock stack funness...Yay!
3151 if (stack.mFullscreen) {
3152 // The dock stack went fullscreen which is kinda like dismissing it.
3153 // In this case we make all other static stacks fullscreen and move all
3154 // docked stack tasks to the fullscreen stack.
3155 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale3797c222015-10-27 14:21:58 -07003156 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
Jorim Jaggic4025202015-10-22 16:43:34 +02003157 resizeStackLocked(i, null, preserveWindows, true);
3158 }
3159 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003160
Jorim Jaggic4025202015-10-22 16:43:34 +02003161 final int count = tasks.size();
3162 for (int i = 0; i < count; i++) {
3163 moveTaskToStackLocked(tasks.get(i).taskId,
Jorim Jaggi030979c2015-11-20 15:14:43 -08003164 FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP, FORCE_FOCUS, "resizeStack",
Jorim Jaggi55387522015-11-24 18:21:10 -08003165 false /* animate */);
Jorim Jaggic4025202015-10-22 16:43:34 +02003166 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003167
Jorim Jaggic4025202015-10-22 16:43:34 +02003168 // stack shouldn't contain anymore activities, so nothing to resume.
3169 r = null;
3170 } else {
3171 // Docked stacks occupy a dedicated region on screen so the size of all other
3172 // static stacks need to be adjusted so they don't overlap with the docked stack.
3173 // We get the bounds to use from window manager which has been adjusted for any
3174 // screen controls and is also the same for all stacks.
3175 mWindowManager.getStackDockedModeBounds(HOME_STACK_ID, tempRect);
3176
3177 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale3797c222015-10-27 14:21:58 -07003178 if (StackId.isResizeableByDockedStack(i)) {
Jorim Jaggic4025202015-10-22 16:43:34 +02003179 ActivityStack otherStack = getStack(i);
3180 if (otherStack != null) {
3181 resizeStackLocked(i, tempRect, PRESERVE_WINDOWS, true);
3182 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003183 }
3184 }
3185 }
Jorim Jaggic4025202015-10-22 16:43:34 +02003186 // Since we are resizing the stack, all other operations should strive to preserve
3187 // windows.
3188 preserveWindows = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003189 }
Jorim Jaggic4025202015-10-22 16:43:34 +02003190 stack.setBounds(bounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003191
Jorim Jaggic4025202015-10-22 16:43:34 +02003192 if (r != null) {
3193 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0, preserveWindows);
3194 // And we need to make sure at this point that all other activities
3195 // are made visible with the correct configuration.
3196 ensureActivitiesVisibleLocked(r, 0, preserveWindows);
3197 if (!updated) {
3198 resumeTopActivitiesLocked(stack, null, null);
3199 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003200 }
Jorim Jaggic4025202015-10-22 16:43:34 +02003201 } finally {
3202 mWindowManager.continueSurfaceLayout();
3203 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003204 }
3205 }
3206
Chong Zhang6de2ae82015-09-30 18:25:21 -07003207 void resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003208 if (!task.mResizeable) {
3209 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
3210 return;
3211 }
3212
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003213 adjustForMinimalTaskDimensions(task, bounds);
3214
Chong Zhang87b21722015-09-21 15:39:51 -07003215 // If this is a forced resize, let it go through even if the bounds is not changing,
3216 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07003217 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Chong Zhang87b21722015-09-21 15:39:51 -07003218 if (task.mBounds != null && task.mBounds.equals(bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003219 // Nothing to do here...
3220 return;
3221 }
3222
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003223 if (!mWindowManager.isValidTaskId(task.taskId)) {
3224 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07003225 // All we can do for now is update the bounds so it can be used when the task is
3226 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07003227 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003228 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
3229 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07003230 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003231 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003232 return;
3233 }
3234
Chong Zhang6de2ae82015-09-30 18:25:21 -07003235 // Do not move the task to another stack here.
3236 // This method assumes that the task is already placed in the right stack.
3237 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003238
Chong Zhang6de2ae82015-09-30 18:25:21 -07003239 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07003240
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07003241 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07003242 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003243 // way and the activity was kept the way it was. If it's false, it means the activity had
3244 // to be relaunched due to configuration change.
3245 boolean kept = true;
3246 if (overrideConfig != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003247 ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08003248 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003249 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003250 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003251 // All other activities must be made visible with their correct configuration.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003252 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003253 if (!kept) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08003254 resumeTopActivitiesLocked(stack, null, null);
3255 }
3256 }
3257 }
Chong Zhang87b21722015-09-21 15:39:51 -07003258 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003259
3260 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwale60454db2015-01-23 16:05:07 -08003261 }
3262
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003263 private void adjustForMinimalTaskDimensions(TaskRecord task, Rect bounds) {
3264 if (bounds == null) {
3265 return;
3266 }
3267 int minimalSize = task.mMinimalSize == -1 ? mDefaultMinimalSizeOfResizeableTask
3268 : task.mMinimalSize;
3269 final boolean adjustWidth = minimalSize > bounds.width();
3270 final boolean adjustHeight = minimalSize > bounds.height();
3271 if (!(adjustWidth || adjustHeight)) {
3272 return;
3273 }
3274 Rect taskBounds = task.mBounds;
3275 if (adjustWidth) {
3276 if (taskBounds != null && bounds.right == taskBounds.right) {
3277 bounds.left = bounds.right - minimalSize;
3278 } else {
3279 // Either left bounds match, or neither match, or the previous bounds were
3280 // fullscreen and we default to keeping left.
3281 bounds.right = bounds.left + minimalSize;
3282 }
3283 }
3284 if (adjustHeight) {
3285 if (taskBounds != null && bounds.bottom == taskBounds.bottom) {
3286 bounds.top = bounds.bottom - minimalSize;
3287 } else {
3288 // Either top bounds match, or neither match, or the previous bounds were
3289 // fullscreen and we default to keeping top.
3290 bounds.bottom = bounds.top + minimalSize;
3291 }
3292 }
3293 }
3294
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003295 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003296 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3297 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08003298 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003299 }
3300
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003301 ActivityContainer activityContainer = new ActivityContainer(stackId);
3302 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003303 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08003304 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003305 }
3306
3307 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07003308 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003309 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
3310 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07003311 break;
3312 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003313 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003314 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003315 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003316 }
3317
Chong Zhang5dcb2752015-08-18 13:50:26 -07003318 /**
3319 * Restores a recent task to a stack
3320 * @param task The recent task to be restored.
3321 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale3797c222015-10-27 14:21:58 -07003322 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07003323 * @return true if the task has been restored successfully.
3324 */
3325 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
3326 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07003327 stackId = task.getLaunchStackId();
Chong Zhang5dcb2752015-08-18 13:50:26 -07003328 }
Wale Ogunwale706ed792015-08-02 10:29:44 -07003329 if (task.stack != null) {
3330 // Task has already been restored once. See if we need to do anything more
3331 if (task.stack.mStackId == stackId) {
3332 // Nothing else to do since it is already restored in the right stack.
3333 return true;
3334 }
3335 // Remove current stack association, so we can re-associate the task with the
3336 // right stack below.
Wale Ogunwale040b4702015-08-06 18:10:50 -07003337 task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003338 }
3339
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08003340 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07003341 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003342
3343 if (stack == null) {
3344 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003345 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3346 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003347 return false;
3348 }
3349
Wale Ogunwale5f986092015-12-04 15:35:38 -08003350 stack.addTask(task, false, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003351 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3352 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003353 final ArrayList<ActivityRecord> activities = task.mActivities;
3354 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003355 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003356 }
3357 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07003358 }
3359
Wale Ogunwale040b4702015-08-06 18:10:50 -07003360 /**
3361 * Moves the specified task record to the input stack id.
3362 * WARNING: This method performs an unchecked/raw move of the task and
3363 * can leave the system in an unstable state if used incorrectly.
3364 * Use {@link #moveTaskToStackLocked} to perform safe task movement
3365 * to a stack.
3366 * @param task Task to move.
3367 * @param stackId Id of stack to move task to.
3368 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07003369 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07003370 * @param reason Reason the task is been moved.
3371 * @return The stack the task was moved to.
3372 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07003373 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07003374 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
3375 final ActivityRecord r = task.getTopActivity();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003376 final boolean wasFocused = isFocusedStack(task.stack) && (topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07003377 final boolean wasResumed = wasFocused && (task.stack.mResumedActivity == r);
3378
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07003379 final boolean resizeable = task.mResizeable;
3380 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
3381 // if a docked stack is created below which will lead to the stack we are moving from and
3382 // its resizeable tasks being resized.
3383 task.mResizeable = false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003384 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07003385 task.mResizeable = resizeable;
Wale Ogunwale040b4702015-08-06 18:10:50 -07003386 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08003387 stack.addTask(task, toTop, reason);
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 "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003466 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003467 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 Ogunwale5f986092015-12-04 15:35:38 -08003502 stack.positionTask(task, position);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003503 // The task might have already been running and its visibility needs to be synchronized with
3504 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003505 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003506 resumeTopActivitiesLocked();
3507 }
3508
Craig Mautnerac6f8432013-07-17 13:24:59 -07003509 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003510 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003511 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3512 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003513 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3514 final ActivityStack stack = stacks.get(stackNdx);
3515 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003516 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003517 continue;
3518 }
3519 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003520 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
3521 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003522 continue;
3523 }
3524 final ActivityRecord ar = stack.findTaskLocked(r);
3525 if (ar != null) {
3526 return ar;
3527 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003528 }
3529 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003530 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07003531 return null;
3532 }
3533
3534 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003535 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3536 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003537 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3538 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
3539 if (ar != null) {
3540 return ar;
3541 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003542 }
3543 }
3544 return null;
3545 }
3546
Craig Mautner8d341ef2013-03-26 09:03:27 -07003547 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003548 scheduleSleepTimeout();
3549 if (!mGoingToSleep.isHeld()) {
3550 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003551 if (mLaunchingActivity.isHeld()) {
3552 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3553 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003554 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003555 mLaunchingActivity.release();
3556 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003557 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003558 }
Amith Yamasanice15e152013-09-19 12:30:32 -07003559 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003560 }
3561
3562 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003563 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003564
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003565 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003566 final long endTime = System.currentTimeMillis() + timeout;
3567 while (true) {
3568 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003569 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3570 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003571 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3572 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
3573 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003574 }
3575 if (cantShutdown) {
3576 long timeRemaining = endTime - System.currentTimeMillis();
3577 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003578 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003579 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003580 } catch (InterruptedException e) {
3581 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003582 } else {
3583 Slog.w(TAG, "Activity manager shutdown timed out");
3584 timedout = true;
3585 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003586 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003587 } else {
3588 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003589 }
3590 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003591
3592 // Force checkReadyForSleep to complete.
3593 mSleepTimeout = true;
3594 checkReadyForSleepLocked();
3595
Craig Mautner8d341ef2013-03-26 09:03:27 -07003596 return timedout;
3597 }
3598
3599 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003600 removeSleepTimeouts();
3601 if (mGoingToSleep.isHeld()) {
3602 mGoingToSleep.release();
3603 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003604 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3605 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003606 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3607 final ActivityStack stack = stacks.get(stackNdx);
3608 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003609 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003610 resumeTopActivitiesLocked();
3611 }
Craig Mautner5314a402013-09-26 12:40:16 -07003612 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003613 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003614 mGoingToSleepActivities.clear();
3615 }
3616
3617 void activitySleptLocked(ActivityRecord r) {
3618 mGoingToSleepActivities.remove(r);
3619 checkReadyForSleepLocked();
3620 }
3621
3622 void checkReadyForSleepLocked() {
3623 if (!mService.isSleepingOrShuttingDown()) {
3624 // Do not care.
3625 return;
3626 }
3627
3628 if (!mSleepTimeout) {
3629 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003630 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3631 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003632 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3633 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3634 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003635 }
3636
3637 if (mStoppingActivities.size() > 0) {
3638 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003639 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003640 + mStoppingActivities.size() + " activities");
3641 scheduleIdleLocked();
3642 dontSleep = true;
3643 }
3644
3645 if (mGoingToSleepActivities.size() > 0) {
3646 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003647 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003648 + mGoingToSleepActivities.size() + " activities");
3649 dontSleep = true;
3650 }
3651
3652 if (dontSleep) {
3653 return;
3654 }
3655 }
3656
Craig Mautnere0a38842013-12-16 16:14:02 -08003657 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3658 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003659 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3660 stacks.get(stackNdx).goToSleep();
3661 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003662 }
3663
3664 removeSleepTimeouts();
3665
3666 if (mGoingToSleep.isHeld()) {
3667 mGoingToSleep.release();
3668 }
3669 if (mService.mShuttingDown) {
3670 mService.notifyAll();
3671 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003672 }
3673
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003674 boolean reportResumedActivityLocked(ActivityRecord r) {
3675 final ActivityStack stack = r.task.stack;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003676 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003677 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003678 }
3679 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003680 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003681 mWindowManager.executeAppTransition();
3682 return true;
3683 }
3684 return false;
3685 }
3686
Craig Mautner8d341ef2013-03-26 09:03:27 -07003687 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003688 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3689 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003690 int stackNdx = stacks.size() - 1;
3691 while (stackNdx >= 0) {
3692 stacks.get(stackNdx).handleAppCrashLocked(app);
3693 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003694 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003695 }
3696 }
3697
Jose Lima4b6c6692014-08-12 17:41:12 -07003698 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003699 final ActivityStack stack = r.task.stack;
3700 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003701 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3702 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003703 return false;
3704 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003705 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003706 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3707 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003708
3709 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003710 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003711 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003712 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003713 return true;
3714 }
3715
3716 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003717 if (visible && top.fullscreen) {
3718 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003719 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3720 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3721 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3722 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003723 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003724 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3725 // Only the activity set as currently visible behind should actively reset its
3726 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003727 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3728 "requestVisibleBehind: returning visible=" + visible
3729 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3730 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003731 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003732 }
3733
Jose Lima4b6c6692014-08-12 17:41:12 -07003734 stack.setVisibleBehindActivity(visible ? r : null);
3735 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003736 // Make the activity immediately above r opaque.
3737 final ActivityRecord next = stack.findNextTranslucentActivity(r);
3738 if (next != null) {
3739 mService.convertFromTranslucent(next.appToken);
3740 }
3741 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003742 if (top.app != null && top.app.thread != null) {
3743 // Notify the top app of the change.
3744 try {
3745 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3746 } catch (RemoteException e) {
3747 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003748 }
3749 return true;
3750 }
3751
Craig Mautnerbb742462014-07-07 15:28:55 -07003752 // Called when WindowManager has finished animating the launchingBehind activity to the back.
3753 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
3754 r.mLaunchTaskBehind = false;
3755 final TaskRecord task = r.task;
Winson8b1871d2015-11-20 09:56:20 -08003756 task.setLastThumbnailLocked(task.stack.screenshotActivitiesLocked(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003757 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08003758 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07003759 mWindowManager.setAppVisibility(r.appToken, false);
3760 }
3761
3762 void scheduleLaunchTaskBehindComplete(IBinder token) {
3763 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3764 }
3765
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003766 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3767 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07003768 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08003769 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3770 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003771 final int topStackNdx = stacks.size() - 1;
3772 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3773 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003774 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07003775 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003776 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003777 }
3778
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003779 void invalidateTaskLayers() {
3780 mTaskLayersChanged = true;
3781 }
3782
3783 void rankTaskLayersIfNeeded() {
3784 if (!mTaskLayersChanged) {
3785 return;
3786 }
3787 mTaskLayersChanged = false;
3788 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
3789 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3790 int baseLayer = 0;
3791 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3792 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
3793 }
3794 }
3795 }
3796
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003797 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3798 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3799 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3800 final int topStackNdx = stacks.size() - 1;
3801 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3802 final ActivityStack stack = stacks.get(stackNdx);
3803 stack.clearOtherAppTimeTrackers(except);
3804 }
3805 }
3806 }
3807
Craig Mautner8d341ef2013-03-26 09:03:27 -07003808 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003809 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3810 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003811 final int numStacks = stacks.size();
3812 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3813 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003814 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003815 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003816 }
3817 }
3818
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003819 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3820 // Examine all activities currently running in the process.
3821 TaskRecord firstTask = null;
3822 // Tasks is non-null only if two or more tasks are found.
3823 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003824 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3825 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003826 ActivityRecord r = app.activities.get(i);
3827 // First, if we find an activity that is in the process of being destroyed,
3828 // then we just aren't going to do anything for now; we want things to settle
3829 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003830 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003831 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003832 return;
3833 }
3834 // Don't consider any activies that are currently not in a state where they
3835 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003836 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3837 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003838 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003839 continue;
3840 }
3841 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003842 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003843 + " from " + r);
3844 if (firstTask == null) {
3845 firstTask = r.task;
3846 } else if (firstTask != r.task) {
3847 if (tasks == null) {
3848 tasks = new ArraySet<>();
3849 tasks.add(firstTask);
3850 }
3851 tasks.add(r.task);
3852 }
3853 }
3854 }
3855 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003856 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003857 return;
3858 }
3859 // If we have activities in multiple tasks that are in a position to be destroyed,
3860 // let's iterate through the tasks and release the oldest one.
3861 final int numDisplays = mActivityDisplays.size();
3862 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3863 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3864 // Step through all stacks starting from behind, to hit the oldest things first.
3865 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3866 final ActivityStack stack = stacks.get(stackNdx);
3867 // Try to release activities in this stack; if we manage to, we are done.
3868 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3869 return;
3870 }
3871 }
3872 }
3873 }
3874
Amith Yamasani37a40c22015-06-17 13:25:42 -07003875 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003876 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003877 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003878 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003879
Craig Mautner858d8a62013-04-23 17:08:34 -07003880 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003881 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3882 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003883 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003884 final ActivityStack stack = stacks.get(stackNdx);
3885 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003886 TaskRecord task = stack.topTask();
3887 if (task != null) {
3888 mWindowManager.moveTaskToTop(task.taskId);
3889 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003890 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003891 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003892
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003893 ActivityStack stack = getStack(restoreStackId);
3894 if (stack == null) {
3895 stack = mHomeStack;
3896 }
3897 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003898 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003899 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003900 } else {
3901 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003902 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003903 }
Craig Mautner93529a42013-10-04 15:03:13 -07003904 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003905 }
3906
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003907 /**
3908 * Add background users to send boot completed events to.
3909 * @param userId The user being started in the background
3910 * @param uss The state object for the user.
3911 */
Amith Yamasani37a40c22015-06-17 13:25:42 -07003912 public void startBackgroundUserLocked(int userId, UserState uss) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003913 mStartingBackgroundUsers.add(uss);
3914 }
3915
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003916 /** Checks whether the userid is a profile of the current user. */
3917 boolean isCurrentProfileLocked(int userId) {
3918 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003919 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003920 }
3921
Chong Zhang45c25ce2015-08-10 22:18:26 -07003922 /** Checks whether the activity should be shown for current user. */
3923 boolean okToShowLocked(ActivityRecord r) {
3924 return r != null && (isCurrentProfileLocked(r.userId)
3925 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
3926 }
3927
Craig Mautnerde4ef022013-04-07 19:01:33 -07003928 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003929 ArrayList<ActivityRecord> stops = null;
3930
3931 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003932 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3933 ActivityRecord s = mStoppingActivities.get(activityNdx);
3934 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003935 if (DEBUG_ALL) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003936 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3937 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003938 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003939 if (s.finishing) {
3940 // If this activity is finishing, it is sitting on top of
3941 // everyone else but we now know it is no longer needed...
3942 // so get rid of it. Otherwise, we need to go through the
3943 // normal flow and hide it once we determine that it is
3944 // hidden by the activities in front of it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003945 if (DEBUG_ALL) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003946 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003947 }
3948 }
Craig Mautner8c14c152015-01-15 17:32:07 -08003949 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003950 if (DEBUG_ALL) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003951 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003952 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003953 }
3954 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003955 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003956 }
3957 }
3958
3959 return stops;
3960 }
3961
Craig Mautnercf910b02013-04-23 11:23:27 -07003962 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003963 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3964 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3965 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3966 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003967 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003968 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003969 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003970 if (r == null) Slog.e(TAG,
3971 "validateTop...: null top activity, stack=" + stack);
3972 else {
3973 final ActivityRecord pausing = stack.mPausingActivity;
3974 if (pausing != null && pausing == r) Slog.e(TAG,
3975 "validateTop...: top stack has pausing activity r=" + r
3976 + " state=" + state);
3977 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3978 "validateTop...: activity in front not resumed r=" + r
3979 + " state=" + state);
3980 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003981 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003982 final ActivityRecord resumed = stack.mResumedActivity;
3983 if (resumed != null && resumed == r) Slog.e(TAG,
3984 "validateTop...: back stack has resumed activity r=" + r
3985 + " state=" + state);
3986 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3987 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003988 }
3989 }
3990 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003991 }
3992
Craig Mautnere0570202015-05-13 13:06:11 -07003993 private String lockTaskModeToString() {
3994 switch (mLockTaskModeState) {
3995 case LOCK_TASK_MODE_LOCKED:
3996 return "LOCKED";
3997 case LOCK_TASK_MODE_PINNED:
3998 return "PINNED";
3999 case LOCK_TASK_MODE_NONE:
4000 return "NONE";
4001 default: return "unknown=" + mLockTaskModeState;
4002 }
4003 }
4004
Craig Mautner27084302013-03-25 08:05:25 -07004005 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07004006 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004007 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07004008 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
4009 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
4010 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08004011 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07004012 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
4013 final SparseArray<String[]> packages = mService.mLockTaskPackages;
4014 if (packages.size() > 0) {
4015 pw.println(" mLockTaskPackages (userId:packages)=");
4016 for (int i = 0; i < packages.size(); ++i) {
4017 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
4018 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
4019 }
4020 }
4021 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07004022 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07004023
Craig Mautner20e72272013-04-01 13:45:53 -07004024 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004025 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07004026 }
4027
Dianne Hackborn390517b2013-05-30 15:03:32 -07004028 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
4029 boolean needSep, String prefix) {
4030 if (activity != null) {
4031 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
4032 if (needSep) {
4033 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07004034 }
4035 pw.print(prefix);
4036 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004037 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07004038 }
4039 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004040 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07004041 }
4042
Craig Mautner8d341ef2013-03-26 09:03:27 -07004043 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
4044 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004045 boolean printed = false;
4046 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08004047 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
4048 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004049 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07004050 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08004051 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07004052 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004053 final ActivityStack stack = stacks.get(stackNdx);
4054 StringBuilder stackHeader = new StringBuilder(128);
4055 stackHeader.append(" Stack #");
4056 stackHeader.append(stack.mStackId);
4057 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07004058 stackHeader.append("\n");
4059 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
4060 stackHeader.append("\n");
4061 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004062 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
4063 needSep, stackHeader.toString());
4064 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
4065 !dumpAll, false, dumpPackage, true,
4066 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07004067
Craig Mautner4a1cb222013-12-04 16:14:06 -08004068 needSep = printed;
4069 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
4070 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004071 if (pr) {
4072 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004073 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004074 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004075 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
4076 " mResumedActivity: ");
4077 if (pr) {
4078 printed = true;
4079 needSep = false;
4080 }
4081 if (dumpAll) {
4082 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
4083 " mLastPausedActivity: ");
4084 if (pr) {
4085 printed = true;
4086 needSep = true;
4087 }
4088 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
4089 needSep, " mLastNoHistoryActivity: ");
4090 }
4091 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004092 }
4093 }
4094
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004095 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
4096 false, dumpPackage, true, " Activities waiting to finish:", null);
4097 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
4098 false, dumpPackage, true, " Activities waiting to stop:", null);
4099 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
4100 false, dumpPackage, true, " Activities waiting for another to become visible:",
4101 null);
4102 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
4103 false, dumpPackage, true, " Activities waiting to sleep:", null);
4104 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
4105 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004106
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004107 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004108 }
4109
Dianne Hackborn390517b2013-05-30 15:03:32 -07004110 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07004111 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004112 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07004113 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07004114 String innerPrefix = null;
4115 String[] args = null;
4116 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004117 for (int i=list.size()-1; i>=0; i--) {
4118 final ActivityRecord r = list.get(i);
4119 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
4120 continue;
4121 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004122 if (innerPrefix == null) {
4123 innerPrefix = prefix + " ";
4124 args = new String[0];
4125 }
4126 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004127 final boolean full = !brief && (complete || !r.isInHistory());
4128 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004129 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07004130 needNL = false;
4131 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004132 if (header1 != null) {
4133 pw.println(header1);
4134 header1 = null;
4135 }
4136 if (header2 != null) {
4137 pw.println(header2);
4138 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07004139 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07004140 if (lastTask != r.task) {
4141 lastTask = r.task;
4142 pw.print(prefix);
4143 pw.print(full ? "* " : " ");
4144 pw.println(lastTask);
4145 if (full) {
4146 lastTask.dump(pw, prefix + " ");
4147 } else if (complete) {
4148 // Complete + brief == give a summary. Isn't that obvious?!?
4149 if (lastTask.intent != null) {
4150 pw.print(prefix); pw.print(" ");
4151 pw.println(lastTask.intent.toInsecureStringWithClip());
4152 }
4153 }
4154 }
4155 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
4156 pw.print(" #"); pw.print(i); pw.print(": ");
4157 pw.println(r);
4158 if (full) {
4159 r.dump(pw, innerPrefix);
4160 } else if (complete) {
4161 // Complete + brief == give a summary. Isn't that obvious?!?
4162 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
4163 if (r.app != null) {
4164 pw.print(innerPrefix); pw.println(r.app);
4165 }
4166 }
4167 if (client && r.app != null && r.app.thread != null) {
4168 // flush anything that is already in the PrintWriter since the thread is going
4169 // to write to the file descriptor directly
4170 pw.flush();
4171 try {
4172 TransferPipe tp = new TransferPipe();
4173 try {
4174 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
4175 r.appToken, innerPrefix, args);
4176 // Short timeout, since blocking here can
4177 // deadlock with the application.
4178 tp.go(fd, 2000);
4179 } finally {
4180 tp.kill();
4181 }
4182 } catch (IOException e) {
4183 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
4184 } catch (RemoteException e) {
4185 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
4186 }
4187 needNL = true;
4188 }
4189 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004190 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004191 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004192
Craig Mautnerf3333272013-04-22 10:55:53 -07004193 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004194 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4195 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07004196 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
4197 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07004198 }
4199
4200 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07004201 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07004202 }
4203
4204 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004205 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
4206 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07004207 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
4208 }
4209
Craig Mautner05d29032013-05-03 13:40:13 -07004210 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08004211 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
4212 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
4213 }
Craig Mautner05d29032013-05-03 13:40:13 -07004214 }
4215
Craig Mautner0eea92c2013-05-16 13:35:39 -07004216 void removeSleepTimeouts() {
4217 mSleepTimeout = false;
4218 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
4219 }
4220
4221 final void scheduleSleepTimeout() {
4222 removeSleepTimeouts();
4223 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
4224 }
4225
Craig Mautner4a1cb222013-12-04 16:14:06 -08004226 @Override
4227 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004228 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004229 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
4230 }
4231
4232 @Override
4233 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004234 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004235 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
4236 }
4237
4238 @Override
4239 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004240 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004241 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
4242 }
4243
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004244 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08004245 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004246 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08004247 newDisplay = mActivityDisplays.get(displayId) == null;
4248 if (newDisplay) {
4249 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07004250 if (activityDisplay.mDisplay == null) {
4251 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
4252 return;
4253 }
Craig Mautner4504de52013-12-20 09:06:56 -08004254 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004255 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08004256 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004257 }
Craig Mautner4504de52013-12-20 09:06:56 -08004258 if (newDisplay) {
4259 mWindowManager.onDisplayAdded(displayId);
4260 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004261 }
4262
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004263 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
4264 if (display.mDisplayId != Display.DEFAULT_DISPLAY) {
4265 return;
4266 }
4267 final float fraction = mService.mContext.getResources().getFraction(com.android.internal.R.
4268 fraction.config_displayFractionForDefaultMinimalSizeOfResizeableTask, 1, 1);
4269 mDefaultMinimalSizeOfResizeableTask = (int) (fraction * Math.min(
4270 display.mDisplayInfo.logicalWidth, display.mDisplayInfo.logicalHeight));
4271 }
4272
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004273 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004274 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004275 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4276 if (activityDisplay != null) {
4277 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004278 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004279 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004280 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004281 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004282 }
4283 }
4284 mWindowManager.onDisplayRemoved(displayId);
4285 }
4286
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004287 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004288 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004289 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4290 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004291 // TODO: Update the bounds.
4292 }
4293 }
4294 mWindowManager.onDisplayChanged(displayId);
4295 }
4296
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004297 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004298 StackInfo info = new StackInfo();
4299 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
4300 info.displayId = Display.DEFAULT_DISPLAY;
4301 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004302 info.userId = stack.mCurrentUser;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004303
4304 ArrayList<TaskRecord> tasks = stack.getAllTasks();
4305 final int numTasks = tasks.size();
4306 int[] taskIds = new int[numTasks];
4307 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004308 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004309 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08004310 for (int i = 0; i < numTasks; ++i) {
4311 final TaskRecord task = tasks.get(i);
4312 taskIds[i] = task.taskId;
4313 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
4314 : task.realActivity != null ? task.realActivity.flattenToString()
4315 : task.getTopActivity() != null ? task.getTopActivity().packageName
4316 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004317 taskBounds[i] = new Rect();
4318 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004319 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004320 }
4321 info.taskIds = taskIds;
4322 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07004323 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004324 info.taskUserIds = taskUserIds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004325 return info;
4326 }
4327
4328 StackInfo getStackInfoLocked(int stackId) {
4329 ActivityStack stack = getStack(stackId);
4330 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004331 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004332 }
4333 return null;
4334 }
4335
4336 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004337 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08004338 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
4339 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004340 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004341 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08004342 }
4343 }
4344 return list;
4345 }
4346
Craig Mautner15df08a2015-04-01 12:17:18 -07004347 TaskRecord getLockedTaskLocked() {
4348 final int top = mLockTaskModeTasks.size() - 1;
4349 if (top >= 0) {
4350 return mLockTaskModeTasks.get(top);
4351 }
4352 return null;
4353 }
4354
4355 boolean isLockedTask(TaskRecord task) {
4356 return mLockTaskModeTasks.contains(task);
4357 }
4358
4359 boolean isLastLockedTask(TaskRecord task) {
4360 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
4361 }
4362
4363 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07004364 if (!mLockTaskModeTasks.remove(task)) {
4365 return;
4366 }
4367 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
4368 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004369 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07004370 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
4371 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07004372 final Message lockTaskMsg = Message.obtain();
4373 lockTaskMsg.arg1 = task.userId;
4374 lockTaskMsg.what = LOCK_TASK_END_MSG;
4375 mHandler.sendMessage(lockTaskMsg);
4376 }
4377 }
4378
Chong Zhangc806d902015-11-30 09:44:27 -08004379 private void showNonResizeableDockToast(int taskId) {
4380 mWindowManager.scheduleShowNonResizeableDockToast(taskId);
Chong Zhangb15758a2015-11-17 12:12:03 -08004381 }
4382
Craig Mautner6cd6cec2015-04-01 00:02:12 -07004383 void showLockTaskToast() {
4384 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04004385 }
4386
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004387 void showLockTaskEscapeMessageLocked(TaskRecord task) {
4388 if (mLockTaskModeTasks.contains(task)) {
4389 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
4390 }
4391 }
4392
Craig Mautner432f64e2015-05-20 14:59:57 -07004393 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
4394 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004395 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07004396 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07004397 final TaskRecord lockedTask = getLockedTaskLocked();
4398 if (lockedTask != null) {
4399 removeLockedTaskLocked(lockedTask);
4400 if (!mLockTaskModeTasks.isEmpty()) {
4401 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07004402 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4403 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07004404 lockedTask.performClearTaskLocked();
4405 resumeTopActivitiesLocked();
4406 return;
4407 }
Christopher Tate3bd90612014-06-18 16:37:52 -07004408 }
Craig Mautnere0570202015-05-13 13:06:11 -07004409 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4410 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08004411 return;
4412 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004413
4414 // Should have already been checked, but do it again.
4415 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07004416 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4417 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08004418 return;
4419 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004420 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07004421 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07004422 return;
4423 }
4424
4425 if (mLockTaskModeTasks.isEmpty()) {
4426 // First locktask.
4427 final Message lockTaskMsg = Message.obtain();
4428 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
4429 lockTaskMsg.arg1 = task.userId;
4430 lockTaskMsg.what = LOCK_TASK_START_MSG;
4431 lockTaskMsg.arg2 = lockTaskModeState;
4432 mHandler.sendMessage(lockTaskMsg);
4433 }
4434 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07004435 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
4436 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07004437 mLockTaskModeTasks.remove(task);
4438 mLockTaskModeTasks.add(task);
4439
4440 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07004441 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07004442 }
Craig Mautner432f64e2015-05-20 14:59:57 -07004443
4444 if (andResume) {
4445 findTaskToMoveToFrontLocked(task, 0, null, reason);
4446 resumeTopActivitiesLocked();
4447 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004448 }
4449
4450 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04004451 return isLockTaskModeViolation(task, false);
4452 }
4453
4454 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
4455 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004456 return false;
4457 }
4458 final int lockTaskAuth = task.mLockTaskAuth;
4459 switch (lockTaskAuth) {
4460 case LOCK_TASK_AUTH_DONT_LOCK:
4461 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01004462 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07004463 case LOCK_TASK_AUTH_LAUNCHABLE:
4464 case LOCK_TASK_AUTH_WHITELISTED:
4465 return false;
4466 case LOCK_TASK_AUTH_PINNABLE:
4467 // Pinnable tasks can't be launched on top of locktask tasks.
4468 return !mLockTaskModeTasks.isEmpty();
4469 default:
4470 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
4471 return true;
4472 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004473 }
4474
Craig Mautner15df08a2015-04-01 12:17:18 -07004475 void onLockTaskPackagesUpdatedLocked() {
4476 boolean didSomething = false;
4477 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
4478 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01004479 final boolean wasWhitelisted =
4480 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4481 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07004482 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01004483 final boolean isWhitelisted =
4484 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4485 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
4486 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004487 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07004488 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
4489 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07004490 removeLockedTaskLocked(lockedTask);
4491 lockedTask.performClearTaskLocked();
4492 didSomething = true;
4493 }
4494 }
4495 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4496 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4497 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4498 final ActivityStack stack = stacks.get(stackNdx);
4499 stack.onLockTaskPackagesUpdatedLocked();
4500 }
4501 }
Craig Mautnere0570202015-05-13 13:06:11 -07004502 final ActivityRecord r = topRunningActivityLocked();
4503 final TaskRecord task = r != null ? r.task : null;
4504 if (mLockTaskModeTasks.isEmpty() && task != null
4505 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4506 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004507 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4508 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4509 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4510 false);
4511 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004512 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004513 if (didSomething) {
4514 resumeTopActivitiesLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004515 }
4516 }
4517
Benjamin Franz43261142015-02-11 15:59:44 +00004518 int getLockTaskModeState() {
4519 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004520 }
4521
Filip Gruszczynski77d94482015-12-11 13:59:52 -08004522 void logStackState() {
4523 mActivityMetricsLogger.logWindowState();
4524 }
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}