blob: 0bdeed771ba80a33ab8b613b10a666b6307eb39d [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
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700187 // Force the focus to change to the stack we are moving a task to..
188 static final boolean FORCE_FOCUS = true;
189
Svetoslav7008b512015-06-24 18:47:07 -0700190 // Activity actions an app cannot start if it uses a permission which is not granted.
191 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
192 new ArrayMap<>();
193 static {
194 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
195 Manifest.permission.CAMERA);
196 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
197 Manifest.permission.CAMERA);
198 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
199 Manifest.permission.CALL_PHONE);
200 }
201
Svet Ganov99b60432015-06-27 13:15:22 -0700202 /** Action restriction: launching the activity is not restricted. */
203 private static final int ACTIVITY_RESTRICTION_NONE = 0;
204 /** Action restriction: launching the activity is restricted by a permission. */
205 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
206 /** Action restriction: launching the activity is restricted by an app op. */
207 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700208
justinzhang5286d3f2014-05-12 17:06:01 -0400209 /** Status Bar Service **/
210 private IBinder mToken = new Binder();
211 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400212 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400213
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700214 // For debugging to make sure the caller when acquiring/releasing our
215 // wake lock is the system process.
216 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700217
Craig Mautner27084302013-03-25 08:05:25 -0700218 final ActivityManagerService mService;
219
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800220 private final RecentTasks mRecentTasks;
221
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700222 final ActivityStackSupervisorHandler mHandler;
223
224 /** Short cut */
225 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800226 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700227
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700228 /** Counter for next free stack ID to use for dynamic activity stacks. */
229 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700230
231 /** Task identifier that activities are currently being started in. Incremented each time a
232 * new task is created. */
233 private int mCurTaskId = 0;
234
Craig Mautner2420ead2013-04-01 17:13:20 -0700235 /** The current user */
236 private int mCurrentUser;
237
Craig Mautnere0a38842013-12-16 16:14:02 -0800238 /** The stack containing the launcher app. Assumed to always be attached to
239 * Display.DEFAULT_DISPLAY. */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700240 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700241
Craig Mautnere0a38842013-12-16 16:14:02 -0800242 /** The stack currently receiving input or launching the next activity. */
Craig Mautner29219d92013-04-16 20:19:12 -0700243 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700244
Craig Mautner4a1cb222013-12-04 16:14:06 -0800245 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
246 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800247 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800248 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700249
250 /** List of activities that are waiting for a new activity to become visible before completing
251 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800252 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700253
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700254 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800255 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700256
257 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800258 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700259
Craig Mautnerde4ef022013-04-07 19:01:33 -0700260 /** List of activities that are ready to be stopped, but waiting for the next activity to
261 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800262 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700263
Craig Mautnerf3333272013-04-22 10:55:53 -0700264 /** List of activities that are ready to be finished, but waiting for the previous activity to
265 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800266 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700267
Craig Mautner0eea92c2013-05-16 13:35:39 -0700268 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800269 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700270
Craig Mautnerf3333272013-04-22 10:55:53 -0700271 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700272 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700273
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700274 /** Used to queue up any background users being started */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700275 final ArrayList<UserState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700276
Craig Mautnerde4ef022013-04-07 19:01:33 -0700277 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
278 * is being brought in front of us. */
279 boolean mUserLeaving = false;
280
Craig Mautner0eea92c2013-05-16 13:35:39 -0700281 /** Set when we have taken too long waiting to go to sleep. */
282 boolean mSleepTimeout = false;
283
Jose Lima58e66d62014-05-27 20:00:27 -0700284 /** Indicates if we are running on a Leanback-only (TV) device. Only initialized after
285 * setWindowManager is called. **/
286 private boolean mLeanbackOnlyDevice;
287
Craig Mautner0eea92c2013-05-16 13:35:39 -0700288 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700289 * We don't want to allow the device to go to sleep while in the process
290 * of launching an activity. This is primarily to allow alarm intent
291 * receivers to launch an activity and get that to run before the device
292 * goes back to sleep.
293 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700294 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700295
296 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700297 * Set when the system is going to sleep, until we have
298 * successfully paused the current activity and released our wake lock.
299 * At that point the system is allowed to actually sleep.
300 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700301 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700302
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700303 /** Stack id of the front stack when user switched, indexed by userId. */
304 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700305
Craig Mautner4504de52013-12-20 09:06:56 -0800306 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800307 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700308 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800309
310 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700311 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800312
Jeff Brownca9bc702014-02-11 14:32:56 -0800313 InputManagerInternal mInputManagerInternal;
314
Craig Mautner15df08a2015-04-01 12:17:18 -0700315 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
316 * may be finished until there is only one entry left. If this is empty the system is not
317 * in lockTask mode. */
318 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000319 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700320 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
321 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000322 */
323 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400324 /**
325 * Notifies the user when entering/exiting lock-task.
326 */
327 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800328
Craig Mautner15df08a2015-04-01 12:17:18 -0700329 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches = new ArrayList<>();
Craig Mautneree36c772014-07-16 14:56:05 -0700330
Craig Mautner42d04db2014-11-06 12:13:23 -0800331 /** Used to keep resumeTopActivityLocked() from being entered recursively */
332 boolean inResumeTopActivity;
333
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700334 // temp. rect used during resize calculation so we don't need to create a new object each time.
335 private final Rect tempRect = new Rect();
336
Craig Mautneree36c772014-07-16 14:56:05 -0700337 /**
338 * Description of a request to start a new activity, which has been held
339 * due to app switches being disabled.
340 */
341 static class PendingActivityLaunch {
342 final ActivityRecord r;
343 final ActivityRecord sourceRecord;
344 final int startFlags;
345 final ActivityStack stack;
346
347 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
348 int _startFlags, ActivityStack _stack) {
349 r = _r;
350 sourceRecord = _sourceRecord;
351 startFlags = _startFlags;
352 stack = _stack;
353 }
354 }
355
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800356 public ActivityStackSupervisor(ActivityManagerService service, RecentTasks recentTasks) {
Craig Mautner27084302013-03-25 08:05:25 -0700357 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800358 mRecentTasks = recentTasks;
Jeff Brown2c43c332014-06-12 22:38:59 -0700359 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
360 }
361
362 /**
363 * At the time when the constructor runs, the power manager has not yet been
364 * initialized. So we initialize our wakelocks afterwards.
365 */
366 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800367 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700368 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700369 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700370 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700371 }
372
justinzhang5286d3f2014-05-12 17:06:01 -0400373 // This function returns a IStatusBarService. The value is from ServiceManager.
374 // getService and is cached.
375 private IStatusBarService getStatusBarService() {
376 synchronized (mService) {
377 if (mStatusBarService == null) {
378 mStatusBarService = IStatusBarService.Stub.asInterface(
379 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
380 if (mStatusBarService == null) {
381 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
382 }
383 }
384 return mStatusBarService;
385 }
386 }
387
Jason Monk35c62a42014-06-17 10:24:47 -0400388 private IDevicePolicyManager getDevicePolicyManager() {
389 synchronized (mService) {
390 if (mDevicePolicyManager == null) {
391 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
392 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
393 if (mDevicePolicyManager == null) {
394 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
395 }
396 }
397 return mDevicePolicyManager;
398 }
399 }
400
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700401 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800402 synchronized (mService) {
403 mWindowManager = wm;
404
405 mDisplayManager =
406 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
407 mDisplayManager.registerDisplayListener(this, null);
408
409 Display[] displays = mDisplayManager.getDisplays();
410 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
411 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800412 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700413 if (activityDisplay.mDisplay == null) {
414 throw new IllegalStateException("Default Display does not exist");
415 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800416 mActivityDisplays.put(displayId, activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800417 }
418
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700419 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800420 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800421
422 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Jose Lima58e66d62014-05-27 20:00:27 -0700423
424 // Initialize this here, now that we can get a valid reference to PackageManager.
425 mLeanbackOnlyDevice = isLeanbackOnlyDevice();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800426 }
Craig Mautner27084302013-03-25 08:05:25 -0700427 }
428
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200429 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700430 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200431 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700432 }
433
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700434 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800435 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700436 }
437
Craig Mautnerde4ef022013-04-07 19:01:33 -0700438 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800439 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700440 }
441
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800442 /** Top of all visible stacks is/should always be equal to the focused stack.
443 * Use {@link ActivityStack#isStackVisibleLocked} to determine if a specific
444 * stack is visible or not. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700445 boolean isFrontStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700446 if (stack == null) {
447 return false;
448 }
449
Craig Mautnerdf88d732014-01-27 09:21:32 -0800450 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
451 if (parent != null) {
452 stack = parent.task.stack;
453 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800454 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700455 }
456
Craig Mautner299f9602015-01-26 09:47:33 -0800457 void moveHomeStack(boolean toFront, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800458 moveHomeStack(toFront, reason, null);
459 }
460
461 void moveHomeStack(boolean toFront, String reason, ActivityStack lastFocusedStack) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800462 ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautnerde313752015-01-22 14:28:03 -0800463 final int topNdx = stacks.size() - 1;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800464 if (topNdx <= 0) {
465 return;
466 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700467
468 // The home stack should either be at the top or bottom of the stack list.
469 if ((toFront && (stacks.get(topNdx) != mHomeStack))
470 || (!toFront && (stacks.get(0) != mHomeStack))) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700471 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveHomeTask: topStack old="
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700472 + ((lastFocusedStack != null) ? lastFocusedStack : stacks.get(topNdx))
473 + " new=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800474 stacks.remove(mHomeStack);
475 stacks.add(toFront ? topNdx : 0, mHomeStack);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700476 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800477
478 if (lastFocusedStack != null) {
479 mLastFocusedStack = lastFocusedStack;
480 }
481 mFocusedStack = stacks.get(topNdx);
482
Craig Mautnerde313752015-01-22 14:28:03 -0800483 EventLog.writeEvent(EventLogTags.AM_HOME_STACK_MOVED,
484 mCurrentUser, toFront ? 1 : 0, stacks.get(topNdx).getStackId(),
Craig Mautner299f9602015-01-26 09:47:33 -0800485 mFocusedStack == null ? -1 : mFocusedStack.getStackId(), reason);
Craig Mautnerde313752015-01-22 14:28:03 -0800486
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800487 if (mService.mBooting || !mService.mBooted) {
488 final ActivityRecord r = topRunningActivityLocked();
489 if (r != null && r.idle) {
490 checkFinishBootingLocked();
491 }
492 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700493 }
494
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700495 /** Returns true if the focus activity was adjusted to the home stack top activity. */
496 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700497 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
498 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700499 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700500 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700501
Craig Mautner84984fa2014-06-19 11:19:20 -0700502 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700503
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700504 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700505 if (top == null) {
506 return false;
507 }
508 mService.setFocusedActivityLocked(top, reason);
509 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700510 }
511
Craig Mautner299f9602015-01-26 09:47:33 -0800512 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700513 if (!mService.mBooting && !mService.mBooted) {
514 // Not ready yet!
515 return false;
516 }
517
Craig Mautner84984fa2014-06-19 11:19:20 -0700518 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
519 mWindowManager.showRecentApps();
520 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700521 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700522
Craig Mautner84984fa2014-06-19 11:19:20 -0700523 if (prev != null) {
524 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
525 }
526
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700527 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
528 ActivityRecord r = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700529 if (r != null) {
Craig Mautner299f9602015-01-26 09:47:33 -0800530 mService.setFocusedActivityLocked(r, reason);
Craig Mautner05d29032013-05-03 13:40:13 -0700531 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700532 }
Craig Mautner299f9602015-01-26 09:47:33 -0800533 return mService.startHomeActivityLocked(mCurrentUser, reason);
Craig Mautner69ada552013-04-18 13:51:51 -0700534 }
535
Craig Mautner8d341ef2013-03-26 09:03:27 -0700536 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700537 return anyTaskForIdLocked(id, true);
538 }
539
540 /**
541 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
542 * @param id Id of the task we would like returned.
543 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
544 * restore the task from recents to the active list.
545 */
546 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800547 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800548 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800549 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800550 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
551 ActivityStack stack = stacks.get(stackNdx);
552 TaskRecord task = stack.taskForIdLocked(id);
553 if (task != null) {
554 return task;
555 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700556 }
557 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800558
559 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700560 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800561 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800562 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700563 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800564 return null;
565 }
566
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700567 if (!restoreFromRecents) {
568 return task;
569 }
570
Chong Zhang5dcb2752015-08-18 13:50:26 -0700571 if (!restoreRecentTaskLocked(task, INVALID_STACK_ID)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700572 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
573 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800574 return null;
575 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700576 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800577 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700578 }
579
Craig Mautner6170f732013-04-02 13:05:23 -0700580 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800581 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800582 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800583 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800584 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
585 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
586 if (r != null) {
587 return r;
588 }
Craig Mautner6170f732013-04-02 13:05:23 -0700589 }
590 }
591 return null;
592 }
593
Craig Mautneref73ee12014-04-23 11:45:37 -0700594 void setNextTaskId(int taskId) {
595 if (taskId > mCurTaskId) {
596 mCurTaskId = taskId;
597 }
598 }
599
Craig Mautner8d341ef2013-03-26 09:03:27 -0700600 int getNextTaskId() {
601 do {
602 mCurTaskId++;
603 if (mCurTaskId <= 0) {
604 mCurTaskId = 1;
605 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700606 } while (anyTaskForIdLocked(mCurTaskId, false) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700607 return mCurTaskId;
608 }
609
Craig Mautnerde4ef022013-04-07 19:01:33 -0700610 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800611 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700612 if (stack == null) {
613 return null;
614 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700615 ActivityRecord resumedActivity = stack.mResumedActivity;
616 if (resumedActivity == null || resumedActivity.app == null) {
617 resumedActivity = stack.mPausingActivity;
618 if (resumedActivity == null || resumedActivity.app == null) {
619 resumedActivity = stack.topRunningActivityLocked(null);
620 }
621 }
622 return resumedActivity;
623 }
624
Dianne Hackbornff072722014-09-24 10:56:28 -0700625 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700626 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800627 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800628 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
629 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800630 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
631 final ActivityStack stack = stacks.get(stackNdx);
632 if (!isFrontStack(stack)) {
633 continue;
634 }
635 ActivityRecord hr = stack.topRunningActivityLocked(null);
636 if (hr != null) {
637 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
638 && processName.equals(hr.processName)) {
639 try {
George Mount2c92c972014-03-20 09:38:23 -0700640 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800641 didSomething = true;
642 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700643 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800644 Slog.w(TAG, "Exception in new application when starting activity "
645 + hr.intent.getComponent().flattenToShortString(), e);
646 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700647 }
Craig Mautner20e72272013-04-01 13:45:53 -0700648 }
Craig Mautner20e72272013-04-01 13:45:53 -0700649 }
650 }
651 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700652 if (!didSomething) {
653 ensureActivitiesVisibleLocked(null, 0);
654 }
Craig Mautner20e72272013-04-01 13:45:53 -0700655 return didSomething;
656 }
657
658 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800659 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
660 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800661 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
662 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner34b73df2014-01-12 21:11:08 -0800663 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800664 continue;
665 }
666 final ActivityRecord resumedActivity = stack.mResumedActivity;
667 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700668 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800669 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800670 return false;
671 }
Craig Mautner20e72272013-04-01 13:45:53 -0700672 }
673 }
674 return true;
675 }
676
Craig Mautnerde4ef022013-04-07 19:01:33 -0700677 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800678 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
679 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800680 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
681 final ActivityStack stack = stacks.get(stackNdx);
682 if (isFrontStack(stack)) {
683 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700684 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800685 return false;
686 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700687 }
688 }
689 }
690 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700691 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800692 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
693 mLastFocusedStack + " to=" + mFocusedStack);
694 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700695 return true;
696 }
697
698 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800699 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800700 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
701 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800702 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
703 final ActivityStack stack = stacks.get(stackNdx);
704 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800705 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700706 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800707 return false;
708 }
709 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800710 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700711 }
712 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800713 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700714 }
715
Craig Mautner2acc3892013-09-23 10:28:14 -0700716 /**
717 * Pause all activities in either all of the stacks or just the back stacks.
718 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700719 * @return true if any activity was paused as a result of this call.
720 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700721 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700722 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800723 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
724 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800725 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
726 final ActivityStack stack = stacks.get(stackNdx);
727 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700728 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800729 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700730 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
731 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800732 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700733 }
734 }
735 return someActivityPaused;
736 }
737
Craig Mautnerde4ef022013-04-07 19:01:33 -0700738 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700739 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800740 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
741 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800742 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
743 final ActivityStack stack = stacks.get(stackNdx);
744 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700745 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800746 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700747 Slog.d(TAG_STATES,
748 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800749 pausing = false;
750 } else {
751 return false;
752 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700753 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700754 }
755 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700756 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700757 }
758
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700759 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
760 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500761 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800762 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
763 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
764 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
765 final ActivityStack stack = stacks.get(stackNdx);
766 if (stack.mResumedActivity != null &&
767 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700768 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800769 }
770 }
771 }
772 }
773
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700774 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700775 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700776 }
777
778 void sendWaitingVisibleReportLocked(ActivityRecord r) {
779 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700780 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700781 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700782 if (w.who == null) {
783 changed = true;
784 w.timeout = false;
785 if (r != null) {
786 w.who = new ComponentName(r.info.packageName, r.info.name);
787 }
788 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
789 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700790 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700791 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700792 if (changed) {
793 mService.notifyAll();
794 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700795 }
796
797 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
798 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700799 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700800 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700801 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700802 if (w.who == null) {
803 changed = true;
804 w.timeout = timeout;
805 if (r != null) {
806 w.who = new ComponentName(r.info.packageName, r.info.name);
807 }
808 w.thisTime = thisTime;
809 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700810 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700811 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700812 if (changed) {
813 mService.notifyAll();
814 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700815 }
816
Craig Mautner29219d92013-04-16 20:19:12 -0700817 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800818 final ActivityStack focusedStack = mFocusedStack;
Craig Mautner1602ec22013-05-12 10:24:27 -0700819 ActivityRecord r = focusedStack.topRunningActivityLocked(null);
820 if (r != null) {
821 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700822 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700823
Craig Mautner4a1cb222013-12-04 16:14:06 -0800824 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800825 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800826 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
827 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700828 if (stack != focusedStack && isFrontStack(stack)) {
Craig Mautner29219d92013-04-16 20:19:12 -0700829 r = stack.topRunningActivityLocked(null);
830 if (r != null) {
831 return r;
832 }
833 }
834 }
835 return null;
836 }
837
Dianne Hackborn09233282014-04-30 11:33:59 -0700838 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700839 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800840 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
841 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800842 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800843 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800844 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800845 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
846 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700847 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800848 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700849 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700850 }
851 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700852
853 // The lists are already sorted from most recent to oldest. Just pull the most recent off
854 // each list and add it to list. Stop when all lists are empty or maxNum reached.
855 while (maxNum > 0) {
856 long mostRecentActiveTime = Long.MIN_VALUE;
857 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800858 final int numTaskLists = runningTaskLists.size();
859 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
860 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700861 if (!stackTaskList.isEmpty()) {
862 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
863 if (lastActiveTime > mostRecentActiveTime) {
864 mostRecentActiveTime = lastActiveTime;
865 selectedStackList = stackTaskList;
866 }
867 }
868 }
869 if (selectedStackList != null) {
870 list.add(selectedStackList.remove(0));
871 --maxNum;
872 } else {
873 break;
874 }
875 }
Craig Mautner20e72272013-04-01 13:45:53 -0700876 }
877
Craig Mautner23ac33b2013-04-01 16:26:35 -0700878 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700879 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700880 // Collect information about the target of the Intent.
881 ActivityInfo aInfo;
882 try {
883 ResolveInfo rInfo =
884 AppGlobals.getPackageManager().resolveIntent(
885 intent, resolvedType,
886 PackageManager.MATCH_DEFAULT_ONLY
887 | ActivityManagerService.STOCK_PM_FLAGS, userId);
888 aInfo = rInfo != null ? rInfo.activityInfo : null;
889 } catch (RemoteException e) {
890 aInfo = null;
891 }
892
893 if (aInfo != null) {
894 // Store the found target back into the intent, because now that
895 // we have it we never want to do this again. For example, if the
896 // user navigates back to this point in the history, we should
897 // always restart the exact same activity.
898 intent.setComponent(new ComponentName(
899 aInfo.applicationInfo.packageName, aInfo.name));
900
901 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -0700902 if (!aInfo.processName.equals("system")) {
903 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700904 mService.setDebugApp(aInfo.processName, true, false);
905 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700906
Man Caocfa78b22015-06-11 20:14:34 -0700907 if ((startFlags & ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700908 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
909 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700910
Man Caocfa78b22015-06-11 20:14:34 -0700911 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
912 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
913 }
914
915 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700916 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700917 }
918 }
919 }
920 return aInfo;
921 }
922
Craig Mautner299f9602015-01-26 09:47:33 -0800923 void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
924 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700925 startActivityLocked(null /* caller */, intent, null /* resolvedType */, aInfo,
926 null /* voiceSession */, null /* voiceInteractor */, null /* resultTo */,
927 null /* resultWho */, 0 /* requestCode */, 0 /* callingPid */, 0 /* callingUid */,
928 null /* callingPackage */, 0 /* realCallingPid */, 0 /* realCallingUid */,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700929 0 /* startFlags */, null /* options */, false /* ignoreTargetSecurity */,
930 false /* componentSpecified */,
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700931 null /* outActivity */, null /* container */, null /* inTask */);
932 if (inResumeTopActivity) {
933 // If we are in resume section already, home activity will be initialized, but not
934 // resumed (to avoid recursive resume) and will stay that way until something pokes it
935 // again. We need to schedule another resume.
936 scheduleResumeTopActivities();
937 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700938 }
939
Craig Mautner23ac33b2013-04-01 16:26:35 -0700940 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700941 String callingPackage, Intent intent, String resolvedType,
942 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700943 IBinder resultTo, String resultWho, int requestCode, int startFlags,
944 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700945 Bundle options, boolean ignoreTargetSecurity, int userId,
946 IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700947 // Refuse possible leaked file descriptors
948 if (intent != null && intent.hasFileDescriptors()) {
949 throw new IllegalArgumentException("File descriptors passed in Intent");
950 }
951 boolean componentSpecified = intent.getComponent() != null;
952
953 // Don't modify the client's object!
954 intent = new Intent(intent);
955
956 // Collect information about the target of the Intent.
Craig Mautner15df08a2015-04-01 12:17:18 -0700957 ActivityInfo aInfo =
958 resolveActivity(intent, resolvedType, startFlags, profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700959
Craig Mautnere0a38842013-12-16 16:14:02 -0800960 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700961 synchronized (mService) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800962 if (container != null && container.mParentActivity != null &&
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700963 container.mParentActivity.state != RESUMED) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800964 // Cannot start a child activity if the parent is not resumed.
965 return ActivityManager.START_CANCELED;
966 }
Dianne Hackborn95465202014-09-15 16:21:55 -0700967 final int realCallingPid = Binder.getCallingPid();
968 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -0700969 int callingPid;
970 if (callingUid >= 0) {
971 callingPid = -1;
972 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700973 callingPid = realCallingPid;
974 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700975 } else {
976 callingPid = callingUid = -1;
977 }
978
Craig Mautnere0a38842013-12-16 16:14:02 -0800979 final ActivityStack stack;
980 if (container == null || container.mStack.isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800981 stack = mFocusedStack;
Craig Mautnere0a38842013-12-16 16:14:02 -0800982 } else {
983 stack = container.mStack;
984 }
Wale Ogunwale60454db2015-01-23 16:05:07 -0800985 stack.mConfigWillChange = config != null && mService.mConfiguration.diff(config) != 0;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700986 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700987 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700988
989 final long origId = Binder.clearCallingIdentity();
990
991 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800992 (aInfo.applicationInfo.privateFlags
993 &ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700994 // This may be a heavy-weight process! Check to see if we already
995 // have another, different heavy-weight process running.
996 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
997 if (mService.mHeavyWeightProcess != null &&
998 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
999 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001000 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001001 if (caller != null) {
1002 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
1003 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001004 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001005 } else {
1006 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -07001007 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -07001008 + intent.toString());
1009 ActivityOptions.abort(options);
1010 return ActivityManager.START_PERMISSION_DENIED;
1011 }
1012 }
1013
1014 IIntentSender target = mService.getIntentSenderLocked(
1015 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -07001016 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -07001017 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
1018 | PendingIntent.FLAG_ONE_SHOT, null);
1019
1020 Intent newIntent = new Intent();
1021 if (requestCode >= 0) {
1022 // Caller is requesting a result.
1023 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
1024 }
1025 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
1026 new IntentSender(target));
1027 if (mService.mHeavyWeightProcess.activities.size() > 0) {
1028 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
1029 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
1030 hist.packageName);
1031 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
1032 hist.task.taskId);
1033 }
1034 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
1035 aInfo.packageName);
1036 newIntent.setFlags(intent.getFlags());
1037 newIntent.setClassName("android",
1038 HeavyWeightSwitcherActivity.class.getName());
1039 intent = newIntent;
1040 resolvedType = null;
1041 caller = null;
1042 callingUid = Binder.getCallingUid();
1043 callingPid = Binder.getCallingPid();
1044 componentSpecified = true;
1045 try {
1046 ResolveInfo rInfo =
1047 AppGlobals.getPackageManager().resolveIntent(
1048 intent, null,
1049 PackageManager.MATCH_DEFAULT_ONLY
1050 | ActivityManagerService.STOCK_PM_FLAGS, userId);
1051 aInfo = rInfo != null ? rInfo.activityInfo : null;
1052 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1053 } catch (RemoteException e) {
1054 aInfo = null;
1055 }
1056 }
1057 }
1058 }
1059
Dianne Hackborn91097de2014-04-04 18:02:06 -07001060 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
1061 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -07001062 requestCode, callingPid, callingUid, callingPackage,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001063 realCallingPid, realCallingUid, startFlags, options, ignoreTargetSecurity,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001064 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001065
Craig Mautner85c11a82014-07-17 12:38:18 -07001066 Binder.restoreCallingIdentity(origId);
1067
Craig Mautnerde4ef022013-04-07 19:01:33 -07001068 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001069 // If the caller also wants to switch to a new configuration,
1070 // do so now. This allows a clean switch, as we are waiting
1071 // for the current activity to pause (so we will not destroy
1072 // it), and have not yet started the next activity.
1073 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
1074 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -07001075 stack.mConfigWillChange = false;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001076 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautner23ac33b2013-04-01 16:26:35 -07001077 "Updating to new configuration after starting activity.");
Maxim Bogatov05075302015-05-19 18:33:08 -07001078 mService.updateConfigurationLocked(config, null, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001079 }
1080
Craig Mautner23ac33b2013-04-01 16:26:35 -07001081 if (outResult != null) {
1082 outResult.result = res;
1083 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001084 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001085 do {
1086 try {
1087 mService.wait();
1088 } catch (InterruptedException e) {
1089 }
1090 } while (!outResult.timeout && outResult.who == null);
1091 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001092 ActivityRecord r = stack.topRunningActivityLocked(null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001093 if (r.nowVisible && r.state == RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001094 outResult.timeout = false;
1095 outResult.who = new ComponentName(r.info.packageName, r.info.name);
1096 outResult.totalTime = 0;
1097 outResult.thisTime = 0;
1098 } else {
1099 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001100 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001101 do {
1102 try {
1103 mService.wait();
1104 } catch (InterruptedException e) {
1105 }
1106 } while (!outResult.timeout && outResult.who == null);
1107 }
1108 }
1109 }
1110
1111 return res;
1112 }
1113 }
1114
1115 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1116 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
1117 Bundle options, int userId) {
1118 if (intents == null) {
1119 throw new NullPointerException("intents is null");
1120 }
1121 if (resolvedTypes == null) {
1122 throw new NullPointerException("resolvedTypes is null");
1123 }
1124 if (intents.length != resolvedTypes.length) {
1125 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1126 }
1127
Craig Mautner23ac33b2013-04-01 16:26:35 -07001128
1129 int callingPid;
1130 if (callingUid >= 0) {
1131 callingPid = -1;
1132 } else if (caller == null) {
1133 callingPid = Binder.getCallingPid();
1134 callingUid = Binder.getCallingUid();
1135 } else {
1136 callingPid = callingUid = -1;
1137 }
1138 final long origId = Binder.clearCallingIdentity();
1139 try {
1140 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001141 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001142 for (int i=0; i<intents.length; i++) {
1143 Intent intent = intents[i];
1144 if (intent == null) {
1145 continue;
1146 }
1147
1148 // Refuse possible leaked file descriptors
1149 if (intent != null && intent.hasFileDescriptors()) {
1150 throw new IllegalArgumentException("File descriptors passed in Intent");
1151 }
1152
1153 boolean componentSpecified = intent.getComponent() != null;
1154
1155 // Don't modify the client's object!
1156 intent = new Intent(intent);
1157
1158 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001159 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001160 // TODO: New, check if this is correct
1161 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1162
1163 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001164 (aInfo.applicationInfo.privateFlags
1165 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001166 throw new IllegalArgumentException(
1167 "FLAG_CANT_SAVE_STATE not supported here");
1168 }
1169
1170 Bundle theseOptions;
1171 if (options != null && i == intents.length-1) {
1172 theseOptions = options;
1173 } else {
1174 theseOptions = null;
1175 }
Craig Mautner6170f732013-04-02 13:05:23 -07001176 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001177 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1178 callingPackage, callingPid, callingUid,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001179 0, theseOptions, false, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001180 if (res < 0) {
1181 return res;
1182 }
1183
1184 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1185 }
1186 }
1187 } finally {
1188 Binder.restoreCallingIdentity(origId);
1189 }
1190
1191 return ActivityManager.START_SUCCESS;
1192 }
1193
Craig Mautner2420ead2013-04-01 17:13:20 -07001194 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001195 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001196 throws RemoteException {
1197
Craig Mautner2568c3a2015-03-26 14:22:34 -07001198 if (andResume) {
1199 r.startFreezingScreenLocked(app, 0);
1200 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001201
Craig Mautner2568c3a2015-03-26 14:22:34 -07001202 // schedule launch ticks to collect information about slow apps.
1203 r.startLaunchTickingLocked();
1204 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001205
1206 // Have the window manager re-evaluate the orientation of
1207 // the screen based on the new activity order. Note that
1208 // as a result of this, it can call back into the activity
1209 // manager with a new orientation. We don't care about that,
1210 // because the activity is not currently running so we are
1211 // just restarting it anyway.
1212 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001213 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001214 mService.mConfiguration,
1215 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001216 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001217 }
1218
1219 r.app = app;
1220 app.waitingToKill = null;
1221 r.launchCount++;
1222 r.lastLaunchTime = SystemClock.uptimeMillis();
1223
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001224 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001225
1226 int idx = app.activities.indexOf(r);
1227 if (idx < 0) {
1228 app.activities.add(r);
1229 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001230 mService.updateLruProcessLocked(app, true, null);
1231 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001232
Craig Mautner15df08a2015-04-01 12:17:18 -07001233 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001234 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1235 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001236 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001237 }
1238
1239 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001240 try {
1241 if (app.thread == null) {
1242 throw new RemoteException();
1243 }
1244 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001245 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001246 if (andResume) {
1247 results = r.results;
1248 newIntents = r.newIntents;
1249 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001250 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1251 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1252 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001253 if (andResume) {
1254 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1255 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001256 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001257 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001258 if (r.isHomeActivity() && r.isNotResolverActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001259 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001260 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001261 }
1262 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1263 r.sleeping = false;
1264 r.forceNewConfig = false;
1265 mService.showAskCompatModeDialogLocked(r);
1266 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001267 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001268 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1269 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1270 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001271 final String profileFile = mService.mProfileFile;
1272 if (profileFile != null) {
1273 ParcelFileDescriptor profileFd = mService.mProfileFd;
1274 if (profileFd != null) {
1275 try {
1276 profileFd = profileFd.dup();
1277 } catch (IOException e) {
1278 if (profileFd != null) {
1279 try {
1280 profileFd.close();
1281 } catch (IOException o) {
1282 }
1283 profileFd = null;
1284 }
1285 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001286 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001287
1288 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1289 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001290 }
1291 }
1292 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001293
Craig Mautner2568c3a2015-03-26 14:22:34 -07001294 if (andResume) {
1295 app.hasShownUi = true;
1296 app.pendingUiClean = true;
1297 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001298 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001299 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001300 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001301 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001302 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001303 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001304
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001305 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001306 // This may be a heavy-weight process! Note that the package
1307 // manager will ensure that only activity can run in the main
1308 // process of the .apk, which is the only thing that will be
1309 // considered heavy-weight.
1310 if (app.processName.equals(app.info.packageName)) {
1311 if (mService.mHeavyWeightProcess != null
1312 && mService.mHeavyWeightProcess != app) {
1313 Slog.w(TAG, "Starting new heavy weight process " + app
1314 + " when already running "
1315 + mService.mHeavyWeightProcess);
1316 }
1317 mService.mHeavyWeightProcess = app;
1318 Message msg = mService.mHandler.obtainMessage(
1319 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1320 msg.obj = r;
1321 mService.mHandler.sendMessage(msg);
1322 }
1323 }
1324
1325 } catch (RemoteException e) {
1326 if (r.launchFailed) {
1327 // This is the second time we failed -- finish activity
1328 // and give up.
1329 Slog.e(TAG, "Second failure launching "
1330 + r.intent.getComponent().flattenToShortString()
1331 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001332 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001333 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1334 "2nd-crash", false);
1335 return false;
1336 }
1337
1338 // This is the first time we failed -- restart process and
1339 // retry.
1340 app.activities.remove(r);
1341 throw e;
1342 }
1343
1344 r.launchFailed = false;
1345 if (stack.updateLRUListLocked(r)) {
1346 Slog.w(TAG, "Activity " + r
1347 + " being launched, but already in LRU list");
1348 }
1349
1350 if (andResume) {
1351 // As part of the process of launching, ActivityThread also performs
1352 // a resume.
1353 stack.minimalResumeActivityLocked(r);
1354 } else {
1355 // This activity is not starting in the resumed state... which
1356 // should look like we asked it to pause+stop (but remain visible),
1357 // and it has done so and reported back the current icicle and
1358 // other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001359 if (DEBUG_STATES) Slog.v(TAG_STATES,
1360 "Moving to STOPPED: " + r + " (starting in stopped state)");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001361 r.state = STOPPED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001362 r.stopped = true;
1363 }
1364
1365 // Launch the new version setup screen if needed. We do this -after-
1366 // launching the initial activity (that is, home), so that it can have
1367 // a chance to initialize itself while in the background, making the
1368 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001369 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001370 mService.startSetupActivityLocked();
1371 }
1372
Dianne Hackborn465fa392014-09-14 14:21:18 -07001373 // Update any services we are bound to that might care about whether
1374 // their client may have activities.
1375 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1376
Craig Mautner2420ead2013-04-01 17:13:20 -07001377 return true;
1378 }
1379
Craig Mautnere79d42682013-04-01 19:01:53 -07001380 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001381 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001382 // Is this activity's application already running?
1383 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001384 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001385
1386 r.task.stack.setLaunchTime(r);
1387
1388 if (app != null && app.thread != null) {
1389 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001390 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1391 || !"android".equals(r.info.packageName)) {
1392 // Don't add this if it is a platform component that is marked
1393 // to run in multiple processes, because this is actually
1394 // part of the framework so doesn't make sense to track as a
1395 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001396 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1397 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001398 }
George Mount2c92c972014-03-20 09:38:23 -07001399 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001400 return;
1401 } catch (RemoteException e) {
1402 Slog.w(TAG, "Exception when starting activity "
1403 + r.intent.getComponent().flattenToShortString(), e);
1404 }
1405
1406 // If a dead object exception was thrown -- fall through to
1407 // restart the application.
1408 }
1409
1410 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001411 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001412 }
1413
Craig Mautner6170f732013-04-02 13:05:23 -07001414 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001415 Intent intent, String resolvedType, ActivityInfo aInfo,
1416 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1417 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001418 int callingPid, int callingUid, String callingPackage,
1419 int realCallingPid, int realCallingUid, int startFlags, Bundle options,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001420 boolean ignoreTargetSecurity, boolean componentSpecified, ActivityRecord[] outActivity,
1421 ActivityContainer container, TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001422 int err = ActivityManager.START_SUCCESS;
1423
1424 ProcessRecord callerApp = null;
1425 if (caller != null) {
1426 callerApp = mService.getRecordForAppLocked(caller);
1427 if (callerApp != null) {
1428 callingPid = callerApp.pid;
1429 callingUid = callerApp.info.uid;
1430 } else {
1431 Slog.w(TAG, "Unable to find app for caller " + caller
1432 + " (pid=" + callingPid + ") when starting: "
1433 + intent.toString());
1434 err = ActivityManager.START_PERMISSION_DENIED;
1435 }
1436 }
1437
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001438 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1439
Craig Mautner6170f732013-04-02 13:05:23 -07001440 if (err == ActivityManager.START_SUCCESS) {
Craig Mautner6170f732013-04-02 13:05:23 -07001441 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001442 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001443 + " on display " + (container == null ? (mFocusedStack == null ?
1444 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1445 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1446 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001447 }
1448
1449 ActivityRecord sourceRecord = null;
1450 ActivityRecord resultRecord = null;
1451 if (resultTo != null) {
1452 sourceRecord = isInAnyStackLocked(resultTo);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001453 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
1454 "Will send result to " + resultTo + " " + sourceRecord);
Craig Mautner6170f732013-04-02 13:05:23 -07001455 if (sourceRecord != null) {
1456 if (requestCode >= 0 && !sourceRecord.finishing) {
1457 resultRecord = sourceRecord;
1458 }
1459 }
1460 }
Craig Mautner6170f732013-04-02 13:05:23 -07001461
Dianne Hackborn91097de2014-04-04 18:02:06 -07001462 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001463
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001464 if ((launchFlags & Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001465 // Transfer the result target from the source activity to the new
1466 // one being started, including any failures.
1467 if (requestCode >= 0) {
1468 ActivityOptions.abort(options);
1469 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1470 }
1471 resultRecord = sourceRecord.resultTo;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001472 if (resultRecord != null && !resultRecord.isInStackLocked()) {
1473 resultRecord = null;
1474 }
Craig Mautner6170f732013-04-02 13:05:23 -07001475 resultWho = sourceRecord.resultWho;
1476 requestCode = sourceRecord.requestCode;
1477 sourceRecord.resultTo = null;
1478 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001479 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001480 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001481 if (sourceRecord.launchedFromUid == callingUid) {
1482 // The new activity is being launched from the same uid as the previous
1483 // activity in the flow, and asking to forward its result back to the
1484 // previous. In this case the activity is serving as a trampoline between
1485 // the two, so we also want to update its launchedFromPackage to be the
1486 // same as the previous activity. Note that this is safe, since we know
1487 // these two packages come from the same uid; the caller could just as
1488 // well have supplied that same package name itself. This specifially
1489 // deals with the case of an intent picker/chooser being launched in the app
1490 // flow to redirect to an activity picked by the user, where we want the final
1491 // activity to consider it to have been launched by the previous app activity.
1492 callingPackage = sourceRecord.launchedFromPackage;
1493 }
Craig Mautner6170f732013-04-02 13:05:23 -07001494 }
1495
1496 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1497 // We couldn't find a class that can handle the given Intent.
1498 // That's the end of that!
1499 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1500 }
1501
1502 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1503 // We couldn't find the specific class specified in the Intent.
1504 // Also the end of the line.
1505 err = ActivityManager.START_CLASS_NOT_FOUND;
1506 }
1507
Dianne Hackborn91097de2014-04-04 18:02:06 -07001508 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1509 && sourceRecord.task.voiceSession != null) {
1510 // If this activity is being launched as part of a voice session, we need
1511 // to ensure that it is safe to do so. If the upcoming activity will also
1512 // be part of the voice session, we can only launch it if it has explicitly
1513 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001514 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001515 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1516 try {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001517 intent.addCategory(Intent.CATEGORY_VOICE);
Dianne Hackborn95465202014-09-15 16:21:55 -07001518 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1519 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001520 Slog.w(TAG,
1521 "Activity being started in current voice task does not support voice: "
1522 + intent);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001523 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1524 }
1525 } catch (RemoteException e) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001526 Slog.w(TAG, "Failure checking voice capabilities", e);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001527 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1528 }
1529 }
1530 }
1531
1532 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1533 // If the caller is starting a new voice session, just make sure the target
1534 // is actually allowing it to run this way.
1535 try {
1536 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1537 intent, resolvedType)) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001538 Slog.w(TAG,
1539 "Activity being started in new voice task does not support: "
1540 + intent);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001541 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1542 }
1543 } catch (RemoteException e) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001544 Slog.w(TAG, "Failure checking voice capabilities", e);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001545 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1546 }
1547 }
1548
louis_changcd5d1982014-08-01 10:09:08 +08001549 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1550
Craig Mautner6170f732013-04-02 13:05:23 -07001551 if (err != ActivityManager.START_SUCCESS) {
1552 if (resultRecord != null) {
1553 resultStack.sendActivityResultLocked(-1,
1554 resultRecord, resultWho, requestCode,
1555 Activity.RESULT_CANCELED, null);
1556 }
Craig Mautner6170f732013-04-02 13:05:23 -07001557 ActivityOptions.abort(options);
1558 return err;
1559 }
1560
Svetoslav7008b512015-06-24 18:47:07 -07001561 boolean abort = false;
1562
Svet Ganov99b60432015-06-27 13:15:22 -07001563 final int startAnyPerm = mService.checkPermission(
1564 START_ANY_ACTIVITY, callingPid, callingUid);
1565
1566 if (startAnyPerm != PERMISSION_GRANTED) {
1567 final int componentRestriction = getComponentRestrictionForCallingPackage(
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001568 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
Svet Ganov99b60432015-06-27 13:15:22 -07001569 final int actionRestriction = getActionRestrictionForCallingPackage(
1570 intent.getAction(), callingPackage, callingPid, callingUid);
1571
1572 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1573 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1574 if (resultRecord != null) {
1575 resultStack.sendActivityResultLocked(-1,
1576 resultRecord, resultWho, requestCode,
1577 Activity.RESULT_CANCELED, null);
1578 }
1579 String msg;
1580 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1581 msg = "Permission Denial: starting " + intent.toString()
1582 + " from " + callerApp + " (pid=" + callingPid
1583 + ", uid=" + callingUid + ")" + " with revoked permission "
1584 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1585 } else if (!aInfo.exported) {
1586 msg = "Permission Denial: starting " + intent.toString()
1587 + " from " + callerApp + " (pid=" + callingPid
1588 + ", uid=" + callingUid + ")"
1589 + " not exported from uid " + aInfo.applicationInfo.uid;
1590 } else {
1591 msg = "Permission Denial: starting " + intent.toString()
1592 + " from " + callerApp + " (pid=" + callingPid
1593 + ", uid=" + callingUid + ")"
1594 + " requires " + aInfo.permission;
1595 }
1596 Slog.w(TAG, msg);
1597 throw new SecurityException(msg);
1598 }
1599
1600 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1601 String message = "Appop Denial: starting " + intent.toString()
1602 + " from " + callerApp + " (pid=" + callingPid
1603 + ", uid=" + callingUid + ")"
1604 + " requires " + AppOpsManager.permissionToOp(
1605 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1606 Slog.w(TAG, message);
1607 abort = true;
1608 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1609 String message = "Appop Denial: starting " + intent.toString()
1610 + " from " + callerApp + " (pid=" + callingPid
1611 + ", uid=" + callingUid + ")"
1612 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1613 Slog.w(TAG, message);
1614 abort = true;
1615 }
Svetoslav7008b512015-06-24 18:47:07 -07001616 }
1617
1618 abort |= !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001619 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001620
Craig Mautner6170f732013-04-02 13:05:23 -07001621 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001622 try {
1623 // The Intent we give to the watcher has the extra data
1624 // stripped off, since it can contain private information.
1625 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001626 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001627 aInfo.applicationInfo.packageName);
1628 } catch (RemoteException e) {
1629 mService.mController = null;
1630 }
Ben Gruver5e207332013-04-03 17:41:37 -07001631 }
Craig Mautner6170f732013-04-02 13:05:23 -07001632
Ben Gruver5e207332013-04-03 17:41:37 -07001633 if (abort) {
1634 if (resultRecord != null) {
1635 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001636 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001637 }
Ben Gruver5e207332013-04-03 17:41:37 -07001638 // We pretend to the caller that it was really started, but
1639 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001640 ActivityOptions.abort(options);
1641 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001642 }
1643
1644 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001645 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001646 requestCode, componentSpecified, voiceSession != null, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001647 if (outActivity != null) {
1648 outActivity[0] = r;
1649 }
1650
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001651 if (r.appTimeTracker == null && sourceRecord != null) {
1652 // If the caller didn't specify an explicit time tracker, we want to continue
1653 // tracking under any it has.
1654 r.appTimeTracker = sourceRecord.appTimeTracker;
1655 }
1656
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001657 final ActivityStack stack = mFocusedStack;
Dianne Hackborn91097de2014-04-04 18:02:06 -07001658 if (voiceSession == null && (stack.mResumedActivity == null
1659 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001660 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1661 realCallingPid, realCallingUid, "Activity start")) {
Craig Mautner6170f732013-04-02 13:05:23 -07001662 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001663 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautneree36c772014-07-16 14:56:05 -07001664 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001665 ActivityOptions.abort(options);
1666 return ActivityManager.START_SWITCHES_CANCELED;
1667 }
1668 }
1669
1670 if (mService.mDidAppSwitch) {
1671 // This is the second allowed switch since we stopped switches,
1672 // so now just generally allow switches. Use case: user presses
1673 // home (switches disabled, switch to home, mDidAppSwitch now true);
1674 // user taps a home icon (coming from home so allowed, we hit here
1675 // and now allow anyone to switch again).
1676 mService.mAppSwitchesAllowedTime = 0;
1677 } else {
1678 mService.mDidAppSwitch = true;
1679 }
1680
Craig Mautneree36c772014-07-16 14:56:05 -07001681 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001682
Dianne Hackborn91097de2014-04-04 18:02:06 -07001683 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001684 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001685
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001686 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001687 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001688 // activity start, but we are not actually doing an activity
1689 // switch... just dismiss the keyguard now, because we
1690 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001691 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001692 }
1693 return err;
1694 }
1695
Svet Ganov99b60432015-06-27 13:15:22 -07001696 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001697 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001698 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1699 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001700 == PackageManager.PERMISSION_DENIED) {
1701 return ACTIVITY_RESTRICTION_PERMISSION;
1702 }
1703
Christopher Tateff7add02015-08-17 10:23:22 -07001704 if (activityInfo.permission == null) {
1705 return ACTIVITY_RESTRICTION_NONE;
1706 }
1707
Svet Ganov99b60432015-06-27 13:15:22 -07001708 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1709 if (opCode == AppOpsManager.OP_NONE) {
1710 return ACTIVITY_RESTRICTION_NONE;
1711 }
1712
1713 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1714 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001715 if (!ignoreTargetSecurity) {
1716 return ACTIVITY_RESTRICTION_APPOP;
1717 }
Svet Ganov99b60432015-06-27 13:15:22 -07001718 }
1719
1720 return ACTIVITY_RESTRICTION_NONE;
1721 }
1722
Svetoslav7008b512015-06-24 18:47:07 -07001723 private int getActionRestrictionForCallingPackage(String action,
1724 String callingPackage, int callingPid, int callingUid) {
1725 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001726 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001727 }
1728
1729 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1730 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001731 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001732 }
1733
1734 final PackageInfo packageInfo;
1735 try {
1736 packageInfo = mService.mContext.getPackageManager()
1737 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1738 } catch (PackageManager.NameNotFoundException e) {
1739 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001740 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001741 }
1742
1743 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001744 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001745 }
1746
1747 if (mService.checkPermission(permission, callingPid, callingUid) ==
1748 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001749 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001750 }
1751
1752 final int opCode = AppOpsManager.permissionToOpCode(permission);
1753 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001754 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001755 }
1756
1757 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1758 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001759 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001760 }
1761
Svet Ganov99b60432015-06-27 13:15:22 -07001762 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001763 }
1764
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001765 ActivityStack computeStackFocus(ActivityRecord r, boolean newTask) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001766 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001767
1768 // On leanback only devices we should keep all activities in the same stack.
1769 if (!mLeanbackOnlyDevice &&
1770 (r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001771
1772 ActivityStack stack;
1773
Wale Ogunwale7d701172015-03-11 15:36:30 -07001774 if (task != null && task.stack != null) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001775 stack = task.stack;
1776 if (stack.isOnHomeDisplay()) {
1777 if (mFocusedStack != stack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001778 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1779 "computeStackFocus: Setting " + "focused stack to r=" + r
1780 + " task=" + task);
Craig Mautnere0a38842013-12-16 16:14:02 -08001781 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001782 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001783 "computeStackFocus: Focused stack already=" + mFocusedStack);
Craig Mautnere0a38842013-12-16 16:14:02 -08001784 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001785 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001786 return stack;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001787 }
1788
Craig Mautnere0a38842013-12-16 16:14:02 -08001789 final ActivityContainer container = r.mInitialActivityContainer;
1790 if (container != null) {
1791 // The first time put it on the desired stack, after this put on task stack.
1792 r.mInitialActivityContainer = null;
1793 return container.mStack;
1794 }
1795
Craig Mautner1b4bf852014-05-26 15:06:32 -07001796 if (mFocusedStack != mHomeStack && (!newTask ||
1797 mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001798 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001799 "computeStackFocus: Have a focused stack=" + mFocusedStack);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001800 return mFocusedStack;
1801 }
1802
Wale Ogunwale706ed792015-08-02 10:29:44 -07001803 // We first try to put the task in the first dynamic stack.
Craig Mautnere0a38842013-12-16 16:14:02 -08001804 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1805 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001806 stack = homeDisplayStacks.get(stackNdx);
Wale Ogunwale706ed792015-08-02 10:29:44 -07001807 final boolean isDynamicStack = stack.mStackId >= FIRST_DYNAMIC_STACK_ID;
1808 if (isDynamicStack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001809 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001810 "computeStackFocus: Setting focused stack=" + stack);
1811 return stack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001812 }
1813 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001814
Wale Ogunwale706ed792015-08-02 10:29:44 -07001815 // If there is no suitable dynamic stack then we figure out which static stack to use.
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001816 stack = getStack(
Wale Ogunwale706ed792015-08-02 10:29:44 -07001817 task != null
1818 ? task.getLaunchStackId(mFocusedStack) : FULLSCREEN_WORKSPACE_STACK_ID,
Wale Ogunwale040b4702015-08-06 18:10:50 -07001819 CREATE_IF_NEEDED, ON_TOP);
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001820 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS, "computeStackFocus: New stack r="
1821 + r + " stackId=" + stack.mStackId);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001822 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001823 }
1824 return mHomeStack;
1825 }
1826
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001827 boolean setFocusedStack(ActivityRecord r, String reason) {
1828 if (r == null) {
1829 // Not sure what you are trying to do, but it is not going to work...
1830 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001831 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001832 final TaskRecord task = r.task;
1833 if (task == null || task.stack == null) {
1834 Slog.w(TAG, "Can't set focus stack for r=" + r + " task=" + task);
1835 return false;
1836 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001837 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001838 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001839 }
1840
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001841 final int startActivityUncheckedLocked(final ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001842 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001843 boolean doResume, Bundle options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001844 final Intent intent = r.intent;
1845 final int callingUid = r.launchedFromUid;
1846
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001847 // In some flows in to this function, we retrieve the task record and hold on to it
1848 // without a lock before calling back in to here... so the task at this point may
1849 // not actually be in recents. Check for that, and if it isn't in recents just
1850 // consider it invalid.
1851 if (inTask != null && !inTask.inRecents) {
1852 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1853 inTask = null;
1854 }
1855
Craig Mautnerad400af2014-08-13 16:41:36 -07001856 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001857 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1858 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001859
Craig Mautnera228ae92014-07-09 05:44:55 -07001860 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001861 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001862 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001863 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1864 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1865 "\"singleInstance\" or \"singleTask\"");
1866 launchFlags &=
1867 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1868 } else {
1869 switch (r.info.documentLaunchMode) {
1870 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1871 break;
1872 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1873 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1874 break;
1875 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1876 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1877 break;
1878 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1879 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1880 break;
1881 }
1882 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001883
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001884 final boolean launchTaskBehind = r.mLaunchTaskBehind
1885 && !launchSingleTask && !launchSingleInstance
1886 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001887
Wale Ogunwale7d701172015-03-11 15:36:30 -07001888 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0
1889 && r.resultTo.task.stack != null) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001890 // For whatever reason this activity is being launched into a new
1891 // task... yet the caller has requested a result back. Well, that
1892 // is pretty messed up, so instead immediately send back a cancel
1893 // and let the new task continue launched as normal without a
1894 // dependency on its originator.
1895 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1896 r.resultTo.task.stack.sendActivityResultLocked(-1,
1897 r.resultTo, r.resultWho, r.requestCode,
1898 Activity.RESULT_CANCELED, null);
1899 r.resultTo = null;
1900 }
1901
1902 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1903 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1904 }
1905
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001906 // If we are actually going to launch in to a new task, there are some cases where
1907 // we further want to do multiple task.
1908 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1909 if (launchTaskBehind
1910 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
1911 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1912 }
1913 }
1914
Craig Mautner8849a5e2013-04-02 16:41:03 -07001915 // We'll invoke onUserLeaving before onPause only if the launching
1916 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001917 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001918 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
1919 "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001920
1921 // If the caller has asked not to resume at this point, we make note
1922 // of this in the record so that we can skip it when trying to find
1923 // the top running activity.
Chong Zhang45c25ce2015-08-10 22:18:26 -07001924 if (!doResume || !okToShowLocked(r)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001925 r.delayedResume = true;
Chong Zhang45c25ce2015-08-10 22:18:26 -07001926 doResume = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001927 }
1928
Craig Mautnera254cd72014-05-25 16:47:39 -07001929 ActivityRecord notTop =
1930 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001931
1932 // If the onlyIfNeeded flag is set, then we can do this if the activity
1933 // being launched is the same as the one making the call... or, as
1934 // a special case, if we do not know the caller then we count the
1935 // current top activity as the caller.
1936 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1937 ActivityRecord checkedCaller = sourceRecord;
1938 if (checkedCaller == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001939 checkedCaller = mFocusedStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001940 }
1941 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1942 // Caller is not the same as launcher, so always needed.
1943 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1944 }
1945 }
1946
Craig Mautner8849a5e2013-04-02 16:41:03 -07001947 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001948 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001949
1950 // If the caller is not coming from another activity, but has given us an
1951 // explicit task into which they would like us to launch the new activity,
1952 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001953 if (sourceRecord == null && inTask != null && inTask.stack != null) {
1954 final Intent baseIntent = inTask.getBaseIntent();
1955 final ActivityRecord root = inTask.getRootActivity();
1956 if (baseIntent == null) {
1957 ActivityOptions.abort(options);
1958 throw new IllegalArgumentException("Launching into task without base intent: "
1959 + inTask);
1960 }
1961
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001962 // If this task is empty, then we are adding the first activity -- it
1963 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001964 if (launchSingleInstance || launchSingleTask) {
1965 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
1966 ActivityOptions.abort(options);
1967 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
1968 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001969 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001970 if (root != null) {
1971 ActivityOptions.abort(options);
1972 throw new IllegalArgumentException("Caller with inTask " + inTask
1973 + " has root " + root + " but target is singleInstance/Task");
1974 }
1975 }
1976
1977 // If task is empty, then adopt the interesting intent launch flags in to the
1978 // activity being started.
1979 if (root == null) {
1980 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
1981 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
1982 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
1983 launchFlags = (launchFlags&~flagsOfInterest)
1984 | (baseIntent.getFlags()&flagsOfInterest);
1985 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001986 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07001987 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001988
Dianne Hackborn962d5352014-08-29 16:27:40 -07001989 // If the task is not empty and the caller is asking to start it as the root
1990 // of a new task, then we don't actually want to start this on the task. We
1991 // will bring the task to the front, and possibly give it a new intent.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001992 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07001993 addingToTask = false;
1994
1995 } else {
1996 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001997 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07001998
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001999 reuseTask = inTask;
2000 } else {
2001 inTask = null;
2002 }
2003
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002004 if (inTask == null) {
2005 if (sourceRecord == null) {
2006 // This activity is not being started from another... in this
2007 // case we -always- start a new task.
2008 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
2009 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
2010 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
2011 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2012 }
2013 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
2014 // The original activity who is starting us is running as a single
2015 // instance... this new activity it is starting must go on its
2016 // own task.
2017 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2018 } else if (launchSingleInstance || launchSingleTask) {
2019 // The activity being started is a single instance... it always
2020 // gets launched into its own task.
2021 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2022 }
2023 }
2024
2025 ActivityInfo newTaskInfo = null;
2026 Intent newTaskIntent = null;
2027 ActivityStack sourceStack;
2028 if (sourceRecord != null) {
2029 if (sourceRecord.finishing) {
2030 // If the source is finishing, we can't further count it as our source. This
2031 // is because the task it is associated with may now be empty and on its way out,
2032 // so we don't want to blindly throw it in to that task. Instead we will take
2033 // the NEW_TASK flow and try to find a task for it. But save the task information
2034 // so it can be used when creating the new task.
2035 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
2036 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
2037 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
2038 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2039 newTaskInfo = sourceRecord.info;
2040 newTaskIntent = sourceRecord.task.intent;
2041 }
2042 sourceRecord = null;
2043 sourceStack = null;
2044 } else {
2045 sourceStack = sourceRecord.task.stack;
2046 }
2047 } else {
2048 sourceStack = null;
2049 }
2050
2051 boolean movedHome = false;
2052 ActivityStack targetStack;
2053
2054 intent.setFlags(launchFlags);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002055 final boolean noAnimation = (launchFlags & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002056
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002057 // We may want to try to place the new activity in to an existing task. We always
2058 // do this if the target activity is singleTask or singleInstance; we will also do
2059 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
2060 // us to still place it in a new task: multi task, always doc mode, or being asked to
2061 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07002062 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
2063 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07002064 || launchSingleInstance || launchSingleTask) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002065 // If bring to front is requested, and no result is requested and we have not
2066 // been given an explicit task to launch in to, and
Craig Mautner8849a5e2013-04-02 16:41:03 -07002067 // we can find a task that was started with this same
2068 // component, then instead of launching bring that one to the front.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002069 if (inTask == null && r.resultTo == null) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002070 // See if there is a task to bring to the front. If this is
2071 // a SINGLE_INSTANCE activity, there can be one and only one
2072 // instance of it in the history, and it is always in its own
2073 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07002074 ActivityRecord intentActivity = !launchSingleInstance ?
2075 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002076 if (intentActivity != null) {
Jason Monk25d237b2015-06-19 10:39:39 -04002077 // When the flags NEW_TASK and CLEAR_TASK are set, then the task gets reused
2078 // but still needs to be a lock task mode violation since the task gets
2079 // cleared out and the device would otherwise leave the locked task.
2080 if (isLockTaskModeViolation(intentActivity.task,
2081 (launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
2082 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))) {
Craig Mautner6cd6cec2015-04-01 00:02:12 -07002083 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07002084 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08002085 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2086 }
Craig Mautner29219d92013-04-16 20:19:12 -07002087 if (r.task == null) {
2088 r.task = intentActivity.task;
2089 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002090 if (intentActivity.task.intent == null) {
2091 // This task was started because of movement of
2092 // the activity based on affinity... now that we
2093 // are actually launching it, we can assign the
2094 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07002095 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002096 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002097 targetStack = intentActivity.task.stack;
2098 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002099 // If the target task is not in the front, then we need
2100 // to bring it to the front... except... well, with
2101 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
2102 // to have the same behavior as if a new instance was
2103 // being started, which means not bringing it to the front
2104 // if the caller is not itself in the front.
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002105 final ActivityStack focusStack = getFocusedStack();
2106 ActivityRecord curTop = (focusStack == null)
2107 ? null : focusStack.topRunningNonDelayedActivityLocked(notTop);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002108 boolean movedToFront = false;
Craig Mautner7504d7b2013-09-17 10:50:53 -07002109 if (curTop != null && (curTop.task != intentActivity.task ||
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002110 curTop.task != focusStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002111 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07002112 if (sourceRecord == null || (sourceStack.topActivity() != null &&
2113 sourceStack.topActivity().task == sourceRecord.task)) {
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002114 // We really do want to push this one into the user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07002115 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002116 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
2117 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002118 movedHome = true;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002119 targetStack.moveTaskToFrontLocked(intentActivity.task, noAnimation,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002120 options, r.appTimeTracker, "bringingFoundTaskToFront");
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002121 movedToFront = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002122 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07002123 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2124 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07002125 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07002126 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002127 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002128 options = null;
2129 }
2130 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002131 if (!movedToFront && doResume) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002132 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Bring to front target: " + targetStack
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002133 + " from " + intentActivity);
2134 targetStack.moveToFront("intentActivityFound");
2135 }
2136
Craig Mautner8849a5e2013-04-02 16:41:03 -07002137 // If the caller has requested that the target task be
2138 // reset, then do so.
2139 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
2140 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
2141 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002142 if ((startFlags & ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002143 // We don't need to start a new activity, and
2144 // the client said not to do anything if that
2145 // is the case, so this is it! And for paranoia, make
2146 // sure we have correctly resumed the top activity.
2147 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002148 resumeTopActivitiesLocked(targetStack, null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002149
2150 // Make sure to notify Keyguard as well if we are not running an app
2151 // transition later.
2152 if (!movedToFront) {
2153 notifyActivityDrawnForKeyguard();
2154 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002155 } else {
2156 ActivityOptions.abort(options);
2157 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002158 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002159 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2160 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002161 if ((launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002162 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002163 // The caller has requested to completely replace any
2164 // existing task with its new activity. Well that should
2165 // not be too hard...
2166 reuseTask = intentActivity.task;
2167 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07002168 reuseTask.setIntent(r);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002169 } else if ((launchFlags & FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07002170 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002171 // In this situation we want to remove all activities
2172 // from the task up to the one being started. In most
2173 // cases this means we are resetting the task to its
2174 // initial state.
2175 ActivityRecord top =
2176 intentActivity.task.performClearTaskLocked(r, launchFlags);
2177 if (top != null) {
2178 if (top.frontOfTask) {
2179 // Activity aliases may mean we use different
2180 // intents for the top activity, so make sure
2181 // the task now has the identity of the new
2182 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07002183 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002184 }
2185 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
2186 r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002187 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002188 } else {
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002189 // A special case: we need to start the activity because it is not
2190 // currently running, and the caller has asked to clear the current
2191 // task to have this activity at the top.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002192 addingToTask = true;
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002193 // Now pretend like this activity is being started by the top of its
2194 // task, so it is put in the right place.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002195 sourceRecord = intentActivity;
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002196 TaskRecord task = sourceRecord.task;
2197 if (task != null && task.stack == null) {
2198 // Target stack got cleared when we all activities were removed
2199 // above. Go ahead and reset it.
2200 targetStack = computeStackFocus(sourceRecord, false /* newTask */);
2201 targetStack.addTask(
2202 task, !launchTaskBehind /* toTop */, false /* moving */);
2203 }
2204
Craig Mautner8849a5e2013-04-02 16:41:03 -07002205 }
2206 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
2207 // In this case the top activity on the task is the
2208 // same as the one being launched, so we take that
2209 // as a request to bring the task to the foreground.
2210 // If the top activity in the task is the root
2211 // activity, deliver this new intent to it if it
2212 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07002213 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07002214 && intentActivity.realActivity.equals(r.realActivity)) {
2215 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
2216 intentActivity.task);
2217 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07002218 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002219 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002220 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
2221 r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002222 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
2223 // In this case we are launching the root activity
2224 // of the task, but with a different intent. We
2225 // should start a new instance on top.
2226 addingToTask = true;
2227 sourceRecord = intentActivity;
2228 }
2229 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
2230 // In this case an activity is being launched in to an
2231 // existing task, without resetting that task. This
2232 // is typically the situation of launching an activity
2233 // from a notification or shortcut. We want to place
2234 // the new activity on top of the current task.
2235 addingToTask = true;
2236 sourceRecord = intentActivity;
2237 } else if (!intentActivity.task.rootWasReset) {
2238 // In this case we are launching in to an existing task
2239 // that has not yet been started from its front door.
2240 // The current task has been brought to the front.
2241 // Ideally, we'd probably like to place this new task
2242 // at the bottom of its stack, but that's a little hard
2243 // to do with the current organization of the code so
2244 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07002245 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002246 }
2247 if (!addingToTask && reuseTask == null) {
2248 // We didn't do anything... but it was needed (a.k.a., client
2249 // don't use that intent!) And for paranoia, make
2250 // sure we have correctly resumed the top activity.
2251 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002252 targetStack.resumeTopActivityLocked(null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002253 if (!movedToFront) {
2254 // Make sure to notify Keyguard as well if we are not running an app
2255 // transition later.
2256 notifyActivityDrawnForKeyguard();
2257 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002258 } else {
2259 ActivityOptions.abort(options);
2260 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002261 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002262 return ActivityManager.START_TASK_TO_FRONT;
2263 }
2264 }
2265 }
2266 }
2267
2268 //String uri = r.intent.toURI();
2269 //Intent intent2 = new Intent(uri);
2270 //Slog.i(TAG, "Given intent: " + r.intent);
2271 //Slog.i(TAG, "URI is: " + uri);
2272 //Slog.i(TAG, "To intent: " + intent2);
2273
2274 if (r.packageName != null) {
2275 // If the activity being launched is the same as the one currently
2276 // at the top, then we need to check if it should only be launched
2277 // once.
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002278 ActivityStack topStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002279 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002280 if (top != null && r.resultTo == null) {
2281 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2282 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07002283 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07002284 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002285 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
2286 top.task);
2287 // For paranoia, make sure we have correctly
2288 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002289 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002290 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002291 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002292 }
2293 ActivityOptions.abort(options);
2294 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2295 // We don't need to start a new activity, and
2296 // the client said not to do anything if that
2297 // is the case, so this is it!
2298 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2299 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002300 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002301 return ActivityManager.START_DELIVERED_TO_TOP;
2302 }
2303 }
2304 }
2305 }
2306
2307 } else {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002308 if (r.resultTo != null && r.resultTo.task.stack != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002309 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2310 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002311 }
2312 ActivityOptions.abort(options);
2313 return ActivityManager.START_CLASS_NOT_FOUND;
2314 }
2315
2316 boolean newTask = false;
2317 boolean keepCurTransition = false;
2318
Craig Mautnerbb742462014-07-07 15:28:55 -07002319 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002320 sourceRecord.task : null;
2321
Craig Mautner8849a5e2013-04-02 16:41:03 -07002322 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002323 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002324 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002325 newTask = true;
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002326 targetStack = computeStackFocus(r, newTask);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002327 if (doResume) {
2328 targetStack.moveToFront("startingNewTask");
2329 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002330
Craig Mautner8849a5e2013-04-02 16:41:03 -07002331 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002332 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2333 newTaskInfo != null ? newTaskInfo : r.info,
2334 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002335 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2336 taskToAffiliate);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002337 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2338 "Starting new activity " + r + " in new task " + r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002339 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002340 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002341 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002342 if (isLockTaskModeViolation(r.task)) {
2343 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2344 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2345 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002346 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002347 if ((launchFlags &
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002348 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2349 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002350 // Caller wants to appear on home activity, so before starting
2351 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002352 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002353 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002354 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002355 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002356 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002357 if (isLockTaskModeViolation(sourceTask)) {
2358 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2359 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2360 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002361 targetStack = sourceTask.stack;
Chong Zhang45c25ce2015-08-10 22:18:26 -07002362 if (doResume) {
2363 targetStack.moveToFront("sourceStackToFront");
2364 }
Craig Mautner737fae22014-10-15 12:52:10 -07002365 final TaskRecord topTask = targetStack.topTask();
2366 if (topTask != sourceTask) {
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002367 targetStack.moveTaskToFrontLocked(sourceTask, noAnimation, options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002368 r.appTimeTracker, "sourceTaskToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002369 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002370 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002371 // In this case, we are adding the activity to an existing
2372 // task, but the caller has asked to clear that task if the
2373 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002374 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002375 keepCurTransition = true;
2376 if (top != null) {
2377 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002378 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002379 // For paranoia, make sure we have correctly
2380 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002381 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002382 if (doResume) {
2383 targetStack.resumeTopActivityLocked(null);
2384 }
2385 ActivityOptions.abort(options);
2386 return ActivityManager.START_DELIVERED_TO_TOP;
2387 }
2388 } else if (!addingToTask &&
2389 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2390 // In this case, we are launching an activity in our own task
2391 // that may already be running somewhere in the history, and
2392 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002393 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002394 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002395 final TaskRecord task = top.task;
2396 task.moveActivityToFrontLocked(top);
2397 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002398 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002399 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002400 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002401 if (doResume) {
2402 targetStack.resumeTopActivityLocked(null);
2403 }
2404 return ActivityManager.START_DELIVERED_TO_TOP;
2405 }
2406 }
2407 // An existing activity is starting this new activity, so we want
2408 // to keep the new one in the same task as the one that is starting
2409 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002410 r.setTask(sourceTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002411 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002412 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002413
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002414 } else if (inTask != null) {
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002415 // The caller is asking that the new activity be started in an explicit
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002416 // task it has provided to us.
2417 if (isLockTaskModeViolation(inTask)) {
2418 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2419 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2420 }
2421 targetStack = inTask.stack;
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002422 targetStack.moveTaskToFrontLocked(inTask, noAnimation, options, r.appTimeTracker,
2423 "inTaskToFront");
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002424
2425 // Check whether we should actually launch the new activity in to the task,
2426 // or just reuse the current activity on top.
2427 ActivityRecord top = inTask.getTopActivity();
2428 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2429 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2430 || launchSingleTop || launchSingleTask) {
2431 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2432 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2433 // We don't need to start a new activity, and
2434 // the client said not to do anything if that
2435 // is the case, so this is it!
2436 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2437 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002438 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002439 return ActivityManager.START_DELIVERED_TO_TOP;
2440 }
2441 }
2442
Dianne Hackborn962d5352014-08-29 16:27:40 -07002443 if (!addingToTask) {
2444 // We don't actually want to have this activity added to the task, so just
2445 // stop here but still tell the caller that we consumed the intent.
2446 ActivityOptions.abort(options);
2447 return ActivityManager.START_TASK_TO_FRONT;
2448 }
2449
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002450 r.setTask(inTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002451 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002452 + " in explicit task " + r.task);
2453
Craig Mautner8849a5e2013-04-02 16:41:03 -07002454 } else {
2455 // This not being started from an existing activity, and not part
2456 // of a new task... just put it in the top task, though these days
2457 // this case should never happen.
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002458 targetStack = computeStackFocus(r, newTask);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002459 if (doResume) {
2460 targetStack.moveToFront("addingToTopTask");
2461 }
Craig Mautner1602ec22013-05-12 10:24:27 -07002462 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002463 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002464 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002465 mWindowManager.moveTaskToTop(r.task.taskId);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002466 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Craig Mautner8849a5e2013-04-02 16:41:03 -07002467 + " in new guessed " + r.task);
2468 }
2469
2470 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002471 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002472
Craig Mautner76e2a762014-06-24 09:05:43 -07002473 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2474 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2475 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002476 if (newTask) {
2477 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2478 }
2479 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002480 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002481 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002482 if (!launchTaskBehind && doResume) {
Craig Mautner299f9602015-01-26 09:47:33 -08002483 mService.setFocusedActivityLocked(r, "startedActivity");
Craig Mautnerbb742462014-07-07 15:28:55 -07002484 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002485 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002486 return ActivityManager.START_SUCCESS;
2487 }
2488
Craig Mautneree36c772014-07-16 14:56:05 -07002489 final void doPendingActivityLaunchesLocked(boolean doResume) {
2490 while (!mPendingActivityLaunches.isEmpty()) {
2491 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Craig Mautneraa9b0f12014-07-17 10:50:18 -07002492 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002493 doResume && mPendingActivityLaunches.isEmpty(), null, null);
Craig Mautneree36c772014-07-16 14:56:05 -07002494 }
2495 }
2496
Craig Mautner7f13ed32014-07-28 14:00:35 -07002497 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002498 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2499 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002500 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002501 mPendingActivityLaunches.remove(palNdx);
2502 }
2503 }
2504 }
2505
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002506 void setLaunchSource(int uid) {
2507 mLaunchingActivity.setWorkSource(new WorkSource(uid));
2508 }
2509
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002510 void acquireLaunchWakelock() {
2511 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2512 throw new IllegalStateException("Calling must be system uid");
2513 }
2514 mLaunchingActivity.acquire();
2515 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2516 // To be safe, don't allow the wake lock to be held for too long.
2517 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2518 }
2519 }
2520
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002521 /**
2522 * Called when the frontmost task is idle.
2523 * @return the state of mService.mBooting before this was called.
2524 */
2525 private boolean checkFinishBootingLocked() {
2526 final boolean booting = mService.mBooting;
2527 boolean enableScreen = false;
2528 mService.mBooting = false;
2529 if (!mService.mBooted) {
2530 mService.mBooted = true;
2531 enableScreen = true;
2532 }
2533 if (booting || enableScreen) {
2534 mService.postFinishBooting(booting, enableScreen);
2535 }
2536 return booting;
2537 }
2538
Craig Mautnerf3333272013-04-22 10:55:53 -07002539 // Checked.
2540 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2541 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002542 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002543
Craig Mautnerf3333272013-04-22 10:55:53 -07002544 ArrayList<ActivityRecord> stops = null;
2545 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07002546 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07002547 int NS = 0;
2548 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002549 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002550 boolean activityRemoved = false;
2551
Wale Ogunwale7d701172015-03-11 15:36:30 -07002552 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002553 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002554 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
2555 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002556 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2557 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002558 if (fromTimeout) {
2559 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002560 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002561
2562 // This is a hack to semi-deal with a race condition
2563 // in the client where it can be constructed with a
2564 // newer configuration from when we asked it to launch.
2565 // We'll update with whatever configuration it now says
2566 // it used to launch.
2567 if (config != null) {
2568 r.configuration = config;
2569 }
2570
2571 // We are now idle. If someone is waiting for a thumbnail from
2572 // us, we can now deliver.
2573 r.idle = true;
2574
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002575 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Dianne Hackborn9449a612014-10-01 15:53:28 -07002576 if (isFrontStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002577 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002578 }
2579 }
2580
2581 if (allResumedActivitiesIdle()) {
2582 if (r != null) {
2583 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002584 }
2585
2586 if (mLaunchingActivity.isHeld()) {
2587 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2588 if (VALIDATE_WAKE_LOCK_CALLER &&
2589 Binder.getCallingUid() != Process.myUid()) {
2590 throw new IllegalStateException("Calling must be system uid");
2591 }
2592 mLaunchingActivity.release();
2593 }
2594 ensureActivitiesVisibleLocked(null, 0);
Craig Mautnerf3333272013-04-22 10:55:53 -07002595 }
2596
2597 // Atomically retrieve all of the other things to do.
2598 stops = processStoppingActivitiesLocked(true);
2599 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002600 if ((NF = mFinishingActivities.size()) > 0) {
2601 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002602 mFinishingActivities.clear();
2603 }
2604
Craig Mautnerf3333272013-04-22 10:55:53 -07002605 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002606 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07002607 mStartingUsers.clear();
2608 }
2609
Craig Mautnerf3333272013-04-22 10:55:53 -07002610 // Stop any activities that are scheduled to do so but have been
2611 // waiting for the next one to start.
2612 for (int i = 0; i < NS; i++) {
2613 r = stops.get(i);
2614 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002615 if (stack != null) {
2616 if (r.finishing) {
2617 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2618 } else {
2619 stack.stopActivityLocked(r);
2620 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002621 }
2622 }
2623
2624 // Finish any activities that are scheduled to do so but have been
2625 // waiting for the next one to start.
2626 for (int i = 0; i < NF; i++) {
2627 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07002628 final ActivityStack stack = r.task.stack;
2629 if (stack != null) {
2630 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
2631 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002632 }
2633
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002634 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002635 // Complete user switch
2636 if (startingUsers != null) {
2637 for (int i = 0; i < startingUsers.size(); i++) {
2638 mService.finishUserSwitch(startingUsers.get(i));
2639 }
2640 }
2641 // Complete starting up of background users
2642 if (mStartingBackgroundUsers.size() > 0) {
Amith Yamasani37a40c22015-06-17 13:25:42 -07002643 startingUsers = new ArrayList<UserState>(mStartingBackgroundUsers);
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002644 mStartingBackgroundUsers.clear();
2645 for (int i = 0; i < startingUsers.size(); i++) {
2646 mService.finishUserBoot(startingUsers.get(i));
2647 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002648 }
2649 }
2650
2651 mService.trimApplications();
2652 //dump();
2653 //mWindowManager.dump();
2654
Craig Mautnerf3333272013-04-22 10:55:53 -07002655 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002656 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002657 }
2658
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002659 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002660 }
2661
Craig Mautner8e569572013-10-11 17:36:59 -07002662 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002663 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002664 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2665 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002666 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2667 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2668 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002669 }
Craig Mautner19091252013-10-05 00:03:53 -07002670 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002671 }
2672
2673 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002674 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2675 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002676 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2677 stacks.get(stackNdx).closeSystemDialogsLocked();
2678 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002679 }
2680 }
2681
Craig Mautner93529a42013-10-04 15:03:13 -07002682 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002683 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002684 }
2685
Craig Mautner8d341ef2013-03-26 09:03:27 -07002686 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07002687 * Update the last used stack id for non-current user (current user's last
2688 * used stack is the focused stack)
2689 */
2690 void updateUserStackLocked(int userId, ActivityStack stack) {
2691 if (userId != mCurrentUser) {
2692 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
2693 }
2694 }
2695
2696 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07002697 * @return true if some activity was finished (or would have finished if doit were true).
2698 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07002699 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
2700 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002701 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002702 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2703 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002704 final int numStacks = stacks.size();
2705 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2706 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07002707 if (stack.finishDisabledPackageActivitiesLocked(
2708 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002709 didSomething = true;
2710 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002711 }
2712 }
2713 return didSomething;
2714 }
2715
Dianne Hackborna413dc02013-07-12 12:02:55 -07002716 void updatePreviousProcessLocked(ActivityRecord r) {
2717 // Now that this process has stopped, we may want to consider
2718 // it to be the previous app to try to keep around in case
2719 // the user wants to return to it.
2720
2721 // First, found out what is currently the foreground app, so that
2722 // we don't blow away the previous app if this activity is being
2723 // hosted by the process that is actually still the foreground.
2724 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002725 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2726 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002727 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2728 final ActivityStack stack = stacks.get(stackNdx);
2729 if (isFrontStack(stack)) {
2730 if (stack.mResumedActivity != null) {
2731 fgApp = stack.mResumedActivity.app;
2732 } else if (stack.mPausingActivity != null) {
2733 fgApp = stack.mPausingActivity.app;
2734 }
2735 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002736 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002737 }
2738 }
2739
2740 // Now set this one as the previous process, only if that really
2741 // makes sense to.
2742 if (r.app != null && fgApp != null && r.app != fgApp
2743 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002744 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002745 mService.mPreviousProcess = r.app;
2746 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2747 }
2748 }
2749
Craig Mautner05d29032013-05-03 13:40:13 -07002750 boolean resumeTopActivitiesLocked() {
2751 return resumeTopActivitiesLocked(null, null, null);
2752 }
2753
2754 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2755 Bundle targetOptions) {
2756 if (targetStack == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002757 targetStack = mFocusedStack;
Craig Mautner05d29032013-05-03 13:40:13 -07002758 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002759 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002760 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002761 if (isFrontStack(targetStack)) {
2762 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2763 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002764
Craig Mautnere0a38842013-12-16 16:14:02 -08002765 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2766 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002767 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2768 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002769 if (stack == targetStack) {
2770 // Already started above.
2771 continue;
2772 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002773 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002774 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002775 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002776 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002777 }
Craig Mautner05d29032013-05-03 13:40:13 -07002778 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002779 }
2780
Todd Kennedy539db512014-12-15 09:57:55 -08002781 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002782 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2783 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002784 final int numStacks = stacks.size();
2785 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2786 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08002787 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002788 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002789 }
2790 }
2791
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002792 void finishVoiceTask(IVoiceInteractionSession session) {
2793 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2794 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2795 final int numStacks = stacks.size();
2796 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2797 final ActivityStack stack = stacks.get(stackNdx);
2798 stack.finishVoiceTask(session);
2799 }
2800 }
2801 }
2802
Craig Mautner299f9602015-01-26 09:47:33 -08002803 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002804 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2805 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002806 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002807 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2808 // Caller wants the home activity moved with it. To accomplish this,
2809 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002810 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002811 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07002812 if (task.stack == null) {
2813 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2814 + task + " to front. Stack is null");
2815 return;
2816 }
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002817 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
2818 task.getTopActivity() == null ? null : task.getTopActivity().appTimeTracker,
2819 reason);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002820 if (DEBUG_STACK) Slog.d(TAG_STACK,
2821 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002822 }
2823
Craig Mautner967212c2013-04-13 21:10:58 -07002824 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07002825 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002826 }
2827
2828 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002829 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2830 if (activityContainer != null) {
2831 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002832 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002833 if (!createStaticStackIfNeeded
2834 || (stackId < FIRST_STATIC_STACK_ID || stackId > LAST_STATIC_STACK_ID)) {
2835 return null;
2836 }
2837 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002838 }
2839
Craig Mautner967212c2013-04-13 21:10:58 -07002840 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002841 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002842 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2843 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002844 }
2845 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002846 }
2847
Craig Mautner4a1cb222013-12-04 16:14:06 -08002848 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002849 ActivityRecord homeActivity = getHomeActivity();
2850 if (homeActivity != null) {
2851 return homeActivity.appToken;
2852 }
2853 return null;
2854 }
2855
2856 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002857 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002858 }
2859
2860 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002861 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2862 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2863 final TaskRecord task = tasks.get(taskNdx);
2864 if (task.isHomeTask()) {
2865 final ArrayList<ActivityRecord> activities = task.mActivities;
2866 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2867 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002868 if (r.isHomeActivity()
2869 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002870 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002871 }
2872 }
2873 }
2874 }
2875 return null;
2876 }
2877
Todd Kennedyca4d8422015-01-15 15:19:22 -08002878 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002879 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002880 ActivityContainer activityContainer =
2881 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002882 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002883 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2884 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002885 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002886 return activityContainer;
2887 }
2888
Craig Mautner34b73df2014-01-12 21:11:08 -08002889 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002890 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2891 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2892 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002893 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2894 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002895 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002896 }
2897 }
2898
Craig Mautner95da1082014-02-24 17:54:35 -08002899 void deleteActivityContainer(IActivityContainer container) {
2900 ActivityContainer activityContainer = (ActivityContainer)container;
2901 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002902 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2903 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08002904 final int stackId = activityContainer.mStackId;
2905 mActivityContainers.remove(stackId);
2906 mWindowManager.removeStack(stackId);
2907 }
2908 }
2909
Wale Ogunwale60454db2015-01-23 16:05:07 -08002910 void resizeStackLocked(int stackId, Rect bounds) {
2911 final ActivityStack stack = getStack(stackId);
2912 if (stack == null) {
2913 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2914 return;
2915 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002916
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002917 ActivityRecord r = stack.topRunningActivityLocked(null);
2918 final boolean resizeTasks = r != null && r.task.mResizeable;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002919
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002920 final IntArray changedTaskIds = new IntArray(stack.numTasks());
2921 final List<Configuration> newTaskConfigs = new ArrayList<>(stack.numTasks());
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002922 stack.mFullscreen = mWindowManager.resizeStack(
2923 stackId, bounds, resizeTasks, changedTaskIds, newTaskConfigs);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002924 for (int i = changedTaskIds.size() - 1; i >= 0; i--) {
2925 final TaskRecord task = anyTaskForIdLocked(changedTaskIds.get(i), false);
2926 if (task == null) {
2927 Slog.wtf(TAG, "Task in WindowManager, but not in ActivityManager???");
2928 continue;
2929 }
Wale Ogunwale706ed792015-08-02 10:29:44 -07002930 task.updateOverrideConfiguration(newTaskConfigs.get(i), bounds);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002931 }
2932
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002933 if (stack.mStackId == DOCKED_STACK_ID) {
2934 // Dock stack funness...Yay!
2935 if (stack.mFullscreen) {
2936 // The dock stack went fullscreen which is kinda like dismissing it.
2937 // In this case we make all other static stacks fullscreen and move all
2938 // docked stack tasks to the fullscreen stack.
2939 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2940 if (i != DOCKED_STACK_ID) {
2941 resizeStackLocked(i, null);
2942 }
2943 }
2944
2945 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2946 final int count = tasks.size();
2947 for (int i = 0; i < count; i++) {
2948 moveTaskToStackLocked(tasks.get(i).taskId,
2949 FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP, FORCE_FOCUS);
2950 }
2951
2952 // stack shouldn't contain anymore activities, so nothing to resume.
2953 r = null;
2954 } else {
2955 // Docked stacks occupy a dedicated region on screen so the size of all other
2956 // static stacks need to be adjusted so they don't overlap with the docked stack.
2957 final int leftChange = stack.mBounds.left - bounds.left;
2958 final int rightChange = stack.mBounds.right - bounds.right;
2959 final int topChange = stack.mBounds.top - bounds.top;
2960 final int bottomChange = stack.mBounds.bottom - bounds.bottom;
2961
2962 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2963 if (i != DOCKED_STACK_ID) {
2964 ActivityStack otherStack = getStack(i);
2965 if (otherStack != null) {
2966 tempRect.set(otherStack.mBounds);
2967 // We adjust the opposing sides of the other stacks to
2968 // the side in the dock stack that changed.
2969 tempRect.left -= rightChange;
2970 tempRect.right -= leftChange;
2971 tempRect.top -= bottomChange;
2972 tempRect.bottom -= topChange;
2973 resizeStackLocked(i, tempRect);
2974 }
2975 }
2976 }
2977
2978 }
2979 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002980 stack.setBounds(bounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002981
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002982 if (r != null) {
2983 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0);
2984 // And we need to make sure at this point that all other activities
2985 // are made visible with the correct configuration.
2986 ensureActivitiesVisibleLocked(r, 0);
2987 if (!updated) {
2988 resumeTopActivitiesLocked(stack, null, null);
2989 }
2990 }
2991 }
2992
2993 void resizeTaskLocked(TaskRecord task, Rect bounds) {
2994 if (!task.mResizeable) {
2995 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
2996 return;
2997 }
2998
2999 if (task.mBounds != null && task.mBounds.equals(bounds)) {
3000 // Nothing to do here...
3001 return;
3002 }
3003
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003004 if (!mWindowManager.isValidTaskId(task.taskId)) {
3005 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07003006 // All we can do for now is update the bounds so it can be used when the task is
3007 // added to window manager.
3008 task.mBounds = task.mLastNonFullscreenBounds = new Rect(bounds);
3009 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
3010 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07003011 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003012 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003013 return;
3014 }
3015
Wale Ogunwale040b4702015-08-06 18:10:50 -07003016 // The stack of a task is determined by its size (fullscreen vs non-fullscreen).
3017 // Place the task in the right stack if it isn't there already based on the requested
3018 // bounds.
3019 int stackId = task.stack.mStackId;
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -07003020 final boolean wasFrontStack = isFrontStack(task.stack);
Wale Ogunwale040b4702015-08-06 18:10:50 -07003021 if (bounds == null && stackId != FULLSCREEN_WORKSPACE_STACK_ID) {
3022 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003023 } else if (bounds != null
3024 && stackId != FREEFORM_WORKSPACE_STACK_ID && stackId != DOCKED_STACK_ID) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07003025 stackId = FREEFORM_WORKSPACE_STACK_ID;
3026 }
3027 if (stackId != task.stack.mStackId) {
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -07003028 final String reason = "resizeTask";
3029 final ActivityStack stack =
3030 moveTaskToStackUncheckedLocked(task, stackId, ON_TOP, reason);
3031 if (wasFrontStack) {
3032 // Since the stack was previously in front,
3033 // move the stack in which we are placing the task to the front.
3034 stack.moveToFront(reason);
3035 }
Wale Ogunwale040b4702015-08-06 18:10:50 -07003036 }
3037
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003038 final Configuration overrideConfig = mWindowManager.resizeTask(task.taskId, bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003039 if (task.updateOverrideConfiguration(overrideConfig, bounds)) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003040 ActivityRecord r = task.topRunningActivityLocked(null);
Wale Ogunwale60454db2015-01-23 16:05:07 -08003041 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003042 final ActivityStack stack = task.stack;
Wale Ogunwale60454db2015-01-23 16:05:07 -08003043 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0);
3044 // And we need to make sure at this point that all other activities
3045 // are made visible with the correct configuration.
3046 ensureActivitiesVisibleLocked(r, 0);
3047 if (!updated) {
3048 resumeTopActivitiesLocked(stack, null, null);
3049 }
3050 }
3051 }
3052 }
3053
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003054 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003055 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3056 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08003057 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003058 }
3059
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003060 ActivityContainer activityContainer = new ActivityContainer(stackId);
3061 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003062 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08003063 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003064 }
3065
3066 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07003067 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003068 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
3069 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07003070 break;
3071 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003072 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003073 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003074 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003075 }
3076
Chong Zhang5dcb2752015-08-18 13:50:26 -07003077 /**
3078 * Restores a recent task to a stack
3079 * @param task The recent task to be restored.
3080 * @param stackId The stack to restore the task to (default launch stack will be used
3081 * if stackId is invalid).
3082 * @return true if the task has been restored successfully.
3083 */
3084 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
3085 if (stackId == INVALID_STACK_ID) {
3086 stackId = mLeanbackOnlyDevice ?
3087 mHomeStack.mStackId : task.getLaunchStackId(mFocusedStack);
3088 }
Wale Ogunwale706ed792015-08-02 10:29:44 -07003089 if (task.stack != null) {
3090 // Task has already been restored once. See if we need to do anything more
3091 if (task.stack.mStackId == stackId) {
3092 // Nothing else to do since it is already restored in the right stack.
3093 return true;
3094 }
3095 // Remove current stack association, so we can re-associate the task with the
3096 // right stack below.
Wale Ogunwale040b4702015-08-06 18:10:50 -07003097 task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003098 }
3099
3100 ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07003101 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003102
3103 if (stack == null) {
3104 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003105 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3106 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003107 return false;
3108 }
3109
3110 stack.addTask(task, false, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003111 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3112 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003113 final ArrayList<ActivityRecord> activities = task.mActivities;
3114 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003115 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003116 }
3117 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07003118 }
3119
Wale Ogunwale040b4702015-08-06 18:10:50 -07003120 /**
3121 * Moves the specified task record to the input stack id.
3122 * WARNING: This method performs an unchecked/raw move of the task and
3123 * can leave the system in an unstable state if used incorrectly.
3124 * Use {@link #moveTaskToStackLocked} to perform safe task movement
3125 * to a stack.
3126 * @param task Task to move.
3127 * @param stackId Id of stack to move task to.
3128 * @param toTop True if the task should be placed at the top of the stack.
3129 * @param reason Reason the task is been moved.
3130 * @return The stack the task was moved to.
3131 */
3132 private ActivityStack moveTaskToStackUncheckedLocked(
3133 TaskRecord task, int stackId, boolean toTop, String reason) {
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003134 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwale040b4702015-08-06 18:10:50 -07003135 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
3136 if (task.stack != null) {
3137 task.stack.removeTask(task, reason, MOVING);
3138 }
3139 stack.addTask(task, toTop, MOVING);
3140 return stack;
3141 }
3142
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003143 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003144 final TaskRecord task = anyTaskForIdLocked(taskId);
3145 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08003146 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003147 return;
3148 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003149 final String reason = "moveTaskToStack";
3150 final ActivityRecord top = task.topRunningActivityLocked(null);
3151 final boolean adjustFocus = forceFocus || mService.mFocusedActivity == top;
3152 final ActivityStack stack =
3153 moveTaskToStackUncheckedLocked(task, stackId, toTop, reason);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003154
3155 // Make sure the task has the appropriate bounds/size for the stack it is in.
3156 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003157 resizeTaskLocked(task, stack.mBounds);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003158 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID
3159 && task.mBounds == null && task.mLastNonFullscreenBounds != null) {
3160 resizeTaskLocked(task, task.mLastNonFullscreenBounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003161 } else if (stackId == DOCKED_STACK_ID) {
3162 resizeTaskLocked(task, stack.mBounds);
3163 }
3164
3165 if (top != null && adjustFocus) {
3166 if (mService.mFocusedActivity != top) {
3167 mService.setFocusedActivityLocked(top, reason);
3168 } else {
3169 setFocusedStack(top, reason);
3170 }
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003171 }
3172
Stefan Kuhne54714cd2015-05-12 13:42:18 -07003173 // The task might have already been running and its visibility needs to be synchronized with
3174 // the visibility of the stack / windows.
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003175 ensureActivitiesVisibleLocked(null, 0);
Craig Mautner05d29032013-05-03 13:40:13 -07003176 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003177 }
3178
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003179 void positionTaskInStackLocked(int taskId, int stackId, int position) {
3180 final TaskRecord task = anyTaskForIdLocked(taskId);
3181 if (task == null) {
3182 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
3183 return;
3184 }
3185 ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07003186 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003187 mWindowManager.positionTaskInStack(taskId, stackId, position);
3188 final boolean stackChanged = task.stack != null && task.stack != stack;
3189 if (stackChanged) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07003190 task.stack.removeTask(task, "moveTaskToStack", MOVING);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003191 }
3192 stack.positionTask(task, position, stackChanged);
3193 // The task might have already been running and its visibility needs to be synchronized with
3194 // the visibility of the stack / windows.
3195 stack.ensureActivitiesVisibleLocked(null, 0);
3196 resumeTopActivitiesLocked();
3197 }
3198
Craig Mautnerac6f8432013-07-17 13:24:59 -07003199 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003200 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003201 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3202 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003203 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3204 final ActivityStack stack = stacks.get(stackNdx);
3205 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003206 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003207 continue;
3208 }
3209 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003210 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
3211 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003212 continue;
3213 }
3214 final ActivityRecord ar = stack.findTaskLocked(r);
3215 if (ar != null) {
3216 return ar;
3217 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003218 }
3219 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003220 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07003221 return null;
3222 }
3223
3224 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003225 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3226 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003227 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3228 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
3229 if (ar != null) {
3230 return ar;
3231 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003232 }
3233 }
3234 return null;
3235 }
3236
Craig Mautner8d341ef2013-03-26 09:03:27 -07003237 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003238 scheduleSleepTimeout();
3239 if (!mGoingToSleep.isHeld()) {
3240 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003241 if (mLaunchingActivity.isHeld()) {
3242 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3243 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003244 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003245 mLaunchingActivity.release();
3246 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003247 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003248 }
Amith Yamasanice15e152013-09-19 12:30:32 -07003249 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003250 }
3251
3252 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003253 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003254
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003255 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003256 final long endTime = System.currentTimeMillis() + timeout;
3257 while (true) {
3258 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003259 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3260 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003261 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3262 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
3263 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003264 }
3265 if (cantShutdown) {
3266 long timeRemaining = endTime - System.currentTimeMillis();
3267 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003268 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003269 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003270 } catch (InterruptedException e) {
3271 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003272 } else {
3273 Slog.w(TAG, "Activity manager shutdown timed out");
3274 timedout = true;
3275 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003276 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003277 } else {
3278 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003279 }
3280 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003281
3282 // Force checkReadyForSleep to complete.
3283 mSleepTimeout = true;
3284 checkReadyForSleepLocked();
3285
Craig Mautner8d341ef2013-03-26 09:03:27 -07003286 return timedout;
3287 }
3288
3289 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003290 removeSleepTimeouts();
3291 if (mGoingToSleep.isHeld()) {
3292 mGoingToSleep.release();
3293 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003294 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3295 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003296 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3297 final ActivityStack stack = stacks.get(stackNdx);
3298 stack.awakeFromSleepingLocked();
3299 if (isFrontStack(stack)) {
3300 resumeTopActivitiesLocked();
3301 }
Craig Mautner5314a402013-09-26 12:40:16 -07003302 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003303 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003304 mGoingToSleepActivities.clear();
3305 }
3306
3307 void activitySleptLocked(ActivityRecord r) {
3308 mGoingToSleepActivities.remove(r);
3309 checkReadyForSleepLocked();
3310 }
3311
3312 void checkReadyForSleepLocked() {
3313 if (!mService.isSleepingOrShuttingDown()) {
3314 // Do not care.
3315 return;
3316 }
3317
3318 if (!mSleepTimeout) {
3319 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003320 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3321 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003322 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3323 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3324 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003325 }
3326
3327 if (mStoppingActivities.size() > 0) {
3328 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003329 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003330 + mStoppingActivities.size() + " activities");
3331 scheduleIdleLocked();
3332 dontSleep = true;
3333 }
3334
3335 if (mGoingToSleepActivities.size() > 0) {
3336 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003337 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003338 + mGoingToSleepActivities.size() + " activities");
3339 dontSleep = true;
3340 }
3341
3342 if (dontSleep) {
3343 return;
3344 }
3345 }
3346
Craig Mautnere0a38842013-12-16 16:14:02 -08003347 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3348 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003349 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3350 stacks.get(stackNdx).goToSleep();
3351 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003352 }
3353
3354 removeSleepTimeouts();
3355
3356 if (mGoingToSleep.isHeld()) {
3357 mGoingToSleep.release();
3358 }
3359 if (mService.mShuttingDown) {
3360 mService.notifyAll();
3361 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003362 }
3363
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003364 boolean reportResumedActivityLocked(ActivityRecord r) {
3365 final ActivityStack stack = r.task.stack;
3366 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003367 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003368 }
3369 if (allResumedActivitiesComplete()) {
3370 ensureActivitiesVisibleLocked(null, 0);
3371 mWindowManager.executeAppTransition();
3372 return true;
3373 }
3374 return false;
3375 }
3376
Craig Mautner8d341ef2013-03-26 09:03:27 -07003377 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003378 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3379 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003380 int stackNdx = stacks.size() - 1;
3381 while (stackNdx >= 0) {
3382 stacks.get(stackNdx).handleAppCrashLocked(app);
3383 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003384 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003385 }
3386 }
3387
Jose Lima4b6c6692014-08-12 17:41:12 -07003388 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003389 final ActivityStack stack = r.task.stack;
3390 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003391 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3392 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003393 return false;
3394 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003395 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003396 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3397 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003398
3399 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003400 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003401 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003402 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003403 return true;
3404 }
3405
3406 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003407 if (visible && top.fullscreen) {
3408 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003409 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3410 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3411 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3412 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003413 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003414 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3415 // Only the activity set as currently visible behind should actively reset its
3416 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003417 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3418 "requestVisibleBehind: returning visible=" + visible
3419 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3420 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003421 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003422 }
3423
Jose Lima4b6c6692014-08-12 17:41:12 -07003424 stack.setVisibleBehindActivity(visible ? r : null);
3425 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003426 // Make the activity immediately above r opaque.
3427 final ActivityRecord next = stack.findNextTranslucentActivity(r);
3428 if (next != null) {
3429 mService.convertFromTranslucent(next.appToken);
3430 }
3431 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003432 if (top.app != null && top.app.thread != null) {
3433 // Notify the top app of the change.
3434 try {
3435 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3436 } catch (RemoteException e) {
3437 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003438 }
3439 return true;
3440 }
3441
Craig Mautnerbb742462014-07-07 15:28:55 -07003442 // Called when WindowManager has finished animating the launchingBehind activity to the back.
3443 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
3444 r.mLaunchTaskBehind = false;
3445 final TaskRecord task = r.task;
3446 task.setLastThumbnail(task.stack.screenshotActivities(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003447 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08003448 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07003449 mWindowManager.setAppVisibility(r.appToken, false);
3450 }
3451
3452 void scheduleLaunchTaskBehindComplete(IBinder token) {
3453 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3454 }
3455
Craig Mautnerde4ef022013-04-07 19:01:33 -07003456 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
Craig Mautner580ea812013-04-25 12:58:38 -07003457 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08003458 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3459 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003460 final int topStackNdx = stacks.size() - 1;
3461 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3462 final ActivityStack stack = stacks.get(stackNdx);
Jose Lima729cb232014-05-05 15:59:40 -07003463 stack.ensureActivitiesVisibleLocked(starting, configChanges);
Craig Mautner580ea812013-04-25 12:58:38 -07003464 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003465 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003466 }
3467
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003468 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3469 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3470 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3471 final int topStackNdx = stacks.size() - 1;
3472 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3473 final ActivityStack stack = stacks.get(stackNdx);
3474 stack.clearOtherAppTimeTrackers(except);
3475 }
3476 }
3477 }
3478
Craig Mautner8d341ef2013-03-26 09:03:27 -07003479 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003480 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3481 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003482 final int numStacks = stacks.size();
3483 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3484 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003485 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003486 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003487 }
3488 }
3489
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003490 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3491 // Examine all activities currently running in the process.
3492 TaskRecord firstTask = null;
3493 // Tasks is non-null only if two or more tasks are found.
3494 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003495 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3496 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003497 ActivityRecord r = app.activities.get(i);
3498 // First, if we find an activity that is in the process of being destroyed,
3499 // then we just aren't going to do anything for now; we want things to settle
3500 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003501 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003502 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003503 return;
3504 }
3505 // Don't consider any activies that are currently not in a state where they
3506 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003507 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3508 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003509 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003510 continue;
3511 }
3512 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003513 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003514 + " from " + r);
3515 if (firstTask == null) {
3516 firstTask = r.task;
3517 } else if (firstTask != r.task) {
3518 if (tasks == null) {
3519 tasks = new ArraySet<>();
3520 tasks.add(firstTask);
3521 }
3522 tasks.add(r.task);
3523 }
3524 }
3525 }
3526 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003527 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003528 return;
3529 }
3530 // If we have activities in multiple tasks that are in a position to be destroyed,
3531 // let's iterate through the tasks and release the oldest one.
3532 final int numDisplays = mActivityDisplays.size();
3533 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3534 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3535 // Step through all stacks starting from behind, to hit the oldest things first.
3536 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3537 final ActivityStack stack = stacks.get(stackNdx);
3538 // Try to release activities in this stack; if we manage to, we are done.
3539 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3540 return;
3541 }
3542 }
3543 }
3544 }
3545
Amith Yamasani37a40c22015-06-17 13:25:42 -07003546 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003547 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003548 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003549 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003550
Craig Mautner858d8a62013-04-23 17:08:34 -07003551 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003552 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3553 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003554 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003555 final ActivityStack stack = stacks.get(stackNdx);
3556 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003557 TaskRecord task = stack.topTask();
3558 if (task != null) {
3559 mWindowManager.moveTaskToTop(task.taskId);
3560 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003561 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003562 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003563
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003564 ActivityStack stack = getStack(restoreStackId);
3565 if (stack == null) {
3566 stack = mHomeStack;
3567 }
3568 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003569 if (stack.isOnHomeDisplay()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003570 moveHomeStack(homeInFront, "switchUserOnHomeDisplay");
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003571 TaskRecord task = stack.topTask();
3572 if (task != null) {
3573 mWindowManager.moveTaskToTop(task.taskId);
3574 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003575 } else {
3576 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003577 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003578 }
Craig Mautner93529a42013-10-04 15:03:13 -07003579 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003580 }
3581
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003582 /**
3583 * Add background users to send boot completed events to.
3584 * @param userId The user being started in the background
3585 * @param uss The state object for the user.
3586 */
Amith Yamasani37a40c22015-06-17 13:25:42 -07003587 public void startBackgroundUserLocked(int userId, UserState uss) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003588 mStartingBackgroundUsers.add(uss);
3589 }
3590
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003591 /** Checks whether the userid is a profile of the current user. */
3592 boolean isCurrentProfileLocked(int userId) {
3593 if (userId == mCurrentUser) return true;
3594 for (int i = 0; i < mService.mCurrentProfileIds.length; i++) {
3595 if (mService.mCurrentProfileIds[i] == userId) return true;
3596 }
3597 return false;
3598 }
3599
Chong Zhang45c25ce2015-08-10 22:18:26 -07003600 /** Checks whether the activity should be shown for current user. */
3601 boolean okToShowLocked(ActivityRecord r) {
3602 return r != null && (isCurrentProfileLocked(r.userId)
3603 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
3604 }
3605
Craig Mautnerde4ef022013-04-07 19:01:33 -07003606 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003607 ArrayList<ActivityRecord> stops = null;
3608
3609 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003610 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3611 ActivityRecord s = mStoppingActivities.get(activityNdx);
3612 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003613 if (DEBUG_ALL) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003614 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3615 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003616 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003617 if (s.finishing) {
3618 // If this activity is finishing, it is sitting on top of
3619 // everyone else but we now know it is no longer needed...
3620 // so get rid of it. Otherwise, we need to go through the
3621 // normal flow and hide it once we determine that it is
3622 // hidden by the activities in front of it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003623 if (DEBUG_ALL) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003624 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003625 }
3626 }
Craig Mautner8c14c152015-01-15 17:32:07 -08003627 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003628 if (DEBUG_ALL) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003629 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003630 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003631 }
3632 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003633 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003634 }
3635 }
3636
3637 return stops;
3638 }
3639
Craig Mautnercf910b02013-04-23 11:23:27 -07003640 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003641 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3642 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3643 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3644 final ActivityStack stack = stacks.get(stackNdx);
3645 final ActivityRecord r = stack.topRunningActivityLocked(null);
3646 final ActivityState state = r == null ? DESTROYED : r.state;
3647 if (isFrontStack(stack)) {
3648 if (r == null) Slog.e(TAG,
3649 "validateTop...: null top activity, stack=" + stack);
3650 else {
3651 final ActivityRecord pausing = stack.mPausingActivity;
3652 if (pausing != null && pausing == r) Slog.e(TAG,
3653 "validateTop...: top stack has pausing activity r=" + r
3654 + " state=" + state);
3655 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3656 "validateTop...: activity in front not resumed r=" + r
3657 + " state=" + state);
3658 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003659 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003660 final ActivityRecord resumed = stack.mResumedActivity;
3661 if (resumed != null && resumed == r) Slog.e(TAG,
3662 "validateTop...: back stack has resumed activity r=" + r
3663 + " state=" + state);
3664 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3665 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003666 }
3667 }
3668 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003669 }
3670
Craig Mautnere0570202015-05-13 13:06:11 -07003671 private String lockTaskModeToString() {
3672 switch (mLockTaskModeState) {
3673 case LOCK_TASK_MODE_LOCKED:
3674 return "LOCKED";
3675 case LOCK_TASK_MODE_PINNED:
3676 return "PINNED";
3677 case LOCK_TASK_MODE_NONE:
3678 return "NONE";
3679 default: return "unknown=" + mLockTaskModeState;
3680 }
3681 }
3682
Craig Mautner27084302013-03-25 08:05:25 -07003683 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003684 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003685 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003686 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
3687 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
3688 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003689 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003690 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
3691 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3692 if (packages.size() > 0) {
3693 pw.println(" mLockTaskPackages (userId:packages)=");
3694 for (int i = 0; i < packages.size(); ++i) {
3695 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3696 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3697 }
3698 }
3699 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003700 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003701
Craig Mautner20e72272013-04-01 13:45:53 -07003702 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003703 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003704 }
3705
Dianne Hackborn390517b2013-05-30 15:03:32 -07003706 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3707 boolean needSep, String prefix) {
3708 if (activity != null) {
3709 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3710 if (needSep) {
3711 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003712 }
3713 pw.print(prefix);
3714 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003715 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003716 }
3717 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003718 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003719 }
3720
Craig Mautner8d341ef2013-03-26 09:03:27 -07003721 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3722 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003723 boolean printed = false;
3724 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003725 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3726 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003727 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003728 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003729 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003730 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003731 final ActivityStack stack = stacks.get(stackNdx);
3732 StringBuilder stackHeader = new StringBuilder(128);
3733 stackHeader.append(" Stack #");
3734 stackHeader.append(stack.mStackId);
3735 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003736 stackHeader.append("\n");
3737 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3738 stackHeader.append("\n");
3739 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003740 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3741 needSep, stackHeader.toString());
3742 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3743 !dumpAll, false, dumpPackage, true,
3744 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003745
Craig Mautner4a1cb222013-12-04 16:14:06 -08003746 needSep = printed;
3747 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3748 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003749 if (pr) {
3750 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003751 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003752 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003753 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3754 " mResumedActivity: ");
3755 if (pr) {
3756 printed = true;
3757 needSep = false;
3758 }
3759 if (dumpAll) {
3760 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3761 " mLastPausedActivity: ");
3762 if (pr) {
3763 printed = true;
3764 needSep = true;
3765 }
3766 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3767 needSep, " mLastNoHistoryActivity: ");
3768 }
3769 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003770 }
3771 }
3772
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003773 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3774 false, dumpPackage, true, " Activities waiting to finish:", null);
3775 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3776 false, dumpPackage, true, " Activities waiting to stop:", null);
3777 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3778 false, dumpPackage, true, " Activities waiting for another to become visible:",
3779 null);
3780 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3781 false, dumpPackage, true, " Activities waiting to sleep:", null);
3782 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3783 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003784
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003785 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003786 }
3787
Dianne Hackborn390517b2013-05-30 15:03:32 -07003788 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003789 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003790 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003791 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003792 String innerPrefix = null;
3793 String[] args = null;
3794 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003795 for (int i=list.size()-1; i>=0; i--) {
3796 final ActivityRecord r = list.get(i);
3797 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3798 continue;
3799 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003800 if (innerPrefix == null) {
3801 innerPrefix = prefix + " ";
3802 args = new String[0];
3803 }
3804 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003805 final boolean full = !brief && (complete || !r.isInHistory());
3806 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003807 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003808 needNL = false;
3809 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003810 if (header1 != null) {
3811 pw.println(header1);
3812 header1 = null;
3813 }
3814 if (header2 != null) {
3815 pw.println(header2);
3816 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003817 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003818 if (lastTask != r.task) {
3819 lastTask = r.task;
3820 pw.print(prefix);
3821 pw.print(full ? "* " : " ");
3822 pw.println(lastTask);
3823 if (full) {
3824 lastTask.dump(pw, prefix + " ");
3825 } else if (complete) {
3826 // Complete + brief == give a summary. Isn't that obvious?!?
3827 if (lastTask.intent != null) {
3828 pw.print(prefix); pw.print(" ");
3829 pw.println(lastTask.intent.toInsecureStringWithClip());
3830 }
3831 }
3832 }
3833 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3834 pw.print(" #"); pw.print(i); pw.print(": ");
3835 pw.println(r);
3836 if (full) {
3837 r.dump(pw, innerPrefix);
3838 } else if (complete) {
3839 // Complete + brief == give a summary. Isn't that obvious?!?
3840 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3841 if (r.app != null) {
3842 pw.print(innerPrefix); pw.println(r.app);
3843 }
3844 }
3845 if (client && r.app != null && r.app.thread != null) {
3846 // flush anything that is already in the PrintWriter since the thread is going
3847 // to write to the file descriptor directly
3848 pw.flush();
3849 try {
3850 TransferPipe tp = new TransferPipe();
3851 try {
3852 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3853 r.appToken, innerPrefix, args);
3854 // Short timeout, since blocking here can
3855 // deadlock with the application.
3856 tp.go(fd, 2000);
3857 } finally {
3858 tp.kill();
3859 }
3860 } catch (IOException e) {
3861 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3862 } catch (RemoteException e) {
3863 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3864 }
3865 needNL = true;
3866 }
3867 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003868 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003869 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003870
Craig Mautnerf3333272013-04-22 10:55:53 -07003871 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003872 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3873 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003874 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3875 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003876 }
3877
3878 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003879 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003880 }
3881
3882 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003883 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3884 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003885 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3886 }
3887
Craig Mautner05d29032013-05-03 13:40:13 -07003888 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003889 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3890 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3891 }
Craig Mautner05d29032013-05-03 13:40:13 -07003892 }
3893
Craig Mautner0eea92c2013-05-16 13:35:39 -07003894 void removeSleepTimeouts() {
3895 mSleepTimeout = false;
3896 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3897 }
3898
3899 final void scheduleSleepTimeout() {
3900 removeSleepTimeouts();
3901 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3902 }
3903
Craig Mautner4a1cb222013-12-04 16:14:06 -08003904 @Override
3905 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003906 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003907 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3908 }
3909
3910 @Override
3911 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003912 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003913 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3914 }
3915
3916 @Override
3917 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003918 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003919 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3920 }
3921
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003922 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003923 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003924 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003925 newDisplay = mActivityDisplays.get(displayId) == null;
3926 if (newDisplay) {
3927 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003928 if (activityDisplay.mDisplay == null) {
3929 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3930 return;
3931 }
Craig Mautner4504de52013-12-20 09:06:56 -08003932 mActivityDisplays.put(displayId, activityDisplay);
3933 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003934 }
Craig Mautner4504de52013-12-20 09:06:56 -08003935 if (newDisplay) {
3936 mWindowManager.onDisplayAdded(displayId);
3937 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003938 }
3939
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003940 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003941 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003942 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3943 if (activityDisplay != null) {
3944 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003945 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003946 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003947 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003948 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003949 }
3950 }
3951 mWindowManager.onDisplayRemoved(displayId);
3952 }
3953
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003954 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003955 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003956 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3957 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003958 // TODO: Update the bounds.
3959 }
3960 }
3961 mWindowManager.onDisplayChanged(displayId);
3962 }
3963
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003964 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003965 StackInfo info = new StackInfo();
3966 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3967 info.displayId = Display.DEFAULT_DISPLAY;
3968 info.stackId = stack.mStackId;
3969
3970 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3971 final int numTasks = tasks.size();
3972 int[] taskIds = new int[numTasks];
3973 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003974 Rect[] taskBounds = new Rect[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003975 for (int i = 0; i < numTasks; ++i) {
3976 final TaskRecord task = tasks.get(i);
3977 taskIds[i] = task.taskId;
3978 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3979 : task.realActivity != null ? task.realActivity.flattenToString()
3980 : task.getTopActivity() != null ? task.getTopActivity().packageName
3981 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003982 taskBounds[i] = new Rect();
3983 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003984 }
3985 info.taskIds = taskIds;
3986 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003987 info.taskBounds = taskBounds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003988 return info;
3989 }
3990
3991 StackInfo getStackInfoLocked(int stackId) {
3992 ActivityStack stack = getStack(stackId);
3993 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003994 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003995 }
3996 return null;
3997 }
3998
3999 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004000 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08004001 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
4002 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004003 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004004 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08004005 }
4006 }
4007 return list;
4008 }
4009
Craig Mautner15df08a2015-04-01 12:17:18 -07004010 TaskRecord getLockedTaskLocked() {
4011 final int top = mLockTaskModeTasks.size() - 1;
4012 if (top >= 0) {
4013 return mLockTaskModeTasks.get(top);
4014 }
4015 return null;
4016 }
4017
4018 boolean isLockedTask(TaskRecord task) {
4019 return mLockTaskModeTasks.contains(task);
4020 }
4021
4022 boolean isLastLockedTask(TaskRecord task) {
4023 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
4024 }
4025
4026 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07004027 if (!mLockTaskModeTasks.remove(task)) {
4028 return;
4029 }
4030 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
4031 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004032 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07004033 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
4034 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07004035 final Message lockTaskMsg = Message.obtain();
4036 lockTaskMsg.arg1 = task.userId;
4037 lockTaskMsg.what = LOCK_TASK_END_MSG;
4038 mHandler.sendMessage(lockTaskMsg);
4039 }
4040 }
4041
Craig Mautner6cd6cec2015-04-01 00:02:12 -07004042 void showLockTaskToast() {
4043 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04004044 }
4045
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004046 void showLockTaskEscapeMessageLocked(TaskRecord task) {
4047 if (mLockTaskModeTasks.contains(task)) {
4048 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
4049 }
4050 }
4051
Craig Mautner432f64e2015-05-20 14:59:57 -07004052 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
4053 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004054 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07004055 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07004056 final TaskRecord lockedTask = getLockedTaskLocked();
4057 if (lockedTask != null) {
4058 removeLockedTaskLocked(lockedTask);
4059 if (!mLockTaskModeTasks.isEmpty()) {
4060 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07004061 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4062 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07004063 lockedTask.performClearTaskLocked();
4064 resumeTopActivitiesLocked();
4065 return;
4066 }
Christopher Tate3bd90612014-06-18 16:37:52 -07004067 }
Craig Mautnere0570202015-05-13 13:06:11 -07004068 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4069 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08004070 return;
4071 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004072
4073 // Should have already been checked, but do it again.
4074 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07004075 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4076 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08004077 return;
4078 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004079 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07004080 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07004081 return;
4082 }
4083
4084 if (mLockTaskModeTasks.isEmpty()) {
4085 // First locktask.
4086 final Message lockTaskMsg = Message.obtain();
4087 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
4088 lockTaskMsg.arg1 = task.userId;
4089 lockTaskMsg.what = LOCK_TASK_START_MSG;
4090 lockTaskMsg.arg2 = lockTaskModeState;
4091 mHandler.sendMessage(lockTaskMsg);
4092 }
4093 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07004094 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
4095 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07004096 mLockTaskModeTasks.remove(task);
4097 mLockTaskModeTasks.add(task);
4098
4099 if (task.mLockTaskUid == -1) {
4100 task.mLockTaskUid = task.mCallingUid;
4101 }
Craig Mautner432f64e2015-05-20 14:59:57 -07004102
4103 if (andResume) {
4104 findTaskToMoveToFrontLocked(task, 0, null, reason);
4105 resumeTopActivitiesLocked();
4106 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004107 }
4108
4109 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04004110 return isLockTaskModeViolation(task, false);
4111 }
4112
4113 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
4114 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004115 return false;
4116 }
4117 final int lockTaskAuth = task.mLockTaskAuth;
4118 switch (lockTaskAuth) {
4119 case LOCK_TASK_AUTH_DONT_LOCK:
4120 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01004121 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07004122 case LOCK_TASK_AUTH_LAUNCHABLE:
4123 case LOCK_TASK_AUTH_WHITELISTED:
4124 return false;
4125 case LOCK_TASK_AUTH_PINNABLE:
4126 // Pinnable tasks can't be launched on top of locktask tasks.
4127 return !mLockTaskModeTasks.isEmpty();
4128 default:
4129 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
4130 return true;
4131 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004132 }
4133
Craig Mautner15df08a2015-04-01 12:17:18 -07004134 void onLockTaskPackagesUpdatedLocked() {
4135 boolean didSomething = false;
4136 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
4137 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01004138 final boolean wasWhitelisted =
4139 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4140 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07004141 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01004142 final boolean isWhitelisted =
4143 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4144 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
4145 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004146 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07004147 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
4148 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07004149 removeLockedTaskLocked(lockedTask);
4150 lockedTask.performClearTaskLocked();
4151 didSomething = true;
4152 }
4153 }
4154 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4155 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4156 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4157 final ActivityStack stack = stacks.get(stackNdx);
4158 stack.onLockTaskPackagesUpdatedLocked();
4159 }
4160 }
Craig Mautnere0570202015-05-13 13:06:11 -07004161 final ActivityRecord r = topRunningActivityLocked();
4162 final TaskRecord task = r != null ? r.task : null;
4163 if (mLockTaskModeTasks.isEmpty() && task != null
4164 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4165 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004166 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4167 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4168 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4169 false);
4170 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004171 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004172 if (didSomething) {
4173 resumeTopActivitiesLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004174 }
4175 }
4176
Benjamin Franz43261142015-02-11 15:59:44 +00004177 int getLockTaskModeState() {
4178 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004179 }
4180
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004181 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004182
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004183 public ActivityStackSupervisorHandler(Looper looper) {
4184 super(looper);
4185 }
4186
Craig Mautnerf3333272013-04-22 10:55:53 -07004187 void activityIdleInternal(ActivityRecord r) {
4188 synchronized (mService) {
4189 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
4190 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004191 }
4192
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004193 @Override
4194 public void handleMessage(Message msg) {
4195 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004196 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004197 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4198 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004199 if (mService.mDidDexOpt) {
4200 mService.mDidDexOpt = false;
4201 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4202 nmsg.obj = msg.obj;
4203 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4204 return;
4205 }
4206 // We don't at this point know if the activity is fullscreen,
4207 // so we need to be conservative and assume it isn't.
4208 activityIdleInternal((ActivityRecord)msg.obj);
4209 } break;
4210 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004211 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004212 activityIdleInternal((ActivityRecord)msg.obj);
4213 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004214 case RESUME_TOP_ACTIVITY_MSG: {
4215 synchronized (mService) {
4216 resumeTopActivitiesLocked();
4217 }
4218 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004219 case SLEEP_TIMEOUT_MSG: {
4220 synchronized (mService) {
4221 if (mService.isSleepingOrShuttingDown()) {
4222 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4223 mSleepTimeout = true;
4224 checkReadyForSleepLocked();
4225 }
4226 }
4227 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004228 case LAUNCH_TIMEOUT_MSG: {
4229 if (mService.mDidDexOpt) {
4230 mService.mDidDexOpt = false;
4231 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4232 return;
4233 }
4234 synchronized (mService) {
4235 if (mLaunchingActivity.isHeld()) {
4236 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4237 if (VALIDATE_WAKE_LOCK_CALLER
4238 && Binder.getCallingUid() != Process.myUid()) {
4239 throw new IllegalStateException("Calling must be system uid");
4240 }
4241 mLaunchingActivity.release();
4242 }
4243 }
4244 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004245 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004246 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004247 } break;
4248 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004249 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004250 } break;
4251 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004252 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004253 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004254 case CONTAINER_CALLBACK_VISIBILITY: {
4255 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004256 final IActivityContainerCallback callback = container.mCallback;
4257 if (callback != null) {
4258 try {
4259 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4260 } catch (RemoteException e) {
4261 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004262 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004263 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004264 case LOCK_TASK_START_MSG: {
4265 // When lock task starts, we disable the status bars.
4266 try {
Jason Monk62515be2014-05-21 16:06:19 -04004267 if (mLockTaskNotify == null) {
4268 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004269 }
Jason Monk62515be2014-05-21 16:06:19 -04004270 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004271 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004272 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004273 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004274 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004275 flags = StatusBarManager.DISABLE_MASK
4276 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004277 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004278 flags = StatusBarManager.DISABLE_MASK
4279 & (~StatusBarManager.DISABLE_BACK)
4280 & (~StatusBarManager.DISABLE_HOME)
4281 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004282 }
4283 getStatusBarService().disable(flags, mToken,
4284 mService.mContext.getPackageName());
4285 }
4286 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004287 if (getDevicePolicyManager() != null) {
4288 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004289 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004290 }
justinzhang5286d3f2014-05-12 17:06:01 -04004291 } catch (RemoteException ex) {
4292 throw new RuntimeException(ex);
4293 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004294 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004295 case LOCK_TASK_END_MSG: {
4296 // When lock task ends, we enable the status bars.
4297 try {
Jason Monk62515be2014-05-21 16:06:19 -04004298 if (getStatusBarService() != null) {
4299 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4300 mService.mContext.getPackageName());
4301 }
4302 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004303 if (getDevicePolicyManager() != null) {
4304 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4305 msg.arg1);
4306 }
Jason Monk62515be2014-05-21 16:06:19 -04004307 if (mLockTaskNotify == null) {
4308 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4309 }
4310 mLockTaskNotify.show(false);
4311 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004312 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004313 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004314 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004315 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004316 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04004317 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04004318 new LockPatternUtils(mService.mContext)
4319 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004320 }
4321 } catch (SettingNotFoundException e) {
4322 // No setting, don't lock.
4323 }
justinzhang5286d3f2014-05-12 17:06:01 -04004324 } catch (RemoteException ex) {
4325 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004326 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004327 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004328 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004329 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004330 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4331 if (mLockTaskNotify == null) {
4332 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4333 }
4334 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4335 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004336 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4337 final ActivityContainer container = (ActivityContainer) msg.obj;
4338 final IActivityContainerCallback callback = container.mCallback;
4339 if (callback != null) {
4340 try {
4341 callback.onAllActivitiesComplete(container.asBinder());
4342 } catch (RemoteException e) {
4343 }
4344 }
4345 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004346 case LAUNCH_TASK_BEHIND_COMPLETE: {
4347 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004348 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004349 if (r != null) {
4350 handleLaunchTaskBehindCompleteLocked(r);
4351 }
4352 }
4353 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004354 }
4355 }
4356 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004357
Ying Wangb081a592014-04-22 15:20:16 -07004358 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07004359 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07004360 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004361 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004362 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004363 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004364 ActivityRecord mParentActivity = null;
4365 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004366
Craig Mautnere3a00d72014-04-16 08:31:19 -07004367 boolean mVisible = true;
4368
Craig Mautner4a1cb222013-12-04 16:14:06 -08004369 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004370 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004371
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004372 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4373 final static int CONTAINER_STATE_NO_SURFACE = 1;
4374 final static int CONTAINER_STATE_FINISHING = 2;
4375 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4376
4377 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004378 synchronized (mService) {
4379 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004380 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004381 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004382 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004383 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004384 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004385
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004386 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004387 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004388 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08004389 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004390 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004391 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004392 }
4393
4394 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004395 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004396 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004397 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4398 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004399 return;
4400 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004401 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004402 }
4403 }
4404
4405 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004406 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004407 synchronized (mService) {
4408 if (mActivityDisplay != null) {
4409 return mActivityDisplay.mDisplayId;
4410 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004411 }
4412 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004413 }
4414
Jeff Brownca9bc702014-02-11 14:32:56 -08004415 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004416 public int getStackId() {
4417 synchronized (mService) {
4418 return mStackId;
4419 }
4420 }
4421
4422 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004423 public boolean injectEvent(InputEvent event) {
4424 final long origId = Binder.clearCallingIdentity();
4425 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004426 synchronized (mService) {
4427 if (mActivityDisplay != null) {
4428 return mInputManagerInternal.injectInputEvent(event,
4429 mActivityDisplay.mDisplayId,
4430 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4431 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004432 }
4433 return false;
4434 } finally {
4435 Binder.restoreCallingIdentity(origId);
4436 }
4437 }
4438
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004439 @Override
4440 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004441 synchronized (mService) {
4442 if (mContainerState == CONTAINER_STATE_FINISHING) {
4443 return;
4444 }
4445 mContainerState = CONTAINER_STATE_FINISHING;
4446
Craig Mautnerd163e752014-06-13 17:18:47 -07004447 long origId = Binder.clearCallingIdentity();
4448 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004449 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07004450 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004451 } finally {
4452 Binder.restoreCallingIdentity(origId);
4453 }
4454 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004455 }
4456
Craig Mautner60257702014-09-17 15:02:33 -07004457 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004458 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004459 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004460 if (mActivityDisplay != null) {
4461 mActivityDisplay.detachActivitiesLocked(mStack);
4462 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004463 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004464 }
4465 }
4466
4467 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004468 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004469 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnerb9168362015-02-26 20:40:19 -08004470 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004471 Binder.getCallingUid(), mCurrentUser, false,
4472 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004473
Craig Mautnere0a38842013-12-16 16:14:02 -08004474 // TODO: Switch to user app stacks here.
4475 String mimeType = intent.getType();
Craig Mautnerb9168362015-02-26 20:40:19 -08004476 final Uri data = intent.getData();
4477 if (mimeType == null && data != null && "content".equals(data.getScheme())) {
4478 mimeType = mService.getProviderMimeType(data, userId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004479 }
Craig Mautnerb9168362015-02-26 20:40:19 -08004480 checkEmbeddedAllowedInner(userId, intent, mimeType);
4481
4482 intent.addFlags(FORCE_NEW_TASK_FLAGS);
4483 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07004484 0, 0, null, null, null, null, false, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004485 }
4486
4487 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004488 public final int startActivityIntentSender(IIntentSender intentSender)
4489 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004490 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4491
4492 if (!(intentSender instanceof PendingIntentRecord)) {
4493 throw new IllegalArgumentException("Bad PendingIntent object");
4494 }
4495
Craig Mautnerb9168362015-02-26 20:40:19 -08004496 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004497 Binder.getCallingUid(), mCurrentUser, false,
4498 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004499
4500 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4501 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4502 pendingIntent.key.requestResolvedType);
4503
4504 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4505 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4506 }
4507
4508 private void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004509 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004510 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004511 throw new SecurityException(
4512 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4513 }
4514 }
4515
Craig Mautnerdf88d732014-01-27 09:21:32 -08004516 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004517 public IBinder asBinder() {
4518 return this;
4519 }
4520
Craig Mautner4504de52013-12-20 09:06:56 -08004521 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004522 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004523 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004524 }
4525
Craig Mautner4a1cb222013-12-04 16:14:06 -08004526 ActivityStackSupervisor getOuter() {
4527 return ActivityStackSupervisor.this;
4528 }
4529
Craig Mautnerd163e752014-06-13 17:18:47 -07004530 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004531 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004532 }
4533
4534 void getBounds(Point outBounds) {
Craig Mautnerd163e752014-06-13 17:18:47 -07004535 synchronized (mService) {
4536 if (mActivityDisplay != null) {
4537 mActivityDisplay.getBounds(outBounds);
4538 } else {
4539 outBounds.set(0, 0);
4540 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004541 }
4542 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004543
Craig Mautner6985bad2014-04-21 15:22:06 -07004544 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004545 void setVisible(boolean visible) {
4546 if (mVisible != visible) {
4547 mVisible = visible;
4548 if (mCallback != null) {
4549 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4550 0 /* unused */, this).sendToTarget();
4551 }
4552 }
4553 }
4554
Craig Mautner6985bad2014-04-21 15:22:06 -07004555 void setDrawn() {
4556 }
4557
Craig Mautner1b4bf852014-05-26 15:06:32 -07004558 // You can always start a new task on a regular ActivityStack.
4559 boolean isEligibleForNewTasks() {
4560 return true;
4561 }
4562
Craig Mautnerd163e752014-06-13 17:18:47 -07004563 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004564 detachLocked();
4565 deleteActivityContainer(this);
4566 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004567 }
4568
Craig Mautner34b73df2014-01-12 21:11:08 -08004569 @Override
4570 public String toString() {
4571 return mIdString + (mActivityDisplay == null ? "N" : "A");
4572 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004573 }
4574
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004575 private class VirtualActivityContainer extends ActivityContainer {
4576 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004577 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004578
4579 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4580 super(getNextStackId());
4581 mParentActivity = parent;
4582 mCallback = callback;
4583 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004584 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004585 }
4586
4587 @Override
4588 public void setSurface(Surface surface, int width, int height, int density) {
4589 super.setSurface(surface, width, height, density);
4590
4591 synchronized (mService) {
4592 final long origId = Binder.clearCallingIdentity();
4593 try {
4594 setSurfaceLocked(surface, width, height, density);
4595 } finally {
4596 Binder.restoreCallingIdentity(origId);
4597 }
4598 }
4599 }
4600
4601 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4602 if (mContainerState == CONTAINER_STATE_FINISHING) {
4603 return;
4604 }
4605 VirtualActivityDisplay virtualActivityDisplay =
4606 (VirtualActivityDisplay) mActivityDisplay;
4607 if (virtualActivityDisplay == null) {
4608 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004609 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004610 mActivityDisplay = virtualActivityDisplay;
4611 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004612 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004613 }
4614
4615 if (mSurface != null) {
4616 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004617 }
4618
Craig Mautner6985bad2014-04-21 15:22:06 -07004619 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004620 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004621 mStack.resumeTopActivityLocked(null);
4622 } else {
4623 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004624 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004625 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004626 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004627 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004628 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004629
Craig Mautnerd163e752014-06-13 17:18:47 -07004630 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004631
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004632 if (DEBUG_STACK) Slog.d(TAG_STACK,
4633 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004634 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004635
Craig Mautner6985bad2014-04-21 15:22:06 -07004636 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004637 boolean isAttachedLocked() {
4638 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004639 }
4640
4641 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004642 void setDrawn() {
4643 synchronized (mService) {
4644 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004645 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004646 }
4647 }
4648
Craig Mautner1b4bf852014-05-26 15:06:32 -07004649 // Never start a new task on an ActivityView if it isn't explicitly specified.
4650 @Override
4651 boolean isEligibleForNewTasks() {
4652 return false;
4653 }
4654
Craig Mautnerd163e752014-06-13 17:18:47 -07004655 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004656 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004657 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4658 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4659 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4660 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4661 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004662 }
4663 }
4664
Craig Mautner4a1cb222013-12-04 16:14:06 -08004665 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4666 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004667 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004668 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004669 int mDisplayId;
4670 Display mDisplay;
4671 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004672
Craig Mautner4a1cb222013-12-04 16:14:06 -08004673 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4674 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08004675 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004676
Jose Lima4b6c6692014-08-12 17:41:12 -07004677 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004678
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004679 ActivityDisplay() {
4680 }
Craig Mautner4504de52013-12-20 09:06:56 -08004681
Craig Mautner1a70a162014-09-13 12:09:31 -07004682 // After instantiation, check that mDisplay is not null before using this. The alternative
4683 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004684 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004685 final Display display = mDisplayManager.getDisplay(displayId);
4686 if (display == null) {
4687 return;
4688 }
4689 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004690 }
4691
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004692 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004693 mDisplay = display;
4694 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004695 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004696 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004697
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004698 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004699 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004700 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
4701 + " onTop=" + onTop);
4702 if (onTop) {
4703 mStacks.add(stack);
4704 } else {
4705 mStacks.add(0, stack);
4706 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004707 }
4708
Craig Mautnere0a38842013-12-16 16:14:02 -08004709 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004710 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004711 + " from displayId=" + mDisplayId);
4712 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004713 }
4714
4715 void getBounds(Point bounds) {
4716 mDisplay.getDisplayInfo(mDisplayInfo);
4717 bounds.x = mDisplayInfo.appWidth;
4718 bounds.y = mDisplayInfo.appHeight;
4719 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004720
Jose Lima4b6c6692014-08-12 17:41:12 -07004721 void setVisibleBehindActivity(ActivityRecord r) {
4722 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004723 }
4724
Jose Lima4b6c6692014-08-12 17:41:12 -07004725 boolean hasVisibleBehindActivity() {
4726 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004727 }
4728
Craig Mautner34b73df2014-01-12 21:11:08 -08004729 @Override
4730 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004731 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4732 }
4733 }
4734
4735 class VirtualActivityDisplay extends ActivityDisplay {
4736 VirtualDisplay mVirtualDisplay;
4737
Craig Mautner6985bad2014-04-21 15:22:06 -07004738 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004739 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004740 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4741 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4742 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4743 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004744
4745 init(mVirtualDisplay.getDisplay());
4746
4747 mWindowManager.handleDisplayAdded(mDisplayId);
4748 }
4749
4750 void setSurface(Surface surface) {
4751 if (mVirtualDisplay != null) {
4752 mVirtualDisplay.setSurface(surface);
4753 }
4754 }
4755
4756 @Override
4757 void detachActivitiesLocked(ActivityStack stack) {
4758 super.detachActivitiesLocked(stack);
4759 if (mVirtualDisplay != null) {
4760 mVirtualDisplay.release();
4761 mVirtualDisplay = null;
4762 }
4763 }
4764
4765 @Override
4766 public String toString() {
4767 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004768 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004769 }
Jose Lima58e66d62014-05-27 20:00:27 -07004770
4771 private boolean isLeanbackOnlyDevice() {
4772 boolean onLeanbackOnly = false;
4773 try {
4774 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
4775 PackageManager.FEATURE_LEANBACK_ONLY);
4776 } catch (RemoteException e) {
4777 // noop
4778 }
4779
4780 return onLeanbackOnly;
4781 }
Craig Mautner27084302013-03-25 08:05:25 -07004782}