blob: 9389056e6a941001cc64e2273b3b0c9736a1ba19 [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;
Craig Mautner8d341ef2013-03-26 09:03:27 -070090import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070091import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070092import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070093import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070094import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070095import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070096import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070097import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070098import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070099import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -0400100import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700101import android.os.SystemClock;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100102import android.os.SystemProperties;
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;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100108import android.os.storage.StorageManager;
Svetoslav7008b512015-06-24 18:47:07 -0700109import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -0400110import android.provider.Settings;
111import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700112import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -0700113import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700114import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -0700115import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700116import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800117import android.util.SparseArray;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700118
Craig Mautner4a1cb222013-12-04 16:14:06 -0800119import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800120import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800121import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -0800122import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800123import android.view.Surface;
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
justinzhang5286d3f2014-05-12 17:06:01 -0400135
Craig Mautner8d341ef2013-03-26 09:03:27 -0700136import java.io.FileDescriptor;
137import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700138import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700139import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700140import java.util.Arrays;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700141import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700142import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700143
Craig Mautner4a1cb222013-12-04 16:14:06 -0800144public final class ActivityStackSupervisor implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800145 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700146 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700147 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700148 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700149 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700150 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700151 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700152 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700153 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
154 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700155 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700156 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700157 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
158 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700159 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700160 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800161
Craig Mautnerf3333272013-04-22 10:55:53 -0700162 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700163 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700164
Craig Mautner0eea92c2013-05-16 13:35:39 -0700165 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700166 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700167
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700168 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700169 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700170
Craig Mautner05d29032013-05-03 13:40:13 -0700171 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
172 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
173 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700174 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700175 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800176 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
177 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
178 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700179 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400180 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
181 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700182 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700183 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700184 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800185
Wale Ogunwale040b4702015-08-06 18:10:50 -0700186 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700187
Jason Monk62515be2014-05-21 16:06:19 -0400188 private static final String LOCK_TASK_TAG = "Lock-to-App";
189
Wale Ogunwale040b4702015-08-06 18:10:50 -0700190 // Used to indicate if an object (e.g. stack) that we are trying to get
191 // should be created if it doesn't exist already.
192 private static final boolean CREATE_IF_NEEDED = true;
193
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700194 // Used to indicate that windows of activities should be preserved during the resize.
195 static final boolean PRESERVE_WINDOWS = true;
196
Wale Ogunwale040b4702015-08-06 18:10:50 -0700197 // Used to indicate if an object (e.g. task) should be moved/created
198 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700199 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700200
201 // Used to indicate that an objects (e.g. task) removal from its container
202 // (e.g. stack) is due to it moving to another container.
203 static final boolean MOVING = true;
204
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700205 // Force the focus to change to the stack we are moving a task to..
206 static final boolean FORCE_FOCUS = true;
207
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700208 // Restore task from the saved recents if it can't be found in any live stack.
209 static final boolean RESTORE_FROM_RECENTS = true;
210
Svetoslav7008b512015-06-24 18:47:07 -0700211 // Activity actions an app cannot start if it uses a permission which is not granted.
212 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
213 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700214
Svetoslav7008b512015-06-24 18:47:07 -0700215 static {
216 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
217 Manifest.permission.CAMERA);
218 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
219 Manifest.permission.CAMERA);
220 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
221 Manifest.permission.CALL_PHONE);
222 }
223
Svet Ganov99b60432015-06-27 13:15:22 -0700224 /** Action restriction: launching the activity is not restricted. */
225 private static final int ACTIVITY_RESTRICTION_NONE = 0;
226 /** Action restriction: launching the activity is restricted by a permission. */
227 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
228 /** Action restriction: launching the activity is restricted by an app op. */
229 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700230
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700231 // The height/width divide used when fitting a task within a bounds with method
232 // {@link #fitWithinBounds}.
233 // We always want the task to to be visible in the bounds without affecting its size when
234 // fitting. To make sure this is the case, we don't adjust the task left or top side pass
235 // the input bounds right or bottom side minus the width or height divided by this value.
236 private static final int FIT_WITHIN_BOUNDS_DIVIDER = 3;
237
justinzhang5286d3f2014-05-12 17:06:01 -0400238 /** Status Bar Service **/
239 private IBinder mToken = new Binder();
240 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400241 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400242
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700243 // For debugging to make sure the caller when acquiring/releasing our
244 // wake lock is the system process.
245 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700246
Craig Mautner27084302013-03-25 08:05:25 -0700247 final ActivityManagerService mService;
248
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800249 private final RecentTasks mRecentTasks;
250
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700251 final ActivityStackSupervisorHandler mHandler;
252
253 /** Short cut */
254 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800255 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700256
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700257 /** Counter for next free stack ID to use for dynamic activity stacks. */
258 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700259
260 /** Task identifier that activities are currently being started in. Incremented each time a
261 * new task is created. */
262 private int mCurTaskId = 0;
263
Craig Mautner2420ead2013-04-01 17:13:20 -0700264 /** The current user */
265 private int mCurrentUser;
266
Craig Mautnere0a38842013-12-16 16:14:02 -0800267 /** The stack containing the launcher app. Assumed to always be attached to
268 * Display.DEFAULT_DISPLAY. */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700269 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700270
Craig Mautnere0a38842013-12-16 16:14:02 -0800271 /** The stack currently receiving input or launching the next activity. */
Craig Mautner29219d92013-04-16 20:19:12 -0700272 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700273
Craig Mautner4a1cb222013-12-04 16:14:06 -0800274 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
275 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800276 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800277 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700278
279 /** List of activities that are waiting for a new activity to become visible before completing
280 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800281 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700282
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700283 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800284 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700285
286 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800287 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700288
Craig Mautnerde4ef022013-04-07 19:01:33 -0700289 /** List of activities that are ready to be stopped, but waiting for the next activity to
290 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800291 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700292
Craig Mautnerf3333272013-04-22 10:55:53 -0700293 /** List of activities that are ready to be finished, but waiting for the previous activity to
294 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800295 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700296
Craig Mautner0eea92c2013-05-16 13:35:39 -0700297 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800298 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700299
Craig Mautnerf3333272013-04-22 10:55:53 -0700300 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700301 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700302
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700303 /** Used to queue up any background users being started */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700304 final ArrayList<UserState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700305
Craig Mautnerde4ef022013-04-07 19:01:33 -0700306 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
307 * is being brought in front of us. */
308 boolean mUserLeaving = false;
309
Craig Mautner0eea92c2013-05-16 13:35:39 -0700310 /** Set when we have taken too long waiting to go to sleep. */
311 boolean mSleepTimeout = false;
312
313 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700314 * We don't want to allow the device to go to sleep while in the process
315 * of launching an activity. This is primarily to allow alarm intent
316 * receivers to launch an activity and get that to run before the device
317 * goes back to sleep.
318 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700319 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700320
321 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700322 * Set when the system is going to sleep, until we have
323 * successfully paused the current activity and released our wake lock.
324 * At that point the system is allowed to actually sleep.
325 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700326 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700327
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700328 /** Stack id of the front stack when user switched, indexed by userId. */
329 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700330
Craig Mautner4504de52013-12-20 09:06:56 -0800331 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800332 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700333 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800334
335 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700336 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800337
Jeff Brownca9bc702014-02-11 14:32:56 -0800338 InputManagerInternal mInputManagerInternal;
339
Craig Mautner15df08a2015-04-01 12:17:18 -0700340 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
341 * may be finished until there is only one entry left. If this is empty the system is not
342 * in lockTask mode. */
343 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000344 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700345 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
346 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000347 */
348 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400349 /**
350 * Notifies the user when entering/exiting lock-task.
351 */
352 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800353
Craig Mautner15df08a2015-04-01 12:17:18 -0700354 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches = new ArrayList<>();
Craig Mautneree36c772014-07-16 14:56:05 -0700355
Craig Mautner42d04db2014-11-06 12:13:23 -0800356 /** Used to keep resumeTopActivityLocked() from being entered recursively */
357 boolean inResumeTopActivity;
358
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700359 // 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 -0700360 private final Rect tempRect = new Rect();
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700361 private final Rect tempRect2 = new Rect();
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700362
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700363 private final SparseArray<Configuration> mTmpConfigs = new SparseArray<>();
364 private final SparseArray<Rect> mTmpBounds = new SparseArray<>();
365
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700366 // The default minimal size that will be used if the activity doesn't specify its minimal size.
367 // It will be calculated when the default display gets added.
368 private int mDefaultMinimalSizeOfResizeableTask = -1;
369
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700370 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
371 private boolean mTaskLayersChanged = true;
372
Craig Mautneree36c772014-07-16 14:56:05 -0700373 /**
374 * Description of a request to start a new activity, which has been held
375 * due to app switches being disabled.
376 */
377 static class PendingActivityLaunch {
378 final ActivityRecord r;
379 final ActivityRecord sourceRecord;
380 final int startFlags;
381 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700382 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700383
384 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700385 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700386 r = _r;
387 sourceRecord = _sourceRecord;
388 startFlags = _startFlags;
389 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700390 callerApp = _callerApp;
391 }
392
393 void sendErrorResult(String message) {
394 try {
395 if (callerApp.thread != null) {
396 callerApp.thread.scheduleCrash(message);
397 }
398 } catch (RemoteException e) {
399 Slog.e(TAG, "Exception scheduling crash of failed "
400 + "activity launcher sourceRecord=" + sourceRecord, e);
401 }
Craig Mautneree36c772014-07-16 14:56:05 -0700402 }
403 }
404
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800405 public ActivityStackSupervisor(ActivityManagerService service, RecentTasks recentTasks) {
Craig Mautner27084302013-03-25 08:05:25 -0700406 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800407 mRecentTasks = recentTasks;
Jeff Brown2c43c332014-06-12 22:38:59 -0700408 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
409 }
410
411 /**
412 * At the time when the constructor runs, the power manager has not yet been
413 * initialized. So we initialize our wakelocks afterwards.
414 */
415 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800416 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700417 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700418 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700419 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700420 }
421
justinzhang5286d3f2014-05-12 17:06:01 -0400422 // This function returns a IStatusBarService. The value is from ServiceManager.
423 // getService and is cached.
424 private IStatusBarService getStatusBarService() {
425 synchronized (mService) {
426 if (mStatusBarService == null) {
427 mStatusBarService = IStatusBarService.Stub.asInterface(
428 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
429 if (mStatusBarService == null) {
430 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
431 }
432 }
433 return mStatusBarService;
434 }
435 }
436
Jason Monk35c62a42014-06-17 10:24:47 -0400437 private IDevicePolicyManager getDevicePolicyManager() {
438 synchronized (mService) {
439 if (mDevicePolicyManager == null) {
440 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
441 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
442 if (mDevicePolicyManager == null) {
443 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
444 }
445 }
446 return mDevicePolicyManager;
447 }
448 }
449
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700450 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800451 synchronized (mService) {
452 mWindowManager = wm;
453
454 mDisplayManager =
455 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
456 mDisplayManager.registerDisplayListener(this, null);
457
458 Display[] displays = mDisplayManager.getDisplays();
459 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
460 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800461 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700462 if (activityDisplay.mDisplay == null) {
463 throw new IllegalStateException("Default Display does not exist");
464 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800465 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700466 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800467 }
468
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700469 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800470 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800471
472 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800473 }
Craig Mautner27084302013-03-25 08:05:25 -0700474 }
475
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200476 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700477 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200478 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700479 }
480
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700481 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800482 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700483 }
484
Craig Mautnerde4ef022013-04-07 19:01:33 -0700485 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800486 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700487 }
488
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700489 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700490 if (stack == null) {
491 return false;
492 }
493
Craig Mautnerdf88d732014-01-27 09:21:32 -0800494 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
495 if (parent != null) {
496 stack = parent.task.stack;
497 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800498 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700499 }
500
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700501 /** The top most stack. */
502 boolean isFrontStack(ActivityStack stack) {
503 if (stack == null) {
504 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800505 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700506
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700507 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
508 if (parent != null) {
509 stack = parent.task.stack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800510 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700511 return stack == mHomeStack.mStacks.get((mHomeStack.mStacks.size() - 1));
512 }
513
514 void setFocusStack(String reason, ActivityStack focusedStack) {
515 mLastFocusedStack = mFocusedStack;
516 mFocusedStack = focusedStack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800517
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700518 EventLogTags.writeAmFocusedStack(
519 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
520 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
Craig Mautnerde313752015-01-22 14:28:03 -0800521
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800522 if (mService.mBooting || !mService.mBooted) {
523 final ActivityRecord r = topRunningActivityLocked();
524 if (r != null && r.idle) {
525 checkFinishBootingLocked();
526 }
527 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700528 }
529
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700530 void moveHomeStackToFront(String reason) {
531 mHomeStack.moveToFront(reason);
532 }
533
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700534 /** Returns true if the focus activity was adjusted to the home stack top activity. */
535 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700536 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
537 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700538 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700539 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700540
Craig Mautner84984fa2014-06-19 11:19:20 -0700541 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700542
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700543 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700544 if (top == null) {
545 return false;
546 }
547 mService.setFocusedActivityLocked(top, reason);
548 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700549 }
550
Craig Mautner299f9602015-01-26 09:47:33 -0800551 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700552 if (!mService.mBooting && !mService.mBooted) {
553 // Not ready yet!
554 return false;
555 }
556
Craig Mautner84984fa2014-06-19 11:19:20 -0700557 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
558 mWindowManager.showRecentApps();
559 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700560 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700561
Craig Mautner84984fa2014-06-19 11:19:20 -0700562 if (prev != null) {
563 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
564 }
565
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700566 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
567 ActivityRecord r = getHomeActivity();
Mark Lua56ea122015-10-08 13:31:01 +0800568 // Only resume home activity if isn't finishing.
569 if (r != null && !r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -0800570 mService.setFocusedActivityLocked(r, reason);
Craig Mautner05d29032013-05-03 13:40:13 -0700571 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700572 }
Craig Mautner299f9602015-01-26 09:47:33 -0800573 return mService.startHomeActivityLocked(mCurrentUser, reason);
Craig Mautner69ada552013-04-18 13:51:51 -0700574 }
575
Craig Mautner8d341ef2013-03-26 09:03:27 -0700576 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700577 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700578 }
579
580 /**
581 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
582 * @param id Id of the task we would like returned.
583 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
584 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700585 * @param stackId The stack to restore the task to (default launch stack will be used if
586 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700587 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700588 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800589 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800590 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800591 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800592 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
593 ActivityStack stack = stacks.get(stackNdx);
594 TaskRecord task = stack.taskForIdLocked(id);
595 if (task != null) {
596 return task;
597 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700598 }
599 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800600
601 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700602 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800603 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800604 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700605 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800606 return null;
607 }
608
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700609 if (!restoreFromRecents) {
610 return task;
611 }
612
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700613 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700614 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
615 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800616 return null;
617 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700618 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800619 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700620 }
621
Craig Mautner6170f732013-04-02 13:05:23 -0700622 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800623 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800624 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800625 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800626 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
627 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
628 if (r != null) {
629 return r;
630 }
Craig Mautner6170f732013-04-02 13:05:23 -0700631 }
632 }
633 return null;
634 }
635
Craig Mautneref73ee12014-04-23 11:45:37 -0700636 void setNextTaskId(int taskId) {
637 if (taskId > mCurTaskId) {
638 mCurTaskId = taskId;
639 }
640 }
641
Craig Mautner8d341ef2013-03-26 09:03:27 -0700642 int getNextTaskId() {
643 do {
644 mCurTaskId++;
645 if (mCurTaskId <= 0) {
646 mCurTaskId = 1;
647 }
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700648 } while (anyTaskForIdLocked(mCurTaskId, !RESTORE_FROM_RECENTS, INVALID_STACK_ID) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700649 return mCurTaskId;
650 }
651
Craig Mautnerde4ef022013-04-07 19:01:33 -0700652 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800653 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700654 if (stack == null) {
655 return null;
656 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700657 ActivityRecord resumedActivity = stack.mResumedActivity;
658 if (resumedActivity == null || resumedActivity.app == null) {
659 resumedActivity = stack.mPausingActivity;
660 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700661 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700662 }
663 }
664 return resumedActivity;
665 }
666
Dianne Hackbornff072722014-09-24 10:56:28 -0700667 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700668 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800669 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800670 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
671 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800672 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
673 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700674 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800675 continue;
676 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700677 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800678 if (hr != null) {
679 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
680 && processName.equals(hr.processName)) {
681 try {
George Mount2c92c972014-03-20 09:38:23 -0700682 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800683 didSomething = true;
684 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700685 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800686 Slog.w(TAG, "Exception in new application when starting activity "
687 + hr.intent.getComponent().flattenToShortString(), e);
688 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700689 }
Craig Mautner20e72272013-04-01 13:45:53 -0700690 }
Craig Mautner20e72272013-04-01 13:45:53 -0700691 }
692 }
693 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700694 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700695 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700696 }
Craig Mautner20e72272013-04-01 13:45:53 -0700697 return didSomething;
698 }
699
700 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800701 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
702 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800703 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
704 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700705 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800706 continue;
707 }
708 final ActivityRecord resumedActivity = stack.mResumedActivity;
709 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700710 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800711 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800712 return false;
713 }
Craig Mautner20e72272013-04-01 13:45:53 -0700714 }
715 }
716 return true;
717 }
718
Craig Mautnerde4ef022013-04-07 19:01:33 -0700719 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800720 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
721 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800722 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
723 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700724 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800725 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700726 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800727 return false;
728 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700729 }
730 }
731 }
732 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700733 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800734 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
735 mLastFocusedStack + " to=" + mFocusedStack);
736 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700737 return true;
738 }
739
740 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800741 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800742 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
743 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800744 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
745 final ActivityStack stack = stacks.get(stackNdx);
746 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800747 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700748 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800749 return false;
750 }
751 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800752 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700753 }
754 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800755 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700756 }
757
Craig Mautner2acc3892013-09-23 10:28:14 -0700758 /**
759 * Pause all activities in either all of the stacks or just the back stacks.
760 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700761 * @return true if any activity was paused as a result of this call.
762 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700763 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700764 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800765 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
766 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800767 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
768 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700769 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700770 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800771 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700772 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
773 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800774 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700775 }
776 }
777 return someActivityPaused;
778 }
779
Craig Mautnerde4ef022013-04-07 19:01:33 -0700780 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700781 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800782 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
783 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800784 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
785 final ActivityStack stack = stacks.get(stackNdx);
786 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700787 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800788 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700789 Slog.d(TAG_STATES,
790 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800791 pausing = false;
792 } else {
793 return false;
794 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700795 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700796 }
797 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700798 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700799 }
800
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700801 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
802 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500803 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800804 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
805 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
806 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
807 final ActivityStack stack = stacks.get(stackNdx);
808 if (stack.mResumedActivity != null &&
809 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700810 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800811 }
812 }
813 }
814 }
815
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700816 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700817 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700818 }
819
820 void sendWaitingVisibleReportLocked(ActivityRecord r) {
821 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700822 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700823 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700824 if (w.who == null) {
825 changed = true;
826 w.timeout = false;
827 if (r != null) {
828 w.who = new ComponentName(r.info.packageName, r.info.name);
829 }
830 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
831 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700832 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700833 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700834 if (changed) {
835 mService.notifyAll();
836 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700837 }
838
839 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
840 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700841 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700842 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700843 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700844 if (w.who == null) {
845 changed = true;
846 w.timeout = timeout;
847 if (r != null) {
848 w.who = new ComponentName(r.info.packageName, r.info.name);
849 }
850 w.thisTime = thisTime;
851 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700852 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700853 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700854 if (changed) {
855 mService.notifyAll();
856 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700857 }
858
Craig Mautner29219d92013-04-16 20:19:12 -0700859 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800860 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700861 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -0700862 if (r != null) {
863 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700864 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700865
Craig Mautner4a1cb222013-12-04 16:14:06 -0800866 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800867 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800868 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
869 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700870 if (stack != focusedStack && isFrontStack(stack)) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700871 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -0700872 if (r != null) {
873 return r;
874 }
875 }
876 }
877 return null;
878 }
879
Dianne Hackborn09233282014-04-30 11:33:59 -0700880 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700881 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800882 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
883 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800884 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800885 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800886 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800887 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
888 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700889 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800890 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700891 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700892 }
893 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700894
895 // The lists are already sorted from most recent to oldest. Just pull the most recent off
896 // each list and add it to list. Stop when all lists are empty or maxNum reached.
897 while (maxNum > 0) {
898 long mostRecentActiveTime = Long.MIN_VALUE;
899 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800900 final int numTaskLists = runningTaskLists.size();
901 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
902 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700903 if (!stackTaskList.isEmpty()) {
904 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
905 if (lastActiveTime > mostRecentActiveTime) {
906 mostRecentActiveTime = lastActiveTime;
907 selectedStackList = stackTaskList;
908 }
909 }
910 }
911 if (selectedStackList != null) {
912 list.add(selectedStackList.remove(0));
913 --maxNum;
914 } else {
915 break;
916 }
917 }
Craig Mautner20e72272013-04-01 13:45:53 -0700918 }
919
Craig Mautner23ac33b2013-04-01 16:26:35 -0700920 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700921 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700922 // Collect information about the target of the Intent.
923 ActivityInfo aInfo;
924 try {
925 ResolveInfo rInfo =
926 AppGlobals.getPackageManager().resolveIntent(
927 intent, resolvedType,
928 PackageManager.MATCH_DEFAULT_ONLY
929 | ActivityManagerService.STOCK_PM_FLAGS, userId);
930 aInfo = rInfo != null ? rInfo.activityInfo : null;
931 } catch (RemoteException e) {
932 aInfo = null;
933 }
934
935 if (aInfo != null) {
936 // Store the found target back into the intent, because now that
937 // we have it we never want to do this again. For example, if the
938 // user navigates back to this point in the history, we should
939 // always restart the exact same activity.
940 intent.setComponent(new ComponentName(
941 aInfo.applicationInfo.packageName, aInfo.name));
942
943 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -0700944 if (!aInfo.processName.equals("system")) {
945 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700946 mService.setDebugApp(aInfo.processName, true, false);
947 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700948
Man Caocfa78b22015-06-11 20:14:34 -0700949 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
950 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
951 }
952
953 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700954 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700955 }
956 }
957 }
958 return aInfo;
959 }
960
Craig Mautner299f9602015-01-26 09:47:33 -0800961 void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
962 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700963 startActivityLocked(null /* caller */, intent, null /* resolvedType */, aInfo,
964 null /* voiceSession */, null /* voiceInteractor */, null /* resultTo */,
965 null /* resultWho */, 0 /* requestCode */, 0 /* callingPid */, 0 /* callingUid */,
966 null /* callingPackage */, 0 /* realCallingPid */, 0 /* realCallingUid */,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700967 0 /* startFlags */, null /* options */, false /* ignoreTargetSecurity */,
968 false /* componentSpecified */,
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700969 null /* outActivity */, null /* container */, null /* inTask */);
970 if (inResumeTopActivity) {
971 // If we are in resume section already, home activity will be initialized, but not
972 // resumed (to avoid recursive resume) and will stay that way until something pokes it
973 // again. We need to schedule another resume.
974 scheduleResumeTopActivities();
975 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700976 }
977
Craig Mautner23ac33b2013-04-01 16:26:35 -0700978 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700979 String callingPackage, Intent intent, String resolvedType,
980 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700981 IBinder resultTo, String resultWho, int requestCode, int startFlags,
982 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700983 Bundle options, boolean ignoreTargetSecurity, int userId,
984 IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700985 // Refuse possible leaked file descriptors
986 if (intent != null && intent.hasFileDescriptors()) {
987 throw new IllegalArgumentException("File descriptors passed in Intent");
988 }
989 boolean componentSpecified = intent.getComponent() != null;
990
991 // Don't modify the client's object!
992 intent = new Intent(intent);
993
994 // Collect information about the target of the Intent.
Craig Mautner15df08a2015-04-01 12:17:18 -0700995 ActivityInfo aInfo =
996 resolveActivity(intent, resolvedType, startFlags, profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700997
Craig Mautnere0a38842013-12-16 16:14:02 -0800998 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700999 synchronized (mService) {
Craig Mautnerb9168362015-02-26 20:40:19 -08001000 if (container != null && container.mParentActivity != null &&
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001001 container.mParentActivity.state != RESUMED) {
Craig Mautnerb9168362015-02-26 20:40:19 -08001002 // Cannot start a child activity if the parent is not resumed.
1003 return ActivityManager.START_CANCELED;
1004 }
Dianne Hackborn95465202014-09-15 16:21:55 -07001005 final int realCallingPid = Binder.getCallingPid();
1006 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -07001007 int callingPid;
1008 if (callingUid >= 0) {
1009 callingPid = -1;
1010 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001011 callingPid = realCallingPid;
1012 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001013 } else {
1014 callingPid = callingUid = -1;
1015 }
1016
Craig Mautnere0a38842013-12-16 16:14:02 -08001017 final ActivityStack stack;
1018 if (container == null || container.mStack.isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001019 stack = mFocusedStack;
Craig Mautnere0a38842013-12-16 16:14:02 -08001020 } else {
1021 stack = container.mStack;
1022 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08001023 stack.mConfigWillChange = config != null && mService.mConfiguration.diff(config) != 0;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001024 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautnerde4ef022013-04-07 19:01:33 -07001025 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001026
1027 final long origId = Binder.clearCallingIdentity();
1028
1029 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001030 (aInfo.applicationInfo.privateFlags
1031 &ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001032 // This may be a heavy-weight process! Check to see if we already
1033 // have another, different heavy-weight process running.
1034 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
1035 if (mService.mHeavyWeightProcess != null &&
1036 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
1037 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001038 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001039 if (caller != null) {
1040 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
1041 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001042 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001043 } else {
1044 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -07001045 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -07001046 + intent.toString());
1047 ActivityOptions.abort(options);
1048 return ActivityManager.START_PERMISSION_DENIED;
1049 }
1050 }
1051
1052 IIntentSender target = mService.getIntentSenderLocked(
1053 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -07001054 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -07001055 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
1056 | PendingIntent.FLAG_ONE_SHOT, null);
1057
1058 Intent newIntent = new Intent();
1059 if (requestCode >= 0) {
1060 // Caller is requesting a result.
1061 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
1062 }
1063 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
1064 new IntentSender(target));
1065 if (mService.mHeavyWeightProcess.activities.size() > 0) {
1066 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
1067 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
1068 hist.packageName);
1069 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
1070 hist.task.taskId);
1071 }
1072 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
1073 aInfo.packageName);
1074 newIntent.setFlags(intent.getFlags());
1075 newIntent.setClassName("android",
1076 HeavyWeightSwitcherActivity.class.getName());
1077 intent = newIntent;
1078 resolvedType = null;
1079 caller = null;
1080 callingUid = Binder.getCallingUid();
1081 callingPid = Binder.getCallingPid();
1082 componentSpecified = true;
1083 try {
1084 ResolveInfo rInfo =
1085 AppGlobals.getPackageManager().resolveIntent(
1086 intent, null,
1087 PackageManager.MATCH_DEFAULT_ONLY
1088 | ActivityManagerService.STOCK_PM_FLAGS, userId);
1089 aInfo = rInfo != null ? rInfo.activityInfo : null;
1090 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1091 } catch (RemoteException e) {
1092 aInfo = null;
1093 }
1094 }
1095 }
1096 }
1097
Dianne Hackborn91097de2014-04-04 18:02:06 -07001098 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
1099 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -07001100 requestCode, callingPid, callingUid, callingPackage,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001101 realCallingPid, realCallingUid, startFlags, options, ignoreTargetSecurity,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001102 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001103
Craig Mautner85c11a82014-07-17 12:38:18 -07001104 Binder.restoreCallingIdentity(origId);
1105
Craig Mautnerde4ef022013-04-07 19:01:33 -07001106 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001107 // If the caller also wants to switch to a new configuration,
1108 // do so now. This allows a clean switch, as we are waiting
1109 // for the current activity to pause (so we will not destroy
1110 // it), and have not yet started the next activity.
1111 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
1112 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -07001113 stack.mConfigWillChange = false;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001114 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautner23ac33b2013-04-01 16:26:35 -07001115 "Updating to new configuration after starting activity.");
Maxim Bogatov05075302015-05-19 18:33:08 -07001116 mService.updateConfigurationLocked(config, null, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001117 }
1118
Craig Mautner23ac33b2013-04-01 16:26:35 -07001119 if (outResult != null) {
1120 outResult.result = res;
1121 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001122 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001123 do {
1124 try {
1125 mService.wait();
1126 } catch (InterruptedException e) {
1127 }
1128 } while (!outResult.timeout && outResult.who == null);
1129 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001130 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001131 if (r.nowVisible && r.state == RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001132 outResult.timeout = false;
1133 outResult.who = new ComponentName(r.info.packageName, r.info.name);
1134 outResult.totalTime = 0;
1135 outResult.thisTime = 0;
1136 } else {
1137 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001138 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001139 do {
1140 try {
1141 mService.wait();
1142 } catch (InterruptedException e) {
1143 }
1144 } while (!outResult.timeout && outResult.who == null);
1145 }
1146 }
1147 }
1148
1149 return res;
1150 }
1151 }
1152
1153 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1154 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
1155 Bundle options, int userId) {
1156 if (intents == null) {
1157 throw new NullPointerException("intents is null");
1158 }
1159 if (resolvedTypes == null) {
1160 throw new NullPointerException("resolvedTypes is null");
1161 }
1162 if (intents.length != resolvedTypes.length) {
1163 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1164 }
1165
Craig Mautner23ac33b2013-04-01 16:26:35 -07001166
1167 int callingPid;
1168 if (callingUid >= 0) {
1169 callingPid = -1;
1170 } else if (caller == null) {
1171 callingPid = Binder.getCallingPid();
1172 callingUid = Binder.getCallingUid();
1173 } else {
1174 callingPid = callingUid = -1;
1175 }
1176 final long origId = Binder.clearCallingIdentity();
1177 try {
1178 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001179 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001180 for (int i=0; i<intents.length; i++) {
1181 Intent intent = intents[i];
1182 if (intent == null) {
1183 continue;
1184 }
1185
1186 // Refuse possible leaked file descriptors
1187 if (intent != null && intent.hasFileDescriptors()) {
1188 throw new IllegalArgumentException("File descriptors passed in Intent");
1189 }
1190
1191 boolean componentSpecified = intent.getComponent() != null;
1192
1193 // Don't modify the client's object!
1194 intent = new Intent(intent);
1195
1196 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001197 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001198 // TODO: New, check if this is correct
1199 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1200
1201 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001202 (aInfo.applicationInfo.privateFlags
1203 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001204 throw new IllegalArgumentException(
1205 "FLAG_CANT_SAVE_STATE not supported here");
1206 }
1207
1208 Bundle theseOptions;
1209 if (options != null && i == intents.length-1) {
1210 theseOptions = options;
1211 } else {
1212 theseOptions = null;
1213 }
Craig Mautner6170f732013-04-02 13:05:23 -07001214 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001215 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1216 callingPackage, callingPid, callingUid,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001217 0, theseOptions, false, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001218 if (res < 0) {
1219 return res;
1220 }
1221
1222 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1223 }
1224 }
1225 } finally {
1226 Binder.restoreCallingIdentity(origId);
1227 }
1228
1229 return ActivityManager.START_SUCCESS;
1230 }
1231
Craig Mautner2420ead2013-04-01 17:13:20 -07001232 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001233 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001234 throws RemoteException {
1235
Craig Mautner2568c3a2015-03-26 14:22:34 -07001236 if (andResume) {
1237 r.startFreezingScreenLocked(app, 0);
1238 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001239
Craig Mautner2568c3a2015-03-26 14:22:34 -07001240 // schedule launch ticks to collect information about slow apps.
1241 r.startLaunchTickingLocked();
1242 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001243
1244 // Have the window manager re-evaluate the orientation of
1245 // the screen based on the new activity order. Note that
1246 // as a result of this, it can call back into the activity
1247 // manager with a new orientation. We don't care about that,
1248 // because the activity is not currently running so we are
1249 // just restarting it anyway.
1250 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001251 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001252 mService.mConfiguration,
1253 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001254 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001255 }
1256
1257 r.app = app;
1258 app.waitingToKill = null;
1259 r.launchCount++;
1260 r.lastLaunchTime = SystemClock.uptimeMillis();
1261
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001262 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001263
1264 int idx = app.activities.indexOf(r);
1265 if (idx < 0) {
1266 app.activities.add(r);
1267 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001268 mService.updateLruProcessLocked(app, true, null);
1269 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001270
Craig Mautner15df08a2015-04-01 12:17:18 -07001271 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001272 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1273 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001274 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001275 }
1276
1277 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001278 try {
1279 if (app.thread == null) {
1280 throw new RemoteException();
1281 }
1282 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001283 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001284 if (andResume) {
1285 results = r.results;
1286 newIntents = r.newIntents;
1287 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001288 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1289 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1290 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001291 if (andResume) {
1292 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1293 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001294 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001295 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001296 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001297 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001298 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001299 }
1300 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1301 r.sleeping = false;
1302 r.forceNewConfig = false;
1303 mService.showAskCompatModeDialogLocked(r);
1304 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001305 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001306 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1307 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1308 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001309 final String profileFile = mService.mProfileFile;
1310 if (profileFile != null) {
1311 ParcelFileDescriptor profileFd = mService.mProfileFd;
1312 if (profileFd != null) {
1313 try {
1314 profileFd = profileFd.dup();
1315 } catch (IOException e) {
1316 if (profileFd != null) {
1317 try {
1318 profileFd.close();
1319 } catch (IOException o) {
1320 }
1321 profileFd = null;
1322 }
1323 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001324 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001325
1326 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1327 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001328 }
1329 }
1330 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001331
Craig Mautner2568c3a2015-03-26 14:22:34 -07001332 if (andResume) {
1333 app.hasShownUi = true;
1334 app.pendingUiClean = true;
1335 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001336 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001337 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001338 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001339 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001340 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001341 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001342
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001343 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001344 // This may be a heavy-weight process! Note that the package
1345 // manager will ensure that only activity can run in the main
1346 // process of the .apk, which is the only thing that will be
1347 // considered heavy-weight.
1348 if (app.processName.equals(app.info.packageName)) {
1349 if (mService.mHeavyWeightProcess != null
1350 && mService.mHeavyWeightProcess != app) {
1351 Slog.w(TAG, "Starting new heavy weight process " + app
1352 + " when already running "
1353 + mService.mHeavyWeightProcess);
1354 }
1355 mService.mHeavyWeightProcess = app;
1356 Message msg = mService.mHandler.obtainMessage(
1357 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1358 msg.obj = r;
1359 mService.mHandler.sendMessage(msg);
1360 }
1361 }
1362
1363 } catch (RemoteException e) {
1364 if (r.launchFailed) {
1365 // This is the second time we failed -- finish activity
1366 // and give up.
1367 Slog.e(TAG, "Second failure launching "
1368 + r.intent.getComponent().flattenToShortString()
1369 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001370 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001371 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1372 "2nd-crash", false);
1373 return false;
1374 }
1375
1376 // This is the first time we failed -- restart process and
1377 // retry.
1378 app.activities.remove(r);
1379 throw e;
1380 }
1381
1382 r.launchFailed = false;
1383 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001384 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001385 }
1386
1387 if (andResume) {
1388 // As part of the process of launching, ActivityThread also performs
1389 // a resume.
1390 stack.minimalResumeActivityLocked(r);
1391 } else {
1392 // This activity is not starting in the resumed state... which
1393 // should look like we asked it to pause+stop (but remain visible),
1394 // and it has done so and reported back the current icicle and
1395 // other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001396 if (DEBUG_STATES) Slog.v(TAG_STATES,
1397 "Moving to STOPPED: " + r + " (starting in stopped state)");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001398 r.state = STOPPED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001399 r.stopped = true;
1400 }
1401
1402 // Launch the new version setup screen if needed. We do this -after-
1403 // launching the initial activity (that is, home), so that it can have
1404 // a chance to initialize itself while in the background, making the
1405 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001406 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001407 mService.startSetupActivityLocked();
1408 }
1409
Dianne Hackborn465fa392014-09-14 14:21:18 -07001410 // Update any services we are bound to that might care about whether
1411 // their client may have activities.
1412 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1413
Craig Mautner2420ead2013-04-01 17:13:20 -07001414 return true;
1415 }
1416
Craig Mautnere79d42682013-04-01 19:01:53 -07001417 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001418 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001419 // Is this activity's application already running?
1420 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001421 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001422
1423 r.task.stack.setLaunchTime(r);
1424
1425 if (app != null && app.thread != null) {
1426 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001427 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1428 || !"android".equals(r.info.packageName)) {
1429 // Don't add this if it is a platform component that is marked
1430 // to run in multiple processes, because this is actually
1431 // part of the framework so doesn't make sense to track as a
1432 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001433 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1434 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001435 }
George Mount2c92c972014-03-20 09:38:23 -07001436 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001437 return;
1438 } catch (RemoteException e) {
1439 Slog.w(TAG, "Exception when starting activity "
1440 + r.intent.getComponent().flattenToShortString(), e);
1441 }
1442
1443 // If a dead object exception was thrown -- fall through to
1444 // restart the application.
1445 }
1446
1447 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001448 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001449 }
1450
Craig Mautner6170f732013-04-02 13:05:23 -07001451 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001452 Intent intent, String resolvedType, ActivityInfo aInfo,
1453 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1454 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001455 int callingPid, int callingUid, String callingPackage,
1456 int realCallingPid, int realCallingUid, int startFlags, Bundle options,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001457 boolean ignoreTargetSecurity, boolean componentSpecified, ActivityRecord[] outActivity,
1458 ActivityContainer container, TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001459 int err = ActivityManager.START_SUCCESS;
1460
1461 ProcessRecord callerApp = null;
1462 if (caller != null) {
1463 callerApp = mService.getRecordForAppLocked(caller);
1464 if (callerApp != null) {
1465 callingPid = callerApp.pid;
1466 callingUid = callerApp.info.uid;
1467 } else {
1468 Slog.w(TAG, "Unable to find app for caller " + caller
1469 + " (pid=" + callingPid + ") when starting: "
1470 + intent.toString());
1471 err = ActivityManager.START_PERMISSION_DENIED;
1472 }
1473 }
1474
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001475 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1476
Craig Mautner6170f732013-04-02 13:05:23 -07001477 if (err == ActivityManager.START_SUCCESS) {
Craig Mautner6170f732013-04-02 13:05:23 -07001478 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001479 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001480 + " on display " + (container == null ? (mFocusedStack == null ?
1481 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1482 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1483 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001484 }
1485
1486 ActivityRecord sourceRecord = null;
1487 ActivityRecord resultRecord = null;
1488 if (resultTo != null) {
1489 sourceRecord = isInAnyStackLocked(resultTo);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001490 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
1491 "Will send result to " + resultTo + " " + sourceRecord);
Craig Mautner6170f732013-04-02 13:05:23 -07001492 if (sourceRecord != null) {
1493 if (requestCode >= 0 && !sourceRecord.finishing) {
1494 resultRecord = sourceRecord;
1495 }
1496 }
1497 }
Craig Mautner6170f732013-04-02 13:05:23 -07001498
Dianne Hackborn91097de2014-04-04 18:02:06 -07001499 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001500
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001501 if ((launchFlags & Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001502 // Transfer the result target from the source activity to the new
1503 // one being started, including any failures.
1504 if (requestCode >= 0) {
1505 ActivityOptions.abort(options);
1506 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1507 }
1508 resultRecord = sourceRecord.resultTo;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001509 if (resultRecord != null && !resultRecord.isInStackLocked()) {
1510 resultRecord = null;
1511 }
Craig Mautner6170f732013-04-02 13:05:23 -07001512 resultWho = sourceRecord.resultWho;
1513 requestCode = sourceRecord.requestCode;
1514 sourceRecord.resultTo = null;
1515 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001516 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001517 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001518 if (sourceRecord.launchedFromUid == callingUid) {
1519 // The new activity is being launched from the same uid as the previous
1520 // activity in the flow, and asking to forward its result back to the
1521 // previous. In this case the activity is serving as a trampoline between
1522 // the two, so we also want to update its launchedFromPackage to be the
1523 // same as the previous activity. Note that this is safe, since we know
1524 // these two packages come from the same uid; the caller could just as
1525 // well have supplied that same package name itself. This specifially
1526 // deals with the case of an intent picker/chooser being launched in the app
1527 // flow to redirect to an activity picked by the user, where we want the final
1528 // activity to consider it to have been launched by the previous app activity.
1529 callingPackage = sourceRecord.launchedFromPackage;
1530 }
Craig Mautner6170f732013-04-02 13:05:23 -07001531 }
1532
1533 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1534 // We couldn't find a class that can handle the given Intent.
1535 // That's the end of that!
1536 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1537 }
1538
1539 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1540 // We couldn't find the specific class specified in the Intent.
1541 // Also the end of the line.
1542 err = ActivityManager.START_CLASS_NOT_FOUND;
1543 }
1544
Dianne Hackborn91097de2014-04-04 18:02:06 -07001545 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1546 && sourceRecord.task.voiceSession != null) {
1547 // If this activity is being launched as part of a voice session, we need
1548 // to ensure that it is safe to do so. If the upcoming activity will also
1549 // be part of the voice session, we can only launch it if it has explicitly
1550 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001551 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001552 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1553 try {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001554 intent.addCategory(Intent.CATEGORY_VOICE);
Dianne Hackborn95465202014-09-15 16:21:55 -07001555 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1556 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001557 Slog.w(TAG,
1558 "Activity being started in current voice task does not support voice: "
1559 + intent);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001560 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1561 }
1562 } catch (RemoteException e) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001563 Slog.w(TAG, "Failure checking voice capabilities", e);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001564 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1565 }
1566 }
1567 }
1568
1569 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1570 // If the caller is starting a new voice session, just make sure the target
1571 // is actually allowing it to run this way.
1572 try {
1573 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1574 intent, resolvedType)) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001575 Slog.w(TAG,
1576 "Activity being started in new voice task does not support: "
1577 + intent);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001578 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1579 }
1580 } catch (RemoteException e) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001581 Slog.w(TAG, "Failure checking voice capabilities", e);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001582 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1583 }
1584 }
1585
louis_changcd5d1982014-08-01 10:09:08 +08001586 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1587
Craig Mautner6170f732013-04-02 13:05:23 -07001588 if (err != ActivityManager.START_SUCCESS) {
1589 if (resultRecord != null) {
1590 resultStack.sendActivityResultLocked(-1,
1591 resultRecord, resultWho, requestCode,
1592 Activity.RESULT_CANCELED, null);
1593 }
Craig Mautner6170f732013-04-02 13:05:23 -07001594 ActivityOptions.abort(options);
1595 return err;
1596 }
1597
Svetoslav7008b512015-06-24 18:47:07 -07001598 boolean abort = false;
1599
Svet Ganov99b60432015-06-27 13:15:22 -07001600 final int startAnyPerm = mService.checkPermission(
1601 START_ANY_ACTIVITY, callingPid, callingUid);
1602
1603 if (startAnyPerm != PERMISSION_GRANTED) {
1604 final int componentRestriction = getComponentRestrictionForCallingPackage(
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001605 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
Svet Ganov99b60432015-06-27 13:15:22 -07001606 final int actionRestriction = getActionRestrictionForCallingPackage(
1607 intent.getAction(), callingPackage, callingPid, callingUid);
1608
1609 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1610 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1611 if (resultRecord != null) {
1612 resultStack.sendActivityResultLocked(-1,
1613 resultRecord, resultWho, requestCode,
1614 Activity.RESULT_CANCELED, null);
1615 }
1616 String msg;
1617 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1618 msg = "Permission Denial: starting " + intent.toString()
1619 + " from " + callerApp + " (pid=" + callingPid
1620 + ", uid=" + callingUid + ")" + " with revoked permission "
1621 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1622 } else if (!aInfo.exported) {
1623 msg = "Permission Denial: starting " + intent.toString()
1624 + " from " + callerApp + " (pid=" + callingPid
1625 + ", uid=" + callingUid + ")"
1626 + " not exported from uid " + aInfo.applicationInfo.uid;
1627 } else {
1628 msg = "Permission Denial: starting " + intent.toString()
1629 + " from " + callerApp + " (pid=" + callingPid
1630 + ", uid=" + callingUid + ")"
1631 + " requires " + aInfo.permission;
1632 }
1633 Slog.w(TAG, msg);
1634 throw new SecurityException(msg);
1635 }
1636
1637 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1638 String message = "Appop Denial: starting " + intent.toString()
1639 + " from " + callerApp + " (pid=" + callingPid
1640 + ", uid=" + callingUid + ")"
1641 + " requires " + AppOpsManager.permissionToOp(
1642 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1643 Slog.w(TAG, message);
1644 abort = true;
1645 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1646 String message = "Appop Denial: starting " + intent.toString()
1647 + " from " + callerApp + " (pid=" + callingPid
1648 + ", uid=" + callingUid + ")"
1649 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1650 Slog.w(TAG, message);
1651 abort = true;
1652 }
Svetoslav7008b512015-06-24 18:47:07 -07001653 }
1654
1655 abort |= !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001656 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001657
Craig Mautner6170f732013-04-02 13:05:23 -07001658 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001659 try {
1660 // The Intent we give to the watcher has the extra data
1661 // stripped off, since it can contain private information.
1662 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001663 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001664 aInfo.applicationInfo.packageName);
1665 } catch (RemoteException e) {
1666 mService.mController = null;
1667 }
Ben Gruver5e207332013-04-03 17:41:37 -07001668 }
Craig Mautner6170f732013-04-02 13:05:23 -07001669
Clara Bayarrif7fea162015-10-22 16:09:52 +01001670 UserInfo user = getUserInfo(userId);
1671 // TODO: Timeout for work challenge
1672 if (user.isManagedProfile()
1673 && mService.mContext.getSystemService(StorageManager.class)
1674 .isPerUserEncryptionEnabled()) {
1675 KeyguardManager km = (KeyguardManager) mService.mContext
1676 .getSystemService(Context.KEYGUARD_SERVICE);
1677
1678 IIntentSender target = mService.getIntentSenderLocked(
1679 ActivityManager.INTENT_SENDER_ACTIVITY, callingPackage,
1680 Binder.getCallingUid(), userId, null, null, 0, new Intent[]{ intent },
1681 new String[]{ resolvedType },
1682 PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT
1683 | PendingIntent.FLAG_IMMUTABLE, null);
1684 int flags = intent.getFlags();
1685 intent = km.createConfirmDeviceCredentialIntent(null, null);
1686 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1687 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1688 intent.putExtra(Intent.EXTRA_PACKAGE_NAME, aInfo.packageName);
1689 intent.putExtra(Intent.EXTRA_INTENT, new IntentSender(target));
1690 intent.putExtra(Intent.EXTRA_USER_ID, userId);
1691 intent.setFlags(flags);
1692
1693 resolvedType = null;
1694 callingUid = realCallingUid;
1695 callingPid = realCallingPid;
1696
1697 UserInfo parent = UserManager.get(mService.mContext).getProfileParent(userId);
1698 aInfo = resolveActivity(intent, null, PackageManager.MATCH_DEFAULT_ONLY
1699 | ActivityManagerService.STOCK_PM_FLAGS, null, parent.id);
1700 }
1701
Ben Gruver5e207332013-04-03 17:41:37 -07001702 if (abort) {
1703 if (resultRecord != null) {
1704 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001705 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001706 }
Ben Gruver5e207332013-04-03 17:41:37 -07001707 // We pretend to the caller that it was really started, but
1708 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001709 ActivityOptions.abort(options);
1710 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001711 }
1712
1713 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001714 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001715 requestCode, componentSpecified, voiceSession != null, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001716 if (outActivity != null) {
1717 outActivity[0] = r;
1718 }
1719
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001720 if (r.appTimeTracker == null && sourceRecord != null) {
1721 // If the caller didn't specify an explicit time tracker, we want to continue
1722 // tracking under any it has.
1723 r.appTimeTracker = sourceRecord.appTimeTracker;
1724 }
1725
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001726 final ActivityStack stack = mFocusedStack;
Dianne Hackborn91097de2014-04-04 18:02:06 -07001727 if (voiceSession == null && (stack.mResumedActivity == null
1728 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001729 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1730 realCallingPid, realCallingUid, "Activity start")) {
Robert Carr13997f52015-10-23 13:13:39 -07001731 PendingActivityLaunch pal = new PendingActivityLaunch(r,
1732 sourceRecord, startFlags, stack, callerApp);
Craig Mautneree36c772014-07-16 14:56:05 -07001733 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001734 ActivityOptions.abort(options);
1735 return ActivityManager.START_SWITCHES_CANCELED;
1736 }
1737 }
1738
1739 if (mService.mDidAppSwitch) {
1740 // This is the second allowed switch since we stopped switches,
1741 // so now just generally allow switches. Use case: user presses
1742 // home (switches disabled, switch to home, mDidAppSwitch now true);
1743 // user taps a home icon (coming from home so allowed, we hit here
1744 // and now allow anyone to switch again).
1745 mService.mAppSwitchesAllowedTime = 0;
1746 } else {
1747 mService.mDidAppSwitch = true;
1748 }
1749
Craig Mautneree36c772014-07-16 14:56:05 -07001750 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001751
Dianne Hackborn91097de2014-04-04 18:02:06 -07001752 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001753 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001754
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001755 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001756 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001757 // activity start, but we are not actually doing an activity
1758 // switch... just dismiss the keyguard now, because we
1759 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001760 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001761 }
1762 return err;
1763 }
1764
Clara Bayarrif7fea162015-10-22 16:09:52 +01001765 private UserInfo getUserInfo(int userId) {
1766 final long identity = Binder.clearCallingIdentity();
1767 try {
1768 return UserManager.get(mService.mContext).getUserInfo(userId);
1769 } finally {
1770 Binder.restoreCallingIdentity(identity);
1771 }
1772 }
1773
Svet Ganov99b60432015-06-27 13:15:22 -07001774 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001775 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001776 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1777 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001778 == PackageManager.PERMISSION_DENIED) {
1779 return ACTIVITY_RESTRICTION_PERMISSION;
1780 }
1781
Christopher Tateff7add02015-08-17 10:23:22 -07001782 if (activityInfo.permission == null) {
1783 return ACTIVITY_RESTRICTION_NONE;
1784 }
1785
Svet Ganov99b60432015-06-27 13:15:22 -07001786 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1787 if (opCode == AppOpsManager.OP_NONE) {
1788 return ACTIVITY_RESTRICTION_NONE;
1789 }
1790
1791 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1792 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001793 if (!ignoreTargetSecurity) {
1794 return ACTIVITY_RESTRICTION_APPOP;
1795 }
Svet Ganov99b60432015-06-27 13:15:22 -07001796 }
1797
1798 return ACTIVITY_RESTRICTION_NONE;
1799 }
1800
Svetoslav7008b512015-06-24 18:47:07 -07001801 private int getActionRestrictionForCallingPackage(String action,
1802 String callingPackage, int callingPid, int callingUid) {
1803 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001804 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001805 }
1806
1807 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1808 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001809 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001810 }
1811
1812 final PackageInfo packageInfo;
1813 try {
1814 packageInfo = mService.mContext.getPackageManager()
1815 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1816 } catch (PackageManager.NameNotFoundException e) {
1817 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001818 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001819 }
1820
1821 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001822 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001823 }
1824
1825 if (mService.checkPermission(permission, callingPid, callingUid) ==
1826 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001827 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001828 }
1829
1830 final int opCode = AppOpsManager.permissionToOpCode(permission);
1831 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001832 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001833 }
1834
1835 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1836 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001837 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001838 }
1839
Svet Ganov99b60432015-06-27 13:15:22 -07001840 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001841 }
1842
Chong Zhang0fa656b2015-08-31 15:17:21 -07001843 ActivityStack computeStackFocus(ActivityRecord r, boolean newTask, Rect bounds) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001844 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001845
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001846 if (!(r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
1847 return mHomeStack;
1848 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001849
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001850 ActivityStack stack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001851
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001852 if (task != null && task.stack != null) {
1853 stack = task.stack;
1854 if (stack.isOnHomeDisplay()) {
1855 if (mFocusedStack != stack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001856 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001857 "computeStackFocus: Setting " + "focused stack to r=" + r
1858 + " task=" + task);
1859 } else {
1860 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1861 "computeStackFocus: Focused stack already=" + mFocusedStack);
Craig Mautner858d8a62013-04-23 17:08:34 -07001862 }
1863 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001864 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001865 }
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001866
1867 final ActivityContainer container = r.mInitialActivityContainer;
1868 if (container != null) {
1869 // The first time put it on the desired stack, after this put on task stack.
1870 r.mInitialActivityContainer = null;
1871 return container.mStack;
1872 }
1873
1874 // The fullscreen stack can contain any task regardless of if the task is resizeable
1875 // or not. So, we let the task go in the fullscreen task if it is the focus stack.
1876 // If the freeform stack has focus, and the activity to be launched is resizeable,
1877 // we can also put it in the focused stack.
1878 final boolean canUseFocusedStack =
1879 mFocusedStack.mStackId == FULLSCREEN_WORKSPACE_STACK_ID
1880 || mFocusedStack.mStackId == FREEFORM_WORKSPACE_STACK_ID && r.info.resizeable;
1881 if (canUseFocusedStack
1882 && (!newTask || mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
1883 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1884 "computeStackFocus: Have a focused stack=" + mFocusedStack);
1885 return mFocusedStack;
1886 }
1887
1888 // We first try to put the task in the first dynamic stack.
1889 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1890 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
1891 stack = homeDisplayStacks.get(stackNdx);
Wale Ogunwale3797c222015-10-27 14:21:58 -07001892 if (!StackId.isStaticStack(stack.mStackId)) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001893 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1894 "computeStackFocus: Setting focused stack=" + stack);
1895 return stack;
1896 }
1897 }
1898
1899 // If there is no suitable dynamic stack then we figure out which static stack to use.
1900 final int stackId = task != null ? task.getLaunchStackId() :
1901 bounds != null ? FREEFORM_WORKSPACE_STACK_ID :
1902 FULLSCREEN_WORKSPACE_STACK_ID;
1903 stack = getStack(stackId, CREATE_IF_NEEDED, ON_TOP);
1904 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS, "computeStackFocus: New stack r="
1905 + r + " stackId=" + stack.mStackId);
1906 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001907 }
1908
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001909 boolean setFocusedStack(ActivityRecord r, String reason) {
1910 if (r == null) {
1911 // Not sure what you are trying to do, but it is not going to work...
1912 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001913 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001914 final TaskRecord task = r.task;
1915 if (task == null || task.stack == null) {
1916 Slog.w(TAG, "Can't set focus stack for r=" + r + " task=" + task);
1917 return false;
1918 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001919 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001920 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001921 }
1922
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001923 final int startActivityUncheckedLocked(final ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001924 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001925 boolean doResume, Bundle options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001926 final Intent intent = r.intent;
1927 final int callingUid = r.launchedFromUid;
1928
Chong Zhang0fa656b2015-08-31 15:17:21 -07001929 boolean overrideBounds = false;
1930 Rect newBounds = null;
Chong Zhang56f171d2015-10-08 10:39:23 -07001931 if (options != null && (r.info.resizeable || (inTask != null && inTask.mResizeable))) {
1932 ActivityOptions opts = new ActivityOptions(options);
1933 if (opts.hasBounds()) {
Chong Zhang0fa656b2015-08-31 15:17:21 -07001934 overrideBounds = true;
Chong Zhang56f171d2015-10-08 10:39:23 -07001935 newBounds = opts.getBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07001936 }
1937 }
1938
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001939 // In some flows in to this function, we retrieve the task record and hold on to it
1940 // without a lock before calling back in to here... so the task at this point may
1941 // not actually be in recents. Check for that, and if it isn't in recents just
1942 // consider it invalid.
1943 if (inTask != null && !inTask.inRecents) {
1944 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1945 inTask = null;
1946 }
1947
Craig Mautnerad400af2014-08-13 16:41:36 -07001948 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001949 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1950 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001951
Craig Mautnera228ae92014-07-09 05:44:55 -07001952 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001953 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001954 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001955 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1956 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1957 "\"singleInstance\" or \"singleTask\"");
1958 launchFlags &=
1959 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1960 } else {
1961 switch (r.info.documentLaunchMode) {
1962 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1963 break;
1964 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1965 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1966 break;
1967 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1968 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1969 break;
1970 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1971 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1972 break;
1973 }
1974 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001975
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001976 final boolean launchTaskBehind = r.mLaunchTaskBehind
1977 && !launchSingleTask && !launchSingleInstance
1978 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001979
Wale Ogunwale7d701172015-03-11 15:36:30 -07001980 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0
1981 && r.resultTo.task.stack != null) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001982 // For whatever reason this activity is being launched into a new
1983 // task... yet the caller has requested a result back. Well, that
1984 // is pretty messed up, so instead immediately send back a cancel
1985 // and let the new task continue launched as normal without a
1986 // dependency on its originator.
1987 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1988 r.resultTo.task.stack.sendActivityResultLocked(-1,
1989 r.resultTo, r.resultWho, r.requestCode,
1990 Activity.RESULT_CANCELED, null);
1991 r.resultTo = null;
1992 }
1993
1994 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1995 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1996 }
1997
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001998 // If we are actually going to launch in to a new task, there are some cases where
1999 // we further want to do multiple task.
2000 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
2001 if (launchTaskBehind
2002 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
2003 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
2004 }
2005 }
2006
Craig Mautner8849a5e2013-04-02 16:41:03 -07002007 // We'll invoke onUserLeaving before onPause only if the launching
2008 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07002009 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002010 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
2011 "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002012
2013 // If the caller has asked not to resume at this point, we make note
2014 // of this in the record so that we can skip it when trying to find
2015 // the top running activity.
Chong Zhang45c25ce2015-08-10 22:18:26 -07002016 if (!doResume || !okToShowLocked(r)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002017 r.delayedResume = true;
Chong Zhang45c25ce2015-08-10 22:18:26 -07002018 doResume = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002019 }
2020
Craig Mautnera254cd72014-05-25 16:47:39 -07002021 ActivityRecord notTop =
2022 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002023
2024 // If the onlyIfNeeded flag is set, then we can do this if the activity
2025 // being launched is the same as the one making the call... or, as
2026 // a special case, if we do not know the caller then we count the
2027 // current top activity as the caller.
2028 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2029 ActivityRecord checkedCaller = sourceRecord;
2030 if (checkedCaller == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002031 checkedCaller = mFocusedStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002032 }
2033 if (!checkedCaller.realActivity.equals(r.realActivity)) {
2034 // Caller is not the same as launcher, so always needed.
2035 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
2036 }
2037 }
2038
Craig Mautner8849a5e2013-04-02 16:41:03 -07002039 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002040 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002041
2042 // If the caller is not coming from another activity, but has given us an
2043 // explicit task into which they would like us to launch the new activity,
2044 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002045 if (sourceRecord == null && inTask != null && inTask.stack != null) {
2046 final Intent baseIntent = inTask.getBaseIntent();
2047 final ActivityRecord root = inTask.getRootActivity();
2048 if (baseIntent == null) {
2049 ActivityOptions.abort(options);
2050 throw new IllegalArgumentException("Launching into task without base intent: "
2051 + inTask);
2052 }
2053
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002054 // If this task is empty, then we are adding the first activity -- it
2055 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002056 if (launchSingleInstance || launchSingleTask) {
2057 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
2058 ActivityOptions.abort(options);
2059 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
2060 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002061 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002062 if (root != null) {
2063 ActivityOptions.abort(options);
2064 throw new IllegalArgumentException("Caller with inTask " + inTask
2065 + " has root " + root + " but target is singleInstance/Task");
2066 }
2067 }
2068
2069 // If task is empty, then adopt the interesting intent launch flags in to the
2070 // activity being started.
2071 if (root == null) {
2072 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
2073 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
2074 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
2075 launchFlags = (launchFlags&~flagsOfInterest)
2076 | (baseIntent.getFlags()&flagsOfInterest);
2077 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002078 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07002079 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002080
Dianne Hackborn962d5352014-08-29 16:27:40 -07002081 // If the task is not empty and the caller is asking to start it as the root
2082 // of a new task, then we don't actually want to start this on the task. We
2083 // will bring the task to the front, and possibly give it a new intent.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002084 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07002085 addingToTask = false;
2086
2087 } else {
2088 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002089 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07002090
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002091 reuseTask = inTask;
2092 } else {
2093 inTask = null;
Chong Zhang85ee6542015-10-02 13:36:38 -07002094 // Launch ResolverActivity in the source task, so that it stays in the task
2095 // bounds when in freeform workspace.
2096 // Also put noDisplay activities in the source task. These by itself can
2097 // be placed in any task/stack, however it could launch other activities
2098 // like ResolverActivity, and we want those to stay in the original task.
2099 if (r.isResolverActivity() || r.noDisplay) {
2100 addingToTask = true;
2101 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002102 }
2103
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002104 if (inTask == null) {
2105 if (sourceRecord == null) {
2106 // This activity is not being started from another... in this
2107 // case we -always- start a new task.
2108 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
2109 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
2110 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
2111 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2112 }
2113 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
2114 // The original activity who is starting us is running as a single
2115 // instance... this new activity it is starting must go on its
2116 // own task.
2117 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2118 } else if (launchSingleInstance || launchSingleTask) {
2119 // The activity being started is a single instance... it always
2120 // gets launched into its own task.
2121 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2122 }
2123 }
2124
2125 ActivityInfo newTaskInfo = null;
2126 Intent newTaskIntent = null;
2127 ActivityStack sourceStack;
2128 if (sourceRecord != null) {
2129 if (sourceRecord.finishing) {
2130 // If the source is finishing, we can't further count it as our source. This
2131 // is because the task it is associated with may now be empty and on its way out,
2132 // so we don't want to blindly throw it in to that task. Instead we will take
2133 // the NEW_TASK flow and try to find a task for it. But save the task information
2134 // so it can be used when creating the new task.
2135 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
2136 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
2137 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
2138 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2139 newTaskInfo = sourceRecord.info;
2140 newTaskIntent = sourceRecord.task.intent;
2141 }
2142 sourceRecord = null;
2143 sourceStack = null;
2144 } else {
2145 sourceStack = sourceRecord.task.stack;
2146 }
2147 } else {
2148 sourceStack = null;
2149 }
2150
2151 boolean movedHome = false;
2152 ActivityStack targetStack;
2153
2154 intent.setFlags(launchFlags);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002155 final boolean noAnimation = (launchFlags & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002156
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002157 // We may want to try to place the new activity in to an existing task. We always
2158 // do this if the target activity is singleTask or singleInstance; we will also do
2159 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
2160 // us to still place it in a new task: multi task, always doc mode, or being asked to
2161 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07002162 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
2163 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07002164 || launchSingleInstance || launchSingleTask) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002165 // If bring to front is requested, and no result is requested and we have not
2166 // been given an explicit task to launch in to, and
Craig Mautner8849a5e2013-04-02 16:41:03 -07002167 // we can find a task that was started with this same
2168 // component, then instead of launching bring that one to the front.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002169 if (inTask == null && r.resultTo == null) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002170 // See if there is a task to bring to the front. If this is
2171 // a SINGLE_INSTANCE activity, there can be one and only one
2172 // instance of it in the history, and it is always in its own
2173 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07002174 ActivityRecord intentActivity = !launchSingleInstance ?
2175 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002176 if (intentActivity != null) {
Jason Monk25d237b2015-06-19 10:39:39 -04002177 // When the flags NEW_TASK and CLEAR_TASK are set, then the task gets reused
2178 // but still needs to be a lock task mode violation since the task gets
2179 // cleared out and the device would otherwise leave the locked task.
2180 if (isLockTaskModeViolation(intentActivity.task,
2181 (launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
2182 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))) {
Craig Mautner6cd6cec2015-04-01 00:02:12 -07002183 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07002184 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08002185 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2186 }
Craig Mautner29219d92013-04-16 20:19:12 -07002187 if (r.task == null) {
2188 r.task = intentActivity.task;
2189 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002190 if (intentActivity.task.intent == null) {
2191 // This task was started because of movement of
2192 // the activity based on affinity... now that we
2193 // are actually launching it, we can assign the
2194 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07002195 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002196 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002197 targetStack = intentActivity.task.stack;
2198 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002199 // If the target task is not in the front, then we need
2200 // to bring it to the front... except... well, with
2201 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
2202 // to have the same behavior as if a new instance was
2203 // being started, which means not bringing it to the front
2204 // if the caller is not itself in the front.
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002205 final ActivityStack focusStack = getFocusedStack();
2206 ActivityRecord curTop = (focusStack == null)
2207 ? null : focusStack.topRunningNonDelayedActivityLocked(notTop);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002208 boolean movedToFront = false;
Craig Mautner7504d7b2013-09-17 10:50:53 -07002209 if (curTop != null && (curTop.task != intentActivity.task ||
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002210 curTop.task != focusStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002211 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07002212 if (sourceRecord == null || (sourceStack.topActivity() != null &&
2213 sourceStack.topActivity().task == sourceRecord.task)) {
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002214 // We really do want to push this one into the user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07002215 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002216 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
2217 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002218 movedHome = true;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002219 targetStack.moveTaskToFrontLocked(intentActivity.task, noAnimation,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002220 options, r.appTimeTracker, "bringingFoundTaskToFront");
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002221 movedToFront = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002222 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07002223 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2224 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07002225 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07002226 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002227 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002228 options = null;
2229 }
2230 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002231 if (!movedToFront && doResume) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002232 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Bring to front target: " + targetStack
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002233 + " from " + intentActivity);
2234 targetStack.moveToFront("intentActivityFound");
2235 }
2236
Craig Mautner8849a5e2013-04-02 16:41:03 -07002237 // If the caller has requested that the target task be
2238 // reset, then do so.
2239 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
2240 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
2241 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002242 if ((startFlags & ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002243 // We don't need to start a new activity, and
2244 // the client said not to do anything if that
2245 // is the case, so this is it! And for paranoia, make
2246 // sure we have correctly resumed the top activity.
2247 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002248 resumeTopActivitiesLocked(targetStack, null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002249
2250 // Make sure to notify Keyguard as well if we are not running an app
2251 // transition later.
2252 if (!movedToFront) {
2253 notifyActivityDrawnForKeyguard();
2254 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002255 } else {
2256 ActivityOptions.abort(options);
2257 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002258 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002259 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2260 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002261 if ((launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002262 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002263 // The caller has requested to completely replace any
2264 // existing task with its new activity. Well that should
2265 // not be too hard...
2266 reuseTask = intentActivity.task;
2267 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07002268 reuseTask.setIntent(r);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002269 } else if ((launchFlags & FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07002270 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002271 // In this situation we want to remove all activities
2272 // from the task up to the one being started. In most
2273 // cases this means we are resetting the task to its
2274 // initial state.
2275 ActivityRecord top =
2276 intentActivity.task.performClearTaskLocked(r, launchFlags);
2277 if (top != null) {
2278 if (top.frontOfTask) {
2279 // Activity aliases may mean we use different
2280 // intents for the top activity, so make sure
2281 // the task now has the identity of the new
2282 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07002283 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002284 }
2285 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
2286 r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002287 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002288 } else {
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002289 // A special case: we need to start the activity because it is not
2290 // currently running, and the caller has asked to clear the current
2291 // task to have this activity at the top.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002292 addingToTask = true;
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002293 // Now pretend like this activity is being started by the top of its
2294 // task, so it is put in the right place.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002295 sourceRecord = intentActivity;
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002296 TaskRecord task = sourceRecord.task;
2297 if (task != null && task.stack == null) {
2298 // Target stack got cleared when we all activities were removed
2299 // above. Go ahead and reset it.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002300 targetStack = computeStackFocus(
2301 sourceRecord, false /* newTask */, null /* bounds */);
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002302 targetStack.addTask(
2303 task, !launchTaskBehind /* toTop */, false /* moving */);
2304 }
2305
Craig Mautner8849a5e2013-04-02 16:41:03 -07002306 }
2307 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
2308 // In this case the top activity on the task is the
2309 // same as the one being launched, so we take that
2310 // as a request to bring the task to the foreground.
2311 // If the top activity in the task is the root
2312 // activity, deliver this new intent to it if it
2313 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07002314 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07002315 && intentActivity.realActivity.equals(r.realActivity)) {
2316 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
2317 intentActivity.task);
2318 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07002319 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002320 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002321 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
2322 r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002323 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
2324 // In this case we are launching the root activity
2325 // of the task, but with a different intent. We
2326 // should start a new instance on top.
2327 addingToTask = true;
2328 sourceRecord = intentActivity;
2329 }
2330 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
2331 // In this case an activity is being launched in to an
2332 // existing task, without resetting that task. This
2333 // is typically the situation of launching an activity
2334 // from a notification or shortcut. We want to place
2335 // the new activity on top of the current task.
2336 addingToTask = true;
2337 sourceRecord = intentActivity;
2338 } else if (!intentActivity.task.rootWasReset) {
2339 // In this case we are launching in to an existing task
2340 // that has not yet been started from its front door.
2341 // The current task has been brought to the front.
2342 // Ideally, we'd probably like to place this new task
2343 // at the bottom of its stack, but that's a little hard
2344 // to do with the current organization of the code so
2345 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07002346 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002347 }
2348 if (!addingToTask && reuseTask == null) {
2349 // We didn't do anything... but it was needed (a.k.a., client
2350 // don't use that intent!) And for paranoia, make
2351 // sure we have correctly resumed the top activity.
2352 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002353 targetStack.resumeTopActivityLocked(null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002354 if (!movedToFront) {
2355 // Make sure to notify Keyguard as well if we are not running an app
2356 // transition later.
2357 notifyActivityDrawnForKeyguard();
2358 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002359 } else {
2360 ActivityOptions.abort(options);
2361 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002362 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002363 return ActivityManager.START_TASK_TO_FRONT;
2364 }
2365 }
2366 }
2367 }
2368
2369 //String uri = r.intent.toURI();
2370 //Intent intent2 = new Intent(uri);
2371 //Slog.i(TAG, "Given intent: " + r.intent);
2372 //Slog.i(TAG, "URI is: " + uri);
2373 //Slog.i(TAG, "To intent: " + intent2);
2374
2375 if (r.packageName != null) {
2376 // If the activity being launched is the same as the one currently
2377 // at the top, then we need to check if it should only be launched
2378 // once.
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002379 ActivityStack topStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002380 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002381 if (top != null && r.resultTo == null) {
2382 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2383 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07002384 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07002385 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002386 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
2387 top.task);
2388 // For paranoia, make sure we have correctly
2389 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002390 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002391 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002392 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002393 }
2394 ActivityOptions.abort(options);
2395 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2396 // We don't need to start a new activity, and
2397 // the client said not to do anything if that
2398 // is the case, so this is it!
2399 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2400 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002401 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002402 return ActivityManager.START_DELIVERED_TO_TOP;
2403 }
2404 }
2405 }
2406 }
2407
2408 } else {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002409 if (r.resultTo != null && r.resultTo.task.stack != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002410 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2411 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002412 }
2413 ActivityOptions.abort(options);
2414 return ActivityManager.START_CLASS_NOT_FOUND;
2415 }
2416
2417 boolean newTask = false;
2418 boolean keepCurTransition = false;
2419
Craig Mautnerbb742462014-07-07 15:28:55 -07002420 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002421 sourceRecord.task : null;
2422
Craig Mautner8849a5e2013-04-02 16:41:03 -07002423 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002424 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002425 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002426 newTask = true;
Chong Zhang0fa656b2015-08-31 15:17:21 -07002427 targetStack = computeStackFocus(r, newTask, newBounds);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002428 if (doResume) {
2429 targetStack.moveToFront("startingNewTask");
2430 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002431
Craig Mautner8849a5e2013-04-02 16:41:03 -07002432 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002433 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2434 newTaskInfo != null ? newTaskInfo : r.info,
2435 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002436 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2437 taskToAffiliate);
Chong Zhang0fa656b2015-08-31 15:17:21 -07002438 if (overrideBounds) {
2439 r.task.updateOverrideConfiguration(newBounds);
2440 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002441 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2442 "Starting new activity " + r + " in new task " + r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002443 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002444 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002445 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002446 if (isLockTaskModeViolation(r.task)) {
2447 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2448 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2449 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002450 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002451 if ((launchFlags &
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002452 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2453 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002454 // Caller wants to appear on home activity, so before starting
2455 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002456 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002457 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002458 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002459 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002460 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002461 if (isLockTaskModeViolation(sourceTask)) {
2462 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2463 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2464 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002465 targetStack = sourceTask.stack;
Chong Zhang45c25ce2015-08-10 22:18:26 -07002466 if (doResume) {
2467 targetStack.moveToFront("sourceStackToFront");
2468 }
Craig Mautner737fae22014-10-15 12:52:10 -07002469 final TaskRecord topTask = targetStack.topTask();
2470 if (topTask != sourceTask) {
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002471 targetStack.moveTaskToFrontLocked(sourceTask, noAnimation, options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002472 r.appTimeTracker, "sourceTaskToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002473 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002474 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002475 // In this case, we are adding the activity to an existing
2476 // task, but the caller has asked to clear that task if the
2477 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002478 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002479 keepCurTransition = true;
2480 if (top != null) {
2481 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002482 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002483 // For paranoia, make sure we have correctly
2484 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002485 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002486 if (doResume) {
2487 targetStack.resumeTopActivityLocked(null);
2488 }
2489 ActivityOptions.abort(options);
2490 return ActivityManager.START_DELIVERED_TO_TOP;
2491 }
2492 } else if (!addingToTask &&
2493 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2494 // In this case, we are launching an activity in our own task
2495 // that may already be running somewhere in the history, and
2496 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002497 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002498 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002499 final TaskRecord task = top.task;
2500 task.moveActivityToFrontLocked(top);
2501 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002502 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002503 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002504 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002505 if (doResume) {
2506 targetStack.resumeTopActivityLocked(null);
2507 }
2508 return ActivityManager.START_DELIVERED_TO_TOP;
2509 }
2510 }
2511 // An existing activity is starting this new activity, so we want
2512 // to keep the new one in the same task as the one that is starting
2513 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002514 r.setTask(sourceTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002515 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002516 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002517
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002518 } else if (inTask != null) {
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002519 // The caller is asking that the new activity be started in an explicit
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002520 // task it has provided to us.
2521 if (isLockTaskModeViolation(inTask)) {
2522 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2523 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2524 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002525 if (overrideBounds) {
2526 inTask.updateOverrideConfiguration(newBounds);
2527 int stackId = inTask.getLaunchStackId();
2528 if (stackId != inTask.stack.mStackId) {
2529 moveTaskToStackUncheckedLocked(
2530 inTask, stackId, ON_TOP, !FORCE_FOCUS, "inTaskToFront");
2531 }
2532 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002533 targetStack = inTask.stack;
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002534 targetStack.moveTaskToFrontLocked(inTask, noAnimation, options, r.appTimeTracker,
2535 "inTaskToFront");
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002536
2537 // Check whether we should actually launch the new activity in to the task,
2538 // or just reuse the current activity on top.
2539 ActivityRecord top = inTask.getTopActivity();
2540 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2541 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2542 || launchSingleTop || launchSingleTask) {
2543 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2544 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2545 // We don't need to start a new activity, and
2546 // the client said not to do anything if that
2547 // is the case, so this is it!
2548 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2549 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002550 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002551 return ActivityManager.START_DELIVERED_TO_TOP;
2552 }
2553 }
2554
Dianne Hackborn962d5352014-08-29 16:27:40 -07002555 if (!addingToTask) {
2556 // We don't actually want to have this activity added to the task, so just
2557 // stop here but still tell the caller that we consumed the intent.
2558 ActivityOptions.abort(options);
2559 return ActivityManager.START_TASK_TO_FRONT;
2560 }
2561
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002562 r.setTask(inTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002563 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002564 + " in explicit task " + r.task);
2565
Craig Mautner8849a5e2013-04-02 16:41:03 -07002566 } else {
2567 // This not being started from an existing activity, and not part
2568 // of a new task... just put it in the top task, though these days
2569 // this case should never happen.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002570 targetStack = computeStackFocus(r, newTask, null /* bounds */);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002571 if (doResume) {
2572 targetStack.moveToFront("addingToTopTask");
2573 }
Craig Mautner1602ec22013-05-12 10:24:27 -07002574 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002575 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002576 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002577 mWindowManager.moveTaskToTop(r.task.taskId);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002578 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Craig Mautner8849a5e2013-04-02 16:41:03 -07002579 + " in new guessed " + r.task);
2580 }
2581
2582 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002583 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002584
Craig Mautner76e2a762014-06-24 09:05:43 -07002585 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2586 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2587 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002588 if (newTask) {
2589 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2590 }
2591 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002592 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002593 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002594 if (!launchTaskBehind && doResume) {
Craig Mautner299f9602015-01-26 09:47:33 -08002595 mService.setFocusedActivityLocked(r, "startedActivity");
Craig Mautnerbb742462014-07-07 15:28:55 -07002596 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002597 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002598 return ActivityManager.START_SUCCESS;
2599 }
2600
Craig Mautneree36c772014-07-16 14:56:05 -07002601 final void doPendingActivityLaunchesLocked(boolean doResume) {
2602 while (!mPendingActivityLaunches.isEmpty()) {
2603 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Robert Carrf1ddb5e2015-10-20 14:44:45 -07002604
2605 try {
2606 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Robert Carr13997f52015-10-23 13:13:39 -07002607 doResume && mPendingActivityLaunches.isEmpty(), null, null);
Robert Carrf1ddb5e2015-10-20 14:44:45 -07002608 } catch (Exception e) {
Robert Carr13997f52015-10-23 13:13:39 -07002609 Slog.e(TAG, "Exception during pending activity launch pal=" + pal, e);
2610 pal.sendErrorResult(e.getMessage());
Robert Carrf1ddb5e2015-10-20 14:44:45 -07002611 }
Craig Mautneree36c772014-07-16 14:56:05 -07002612 }
2613 }
2614
Craig Mautner7f13ed32014-07-28 14:00:35 -07002615 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002616 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2617 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002618 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002619 mPendingActivityLaunches.remove(palNdx);
2620 }
2621 }
2622 }
2623
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002624 void setLaunchSource(int uid) {
2625 mLaunchingActivity.setWorkSource(new WorkSource(uid));
2626 }
2627
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002628 void acquireLaunchWakelock() {
2629 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2630 throw new IllegalStateException("Calling must be system uid");
2631 }
2632 mLaunchingActivity.acquire();
2633 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2634 // To be safe, don't allow the wake lock to be held for too long.
2635 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2636 }
2637 }
2638
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002639 /**
2640 * Called when the frontmost task is idle.
2641 * @return the state of mService.mBooting before this was called.
2642 */
2643 private boolean checkFinishBootingLocked() {
2644 final boolean booting = mService.mBooting;
2645 boolean enableScreen = false;
2646 mService.mBooting = false;
2647 if (!mService.mBooted) {
2648 mService.mBooted = true;
2649 enableScreen = true;
2650 }
2651 if (booting || enableScreen) {
2652 mService.postFinishBooting(booting, enableScreen);
2653 }
2654 return booting;
2655 }
2656
Craig Mautnerf3333272013-04-22 10:55:53 -07002657 // Checked.
2658 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2659 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002660 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002661
Craig Mautnerf3333272013-04-22 10:55:53 -07002662 ArrayList<ActivityRecord> stops = null;
2663 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07002664 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07002665 int NS = 0;
2666 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002667 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002668 boolean activityRemoved = false;
2669
Wale Ogunwale7d701172015-03-11 15:36:30 -07002670 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002671 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002672 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
2673 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002674 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2675 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002676 if (fromTimeout) {
2677 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002678 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002679
2680 // This is a hack to semi-deal with a race condition
2681 // in the client where it can be constructed with a
2682 // newer configuration from when we asked it to launch.
2683 // We'll update with whatever configuration it now says
2684 // it used to launch.
2685 if (config != null) {
2686 r.configuration = config;
2687 }
2688
2689 // We are now idle. If someone is waiting for a thumbnail from
2690 // us, we can now deliver.
2691 r.idle = true;
2692
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002693 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002694 if (isFocusedStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002695 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002696 }
2697 }
2698
2699 if (allResumedActivitiesIdle()) {
2700 if (r != null) {
2701 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002702 }
2703
2704 if (mLaunchingActivity.isHeld()) {
2705 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2706 if (VALIDATE_WAKE_LOCK_CALLER &&
2707 Binder.getCallingUid() != Process.myUid()) {
2708 throw new IllegalStateException("Calling must be system uid");
2709 }
2710 mLaunchingActivity.release();
2711 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002712 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07002713 }
2714
2715 // Atomically retrieve all of the other things to do.
2716 stops = processStoppingActivitiesLocked(true);
2717 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002718 if ((NF = mFinishingActivities.size()) > 0) {
2719 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002720 mFinishingActivities.clear();
2721 }
2722
Craig Mautnerf3333272013-04-22 10:55:53 -07002723 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002724 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07002725 mStartingUsers.clear();
2726 }
2727
Craig Mautnerf3333272013-04-22 10:55:53 -07002728 // Stop any activities that are scheduled to do so but have been
2729 // waiting for the next one to start.
2730 for (int i = 0; i < NS; i++) {
2731 r = stops.get(i);
2732 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002733 if (stack != null) {
2734 if (r.finishing) {
2735 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2736 } else {
2737 stack.stopActivityLocked(r);
2738 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002739 }
2740 }
2741
2742 // Finish any activities that are scheduled to do so but have been
2743 // waiting for the next one to start.
2744 for (int i = 0; i < NF; i++) {
2745 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07002746 final ActivityStack stack = r.task.stack;
2747 if (stack != null) {
2748 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
2749 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002750 }
2751
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002752 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002753 // Complete user switch
2754 if (startingUsers != null) {
2755 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002756 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002757 }
2758 }
2759 // Complete starting up of background users
2760 if (mStartingBackgroundUsers.size() > 0) {
Amith Yamasani37a40c22015-06-17 13:25:42 -07002761 startingUsers = new ArrayList<UserState>(mStartingBackgroundUsers);
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002762 mStartingBackgroundUsers.clear();
2763 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002764 mService.mUserController.finishUserBoot(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002765 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002766 }
2767 }
2768
2769 mService.trimApplications();
2770 //dump();
2771 //mWindowManager.dump();
2772
Craig Mautnerf3333272013-04-22 10:55:53 -07002773 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002774 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002775 }
2776
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002777 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002778 }
2779
Craig Mautner8e569572013-10-11 17:36:59 -07002780 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002781 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002782 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2783 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002784 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2785 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2786 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002787 }
Craig Mautner19091252013-10-05 00:03:53 -07002788 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002789 }
2790
2791 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002792 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2793 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002794 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2795 stacks.get(stackNdx).closeSystemDialogsLocked();
2796 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002797 }
2798 }
2799
Craig Mautner93529a42013-10-04 15:03:13 -07002800 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002801 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002802 }
2803
Craig Mautner8d341ef2013-03-26 09:03:27 -07002804 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07002805 * Update the last used stack id for non-current user (current user's last
2806 * used stack is the focused stack)
2807 */
2808 void updateUserStackLocked(int userId, ActivityStack stack) {
2809 if (userId != mCurrentUser) {
2810 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
2811 }
2812 }
2813
2814 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07002815 * @return true if some activity was finished (or would have finished if doit were true).
2816 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07002817 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
2818 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002819 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002820 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2821 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08002822 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002823 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07002824 if (stack.finishDisabledPackageActivitiesLocked(
2825 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002826 didSomething = true;
2827 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002828 }
2829 }
2830 return didSomething;
2831 }
2832
Dianne Hackborna413dc02013-07-12 12:02:55 -07002833 void updatePreviousProcessLocked(ActivityRecord r) {
2834 // Now that this process has stopped, we may want to consider
2835 // it to be the previous app to try to keep around in case
2836 // the user wants to return to it.
2837
2838 // First, found out what is currently the foreground app, so that
2839 // we don't blow away the previous app if this activity is being
2840 // hosted by the process that is actually still the foreground.
2841 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002842 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2843 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002844 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2845 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002846 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002847 if (stack.mResumedActivity != null) {
2848 fgApp = stack.mResumedActivity.app;
2849 } else if (stack.mPausingActivity != null) {
2850 fgApp = stack.mPausingActivity.app;
2851 }
2852 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002853 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002854 }
2855 }
2856
2857 // Now set this one as the previous process, only if that really
2858 // makes sense to.
2859 if (r.app != null && fgApp != null && r.app != fgApp
2860 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002861 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002862 mService.mPreviousProcess = r.app;
2863 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2864 }
2865 }
2866
Craig Mautner05d29032013-05-03 13:40:13 -07002867 boolean resumeTopActivitiesLocked() {
2868 return resumeTopActivitiesLocked(null, null, null);
2869 }
2870
2871 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2872 Bundle targetOptions) {
2873 if (targetStack == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002874 targetStack = mFocusedStack;
Craig Mautner05d29032013-05-03 13:40:13 -07002875 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002876 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002877 boolean result = false;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002878 if (isFocusedStack(targetStack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002879 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2880 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002881
Craig Mautnere0a38842013-12-16 16:14:02 -08002882 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2883 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002884 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2885 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002886 if (stack == targetStack) {
2887 // Already started above.
2888 continue;
2889 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002890 if (isFocusedStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002891 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002892 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002893 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002894 }
Craig Mautner05d29032013-05-03 13:40:13 -07002895 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002896 }
2897
Todd Kennedy539db512014-12-15 09:57:55 -08002898 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002899 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2900 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002901 final int numStacks = stacks.size();
2902 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2903 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08002904 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002905 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002906 }
2907 }
2908
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002909 void finishVoiceTask(IVoiceInteractionSession session) {
2910 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2911 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2912 final int numStacks = stacks.size();
2913 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2914 final ActivityStack stack = stacks.get(stackNdx);
2915 stack.finishVoiceTask(session);
2916 }
2917 }
2918 }
2919
Craig Mautner299f9602015-01-26 09:47:33 -08002920 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002921 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2922 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002923 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002924 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2925 // Caller wants the home activity moved with it. To accomplish this,
2926 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002927 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002928 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07002929 if (task.stack == null) {
2930 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2931 + task + " to front. Stack is null");
2932 return;
2933 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002934
Chong Zhang0fa656b2015-08-31 15:17:21 -07002935 if (task.mResizeable && options != null) {
2936 ActivityOptions opts = new ActivityOptions(options);
2937 if (opts.hasBounds()) {
2938 Rect bounds = opts.getBounds();
2939 task.updateOverrideConfiguration(bounds);
Chong Zhang112eb8c2015-11-02 11:17:00 -08002940 final int stackId = task.getLaunchStackId();
2941 if (stackId != task.stack.mStackId) {
2942 moveTaskToStackUncheckedLocked(task, stackId, ON_TOP, !FORCE_FOCUS, reason);
2943 // moveTaskToStackUncheckedLocked() should already placed the task on top,
2944 // still need moveTaskToFrontLocked() below for any transition settings.
2945 }
2946 // WM resizeTask must be done after the task is moved to the correct stack,
2947 // because Task's setBounds() also updates dim layer's bounds, but that has
2948 // dependency on the stack.
Chong Zhang87b21722015-09-21 15:39:51 -07002949 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig,
2950 false /*relayout*/, false /*forced*/);
Chong Zhang0fa656b2015-08-31 15:17:21 -07002951 }
2952 }
2953
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002954 final ActivityRecord r = task.getTopActivity();
2955 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
2956 r == null ? null : r.appTimeTracker, reason);
2957
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002958 if (DEBUG_STACK) Slog.d(TAG_STACK,
2959 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002960 }
2961
Craig Mautner967212c2013-04-13 21:10:58 -07002962 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07002963 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002964 }
2965
2966 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002967 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2968 if (activityContainer != null) {
2969 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002970 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07002971 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002972 return null;
2973 }
2974 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002975 }
2976
Craig Mautner967212c2013-04-13 21:10:58 -07002977 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07002978 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002979 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2980 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002981 }
2982 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002983 }
2984
Craig Mautner4a1cb222013-12-04 16:14:06 -08002985 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002986 ActivityRecord homeActivity = getHomeActivity();
2987 if (homeActivity != null) {
2988 return homeActivity.appToken;
2989 }
2990 return null;
2991 }
2992
2993 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002994 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002995 }
2996
2997 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002998 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2999 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
3000 final TaskRecord task = tasks.get(taskNdx);
3001 if (task.isHomeTask()) {
3002 final ArrayList<ActivityRecord> activities = task.mActivities;
3003 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3004 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07003005 if (r.isHomeActivity()
3006 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003007 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003008 }
3009 }
3010 }
3011 }
3012 return null;
3013 }
3014
Todd Kennedyca4d8422015-01-15 15:19:22 -08003015 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08003016 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003017 ActivityContainer activityContainer =
3018 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003019 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003020 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
3021 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003022 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003023 return activityContainer;
3024 }
3025
Craig Mautner34b73df2014-01-12 21:11:08 -08003026 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003027 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
3028 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
3029 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003030 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
3031 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003032 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08003033 }
3034 }
3035
Craig Mautner95da1082014-02-24 17:54:35 -08003036 void deleteActivityContainer(IActivityContainer container) {
3037 ActivityContainer activityContainer = (ActivityContainer)container;
3038 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003039 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
3040 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08003041 final int stackId = activityContainer.mStackId;
3042 mActivityContainers.remove(stackId);
3043 mWindowManager.removeStack(stackId);
3044 }
3045 }
3046
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07003047 void resizeStackLocked(int stackId, Rect bounds, boolean preserveWindows,
3048 boolean allowResizeInDockedMode) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08003049 final ActivityStack stack = getStack(stackId);
3050 if (stack == null) {
3051 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
3052 return;
3053 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003054
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07003055 if (!allowResizeInDockedMode
3056 && stackId != DOCKED_STACK_ID && getStack(DOCKED_STACK_ID) != null) {
3057 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
3058 // stack size changing so things don't get out of sync.
3059 return;
3060 }
3061
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003062 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02003063 mWindowManager.deferSurfaceLayout();
3064 try {
3065 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003066
Jorim Jaggic4025202015-10-22 16:43:34 +02003067 mTmpBounds.clear();
3068 mTmpConfigs.clear();
3069 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3070 for (int i = tasks.size() - 1; i >= 0; i--) {
3071 TaskRecord task = tasks.get(i);
3072 if (task.mResizeable) {
3073 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
3074 // For freeform stack we don't adjust the size of the tasks to match that
3075 // of the stack, but we do try to make sure the tasks are still contained
3076 // with the bounds of the stack.
3077 tempRect2.set(task.mBounds);
3078 fitWithinBounds(tempRect2, bounds);
3079 task.updateOverrideConfiguration(tempRect2);
3080 } else {
3081 task.updateOverrideConfiguration(bounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003082 }
3083 }
3084
Jorim Jaggic4025202015-10-22 16:43:34 +02003085 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
3086 mTmpBounds.put(task.taskId, task.mBounds);
3087 }
3088 stack.mFullscreen = mWindowManager.resizeStack(stackId, bounds, mTmpConfigs, mTmpBounds);
3089 if (stack.mStackId == DOCKED_STACK_ID) {
3090 // Dock stack funness...Yay!
3091 if (stack.mFullscreen) {
3092 // The dock stack went fullscreen which is kinda like dismissing it.
3093 // In this case we make all other static stacks fullscreen and move all
3094 // docked stack tasks to the fullscreen stack.
3095 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale3797c222015-10-27 14:21:58 -07003096 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
Jorim Jaggic4025202015-10-22 16:43:34 +02003097 resizeStackLocked(i, null, preserveWindows, true);
3098 }
3099 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003100
Jorim Jaggic4025202015-10-22 16:43:34 +02003101 final int count = tasks.size();
3102 for (int i = 0; i < count; i++) {
3103 moveTaskToStackLocked(tasks.get(i).taskId,
Filip Gruszczynski90186c62015-10-26 14:07:00 -07003104 FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP, FORCE_FOCUS, "resizeStack");
Jorim Jaggic4025202015-10-22 16:43:34 +02003105 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003106
Jorim Jaggic4025202015-10-22 16:43:34 +02003107 // stack shouldn't contain anymore activities, so nothing to resume.
3108 r = null;
3109 } else {
3110 // Docked stacks occupy a dedicated region on screen so the size of all other
3111 // static stacks need to be adjusted so they don't overlap with the docked stack.
3112 // We get the bounds to use from window manager which has been adjusted for any
3113 // screen controls and is also the same for all stacks.
3114 mWindowManager.getStackDockedModeBounds(HOME_STACK_ID, tempRect);
3115
3116 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale3797c222015-10-27 14:21:58 -07003117 if (StackId.isResizeableByDockedStack(i)) {
Jorim Jaggic4025202015-10-22 16:43:34 +02003118 ActivityStack otherStack = getStack(i);
3119 if (otherStack != null) {
3120 resizeStackLocked(i, tempRect, PRESERVE_WINDOWS, true);
3121 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003122 }
3123 }
3124 }
Jorim Jaggic4025202015-10-22 16:43:34 +02003125 // Since we are resizing the stack, all other operations should strive to preserve
3126 // windows.
3127 preserveWindows = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003128 }
Jorim Jaggic4025202015-10-22 16:43:34 +02003129 stack.setBounds(bounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003130
Jorim Jaggic4025202015-10-22 16:43:34 +02003131 if (r != null) {
3132 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0, preserveWindows);
3133 // And we need to make sure at this point that all other activities
3134 // are made visible with the correct configuration.
3135 ensureActivitiesVisibleLocked(r, 0, preserveWindows);
3136 if (!updated) {
3137 resumeTopActivitiesLocked(stack, null, null);
3138 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003139 }
Jorim Jaggic4025202015-10-22 16:43:34 +02003140 } finally {
3141 mWindowManager.continueSurfaceLayout();
3142 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003143 }
3144 }
3145
Chong Zhang6de2ae82015-09-30 18:25:21 -07003146 void resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003147 if (!task.mResizeable) {
3148 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
3149 return;
3150 }
3151
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003152 adjustForMinimalTaskDimensions(task, bounds);
3153
Chong Zhang87b21722015-09-21 15:39:51 -07003154 // If this is a forced resize, let it go through even if the bounds is not changing,
3155 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07003156 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Chong Zhang87b21722015-09-21 15:39:51 -07003157 if (task.mBounds != null && task.mBounds.equals(bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003158 // Nothing to do here...
3159 return;
3160 }
3161
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003162 if (!mWindowManager.isValidTaskId(task.taskId)) {
3163 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07003164 // All we can do for now is update the bounds so it can be used when the task is
3165 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07003166 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003167 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
3168 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07003169 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003170 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003171 return;
3172 }
3173
Chong Zhang6de2ae82015-09-30 18:25:21 -07003174 // Do not move the task to another stack here.
3175 // This method assumes that the task is already placed in the right stack.
3176 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003177
Chong Zhang6de2ae82015-09-30 18:25:21 -07003178 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07003179
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07003180 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07003181 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003182 // way and the activity was kept the way it was. If it's false, it means the activity had
3183 // to be relaunched due to configuration change.
3184 boolean kept = true;
3185 if (overrideConfig != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003186 ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08003187 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003188 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003189 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003190 // All other activities must be made visible with their correct configuration.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003191 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003192 if (!kept) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08003193 resumeTopActivitiesLocked(stack, null, null);
3194 }
3195 }
3196 }
Chong Zhang87b21722015-09-21 15:39:51 -07003197 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003198
3199 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwale60454db2015-01-23 16:05:07 -08003200 }
3201
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003202 private void adjustForMinimalTaskDimensions(TaskRecord task, Rect bounds) {
3203 if (bounds == null) {
3204 return;
3205 }
3206 int minimalSize = task.mMinimalSize == -1 ? mDefaultMinimalSizeOfResizeableTask
3207 : task.mMinimalSize;
3208 final boolean adjustWidth = minimalSize > bounds.width();
3209 final boolean adjustHeight = minimalSize > bounds.height();
3210 if (!(adjustWidth || adjustHeight)) {
3211 return;
3212 }
3213 Rect taskBounds = task.mBounds;
3214 if (adjustWidth) {
3215 if (taskBounds != null && bounds.right == taskBounds.right) {
3216 bounds.left = bounds.right - minimalSize;
3217 } else {
3218 // Either left bounds match, or neither match, or the previous bounds were
3219 // fullscreen and we default to keeping left.
3220 bounds.right = bounds.left + minimalSize;
3221 }
3222 }
3223 if (adjustHeight) {
3224 if (taskBounds != null && bounds.bottom == taskBounds.bottom) {
3225 bounds.top = bounds.bottom - minimalSize;
3226 } else {
3227 // Either top bounds match, or neither match, or the previous bounds were
3228 // fullscreen and we default to keeping top.
3229 bounds.bottom = bounds.top + minimalSize;
3230 }
3231 }
3232 }
3233
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003234 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003235 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3236 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08003237 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003238 }
3239
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003240 ActivityContainer activityContainer = new ActivityContainer(stackId);
3241 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003242 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08003243 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003244 }
3245
3246 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07003247 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003248 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
3249 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07003250 break;
3251 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003252 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003253 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003254 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003255 }
3256
Chong Zhang5dcb2752015-08-18 13:50:26 -07003257 /**
3258 * Restores a recent task to a stack
3259 * @param task The recent task to be restored.
3260 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale3797c222015-10-27 14:21:58 -07003261 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07003262 * @return true if the task has been restored successfully.
3263 */
3264 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
3265 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07003266 stackId = task.getLaunchStackId();
Chong Zhang5dcb2752015-08-18 13:50:26 -07003267 }
Wale Ogunwale706ed792015-08-02 10:29:44 -07003268 if (task.stack != null) {
3269 // Task has already been restored once. See if we need to do anything more
3270 if (task.stack.mStackId == stackId) {
3271 // Nothing else to do since it is already restored in the right stack.
3272 return true;
3273 }
3274 // Remove current stack association, so we can re-associate the task with the
3275 // right stack below.
Wale Ogunwale040b4702015-08-06 18:10:50 -07003276 task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003277 }
3278
3279 ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07003280 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003281
3282 if (stack == null) {
3283 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003284 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3285 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003286 return false;
3287 }
3288
3289 stack.addTask(task, false, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003290 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3291 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003292 final ArrayList<ActivityRecord> activities = task.mActivities;
3293 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003294 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003295 }
3296 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07003297 }
3298
Wale Ogunwale040b4702015-08-06 18:10:50 -07003299 /**
3300 * Moves the specified task record to the input stack id.
3301 * WARNING: This method performs an unchecked/raw move of the task and
3302 * can leave the system in an unstable state if used incorrectly.
3303 * Use {@link #moveTaskToStackLocked} to perform safe task movement
3304 * to a stack.
3305 * @param task Task to move.
3306 * @param stackId Id of stack to move task to.
3307 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07003308 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07003309 * @param reason Reason the task is been moved.
3310 * @return The stack the task was moved to.
3311 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07003312 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07003313 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
3314 final ActivityRecord r = task.getTopActivity();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003315 final boolean wasFocused = isFocusedStack(task.stack) && (topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07003316 final boolean wasResumed = wasFocused && (task.stack.mResumedActivity == r);
3317
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07003318 final boolean resizeable = task.mResizeable;
3319 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
3320 // if a docked stack is created below which will lead to the stack we are moving from and
3321 // its resizeable tasks being resized.
3322 task.mResizeable = false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003323 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07003324 task.mResizeable = resizeable;
Wale Ogunwale040b4702015-08-06 18:10:50 -07003325 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
3326 if (task.stack != null) {
3327 task.stack.removeTask(task, reason, MOVING);
3328 }
3329 stack.addTask(task, toTop, MOVING);
Chong Zhang02898352015-08-21 17:27:14 -07003330
3331 // If the task had focus before (or we're requested to move focus),
3332 // move focus to the new stack.
Wale Ogunwale079a0042015-10-24 11:44:07 -07003333 stack.setFocusAndResumeStateIfNeeded(
3334 r, forceFocus || wasFocused, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07003335
Wale Ogunwale040b4702015-08-06 18:10:50 -07003336 return stack;
3337 }
3338
Filip Gruszczynski90186c62015-10-26 14:07:00 -07003339 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
3340 String reason) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003341 final TaskRecord task = anyTaskForIdLocked(taskId);
3342 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08003343 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003344 return;
3345 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07003346 if (StackId.preserveWindowOnTaskMove(stackId)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07003347 // We are about to relaunch the activity because its configuration changed due to
3348 // being maximized, i.e. size change. The activity will first remove the old window
3349 // and then add a new one. This call will tell window manager about this, so it can
3350 // preserve the old window until the new one is drawn. This prevents having a gap
3351 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07003352 // entrance of the new window to be properly animated.
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07003353 ActivityRecord r = task.getTopActivity();
3354 mWindowManager.setReplacingWindow(r.appToken, true /* animate */);
3355 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003356 final ActivityStack stack =
Filip Gruszczynski90186c62015-10-26 14:07:00 -07003357 moveTaskToStackUncheckedLocked(task, stackId, toTop, forceFocus,
3358 "moveTaskToStack:" + reason);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003359
3360 // Make sure the task has the appropriate bounds/size for the stack it is in.
3361 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Chong Zhang6de2ae82015-09-30 18:25:21 -07003362 resizeTaskLocked(task, stack.mBounds,
3363 RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003364 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID
3365 && task.mBounds == null && task.mLastNonFullscreenBounds != null) {
Chong Zhang6de2ae82015-09-30 18:25:21 -07003366 resizeTaskLocked(task, task.mLastNonFullscreenBounds,
3367 RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale99db1862015-10-23 20:08:22 -07003368 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Chong Zhang6de2ae82015-09-30 18:25:21 -07003369 resizeTaskLocked(task, stack.mBounds,
3370 RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003371 }
3372
Stefan Kuhne54714cd2015-05-12 13:42:18 -07003373 // The task might have already been running and its visibility needs to be synchronized with
3374 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003375 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautner05d29032013-05-03 13:40:13 -07003376 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003377 }
3378
Wale Ogunwale079a0042015-10-24 11:44:07 -07003379 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
3380 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
3381 if (stack == null) {
3382 throw new IllegalArgumentException(
3383 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
3384 }
3385
3386 final ActivityRecord r = stack.topRunningActivityLocked();
3387 if (r == null) {
3388 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
3389 + " in stack=" + stack);
3390 return false;
3391 }
3392
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07003393 if (!r.info.supportsPip) {
3394 Slog.w(TAG,
3395 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Wale Ogunwale079a0042015-10-24 11:44:07 -07003396 + " r=" + r);
3397 return false;
3398 }
3399
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07003400 final TaskRecord task = r.task;
Wale Ogunwale079a0042015-10-24 11:44:07 -07003401 if (task.mActivities.size() == 1) {
3402 // There is only one activity in the task. So, we can just move the task over to the
3403 // pinned stack without re-parenting the activity in a different task.
Filip Gruszczynski90186c62015-10-26 14:07:00 -07003404 moveTaskToStackLocked(task.taskId, PINNED_STACK_ID, ON_TOP, FORCE_FOCUS,
3405 "moveTopActivityToPinnedStack");
Wale Ogunwale079a0042015-10-24 11:44:07 -07003406 } else {
3407 final ActivityStack pinnedStack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
3408 pinnedStack.moveActivityToStack(r);
3409 }
3410
3411 resizeStackLocked(PINNED_STACK_ID, bounds, PRESERVE_WINDOWS, true);
3412
3413 // The task might have already been running and its visibility needs to be synchronized with
3414 // the visibility of the stack / windows.
3415 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
3416 resumeTopActivitiesLocked();
3417 return true;
3418 }
3419
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003420 void positionTaskInStackLocked(int taskId, int stackId, int position) {
3421 final TaskRecord task = anyTaskForIdLocked(taskId);
3422 if (task == null) {
3423 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
3424 return;
3425 }
3426 ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07003427 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003428 mWindowManager.positionTaskInStack(taskId, stackId, position);
3429 final boolean stackChanged = task.stack != null && task.stack != stack;
3430 if (stackChanged) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07003431 task.stack.removeTask(task, "moveTaskToStack", MOVING);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003432 }
3433 stack.positionTask(task, position, stackChanged);
3434 // The task might have already been running and its visibility needs to be synchronized with
3435 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003436 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003437 resumeTopActivitiesLocked();
3438 }
3439
Craig Mautnerac6f8432013-07-17 13:24:59 -07003440 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003441 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003442 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3443 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003444 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3445 final ActivityStack stack = stacks.get(stackNdx);
3446 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003447 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003448 continue;
3449 }
3450 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003451 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
3452 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003453 continue;
3454 }
3455 final ActivityRecord ar = stack.findTaskLocked(r);
3456 if (ar != null) {
3457 return ar;
3458 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003459 }
3460 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003461 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07003462 return null;
3463 }
3464
3465 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003466 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3467 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003468 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3469 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
3470 if (ar != null) {
3471 return ar;
3472 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003473 }
3474 }
3475 return null;
3476 }
3477
Craig Mautner8d341ef2013-03-26 09:03:27 -07003478 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003479 scheduleSleepTimeout();
3480 if (!mGoingToSleep.isHeld()) {
3481 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003482 if (mLaunchingActivity.isHeld()) {
3483 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3484 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003485 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003486 mLaunchingActivity.release();
3487 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003488 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003489 }
Amith Yamasanice15e152013-09-19 12:30:32 -07003490 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003491 }
3492
3493 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003494 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003495
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003496 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003497 final long endTime = System.currentTimeMillis() + timeout;
3498 while (true) {
3499 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003500 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3501 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003502 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3503 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
3504 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003505 }
3506 if (cantShutdown) {
3507 long timeRemaining = endTime - System.currentTimeMillis();
3508 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003509 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003510 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003511 } catch (InterruptedException e) {
3512 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003513 } else {
3514 Slog.w(TAG, "Activity manager shutdown timed out");
3515 timedout = true;
3516 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003517 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003518 } else {
3519 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003520 }
3521 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003522
3523 // Force checkReadyForSleep to complete.
3524 mSleepTimeout = true;
3525 checkReadyForSleepLocked();
3526
Craig Mautner8d341ef2013-03-26 09:03:27 -07003527 return timedout;
3528 }
3529
3530 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003531 removeSleepTimeouts();
3532 if (mGoingToSleep.isHeld()) {
3533 mGoingToSleep.release();
3534 }
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 ActivityStack stack = stacks.get(stackNdx);
3539 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003540 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003541 resumeTopActivitiesLocked();
3542 }
Craig Mautner5314a402013-09-26 12:40:16 -07003543 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003544 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003545 mGoingToSleepActivities.clear();
3546 }
3547
3548 void activitySleptLocked(ActivityRecord r) {
3549 mGoingToSleepActivities.remove(r);
3550 checkReadyForSleepLocked();
3551 }
3552
3553 void checkReadyForSleepLocked() {
3554 if (!mService.isSleepingOrShuttingDown()) {
3555 // Do not care.
3556 return;
3557 }
3558
3559 if (!mSleepTimeout) {
3560 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003561 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3562 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003563 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3564 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3565 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003566 }
3567
3568 if (mStoppingActivities.size() > 0) {
3569 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003570 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003571 + mStoppingActivities.size() + " activities");
3572 scheduleIdleLocked();
3573 dontSleep = true;
3574 }
3575
3576 if (mGoingToSleepActivities.size() > 0) {
3577 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003578 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003579 + mGoingToSleepActivities.size() + " activities");
3580 dontSleep = true;
3581 }
3582
3583 if (dontSleep) {
3584 return;
3585 }
3586 }
3587
Craig Mautnere0a38842013-12-16 16:14:02 -08003588 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3589 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003590 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3591 stacks.get(stackNdx).goToSleep();
3592 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003593 }
3594
3595 removeSleepTimeouts();
3596
3597 if (mGoingToSleep.isHeld()) {
3598 mGoingToSleep.release();
3599 }
3600 if (mService.mShuttingDown) {
3601 mService.notifyAll();
3602 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003603 }
3604
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003605 boolean reportResumedActivityLocked(ActivityRecord r) {
3606 final ActivityStack stack = r.task.stack;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003607 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003608 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003609 }
3610 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003611 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003612 mWindowManager.executeAppTransition();
3613 return true;
3614 }
3615 return false;
3616 }
3617
Craig Mautner8d341ef2013-03-26 09:03:27 -07003618 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003619 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3620 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003621 int stackNdx = stacks.size() - 1;
3622 while (stackNdx >= 0) {
3623 stacks.get(stackNdx).handleAppCrashLocked(app);
3624 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003625 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003626 }
3627 }
3628
Jose Lima4b6c6692014-08-12 17:41:12 -07003629 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003630 final ActivityStack stack = r.task.stack;
3631 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003632 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3633 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003634 return false;
3635 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003636 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003637 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3638 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003639
3640 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003641 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003642 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003643 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003644 return true;
3645 }
3646
3647 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003648 if (visible && top.fullscreen) {
3649 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003650 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3651 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3652 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3653 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003654 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003655 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3656 // Only the activity set as currently visible behind should actively reset its
3657 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003658 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3659 "requestVisibleBehind: returning visible=" + visible
3660 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3661 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003662 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003663 }
3664
Jose Lima4b6c6692014-08-12 17:41:12 -07003665 stack.setVisibleBehindActivity(visible ? r : null);
3666 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003667 // Make the activity immediately above r opaque.
3668 final ActivityRecord next = stack.findNextTranslucentActivity(r);
3669 if (next != null) {
3670 mService.convertFromTranslucent(next.appToken);
3671 }
3672 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003673 if (top.app != null && top.app.thread != null) {
3674 // Notify the top app of the change.
3675 try {
3676 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3677 } catch (RemoteException e) {
3678 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003679 }
3680 return true;
3681 }
3682
Craig Mautnerbb742462014-07-07 15:28:55 -07003683 // Called when WindowManager has finished animating the launchingBehind activity to the back.
3684 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
3685 r.mLaunchTaskBehind = false;
3686 final TaskRecord task = r.task;
3687 task.setLastThumbnail(task.stack.screenshotActivities(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003688 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08003689 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07003690 mWindowManager.setAppVisibility(r.appToken, false);
3691 }
3692
3693 void scheduleLaunchTaskBehindComplete(IBinder token) {
3694 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3695 }
3696
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003697 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3698 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07003699 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08003700 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3701 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003702 final int topStackNdx = stacks.size() - 1;
3703 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3704 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003705 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07003706 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003707 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003708 }
3709
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003710 void invalidateTaskLayers() {
3711 mTaskLayersChanged = true;
3712 }
3713
3714 void rankTaskLayersIfNeeded() {
3715 if (!mTaskLayersChanged) {
3716 return;
3717 }
3718 mTaskLayersChanged = false;
3719 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
3720 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3721 int baseLayer = 0;
3722 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3723 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
3724 }
3725 }
3726 }
3727
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003728 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3729 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3730 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3731 final int topStackNdx = stacks.size() - 1;
3732 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3733 final ActivityStack stack = stacks.get(stackNdx);
3734 stack.clearOtherAppTimeTrackers(except);
3735 }
3736 }
3737 }
3738
Craig Mautner8d341ef2013-03-26 09:03:27 -07003739 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003740 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3741 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003742 final int numStacks = stacks.size();
3743 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3744 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003745 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003746 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003747 }
3748 }
3749
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003750 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3751 // Examine all activities currently running in the process.
3752 TaskRecord firstTask = null;
3753 // Tasks is non-null only if two or more tasks are found.
3754 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003755 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3756 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003757 ActivityRecord r = app.activities.get(i);
3758 // First, if we find an activity that is in the process of being destroyed,
3759 // then we just aren't going to do anything for now; we want things to settle
3760 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003761 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003762 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003763 return;
3764 }
3765 // Don't consider any activies that are currently not in a state where they
3766 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003767 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3768 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003769 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003770 continue;
3771 }
3772 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003773 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003774 + " from " + r);
3775 if (firstTask == null) {
3776 firstTask = r.task;
3777 } else if (firstTask != r.task) {
3778 if (tasks == null) {
3779 tasks = new ArraySet<>();
3780 tasks.add(firstTask);
3781 }
3782 tasks.add(r.task);
3783 }
3784 }
3785 }
3786 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003787 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003788 return;
3789 }
3790 // If we have activities in multiple tasks that are in a position to be destroyed,
3791 // let's iterate through the tasks and release the oldest one.
3792 final int numDisplays = mActivityDisplays.size();
3793 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3794 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3795 // Step through all stacks starting from behind, to hit the oldest things first.
3796 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3797 final ActivityStack stack = stacks.get(stackNdx);
3798 // Try to release activities in this stack; if we manage to, we are done.
3799 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3800 return;
3801 }
3802 }
3803 }
3804 }
3805
Amith Yamasani37a40c22015-06-17 13:25:42 -07003806 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003807 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003808 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003809 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003810
Craig Mautner858d8a62013-04-23 17:08:34 -07003811 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003812 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3813 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003814 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003815 final ActivityStack stack = stacks.get(stackNdx);
3816 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003817 TaskRecord task = stack.topTask();
3818 if (task != null) {
3819 mWindowManager.moveTaskToTop(task.taskId);
3820 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003821 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003822 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003823
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003824 ActivityStack stack = getStack(restoreStackId);
3825 if (stack == null) {
3826 stack = mHomeStack;
3827 }
3828 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003829 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003830 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003831 } else {
3832 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003833 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003834 }
Craig Mautner93529a42013-10-04 15:03:13 -07003835 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003836 }
3837
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003838 /**
3839 * Add background users to send boot completed events to.
3840 * @param userId The user being started in the background
3841 * @param uss The state object for the user.
3842 */
Amith Yamasani37a40c22015-06-17 13:25:42 -07003843 public void startBackgroundUserLocked(int userId, UserState uss) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003844 mStartingBackgroundUsers.add(uss);
3845 }
3846
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003847 /** Checks whether the userid is a profile of the current user. */
3848 boolean isCurrentProfileLocked(int userId) {
3849 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003850 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003851 }
3852
Chong Zhang45c25ce2015-08-10 22:18:26 -07003853 /** Checks whether the activity should be shown for current user. */
3854 boolean okToShowLocked(ActivityRecord r) {
3855 return r != null && (isCurrentProfileLocked(r.userId)
3856 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
3857 }
3858
Craig Mautnerde4ef022013-04-07 19:01:33 -07003859 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003860 ArrayList<ActivityRecord> stops = null;
3861
3862 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003863 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3864 ActivityRecord s = mStoppingActivities.get(activityNdx);
3865 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003866 if (DEBUG_ALL) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003867 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3868 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003869 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003870 if (s.finishing) {
3871 // If this activity is finishing, it is sitting on top of
3872 // everyone else but we now know it is no longer needed...
3873 // so get rid of it. Otherwise, we need to go through the
3874 // normal flow and hide it once we determine that it is
3875 // hidden by the activities in front of it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003876 if (DEBUG_ALL) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003877 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003878 }
3879 }
Craig Mautner8c14c152015-01-15 17:32:07 -08003880 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003881 if (DEBUG_ALL) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003882 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003883 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003884 }
3885 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003886 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003887 }
3888 }
3889
3890 return stops;
3891 }
3892
Craig Mautnercf910b02013-04-23 11:23:27 -07003893 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003894 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3895 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3896 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3897 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003898 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003899 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003900 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003901 if (r == null) Slog.e(TAG,
3902 "validateTop...: null top activity, stack=" + stack);
3903 else {
3904 final ActivityRecord pausing = stack.mPausingActivity;
3905 if (pausing != null && pausing == r) Slog.e(TAG,
3906 "validateTop...: top stack has pausing activity r=" + r
3907 + " state=" + state);
3908 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3909 "validateTop...: activity in front not resumed r=" + r
3910 + " state=" + state);
3911 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003912 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003913 final ActivityRecord resumed = stack.mResumedActivity;
3914 if (resumed != null && resumed == r) Slog.e(TAG,
3915 "validateTop...: back stack has resumed activity r=" + r
3916 + " state=" + state);
3917 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3918 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003919 }
3920 }
3921 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003922 }
3923
Craig Mautnere0570202015-05-13 13:06:11 -07003924 private String lockTaskModeToString() {
3925 switch (mLockTaskModeState) {
3926 case LOCK_TASK_MODE_LOCKED:
3927 return "LOCKED";
3928 case LOCK_TASK_MODE_PINNED:
3929 return "PINNED";
3930 case LOCK_TASK_MODE_NONE:
3931 return "NONE";
3932 default: return "unknown=" + mLockTaskModeState;
3933 }
3934 }
3935
Craig Mautner27084302013-03-25 08:05:25 -07003936 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003937 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003938 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003939 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
3940 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
3941 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003942 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003943 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
3944 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3945 if (packages.size() > 0) {
3946 pw.println(" mLockTaskPackages (userId:packages)=");
3947 for (int i = 0; i < packages.size(); ++i) {
3948 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3949 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3950 }
3951 }
3952 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003953 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003954
Craig Mautner20e72272013-04-01 13:45:53 -07003955 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003956 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003957 }
3958
Dianne Hackborn390517b2013-05-30 15:03:32 -07003959 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3960 boolean needSep, String prefix) {
3961 if (activity != null) {
3962 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3963 if (needSep) {
3964 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003965 }
3966 pw.print(prefix);
3967 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003968 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003969 }
3970 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003971 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003972 }
3973
Craig Mautner8d341ef2013-03-26 09:03:27 -07003974 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3975 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003976 boolean printed = false;
3977 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003978 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3979 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003980 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003981 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003982 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003983 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003984 final ActivityStack stack = stacks.get(stackNdx);
3985 StringBuilder stackHeader = new StringBuilder(128);
3986 stackHeader.append(" Stack #");
3987 stackHeader.append(stack.mStackId);
3988 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003989 stackHeader.append("\n");
3990 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3991 stackHeader.append("\n");
3992 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003993 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3994 needSep, stackHeader.toString());
3995 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3996 !dumpAll, false, dumpPackage, true,
3997 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003998
Craig Mautner4a1cb222013-12-04 16:14:06 -08003999 needSep = printed;
4000 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
4001 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004002 if (pr) {
4003 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004004 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004005 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004006 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
4007 " mResumedActivity: ");
4008 if (pr) {
4009 printed = true;
4010 needSep = false;
4011 }
4012 if (dumpAll) {
4013 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
4014 " mLastPausedActivity: ");
4015 if (pr) {
4016 printed = true;
4017 needSep = true;
4018 }
4019 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
4020 needSep, " mLastNoHistoryActivity: ");
4021 }
4022 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004023 }
4024 }
4025
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004026 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
4027 false, dumpPackage, true, " Activities waiting to finish:", null);
4028 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
4029 false, dumpPackage, true, " Activities waiting to stop:", null);
4030 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
4031 false, dumpPackage, true, " Activities waiting for another to become visible:",
4032 null);
4033 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
4034 false, dumpPackage, true, " Activities waiting to sleep:", null);
4035 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
4036 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004037
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004038 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004039 }
4040
Dianne Hackborn390517b2013-05-30 15:03:32 -07004041 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07004042 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004043 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07004044 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07004045 String innerPrefix = null;
4046 String[] args = null;
4047 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004048 for (int i=list.size()-1; i>=0; i--) {
4049 final ActivityRecord r = list.get(i);
4050 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
4051 continue;
4052 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004053 if (innerPrefix == null) {
4054 innerPrefix = prefix + " ";
4055 args = new String[0];
4056 }
4057 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004058 final boolean full = !brief && (complete || !r.isInHistory());
4059 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004060 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07004061 needNL = false;
4062 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004063 if (header1 != null) {
4064 pw.println(header1);
4065 header1 = null;
4066 }
4067 if (header2 != null) {
4068 pw.println(header2);
4069 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07004070 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07004071 if (lastTask != r.task) {
4072 lastTask = r.task;
4073 pw.print(prefix);
4074 pw.print(full ? "* " : " ");
4075 pw.println(lastTask);
4076 if (full) {
4077 lastTask.dump(pw, prefix + " ");
4078 } else if (complete) {
4079 // Complete + brief == give a summary. Isn't that obvious?!?
4080 if (lastTask.intent != null) {
4081 pw.print(prefix); pw.print(" ");
4082 pw.println(lastTask.intent.toInsecureStringWithClip());
4083 }
4084 }
4085 }
4086 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
4087 pw.print(" #"); pw.print(i); pw.print(": ");
4088 pw.println(r);
4089 if (full) {
4090 r.dump(pw, innerPrefix);
4091 } else if (complete) {
4092 // Complete + brief == give a summary. Isn't that obvious?!?
4093 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
4094 if (r.app != null) {
4095 pw.print(innerPrefix); pw.println(r.app);
4096 }
4097 }
4098 if (client && r.app != null && r.app.thread != null) {
4099 // flush anything that is already in the PrintWriter since the thread is going
4100 // to write to the file descriptor directly
4101 pw.flush();
4102 try {
4103 TransferPipe tp = new TransferPipe();
4104 try {
4105 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
4106 r.appToken, innerPrefix, args);
4107 // Short timeout, since blocking here can
4108 // deadlock with the application.
4109 tp.go(fd, 2000);
4110 } finally {
4111 tp.kill();
4112 }
4113 } catch (IOException e) {
4114 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
4115 } catch (RemoteException e) {
4116 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
4117 }
4118 needNL = true;
4119 }
4120 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004121 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004122 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004123
Craig Mautnerf3333272013-04-22 10:55:53 -07004124 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004125 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4126 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07004127 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
4128 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07004129 }
4130
4131 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07004132 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07004133 }
4134
4135 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004136 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
4137 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07004138 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
4139 }
4140
Craig Mautner05d29032013-05-03 13:40:13 -07004141 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08004142 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
4143 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
4144 }
Craig Mautner05d29032013-05-03 13:40:13 -07004145 }
4146
Craig Mautner0eea92c2013-05-16 13:35:39 -07004147 void removeSleepTimeouts() {
4148 mSleepTimeout = false;
4149 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
4150 }
4151
4152 final void scheduleSleepTimeout() {
4153 removeSleepTimeouts();
4154 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
4155 }
4156
Craig Mautner4a1cb222013-12-04 16:14:06 -08004157 @Override
4158 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004159 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004160 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
4161 }
4162
4163 @Override
4164 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004165 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004166 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
4167 }
4168
4169 @Override
4170 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004171 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004172 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
4173 }
4174
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004175 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08004176 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004177 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08004178 newDisplay = mActivityDisplays.get(displayId) == null;
4179 if (newDisplay) {
4180 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07004181 if (activityDisplay.mDisplay == null) {
4182 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
4183 return;
4184 }
Craig Mautner4504de52013-12-20 09:06:56 -08004185 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004186 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08004187 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004188 }
Craig Mautner4504de52013-12-20 09:06:56 -08004189 if (newDisplay) {
4190 mWindowManager.onDisplayAdded(displayId);
4191 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004192 }
4193
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004194 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
4195 if (display.mDisplayId != Display.DEFAULT_DISPLAY) {
4196 return;
4197 }
4198 final float fraction = mService.mContext.getResources().getFraction(com.android.internal.R.
4199 fraction.config_displayFractionForDefaultMinimalSizeOfResizeableTask, 1, 1);
4200 mDefaultMinimalSizeOfResizeableTask = (int) (fraction * Math.min(
4201 display.mDisplayInfo.logicalWidth, display.mDisplayInfo.logicalHeight));
4202 }
4203
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004204 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004205 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004206 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4207 if (activityDisplay != null) {
4208 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004209 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004210 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004211 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004212 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004213 }
4214 }
4215 mWindowManager.onDisplayRemoved(displayId);
4216 }
4217
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004218 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004219 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004220 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4221 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004222 // TODO: Update the bounds.
4223 }
4224 }
4225 mWindowManager.onDisplayChanged(displayId);
4226 }
4227
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004228 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004229 StackInfo info = new StackInfo();
4230 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
4231 info.displayId = Display.DEFAULT_DISPLAY;
4232 info.stackId = stack.mStackId;
4233
4234 ArrayList<TaskRecord> tasks = stack.getAllTasks();
4235 final int numTasks = tasks.size();
4236 int[] taskIds = new int[numTasks];
4237 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004238 Rect[] taskBounds = new Rect[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08004239 for (int i = 0; i < numTasks; ++i) {
4240 final TaskRecord task = tasks.get(i);
4241 taskIds[i] = task.taskId;
4242 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
4243 : task.realActivity != null ? task.realActivity.flattenToString()
4244 : task.getTopActivity() != null ? task.getTopActivity().packageName
4245 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004246 taskBounds[i] = new Rect();
4247 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004248 }
4249 info.taskIds = taskIds;
4250 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07004251 info.taskBounds = taskBounds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004252 return info;
4253 }
4254
4255 StackInfo getStackInfoLocked(int stackId) {
4256 ActivityStack stack = getStack(stackId);
4257 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004258 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004259 }
4260 return null;
4261 }
4262
4263 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004264 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08004265 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
4266 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004267 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004268 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08004269 }
4270 }
4271 return list;
4272 }
4273
Craig Mautner15df08a2015-04-01 12:17:18 -07004274 TaskRecord getLockedTaskLocked() {
4275 final int top = mLockTaskModeTasks.size() - 1;
4276 if (top >= 0) {
4277 return mLockTaskModeTasks.get(top);
4278 }
4279 return null;
4280 }
4281
4282 boolean isLockedTask(TaskRecord task) {
4283 return mLockTaskModeTasks.contains(task);
4284 }
4285
4286 boolean isLastLockedTask(TaskRecord task) {
4287 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
4288 }
4289
4290 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07004291 if (!mLockTaskModeTasks.remove(task)) {
4292 return;
4293 }
4294 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
4295 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004296 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07004297 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
4298 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07004299 final Message lockTaskMsg = Message.obtain();
4300 lockTaskMsg.arg1 = task.userId;
4301 lockTaskMsg.what = LOCK_TASK_END_MSG;
4302 mHandler.sendMessage(lockTaskMsg);
4303 }
4304 }
4305
Craig Mautner6cd6cec2015-04-01 00:02:12 -07004306 void showLockTaskToast() {
4307 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04004308 }
4309
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004310 void showLockTaskEscapeMessageLocked(TaskRecord task) {
4311 if (mLockTaskModeTasks.contains(task)) {
4312 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
4313 }
4314 }
4315
Craig Mautner432f64e2015-05-20 14:59:57 -07004316 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
4317 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004318 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07004319 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07004320 final TaskRecord lockedTask = getLockedTaskLocked();
4321 if (lockedTask != null) {
4322 removeLockedTaskLocked(lockedTask);
4323 if (!mLockTaskModeTasks.isEmpty()) {
4324 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07004325 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4326 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07004327 lockedTask.performClearTaskLocked();
4328 resumeTopActivitiesLocked();
4329 return;
4330 }
Christopher Tate3bd90612014-06-18 16:37:52 -07004331 }
Craig Mautnere0570202015-05-13 13:06:11 -07004332 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4333 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08004334 return;
4335 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004336
4337 // Should have already been checked, but do it again.
4338 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07004339 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4340 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08004341 return;
4342 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004343 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07004344 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07004345 return;
4346 }
4347
4348 if (mLockTaskModeTasks.isEmpty()) {
4349 // First locktask.
4350 final Message lockTaskMsg = Message.obtain();
4351 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
4352 lockTaskMsg.arg1 = task.userId;
4353 lockTaskMsg.what = LOCK_TASK_START_MSG;
4354 lockTaskMsg.arg2 = lockTaskModeState;
4355 mHandler.sendMessage(lockTaskMsg);
4356 }
4357 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07004358 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
4359 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07004360 mLockTaskModeTasks.remove(task);
4361 mLockTaskModeTasks.add(task);
4362
4363 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07004364 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07004365 }
Craig Mautner432f64e2015-05-20 14:59:57 -07004366
4367 if (andResume) {
4368 findTaskToMoveToFrontLocked(task, 0, null, reason);
4369 resumeTopActivitiesLocked();
4370 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004371 }
4372
4373 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04004374 return isLockTaskModeViolation(task, false);
4375 }
4376
4377 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
4378 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004379 return false;
4380 }
4381 final int lockTaskAuth = task.mLockTaskAuth;
4382 switch (lockTaskAuth) {
4383 case LOCK_TASK_AUTH_DONT_LOCK:
4384 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01004385 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07004386 case LOCK_TASK_AUTH_LAUNCHABLE:
4387 case LOCK_TASK_AUTH_WHITELISTED:
4388 return false;
4389 case LOCK_TASK_AUTH_PINNABLE:
4390 // Pinnable tasks can't be launched on top of locktask tasks.
4391 return !mLockTaskModeTasks.isEmpty();
4392 default:
4393 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
4394 return true;
4395 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004396 }
4397
Craig Mautner15df08a2015-04-01 12:17:18 -07004398 void onLockTaskPackagesUpdatedLocked() {
4399 boolean didSomething = false;
4400 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
4401 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01004402 final boolean wasWhitelisted =
4403 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4404 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07004405 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01004406 final boolean isWhitelisted =
4407 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4408 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
4409 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004410 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07004411 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
4412 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07004413 removeLockedTaskLocked(lockedTask);
4414 lockedTask.performClearTaskLocked();
4415 didSomething = true;
4416 }
4417 }
4418 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4419 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4420 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4421 final ActivityStack stack = stacks.get(stackNdx);
4422 stack.onLockTaskPackagesUpdatedLocked();
4423 }
4424 }
Craig Mautnere0570202015-05-13 13:06:11 -07004425 final ActivityRecord r = topRunningActivityLocked();
4426 final TaskRecord task = r != null ? r.task : null;
4427 if (mLockTaskModeTasks.isEmpty() && task != null
4428 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4429 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004430 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4431 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4432 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4433 false);
4434 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004435 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004436 if (didSomething) {
4437 resumeTopActivitiesLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004438 }
4439 }
4440
Benjamin Franz43261142015-02-11 15:59:44 +00004441 int getLockTaskModeState() {
4442 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004443 }
4444
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004445 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004446
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004447 public ActivityStackSupervisorHandler(Looper looper) {
4448 super(looper);
4449 }
4450
Craig Mautnerf3333272013-04-22 10:55:53 -07004451 void activityIdleInternal(ActivityRecord r) {
4452 synchronized (mService) {
4453 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
4454 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004455 }
4456
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004457 @Override
4458 public void handleMessage(Message msg) {
4459 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004460 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004461 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4462 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004463 if (mService.mDidDexOpt) {
4464 mService.mDidDexOpt = false;
4465 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4466 nmsg.obj = msg.obj;
4467 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4468 return;
4469 }
4470 // We don't at this point know if the activity is fullscreen,
4471 // so we need to be conservative and assume it isn't.
4472 activityIdleInternal((ActivityRecord)msg.obj);
4473 } break;
4474 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004475 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004476 activityIdleInternal((ActivityRecord)msg.obj);
4477 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004478 case RESUME_TOP_ACTIVITY_MSG: {
4479 synchronized (mService) {
4480 resumeTopActivitiesLocked();
4481 }
4482 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004483 case SLEEP_TIMEOUT_MSG: {
4484 synchronized (mService) {
4485 if (mService.isSleepingOrShuttingDown()) {
4486 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4487 mSleepTimeout = true;
4488 checkReadyForSleepLocked();
4489 }
4490 }
4491 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004492 case LAUNCH_TIMEOUT_MSG: {
4493 if (mService.mDidDexOpt) {
4494 mService.mDidDexOpt = false;
4495 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4496 return;
4497 }
4498 synchronized (mService) {
4499 if (mLaunchingActivity.isHeld()) {
4500 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4501 if (VALIDATE_WAKE_LOCK_CALLER
4502 && Binder.getCallingUid() != Process.myUid()) {
4503 throw new IllegalStateException("Calling must be system uid");
4504 }
4505 mLaunchingActivity.release();
4506 }
4507 }
4508 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004509 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004510 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004511 } break;
4512 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004513 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004514 } break;
4515 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004516 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004517 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004518 case CONTAINER_CALLBACK_VISIBILITY: {
4519 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004520 final IActivityContainerCallback callback = container.mCallback;
4521 if (callback != null) {
4522 try {
4523 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4524 } catch (RemoteException e) {
4525 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004526 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004527 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004528 case LOCK_TASK_START_MSG: {
4529 // When lock task starts, we disable the status bars.
4530 try {
Jason Monk62515be2014-05-21 16:06:19 -04004531 if (mLockTaskNotify == null) {
4532 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004533 }
Jason Monk62515be2014-05-21 16:06:19 -04004534 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004535 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004536 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004537 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004538 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004539 flags = StatusBarManager.DISABLE_MASK
4540 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004541 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004542 flags = StatusBarManager.DISABLE_MASK
4543 & (~StatusBarManager.DISABLE_BACK)
4544 & (~StatusBarManager.DISABLE_HOME)
4545 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004546 }
4547 getStatusBarService().disable(flags, mToken,
4548 mService.mContext.getPackageName());
4549 }
4550 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004551 if (getDevicePolicyManager() != null) {
4552 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004553 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004554 }
justinzhang5286d3f2014-05-12 17:06:01 -04004555 } catch (RemoteException ex) {
4556 throw new RuntimeException(ex);
4557 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004558 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004559 case LOCK_TASK_END_MSG: {
4560 // When lock task ends, we enable the status bars.
4561 try {
Jason Monk62515be2014-05-21 16:06:19 -04004562 if (getStatusBarService() != null) {
4563 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4564 mService.mContext.getPackageName());
4565 }
4566 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004567 if (getDevicePolicyManager() != null) {
4568 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4569 msg.arg1);
4570 }
Jason Monk62515be2014-05-21 16:06:19 -04004571 if (mLockTaskNotify == null) {
4572 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4573 }
4574 mLockTaskNotify.show(false);
4575 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004576 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004577 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004578 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004579 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004580 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04004581 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04004582 new LockPatternUtils(mService.mContext)
4583 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004584 }
4585 } catch (SettingNotFoundException e) {
4586 // No setting, don't lock.
4587 }
justinzhang5286d3f2014-05-12 17:06:01 -04004588 } catch (RemoteException ex) {
4589 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004590 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004591 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004592 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004593 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004594 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4595 if (mLockTaskNotify == null) {
4596 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4597 }
4598 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4599 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004600 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4601 final ActivityContainer container = (ActivityContainer) msg.obj;
4602 final IActivityContainerCallback callback = container.mCallback;
4603 if (callback != null) {
4604 try {
4605 callback.onAllActivitiesComplete(container.asBinder());
4606 } catch (RemoteException e) {
4607 }
4608 }
4609 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004610 case LAUNCH_TASK_BEHIND_COMPLETE: {
4611 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004612 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004613 if (r != null) {
4614 handleLaunchTaskBehindCompleteLocked(r);
4615 }
4616 }
4617 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004618 }
4619 }
4620 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004621
Ying Wangb081a592014-04-22 15:20:16 -07004622 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07004623 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07004624 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004625 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004626 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004627 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004628 ActivityRecord mParentActivity = null;
4629 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004630
Craig Mautnere3a00d72014-04-16 08:31:19 -07004631 boolean mVisible = true;
4632
Craig Mautner4a1cb222013-12-04 16:14:06 -08004633 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004634 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004635
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004636 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4637 final static int CONTAINER_STATE_NO_SURFACE = 1;
4638 final static int CONTAINER_STATE_FINISHING = 2;
4639 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4640
4641 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004642 synchronized (mService) {
4643 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004644 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004645 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004646 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004647 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004648 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004649
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004650 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004651 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004652 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08004653 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004654 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004655 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004656 }
4657
4658 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004659 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004660 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004661 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4662 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004663 return;
4664 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004665 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004666 }
4667 }
4668
4669 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004670 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004671 synchronized (mService) {
4672 if (mActivityDisplay != null) {
4673 return mActivityDisplay.mDisplayId;
4674 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004675 }
4676 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004677 }
4678
Jeff Brownca9bc702014-02-11 14:32:56 -08004679 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004680 public int getStackId() {
4681 synchronized (mService) {
4682 return mStackId;
4683 }
4684 }
4685
4686 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004687 public boolean injectEvent(InputEvent event) {
4688 final long origId = Binder.clearCallingIdentity();
4689 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004690 synchronized (mService) {
4691 if (mActivityDisplay != null) {
4692 return mInputManagerInternal.injectInputEvent(event,
4693 mActivityDisplay.mDisplayId,
4694 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4695 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004696 }
4697 return false;
4698 } finally {
4699 Binder.restoreCallingIdentity(origId);
4700 }
4701 }
4702
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004703 @Override
4704 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004705 synchronized (mService) {
4706 if (mContainerState == CONTAINER_STATE_FINISHING) {
4707 return;
4708 }
4709 mContainerState = CONTAINER_STATE_FINISHING;
4710
Craig Mautnerd163e752014-06-13 17:18:47 -07004711 long origId = Binder.clearCallingIdentity();
4712 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004713 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07004714 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004715 } finally {
4716 Binder.restoreCallingIdentity(origId);
4717 }
4718 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004719 }
4720
Craig Mautner60257702014-09-17 15:02:33 -07004721 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004722 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004723 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004724 if (mActivityDisplay != null) {
4725 mActivityDisplay.detachActivitiesLocked(mStack);
4726 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004727 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004728 }
4729 }
4730
4731 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004732 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004733 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004734 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004735 Binder.getCallingUid(), mCurrentUser, false,
4736 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004737
Craig Mautnere0a38842013-12-16 16:14:02 -08004738 // TODO: Switch to user app stacks here.
4739 String mimeType = intent.getType();
Craig Mautnerb9168362015-02-26 20:40:19 -08004740 final Uri data = intent.getData();
4741 if (mimeType == null && data != null && "content".equals(data.getScheme())) {
4742 mimeType = mService.getProviderMimeType(data, userId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004743 }
Craig Mautnerb9168362015-02-26 20:40:19 -08004744 checkEmbeddedAllowedInner(userId, intent, mimeType);
4745
4746 intent.addFlags(FORCE_NEW_TASK_FLAGS);
4747 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07004748 0, 0, null, null, null, null, false, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004749 }
4750
4751 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004752 public final int startActivityIntentSender(IIntentSender intentSender)
4753 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004754 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4755
4756 if (!(intentSender instanceof PendingIntentRecord)) {
4757 throw new IllegalArgumentException("Bad PendingIntent object");
4758 }
4759
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004760 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004761 Binder.getCallingUid(), mCurrentUser, false,
4762 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004763
4764 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4765 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4766 pendingIntent.key.requestResolvedType);
4767
4768 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4769 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4770 }
4771
4772 private void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004773 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004774 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004775 throw new SecurityException(
4776 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4777 }
4778 }
4779
Craig Mautnerdf88d732014-01-27 09:21:32 -08004780 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004781 public IBinder asBinder() {
4782 return this;
4783 }
4784
Craig Mautner4504de52013-12-20 09:06:56 -08004785 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004786 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004787 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004788 }
4789
Craig Mautner4a1cb222013-12-04 16:14:06 -08004790 ActivityStackSupervisor getOuter() {
4791 return ActivityStackSupervisor.this;
4792 }
4793
Craig Mautnerd163e752014-06-13 17:18:47 -07004794 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004795 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004796 }
4797
Craig Mautner6985bad2014-04-21 15:22:06 -07004798 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004799 void setVisible(boolean visible) {
4800 if (mVisible != visible) {
4801 mVisible = visible;
4802 if (mCallback != null) {
4803 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4804 0 /* unused */, this).sendToTarget();
4805 }
4806 }
4807 }
4808
Craig Mautner6985bad2014-04-21 15:22:06 -07004809 void setDrawn() {
4810 }
4811
Craig Mautner1b4bf852014-05-26 15:06:32 -07004812 // You can always start a new task on a regular ActivityStack.
4813 boolean isEligibleForNewTasks() {
4814 return true;
4815 }
4816
Craig Mautnerd163e752014-06-13 17:18:47 -07004817 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004818 detachLocked();
4819 deleteActivityContainer(this);
4820 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004821 }
4822
Craig Mautner34b73df2014-01-12 21:11:08 -08004823 @Override
4824 public String toString() {
4825 return mIdString + (mActivityDisplay == null ? "N" : "A");
4826 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004827 }
4828
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004829 private class VirtualActivityContainer extends ActivityContainer {
4830 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004831 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004832
4833 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4834 super(getNextStackId());
4835 mParentActivity = parent;
4836 mCallback = callback;
4837 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004838 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004839 }
4840
4841 @Override
4842 public void setSurface(Surface surface, int width, int height, int density) {
4843 super.setSurface(surface, width, height, density);
4844
4845 synchronized (mService) {
4846 final long origId = Binder.clearCallingIdentity();
4847 try {
4848 setSurfaceLocked(surface, width, height, density);
4849 } finally {
4850 Binder.restoreCallingIdentity(origId);
4851 }
4852 }
4853 }
4854
4855 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4856 if (mContainerState == CONTAINER_STATE_FINISHING) {
4857 return;
4858 }
4859 VirtualActivityDisplay virtualActivityDisplay =
4860 (VirtualActivityDisplay) mActivityDisplay;
4861 if (virtualActivityDisplay == null) {
4862 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004863 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004864 mActivityDisplay = virtualActivityDisplay;
4865 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004866 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004867 }
4868
4869 if (mSurface != null) {
4870 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004871 }
4872
Craig Mautner6985bad2014-04-21 15:22:06 -07004873 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004874 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004875 mStack.resumeTopActivityLocked(null);
4876 } else {
4877 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004878 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004879 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004880 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004881 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004882 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004883
Craig Mautnerd163e752014-06-13 17:18:47 -07004884 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004885
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004886 if (DEBUG_STACK) Slog.d(TAG_STACK,
4887 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004888 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004889
Craig Mautner6985bad2014-04-21 15:22:06 -07004890 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004891 boolean isAttachedLocked() {
4892 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004893 }
4894
4895 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004896 void setDrawn() {
4897 synchronized (mService) {
4898 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004899 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004900 }
4901 }
4902
Craig Mautner1b4bf852014-05-26 15:06:32 -07004903 // Never start a new task on an ActivityView if it isn't explicitly specified.
4904 @Override
4905 boolean isEligibleForNewTasks() {
4906 return false;
4907 }
4908
Craig Mautnerd163e752014-06-13 17:18:47 -07004909 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004910 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004911 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4912 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4913 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4914 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4915 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004916 }
4917 }
4918
Craig Mautner4a1cb222013-12-04 16:14:06 -08004919 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4920 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004921 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004922 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004923 int mDisplayId;
4924 Display mDisplay;
4925 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004926
Craig Mautner4a1cb222013-12-04 16:14:06 -08004927 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4928 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08004929 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004930
Jose Lima4b6c6692014-08-12 17:41:12 -07004931 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004932
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004933 ActivityDisplay() {
4934 }
Craig Mautner4504de52013-12-20 09:06:56 -08004935
Craig Mautner1a70a162014-09-13 12:09:31 -07004936 // After instantiation, check that mDisplay is not null before using this. The alternative
4937 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004938 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004939 final Display display = mDisplayManager.getDisplay(displayId);
4940 if (display == null) {
4941 return;
4942 }
4943 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004944 }
4945
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004946 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004947 mDisplay = display;
4948 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004949 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004950 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004951
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004952 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004953 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004954 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
4955 + " onTop=" + onTop);
4956 if (onTop) {
4957 mStacks.add(stack);
4958 } else {
4959 mStacks.add(0, stack);
4960 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004961 }
4962
Craig Mautnere0a38842013-12-16 16:14:02 -08004963 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004964 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004965 + " from displayId=" + mDisplayId);
4966 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004967 }
4968
Jose Lima4b6c6692014-08-12 17:41:12 -07004969 void setVisibleBehindActivity(ActivityRecord r) {
4970 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004971 }
4972
Jose Lima4b6c6692014-08-12 17:41:12 -07004973 boolean hasVisibleBehindActivity() {
4974 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004975 }
4976
Craig Mautner34b73df2014-01-12 21:11:08 -08004977 @Override
4978 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004979 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4980 }
4981 }
4982
4983 class VirtualActivityDisplay extends ActivityDisplay {
4984 VirtualDisplay mVirtualDisplay;
4985
Craig Mautner6985bad2014-04-21 15:22:06 -07004986 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004987 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004988 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4989 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4990 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4991 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004992
4993 init(mVirtualDisplay.getDisplay());
4994
4995 mWindowManager.handleDisplayAdded(mDisplayId);
4996 }
4997
4998 void setSurface(Surface surface) {
4999 if (mVirtualDisplay != null) {
5000 mVirtualDisplay.setSurface(surface);
5001 }
5002 }
5003
5004 @Override
5005 void detachActivitiesLocked(ActivityStack stack) {
5006 super.detachActivitiesLocked(stack);
5007 if (mVirtualDisplay != null) {
5008 mVirtualDisplay.release();
5009 mVirtualDisplay = null;
5010 }
5011 }
5012
5013 @Override
5014 public String toString() {
5015 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08005016 }
Craig Mautnered6649f2013-12-02 14:08:25 -08005017 }
Jose Lima58e66d62014-05-27 20:00:27 -07005018
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07005019 /**
5020 * Adjust bounds to stay within stack bounds.
5021 *
5022 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
5023 * that keep them unchanged, but be contained within the stack bounds.
5024 *
5025 * @param bounds Bounds to be adjusted.
5026 * @param stackBounds Bounds within which the other bounds should remain.
5027 */
5028 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
5029 if (stackBounds == null || stackBounds.contains(bounds)) {
5030 return;
5031 }
5032
5033 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
5034 final int maxRight = stackBounds.right
5035 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
5036 int horizontalDiff = stackBounds.left - bounds.left;
5037 if ((horizontalDiff < 0 && bounds.left >= maxRight)
5038 || (bounds.left + horizontalDiff >= maxRight)) {
5039 horizontalDiff = maxRight - bounds.left;
5040 }
5041 bounds.left += horizontalDiff;
5042 bounds.right += horizontalDiff;
5043 }
5044
5045 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
5046 final int maxBottom = stackBounds.bottom
5047 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
5048 int verticalDiff = stackBounds.top - bounds.top;
5049 if ((verticalDiff < 0 && bounds.top >= maxBottom)
5050 || (bounds.top + verticalDiff >= maxBottom)) {
5051 verticalDiff = maxBottom - bounds.top;
5052 }
5053 bounds.top += verticalDiff;
5054 bounds.bottom += verticalDiff;
5055 }
5056 }
5057
Craig Mautner27084302013-03-25 08:05:25 -07005058}