blob: 5503f5216a767e81bec53ac12fd04cf044c3c97f [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 Ogunwaled80c2632015-03-13 10:26:26 -070021import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK;
22import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
Craig Mautner29219d92013-04-16 20:19:12 -070023import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
24import static android.content.Intent.FLAG_ACTIVITY_TASK_ON_HOME;
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -070025import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Craig Mautner6170f732013-04-02 13:05:23 -070026import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Wale Ogunwaled57969f2014-11-15 19:37:29 -080027import static com.android.server.am.ActivityManagerDebugConfig.*;
Craig Mautner05d29032013-05-03 13:40:13 -070028import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Craig Mautner84984fa2014-06-19 11:19:20 -070029import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
30import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
31import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -070032import static com.android.server.am.ActivityStack.ActivityState.*;
Craig Mautner15df08a2015-04-01 12:17:18 -070033import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
34import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
Benjamin Franz469dd582015-06-09 14:24:36 +010035import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
Craig Mautner15df08a2015-04-01 12:17:18 -070036import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
37import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Craig Mautner8d341ef2013-03-26 09:03:27 -070038
Svetoslav7008b512015-06-24 18:47:07 -070039import android.Manifest;
Craig Mautner2420ead2013-04-01 17:13:20 -070040import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070041import android.app.ActivityManager;
Craig Mautnered6649f2013-12-02 14:08:25 -080042import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070043import android.app.ActivityOptions;
44import android.app.AppGlobals;
Svetoslav7008b512015-06-24 18:47:07 -070045import android.app.AppOpsManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -080046import android.app.IActivityContainer;
47import android.app.IActivityContainerCallback;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070048import android.app.IActivityManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070049import android.app.IApplicationThread;
Craig Mautner23ac33b2013-04-01 16:26:35 -070050import android.app.PendingIntent;
Jeff Hao1b012d32014-08-20 10:35:34 -070051import android.app.ProfilerInfo;
Craig Mautner20e72272013-04-01 13:45:53 -070052import android.app.ActivityManager.RunningTaskInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070053import android.app.IActivityManager.WaitResult;
Craig Mautner2420ead2013-04-01 17:13:20 -070054import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -040055import android.app.StatusBarManager;
Jason Monk35c62a42014-06-17 10:24:47 -040056import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070057import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -070058import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -070059import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070060import android.content.Intent;
Craig Mautner23ac33b2013-04-01 16:26:35 -070061import android.content.IntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070062import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070063import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -070064import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070065import android.content.pm.PackageManager;
66import android.content.pm.ResolveInfo;
67import android.content.res.Configuration;
Craig Mautner4a1cb222013-12-04 16:14:06 -080068import android.graphics.Point;
Wale Ogunwale60454db2015-01-23 16:05:07 -080069import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -080070import android.hardware.display.DisplayManager;
71import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080072import android.hardware.display.DisplayManagerGlobal;
73import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080074import android.hardware.input.InputManager;
75import android.hardware.input.InputManagerInternal;
Craig Mautnerb9168362015-02-26 20:40:19 -080076import android.net.Uri;
Craig Mautner23ac33b2013-04-01 16:26:35 -070077import android.os.Binder;
Craig Mautner8d341ef2013-03-26 09:03:27 -070078import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070079import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070080import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070081import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070082import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070083import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070084import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070085import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070086import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070087import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -040088import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070089import android.os.SystemClock;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -070090import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -070091import android.os.UserHandle;
Dianne Hackborn3d07c942015-03-13 18:02:54 -070092import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -070093import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -040094import android.provider.Settings;
95import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -070096import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -070097import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070098import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -070099import android.util.EventLog;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700100import android.util.IntArray;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700101import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800102import android.util.SparseArray;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700103
Craig Mautner4a1cb222013-12-04 16:14:06 -0800104import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800105import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800106import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -0800107import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800108import android.view.Surface;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700109import com.android.internal.app.HeavyWeightSwitcherActivity;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700110import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800111import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700112import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400113import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -0700114import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -0400115import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800116import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700117import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700118import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700119
justinzhang5286d3f2014-05-12 17:06:01 -0400120
Craig Mautner8d341ef2013-03-26 09:03:27 -0700121import java.io.FileDescriptor;
122import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700123import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700124import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700125import java.util.Arrays;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700126import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700127import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700128
Craig Mautner4a1cb222013-12-04 16:14:06 -0800129public final class ActivityStackSupervisor implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800130 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700131 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700132 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700133 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700134 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700135 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700136 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700137 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700138 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
139 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700140 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700141 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700142 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
143 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700144 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700145 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800146
Craig Mautnerf3333272013-04-22 10:55:53 -0700147 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700148 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700149
Craig Mautner0eea92c2013-05-16 13:35:39 -0700150 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700151 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700152
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700153 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700154 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700155
Craig Mautner05d29032013-05-03 13:40:13 -0700156 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
157 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
158 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700159 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700160 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800161 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
162 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
163 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700164 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400165 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
166 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700167 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700168 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700169 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800170
Wale Ogunwale040b4702015-08-06 18:10:50 -0700171 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700172
Jason Monk62515be2014-05-21 16:06:19 -0400173 private static final String LOCK_TASK_TAG = "Lock-to-App";
174
Wale Ogunwale040b4702015-08-06 18:10:50 -0700175 // Used to indicate if an object (e.g. stack) that we are trying to get
176 // should be created if it doesn't exist already.
177 private static final boolean CREATE_IF_NEEDED = true;
178
179 // Used to indicate if an object (e.g. task) should be moved/created
180 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700181 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700182
183 // Used to indicate that an objects (e.g. task) removal from its container
184 // (e.g. stack) is due to it moving to another container.
185 static final boolean MOVING = true;
186
Svetoslav7008b512015-06-24 18:47:07 -0700187 // Activity actions an app cannot start if it uses a permission which is not granted.
188 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
189 new ArrayMap<>();
190 static {
191 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
192 Manifest.permission.CAMERA);
193 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
194 Manifest.permission.CAMERA);
195 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
196 Manifest.permission.CALL_PHONE);
197 }
198
Svet Ganov99b60432015-06-27 13:15:22 -0700199 /** Action restriction: launching the activity is not restricted. */
200 private static final int ACTIVITY_RESTRICTION_NONE = 0;
201 /** Action restriction: launching the activity is restricted by a permission. */
202 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
203 /** Action restriction: launching the activity is restricted by an app op. */
204 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700205
justinzhang5286d3f2014-05-12 17:06:01 -0400206 /** Status Bar Service **/
207 private IBinder mToken = new Binder();
208 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400209 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400210
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700211 // For debugging to make sure the caller when acquiring/releasing our
212 // wake lock is the system process.
213 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700214
Craig Mautner27084302013-03-25 08:05:25 -0700215 final ActivityManagerService mService;
216
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800217 private final RecentTasks mRecentTasks;
218
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700219 final ActivityStackSupervisorHandler mHandler;
220
221 /** Short cut */
222 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800223 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700224
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700225 /** Counter for next free stack ID to use for dynamic activity stacks. */
226 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700227
228 /** Task identifier that activities are currently being started in. Incremented each time a
229 * new task is created. */
230 private int mCurTaskId = 0;
231
Craig Mautner2420ead2013-04-01 17:13:20 -0700232 /** The current user */
233 private int mCurrentUser;
234
Craig Mautnere0a38842013-12-16 16:14:02 -0800235 /** The stack containing the launcher app. Assumed to always be attached to
236 * Display.DEFAULT_DISPLAY. */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700237 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700238
Craig Mautnere0a38842013-12-16 16:14:02 -0800239 /** The stack currently receiving input or launching the next activity. */
Craig Mautner29219d92013-04-16 20:19:12 -0700240 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700241
Craig Mautner4a1cb222013-12-04 16:14:06 -0800242 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
243 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800244 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800245 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700246
247 /** List of activities that are waiting for a new activity to become visible before completing
248 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800249 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700250
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700251 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800252 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700253
254 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800255 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700256
Craig Mautnerde4ef022013-04-07 19:01:33 -0700257 /** List of activities that are ready to be stopped, but waiting for the next activity to
258 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800259 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700260
Craig Mautnerf3333272013-04-22 10:55:53 -0700261 /** List of activities that are ready to be finished, but waiting for the previous activity to
262 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800263 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700264
Craig Mautner0eea92c2013-05-16 13:35:39 -0700265 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800266 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700267
Craig Mautnerf3333272013-04-22 10:55:53 -0700268 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700269 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700270
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700271 /** Used to queue up any background users being started */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700272 final ArrayList<UserState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700273
Craig Mautnerde4ef022013-04-07 19:01:33 -0700274 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
275 * is being brought in front of us. */
276 boolean mUserLeaving = false;
277
Craig Mautner0eea92c2013-05-16 13:35:39 -0700278 /** Set when we have taken too long waiting to go to sleep. */
279 boolean mSleepTimeout = false;
280
Jose Lima58e66d62014-05-27 20:00:27 -0700281 /** Indicates if we are running on a Leanback-only (TV) device. Only initialized after
282 * setWindowManager is called. **/
283 private boolean mLeanbackOnlyDevice;
284
Craig Mautner0eea92c2013-05-16 13:35:39 -0700285 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700286 * We don't want to allow the device to go to sleep while in the process
287 * of launching an activity. This is primarily to allow alarm intent
288 * receivers to launch an activity and get that to run before the device
289 * goes back to sleep.
290 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700291 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700292
293 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700294 * Set when the system is going to sleep, until we have
295 * successfully paused the current activity and released our wake lock.
296 * At that point the system is allowed to actually sleep.
297 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700298 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700299
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700300 /** Stack id of the front stack when user switched, indexed by userId. */
301 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700302
Craig Mautner4504de52013-12-20 09:06:56 -0800303 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800304 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700305 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800306
307 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700308 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800309
Jeff Brownca9bc702014-02-11 14:32:56 -0800310 InputManagerInternal mInputManagerInternal;
311
Craig Mautner15df08a2015-04-01 12:17:18 -0700312 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
313 * may be finished until there is only one entry left. If this is empty the system is not
314 * in lockTask mode. */
315 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000316 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700317 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
318 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000319 */
320 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400321 /**
322 * Notifies the user when entering/exiting lock-task.
323 */
324 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800325
Craig Mautner15df08a2015-04-01 12:17:18 -0700326 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches = new ArrayList<>();
Craig Mautneree36c772014-07-16 14:56:05 -0700327
Craig Mautner42d04db2014-11-06 12:13:23 -0800328 /** Used to keep resumeTopActivityLocked() from being entered recursively */
329 boolean inResumeTopActivity;
330
Craig Mautneree36c772014-07-16 14:56:05 -0700331 /**
332 * Description of a request to start a new activity, which has been held
333 * due to app switches being disabled.
334 */
335 static class PendingActivityLaunch {
336 final ActivityRecord r;
337 final ActivityRecord sourceRecord;
338 final int startFlags;
339 final ActivityStack stack;
340
341 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
342 int _startFlags, ActivityStack _stack) {
343 r = _r;
344 sourceRecord = _sourceRecord;
345 startFlags = _startFlags;
346 stack = _stack;
347 }
348 }
349
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800350 public ActivityStackSupervisor(ActivityManagerService service, RecentTasks recentTasks) {
Craig Mautner27084302013-03-25 08:05:25 -0700351 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800352 mRecentTasks = recentTasks;
Jeff Brown2c43c332014-06-12 22:38:59 -0700353 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
354 }
355
356 /**
357 * At the time when the constructor runs, the power manager has not yet been
358 * initialized. So we initialize our wakelocks afterwards.
359 */
360 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800361 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700362 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700363 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700364 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700365 }
366
justinzhang5286d3f2014-05-12 17:06:01 -0400367 // This function returns a IStatusBarService. The value is from ServiceManager.
368 // getService and is cached.
369 private IStatusBarService getStatusBarService() {
370 synchronized (mService) {
371 if (mStatusBarService == null) {
372 mStatusBarService = IStatusBarService.Stub.asInterface(
373 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
374 if (mStatusBarService == null) {
375 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
376 }
377 }
378 return mStatusBarService;
379 }
380 }
381
Jason Monk35c62a42014-06-17 10:24:47 -0400382 private IDevicePolicyManager getDevicePolicyManager() {
383 synchronized (mService) {
384 if (mDevicePolicyManager == null) {
385 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
386 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
387 if (mDevicePolicyManager == null) {
388 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
389 }
390 }
391 return mDevicePolicyManager;
392 }
393 }
394
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700395 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800396 synchronized (mService) {
397 mWindowManager = wm;
398
399 mDisplayManager =
400 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
401 mDisplayManager.registerDisplayListener(this, null);
402
403 Display[] displays = mDisplayManager.getDisplays();
404 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
405 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800406 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700407 if (activityDisplay.mDisplay == null) {
408 throw new IllegalStateException("Default Display does not exist");
409 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800410 mActivityDisplays.put(displayId, activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800411 }
412
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700413 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800414 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800415
416 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Jose Lima58e66d62014-05-27 20:00:27 -0700417
418 // Initialize this here, now that we can get a valid reference to PackageManager.
419 mLeanbackOnlyDevice = isLeanbackOnlyDevice();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800420 }
Craig Mautner27084302013-03-25 08:05:25 -0700421 }
422
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200423 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700424 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200425 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700426 }
427
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700428 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800429 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700430 }
431
Craig Mautnerde4ef022013-04-07 19:01:33 -0700432 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800433 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700434 }
435
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800436 /** Top of all visible stacks is/should always be equal to the focused stack.
437 * Use {@link ActivityStack#isStackVisibleLocked} to determine if a specific
438 * stack is visible or not. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700439 boolean isFrontStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700440 if (stack == null) {
441 return false;
442 }
443
Craig Mautnerdf88d732014-01-27 09:21:32 -0800444 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
445 if (parent != null) {
446 stack = parent.task.stack;
447 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800448 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700449 }
450
Craig Mautner299f9602015-01-26 09:47:33 -0800451 void moveHomeStack(boolean toFront, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800452 moveHomeStack(toFront, reason, null);
453 }
454
455 void moveHomeStack(boolean toFront, String reason, ActivityStack lastFocusedStack) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800456 ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautnerde313752015-01-22 14:28:03 -0800457 final int topNdx = stacks.size() - 1;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800458 if (topNdx <= 0) {
459 return;
460 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700461
462 // The home stack should either be at the top or bottom of the stack list.
463 if ((toFront && (stacks.get(topNdx) != mHomeStack))
464 || (!toFront && (stacks.get(0) != mHomeStack))) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700465 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveHomeTask: topStack old="
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700466 + ((lastFocusedStack != null) ? lastFocusedStack : stacks.get(topNdx))
467 + " new=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800468 stacks.remove(mHomeStack);
469 stacks.add(toFront ? topNdx : 0, mHomeStack);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700470 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800471
472 if (lastFocusedStack != null) {
473 mLastFocusedStack = lastFocusedStack;
474 }
475 mFocusedStack = stacks.get(topNdx);
476
Craig Mautnerde313752015-01-22 14:28:03 -0800477 EventLog.writeEvent(EventLogTags.AM_HOME_STACK_MOVED,
478 mCurrentUser, toFront ? 1 : 0, stacks.get(topNdx).getStackId(),
Craig Mautner299f9602015-01-26 09:47:33 -0800479 mFocusedStack == null ? -1 : mFocusedStack.getStackId(), reason);
Craig Mautnerde313752015-01-22 14:28:03 -0800480
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800481 if (mService.mBooting || !mService.mBooted) {
482 final ActivityRecord r = topRunningActivityLocked();
483 if (r != null && r.idle) {
484 checkFinishBootingLocked();
485 }
486 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700487 }
488
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700489 /** Returns true if the focus activity was adjusted to the home stack top activity. */
490 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700491 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
492 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700493 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700494 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700495
Craig Mautner84984fa2014-06-19 11:19:20 -0700496 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700497
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700498 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700499 if (top == null) {
500 return false;
501 }
502 mService.setFocusedActivityLocked(top, reason);
503 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700504 }
505
Craig Mautner299f9602015-01-26 09:47:33 -0800506 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700507 if (!mService.mBooting && !mService.mBooted) {
508 // Not ready yet!
509 return false;
510 }
511
Craig Mautner84984fa2014-06-19 11:19:20 -0700512 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
513 mWindowManager.showRecentApps();
514 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700515 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700516
Craig Mautner84984fa2014-06-19 11:19:20 -0700517 if (prev != null) {
518 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
519 }
520
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700521 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
522 ActivityRecord r = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700523 if (r != null) {
Craig Mautner299f9602015-01-26 09:47:33 -0800524 mService.setFocusedActivityLocked(r, reason);
Craig Mautner05d29032013-05-03 13:40:13 -0700525 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700526 }
Craig Mautner299f9602015-01-26 09:47:33 -0800527 return mService.startHomeActivityLocked(mCurrentUser, reason);
Craig Mautner69ada552013-04-18 13:51:51 -0700528 }
529
Craig Mautner8d341ef2013-03-26 09:03:27 -0700530 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700531 return anyTaskForIdLocked(id, true);
532 }
533
534 /**
535 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
536 * @param id Id of the task we would like returned.
537 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
538 * restore the task from recents to the active list.
539 */
540 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800541 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800542 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800543 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800544 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
545 ActivityStack stack = stacks.get(stackNdx);
546 TaskRecord task = stack.taskForIdLocked(id);
547 if (task != null) {
548 return task;
549 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700550 }
551 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800552
553 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700554 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800555 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800556 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700557 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800558 return null;
559 }
560
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700561 if (!restoreFromRecents) {
562 return task;
563 }
564
Chong Zhang5dcb2752015-08-18 13:50:26 -0700565 if (!restoreRecentTaskLocked(task, INVALID_STACK_ID)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700566 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
567 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800568 return null;
569 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700570 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800571 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700572 }
573
Craig Mautner6170f732013-04-02 13:05:23 -0700574 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800575 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800576 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800577 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800578 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
579 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
580 if (r != null) {
581 return r;
582 }
Craig Mautner6170f732013-04-02 13:05:23 -0700583 }
584 }
585 return null;
586 }
587
Craig Mautneref73ee12014-04-23 11:45:37 -0700588 void setNextTaskId(int taskId) {
589 if (taskId > mCurTaskId) {
590 mCurTaskId = taskId;
591 }
592 }
593
Craig Mautner8d341ef2013-03-26 09:03:27 -0700594 int getNextTaskId() {
595 do {
596 mCurTaskId++;
597 if (mCurTaskId <= 0) {
598 mCurTaskId = 1;
599 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700600 } while (anyTaskForIdLocked(mCurTaskId, false) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700601 return mCurTaskId;
602 }
603
Craig Mautnerde4ef022013-04-07 19:01:33 -0700604 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800605 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700606 if (stack == null) {
607 return null;
608 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700609 ActivityRecord resumedActivity = stack.mResumedActivity;
610 if (resumedActivity == null || resumedActivity.app == null) {
611 resumedActivity = stack.mPausingActivity;
612 if (resumedActivity == null || resumedActivity.app == null) {
613 resumedActivity = stack.topRunningActivityLocked(null);
614 }
615 }
616 return resumedActivity;
617 }
618
Dianne Hackbornff072722014-09-24 10:56:28 -0700619 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700620 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800621 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800622 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
623 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800624 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
625 final ActivityStack stack = stacks.get(stackNdx);
626 if (!isFrontStack(stack)) {
627 continue;
628 }
629 ActivityRecord hr = stack.topRunningActivityLocked(null);
630 if (hr != null) {
631 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
632 && processName.equals(hr.processName)) {
633 try {
George Mount2c92c972014-03-20 09:38:23 -0700634 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800635 didSomething = true;
636 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700637 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800638 Slog.w(TAG, "Exception in new application when starting activity "
639 + hr.intent.getComponent().flattenToShortString(), e);
640 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700641 }
Craig Mautner20e72272013-04-01 13:45:53 -0700642 }
Craig Mautner20e72272013-04-01 13:45:53 -0700643 }
644 }
645 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700646 if (!didSomething) {
647 ensureActivitiesVisibleLocked(null, 0);
648 }
Craig Mautner20e72272013-04-01 13:45:53 -0700649 return didSomething;
650 }
651
652 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800653 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
654 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800655 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
656 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner34b73df2014-01-12 21:11:08 -0800657 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800658 continue;
659 }
660 final ActivityRecord resumedActivity = stack.mResumedActivity;
661 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700662 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800663 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800664 return false;
665 }
Craig Mautner20e72272013-04-01 13:45:53 -0700666 }
667 }
668 return true;
669 }
670
Craig Mautnerde4ef022013-04-07 19:01:33 -0700671 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800672 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
673 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800674 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
675 final ActivityStack stack = stacks.get(stackNdx);
676 if (isFrontStack(stack)) {
677 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700678 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800679 return false;
680 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700681 }
682 }
683 }
684 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700685 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800686 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
687 mLastFocusedStack + " to=" + mFocusedStack);
688 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700689 return true;
690 }
691
692 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800693 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800694 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
695 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800696 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
697 final ActivityStack stack = stacks.get(stackNdx);
698 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800699 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700700 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800701 return false;
702 }
703 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800704 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700705 }
706 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800707 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700708 }
709
Craig Mautner2acc3892013-09-23 10:28:14 -0700710 /**
711 * Pause all activities in either all of the stacks or just the back stacks.
712 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700713 * @return true if any activity was paused as a result of this call.
714 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700715 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700716 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800717 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
718 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800719 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
720 final ActivityStack stack = stacks.get(stackNdx);
721 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700722 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800723 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700724 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
725 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800726 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700727 }
728 }
729 return someActivityPaused;
730 }
731
Craig Mautnerde4ef022013-04-07 19:01:33 -0700732 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700733 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800734 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
735 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800736 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
737 final ActivityStack stack = stacks.get(stackNdx);
738 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700739 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800740 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700741 Slog.d(TAG_STATES,
742 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800743 pausing = false;
744 } else {
745 return false;
746 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700747 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700748 }
749 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700750 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700751 }
752
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700753 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
754 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500755 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800756 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
757 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
758 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
759 final ActivityStack stack = stacks.get(stackNdx);
760 if (stack.mResumedActivity != null &&
761 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700762 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800763 }
764 }
765 }
766 }
767
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700768 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700769 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700770 }
771
772 void sendWaitingVisibleReportLocked(ActivityRecord r) {
773 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700774 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700775 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700776 if (w.who == null) {
777 changed = true;
778 w.timeout = false;
779 if (r != null) {
780 w.who = new ComponentName(r.info.packageName, r.info.name);
781 }
782 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
783 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700784 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700785 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700786 if (changed) {
787 mService.notifyAll();
788 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700789 }
790
791 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
792 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700793 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700794 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700795 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700796 if (w.who == null) {
797 changed = true;
798 w.timeout = timeout;
799 if (r != null) {
800 w.who = new ComponentName(r.info.packageName, r.info.name);
801 }
802 w.thisTime = thisTime;
803 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700804 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700805 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700806 if (changed) {
807 mService.notifyAll();
808 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700809 }
810
Craig Mautner29219d92013-04-16 20:19:12 -0700811 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800812 final ActivityStack focusedStack = mFocusedStack;
Craig Mautner1602ec22013-05-12 10:24:27 -0700813 ActivityRecord r = focusedStack.topRunningActivityLocked(null);
814 if (r != null) {
815 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700816 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700817
Craig Mautner4a1cb222013-12-04 16:14:06 -0800818 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800819 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800820 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
821 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700822 if (stack != focusedStack && isFrontStack(stack)) {
Craig Mautner29219d92013-04-16 20:19:12 -0700823 r = stack.topRunningActivityLocked(null);
824 if (r != null) {
825 return r;
826 }
827 }
828 }
829 return null;
830 }
831
Dianne Hackborn09233282014-04-30 11:33:59 -0700832 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700833 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800834 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
835 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800836 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800837 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800838 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800839 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
840 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700841 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800842 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700843 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700844 }
845 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700846
847 // The lists are already sorted from most recent to oldest. Just pull the most recent off
848 // each list and add it to list. Stop when all lists are empty or maxNum reached.
849 while (maxNum > 0) {
850 long mostRecentActiveTime = Long.MIN_VALUE;
851 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800852 final int numTaskLists = runningTaskLists.size();
853 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
854 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700855 if (!stackTaskList.isEmpty()) {
856 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
857 if (lastActiveTime > mostRecentActiveTime) {
858 mostRecentActiveTime = lastActiveTime;
859 selectedStackList = stackTaskList;
860 }
861 }
862 }
863 if (selectedStackList != null) {
864 list.add(selectedStackList.remove(0));
865 --maxNum;
866 } else {
867 break;
868 }
869 }
Craig Mautner20e72272013-04-01 13:45:53 -0700870 }
871
Craig Mautner23ac33b2013-04-01 16:26:35 -0700872 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700873 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700874 // Collect information about the target of the Intent.
875 ActivityInfo aInfo;
876 try {
877 ResolveInfo rInfo =
878 AppGlobals.getPackageManager().resolveIntent(
879 intent, resolvedType,
880 PackageManager.MATCH_DEFAULT_ONLY
881 | ActivityManagerService.STOCK_PM_FLAGS, userId);
882 aInfo = rInfo != null ? rInfo.activityInfo : null;
883 } catch (RemoteException e) {
884 aInfo = null;
885 }
886
887 if (aInfo != null) {
888 // Store the found target back into the intent, because now that
889 // we have it we never want to do this again. For example, if the
890 // user navigates back to this point in the history, we should
891 // always restart the exact same activity.
892 intent.setComponent(new ComponentName(
893 aInfo.applicationInfo.packageName, aInfo.name));
894
895 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -0700896 if (!aInfo.processName.equals("system")) {
897 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700898 mService.setDebugApp(aInfo.processName, true, false);
899 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700900
Man Caocfa78b22015-06-11 20:14:34 -0700901 if ((startFlags & ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700902 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
903 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700904
Man Caocfa78b22015-06-11 20:14:34 -0700905 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
906 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
907 }
908
909 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700910 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700911 }
912 }
913 }
914 return aInfo;
915 }
916
Craig Mautner299f9602015-01-26 09:47:33 -0800917 void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
918 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700919 startActivityLocked(null /* caller */, intent, null /* resolvedType */, aInfo,
920 null /* voiceSession */, null /* voiceInteractor */, null /* resultTo */,
921 null /* resultWho */, 0 /* requestCode */, 0 /* callingPid */, 0 /* callingUid */,
922 null /* callingPackage */, 0 /* realCallingPid */, 0 /* realCallingUid */,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700923 0 /* startFlags */, null /* options */, false /* ignoreTargetSecurity */,
924 false /* componentSpecified */,
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700925 null /* outActivity */, null /* container */, null /* inTask */);
926 if (inResumeTopActivity) {
927 // If we are in resume section already, home activity will be initialized, but not
928 // resumed (to avoid recursive resume) and will stay that way until something pokes it
929 // again. We need to schedule another resume.
930 scheduleResumeTopActivities();
931 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700932 }
933
Craig Mautner23ac33b2013-04-01 16:26:35 -0700934 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700935 String callingPackage, Intent intent, String resolvedType,
936 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700937 IBinder resultTo, String resultWho, int requestCode, int startFlags,
938 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700939 Bundle options, boolean ignoreTargetSecurity, int userId,
940 IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700941 // Refuse possible leaked file descriptors
942 if (intent != null && intent.hasFileDescriptors()) {
943 throw new IllegalArgumentException("File descriptors passed in Intent");
944 }
945 boolean componentSpecified = intent.getComponent() != null;
946
947 // Don't modify the client's object!
948 intent = new Intent(intent);
949
950 // Collect information about the target of the Intent.
Craig Mautner15df08a2015-04-01 12:17:18 -0700951 ActivityInfo aInfo =
952 resolveActivity(intent, resolvedType, startFlags, profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700953
Craig Mautnere0a38842013-12-16 16:14:02 -0800954 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700955 synchronized (mService) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800956 if (container != null && container.mParentActivity != null &&
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700957 container.mParentActivity.state != RESUMED) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800958 // Cannot start a child activity if the parent is not resumed.
959 return ActivityManager.START_CANCELED;
960 }
Dianne Hackborn95465202014-09-15 16:21:55 -0700961 final int realCallingPid = Binder.getCallingPid();
962 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -0700963 int callingPid;
964 if (callingUid >= 0) {
965 callingPid = -1;
966 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700967 callingPid = realCallingPid;
968 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700969 } else {
970 callingPid = callingUid = -1;
971 }
972
Craig Mautnere0a38842013-12-16 16:14:02 -0800973 final ActivityStack stack;
974 if (container == null || container.mStack.isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800975 stack = mFocusedStack;
Craig Mautnere0a38842013-12-16 16:14:02 -0800976 } else {
977 stack = container.mStack;
978 }
Wale Ogunwale60454db2015-01-23 16:05:07 -0800979 stack.mConfigWillChange = config != null && mService.mConfiguration.diff(config) != 0;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700980 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700981 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700982
983 final long origId = Binder.clearCallingIdentity();
984
985 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800986 (aInfo.applicationInfo.privateFlags
987 &ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700988 // This may be a heavy-weight process! Check to see if we already
989 // have another, different heavy-weight process running.
990 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
991 if (mService.mHeavyWeightProcess != null &&
992 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
993 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700994 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700995 if (caller != null) {
996 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
997 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700998 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700999 } else {
1000 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -07001001 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -07001002 + intent.toString());
1003 ActivityOptions.abort(options);
1004 return ActivityManager.START_PERMISSION_DENIED;
1005 }
1006 }
1007
1008 IIntentSender target = mService.getIntentSenderLocked(
1009 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -07001010 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -07001011 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
1012 | PendingIntent.FLAG_ONE_SHOT, null);
1013
1014 Intent newIntent = new Intent();
1015 if (requestCode >= 0) {
1016 // Caller is requesting a result.
1017 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
1018 }
1019 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
1020 new IntentSender(target));
1021 if (mService.mHeavyWeightProcess.activities.size() > 0) {
1022 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
1023 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
1024 hist.packageName);
1025 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
1026 hist.task.taskId);
1027 }
1028 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
1029 aInfo.packageName);
1030 newIntent.setFlags(intent.getFlags());
1031 newIntent.setClassName("android",
1032 HeavyWeightSwitcherActivity.class.getName());
1033 intent = newIntent;
1034 resolvedType = null;
1035 caller = null;
1036 callingUid = Binder.getCallingUid();
1037 callingPid = Binder.getCallingPid();
1038 componentSpecified = true;
1039 try {
1040 ResolveInfo rInfo =
1041 AppGlobals.getPackageManager().resolveIntent(
1042 intent, null,
1043 PackageManager.MATCH_DEFAULT_ONLY
1044 | ActivityManagerService.STOCK_PM_FLAGS, userId);
1045 aInfo = rInfo != null ? rInfo.activityInfo : null;
1046 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1047 } catch (RemoteException e) {
1048 aInfo = null;
1049 }
1050 }
1051 }
1052 }
1053
Dianne Hackborn91097de2014-04-04 18:02:06 -07001054 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
1055 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -07001056 requestCode, callingPid, callingUid, callingPackage,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001057 realCallingPid, realCallingUid, startFlags, options, ignoreTargetSecurity,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001058 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001059
Craig Mautner85c11a82014-07-17 12:38:18 -07001060 Binder.restoreCallingIdentity(origId);
1061
Craig Mautnerde4ef022013-04-07 19:01:33 -07001062 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001063 // If the caller also wants to switch to a new configuration,
1064 // do so now. This allows a clean switch, as we are waiting
1065 // for the current activity to pause (so we will not destroy
1066 // it), and have not yet started the next activity.
1067 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
1068 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -07001069 stack.mConfigWillChange = false;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001070 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautner23ac33b2013-04-01 16:26:35 -07001071 "Updating to new configuration after starting activity.");
Maxim Bogatov05075302015-05-19 18:33:08 -07001072 mService.updateConfigurationLocked(config, null, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001073 }
1074
Craig Mautner23ac33b2013-04-01 16:26:35 -07001075 if (outResult != null) {
1076 outResult.result = res;
1077 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001078 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001079 do {
1080 try {
1081 mService.wait();
1082 } catch (InterruptedException e) {
1083 }
1084 } while (!outResult.timeout && outResult.who == null);
1085 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001086 ActivityRecord r = stack.topRunningActivityLocked(null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001087 if (r.nowVisible && r.state == RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001088 outResult.timeout = false;
1089 outResult.who = new ComponentName(r.info.packageName, r.info.name);
1090 outResult.totalTime = 0;
1091 outResult.thisTime = 0;
1092 } else {
1093 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001094 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001095 do {
1096 try {
1097 mService.wait();
1098 } catch (InterruptedException e) {
1099 }
1100 } while (!outResult.timeout && outResult.who == null);
1101 }
1102 }
1103 }
1104
1105 return res;
1106 }
1107 }
1108
1109 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1110 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
1111 Bundle options, int userId) {
1112 if (intents == null) {
1113 throw new NullPointerException("intents is null");
1114 }
1115 if (resolvedTypes == null) {
1116 throw new NullPointerException("resolvedTypes is null");
1117 }
1118 if (intents.length != resolvedTypes.length) {
1119 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1120 }
1121
Craig Mautner23ac33b2013-04-01 16:26:35 -07001122
1123 int callingPid;
1124 if (callingUid >= 0) {
1125 callingPid = -1;
1126 } else if (caller == null) {
1127 callingPid = Binder.getCallingPid();
1128 callingUid = Binder.getCallingUid();
1129 } else {
1130 callingPid = callingUid = -1;
1131 }
1132 final long origId = Binder.clearCallingIdentity();
1133 try {
1134 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001135 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001136 for (int i=0; i<intents.length; i++) {
1137 Intent intent = intents[i];
1138 if (intent == null) {
1139 continue;
1140 }
1141
1142 // Refuse possible leaked file descriptors
1143 if (intent != null && intent.hasFileDescriptors()) {
1144 throw new IllegalArgumentException("File descriptors passed in Intent");
1145 }
1146
1147 boolean componentSpecified = intent.getComponent() != null;
1148
1149 // Don't modify the client's object!
1150 intent = new Intent(intent);
1151
1152 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001153 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001154 // TODO: New, check if this is correct
1155 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1156
1157 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001158 (aInfo.applicationInfo.privateFlags
1159 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001160 throw new IllegalArgumentException(
1161 "FLAG_CANT_SAVE_STATE not supported here");
1162 }
1163
1164 Bundle theseOptions;
1165 if (options != null && i == intents.length-1) {
1166 theseOptions = options;
1167 } else {
1168 theseOptions = null;
1169 }
Craig Mautner6170f732013-04-02 13:05:23 -07001170 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001171 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1172 callingPackage, callingPid, callingUid,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001173 0, theseOptions, false, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001174 if (res < 0) {
1175 return res;
1176 }
1177
1178 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1179 }
1180 }
1181 } finally {
1182 Binder.restoreCallingIdentity(origId);
1183 }
1184
1185 return ActivityManager.START_SUCCESS;
1186 }
1187
Craig Mautner2420ead2013-04-01 17:13:20 -07001188 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001189 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001190 throws RemoteException {
1191
Craig Mautner2568c3a2015-03-26 14:22:34 -07001192 if (andResume) {
1193 r.startFreezingScreenLocked(app, 0);
1194 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001195
Craig Mautner2568c3a2015-03-26 14:22:34 -07001196 // schedule launch ticks to collect information about slow apps.
1197 r.startLaunchTickingLocked();
1198 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001199
1200 // Have the window manager re-evaluate the orientation of
1201 // the screen based on the new activity order. Note that
1202 // as a result of this, it can call back into the activity
1203 // manager with a new orientation. We don't care about that,
1204 // because the activity is not currently running so we are
1205 // just restarting it anyway.
1206 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001207 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001208 mService.mConfiguration,
1209 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001210 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001211 }
1212
1213 r.app = app;
1214 app.waitingToKill = null;
1215 r.launchCount++;
1216 r.lastLaunchTime = SystemClock.uptimeMillis();
1217
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001218 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001219
1220 int idx = app.activities.indexOf(r);
1221 if (idx < 0) {
1222 app.activities.add(r);
1223 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001224 mService.updateLruProcessLocked(app, true, null);
1225 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001226
Craig Mautner15df08a2015-04-01 12:17:18 -07001227 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001228 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1229 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001230 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001231 }
1232
1233 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001234 try {
1235 if (app.thread == null) {
1236 throw new RemoteException();
1237 }
1238 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001239 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001240 if (andResume) {
1241 results = r.results;
1242 newIntents = r.newIntents;
1243 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001244 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1245 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1246 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001247 if (andResume) {
1248 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1249 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001250 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001251 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001252 if (r.isHomeActivity() && r.isNotResolverActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001253 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001254 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001255 }
1256 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1257 r.sleeping = false;
1258 r.forceNewConfig = false;
1259 mService.showAskCompatModeDialogLocked(r);
1260 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001261 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001262 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1263 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1264 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001265 final String profileFile = mService.mProfileFile;
1266 if (profileFile != null) {
1267 ParcelFileDescriptor profileFd = mService.mProfileFd;
1268 if (profileFd != null) {
1269 try {
1270 profileFd = profileFd.dup();
1271 } catch (IOException e) {
1272 if (profileFd != null) {
1273 try {
1274 profileFd.close();
1275 } catch (IOException o) {
1276 }
1277 profileFd = null;
1278 }
1279 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001280 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001281
1282 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1283 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001284 }
1285 }
1286 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001287
Craig Mautner2568c3a2015-03-26 14:22:34 -07001288 if (andResume) {
1289 app.hasShownUi = true;
1290 app.pendingUiClean = true;
1291 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001292 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001293 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001294 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001295 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001296 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001297 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001298
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001299 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001300 // This may be a heavy-weight process! Note that the package
1301 // manager will ensure that only activity can run in the main
1302 // process of the .apk, which is the only thing that will be
1303 // considered heavy-weight.
1304 if (app.processName.equals(app.info.packageName)) {
1305 if (mService.mHeavyWeightProcess != null
1306 && mService.mHeavyWeightProcess != app) {
1307 Slog.w(TAG, "Starting new heavy weight process " + app
1308 + " when already running "
1309 + mService.mHeavyWeightProcess);
1310 }
1311 mService.mHeavyWeightProcess = app;
1312 Message msg = mService.mHandler.obtainMessage(
1313 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1314 msg.obj = r;
1315 mService.mHandler.sendMessage(msg);
1316 }
1317 }
1318
1319 } catch (RemoteException e) {
1320 if (r.launchFailed) {
1321 // This is the second time we failed -- finish activity
1322 // and give up.
1323 Slog.e(TAG, "Second failure launching "
1324 + r.intent.getComponent().flattenToShortString()
1325 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001326 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001327 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1328 "2nd-crash", false);
1329 return false;
1330 }
1331
1332 // This is the first time we failed -- restart process and
1333 // retry.
1334 app.activities.remove(r);
1335 throw e;
1336 }
1337
1338 r.launchFailed = false;
1339 if (stack.updateLRUListLocked(r)) {
1340 Slog.w(TAG, "Activity " + r
1341 + " being launched, but already in LRU list");
1342 }
1343
1344 if (andResume) {
1345 // As part of the process of launching, ActivityThread also performs
1346 // a resume.
1347 stack.minimalResumeActivityLocked(r);
1348 } else {
1349 // This activity is not starting in the resumed state... which
1350 // should look like we asked it to pause+stop (but remain visible),
1351 // and it has done so and reported back the current icicle and
1352 // other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001353 if (DEBUG_STATES) Slog.v(TAG_STATES,
1354 "Moving to STOPPED: " + r + " (starting in stopped state)");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001355 r.state = STOPPED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001356 r.stopped = true;
1357 }
1358
1359 // Launch the new version setup screen if needed. We do this -after-
1360 // launching the initial activity (that is, home), so that it can have
1361 // a chance to initialize itself while in the background, making the
1362 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001363 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001364 mService.startSetupActivityLocked();
1365 }
1366
Dianne Hackborn465fa392014-09-14 14:21:18 -07001367 // Update any services we are bound to that might care about whether
1368 // their client may have activities.
1369 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1370
Craig Mautner2420ead2013-04-01 17:13:20 -07001371 return true;
1372 }
1373
Craig Mautnere79d42682013-04-01 19:01:53 -07001374 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001375 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001376 // Is this activity's application already running?
1377 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001378 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001379
1380 r.task.stack.setLaunchTime(r);
1381
1382 if (app != null && app.thread != null) {
1383 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001384 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1385 || !"android".equals(r.info.packageName)) {
1386 // Don't add this if it is a platform component that is marked
1387 // to run in multiple processes, because this is actually
1388 // part of the framework so doesn't make sense to track as a
1389 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001390 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1391 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001392 }
George Mount2c92c972014-03-20 09:38:23 -07001393 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001394 return;
1395 } catch (RemoteException e) {
1396 Slog.w(TAG, "Exception when starting activity "
1397 + r.intent.getComponent().flattenToShortString(), e);
1398 }
1399
1400 // If a dead object exception was thrown -- fall through to
1401 // restart the application.
1402 }
1403
1404 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001405 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001406 }
1407
Craig Mautner6170f732013-04-02 13:05:23 -07001408 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001409 Intent intent, String resolvedType, ActivityInfo aInfo,
1410 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1411 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001412 int callingPid, int callingUid, String callingPackage,
1413 int realCallingPid, int realCallingUid, int startFlags, Bundle options,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001414 boolean ignoreTargetSecurity, boolean componentSpecified, ActivityRecord[] outActivity,
1415 ActivityContainer container, TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001416 int err = ActivityManager.START_SUCCESS;
1417
1418 ProcessRecord callerApp = null;
1419 if (caller != null) {
1420 callerApp = mService.getRecordForAppLocked(caller);
1421 if (callerApp != null) {
1422 callingPid = callerApp.pid;
1423 callingUid = callerApp.info.uid;
1424 } else {
1425 Slog.w(TAG, "Unable to find app for caller " + caller
1426 + " (pid=" + callingPid + ") when starting: "
1427 + intent.toString());
1428 err = ActivityManager.START_PERMISSION_DENIED;
1429 }
1430 }
1431
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001432 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1433
Craig Mautner6170f732013-04-02 13:05:23 -07001434 if (err == ActivityManager.START_SUCCESS) {
Craig Mautner6170f732013-04-02 13:05:23 -07001435 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001436 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001437 + " on display " + (container == null ? (mFocusedStack == null ?
1438 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1439 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1440 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001441 }
1442
1443 ActivityRecord sourceRecord = null;
1444 ActivityRecord resultRecord = null;
1445 if (resultTo != null) {
1446 sourceRecord = isInAnyStackLocked(resultTo);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001447 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
1448 "Will send result to " + resultTo + " " + sourceRecord);
Craig Mautner6170f732013-04-02 13:05:23 -07001449 if (sourceRecord != null) {
1450 if (requestCode >= 0 && !sourceRecord.finishing) {
1451 resultRecord = sourceRecord;
1452 }
1453 }
1454 }
Craig Mautner6170f732013-04-02 13:05:23 -07001455
Dianne Hackborn91097de2014-04-04 18:02:06 -07001456 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001457
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001458 if ((launchFlags & Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001459 // Transfer the result target from the source activity to the new
1460 // one being started, including any failures.
1461 if (requestCode >= 0) {
1462 ActivityOptions.abort(options);
1463 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1464 }
1465 resultRecord = sourceRecord.resultTo;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001466 if (resultRecord != null && !resultRecord.isInStackLocked()) {
1467 resultRecord = null;
1468 }
Craig Mautner6170f732013-04-02 13:05:23 -07001469 resultWho = sourceRecord.resultWho;
1470 requestCode = sourceRecord.requestCode;
1471 sourceRecord.resultTo = null;
1472 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001473 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001474 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001475 if (sourceRecord.launchedFromUid == callingUid) {
1476 // The new activity is being launched from the same uid as the previous
1477 // activity in the flow, and asking to forward its result back to the
1478 // previous. In this case the activity is serving as a trampoline between
1479 // the two, so we also want to update its launchedFromPackage to be the
1480 // same as the previous activity. Note that this is safe, since we know
1481 // these two packages come from the same uid; the caller could just as
1482 // well have supplied that same package name itself. This specifially
1483 // deals with the case of an intent picker/chooser being launched in the app
1484 // flow to redirect to an activity picked by the user, where we want the final
1485 // activity to consider it to have been launched by the previous app activity.
1486 callingPackage = sourceRecord.launchedFromPackage;
1487 }
Craig Mautner6170f732013-04-02 13:05:23 -07001488 }
1489
1490 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1491 // We couldn't find a class that can handle the given Intent.
1492 // That's the end of that!
1493 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1494 }
1495
1496 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1497 // We couldn't find the specific class specified in the Intent.
1498 // Also the end of the line.
1499 err = ActivityManager.START_CLASS_NOT_FOUND;
1500 }
1501
Dianne Hackborn91097de2014-04-04 18:02:06 -07001502 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1503 && sourceRecord.task.voiceSession != null) {
1504 // If this activity is being launched as part of a voice session, we need
1505 // to ensure that it is safe to do so. If the upcoming activity will also
1506 // be part of the voice session, we can only launch it if it has explicitly
1507 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001508 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001509 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1510 try {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001511 intent.addCategory(Intent.CATEGORY_VOICE);
Dianne Hackborn95465202014-09-15 16:21:55 -07001512 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1513 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001514 Slog.w(TAG,
1515 "Activity being started in current voice task does not support voice: "
1516 + intent);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001517 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1518 }
1519 } catch (RemoteException e) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001520 Slog.w(TAG, "Failure checking voice capabilities", e);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001521 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1522 }
1523 }
1524 }
1525
1526 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1527 // If the caller is starting a new voice session, just make sure the target
1528 // is actually allowing it to run this way.
1529 try {
1530 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1531 intent, resolvedType)) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001532 Slog.w(TAG,
1533 "Activity being started in new voice task does not support: "
1534 + intent);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001535 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1536 }
1537 } catch (RemoteException e) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001538 Slog.w(TAG, "Failure checking voice capabilities", e);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001539 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1540 }
1541 }
1542
louis_changcd5d1982014-08-01 10:09:08 +08001543 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1544
Craig Mautner6170f732013-04-02 13:05:23 -07001545 if (err != ActivityManager.START_SUCCESS) {
1546 if (resultRecord != null) {
1547 resultStack.sendActivityResultLocked(-1,
1548 resultRecord, resultWho, requestCode,
1549 Activity.RESULT_CANCELED, null);
1550 }
Craig Mautner6170f732013-04-02 13:05:23 -07001551 ActivityOptions.abort(options);
1552 return err;
1553 }
1554
Svetoslav7008b512015-06-24 18:47:07 -07001555 boolean abort = false;
1556
Svet Ganov99b60432015-06-27 13:15:22 -07001557 final int startAnyPerm = mService.checkPermission(
1558 START_ANY_ACTIVITY, callingPid, callingUid);
1559
1560 if (startAnyPerm != PERMISSION_GRANTED) {
1561 final int componentRestriction = getComponentRestrictionForCallingPackage(
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001562 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
Svet Ganov99b60432015-06-27 13:15:22 -07001563 final int actionRestriction = getActionRestrictionForCallingPackage(
1564 intent.getAction(), callingPackage, callingPid, callingUid);
1565
1566 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1567 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1568 if (resultRecord != null) {
1569 resultStack.sendActivityResultLocked(-1,
1570 resultRecord, resultWho, requestCode,
1571 Activity.RESULT_CANCELED, null);
1572 }
1573 String msg;
1574 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1575 msg = "Permission Denial: starting " + intent.toString()
1576 + " from " + callerApp + " (pid=" + callingPid
1577 + ", uid=" + callingUid + ")" + " with revoked permission "
1578 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1579 } else if (!aInfo.exported) {
1580 msg = "Permission Denial: starting " + intent.toString()
1581 + " from " + callerApp + " (pid=" + callingPid
1582 + ", uid=" + callingUid + ")"
1583 + " not exported from uid " + aInfo.applicationInfo.uid;
1584 } else {
1585 msg = "Permission Denial: starting " + intent.toString()
1586 + " from " + callerApp + " (pid=" + callingPid
1587 + ", uid=" + callingUid + ")"
1588 + " requires " + aInfo.permission;
1589 }
1590 Slog.w(TAG, msg);
1591 throw new SecurityException(msg);
1592 }
1593
1594 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1595 String message = "Appop Denial: starting " + intent.toString()
1596 + " from " + callerApp + " (pid=" + callingPid
1597 + ", uid=" + callingUid + ")"
1598 + " requires " + AppOpsManager.permissionToOp(
1599 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1600 Slog.w(TAG, message);
1601 abort = true;
1602 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1603 String message = "Appop Denial: starting " + intent.toString()
1604 + " from " + callerApp + " (pid=" + callingPid
1605 + ", uid=" + callingUid + ")"
1606 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1607 Slog.w(TAG, message);
1608 abort = true;
1609 }
Svetoslav7008b512015-06-24 18:47:07 -07001610 }
1611
1612 abort |= !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001613 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001614
Craig Mautner6170f732013-04-02 13:05:23 -07001615 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001616 try {
1617 // The Intent we give to the watcher has the extra data
1618 // stripped off, since it can contain private information.
1619 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001620 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001621 aInfo.applicationInfo.packageName);
1622 } catch (RemoteException e) {
1623 mService.mController = null;
1624 }
Ben Gruver5e207332013-04-03 17:41:37 -07001625 }
Craig Mautner6170f732013-04-02 13:05:23 -07001626
Ben Gruver5e207332013-04-03 17:41:37 -07001627 if (abort) {
1628 if (resultRecord != null) {
1629 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001630 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001631 }
Ben Gruver5e207332013-04-03 17:41:37 -07001632 // We pretend to the caller that it was really started, but
1633 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001634 ActivityOptions.abort(options);
1635 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001636 }
1637
1638 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001639 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001640 requestCode, componentSpecified, voiceSession != null, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001641 if (outActivity != null) {
1642 outActivity[0] = r;
1643 }
1644
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001645 if (r.appTimeTracker == null && sourceRecord != null) {
1646 // If the caller didn't specify an explicit time tracker, we want to continue
1647 // tracking under any it has.
1648 r.appTimeTracker = sourceRecord.appTimeTracker;
1649 }
1650
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001651 final ActivityStack stack = mFocusedStack;
Dianne Hackborn91097de2014-04-04 18:02:06 -07001652 if (voiceSession == null && (stack.mResumedActivity == null
1653 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001654 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1655 realCallingPid, realCallingUid, "Activity start")) {
Craig Mautner6170f732013-04-02 13:05:23 -07001656 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001657 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautneree36c772014-07-16 14:56:05 -07001658 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001659 ActivityOptions.abort(options);
1660 return ActivityManager.START_SWITCHES_CANCELED;
1661 }
1662 }
1663
1664 if (mService.mDidAppSwitch) {
1665 // This is the second allowed switch since we stopped switches,
1666 // so now just generally allow switches. Use case: user presses
1667 // home (switches disabled, switch to home, mDidAppSwitch now true);
1668 // user taps a home icon (coming from home so allowed, we hit here
1669 // and now allow anyone to switch again).
1670 mService.mAppSwitchesAllowedTime = 0;
1671 } else {
1672 mService.mDidAppSwitch = true;
1673 }
1674
Craig Mautneree36c772014-07-16 14:56:05 -07001675 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001676
Dianne Hackborn91097de2014-04-04 18:02:06 -07001677 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001678 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001679
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001680 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001681 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001682 // activity start, but we are not actually doing an activity
1683 // switch... just dismiss the keyguard now, because we
1684 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001685 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001686 }
1687 return err;
1688 }
1689
Svet Ganov99b60432015-06-27 13:15:22 -07001690 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001691 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Svet Ganov99b60432015-06-27 13:15:22 -07001692 if (activityInfo.permission == null) {
1693 return ACTIVITY_RESTRICTION_NONE;
1694 }
1695
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001696 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1697 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001698 == PackageManager.PERMISSION_DENIED) {
1699 return ACTIVITY_RESTRICTION_PERMISSION;
1700 }
1701
1702 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1703 if (opCode == AppOpsManager.OP_NONE) {
1704 return ACTIVITY_RESTRICTION_NONE;
1705 }
1706
1707 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1708 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001709 if (!ignoreTargetSecurity) {
1710 return ACTIVITY_RESTRICTION_APPOP;
1711 }
Svet Ganov99b60432015-06-27 13:15:22 -07001712 }
1713
1714 return ACTIVITY_RESTRICTION_NONE;
1715 }
1716
Svetoslav7008b512015-06-24 18:47:07 -07001717 private int getActionRestrictionForCallingPackage(String action,
1718 String callingPackage, int callingPid, int callingUid) {
1719 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001720 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001721 }
1722
1723 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1724 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001725 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001726 }
1727
1728 final PackageInfo packageInfo;
1729 try {
1730 packageInfo = mService.mContext.getPackageManager()
1731 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1732 } catch (PackageManager.NameNotFoundException e) {
1733 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001734 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001735 }
1736
1737 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001738 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001739 }
1740
1741 if (mService.checkPermission(permission, callingPid, callingUid) ==
1742 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001743 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001744 }
1745
1746 final int opCode = AppOpsManager.permissionToOpCode(permission);
1747 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001748 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001749 }
1750
1751 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1752 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001753 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001754 }
1755
Svet Ganov99b60432015-06-27 13:15:22 -07001756 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001757 }
1758
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001759 ActivityStack computeStackFocus(ActivityRecord r, boolean newTask) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001760 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001761
1762 // On leanback only devices we should keep all activities in the same stack.
1763 if (!mLeanbackOnlyDevice &&
1764 (r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001765
1766 ActivityStack stack;
1767
Wale Ogunwale7d701172015-03-11 15:36:30 -07001768 if (task != null && task.stack != null) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001769 stack = task.stack;
1770 if (stack.isOnHomeDisplay()) {
1771 if (mFocusedStack != stack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001772 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1773 "computeStackFocus: Setting " + "focused stack to r=" + r
1774 + " task=" + task);
Craig Mautnere0a38842013-12-16 16:14:02 -08001775 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001776 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001777 "computeStackFocus: Focused stack already=" + mFocusedStack);
Craig Mautnere0a38842013-12-16 16:14:02 -08001778 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001779 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001780 return stack;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001781 }
1782
Craig Mautnere0a38842013-12-16 16:14:02 -08001783 final ActivityContainer container = r.mInitialActivityContainer;
1784 if (container != null) {
1785 // The first time put it on the desired stack, after this put on task stack.
1786 r.mInitialActivityContainer = null;
1787 return container.mStack;
1788 }
1789
Craig Mautner1b4bf852014-05-26 15:06:32 -07001790 if (mFocusedStack != mHomeStack && (!newTask ||
1791 mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001792 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001793 "computeStackFocus: Have a focused stack=" + mFocusedStack);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001794 return mFocusedStack;
1795 }
1796
Wale Ogunwale706ed792015-08-02 10:29:44 -07001797 // We first try to put the task in the first dynamic stack.
Craig Mautnere0a38842013-12-16 16:14:02 -08001798 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1799 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001800 stack = homeDisplayStacks.get(stackNdx);
Wale Ogunwale706ed792015-08-02 10:29:44 -07001801 final boolean isDynamicStack = stack.mStackId >= FIRST_DYNAMIC_STACK_ID;
1802 if (isDynamicStack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001803 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001804 "computeStackFocus: Setting focused stack=" + stack);
1805 return stack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001806 }
1807 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001808
Wale Ogunwale706ed792015-08-02 10:29:44 -07001809 // If there is no suitable dynamic stack then we figure out which static stack to use.
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001810 stack = getStack(
Wale Ogunwale706ed792015-08-02 10:29:44 -07001811 task != null
1812 ? task.getLaunchStackId(mFocusedStack) : FULLSCREEN_WORKSPACE_STACK_ID,
Wale Ogunwale040b4702015-08-06 18:10:50 -07001813 CREATE_IF_NEEDED, ON_TOP);
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001814 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS, "computeStackFocus: New stack r="
1815 + r + " stackId=" + stack.mStackId);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001816 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001817 }
1818 return mHomeStack;
1819 }
1820
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001821 boolean setFocusedStack(ActivityRecord r, String reason) {
1822 if (r == null) {
1823 // Not sure what you are trying to do, but it is not going to work...
1824 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001825 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001826 final TaskRecord task = r.task;
1827 if (task == null || task.stack == null) {
1828 Slog.w(TAG, "Can't set focus stack for r=" + r + " task=" + task);
1829 return false;
1830 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001831 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001832 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001833 }
1834
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001835 final int startActivityUncheckedLocked(final ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001836 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001837 boolean doResume, Bundle options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001838 final Intent intent = r.intent;
1839 final int callingUid = r.launchedFromUid;
1840
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001841 // In some flows in to this function, we retrieve the task record and hold on to it
1842 // without a lock before calling back in to here... so the task at this point may
1843 // not actually be in recents. Check for that, and if it isn't in recents just
1844 // consider it invalid.
1845 if (inTask != null && !inTask.inRecents) {
1846 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1847 inTask = null;
1848 }
1849
Craig Mautnerad400af2014-08-13 16:41:36 -07001850 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001851 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1852 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001853
Craig Mautnera228ae92014-07-09 05:44:55 -07001854 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001855 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001856 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001857 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1858 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1859 "\"singleInstance\" or \"singleTask\"");
1860 launchFlags &=
1861 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1862 } else {
1863 switch (r.info.documentLaunchMode) {
1864 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1865 break;
1866 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1867 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1868 break;
1869 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1870 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1871 break;
1872 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1873 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1874 break;
1875 }
1876 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001877
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001878 final boolean launchTaskBehind = r.mLaunchTaskBehind
1879 && !launchSingleTask && !launchSingleInstance
1880 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001881
Wale Ogunwale7d701172015-03-11 15:36:30 -07001882 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0
1883 && r.resultTo.task.stack != null) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001884 // For whatever reason this activity is being launched into a new
1885 // task... yet the caller has requested a result back. Well, that
1886 // is pretty messed up, so instead immediately send back a cancel
1887 // and let the new task continue launched as normal without a
1888 // dependency on its originator.
1889 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1890 r.resultTo.task.stack.sendActivityResultLocked(-1,
1891 r.resultTo, r.resultWho, r.requestCode,
1892 Activity.RESULT_CANCELED, null);
1893 r.resultTo = null;
1894 }
1895
1896 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1897 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1898 }
1899
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001900 // If we are actually going to launch in to a new task, there are some cases where
1901 // we further want to do multiple task.
1902 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1903 if (launchTaskBehind
1904 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
1905 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1906 }
1907 }
1908
Craig Mautner8849a5e2013-04-02 16:41:03 -07001909 // We'll invoke onUserLeaving before onPause only if the launching
1910 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001911 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001912 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
1913 "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001914
1915 // If the caller has asked not to resume at this point, we make note
1916 // of this in the record so that we can skip it when trying to find
1917 // the top running activity.
Chong Zhang45c25ce2015-08-10 22:18:26 -07001918 if (!doResume || !okToShowLocked(r)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001919 r.delayedResume = true;
Chong Zhang45c25ce2015-08-10 22:18:26 -07001920 doResume = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001921 }
1922
Craig Mautnera254cd72014-05-25 16:47:39 -07001923 ActivityRecord notTop =
1924 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001925
1926 // If the onlyIfNeeded flag is set, then we can do this if the activity
1927 // being launched is the same as the one making the call... or, as
1928 // a special case, if we do not know the caller then we count the
1929 // current top activity as the caller.
1930 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1931 ActivityRecord checkedCaller = sourceRecord;
1932 if (checkedCaller == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001933 checkedCaller = mFocusedStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001934 }
1935 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1936 // Caller is not the same as launcher, so always needed.
1937 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1938 }
1939 }
1940
Craig Mautner8849a5e2013-04-02 16:41:03 -07001941 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001942 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001943
1944 // If the caller is not coming from another activity, but has given us an
1945 // explicit task into which they would like us to launch the new activity,
1946 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001947 if (sourceRecord == null && inTask != null && inTask.stack != null) {
1948 final Intent baseIntent = inTask.getBaseIntent();
1949 final ActivityRecord root = inTask.getRootActivity();
1950 if (baseIntent == null) {
1951 ActivityOptions.abort(options);
1952 throw new IllegalArgumentException("Launching into task without base intent: "
1953 + inTask);
1954 }
1955
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001956 // If this task is empty, then we are adding the first activity -- it
1957 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001958 if (launchSingleInstance || launchSingleTask) {
1959 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
1960 ActivityOptions.abort(options);
1961 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
1962 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001963 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001964 if (root != null) {
1965 ActivityOptions.abort(options);
1966 throw new IllegalArgumentException("Caller with inTask " + inTask
1967 + " has root " + root + " but target is singleInstance/Task");
1968 }
1969 }
1970
1971 // If task is empty, then adopt the interesting intent launch flags in to the
1972 // activity being started.
1973 if (root == null) {
1974 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
1975 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
1976 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
1977 launchFlags = (launchFlags&~flagsOfInterest)
1978 | (baseIntent.getFlags()&flagsOfInterest);
1979 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001980 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07001981 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001982
Dianne Hackborn962d5352014-08-29 16:27:40 -07001983 // If the task is not empty and the caller is asking to start it as the root
1984 // of a new task, then we don't actually want to start this on the task. We
1985 // will bring the task to the front, and possibly give it a new intent.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001986 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07001987 addingToTask = false;
1988
1989 } else {
1990 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001991 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07001992
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001993 reuseTask = inTask;
1994 } else {
1995 inTask = null;
1996 }
1997
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001998 if (inTask == null) {
1999 if (sourceRecord == null) {
2000 // This activity is not being started from another... in this
2001 // case we -always- start a new task.
2002 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
2003 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
2004 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
2005 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2006 }
2007 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
2008 // The original activity who is starting us is running as a single
2009 // instance... this new activity it is starting must go on its
2010 // own task.
2011 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2012 } else if (launchSingleInstance || launchSingleTask) {
2013 // The activity being started is a single instance... it always
2014 // gets launched into its own task.
2015 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2016 }
2017 }
2018
2019 ActivityInfo newTaskInfo = null;
2020 Intent newTaskIntent = null;
2021 ActivityStack sourceStack;
2022 if (sourceRecord != null) {
2023 if (sourceRecord.finishing) {
2024 // If the source is finishing, we can't further count it as our source. This
2025 // is because the task it is associated with may now be empty and on its way out,
2026 // so we don't want to blindly throw it in to that task. Instead we will take
2027 // the NEW_TASK flow and try to find a task for it. But save the task information
2028 // so it can be used when creating the new task.
2029 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
2030 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
2031 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
2032 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2033 newTaskInfo = sourceRecord.info;
2034 newTaskIntent = sourceRecord.task.intent;
2035 }
2036 sourceRecord = null;
2037 sourceStack = null;
2038 } else {
2039 sourceStack = sourceRecord.task.stack;
2040 }
2041 } else {
2042 sourceStack = null;
2043 }
2044
2045 boolean movedHome = false;
2046 ActivityStack targetStack;
2047
2048 intent.setFlags(launchFlags);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002049 final boolean noAnimation = (launchFlags & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002050
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002051 // We may want to try to place the new activity in to an existing task. We always
2052 // do this if the target activity is singleTask or singleInstance; we will also do
2053 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
2054 // us to still place it in a new task: multi task, always doc mode, or being asked to
2055 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07002056 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
2057 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07002058 || launchSingleInstance || launchSingleTask) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002059 // If bring to front is requested, and no result is requested and we have not
2060 // been given an explicit task to launch in to, and
Craig Mautner8849a5e2013-04-02 16:41:03 -07002061 // we can find a task that was started with this same
2062 // component, then instead of launching bring that one to the front.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002063 if (inTask == null && r.resultTo == null) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002064 // See if there is a task to bring to the front. If this is
2065 // a SINGLE_INSTANCE activity, there can be one and only one
2066 // instance of it in the history, and it is always in its own
2067 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07002068 ActivityRecord intentActivity = !launchSingleInstance ?
2069 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002070 if (intentActivity != null) {
Jason Monk25d237b2015-06-19 10:39:39 -04002071 // When the flags NEW_TASK and CLEAR_TASK are set, then the task gets reused
2072 // but still needs to be a lock task mode violation since the task gets
2073 // cleared out and the device would otherwise leave the locked task.
2074 if (isLockTaskModeViolation(intentActivity.task,
2075 (launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
2076 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))) {
Craig Mautner6cd6cec2015-04-01 00:02:12 -07002077 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07002078 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08002079 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2080 }
Craig Mautner29219d92013-04-16 20:19:12 -07002081 if (r.task == null) {
2082 r.task = intentActivity.task;
2083 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002084 if (intentActivity.task.intent == null) {
2085 // This task was started because of movement of
2086 // the activity based on affinity... now that we
2087 // are actually launching it, we can assign the
2088 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07002089 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002090 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002091 targetStack = intentActivity.task.stack;
2092 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002093 // If the target task is not in the front, then we need
2094 // to bring it to the front... except... well, with
2095 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
2096 // to have the same behavior as if a new instance was
2097 // being started, which means not bringing it to the front
2098 // if the caller is not itself in the front.
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002099 final ActivityStack focusStack = getFocusedStack();
2100 ActivityRecord curTop = (focusStack == null)
2101 ? null : focusStack.topRunningNonDelayedActivityLocked(notTop);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002102 boolean movedToFront = false;
Craig Mautner7504d7b2013-09-17 10:50:53 -07002103 if (curTop != null && (curTop.task != intentActivity.task ||
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002104 curTop.task != focusStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002105 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07002106 if (sourceRecord == null || (sourceStack.topActivity() != null &&
2107 sourceStack.topActivity().task == sourceRecord.task)) {
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002108 // We really do want to push this one into the user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07002109 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002110 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
2111 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002112 movedHome = true;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002113 targetStack.moveTaskToFrontLocked(intentActivity.task, noAnimation,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002114 options, r.appTimeTracker, "bringingFoundTaskToFront");
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002115 movedToFront = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002116 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07002117 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2118 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07002119 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07002120 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002121 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002122 options = null;
2123 }
2124 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002125 if (!movedToFront && doResume) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002126 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Bring to front target: " + targetStack
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002127 + " from " + intentActivity);
2128 targetStack.moveToFront("intentActivityFound");
2129 }
2130
Craig Mautner8849a5e2013-04-02 16:41:03 -07002131 // If the caller has requested that the target task be
2132 // reset, then do so.
2133 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
2134 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
2135 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002136 if ((startFlags & ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002137 // We don't need to start a new activity, and
2138 // the client said not to do anything if that
2139 // is the case, so this is it! And for paranoia, make
2140 // sure we have correctly resumed the top activity.
2141 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002142 resumeTopActivitiesLocked(targetStack, null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002143
2144 // Make sure to notify Keyguard as well if we are not running an app
2145 // transition later.
2146 if (!movedToFront) {
2147 notifyActivityDrawnForKeyguard();
2148 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002149 } else {
2150 ActivityOptions.abort(options);
2151 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002152 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002153 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2154 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002155 if ((launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002156 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002157 // The caller has requested to completely replace any
2158 // existing task with its new activity. Well that should
2159 // not be too hard...
2160 reuseTask = intentActivity.task;
2161 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07002162 reuseTask.setIntent(r);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002163 } else if ((launchFlags & FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07002164 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002165 // In this situation we want to remove all activities
2166 // from the task up to the one being started. In most
2167 // cases this means we are resetting the task to its
2168 // initial state.
2169 ActivityRecord top =
2170 intentActivity.task.performClearTaskLocked(r, launchFlags);
2171 if (top != null) {
2172 if (top.frontOfTask) {
2173 // Activity aliases may mean we use different
2174 // intents for the top activity, so make sure
2175 // the task now has the identity of the new
2176 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07002177 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002178 }
2179 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
2180 r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002181 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002182 } else {
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002183 // A special case: we need to start the activity because it is not
2184 // currently running, and the caller has asked to clear the current
2185 // task to have this activity at the top.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002186 addingToTask = true;
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002187 // Now pretend like this activity is being started by the top of its
2188 // task, so it is put in the right place.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002189 sourceRecord = intentActivity;
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002190 TaskRecord task = sourceRecord.task;
2191 if (task != null && task.stack == null) {
2192 // Target stack got cleared when we all activities were removed
2193 // above. Go ahead and reset it.
2194 targetStack = computeStackFocus(sourceRecord, false /* newTask */);
2195 targetStack.addTask(
2196 task, !launchTaskBehind /* toTop */, false /* moving */);
2197 }
2198
Craig Mautner8849a5e2013-04-02 16:41:03 -07002199 }
2200 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
2201 // In this case the top activity on the task is the
2202 // same as the one being launched, so we take that
2203 // as a request to bring the task to the foreground.
2204 // If the top activity in the task is the root
2205 // activity, deliver this new intent to it if it
2206 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07002207 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07002208 && intentActivity.realActivity.equals(r.realActivity)) {
2209 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
2210 intentActivity.task);
2211 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07002212 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002213 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002214 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
2215 r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002216 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
2217 // In this case we are launching the root activity
2218 // of the task, but with a different intent. We
2219 // should start a new instance on top.
2220 addingToTask = true;
2221 sourceRecord = intentActivity;
2222 }
2223 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
2224 // In this case an activity is being launched in to an
2225 // existing task, without resetting that task. This
2226 // is typically the situation of launching an activity
2227 // from a notification or shortcut. We want to place
2228 // the new activity on top of the current task.
2229 addingToTask = true;
2230 sourceRecord = intentActivity;
2231 } else if (!intentActivity.task.rootWasReset) {
2232 // In this case we are launching in to an existing task
2233 // that has not yet been started from its front door.
2234 // The current task has been brought to the front.
2235 // Ideally, we'd probably like to place this new task
2236 // at the bottom of its stack, but that's a little hard
2237 // to do with the current organization of the code so
2238 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07002239 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002240 }
2241 if (!addingToTask && reuseTask == null) {
2242 // We didn't do anything... but it was needed (a.k.a., client
2243 // don't use that intent!) And for paranoia, make
2244 // sure we have correctly resumed the top activity.
2245 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002246 targetStack.resumeTopActivityLocked(null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002247 if (!movedToFront) {
2248 // Make sure to notify Keyguard as well if we are not running an app
2249 // transition later.
2250 notifyActivityDrawnForKeyguard();
2251 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002252 } else {
2253 ActivityOptions.abort(options);
2254 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002255 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002256 return ActivityManager.START_TASK_TO_FRONT;
2257 }
2258 }
2259 }
2260 }
2261
2262 //String uri = r.intent.toURI();
2263 //Intent intent2 = new Intent(uri);
2264 //Slog.i(TAG, "Given intent: " + r.intent);
2265 //Slog.i(TAG, "URI is: " + uri);
2266 //Slog.i(TAG, "To intent: " + intent2);
2267
2268 if (r.packageName != null) {
2269 // If the activity being launched is the same as the one currently
2270 // at the top, then we need to check if it should only be launched
2271 // once.
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002272 ActivityStack topStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002273 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002274 if (top != null && r.resultTo == null) {
2275 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2276 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07002277 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07002278 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002279 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
2280 top.task);
2281 // For paranoia, make sure we have correctly
2282 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002283 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002284 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002285 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002286 }
2287 ActivityOptions.abort(options);
2288 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2289 // We don't need to start a new activity, and
2290 // the client said not to do anything if that
2291 // is the case, so this is it!
2292 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2293 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002294 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002295 return ActivityManager.START_DELIVERED_TO_TOP;
2296 }
2297 }
2298 }
2299 }
2300
2301 } else {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002302 if (r.resultTo != null && r.resultTo.task.stack != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002303 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2304 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002305 }
2306 ActivityOptions.abort(options);
2307 return ActivityManager.START_CLASS_NOT_FOUND;
2308 }
2309
2310 boolean newTask = false;
2311 boolean keepCurTransition = false;
2312
Craig Mautnerbb742462014-07-07 15:28:55 -07002313 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002314 sourceRecord.task : null;
2315
Craig Mautner8849a5e2013-04-02 16:41:03 -07002316 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002317 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002318 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002319 newTask = true;
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002320 targetStack = computeStackFocus(r, newTask);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002321 if (doResume) {
2322 targetStack.moveToFront("startingNewTask");
2323 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002324
Craig Mautner8849a5e2013-04-02 16:41:03 -07002325 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002326 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2327 newTaskInfo != null ? newTaskInfo : r.info,
2328 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002329 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2330 taskToAffiliate);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002331 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2332 "Starting new activity " + r + " in new task " + r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002333 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002334 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002335 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002336 if (isLockTaskModeViolation(r.task)) {
2337 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2338 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2339 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002340 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002341 if ((launchFlags &
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002342 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2343 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002344 // Caller wants to appear on home activity, so before starting
2345 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002346 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002347 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002348 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002349 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002350 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002351 if (isLockTaskModeViolation(sourceTask)) {
2352 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2353 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2354 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002355 targetStack = sourceTask.stack;
Chong Zhang45c25ce2015-08-10 22:18:26 -07002356 if (doResume) {
2357 targetStack.moveToFront("sourceStackToFront");
2358 }
Craig Mautner737fae22014-10-15 12:52:10 -07002359 final TaskRecord topTask = targetStack.topTask();
2360 if (topTask != sourceTask) {
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002361 targetStack.moveTaskToFrontLocked(sourceTask, noAnimation, options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002362 r.appTimeTracker, "sourceTaskToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002363 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002364 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002365 // In this case, we are adding the activity to an existing
2366 // task, but the caller has asked to clear that task if the
2367 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002368 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002369 keepCurTransition = true;
2370 if (top != null) {
2371 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002372 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002373 // For paranoia, make sure we have correctly
2374 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002375 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002376 if (doResume) {
2377 targetStack.resumeTopActivityLocked(null);
2378 }
2379 ActivityOptions.abort(options);
2380 return ActivityManager.START_DELIVERED_TO_TOP;
2381 }
2382 } else if (!addingToTask &&
2383 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2384 // In this case, we are launching an activity in our own task
2385 // that may already be running somewhere in the history, and
2386 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002387 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002388 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002389 final TaskRecord task = top.task;
2390 task.moveActivityToFrontLocked(top);
2391 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002392 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002393 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002394 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002395 if (doResume) {
2396 targetStack.resumeTopActivityLocked(null);
2397 }
2398 return ActivityManager.START_DELIVERED_TO_TOP;
2399 }
2400 }
2401 // An existing activity is starting this new activity, so we want
2402 // to keep the new one in the same task as the one that is starting
2403 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002404 r.setTask(sourceTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002405 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002406 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002407
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002408 } else if (inTask != null) {
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002409 // The caller is asking that the new activity be started in an explicit
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002410 // task it has provided to us.
2411 if (isLockTaskModeViolation(inTask)) {
2412 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2413 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2414 }
2415 targetStack = inTask.stack;
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002416 targetStack.moveTaskToFrontLocked(inTask, noAnimation, options, r.appTimeTracker,
2417 "inTaskToFront");
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002418
2419 // Check whether we should actually launch the new activity in to the task,
2420 // or just reuse the current activity on top.
2421 ActivityRecord top = inTask.getTopActivity();
2422 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2423 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2424 || launchSingleTop || launchSingleTask) {
2425 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2426 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2427 // We don't need to start a new activity, and
2428 // the client said not to do anything if that
2429 // is the case, so this is it!
2430 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2431 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002432 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002433 return ActivityManager.START_DELIVERED_TO_TOP;
2434 }
2435 }
2436
Dianne Hackborn962d5352014-08-29 16:27:40 -07002437 if (!addingToTask) {
2438 // We don't actually want to have this activity added to the task, so just
2439 // stop here but still tell the caller that we consumed the intent.
2440 ActivityOptions.abort(options);
2441 return ActivityManager.START_TASK_TO_FRONT;
2442 }
2443
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002444 r.setTask(inTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002445 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002446 + " in explicit task " + r.task);
2447
Craig Mautner8849a5e2013-04-02 16:41:03 -07002448 } else {
2449 // This not being started from an existing activity, and not part
2450 // of a new task... just put it in the top task, though these days
2451 // this case should never happen.
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002452 targetStack = computeStackFocus(r, newTask);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002453 if (doResume) {
2454 targetStack.moveToFront("addingToTopTask");
2455 }
Craig Mautner1602ec22013-05-12 10:24:27 -07002456 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002457 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002458 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002459 mWindowManager.moveTaskToTop(r.task.taskId);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002460 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Craig Mautner8849a5e2013-04-02 16:41:03 -07002461 + " in new guessed " + r.task);
2462 }
2463
2464 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002465 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002466
Craig Mautner76e2a762014-06-24 09:05:43 -07002467 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2468 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2469 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002470 if (newTask) {
2471 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2472 }
2473 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002474 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002475 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002476 if (!launchTaskBehind && doResume) {
Craig Mautner299f9602015-01-26 09:47:33 -08002477 mService.setFocusedActivityLocked(r, "startedActivity");
Craig Mautnerbb742462014-07-07 15:28:55 -07002478 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002479 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002480 return ActivityManager.START_SUCCESS;
2481 }
2482
Craig Mautneree36c772014-07-16 14:56:05 -07002483 final void doPendingActivityLaunchesLocked(boolean doResume) {
2484 while (!mPendingActivityLaunches.isEmpty()) {
2485 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Craig Mautneraa9b0f12014-07-17 10:50:18 -07002486 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002487 doResume && mPendingActivityLaunches.isEmpty(), null, null);
Craig Mautneree36c772014-07-16 14:56:05 -07002488 }
2489 }
2490
Craig Mautner7f13ed32014-07-28 14:00:35 -07002491 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002492 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2493 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002494 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002495 mPendingActivityLaunches.remove(palNdx);
2496 }
2497 }
2498 }
2499
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002500 void setLaunchSource(int uid) {
2501 mLaunchingActivity.setWorkSource(new WorkSource(uid));
2502 }
2503
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002504 void acquireLaunchWakelock() {
2505 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2506 throw new IllegalStateException("Calling must be system uid");
2507 }
2508 mLaunchingActivity.acquire();
2509 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2510 // To be safe, don't allow the wake lock to be held for too long.
2511 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2512 }
2513 }
2514
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002515 /**
2516 * Called when the frontmost task is idle.
2517 * @return the state of mService.mBooting before this was called.
2518 */
2519 private boolean checkFinishBootingLocked() {
2520 final boolean booting = mService.mBooting;
2521 boolean enableScreen = false;
2522 mService.mBooting = false;
2523 if (!mService.mBooted) {
2524 mService.mBooted = true;
2525 enableScreen = true;
2526 }
2527 if (booting || enableScreen) {
2528 mService.postFinishBooting(booting, enableScreen);
2529 }
2530 return booting;
2531 }
2532
Craig Mautnerf3333272013-04-22 10:55:53 -07002533 // Checked.
2534 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2535 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002536 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002537
Craig Mautnerf3333272013-04-22 10:55:53 -07002538 ArrayList<ActivityRecord> stops = null;
2539 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07002540 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07002541 int NS = 0;
2542 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002543 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002544 boolean activityRemoved = false;
2545
Wale Ogunwale7d701172015-03-11 15:36:30 -07002546 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002547 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002548 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
2549 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002550 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2551 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002552 if (fromTimeout) {
2553 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002554 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002555
2556 // This is a hack to semi-deal with a race condition
2557 // in the client where it can be constructed with a
2558 // newer configuration from when we asked it to launch.
2559 // We'll update with whatever configuration it now says
2560 // it used to launch.
2561 if (config != null) {
2562 r.configuration = config;
2563 }
2564
2565 // We are now idle. If someone is waiting for a thumbnail from
2566 // us, we can now deliver.
2567 r.idle = true;
2568
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002569 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Dianne Hackborn9449a612014-10-01 15:53:28 -07002570 if (isFrontStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002571 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002572 }
2573 }
2574
2575 if (allResumedActivitiesIdle()) {
2576 if (r != null) {
2577 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002578 }
2579
2580 if (mLaunchingActivity.isHeld()) {
2581 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2582 if (VALIDATE_WAKE_LOCK_CALLER &&
2583 Binder.getCallingUid() != Process.myUid()) {
2584 throw new IllegalStateException("Calling must be system uid");
2585 }
2586 mLaunchingActivity.release();
2587 }
2588 ensureActivitiesVisibleLocked(null, 0);
Craig Mautnerf3333272013-04-22 10:55:53 -07002589 }
2590
2591 // Atomically retrieve all of the other things to do.
2592 stops = processStoppingActivitiesLocked(true);
2593 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002594 if ((NF = mFinishingActivities.size()) > 0) {
2595 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002596 mFinishingActivities.clear();
2597 }
2598
Craig Mautnerf3333272013-04-22 10:55:53 -07002599 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002600 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07002601 mStartingUsers.clear();
2602 }
2603
Craig Mautnerf3333272013-04-22 10:55:53 -07002604 // Stop any activities that are scheduled to do so but have been
2605 // waiting for the next one to start.
2606 for (int i = 0; i < NS; i++) {
2607 r = stops.get(i);
2608 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002609 if (stack != null) {
2610 if (r.finishing) {
2611 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2612 } else {
2613 stack.stopActivityLocked(r);
2614 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002615 }
2616 }
2617
2618 // Finish any activities that are scheduled to do so but have been
2619 // waiting for the next one to start.
2620 for (int i = 0; i < NF; i++) {
2621 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07002622 final ActivityStack stack = r.task.stack;
2623 if (stack != null) {
2624 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
2625 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002626 }
2627
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002628 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002629 // Complete user switch
2630 if (startingUsers != null) {
2631 for (int i = 0; i < startingUsers.size(); i++) {
2632 mService.finishUserSwitch(startingUsers.get(i));
2633 }
2634 }
2635 // Complete starting up of background users
2636 if (mStartingBackgroundUsers.size() > 0) {
Amith Yamasani37a40c22015-06-17 13:25:42 -07002637 startingUsers = new ArrayList<UserState>(mStartingBackgroundUsers);
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002638 mStartingBackgroundUsers.clear();
2639 for (int i = 0; i < startingUsers.size(); i++) {
2640 mService.finishUserBoot(startingUsers.get(i));
2641 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002642 }
2643 }
2644
2645 mService.trimApplications();
2646 //dump();
2647 //mWindowManager.dump();
2648
Craig Mautnerf3333272013-04-22 10:55:53 -07002649 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002650 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002651 }
2652
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002653 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002654 }
2655
Craig Mautner8e569572013-10-11 17:36:59 -07002656 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002657 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002658 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2659 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002660 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2661 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2662 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002663 }
Craig Mautner19091252013-10-05 00:03:53 -07002664 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002665 }
2666
2667 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002668 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2669 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002670 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2671 stacks.get(stackNdx).closeSystemDialogsLocked();
2672 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002673 }
2674 }
2675
Craig Mautner93529a42013-10-04 15:03:13 -07002676 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002677 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002678 }
2679
Craig Mautner8d341ef2013-03-26 09:03:27 -07002680 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07002681 * Update the last used stack id for non-current user (current user's last
2682 * used stack is the focused stack)
2683 */
2684 void updateUserStackLocked(int userId, ActivityStack stack) {
2685 if (userId != mCurrentUser) {
2686 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
2687 }
2688 }
2689
2690 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07002691 * @return true if some activity was finished (or would have finished if doit were true).
2692 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07002693 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
2694 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002695 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002696 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2697 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002698 final int numStacks = stacks.size();
2699 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2700 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07002701 if (stack.finishDisabledPackageActivitiesLocked(
2702 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002703 didSomething = true;
2704 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002705 }
2706 }
2707 return didSomething;
2708 }
2709
Dianne Hackborna413dc02013-07-12 12:02:55 -07002710 void updatePreviousProcessLocked(ActivityRecord r) {
2711 // Now that this process has stopped, we may want to consider
2712 // it to be the previous app to try to keep around in case
2713 // the user wants to return to it.
2714
2715 // First, found out what is currently the foreground app, so that
2716 // we don't blow away the previous app if this activity is being
2717 // hosted by the process that is actually still the foreground.
2718 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002719 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2720 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002721 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2722 final ActivityStack stack = stacks.get(stackNdx);
2723 if (isFrontStack(stack)) {
2724 if (stack.mResumedActivity != null) {
2725 fgApp = stack.mResumedActivity.app;
2726 } else if (stack.mPausingActivity != null) {
2727 fgApp = stack.mPausingActivity.app;
2728 }
2729 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002730 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002731 }
2732 }
2733
2734 // Now set this one as the previous process, only if that really
2735 // makes sense to.
2736 if (r.app != null && fgApp != null && r.app != fgApp
2737 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002738 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002739 mService.mPreviousProcess = r.app;
2740 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2741 }
2742 }
2743
Craig Mautner05d29032013-05-03 13:40:13 -07002744 boolean resumeTopActivitiesLocked() {
2745 return resumeTopActivitiesLocked(null, null, null);
2746 }
2747
2748 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2749 Bundle targetOptions) {
2750 if (targetStack == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002751 targetStack = mFocusedStack;
Craig Mautner05d29032013-05-03 13:40:13 -07002752 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002753 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002754 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002755 if (isFrontStack(targetStack)) {
2756 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2757 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002758
Craig Mautnere0a38842013-12-16 16:14:02 -08002759 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2760 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002761 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2762 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002763 if (stack == targetStack) {
2764 // Already started above.
2765 continue;
2766 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002767 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002768 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002769 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002770 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002771 }
Craig Mautner05d29032013-05-03 13:40:13 -07002772 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002773 }
2774
Todd Kennedy539db512014-12-15 09:57:55 -08002775 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002776 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2777 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002778 final int numStacks = stacks.size();
2779 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2780 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08002781 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002782 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002783 }
2784 }
2785
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002786 void finishVoiceTask(IVoiceInteractionSession session) {
2787 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2788 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2789 final int numStacks = stacks.size();
2790 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2791 final ActivityStack stack = stacks.get(stackNdx);
2792 stack.finishVoiceTask(session);
2793 }
2794 }
2795 }
2796
Craig Mautner299f9602015-01-26 09:47:33 -08002797 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002798 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2799 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002800 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002801 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2802 // Caller wants the home activity moved with it. To accomplish this,
2803 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002804 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002805 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07002806 if (task.stack == null) {
2807 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2808 + task + " to front. Stack is null");
2809 return;
2810 }
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002811 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
2812 task.getTopActivity() == null ? null : task.getTopActivity().appTimeTracker,
2813 reason);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002814 if (DEBUG_STACK) Slog.d(TAG_STACK,
2815 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002816 }
2817
Craig Mautner967212c2013-04-13 21:10:58 -07002818 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07002819 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002820 }
2821
2822 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002823 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2824 if (activityContainer != null) {
2825 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002826 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002827 if (!createStaticStackIfNeeded
2828 || (stackId < FIRST_STATIC_STACK_ID || stackId > LAST_STATIC_STACK_ID)) {
2829 return null;
2830 }
2831 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002832 }
2833
Craig Mautner967212c2013-04-13 21:10:58 -07002834 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002835 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002836 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2837 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002838 }
2839 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002840 }
2841
Craig Mautner4a1cb222013-12-04 16:14:06 -08002842 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002843 ActivityRecord homeActivity = getHomeActivity();
2844 if (homeActivity != null) {
2845 return homeActivity.appToken;
2846 }
2847 return null;
2848 }
2849
2850 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002851 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002852 }
2853
2854 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002855 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2856 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2857 final TaskRecord task = tasks.get(taskNdx);
2858 if (task.isHomeTask()) {
2859 final ArrayList<ActivityRecord> activities = task.mActivities;
2860 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2861 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002862 if (r.isHomeActivity()
2863 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002864 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002865 }
2866 }
2867 }
2868 }
2869 return null;
2870 }
2871
Todd Kennedyca4d8422015-01-15 15:19:22 -08002872 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002873 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002874 ActivityContainer activityContainer =
2875 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002876 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002877 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2878 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002879 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002880 return activityContainer;
2881 }
2882
Craig Mautner34b73df2014-01-12 21:11:08 -08002883 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002884 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2885 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2886 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002887 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2888 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002889 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002890 }
2891 }
2892
Craig Mautner95da1082014-02-24 17:54:35 -08002893 void deleteActivityContainer(IActivityContainer container) {
2894 ActivityContainer activityContainer = (ActivityContainer)container;
2895 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002896 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2897 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08002898 final int stackId = activityContainer.mStackId;
2899 mActivityContainers.remove(stackId);
2900 mWindowManager.removeStack(stackId);
2901 }
2902 }
2903
Wale Ogunwale60454db2015-01-23 16:05:07 -08002904 void resizeStackLocked(int stackId, Rect bounds) {
2905 final ActivityStack stack = getStack(stackId);
2906 if (stack == null) {
2907 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2908 return;
2909 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002910
2911 final ActivityRecord r = stack.topRunningActivityLocked(null);
Wale Ogunwale0e162182015-02-05 08:48:34 -08002912 if (r != null && !r.task.mResizeable) {
2913 Slog.w(TAG, "resizeStack: top task " + r.task + " not resizeable.");
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002914 return;
2915 }
2916
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002917 final IntArray changedTaskIds = new IntArray(stack.numTasks());
2918 final List<Configuration> newTaskConfigs = new ArrayList<>(stack.numTasks());
2919 stack.mFullscreen =
2920 mWindowManager.resizeStack(stackId, bounds, changedTaskIds, newTaskConfigs);
2921 for (int i = changedTaskIds.size() - 1; i >= 0; i--) {
2922 final TaskRecord task = anyTaskForIdLocked(changedTaskIds.get(i), false);
2923 if (task == null) {
2924 Slog.wtf(TAG, "Task in WindowManager, but not in ActivityManager???");
2925 continue;
2926 }
Wale Ogunwale706ed792015-08-02 10:29:44 -07002927 task.updateOverrideConfiguration(newTaskConfigs.get(i), bounds);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002928 }
2929
2930 if (r != null) {
2931 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0);
2932 // And we need to make sure at this point that all other activities
2933 // are made visible with the correct configuration.
2934 ensureActivitiesVisibleLocked(r, 0);
2935 if (!updated) {
2936 resumeTopActivitiesLocked(stack, null, null);
2937 }
2938 }
2939 }
2940
2941 void resizeTaskLocked(TaskRecord task, Rect bounds) {
2942 if (!task.mResizeable) {
2943 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
2944 return;
2945 }
2946
2947 if (task.mBounds != null && task.mBounds.equals(bounds)) {
2948 // Nothing to do here...
2949 return;
2950 }
2951
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002952 if (!mWindowManager.isValidTaskId(task.taskId)) {
2953 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07002954 // All we can do for now is update the bounds so it can be used when the task is
2955 // added to window manager.
2956 task.mBounds = task.mLastNonFullscreenBounds = new Rect(bounds);
2957 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
2958 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07002959 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002960 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002961 return;
2962 }
2963
Wale Ogunwale040b4702015-08-06 18:10:50 -07002964 // The stack of a task is determined by its size (fullscreen vs non-fullscreen).
2965 // Place the task in the right stack if it isn't there already based on the requested
2966 // bounds.
2967 int stackId = task.stack.mStackId;
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -07002968 final boolean wasFrontStack = isFrontStack(task.stack);
Wale Ogunwale040b4702015-08-06 18:10:50 -07002969 if (bounds == null && stackId != FULLSCREEN_WORKSPACE_STACK_ID) {
2970 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
2971 } else if (bounds != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
2972 stackId = FREEFORM_WORKSPACE_STACK_ID;
2973 }
2974 if (stackId != task.stack.mStackId) {
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -07002975 final String reason = "resizeTask";
2976 final ActivityStack stack =
2977 moveTaskToStackUncheckedLocked(task, stackId, ON_TOP, reason);
2978 if (wasFrontStack) {
2979 // Since the stack was previously in front,
2980 // move the stack in which we are placing the task to the front.
2981 stack.moveToFront(reason);
2982 }
Wale Ogunwale040b4702015-08-06 18:10:50 -07002983 }
2984
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002985 final Configuration overrideConfig = mWindowManager.resizeTask(task.taskId, bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002986 if (task.updateOverrideConfiguration(overrideConfig, bounds)) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002987 ActivityRecord r = task.topRunningActivityLocked(null);
Wale Ogunwale60454db2015-01-23 16:05:07 -08002988 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002989 final ActivityStack stack = task.stack;
Wale Ogunwale60454db2015-01-23 16:05:07 -08002990 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0);
2991 // And we need to make sure at this point that all other activities
2992 // are made visible with the correct configuration.
2993 ensureActivitiesVisibleLocked(r, 0);
2994 if (!updated) {
2995 resumeTopActivitiesLocked(stack, null, null);
2996 }
2997 }
2998 }
2999 }
3000
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003001 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003002 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3003 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08003004 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003005 }
3006
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003007 ActivityContainer activityContainer = new ActivityContainer(stackId);
3008 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003009 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08003010 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003011 }
3012
3013 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07003014 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003015 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
3016 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07003017 break;
3018 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003019 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003020 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003021 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003022 }
3023
Chong Zhang5dcb2752015-08-18 13:50:26 -07003024 /**
3025 * Restores a recent task to a stack
3026 * @param task The recent task to be restored.
3027 * @param stackId The stack to restore the task to (default launch stack will be used
3028 * if stackId is invalid).
3029 * @return true if the task has been restored successfully.
3030 */
3031 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
3032 if (stackId == INVALID_STACK_ID) {
3033 stackId = mLeanbackOnlyDevice ?
3034 mHomeStack.mStackId : task.getLaunchStackId(mFocusedStack);
3035 }
Wale Ogunwale706ed792015-08-02 10:29:44 -07003036 if (task.stack != null) {
3037 // Task has already been restored once. See if we need to do anything more
3038 if (task.stack.mStackId == stackId) {
3039 // Nothing else to do since it is already restored in the right stack.
3040 return true;
3041 }
3042 // Remove current stack association, so we can re-associate the task with the
3043 // right stack below.
Wale Ogunwale040b4702015-08-06 18:10:50 -07003044 task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003045 }
3046
3047 ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07003048 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003049
3050 if (stack == null) {
3051 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003052 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3053 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003054 return false;
3055 }
3056
3057 stack.addTask(task, false, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003058 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3059 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003060 final ArrayList<ActivityRecord> activities = task.mActivities;
3061 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07003062 stack.addAppToken(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003063 }
3064 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07003065 }
3066
Wale Ogunwale040b4702015-08-06 18:10:50 -07003067 /**
3068 * Moves the specified task record to the input stack id.
3069 * WARNING: This method performs an unchecked/raw move of the task and
3070 * can leave the system in an unstable state if used incorrectly.
3071 * Use {@link #moveTaskToStackLocked} to perform safe task movement
3072 * to a stack.
3073 * @param task Task to move.
3074 * @param stackId Id of stack to move task to.
3075 * @param toTop True if the task should be placed at the top of the stack.
3076 * @param reason Reason the task is been moved.
3077 * @return The stack the task was moved to.
3078 */
3079 private ActivityStack moveTaskToStackUncheckedLocked(
3080 TaskRecord task, int stackId, boolean toTop, String reason) {
3081 final ActivityStack stack =
3082 getStack(stackId, CREATE_IF_NEEDED, toTop);
3083 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
3084 if (task.stack != null) {
3085 task.stack.removeTask(task, reason, MOVING);
3086 }
3087 stack.addTask(task, toTop, MOVING);
3088 return stack;
3089 }
3090
Craig Mautner299f9602015-01-26 09:47:33 -08003091 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003092 final TaskRecord task = anyTaskForIdLocked(taskId);
3093 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08003094 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003095 return;
3096 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003097 ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07003098 moveTaskToStackUncheckedLocked(task, stackId, toTop, "moveTaskToStack");
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003099
3100 // Make sure the task has the appropriate bounds/size for the stack it is in.
3101 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
3102 resizeTaskLocked(task, null);
3103 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID
3104 && task.mBounds == null && task.mLastNonFullscreenBounds != null) {
3105 resizeTaskLocked(task, task.mLastNonFullscreenBounds);
3106 }
3107
Stefan Kuhne54714cd2015-05-12 13:42:18 -07003108 // The task might have already been running and its visibility needs to be synchronized with
3109 // the visibility of the stack / windows.
3110 stack.ensureActivitiesVisibleLocked(null, 0);
Craig Mautner05d29032013-05-03 13:40:13 -07003111 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003112 }
3113
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003114 void positionTaskInStackLocked(int taskId, int stackId, int position) {
3115 final TaskRecord task = anyTaskForIdLocked(taskId);
3116 if (task == null) {
3117 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
3118 return;
3119 }
3120 ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07003121 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003122 mWindowManager.positionTaskInStack(taskId, stackId, position);
3123 final boolean stackChanged = task.stack != null && task.stack != stack;
3124 if (stackChanged) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07003125 task.stack.removeTask(task, "moveTaskToStack", MOVING);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003126 }
3127 stack.positionTask(task, position, stackChanged);
3128 // The task might have already been running and its visibility needs to be synchronized with
3129 // the visibility of the stack / windows.
3130 stack.ensureActivitiesVisibleLocked(null, 0);
3131 resumeTopActivitiesLocked();
3132 }
3133
Craig Mautnerac6f8432013-07-17 13:24:59 -07003134 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003135 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003136 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3137 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003138 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3139 final ActivityStack stack = stacks.get(stackNdx);
3140 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003141 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003142 continue;
3143 }
3144 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003145 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
3146 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003147 continue;
3148 }
3149 final ActivityRecord ar = stack.findTaskLocked(r);
3150 if (ar != null) {
3151 return ar;
3152 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003153 }
3154 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003155 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07003156 return null;
3157 }
3158
3159 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003160 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3161 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003162 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3163 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
3164 if (ar != null) {
3165 return ar;
3166 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003167 }
3168 }
3169 return null;
3170 }
3171
Craig Mautner8d341ef2013-03-26 09:03:27 -07003172 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003173 scheduleSleepTimeout();
3174 if (!mGoingToSleep.isHeld()) {
3175 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003176 if (mLaunchingActivity.isHeld()) {
3177 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3178 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003179 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003180 mLaunchingActivity.release();
3181 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003182 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003183 }
Amith Yamasanice15e152013-09-19 12:30:32 -07003184 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003185 }
3186
3187 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003188 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003189
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003190 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003191 final long endTime = System.currentTimeMillis() + timeout;
3192 while (true) {
3193 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003194 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3195 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003196 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3197 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
3198 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003199 }
3200 if (cantShutdown) {
3201 long timeRemaining = endTime - System.currentTimeMillis();
3202 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003203 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003204 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003205 } catch (InterruptedException e) {
3206 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003207 } else {
3208 Slog.w(TAG, "Activity manager shutdown timed out");
3209 timedout = true;
3210 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003211 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003212 } else {
3213 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003214 }
3215 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003216
3217 // Force checkReadyForSleep to complete.
3218 mSleepTimeout = true;
3219 checkReadyForSleepLocked();
3220
Craig Mautner8d341ef2013-03-26 09:03:27 -07003221 return timedout;
3222 }
3223
3224 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003225 removeSleepTimeouts();
3226 if (mGoingToSleep.isHeld()) {
3227 mGoingToSleep.release();
3228 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003229 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3230 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003231 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3232 final ActivityStack stack = stacks.get(stackNdx);
3233 stack.awakeFromSleepingLocked();
3234 if (isFrontStack(stack)) {
3235 resumeTopActivitiesLocked();
3236 }
Craig Mautner5314a402013-09-26 12:40:16 -07003237 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003238 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003239 mGoingToSleepActivities.clear();
3240 }
3241
3242 void activitySleptLocked(ActivityRecord r) {
3243 mGoingToSleepActivities.remove(r);
3244 checkReadyForSleepLocked();
3245 }
3246
3247 void checkReadyForSleepLocked() {
3248 if (!mService.isSleepingOrShuttingDown()) {
3249 // Do not care.
3250 return;
3251 }
3252
3253 if (!mSleepTimeout) {
3254 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003255 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3256 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003257 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3258 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3259 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003260 }
3261
3262 if (mStoppingActivities.size() > 0) {
3263 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003264 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003265 + mStoppingActivities.size() + " activities");
3266 scheduleIdleLocked();
3267 dontSleep = true;
3268 }
3269
3270 if (mGoingToSleepActivities.size() > 0) {
3271 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003272 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003273 + mGoingToSleepActivities.size() + " activities");
3274 dontSleep = true;
3275 }
3276
3277 if (dontSleep) {
3278 return;
3279 }
3280 }
3281
Craig Mautnere0a38842013-12-16 16:14:02 -08003282 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3283 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003284 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3285 stacks.get(stackNdx).goToSleep();
3286 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003287 }
3288
3289 removeSleepTimeouts();
3290
3291 if (mGoingToSleep.isHeld()) {
3292 mGoingToSleep.release();
3293 }
3294 if (mService.mShuttingDown) {
3295 mService.notifyAll();
3296 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003297 }
3298
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003299 boolean reportResumedActivityLocked(ActivityRecord r) {
3300 final ActivityStack stack = r.task.stack;
3301 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003302 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003303 }
3304 if (allResumedActivitiesComplete()) {
3305 ensureActivitiesVisibleLocked(null, 0);
3306 mWindowManager.executeAppTransition();
3307 return true;
3308 }
3309 return false;
3310 }
3311
Craig Mautner8d341ef2013-03-26 09:03:27 -07003312 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003313 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3314 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003315 int stackNdx = stacks.size() - 1;
3316 while (stackNdx >= 0) {
3317 stacks.get(stackNdx).handleAppCrashLocked(app);
3318 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003319 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003320 }
3321 }
3322
Jose Lima4b6c6692014-08-12 17:41:12 -07003323 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003324 final ActivityStack stack = r.task.stack;
3325 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003326 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3327 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003328 return false;
3329 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003330 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003331 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3332 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003333
3334 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003335 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003336 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003337 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003338 return true;
3339 }
3340
3341 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003342 if (visible && top.fullscreen) {
3343 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003344 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3345 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3346 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3347 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003348 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003349 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3350 // Only the activity set as currently visible behind should actively reset its
3351 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003352 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3353 "requestVisibleBehind: returning visible=" + visible
3354 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3355 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003356 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003357 }
3358
Jose Lima4b6c6692014-08-12 17:41:12 -07003359 stack.setVisibleBehindActivity(visible ? r : null);
3360 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003361 // Make the activity immediately above r opaque.
3362 final ActivityRecord next = stack.findNextTranslucentActivity(r);
3363 if (next != null) {
3364 mService.convertFromTranslucent(next.appToken);
3365 }
3366 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003367 if (top.app != null && top.app.thread != null) {
3368 // Notify the top app of the change.
3369 try {
3370 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3371 } catch (RemoteException e) {
3372 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003373 }
3374 return true;
3375 }
3376
Craig Mautnerbb742462014-07-07 15:28:55 -07003377 // Called when WindowManager has finished animating the launchingBehind activity to the back.
3378 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
3379 r.mLaunchTaskBehind = false;
3380 final TaskRecord task = r.task;
3381 task.setLastThumbnail(task.stack.screenshotActivities(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003382 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08003383 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07003384 mWindowManager.setAppVisibility(r.appToken, false);
3385 }
3386
3387 void scheduleLaunchTaskBehindComplete(IBinder token) {
3388 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3389 }
3390
Craig Mautnerde4ef022013-04-07 19:01:33 -07003391 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
Craig Mautner580ea812013-04-25 12:58:38 -07003392 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08003393 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3394 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003395 final int topStackNdx = stacks.size() - 1;
3396 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3397 final ActivityStack stack = stacks.get(stackNdx);
Jose Lima729cb232014-05-05 15:59:40 -07003398 stack.ensureActivitiesVisibleLocked(starting, configChanges);
Craig Mautner580ea812013-04-25 12:58:38 -07003399 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003400 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003401 }
3402
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003403 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3404 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3405 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3406 final int topStackNdx = stacks.size() - 1;
3407 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3408 final ActivityStack stack = stacks.get(stackNdx);
3409 stack.clearOtherAppTimeTrackers(except);
3410 }
3411 }
3412 }
3413
Craig Mautner8d341ef2013-03-26 09:03:27 -07003414 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003415 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3416 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003417 final int numStacks = stacks.size();
3418 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3419 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003420 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003421 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003422 }
3423 }
3424
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003425 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3426 // Examine all activities currently running in the process.
3427 TaskRecord firstTask = null;
3428 // Tasks is non-null only if two or more tasks are found.
3429 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003430 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3431 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003432 ActivityRecord r = app.activities.get(i);
3433 // First, if we find an activity that is in the process of being destroyed,
3434 // then we just aren't going to do anything for now; we want things to settle
3435 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003436 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003437 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003438 return;
3439 }
3440 // Don't consider any activies that are currently not in a state where they
3441 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003442 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3443 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003444 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003445 continue;
3446 }
3447 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003448 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003449 + " from " + r);
3450 if (firstTask == null) {
3451 firstTask = r.task;
3452 } else if (firstTask != r.task) {
3453 if (tasks == null) {
3454 tasks = new ArraySet<>();
3455 tasks.add(firstTask);
3456 }
3457 tasks.add(r.task);
3458 }
3459 }
3460 }
3461 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003462 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003463 return;
3464 }
3465 // If we have activities in multiple tasks that are in a position to be destroyed,
3466 // let's iterate through the tasks and release the oldest one.
3467 final int numDisplays = mActivityDisplays.size();
3468 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3469 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3470 // Step through all stacks starting from behind, to hit the oldest things first.
3471 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3472 final ActivityStack stack = stacks.get(stackNdx);
3473 // Try to release activities in this stack; if we manage to, we are done.
3474 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3475 return;
3476 }
3477 }
3478 }
3479 }
3480
Amith Yamasani37a40c22015-06-17 13:25:42 -07003481 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003482 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003483 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003484 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003485
Craig Mautner858d8a62013-04-23 17:08:34 -07003486 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003487 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3488 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003489 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003490 final ActivityStack stack = stacks.get(stackNdx);
3491 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003492 TaskRecord task = stack.topTask();
3493 if (task != null) {
3494 mWindowManager.moveTaskToTop(task.taskId);
3495 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003496 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003497 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003498
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003499 ActivityStack stack = getStack(restoreStackId);
3500 if (stack == null) {
3501 stack = mHomeStack;
3502 }
3503 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003504 if (stack.isOnHomeDisplay()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003505 moveHomeStack(homeInFront, "switchUserOnHomeDisplay");
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003506 TaskRecord task = stack.topTask();
3507 if (task != null) {
3508 mWindowManager.moveTaskToTop(task.taskId);
3509 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003510 } else {
3511 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003512 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003513 }
Craig Mautner93529a42013-10-04 15:03:13 -07003514 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003515 }
3516
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003517 /**
3518 * Add background users to send boot completed events to.
3519 * @param userId The user being started in the background
3520 * @param uss The state object for the user.
3521 */
Amith Yamasani37a40c22015-06-17 13:25:42 -07003522 public void startBackgroundUserLocked(int userId, UserState uss) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003523 mStartingBackgroundUsers.add(uss);
3524 }
3525
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003526 /** Checks whether the userid is a profile of the current user. */
3527 boolean isCurrentProfileLocked(int userId) {
3528 if (userId == mCurrentUser) return true;
3529 for (int i = 0; i < mService.mCurrentProfileIds.length; i++) {
3530 if (mService.mCurrentProfileIds[i] == userId) return true;
3531 }
3532 return false;
3533 }
3534
Chong Zhang45c25ce2015-08-10 22:18:26 -07003535 /** Checks whether the activity should be shown for current user. */
3536 boolean okToShowLocked(ActivityRecord r) {
3537 return r != null && (isCurrentProfileLocked(r.userId)
3538 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
3539 }
3540
Craig Mautnerde4ef022013-04-07 19:01:33 -07003541 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003542 ArrayList<ActivityRecord> stops = null;
3543
3544 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003545 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3546 ActivityRecord s = mStoppingActivities.get(activityNdx);
3547 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003548 if (DEBUG_ALL) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003549 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3550 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003551 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003552 if (s.finishing) {
3553 // If this activity is finishing, it is sitting on top of
3554 // everyone else but we now know it is no longer needed...
3555 // so get rid of it. Otherwise, we need to go through the
3556 // normal flow and hide it once we determine that it is
3557 // hidden by the activities in front of it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003558 if (DEBUG_ALL) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003559 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003560 }
3561 }
Craig Mautner8c14c152015-01-15 17:32:07 -08003562 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003563 if (DEBUG_ALL) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003564 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003565 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003566 }
3567 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003568 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003569 }
3570 }
3571
3572 return stops;
3573 }
3574
Craig Mautnercf910b02013-04-23 11:23:27 -07003575 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003576 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3577 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3578 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3579 final ActivityStack stack = stacks.get(stackNdx);
3580 final ActivityRecord r = stack.topRunningActivityLocked(null);
3581 final ActivityState state = r == null ? DESTROYED : r.state;
3582 if (isFrontStack(stack)) {
3583 if (r == null) Slog.e(TAG,
3584 "validateTop...: null top activity, stack=" + stack);
3585 else {
3586 final ActivityRecord pausing = stack.mPausingActivity;
3587 if (pausing != null && pausing == r) Slog.e(TAG,
3588 "validateTop...: top stack has pausing activity r=" + r
3589 + " state=" + state);
3590 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3591 "validateTop...: activity in front not resumed r=" + r
3592 + " state=" + state);
3593 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003594 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003595 final ActivityRecord resumed = stack.mResumedActivity;
3596 if (resumed != null && resumed == r) Slog.e(TAG,
3597 "validateTop...: back stack has resumed activity r=" + r
3598 + " state=" + state);
3599 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3600 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003601 }
3602 }
3603 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003604 }
3605
Craig Mautnere0570202015-05-13 13:06:11 -07003606 private String lockTaskModeToString() {
3607 switch (mLockTaskModeState) {
3608 case LOCK_TASK_MODE_LOCKED:
3609 return "LOCKED";
3610 case LOCK_TASK_MODE_PINNED:
3611 return "PINNED";
3612 case LOCK_TASK_MODE_NONE:
3613 return "NONE";
3614 default: return "unknown=" + mLockTaskModeState;
3615 }
3616 }
3617
Craig Mautner27084302013-03-25 08:05:25 -07003618 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003619 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003620 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003621 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
3622 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
3623 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003624 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003625 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
3626 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3627 if (packages.size() > 0) {
3628 pw.println(" mLockTaskPackages (userId:packages)=");
3629 for (int i = 0; i < packages.size(); ++i) {
3630 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3631 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3632 }
3633 }
3634 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003635 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003636
Craig Mautner20e72272013-04-01 13:45:53 -07003637 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003638 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003639 }
3640
Dianne Hackborn390517b2013-05-30 15:03:32 -07003641 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3642 boolean needSep, String prefix) {
3643 if (activity != null) {
3644 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3645 if (needSep) {
3646 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003647 }
3648 pw.print(prefix);
3649 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003650 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003651 }
3652 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003653 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003654 }
3655
Craig Mautner8d341ef2013-03-26 09:03:27 -07003656 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3657 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003658 boolean printed = false;
3659 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003660 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3661 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003662 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003663 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003664 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003665 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003666 final ActivityStack stack = stacks.get(stackNdx);
3667 StringBuilder stackHeader = new StringBuilder(128);
3668 stackHeader.append(" Stack #");
3669 stackHeader.append(stack.mStackId);
3670 stackHeader.append(":");
3671 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3672 needSep, stackHeader.toString());
3673 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3674 !dumpAll, false, dumpPackage, true,
3675 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003676
Craig Mautner4a1cb222013-12-04 16:14:06 -08003677 needSep = printed;
3678 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3679 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003680 if (pr) {
3681 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003682 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003683 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003684 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3685 " mResumedActivity: ");
3686 if (pr) {
3687 printed = true;
3688 needSep = false;
3689 }
3690 if (dumpAll) {
3691 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3692 " mLastPausedActivity: ");
3693 if (pr) {
3694 printed = true;
3695 needSep = true;
3696 }
3697 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3698 needSep, " mLastNoHistoryActivity: ");
3699 }
3700 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003701 }
3702 }
3703
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003704 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3705 false, dumpPackage, true, " Activities waiting to finish:", null);
3706 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3707 false, dumpPackage, true, " Activities waiting to stop:", null);
3708 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3709 false, dumpPackage, true, " Activities waiting for another to become visible:",
3710 null);
3711 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3712 false, dumpPackage, true, " Activities waiting to sleep:", null);
3713 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3714 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003715
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003716 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003717 }
3718
Dianne Hackborn390517b2013-05-30 15:03:32 -07003719 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003720 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003721 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003722 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003723 String innerPrefix = null;
3724 String[] args = null;
3725 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003726 for (int i=list.size()-1; i>=0; i--) {
3727 final ActivityRecord r = list.get(i);
3728 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3729 continue;
3730 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003731 if (innerPrefix == null) {
3732 innerPrefix = prefix + " ";
3733 args = new String[0];
3734 }
3735 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003736 final boolean full = !brief && (complete || !r.isInHistory());
3737 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003738 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003739 needNL = false;
3740 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003741 if (header1 != null) {
3742 pw.println(header1);
3743 header1 = null;
3744 }
3745 if (header2 != null) {
3746 pw.println(header2);
3747 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003748 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003749 if (lastTask != r.task) {
3750 lastTask = r.task;
3751 pw.print(prefix);
3752 pw.print(full ? "* " : " ");
3753 pw.println(lastTask);
3754 if (full) {
3755 lastTask.dump(pw, prefix + " ");
3756 } else if (complete) {
3757 // Complete + brief == give a summary. Isn't that obvious?!?
3758 if (lastTask.intent != null) {
3759 pw.print(prefix); pw.print(" ");
3760 pw.println(lastTask.intent.toInsecureStringWithClip());
3761 }
3762 }
3763 }
3764 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3765 pw.print(" #"); pw.print(i); pw.print(": ");
3766 pw.println(r);
3767 if (full) {
3768 r.dump(pw, innerPrefix);
3769 } else if (complete) {
3770 // Complete + brief == give a summary. Isn't that obvious?!?
3771 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3772 if (r.app != null) {
3773 pw.print(innerPrefix); pw.println(r.app);
3774 }
3775 }
3776 if (client && r.app != null && r.app.thread != null) {
3777 // flush anything that is already in the PrintWriter since the thread is going
3778 // to write to the file descriptor directly
3779 pw.flush();
3780 try {
3781 TransferPipe tp = new TransferPipe();
3782 try {
3783 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3784 r.appToken, innerPrefix, args);
3785 // Short timeout, since blocking here can
3786 // deadlock with the application.
3787 tp.go(fd, 2000);
3788 } finally {
3789 tp.kill();
3790 }
3791 } catch (IOException e) {
3792 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3793 } catch (RemoteException e) {
3794 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3795 }
3796 needNL = true;
3797 }
3798 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003799 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003800 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003801
Craig Mautnerf3333272013-04-22 10:55:53 -07003802 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003803 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3804 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003805 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3806 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003807 }
3808
3809 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003810 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003811 }
3812
3813 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003814 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3815 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003816 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3817 }
3818
Craig Mautner05d29032013-05-03 13:40:13 -07003819 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003820 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3821 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3822 }
Craig Mautner05d29032013-05-03 13:40:13 -07003823 }
3824
Craig Mautner0eea92c2013-05-16 13:35:39 -07003825 void removeSleepTimeouts() {
3826 mSleepTimeout = false;
3827 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3828 }
3829
3830 final void scheduleSleepTimeout() {
3831 removeSleepTimeouts();
3832 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3833 }
3834
Craig Mautner4a1cb222013-12-04 16:14:06 -08003835 @Override
3836 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003837 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003838 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3839 }
3840
3841 @Override
3842 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003843 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003844 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3845 }
3846
3847 @Override
3848 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003849 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003850 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3851 }
3852
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003853 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003854 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003855 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003856 newDisplay = mActivityDisplays.get(displayId) == null;
3857 if (newDisplay) {
3858 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003859 if (activityDisplay.mDisplay == null) {
3860 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3861 return;
3862 }
Craig Mautner4504de52013-12-20 09:06:56 -08003863 mActivityDisplays.put(displayId, activityDisplay);
3864 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003865 }
Craig Mautner4504de52013-12-20 09:06:56 -08003866 if (newDisplay) {
3867 mWindowManager.onDisplayAdded(displayId);
3868 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003869 }
3870
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003871 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003872 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003873 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3874 if (activityDisplay != null) {
3875 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003876 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003877 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003878 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003879 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003880 }
3881 }
3882 mWindowManager.onDisplayRemoved(displayId);
3883 }
3884
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003885 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003886 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003887 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3888 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003889 // TODO: Update the bounds.
3890 }
3891 }
3892 mWindowManager.onDisplayChanged(displayId);
3893 }
3894
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003895 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003896 StackInfo info = new StackInfo();
3897 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3898 info.displayId = Display.DEFAULT_DISPLAY;
3899 info.stackId = stack.mStackId;
3900
3901 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3902 final int numTasks = tasks.size();
3903 int[] taskIds = new int[numTasks];
3904 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003905 Rect[] taskBounds = new Rect[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003906 for (int i = 0; i < numTasks; ++i) {
3907 final TaskRecord task = tasks.get(i);
3908 taskIds[i] = task.taskId;
3909 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3910 : task.realActivity != null ? task.realActivity.flattenToString()
3911 : task.getTopActivity() != null ? task.getTopActivity().packageName
3912 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003913 taskBounds[i] = new Rect();
3914 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003915 }
3916 info.taskIds = taskIds;
3917 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003918 info.taskBounds = taskBounds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003919 return info;
3920 }
3921
3922 StackInfo getStackInfoLocked(int stackId) {
3923 ActivityStack stack = getStack(stackId);
3924 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003925 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003926 }
3927 return null;
3928 }
3929
3930 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003931 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003932 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3933 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003934 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003935 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003936 }
3937 }
3938 return list;
3939 }
3940
Craig Mautner15df08a2015-04-01 12:17:18 -07003941 TaskRecord getLockedTaskLocked() {
3942 final int top = mLockTaskModeTasks.size() - 1;
3943 if (top >= 0) {
3944 return mLockTaskModeTasks.get(top);
3945 }
3946 return null;
3947 }
3948
3949 boolean isLockedTask(TaskRecord task) {
3950 return mLockTaskModeTasks.contains(task);
3951 }
3952
3953 boolean isLastLockedTask(TaskRecord task) {
3954 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3955 }
3956
3957 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003958 if (!mLockTaskModeTasks.remove(task)) {
3959 return;
3960 }
3961 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3962 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003963 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003964 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3965 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003966 final Message lockTaskMsg = Message.obtain();
3967 lockTaskMsg.arg1 = task.userId;
3968 lockTaskMsg.what = LOCK_TASK_END_MSG;
3969 mHandler.sendMessage(lockTaskMsg);
3970 }
3971 }
3972
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003973 void showLockTaskToast() {
3974 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04003975 }
3976
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003977 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3978 if (mLockTaskModeTasks.contains(task)) {
3979 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3980 }
3981 }
3982
Craig Mautner432f64e2015-05-20 14:59:57 -07003983 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3984 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003985 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003986 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003987 final TaskRecord lockedTask = getLockedTaskLocked();
3988 if (lockedTask != null) {
3989 removeLockedTaskLocked(lockedTask);
3990 if (!mLockTaskModeTasks.isEmpty()) {
3991 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003992 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3993 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003994 lockedTask.performClearTaskLocked();
3995 resumeTopActivitiesLocked();
3996 return;
3997 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003998 }
Craig Mautnere0570202015-05-13 13:06:11 -07003999 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4000 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08004001 return;
4002 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004003
4004 // Should have already been checked, but do it again.
4005 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07004006 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4007 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08004008 return;
4009 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004010 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07004011 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07004012 return;
4013 }
4014
4015 if (mLockTaskModeTasks.isEmpty()) {
4016 // First locktask.
4017 final Message lockTaskMsg = Message.obtain();
4018 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
4019 lockTaskMsg.arg1 = task.userId;
4020 lockTaskMsg.what = LOCK_TASK_START_MSG;
4021 lockTaskMsg.arg2 = lockTaskModeState;
4022 mHandler.sendMessage(lockTaskMsg);
4023 }
4024 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07004025 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
4026 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07004027 mLockTaskModeTasks.remove(task);
4028 mLockTaskModeTasks.add(task);
4029
4030 if (task.mLockTaskUid == -1) {
4031 task.mLockTaskUid = task.mCallingUid;
4032 }
Craig Mautner432f64e2015-05-20 14:59:57 -07004033
4034 if (andResume) {
4035 findTaskToMoveToFrontLocked(task, 0, null, reason);
4036 resumeTopActivitiesLocked();
4037 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004038 }
4039
4040 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04004041 return isLockTaskModeViolation(task, false);
4042 }
4043
4044 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
4045 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004046 return false;
4047 }
4048 final int lockTaskAuth = task.mLockTaskAuth;
4049 switch (lockTaskAuth) {
4050 case LOCK_TASK_AUTH_DONT_LOCK:
4051 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01004052 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07004053 case LOCK_TASK_AUTH_LAUNCHABLE:
4054 case LOCK_TASK_AUTH_WHITELISTED:
4055 return false;
4056 case LOCK_TASK_AUTH_PINNABLE:
4057 // Pinnable tasks can't be launched on top of locktask tasks.
4058 return !mLockTaskModeTasks.isEmpty();
4059 default:
4060 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
4061 return true;
4062 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004063 }
4064
Craig Mautner15df08a2015-04-01 12:17:18 -07004065 void onLockTaskPackagesUpdatedLocked() {
4066 boolean didSomething = false;
4067 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
4068 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01004069 final boolean wasWhitelisted =
4070 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4071 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07004072 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01004073 final boolean isWhitelisted =
4074 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4075 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
4076 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004077 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07004078 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
4079 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07004080 removeLockedTaskLocked(lockedTask);
4081 lockedTask.performClearTaskLocked();
4082 didSomething = true;
4083 }
4084 }
4085 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4086 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4087 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4088 final ActivityStack stack = stacks.get(stackNdx);
4089 stack.onLockTaskPackagesUpdatedLocked();
4090 }
4091 }
Craig Mautnere0570202015-05-13 13:06:11 -07004092 final ActivityRecord r = topRunningActivityLocked();
4093 final TaskRecord task = r != null ? r.task : null;
4094 if (mLockTaskModeTasks.isEmpty() && task != null
4095 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4096 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004097 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4098 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4099 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4100 false);
4101 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004102 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004103 if (didSomething) {
4104 resumeTopActivitiesLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004105 }
4106 }
4107
Benjamin Franz43261142015-02-11 15:59:44 +00004108 int getLockTaskModeState() {
4109 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004110 }
4111
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004112 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004113
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004114 public ActivityStackSupervisorHandler(Looper looper) {
4115 super(looper);
4116 }
4117
Craig Mautnerf3333272013-04-22 10:55:53 -07004118 void activityIdleInternal(ActivityRecord r) {
4119 synchronized (mService) {
4120 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
4121 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004122 }
4123
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004124 @Override
4125 public void handleMessage(Message msg) {
4126 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004127 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004128 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4129 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004130 if (mService.mDidDexOpt) {
4131 mService.mDidDexOpt = false;
4132 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4133 nmsg.obj = msg.obj;
4134 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4135 return;
4136 }
4137 // We don't at this point know if the activity is fullscreen,
4138 // so we need to be conservative and assume it isn't.
4139 activityIdleInternal((ActivityRecord)msg.obj);
4140 } break;
4141 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004142 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004143 activityIdleInternal((ActivityRecord)msg.obj);
4144 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004145 case RESUME_TOP_ACTIVITY_MSG: {
4146 synchronized (mService) {
4147 resumeTopActivitiesLocked();
4148 }
4149 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004150 case SLEEP_TIMEOUT_MSG: {
4151 synchronized (mService) {
4152 if (mService.isSleepingOrShuttingDown()) {
4153 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4154 mSleepTimeout = true;
4155 checkReadyForSleepLocked();
4156 }
4157 }
4158 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004159 case LAUNCH_TIMEOUT_MSG: {
4160 if (mService.mDidDexOpt) {
4161 mService.mDidDexOpt = false;
4162 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4163 return;
4164 }
4165 synchronized (mService) {
4166 if (mLaunchingActivity.isHeld()) {
4167 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4168 if (VALIDATE_WAKE_LOCK_CALLER
4169 && Binder.getCallingUid() != Process.myUid()) {
4170 throw new IllegalStateException("Calling must be system uid");
4171 }
4172 mLaunchingActivity.release();
4173 }
4174 }
4175 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004176 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004177 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004178 } break;
4179 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004180 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004181 } break;
4182 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004183 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004184 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004185 case CONTAINER_CALLBACK_VISIBILITY: {
4186 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004187 final IActivityContainerCallback callback = container.mCallback;
4188 if (callback != null) {
4189 try {
4190 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4191 } catch (RemoteException e) {
4192 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004193 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004194 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004195 case LOCK_TASK_START_MSG: {
4196 // When lock task starts, we disable the status bars.
4197 try {
Jason Monk62515be2014-05-21 16:06:19 -04004198 if (mLockTaskNotify == null) {
4199 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004200 }
Jason Monk62515be2014-05-21 16:06:19 -04004201 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004202 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004203 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004204 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004205 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004206 flags = StatusBarManager.DISABLE_MASK
4207 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004208 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004209 flags = StatusBarManager.DISABLE_MASK
4210 & (~StatusBarManager.DISABLE_BACK)
4211 & (~StatusBarManager.DISABLE_HOME)
4212 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004213 }
4214 getStatusBarService().disable(flags, mToken,
4215 mService.mContext.getPackageName());
4216 }
4217 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004218 if (getDevicePolicyManager() != null) {
4219 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004220 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004221 }
justinzhang5286d3f2014-05-12 17:06:01 -04004222 } catch (RemoteException ex) {
4223 throw new RuntimeException(ex);
4224 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004225 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004226 case LOCK_TASK_END_MSG: {
4227 // When lock task ends, we enable the status bars.
4228 try {
Jason Monk62515be2014-05-21 16:06:19 -04004229 if (getStatusBarService() != null) {
4230 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4231 mService.mContext.getPackageName());
4232 }
4233 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004234 if (getDevicePolicyManager() != null) {
4235 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4236 msg.arg1);
4237 }
Jason Monk62515be2014-05-21 16:06:19 -04004238 if (mLockTaskNotify == null) {
4239 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4240 }
4241 mLockTaskNotify.show(false);
4242 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004243 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004244 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004245 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004246 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004247 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04004248 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04004249 new LockPatternUtils(mService.mContext)
4250 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004251 }
4252 } catch (SettingNotFoundException e) {
4253 // No setting, don't lock.
4254 }
justinzhang5286d3f2014-05-12 17:06:01 -04004255 } catch (RemoteException ex) {
4256 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004257 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004258 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004259 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004260 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004261 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4262 if (mLockTaskNotify == null) {
4263 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4264 }
4265 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4266 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004267 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4268 final ActivityContainer container = (ActivityContainer) msg.obj;
4269 final IActivityContainerCallback callback = container.mCallback;
4270 if (callback != null) {
4271 try {
4272 callback.onAllActivitiesComplete(container.asBinder());
4273 } catch (RemoteException e) {
4274 }
4275 }
4276 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004277 case LAUNCH_TASK_BEHIND_COMPLETE: {
4278 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004279 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004280 if (r != null) {
4281 handleLaunchTaskBehindCompleteLocked(r);
4282 }
4283 }
4284 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004285 }
4286 }
4287 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004288
Ying Wangb081a592014-04-22 15:20:16 -07004289 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07004290 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07004291 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004292 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004293 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004294 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004295 ActivityRecord mParentActivity = null;
4296 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004297
Craig Mautnere3a00d72014-04-16 08:31:19 -07004298 boolean mVisible = true;
4299
Craig Mautner4a1cb222013-12-04 16:14:06 -08004300 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004301 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004302
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004303 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4304 final static int CONTAINER_STATE_NO_SURFACE = 1;
4305 final static int CONTAINER_STATE_FINISHING = 2;
4306 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4307
4308 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004309 synchronized (mService) {
4310 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004311 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004312 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004313 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004314 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004315 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004316
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004317 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004318 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004319 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08004320 mActivityDisplay = activityDisplay;
4321 mStack.mDisplayId = activityDisplay.mDisplayId;
4322 mStack.mStacks = activityDisplay.mStacks;
4323
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004324 activityDisplay.attachActivities(mStack, onTop);
4325 mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004326 }
4327
4328 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004329 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004330 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004331 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4332 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004333 return;
4334 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004335 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004336 }
4337 }
4338
4339 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004340 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004341 synchronized (mService) {
4342 if (mActivityDisplay != null) {
4343 return mActivityDisplay.mDisplayId;
4344 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004345 }
4346 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004347 }
4348
Jeff Brownca9bc702014-02-11 14:32:56 -08004349 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004350 public int getStackId() {
4351 synchronized (mService) {
4352 return mStackId;
4353 }
4354 }
4355
4356 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004357 public boolean injectEvent(InputEvent event) {
4358 final long origId = Binder.clearCallingIdentity();
4359 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004360 synchronized (mService) {
4361 if (mActivityDisplay != null) {
4362 return mInputManagerInternal.injectInputEvent(event,
4363 mActivityDisplay.mDisplayId,
4364 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4365 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004366 }
4367 return false;
4368 } finally {
4369 Binder.restoreCallingIdentity(origId);
4370 }
4371 }
4372
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004373 @Override
4374 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004375 synchronized (mService) {
4376 if (mContainerState == CONTAINER_STATE_FINISHING) {
4377 return;
4378 }
4379 mContainerState = CONTAINER_STATE_FINISHING;
4380
Craig Mautnerd163e752014-06-13 17:18:47 -07004381 long origId = Binder.clearCallingIdentity();
4382 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004383 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07004384 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004385 } finally {
4386 Binder.restoreCallingIdentity(origId);
4387 }
4388 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004389 }
4390
Craig Mautner60257702014-09-17 15:02:33 -07004391 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004392 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004393 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004394 if (mActivityDisplay != null) {
4395 mActivityDisplay.detachActivitiesLocked(mStack);
4396 mActivityDisplay = null;
4397 mStack.mDisplayId = -1;
4398 mStack.mStacks = null;
Craig Mautnerdf88d732014-01-27 09:21:32 -08004399 mWindowManager.detachStack(mStackId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004400 }
4401 }
4402
4403 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004404 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004405 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnerb9168362015-02-26 20:40:19 -08004406 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004407 Binder.getCallingUid(), mCurrentUser, false,
4408 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004409
Craig Mautnere0a38842013-12-16 16:14:02 -08004410 // TODO: Switch to user app stacks here.
4411 String mimeType = intent.getType();
Craig Mautnerb9168362015-02-26 20:40:19 -08004412 final Uri data = intent.getData();
4413 if (mimeType == null && data != null && "content".equals(data.getScheme())) {
4414 mimeType = mService.getProviderMimeType(data, userId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004415 }
Craig Mautnerb9168362015-02-26 20:40:19 -08004416 checkEmbeddedAllowedInner(userId, intent, mimeType);
4417
4418 intent.addFlags(FORCE_NEW_TASK_FLAGS);
4419 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07004420 0, 0, null, null, null, null, false, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004421 }
4422
4423 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004424 public final int startActivityIntentSender(IIntentSender intentSender)
4425 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004426 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4427
4428 if (!(intentSender instanceof PendingIntentRecord)) {
4429 throw new IllegalArgumentException("Bad PendingIntent object");
4430 }
4431
Craig Mautnerb9168362015-02-26 20:40:19 -08004432 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004433 Binder.getCallingUid(), mCurrentUser, false,
4434 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004435
4436 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4437 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4438 pendingIntent.key.requestResolvedType);
4439
4440 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4441 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4442 }
4443
4444 private void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004445 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004446 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004447 throw new SecurityException(
4448 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4449 }
4450 }
4451
Craig Mautnerdf88d732014-01-27 09:21:32 -08004452 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004453 public IBinder asBinder() {
4454 return this;
4455 }
4456
Craig Mautner4504de52013-12-20 09:06:56 -08004457 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004458 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004459 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004460 }
4461
Craig Mautner4a1cb222013-12-04 16:14:06 -08004462 ActivityStackSupervisor getOuter() {
4463 return ActivityStackSupervisor.this;
4464 }
4465
Craig Mautnerd163e752014-06-13 17:18:47 -07004466 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004467 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004468 }
4469
4470 void getBounds(Point outBounds) {
Craig Mautnerd163e752014-06-13 17:18:47 -07004471 synchronized (mService) {
4472 if (mActivityDisplay != null) {
4473 mActivityDisplay.getBounds(outBounds);
4474 } else {
4475 outBounds.set(0, 0);
4476 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004477 }
4478 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004479
Craig Mautner6985bad2014-04-21 15:22:06 -07004480 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004481 void setVisible(boolean visible) {
4482 if (mVisible != visible) {
4483 mVisible = visible;
4484 if (mCallback != null) {
4485 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4486 0 /* unused */, this).sendToTarget();
4487 }
4488 }
4489 }
4490
Craig Mautner6985bad2014-04-21 15:22:06 -07004491 void setDrawn() {
4492 }
4493
Craig Mautner1b4bf852014-05-26 15:06:32 -07004494 // You can always start a new task on a regular ActivityStack.
4495 boolean isEligibleForNewTasks() {
4496 return true;
4497 }
4498
Craig Mautnerd163e752014-06-13 17:18:47 -07004499 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004500 detachLocked();
4501 deleteActivityContainer(this);
4502 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004503 }
4504
Craig Mautner34b73df2014-01-12 21:11:08 -08004505 @Override
4506 public String toString() {
4507 return mIdString + (mActivityDisplay == null ? "N" : "A");
4508 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004509 }
4510
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004511 private class VirtualActivityContainer extends ActivityContainer {
4512 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004513 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004514
4515 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4516 super(getNextStackId());
4517 mParentActivity = parent;
4518 mCallback = callback;
4519 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004520 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004521 }
4522
4523 @Override
4524 public void setSurface(Surface surface, int width, int height, int density) {
4525 super.setSurface(surface, width, height, density);
4526
4527 synchronized (mService) {
4528 final long origId = Binder.clearCallingIdentity();
4529 try {
4530 setSurfaceLocked(surface, width, height, density);
4531 } finally {
4532 Binder.restoreCallingIdentity(origId);
4533 }
4534 }
4535 }
4536
4537 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4538 if (mContainerState == CONTAINER_STATE_FINISHING) {
4539 return;
4540 }
4541 VirtualActivityDisplay virtualActivityDisplay =
4542 (VirtualActivityDisplay) mActivityDisplay;
4543 if (virtualActivityDisplay == null) {
4544 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004545 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004546 mActivityDisplay = virtualActivityDisplay;
4547 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004548 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004549 }
4550
4551 if (mSurface != null) {
4552 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004553 }
4554
Craig Mautner6985bad2014-04-21 15:22:06 -07004555 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004556 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004557 mStack.resumeTopActivityLocked(null);
4558 } else {
4559 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004560 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004561 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004562 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004563 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004564 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004565
Craig Mautnerd163e752014-06-13 17:18:47 -07004566 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004567
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004568 if (DEBUG_STACK) Slog.d(TAG_STACK,
4569 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004570 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004571
Craig Mautner6985bad2014-04-21 15:22:06 -07004572 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004573 boolean isAttachedLocked() {
4574 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004575 }
4576
4577 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004578 void setDrawn() {
4579 synchronized (mService) {
4580 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004581 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004582 }
4583 }
4584
Craig Mautner1b4bf852014-05-26 15:06:32 -07004585 // Never start a new task on an ActivityView if it isn't explicitly specified.
4586 @Override
4587 boolean isEligibleForNewTasks() {
4588 return false;
4589 }
4590
Craig Mautnerd163e752014-06-13 17:18:47 -07004591 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004592 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004593 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4594 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4595 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4596 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4597 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004598 }
4599 }
4600
Craig Mautner4a1cb222013-12-04 16:14:06 -08004601 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4602 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004603 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004604 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004605 int mDisplayId;
4606 Display mDisplay;
4607 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004608
Craig Mautner4a1cb222013-12-04 16:14:06 -08004609 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4610 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08004611 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004612
Jose Lima4b6c6692014-08-12 17:41:12 -07004613 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004614
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004615 ActivityDisplay() {
4616 }
Craig Mautner4504de52013-12-20 09:06:56 -08004617
Craig Mautner1a70a162014-09-13 12:09:31 -07004618 // After instantiation, check that mDisplay is not null before using this. The alternative
4619 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004620 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004621 final Display display = mDisplayManager.getDisplay(displayId);
4622 if (display == null) {
4623 return;
4624 }
4625 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004626 }
4627
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004628 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004629 mDisplay = display;
4630 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004631 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004632 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004633
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004634 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004635 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004636 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
4637 + " onTop=" + onTop);
4638 if (onTop) {
4639 mStacks.add(stack);
4640 } else {
4641 mStacks.add(0, stack);
4642 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004643 }
4644
Craig Mautnere0a38842013-12-16 16:14:02 -08004645 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004646 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004647 + " from displayId=" + mDisplayId);
4648 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004649 }
4650
4651 void getBounds(Point bounds) {
4652 mDisplay.getDisplayInfo(mDisplayInfo);
4653 bounds.x = mDisplayInfo.appWidth;
4654 bounds.y = mDisplayInfo.appHeight;
4655 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004656
Jose Lima4b6c6692014-08-12 17:41:12 -07004657 void setVisibleBehindActivity(ActivityRecord r) {
4658 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004659 }
4660
Jose Lima4b6c6692014-08-12 17:41:12 -07004661 boolean hasVisibleBehindActivity() {
4662 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004663 }
4664
Craig Mautner34b73df2014-01-12 21:11:08 -08004665 @Override
4666 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004667 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4668 }
4669 }
4670
4671 class VirtualActivityDisplay extends ActivityDisplay {
4672 VirtualDisplay mVirtualDisplay;
4673
Craig Mautner6985bad2014-04-21 15:22:06 -07004674 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004675 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004676 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4677 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4678 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4679 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004680
4681 init(mVirtualDisplay.getDisplay());
4682
4683 mWindowManager.handleDisplayAdded(mDisplayId);
4684 }
4685
4686 void setSurface(Surface surface) {
4687 if (mVirtualDisplay != null) {
4688 mVirtualDisplay.setSurface(surface);
4689 }
4690 }
4691
4692 @Override
4693 void detachActivitiesLocked(ActivityStack stack) {
4694 super.detachActivitiesLocked(stack);
4695 if (mVirtualDisplay != null) {
4696 mVirtualDisplay.release();
4697 mVirtualDisplay = null;
4698 }
4699 }
4700
4701 @Override
4702 public String toString() {
4703 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004704 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004705 }
Jose Lima58e66d62014-05-27 20:00:27 -07004706
4707 private boolean isLeanbackOnlyDevice() {
4708 boolean onLeanbackOnly = false;
4709 try {
4710 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
4711 PackageManager.FEATURE_LEANBACK_ONLY);
4712 } catch (RemoteException e) {
4713 // noop
4714 }
4715
4716 return onLeanbackOnly;
4717 }
Craig Mautner27084302013-03-25 08:05:25 -07004718}