blob: 23e62e22eee1b14090f518aa7afb5dfafcaabb80 [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;
Craig Mautner15df08a2015-04-01 12:17:18 -070020import static android.app.ActivityManager.LOCK_TASK_MODE_LOCKED;
21import static android.app.ActivityManager.LOCK_TASK_MODE_NONE;
22import static android.app.ActivityManager.LOCK_TASK_MODE_PINNED;
Wale Ogunwaled80c2632015-03-13 10:26:26 -070023import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK;
24import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
Craig Mautner29219d92013-04-16 20:19:12 -070025import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
26import static android.content.Intent.FLAG_ACTIVITY_TASK_ON_HOME;
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -070027import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Craig Mautner15df08a2015-04-01 12:17:18 -070028import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_IF_WHITELISTED;
Craig Mautner6170f732013-04-02 13:05:23 -070029import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Wale Ogunwaled57969f2014-11-15 19:37:29 -080030import static com.android.server.am.ActivityManagerDebugConfig.*;
Craig Mautner05d29032013-05-03 13:40:13 -070031import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Craig Mautner84984fa2014-06-19 11:19:20 -070032import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
33import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
34import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -070035import static com.android.server.am.ActivityStack.ActivityState.*;
Craig Mautner15df08a2015-04-01 12:17:18 -070036import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
37import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
38import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
39import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Craig Mautner8d341ef2013-03-26 09:03:27 -070040
Craig Mautner2420ead2013-04-01 17:13:20 -070041import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070042import android.app.ActivityManager;
Craig Mautnered6649f2013-12-02 14:08:25 -080043import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070044import android.app.ActivityOptions;
45import android.app.AppGlobals;
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;
64import android.content.pm.PackageManager;
65import android.content.pm.ResolveInfo;
66import android.content.res.Configuration;
Craig Mautner4a1cb222013-12-04 16:14:06 -080067import android.graphics.Point;
Wale Ogunwale60454db2015-01-23 16:05:07 -080068import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -080069import android.hardware.display.DisplayManager;
70import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080071import android.hardware.display.DisplayManagerGlobal;
72import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080073import android.hardware.input.InputManager;
74import android.hardware.input.InputManagerInternal;
Craig Mautnerb9168362015-02-26 20:40:19 -080075import android.net.Uri;
Craig Mautner23ac33b2013-04-01 16:26:35 -070076import android.os.Binder;
Craig Mautner8d341ef2013-03-26 09:03:27 -070077import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070078import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070079import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070080import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070081import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070082import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070083import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070084import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070085import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070086import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -040087import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070088import android.os.SystemClock;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -070089import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -070090import android.os.UserHandle;
Dianne Hackborn3d07c942015-03-13 18:02:54 -070091import android.os.WorkSource;
Jason Monk62515be2014-05-21 16:06:19 -040092import android.provider.Settings;
93import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -070094import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070095import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -070096import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -070097import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -080098import android.util.SparseArray;
Craig Mautner2219a1b2013-03-25 09:44:30 -070099
Craig Mautner4a1cb222013-12-04 16:14:06 -0800100import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800101import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800102import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -0800103import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800104import android.view.Surface;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700105import com.android.internal.app.HeavyWeightSwitcherActivity;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700106import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800107import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700108import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400109import com.android.internal.statusbar.IStatusBarService;
Jason Monke0697792014-08-04 16:28:09 -0400110import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800111import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700112import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700113import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700114
justinzhang5286d3f2014-05-12 17:06:01 -0400115
Craig Mautner8d341ef2013-03-26 09:03:27 -0700116import java.io.FileDescriptor;
117import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700118import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700119import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700120import java.util.Arrays;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700121import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700122import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700123
Craig Mautner4a1cb222013-12-04 16:14:06 -0800124public final class ActivityStackSupervisor implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800125 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700126 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700127 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700128 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700129 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700130 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700131 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700132 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700133 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
134 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700135 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700136 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700137 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
138 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700139 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700140 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800141
Craig Mautner2219a1b2013-03-25 09:44:30 -0700142 public static final int HOME_STACK_ID = 0;
Craig Mautner27084302013-03-25 08:05:25 -0700143
Craig Mautnerf3333272013-04-22 10:55:53 -0700144 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700145 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700146
Craig Mautner0eea92c2013-05-16 13:35:39 -0700147 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700148 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700149
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700150 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700151 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700152
Craig Mautner05d29032013-05-03 13:40:13 -0700153 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
154 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
155 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700156 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700157 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800158 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
159 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
160 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700161 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400162 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
163 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700164 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700165 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700166 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800167
Craig Mautner4504de52013-12-20 09:06:56 -0800168 private final static String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700169
Jason Monk62515be2014-05-21 16:06:19 -0400170 private static final String LOCK_TASK_TAG = "Lock-to-App";
171
justinzhang5286d3f2014-05-12 17:06:01 -0400172 /** Status Bar Service **/
173 private IBinder mToken = new Binder();
174 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400175 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400176
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700177 // For debugging to make sure the caller when acquiring/releasing our
178 // wake lock is the system process.
179 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700180
Craig Mautner27084302013-03-25 08:05:25 -0700181 final ActivityManagerService mService;
182
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800183 private final RecentTasks mRecentTasks;
184
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700185 final ActivityStackSupervisorHandler mHandler;
186
187 /** Short cut */
188 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800189 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700190
Craig Mautner8d341ef2013-03-26 09:03:27 -0700191 /** Identifier counter for all ActivityStacks */
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700192 private int mLastStackId = HOME_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700193
194 /** Task identifier that activities are currently being started in. Incremented each time a
195 * new task is created. */
196 private int mCurTaskId = 0;
197
Craig Mautner2420ead2013-04-01 17:13:20 -0700198 /** The current user */
199 private int mCurrentUser;
200
Craig Mautnere0a38842013-12-16 16:14:02 -0800201 /** The stack containing the launcher app. Assumed to always be attached to
202 * Display.DEFAULT_DISPLAY. */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700203 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700204
Craig Mautnere0a38842013-12-16 16:14:02 -0800205 /** The stack currently receiving input or launching the next activity. */
Craig Mautner29219d92013-04-16 20:19:12 -0700206 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700207
Craig Mautner4a1cb222013-12-04 16:14:06 -0800208 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
209 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800210 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800211 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700212
213 /** List of activities that are waiting for a new activity to become visible before completing
214 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800215 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700216
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700217 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800218 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700219
220 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800221 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700222
Craig Mautnerde4ef022013-04-07 19:01:33 -0700223 /** List of activities that are ready to be stopped, but waiting for the next activity to
224 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800225 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700226
Craig Mautnerf3333272013-04-22 10:55:53 -0700227 /** List of activities that are ready to be finished, but waiting for the previous activity to
228 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800229 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700230
Craig Mautner0eea92c2013-05-16 13:35:39 -0700231 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800232 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700233
Craig Mautnerf3333272013-04-22 10:55:53 -0700234 /** Used on user changes */
Craig Mautner8c14c152015-01-15 17:32:07 -0800235 final ArrayList<UserStartedState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700236
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700237 /** Used to queue up any background users being started */
Craig Mautner8c14c152015-01-15 17:32:07 -0800238 final ArrayList<UserStartedState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700239
Craig Mautnerde4ef022013-04-07 19:01:33 -0700240 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
241 * is being brought in front of us. */
242 boolean mUserLeaving = false;
243
Craig Mautner0eea92c2013-05-16 13:35:39 -0700244 /** Set when we have taken too long waiting to go to sleep. */
245 boolean mSleepTimeout = false;
246
Jose Lima58e66d62014-05-27 20:00:27 -0700247 /** Indicates if we are running on a Leanback-only (TV) device. Only initialized after
248 * setWindowManager is called. **/
249 private boolean mLeanbackOnlyDevice;
250
Craig Mautner0eea92c2013-05-16 13:35:39 -0700251 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700252 * We don't want to allow the device to go to sleep while in the process
253 * of launching an activity. This is primarily to allow alarm intent
254 * receivers to launch an activity and get that to run before the device
255 * goes back to sleep.
256 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700257 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700258
259 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700260 * Set when the system is going to sleep, until we have
261 * successfully paused the current activity and released our wake lock.
262 * At that point the system is allowed to actually sleep.
263 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700264 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700265
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700266 /** Stack id of the front stack when user switched, indexed by userId. */
267 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700268
Craig Mautner4504de52013-12-20 09:06:56 -0800269 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800270 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700271 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800272
273 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700274 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800275
Jeff Brownca9bc702014-02-11 14:32:56 -0800276 InputManagerInternal mInputManagerInternal;
277
Craig Mautner15df08a2015-04-01 12:17:18 -0700278 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
279 * may be finished until there is only one entry left. If this is empty the system is not
280 * in lockTask mode. */
281 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000282 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700283 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
284 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000285 */
286 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400287 /**
288 * Notifies the user when entering/exiting lock-task.
289 */
290 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800291
Craig Mautner15df08a2015-04-01 12:17:18 -0700292 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches = new ArrayList<>();
Craig Mautneree36c772014-07-16 14:56:05 -0700293
Craig Mautner42d04db2014-11-06 12:13:23 -0800294 /** Used to keep resumeTopActivityLocked() from being entered recursively */
295 boolean inResumeTopActivity;
296
Craig Mautneree36c772014-07-16 14:56:05 -0700297 /**
298 * Description of a request to start a new activity, which has been held
299 * due to app switches being disabled.
300 */
301 static class PendingActivityLaunch {
302 final ActivityRecord r;
303 final ActivityRecord sourceRecord;
304 final int startFlags;
305 final ActivityStack stack;
306
307 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
308 int _startFlags, ActivityStack _stack) {
309 r = _r;
310 sourceRecord = _sourceRecord;
311 startFlags = _startFlags;
312 stack = _stack;
313 }
314 }
315
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800316 public ActivityStackSupervisor(ActivityManagerService service, RecentTasks recentTasks) {
Craig Mautner27084302013-03-25 08:05:25 -0700317 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800318 mRecentTasks = recentTasks;
Jeff Brown2c43c332014-06-12 22:38:59 -0700319 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
320 }
321
322 /**
323 * At the time when the constructor runs, the power manager has not yet been
324 * initialized. So we initialize our wakelocks afterwards.
325 */
326 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800327 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700328 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700329 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700330 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700331 }
332
justinzhang5286d3f2014-05-12 17:06:01 -0400333 // This function returns a IStatusBarService. The value is from ServiceManager.
334 // getService and is cached.
335 private IStatusBarService getStatusBarService() {
336 synchronized (mService) {
337 if (mStatusBarService == null) {
338 mStatusBarService = IStatusBarService.Stub.asInterface(
339 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
340 if (mStatusBarService == null) {
341 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
342 }
343 }
344 return mStatusBarService;
345 }
346 }
347
Jason Monk35c62a42014-06-17 10:24:47 -0400348 private IDevicePolicyManager getDevicePolicyManager() {
349 synchronized (mService) {
350 if (mDevicePolicyManager == null) {
351 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
352 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
353 if (mDevicePolicyManager == null) {
354 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
355 }
356 }
357 return mDevicePolicyManager;
358 }
359 }
360
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700361 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800362 synchronized (mService) {
363 mWindowManager = wm;
364
365 mDisplayManager =
366 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
367 mDisplayManager.registerDisplayListener(this, null);
368
369 Display[] displays = mDisplayManager.getDisplays();
370 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
371 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800372 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700373 if (activityDisplay.mDisplay == null) {
374 throw new IllegalStateException("Default Display does not exist");
375 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800376 mActivityDisplays.put(displayId, activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800377 }
378
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700379 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800380 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800381
382 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Jose Lima58e66d62014-05-27 20:00:27 -0700383
384 // Initialize this here, now that we can get a valid reference to PackageManager.
385 mLeanbackOnlyDevice = isLeanbackOnlyDevice();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800386 }
Craig Mautner27084302013-03-25 08:05:25 -0700387 }
388
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200389 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700390 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200391 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700392 }
393
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700394 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800395 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700396 }
397
Craig Mautnerde4ef022013-04-07 19:01:33 -0700398 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800399 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700400 }
401
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800402 /** Top of all visible stacks is/should always be equal to the focused stack.
403 * Use {@link ActivityStack#isStackVisibleLocked} to determine if a specific
404 * stack is visible or not. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700405 boolean isFrontStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700406 if (stack == null) {
407 return false;
408 }
409
Craig Mautnerdf88d732014-01-27 09:21:32 -0800410 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
411 if (parent != null) {
412 stack = parent.task.stack;
413 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800414 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700415 }
416
Craig Mautner299f9602015-01-26 09:47:33 -0800417 void moveHomeStack(boolean toFront, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800418 moveHomeStack(toFront, reason, null);
419 }
420
421 void moveHomeStack(boolean toFront, String reason, ActivityStack lastFocusedStack) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800422 ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautnerde313752015-01-22 14:28:03 -0800423 final int topNdx = stacks.size() - 1;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800424 if (topNdx <= 0) {
425 return;
426 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700427
428 // The home stack should either be at the top or bottom of the stack list.
429 if ((toFront && (stacks.get(topNdx) != mHomeStack))
430 || (!toFront && (stacks.get(0) != mHomeStack))) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700431 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveHomeTask: topStack old="
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700432 + ((lastFocusedStack != null) ? lastFocusedStack : stacks.get(topNdx))
433 + " new=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800434 stacks.remove(mHomeStack);
435 stacks.add(toFront ? topNdx : 0, mHomeStack);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700436 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800437
438 if (lastFocusedStack != null) {
439 mLastFocusedStack = lastFocusedStack;
440 }
441 mFocusedStack = stacks.get(topNdx);
442
Craig Mautnerde313752015-01-22 14:28:03 -0800443 EventLog.writeEvent(EventLogTags.AM_HOME_STACK_MOVED,
444 mCurrentUser, toFront ? 1 : 0, stacks.get(topNdx).getStackId(),
Craig Mautner299f9602015-01-26 09:47:33 -0800445 mFocusedStack == null ? -1 : mFocusedStack.getStackId(), reason);
Craig Mautnerde313752015-01-22 14:28:03 -0800446
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800447 if (mService.mBooting || !mService.mBooted) {
448 final ActivityRecord r = topRunningActivityLocked();
449 if (r != null && r.idle) {
450 checkFinishBootingLocked();
451 }
452 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700453 }
454
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700455 /** Returns true if the focus activity was adjusted to the home stack top activity. */
456 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700457 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
458 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700459 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700460 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700461
Craig Mautner84984fa2014-06-19 11:19:20 -0700462 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700463
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700464 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700465 if (top == null) {
466 return false;
467 }
468 mService.setFocusedActivityLocked(top, reason);
469 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700470 }
471
Craig Mautner299f9602015-01-26 09:47:33 -0800472 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700473 if (!mService.mBooting && !mService.mBooted) {
474 // Not ready yet!
475 return false;
476 }
477
Craig Mautner84984fa2014-06-19 11:19:20 -0700478 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
479 mWindowManager.showRecentApps();
480 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700481 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700482
Craig Mautner84984fa2014-06-19 11:19:20 -0700483 if (prev != null) {
484 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
485 }
486
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700487 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
488 ActivityRecord r = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700489 if (r != null) {
Craig Mautner299f9602015-01-26 09:47:33 -0800490 mService.setFocusedActivityLocked(r, reason);
Craig Mautner05d29032013-05-03 13:40:13 -0700491 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700492 }
Craig Mautner299f9602015-01-26 09:47:33 -0800493 return mService.startHomeActivityLocked(mCurrentUser, reason);
Craig Mautner69ada552013-04-18 13:51:51 -0700494 }
495
Craig Mautner8d341ef2013-03-26 09:03:27 -0700496 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700497 return anyTaskForIdLocked(id, true);
498 }
499
500 /**
501 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
502 * @param id Id of the task we would like returned.
503 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
504 * restore the task from recents to the active list.
505 */
506 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800507 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800508 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800509 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800510 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
511 ActivityStack stack = stacks.get(stackNdx);
512 TaskRecord task = stack.taskForIdLocked(id);
513 if (task != null) {
514 return task;
515 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700516 }
517 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800518
519 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700520 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800521 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800522 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700523 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800524 return null;
525 }
526
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700527 if (!restoreFromRecents) {
528 return task;
529 }
530
Wale Ogunwale7de05352014-12-12 15:21:33 -0800531 if (!restoreRecentTaskLocked(task)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700532 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
533 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800534 return null;
535 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700536 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800537 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700538 }
539
Craig Mautner6170f732013-04-02 13:05:23 -0700540 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800541 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800542 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800543 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800544 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
545 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
546 if (r != null) {
547 return r;
548 }
Craig Mautner6170f732013-04-02 13:05:23 -0700549 }
550 }
551 return null;
552 }
553
Craig Mautneref73ee12014-04-23 11:45:37 -0700554 void setNextTaskId(int taskId) {
555 if (taskId > mCurTaskId) {
556 mCurTaskId = taskId;
557 }
558 }
559
Craig Mautner8d341ef2013-03-26 09:03:27 -0700560 int getNextTaskId() {
561 do {
562 mCurTaskId++;
563 if (mCurTaskId <= 0) {
564 mCurTaskId = 1;
565 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700566 } while (anyTaskForIdLocked(mCurTaskId, false) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700567 return mCurTaskId;
568 }
569
Craig Mautnerde4ef022013-04-07 19:01:33 -0700570 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800571 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700572 if (stack == null) {
573 return null;
574 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700575 ActivityRecord resumedActivity = stack.mResumedActivity;
576 if (resumedActivity == null || resumedActivity.app == null) {
577 resumedActivity = stack.mPausingActivity;
578 if (resumedActivity == null || resumedActivity.app == null) {
579 resumedActivity = stack.topRunningActivityLocked(null);
580 }
581 }
582 return resumedActivity;
583 }
584
Dianne Hackbornff072722014-09-24 10:56:28 -0700585 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700586 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800587 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800588 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
589 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800590 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
591 final ActivityStack stack = stacks.get(stackNdx);
592 if (!isFrontStack(stack)) {
593 continue;
594 }
595 ActivityRecord hr = stack.topRunningActivityLocked(null);
596 if (hr != null) {
597 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
598 && processName.equals(hr.processName)) {
599 try {
George Mount2c92c972014-03-20 09:38:23 -0700600 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800601 didSomething = true;
602 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700603 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800604 Slog.w(TAG, "Exception in new application when starting activity "
605 + hr.intent.getComponent().flattenToShortString(), e);
606 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700607 }
Craig Mautner20e72272013-04-01 13:45:53 -0700608 }
Craig Mautner20e72272013-04-01 13:45:53 -0700609 }
610 }
611 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700612 if (!didSomething) {
613 ensureActivitiesVisibleLocked(null, 0);
614 }
Craig Mautner20e72272013-04-01 13:45:53 -0700615 return didSomething;
616 }
617
618 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800619 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
620 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800621 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
622 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner34b73df2014-01-12 21:11:08 -0800623 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800624 continue;
625 }
626 final ActivityRecord resumedActivity = stack.mResumedActivity;
627 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700628 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800629 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800630 return false;
631 }
Craig Mautner20e72272013-04-01 13:45:53 -0700632 }
633 }
634 return true;
635 }
636
Craig Mautnerde4ef022013-04-07 19:01:33 -0700637 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800638 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
639 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800640 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
641 final ActivityStack stack = stacks.get(stackNdx);
642 if (isFrontStack(stack)) {
643 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700644 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800645 return false;
646 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700647 }
648 }
649 }
650 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700651 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800652 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
653 mLastFocusedStack + " to=" + mFocusedStack);
654 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700655 return true;
656 }
657
658 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800659 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800660 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
661 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800662 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
663 final ActivityStack stack = stacks.get(stackNdx);
664 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800665 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700666 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800667 return false;
668 }
669 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800670 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700671 }
672 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800673 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700674 }
675
Craig Mautner2acc3892013-09-23 10:28:14 -0700676 /**
677 * Pause all activities in either all of the stacks or just the back stacks.
678 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700679 * @return true if any activity was paused as a result of this call.
680 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700681 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700682 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800683 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
684 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800685 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
686 final ActivityStack stack = stacks.get(stackNdx);
687 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700688 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800689 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700690 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
691 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800692 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700693 }
694 }
695 return someActivityPaused;
696 }
697
Craig Mautnerde4ef022013-04-07 19:01:33 -0700698 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700699 boolean pausing = true;
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.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700705 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800706 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700707 Slog.d(TAG_STATES,
708 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800709 pausing = false;
710 } else {
711 return false;
712 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700713 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700714 }
715 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700716 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700717 }
718
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700719 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
720 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500721 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800722 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
723 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
724 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
725 final ActivityStack stack = stacks.get(stackNdx);
726 if (stack.mResumedActivity != null &&
727 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700728 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800729 }
730 }
731 }
732 }
733
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700734 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700735 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700736 }
737
738 void sendWaitingVisibleReportLocked(ActivityRecord r) {
739 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700740 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700741 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700742 if (w.who == null) {
743 changed = true;
744 w.timeout = false;
745 if (r != null) {
746 w.who = new ComponentName(r.info.packageName, r.info.name);
747 }
748 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
749 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700750 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700751 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700752 if (changed) {
753 mService.notifyAll();
754 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700755 }
756
757 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
758 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700759 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700760 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700761 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700762 if (w.who == null) {
763 changed = true;
764 w.timeout = timeout;
765 if (r != null) {
766 w.who = new ComponentName(r.info.packageName, r.info.name);
767 }
768 w.thisTime = thisTime;
769 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700770 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700771 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700772 if (changed) {
773 mService.notifyAll();
774 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700775 }
776
Craig Mautner29219d92013-04-16 20:19:12 -0700777 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800778 final ActivityStack focusedStack = mFocusedStack;
Craig Mautner1602ec22013-05-12 10:24:27 -0700779 ActivityRecord r = focusedStack.topRunningActivityLocked(null);
780 if (r != null) {
781 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700782 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700783
Craig Mautner4a1cb222013-12-04 16:14:06 -0800784 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800785 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800786 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
787 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700788 if (stack != focusedStack && isFrontStack(stack)) {
Craig Mautner29219d92013-04-16 20:19:12 -0700789 r = stack.topRunningActivityLocked(null);
790 if (r != null) {
791 return r;
792 }
793 }
794 }
795 return null;
796 }
797
Dianne Hackborn09233282014-04-30 11:33:59 -0700798 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700799 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800800 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
801 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800802 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800803 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800804 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800805 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
806 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700807 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800808 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700809 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700810 }
811 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700812
813 // The lists are already sorted from most recent to oldest. Just pull the most recent off
814 // each list and add it to list. Stop when all lists are empty or maxNum reached.
815 while (maxNum > 0) {
816 long mostRecentActiveTime = Long.MIN_VALUE;
817 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800818 final int numTaskLists = runningTaskLists.size();
819 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
820 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700821 if (!stackTaskList.isEmpty()) {
822 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
823 if (lastActiveTime > mostRecentActiveTime) {
824 mostRecentActiveTime = lastActiveTime;
825 selectedStackList = stackTaskList;
826 }
827 }
828 }
829 if (selectedStackList != null) {
830 list.add(selectedStackList.remove(0));
831 --maxNum;
832 } else {
833 break;
834 }
835 }
Craig Mautner20e72272013-04-01 13:45:53 -0700836 }
837
Craig Mautner23ac33b2013-04-01 16:26:35 -0700838 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700839 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700840 // Collect information about the target of the Intent.
841 ActivityInfo aInfo;
842 try {
843 ResolveInfo rInfo =
844 AppGlobals.getPackageManager().resolveIntent(
845 intent, resolvedType,
846 PackageManager.MATCH_DEFAULT_ONLY
847 | ActivityManagerService.STOCK_PM_FLAGS, userId);
848 aInfo = rInfo != null ? rInfo.activityInfo : null;
849 } catch (RemoteException e) {
850 aInfo = null;
851 }
852
853 if (aInfo != null) {
854 // Store the found target back into the intent, because now that
855 // we have it we never want to do this again. For example, if the
856 // user navigates back to this point in the history, we should
857 // always restart the exact same activity.
858 intent.setComponent(new ComponentName(
859 aInfo.applicationInfo.packageName, aInfo.name));
860
861 // Don't debug things in the system process
862 if ((startFlags&ActivityManager.START_FLAG_DEBUG) != 0) {
863 if (!aInfo.processName.equals("system")) {
864 mService.setDebugApp(aInfo.processName, true, false);
865 }
866 }
867
868 if ((startFlags&ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
869 if (!aInfo.processName.equals("system")) {
870 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
871 }
872 }
873
Jeff Hao1b012d32014-08-20 10:35:34 -0700874 if (profilerInfo != null) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700875 if (!aInfo.processName.equals("system")) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700876 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700877 }
878 }
879 }
880 return aInfo;
881 }
882
Craig Mautner299f9602015-01-26 09:47:33 -0800883 void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
884 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
Dianne Hackborn95465202014-09-15 16:21:55 -0700885 startActivityLocked(null, intent, null, aInfo, null, null, null, null, 0, 0, 0, null,
886 0, 0, 0, null, false, null, null, null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700887 }
888
Craig Mautner23ac33b2013-04-01 16:26:35 -0700889 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700890 String callingPackage, Intent intent, String resolvedType,
891 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700892 IBinder resultTo, String resultWho, int requestCode, int startFlags,
893 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700894 Bundle options, int userId, IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700895 // Refuse possible leaked file descriptors
896 if (intent != null && intent.hasFileDescriptors()) {
897 throw new IllegalArgumentException("File descriptors passed in Intent");
898 }
899 boolean componentSpecified = intent.getComponent() != null;
900
901 // Don't modify the client's object!
902 intent = new Intent(intent);
903
904 // Collect information about the target of the Intent.
Craig Mautner15df08a2015-04-01 12:17:18 -0700905 ActivityInfo aInfo =
906 resolveActivity(intent, resolvedType, startFlags, profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700907
Craig Mautnere0a38842013-12-16 16:14:02 -0800908 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700909 synchronized (mService) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800910 if (container != null && container.mParentActivity != null &&
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700911 container.mParentActivity.state != RESUMED) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800912 // Cannot start a child activity if the parent is not resumed.
913 return ActivityManager.START_CANCELED;
914 }
Dianne Hackborn95465202014-09-15 16:21:55 -0700915 final int realCallingPid = Binder.getCallingPid();
916 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -0700917 int callingPid;
918 if (callingUid >= 0) {
919 callingPid = -1;
920 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700921 callingPid = realCallingPid;
922 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700923 } else {
924 callingPid = callingUid = -1;
925 }
926
Craig Mautnere0a38842013-12-16 16:14:02 -0800927 final ActivityStack stack;
928 if (container == null || container.mStack.isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800929 stack = mFocusedStack;
Craig Mautnere0a38842013-12-16 16:14:02 -0800930 } else {
931 stack = container.mStack;
932 }
Wale Ogunwale60454db2015-01-23 16:05:07 -0800933 stack.mConfigWillChange = config != null && mService.mConfiguration.diff(config) != 0;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700934 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700935 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700936
937 final long origId = Binder.clearCallingIdentity();
938
939 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800940 (aInfo.applicationInfo.privateFlags
941 &ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700942 // This may be a heavy-weight process! Check to see if we already
943 // have another, different heavy-weight process running.
944 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
945 if (mService.mHeavyWeightProcess != null &&
946 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
947 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700948 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700949 if (caller != null) {
950 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
951 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700952 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700953 } else {
954 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -0700955 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -0700956 + intent.toString());
957 ActivityOptions.abort(options);
958 return ActivityManager.START_PERMISSION_DENIED;
959 }
960 }
961
962 IIntentSender target = mService.getIntentSenderLocked(
963 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -0700964 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -0700965 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
966 | PendingIntent.FLAG_ONE_SHOT, null);
967
968 Intent newIntent = new Intent();
969 if (requestCode >= 0) {
970 // Caller is requesting a result.
971 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
972 }
973 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
974 new IntentSender(target));
975 if (mService.mHeavyWeightProcess.activities.size() > 0) {
976 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
977 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
978 hist.packageName);
979 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
980 hist.task.taskId);
981 }
982 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
983 aInfo.packageName);
984 newIntent.setFlags(intent.getFlags());
985 newIntent.setClassName("android",
986 HeavyWeightSwitcherActivity.class.getName());
987 intent = newIntent;
988 resolvedType = null;
989 caller = null;
990 callingUid = Binder.getCallingUid();
991 callingPid = Binder.getCallingPid();
992 componentSpecified = true;
993 try {
994 ResolveInfo rInfo =
995 AppGlobals.getPackageManager().resolveIntent(
996 intent, null,
997 PackageManager.MATCH_DEFAULT_ONLY
998 | ActivityManagerService.STOCK_PM_FLAGS, userId);
999 aInfo = rInfo != null ? rInfo.activityInfo : null;
1000 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1001 } catch (RemoteException e) {
1002 aInfo = null;
1003 }
1004 }
1005 }
1006 }
1007
Dianne Hackborn91097de2014-04-04 18:02:06 -07001008 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
1009 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -07001010 requestCode, callingPid, callingUid, callingPackage,
1011 realCallingPid, realCallingUid, startFlags, options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001012 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001013
Craig Mautner85c11a82014-07-17 12:38:18 -07001014 Binder.restoreCallingIdentity(origId);
1015
Craig Mautnerde4ef022013-04-07 19:01:33 -07001016 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001017 // If the caller also wants to switch to a new configuration,
1018 // do so now. This allows a clean switch, as we are waiting
1019 // for the current activity to pause (so we will not destroy
1020 // it), and have not yet started the next activity.
1021 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
1022 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -07001023 stack.mConfigWillChange = false;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001024 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautner23ac33b2013-04-01 16:26:35 -07001025 "Updating to new configuration after starting activity.");
1026 mService.updateConfigurationLocked(config, null, false, false);
1027 }
1028
Craig Mautner23ac33b2013-04-01 16:26:35 -07001029 if (outResult != null) {
1030 outResult.result = res;
1031 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001032 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001033 do {
1034 try {
1035 mService.wait();
1036 } catch (InterruptedException e) {
1037 }
1038 } while (!outResult.timeout && outResult.who == null);
1039 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001040 ActivityRecord r = stack.topRunningActivityLocked(null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001041 if (r.nowVisible && r.state == RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001042 outResult.timeout = false;
1043 outResult.who = new ComponentName(r.info.packageName, r.info.name);
1044 outResult.totalTime = 0;
1045 outResult.thisTime = 0;
1046 } else {
1047 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001048 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001049 do {
1050 try {
1051 mService.wait();
1052 } catch (InterruptedException e) {
1053 }
1054 } while (!outResult.timeout && outResult.who == null);
1055 }
1056 }
1057 }
1058
1059 return res;
1060 }
1061 }
1062
1063 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1064 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
1065 Bundle options, int userId) {
1066 if (intents == null) {
1067 throw new NullPointerException("intents is null");
1068 }
1069 if (resolvedTypes == null) {
1070 throw new NullPointerException("resolvedTypes is null");
1071 }
1072 if (intents.length != resolvedTypes.length) {
1073 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1074 }
1075
Craig Mautner23ac33b2013-04-01 16:26:35 -07001076
1077 int callingPid;
1078 if (callingUid >= 0) {
1079 callingPid = -1;
1080 } else if (caller == null) {
1081 callingPid = Binder.getCallingPid();
1082 callingUid = Binder.getCallingUid();
1083 } else {
1084 callingPid = callingUid = -1;
1085 }
1086 final long origId = Binder.clearCallingIdentity();
1087 try {
1088 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001089 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001090 for (int i=0; i<intents.length; i++) {
1091 Intent intent = intents[i];
1092 if (intent == null) {
1093 continue;
1094 }
1095
1096 // Refuse possible leaked file descriptors
1097 if (intent != null && intent.hasFileDescriptors()) {
1098 throw new IllegalArgumentException("File descriptors passed in Intent");
1099 }
1100
1101 boolean componentSpecified = intent.getComponent() != null;
1102
1103 // Don't modify the client's object!
1104 intent = new Intent(intent);
1105
1106 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001107 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001108 // TODO: New, check if this is correct
1109 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1110
1111 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001112 (aInfo.applicationInfo.privateFlags
1113 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001114 throw new IllegalArgumentException(
1115 "FLAG_CANT_SAVE_STATE not supported here");
1116 }
1117
1118 Bundle theseOptions;
1119 if (options != null && i == intents.length-1) {
1120 theseOptions = options;
1121 } else {
1122 theseOptions = null;
1123 }
Craig Mautner6170f732013-04-02 13:05:23 -07001124 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001125 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1126 callingPackage, callingPid, callingUid,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001127 0, theseOptions, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001128 if (res < 0) {
1129 return res;
1130 }
1131
1132 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1133 }
1134 }
1135 } finally {
1136 Binder.restoreCallingIdentity(origId);
1137 }
1138
1139 return ActivityManager.START_SUCCESS;
1140 }
1141
Craig Mautner2420ead2013-04-01 17:13:20 -07001142 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001143 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001144 throws RemoteException {
1145
Craig Mautner2568c3a2015-03-26 14:22:34 -07001146 if (andResume) {
1147 r.startFreezingScreenLocked(app, 0);
1148 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001149
Craig Mautner2568c3a2015-03-26 14:22:34 -07001150 // schedule launch ticks to collect information about slow apps.
1151 r.startLaunchTickingLocked();
1152 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001153
1154 // Have the window manager re-evaluate the orientation of
1155 // the screen based on the new activity order. Note that
1156 // as a result of this, it can call back into the activity
1157 // manager with a new orientation. We don't care about that,
1158 // because the activity is not currently running so we are
1159 // just restarting it anyway.
1160 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001161 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001162 mService.mConfiguration,
1163 r.mayFreezeScreenLocked(app) ? r.appToken : null);
1164 mService.updateConfigurationLocked(config, r, false, false);
1165 }
1166
1167 r.app = app;
1168 app.waitingToKill = null;
1169 r.launchCount++;
1170 r.lastLaunchTime = SystemClock.uptimeMillis();
1171
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001172 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001173
1174 int idx = app.activities.indexOf(r);
1175 if (idx < 0) {
1176 app.activities.add(r);
1177 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001178 mService.updateLruProcessLocked(app, true, null);
1179 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001180
Craig Mautner15df08a2015-04-01 12:17:18 -07001181 final TaskRecord task = r.task;
1182 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001183 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001184 }
1185
1186 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001187 try {
1188 if (app.thread == null) {
1189 throw new RemoteException();
1190 }
1191 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001192 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001193 if (andResume) {
1194 results = r.results;
1195 newIntents = r.newIntents;
1196 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001197 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1198 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1199 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001200 if (andResume) {
1201 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1202 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001203 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001204 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001205 if (r.isHomeActivity() && r.isNotResolverActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001206 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001207 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001208 }
1209 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1210 r.sleeping = false;
1211 r.forceNewConfig = false;
1212 mService.showAskCompatModeDialogLocked(r);
1213 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001214 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001215 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1216 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1217 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001218 final String profileFile = mService.mProfileFile;
1219 if (profileFile != null) {
1220 ParcelFileDescriptor profileFd = mService.mProfileFd;
1221 if (profileFd != null) {
1222 try {
1223 profileFd = profileFd.dup();
1224 } catch (IOException e) {
1225 if (profileFd != null) {
1226 try {
1227 profileFd.close();
1228 } catch (IOException o) {
1229 }
1230 profileFd = null;
1231 }
1232 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001233 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001234
1235 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1236 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001237 }
1238 }
1239 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001240
Craig Mautner2568c3a2015-03-26 14:22:34 -07001241 if (andResume) {
1242 app.hasShownUi = true;
1243 app.pendingUiClean = true;
1244 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001245 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001246 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001247 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwale60454db2015-01-23 16:05:07 -08001248 new Configuration(stack.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001249 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001250 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001251
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001252 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001253 // This may be a heavy-weight process! Note that the package
1254 // manager will ensure that only activity can run in the main
1255 // process of the .apk, which is the only thing that will be
1256 // considered heavy-weight.
1257 if (app.processName.equals(app.info.packageName)) {
1258 if (mService.mHeavyWeightProcess != null
1259 && mService.mHeavyWeightProcess != app) {
1260 Slog.w(TAG, "Starting new heavy weight process " + app
1261 + " when already running "
1262 + mService.mHeavyWeightProcess);
1263 }
1264 mService.mHeavyWeightProcess = app;
1265 Message msg = mService.mHandler.obtainMessage(
1266 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1267 msg.obj = r;
1268 mService.mHandler.sendMessage(msg);
1269 }
1270 }
1271
1272 } catch (RemoteException e) {
1273 if (r.launchFailed) {
1274 // This is the second time we failed -- finish activity
1275 // and give up.
1276 Slog.e(TAG, "Second failure launching "
1277 + r.intent.getComponent().flattenToShortString()
1278 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001279 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001280 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1281 "2nd-crash", false);
1282 return false;
1283 }
1284
1285 // This is the first time we failed -- restart process and
1286 // retry.
1287 app.activities.remove(r);
1288 throw e;
1289 }
1290
1291 r.launchFailed = false;
1292 if (stack.updateLRUListLocked(r)) {
1293 Slog.w(TAG, "Activity " + r
1294 + " being launched, but already in LRU list");
1295 }
1296
1297 if (andResume) {
1298 // As part of the process of launching, ActivityThread also performs
1299 // a resume.
1300 stack.minimalResumeActivityLocked(r);
1301 } else {
1302 // This activity is not starting in the resumed state... which
1303 // should look like we asked it to pause+stop (but remain visible),
1304 // and it has done so and reported back the current icicle and
1305 // other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001306 if (DEBUG_STATES) Slog.v(TAG_STATES,
1307 "Moving to STOPPED: " + r + " (starting in stopped state)");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001308 r.state = STOPPED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001309 r.stopped = true;
1310 }
1311
1312 // Launch the new version setup screen if needed. We do this -after-
1313 // launching the initial activity (that is, home), so that it can have
1314 // a chance to initialize itself while in the background, making the
1315 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001316 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001317 mService.startSetupActivityLocked();
1318 }
1319
Dianne Hackborn465fa392014-09-14 14:21:18 -07001320 // Update any services we are bound to that might care about whether
1321 // their client may have activities.
1322 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1323
Craig Mautner2420ead2013-04-01 17:13:20 -07001324 return true;
1325 }
1326
Craig Mautnere79d42682013-04-01 19:01:53 -07001327 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001328 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001329 // Is this activity's application already running?
1330 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001331 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001332
1333 r.task.stack.setLaunchTime(r);
1334
1335 if (app != null && app.thread != null) {
1336 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001337 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1338 || !"android".equals(r.info.packageName)) {
1339 // Don't add this if it is a platform component that is marked
1340 // to run in multiple processes, because this is actually
1341 // part of the framework so doesn't make sense to track as a
1342 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001343 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1344 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001345 }
George Mount2c92c972014-03-20 09:38:23 -07001346 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001347 return;
1348 } catch (RemoteException e) {
1349 Slog.w(TAG, "Exception when starting activity "
1350 + r.intent.getComponent().flattenToShortString(), e);
1351 }
1352
1353 // If a dead object exception was thrown -- fall through to
1354 // restart the application.
1355 }
1356
1357 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001358 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001359 }
1360
Craig Mautner6170f732013-04-02 13:05:23 -07001361 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001362 Intent intent, String resolvedType, ActivityInfo aInfo,
1363 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1364 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001365 int callingPid, int callingUid, String callingPackage,
1366 int realCallingPid, int realCallingUid, int startFlags, Bundle options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001367 boolean componentSpecified, ActivityRecord[] outActivity, ActivityContainer container,
1368 TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001369 int err = ActivityManager.START_SUCCESS;
1370
1371 ProcessRecord callerApp = null;
1372 if (caller != null) {
1373 callerApp = mService.getRecordForAppLocked(caller);
1374 if (callerApp != null) {
1375 callingPid = callerApp.pid;
1376 callingUid = callerApp.info.uid;
1377 } else {
1378 Slog.w(TAG, "Unable to find app for caller " + caller
1379 + " (pid=" + callingPid + ") when starting: "
1380 + intent.toString());
1381 err = ActivityManager.START_PERMISSION_DENIED;
1382 }
1383 }
1384
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001385 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1386
Craig Mautner6170f732013-04-02 13:05:23 -07001387 if (err == ActivityManager.START_SUCCESS) {
Craig Mautner6170f732013-04-02 13:05:23 -07001388 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001389 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001390 + " on display " + (container == null ? (mFocusedStack == null ?
1391 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1392 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1393 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001394 }
1395
1396 ActivityRecord sourceRecord = null;
1397 ActivityRecord resultRecord = null;
1398 if (resultTo != null) {
1399 sourceRecord = isInAnyStackLocked(resultTo);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001400 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
1401 "Will send result to " + resultTo + " " + sourceRecord);
Craig Mautner6170f732013-04-02 13:05:23 -07001402 if (sourceRecord != null) {
1403 if (requestCode >= 0 && !sourceRecord.finishing) {
1404 resultRecord = sourceRecord;
1405 }
1406 }
1407 }
Craig Mautner6170f732013-04-02 13:05:23 -07001408
Dianne Hackborn91097de2014-04-04 18:02:06 -07001409 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001410
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001411 if ((launchFlags & Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001412 // Transfer the result target from the source activity to the new
1413 // one being started, including any failures.
1414 if (requestCode >= 0) {
1415 ActivityOptions.abort(options);
1416 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1417 }
1418 resultRecord = sourceRecord.resultTo;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001419 if (resultRecord != null && !resultRecord.isInStackLocked()) {
1420 resultRecord = null;
1421 }
Craig Mautner6170f732013-04-02 13:05:23 -07001422 resultWho = sourceRecord.resultWho;
1423 requestCode = sourceRecord.requestCode;
1424 sourceRecord.resultTo = null;
1425 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001426 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001427 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001428 if (sourceRecord.launchedFromUid == callingUid) {
1429 // The new activity is being launched from the same uid as the previous
1430 // activity in the flow, and asking to forward its result back to the
1431 // previous. In this case the activity is serving as a trampoline between
1432 // the two, so we also want to update its launchedFromPackage to be the
1433 // same as the previous activity. Note that this is safe, since we know
1434 // these two packages come from the same uid; the caller could just as
1435 // well have supplied that same package name itself. This specifially
1436 // deals with the case of an intent picker/chooser being launched in the app
1437 // flow to redirect to an activity picked by the user, where we want the final
1438 // activity to consider it to have been launched by the previous app activity.
1439 callingPackage = sourceRecord.launchedFromPackage;
1440 }
Craig Mautner6170f732013-04-02 13:05:23 -07001441 }
1442
1443 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1444 // We couldn't find a class that can handle the given Intent.
1445 // That's the end of that!
1446 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1447 }
1448
1449 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1450 // We couldn't find the specific class specified in the Intent.
1451 // Also the end of the line.
1452 err = ActivityManager.START_CLASS_NOT_FOUND;
1453 }
1454
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001455 if (err == ActivityManager.START_SUCCESS
1456 && !isCurrentProfileLocked(userId)
1457 && (aInfo.flags & FLAG_SHOW_FOR_ALL_USERS) == 0) {
1458 // Trying to launch a background activity that doesn't show for all users.
1459 err = ActivityManager.START_NOT_CURRENT_USER_ACTIVITY;
1460 }
1461
Dianne Hackborn91097de2014-04-04 18:02:06 -07001462 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1463 && sourceRecord.task.voiceSession != null) {
1464 // If this activity is being launched as part of a voice session, we need
1465 // to ensure that it is safe to do so. If the upcoming activity will also
1466 // be part of the voice session, we can only launch it if it has explicitly
1467 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001468 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001469 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1470 try {
Dianne Hackborn95465202014-09-15 16:21:55 -07001471 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1472 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07001473 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1474 }
1475 } catch (RemoteException e) {
1476 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1477 }
1478 }
1479 }
1480
1481 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1482 // If the caller is starting a new voice session, just make sure the target
1483 // is actually allowing it to run this way.
1484 try {
1485 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1486 intent, resolvedType)) {
1487 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1488 }
1489 } catch (RemoteException e) {
1490 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1491 }
1492 }
1493
louis_changcd5d1982014-08-01 10:09:08 +08001494 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1495
Craig Mautner6170f732013-04-02 13:05:23 -07001496 if (err != ActivityManager.START_SUCCESS) {
1497 if (resultRecord != null) {
1498 resultStack.sendActivityResultLocked(-1,
1499 resultRecord, resultWho, requestCode,
1500 Activity.RESULT_CANCELED, null);
1501 }
Craig Mautner6170f732013-04-02 13:05:23 -07001502 ActivityOptions.abort(options);
1503 return err;
1504 }
1505
1506 final int startAnyPerm = mService.checkPermission(
1507 START_ANY_ACTIVITY, callingPid, callingUid);
1508 final int componentPerm = mService.checkComponentPermission(aInfo.permission, callingPid,
1509 callingUid, aInfo.applicationInfo.uid, aInfo.exported);
1510 if (startAnyPerm != PERMISSION_GRANTED && componentPerm != PERMISSION_GRANTED) {
1511 if (resultRecord != null) {
1512 resultStack.sendActivityResultLocked(-1,
1513 resultRecord, resultWho, requestCode,
1514 Activity.RESULT_CANCELED, null);
1515 }
Craig Mautner6170f732013-04-02 13:05:23 -07001516 String msg;
1517 if (!aInfo.exported) {
1518 msg = "Permission Denial: starting " + intent.toString()
1519 + " from " + callerApp + " (pid=" + callingPid
1520 + ", uid=" + callingUid + ")"
1521 + " not exported from uid " + aInfo.applicationInfo.uid;
1522 } else {
1523 msg = "Permission Denial: starting " + intent.toString()
1524 + " from " + callerApp + " (pid=" + callingPid
1525 + ", uid=" + callingUid + ")"
1526 + " requires " + aInfo.permission;
1527 }
1528 Slog.w(TAG, msg);
1529 throw new SecurityException(msg);
1530 }
1531
Ben Gruverdd72c9e2013-08-06 12:34:17 -07001532 boolean abort = !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001533 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001534
Craig Mautner6170f732013-04-02 13:05:23 -07001535 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001536 try {
1537 // The Intent we give to the watcher has the extra data
1538 // stripped off, since it can contain private information.
1539 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001540 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001541 aInfo.applicationInfo.packageName);
1542 } catch (RemoteException e) {
1543 mService.mController = null;
1544 }
Ben Gruver5e207332013-04-03 17:41:37 -07001545 }
Craig Mautner6170f732013-04-02 13:05:23 -07001546
Ben Gruver5e207332013-04-03 17:41:37 -07001547 if (abort) {
1548 if (resultRecord != null) {
1549 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001550 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001551 }
Ben Gruver5e207332013-04-03 17:41:37 -07001552 // We pretend to the caller that it was really started, but
1553 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001554 ActivityOptions.abort(options);
1555 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001556 }
1557
1558 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001559 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Craig Mautner233ceee2014-05-09 17:05:11 -07001560 requestCode, componentSpecified, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001561 if (outActivity != null) {
1562 outActivity[0] = r;
1563 }
1564
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001565 if (r.appTimeTracker == null && sourceRecord != null) {
1566 // If the caller didn't specify an explicit time tracker, we want to continue
1567 // tracking under any it has.
1568 r.appTimeTracker = sourceRecord.appTimeTracker;
1569 }
1570
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001571 final ActivityStack stack = mFocusedStack;
Dianne Hackborn91097de2014-04-04 18:02:06 -07001572 if (voiceSession == null && (stack.mResumedActivity == null
1573 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001574 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1575 realCallingPid, realCallingUid, "Activity start")) {
Craig Mautner6170f732013-04-02 13:05:23 -07001576 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001577 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautneree36c772014-07-16 14:56:05 -07001578 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001579 ActivityOptions.abort(options);
1580 return ActivityManager.START_SWITCHES_CANCELED;
1581 }
1582 }
1583
1584 if (mService.mDidAppSwitch) {
1585 // This is the second allowed switch since we stopped switches,
1586 // so now just generally allow switches. Use case: user presses
1587 // home (switches disabled, switch to home, mDidAppSwitch now true);
1588 // user taps a home icon (coming from home so allowed, we hit here
1589 // and now allow anyone to switch again).
1590 mService.mAppSwitchesAllowedTime = 0;
1591 } else {
1592 mService.mDidAppSwitch = true;
1593 }
1594
Craig Mautneree36c772014-07-16 14:56:05 -07001595 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001596
Dianne Hackborn91097de2014-04-04 18:02:06 -07001597 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001598 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001599
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001600 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001601 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001602 // activity start, but we are not actually doing an activity
1603 // switch... just dismiss the keyguard now, because we
1604 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001605 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001606 }
1607 return err;
1608 }
1609
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001610 ActivityStack computeStackFocus(ActivityRecord r, boolean newTask) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001611 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001612
1613 // On leanback only devices we should keep all activities in the same stack.
1614 if (!mLeanbackOnlyDevice &&
1615 (r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001616
1617 ActivityStack stack;
1618
Wale Ogunwale7d701172015-03-11 15:36:30 -07001619 if (task != null && task.stack != null) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001620 stack = task.stack;
1621 if (stack.isOnHomeDisplay()) {
1622 if (mFocusedStack != stack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001623 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1624 "computeStackFocus: Setting " + "focused stack to r=" + r
1625 + " task=" + task);
Craig Mautnere0a38842013-12-16 16:14:02 -08001626 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001627 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001628 "computeStackFocus: Focused stack already=" + mFocusedStack);
Craig Mautnere0a38842013-12-16 16:14:02 -08001629 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001630 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001631 return stack;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001632 }
1633
Craig Mautnere0a38842013-12-16 16:14:02 -08001634 final ActivityContainer container = r.mInitialActivityContainer;
1635 if (container != null) {
1636 // The first time put it on the desired stack, after this put on task stack.
1637 r.mInitialActivityContainer = null;
1638 return container.mStack;
1639 }
1640
Craig Mautner1b4bf852014-05-26 15:06:32 -07001641 if (mFocusedStack != mHomeStack && (!newTask ||
1642 mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001643 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001644 "computeStackFocus: Have a focused stack=" + mFocusedStack);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001645 return mFocusedStack;
1646 }
1647
Craig Mautnere0a38842013-12-16 16:14:02 -08001648 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1649 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001650 stack = homeDisplayStacks.get(stackNdx);
Craig Mautnere0a38842013-12-16 16:14:02 -08001651 if (!stack.isHomeStack()) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001652 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001653 "computeStackFocus: Setting focused stack=" + stack);
1654 return stack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001655 }
1656 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001657
Craig Mautner4a1cb222013-12-04 16:14:06 -08001658 // Need to create an app stack for this user.
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001659 stack = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001660 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS, "computeStackFocus: New stack r="
1661 + r + " stackId=" + stack.mStackId);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001662 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001663 }
1664 return mHomeStack;
1665 }
1666
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001667 boolean setFocusedStack(ActivityRecord r, String reason) {
1668 if (r == null) {
1669 // Not sure what you are trying to do, but it is not going to work...
1670 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001671 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001672 final TaskRecord task = r.task;
1673 if (task == null || task.stack == null) {
1674 Slog.w(TAG, "Can't set focus stack for r=" + r + " task=" + task);
1675 return false;
1676 }
1677 task.stack.moveToFront(reason);
1678 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001679 }
1680
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001681 final int startActivityUncheckedLocked(final ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001682 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001683 boolean doResume, Bundle options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001684 final Intent intent = r.intent;
1685 final int callingUid = r.launchedFromUid;
1686
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001687 // In some flows in to this function, we retrieve the task record and hold on to it
1688 // without a lock before calling back in to here... so the task at this point may
1689 // not actually be in recents. Check for that, and if it isn't in recents just
1690 // consider it invalid.
1691 if (inTask != null && !inTask.inRecents) {
1692 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1693 inTask = null;
1694 }
1695
Craig Mautnerad400af2014-08-13 16:41:36 -07001696 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001697 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1698 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001699
Craig Mautnera228ae92014-07-09 05:44:55 -07001700 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001701 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001702 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001703 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1704 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1705 "\"singleInstance\" or \"singleTask\"");
1706 launchFlags &=
1707 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1708 } else {
1709 switch (r.info.documentLaunchMode) {
1710 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1711 break;
1712 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1713 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1714 break;
1715 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1716 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1717 break;
1718 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1719 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1720 break;
1721 }
1722 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001723
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001724 final boolean launchTaskBehind = r.mLaunchTaskBehind
1725 && !launchSingleTask && !launchSingleInstance
1726 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001727
Wale Ogunwale7d701172015-03-11 15:36:30 -07001728 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0
1729 && r.resultTo.task.stack != null) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001730 // For whatever reason this activity is being launched into a new
1731 // task... yet the caller has requested a result back. Well, that
1732 // is pretty messed up, so instead immediately send back a cancel
1733 // and let the new task continue launched as normal without a
1734 // dependency on its originator.
1735 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1736 r.resultTo.task.stack.sendActivityResultLocked(-1,
1737 r.resultTo, r.resultWho, r.requestCode,
1738 Activity.RESULT_CANCELED, null);
1739 r.resultTo = null;
1740 }
1741
1742 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1743 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1744 }
1745
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001746 // If we are actually going to launch in to a new task, there are some cases where
1747 // we further want to do multiple task.
1748 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1749 if (launchTaskBehind
1750 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
1751 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1752 }
1753 }
1754
Craig Mautner8849a5e2013-04-02 16:41:03 -07001755 // We'll invoke onUserLeaving before onPause only if the launching
1756 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001757 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001758 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
1759 "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001760
1761 // If the caller has asked not to resume at this point, we make note
1762 // of this in the record so that we can skip it when trying to find
1763 // the top running activity.
1764 if (!doResume) {
1765 r.delayedResume = true;
1766 }
1767
Craig Mautnera254cd72014-05-25 16:47:39 -07001768 ActivityRecord notTop =
1769 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001770
1771 // If the onlyIfNeeded flag is set, then we can do this if the activity
1772 // being launched is the same as the one making the call... or, as
1773 // a special case, if we do not know the caller then we count the
1774 // current top activity as the caller.
1775 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1776 ActivityRecord checkedCaller = sourceRecord;
1777 if (checkedCaller == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001778 checkedCaller = mFocusedStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001779 }
1780 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1781 // Caller is not the same as launcher, so always needed.
1782 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1783 }
1784 }
1785
Craig Mautner8849a5e2013-04-02 16:41:03 -07001786 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001787 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001788
1789 // If the caller is not coming from another activity, but has given us an
1790 // explicit task into which they would like us to launch the new activity,
1791 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001792 if (sourceRecord == null && inTask != null && inTask.stack != null) {
1793 final Intent baseIntent = inTask.getBaseIntent();
1794 final ActivityRecord root = inTask.getRootActivity();
1795 if (baseIntent == null) {
1796 ActivityOptions.abort(options);
1797 throw new IllegalArgumentException("Launching into task without base intent: "
1798 + inTask);
1799 }
1800
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001801 // If this task is empty, then we are adding the first activity -- it
1802 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001803 if (launchSingleInstance || launchSingleTask) {
1804 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
1805 ActivityOptions.abort(options);
1806 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
1807 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001808 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001809 if (root != null) {
1810 ActivityOptions.abort(options);
1811 throw new IllegalArgumentException("Caller with inTask " + inTask
1812 + " has root " + root + " but target is singleInstance/Task");
1813 }
1814 }
1815
1816 // If task is empty, then adopt the interesting intent launch flags in to the
1817 // activity being started.
1818 if (root == null) {
1819 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
1820 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
1821 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
1822 launchFlags = (launchFlags&~flagsOfInterest)
1823 | (baseIntent.getFlags()&flagsOfInterest);
1824 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001825 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07001826 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001827
Dianne Hackborn962d5352014-08-29 16:27:40 -07001828 // If the task is not empty and the caller is asking to start it as the root
1829 // of a new task, then we don't actually want to start this on the task. We
1830 // will bring the task to the front, and possibly give it a new intent.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001831 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07001832 addingToTask = false;
1833
1834 } else {
1835 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001836 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07001837
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001838 reuseTask = inTask;
1839 } else {
1840 inTask = null;
1841 }
1842
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001843 if (inTask == null) {
1844 if (sourceRecord == null) {
1845 // This activity is not being started from another... in this
1846 // case we -always- start a new task.
1847 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
1848 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
1849 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1850 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1851 }
1852 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1853 // The original activity who is starting us is running as a single
1854 // instance... this new activity it is starting must go on its
1855 // own task.
1856 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1857 } else if (launchSingleInstance || launchSingleTask) {
1858 // The activity being started is a single instance... it always
1859 // gets launched into its own task.
1860 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1861 }
1862 }
1863
1864 ActivityInfo newTaskInfo = null;
1865 Intent newTaskIntent = null;
1866 ActivityStack sourceStack;
1867 if (sourceRecord != null) {
1868 if (sourceRecord.finishing) {
1869 // If the source is finishing, we can't further count it as our source. This
1870 // is because the task it is associated with may now be empty and on its way out,
1871 // so we don't want to blindly throw it in to that task. Instead we will take
1872 // the NEW_TASK flow and try to find a task for it. But save the task information
1873 // so it can be used when creating the new task.
1874 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
1875 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
1876 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1877 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1878 newTaskInfo = sourceRecord.info;
1879 newTaskIntent = sourceRecord.task.intent;
1880 }
1881 sourceRecord = null;
1882 sourceStack = null;
1883 } else {
1884 sourceStack = sourceRecord.task.stack;
1885 }
1886 } else {
1887 sourceStack = null;
1888 }
1889
1890 boolean movedHome = false;
1891 ActivityStack targetStack;
1892
1893 intent.setFlags(launchFlags);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001894 final boolean noAnimation = (launchFlags & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001895
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001896 // We may want to try to place the new activity in to an existing task. We always
1897 // do this if the target activity is singleTask or singleInstance; we will also do
1898 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
1899 // us to still place it in a new task: multi task, always doc mode, or being asked to
1900 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07001901 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
1902 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07001903 || launchSingleInstance || launchSingleTask) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001904 // If bring to front is requested, and no result is requested and we have not
1905 // been given an explicit task to launch in to, and
Craig Mautner8849a5e2013-04-02 16:41:03 -07001906 // we can find a task that was started with this same
1907 // component, then instead of launching bring that one to the front.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001908 if (inTask == null && r.resultTo == null) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001909 // See if there is a task to bring to the front. If this is
1910 // a SINGLE_INSTANCE activity, there can be one and only one
1911 // instance of it in the history, and it is always in its own
1912 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07001913 ActivityRecord intentActivity = !launchSingleInstance ?
1914 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001915 if (intentActivity != null) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001916 if (isLockTaskModeViolation(intentActivity.task)) {
Craig Mautner6cd6cec2015-04-01 00:02:12 -07001917 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07001918 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08001919 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1920 }
Craig Mautner29219d92013-04-16 20:19:12 -07001921 if (r.task == null) {
1922 r.task = intentActivity.task;
1923 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001924 if (intentActivity.task.intent == null) {
1925 // This task was started because of movement of
1926 // the activity based on affinity... now that we
1927 // are actually launching it, we can assign the
1928 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07001929 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001930 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001931 targetStack = intentActivity.task.stack;
1932 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001933 // If the target task is not in the front, then we need
1934 // to bring it to the front... except... well, with
1935 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
1936 // to have the same behavior as if a new instance was
1937 // being started, which means not bringing it to the front
1938 // if the caller is not itself in the front.
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001939 final ActivityStack focusStack = getFocusedStack();
1940 ActivityRecord curTop = (focusStack == null)
1941 ? null : focusStack.topRunningNonDelayedActivityLocked(notTop);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001942 boolean movedToFront = false;
Craig Mautner7504d7b2013-09-17 10:50:53 -07001943 if (curTop != null && (curTop.task != intentActivity.task ||
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001944 curTop.task != focusStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001945 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07001946 if (sourceRecord == null || (sourceStack.topActivity() != null &&
1947 sourceStack.topActivity().task == sourceRecord.task)) {
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001948 // We really do want to push this one into the user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07001949 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07001950 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
1951 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001952 movedHome = true;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001953 targetStack.moveTaskToFrontLocked(intentActivity.task, noAnimation,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001954 options, r.appTimeTracker, "bringingFoundTaskToFront");
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001955 movedToFront = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001956 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07001957 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
1958 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07001959 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07001960 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001961 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001962 options = null;
1963 }
1964 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001965 if (!movedToFront) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001966 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Bring to front target: " + targetStack
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001967 + " from " + intentActivity);
1968 targetStack.moveToFront("intentActivityFound");
1969 }
1970
Craig Mautner8849a5e2013-04-02 16:41:03 -07001971 // If the caller has requested that the target task be
1972 // reset, then do so.
1973 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1974 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
1975 }
Craig Mautner15df08a2015-04-01 12:17:18 -07001976 if ((startFlags & ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001977 // We don't need to start a new activity, and
1978 // the client said not to do anything if that
1979 // is the case, so this is it! And for paranoia, make
1980 // sure we have correctly resumed the top activity.
1981 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07001982 resumeTopActivitiesLocked(targetStack, null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001983
1984 // Make sure to notify Keyguard as well if we are not running an app
1985 // transition later.
1986 if (!movedToFront) {
1987 notifyActivityDrawnForKeyguard();
1988 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001989 } else {
1990 ActivityOptions.abort(options);
1991 }
1992 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
1993 }
Craig Mautner15df08a2015-04-01 12:17:18 -07001994 if ((launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001995 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001996 // The caller has requested to completely replace any
1997 // existing task with its new activity. Well that should
1998 // not be too hard...
1999 reuseTask = intentActivity.task;
2000 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07002001 reuseTask.setIntent(r);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002002 } else if ((launchFlags & FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07002003 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002004 // In this situation we want to remove all activities
2005 // from the task up to the one being started. In most
2006 // cases this means we are resetting the task to its
2007 // initial state.
2008 ActivityRecord top =
2009 intentActivity.task.performClearTaskLocked(r, launchFlags);
2010 if (top != null) {
2011 if (top.frontOfTask) {
2012 // Activity aliases may mean we use different
2013 // intents for the top activity, so make sure
2014 // the task now has the identity of the new
2015 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07002016 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002017 }
2018 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
2019 r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002020 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002021 } else {
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002022 // A special case: we need to start the activity because it is not
2023 // currently running, and the caller has asked to clear the current
2024 // task to have this activity at the top.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002025 addingToTask = true;
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002026 // Now pretend like this activity is being started by the top of its
2027 // task, so it is put in the right place.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002028 sourceRecord = intentActivity;
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002029 TaskRecord task = sourceRecord.task;
2030 if (task != null && task.stack == null) {
2031 // Target stack got cleared when we all activities were removed
2032 // above. Go ahead and reset it.
2033 targetStack = computeStackFocus(sourceRecord, false /* newTask */);
2034 targetStack.addTask(
2035 task, !launchTaskBehind /* toTop */, false /* moving */);
2036 }
2037
Craig Mautner8849a5e2013-04-02 16:41:03 -07002038 }
2039 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
2040 // In this case the top activity on the task is the
2041 // same as the one being launched, so we take that
2042 // as a request to bring the task to the foreground.
2043 // If the top activity in the task is the root
2044 // activity, deliver this new intent to it if it
2045 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07002046 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07002047 && intentActivity.realActivity.equals(r.realActivity)) {
2048 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
2049 intentActivity.task);
2050 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07002051 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002052 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002053 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
2054 r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002055 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
2056 // In this case we are launching the root activity
2057 // of the task, but with a different intent. We
2058 // should start a new instance on top.
2059 addingToTask = true;
2060 sourceRecord = intentActivity;
2061 }
2062 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
2063 // In this case an activity is being launched in to an
2064 // existing task, without resetting that task. This
2065 // is typically the situation of launching an activity
2066 // from a notification or shortcut. We want to place
2067 // the new activity on top of the current task.
2068 addingToTask = true;
2069 sourceRecord = intentActivity;
2070 } else if (!intentActivity.task.rootWasReset) {
2071 // In this case we are launching in to an existing task
2072 // that has not yet been started from its front door.
2073 // The current task has been brought to the front.
2074 // Ideally, we'd probably like to place this new task
2075 // at the bottom of its stack, but that's a little hard
2076 // to do with the current organization of the code so
2077 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07002078 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002079 }
2080 if (!addingToTask && reuseTask == null) {
2081 // We didn't do anything... but it was needed (a.k.a., client
2082 // don't use that intent!) And for paranoia, make
2083 // sure we have correctly resumed the top activity.
2084 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002085 targetStack.resumeTopActivityLocked(null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002086 if (!movedToFront) {
2087 // Make sure to notify Keyguard as well if we are not running an app
2088 // transition later.
2089 notifyActivityDrawnForKeyguard();
2090 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002091 } else {
2092 ActivityOptions.abort(options);
2093 }
2094 return ActivityManager.START_TASK_TO_FRONT;
2095 }
2096 }
2097 }
2098 }
2099
2100 //String uri = r.intent.toURI();
2101 //Intent intent2 = new Intent(uri);
2102 //Slog.i(TAG, "Given intent: " + r.intent);
2103 //Slog.i(TAG, "URI is: " + uri);
2104 //Slog.i(TAG, "To intent: " + intent2);
2105
2106 if (r.packageName != null) {
2107 // If the activity being launched is the same as the one currently
2108 // at the top, then we need to check if it should only be launched
2109 // once.
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002110 ActivityStack topStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002111 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002112 if (top != null && r.resultTo == null) {
2113 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2114 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07002115 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07002116 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002117 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
2118 top.task);
2119 // For paranoia, make sure we have correctly
2120 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002121 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002122 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002123 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002124 }
2125 ActivityOptions.abort(options);
2126 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2127 // We don't need to start a new activity, and
2128 // the client said not to do anything if that
2129 // is the case, so this is it!
2130 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2131 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002132 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002133 return ActivityManager.START_DELIVERED_TO_TOP;
2134 }
2135 }
2136 }
2137 }
2138
2139 } else {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002140 if (r.resultTo != null && r.resultTo.task.stack != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002141 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2142 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002143 }
2144 ActivityOptions.abort(options);
2145 return ActivityManager.START_CLASS_NOT_FOUND;
2146 }
2147
2148 boolean newTask = false;
2149 boolean keepCurTransition = false;
2150
Craig Mautnerbb742462014-07-07 15:28:55 -07002151 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002152 sourceRecord.task : null;
2153
Craig Mautner8849a5e2013-04-02 16:41:03 -07002154 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002155 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002156 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002157 newTask = true;
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002158 targetStack = computeStackFocus(r, newTask);
2159 targetStack.moveToFront("startingNewTask");
2160
Craig Mautner8849a5e2013-04-02 16:41:03 -07002161 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002162 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2163 newTaskInfo != null ? newTaskInfo : r.info,
2164 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002165 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2166 taskToAffiliate);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002167 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2168 "Starting new activity " + r + " in new task " + r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002169 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002170 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002171 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002172 if (isLockTaskModeViolation(r.task)) {
2173 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2174 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2175 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002176 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002177 if ((launchFlags &
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002178 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2179 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002180 // Caller wants to appear on home activity, so before starting
2181 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002182 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002183 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002184 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002185 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002186 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002187 if (isLockTaskModeViolation(sourceTask)) {
2188 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2189 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2190 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002191 targetStack = sourceTask.stack;
Craig Mautner299f9602015-01-26 09:47:33 -08002192 targetStack.moveToFront("sourceStackToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002193 final TaskRecord topTask = targetStack.topTask();
2194 if (topTask != sourceTask) {
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002195 targetStack.moveTaskToFrontLocked(sourceTask, noAnimation, options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002196 r.appTimeTracker, "sourceTaskToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002197 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002198 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002199 // In this case, we are adding the activity to an existing
2200 // task, but the caller has asked to clear that task if the
2201 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002202 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002203 keepCurTransition = true;
2204 if (top != null) {
2205 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002206 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002207 // For paranoia, make sure we have correctly
2208 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002209 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002210 if (doResume) {
2211 targetStack.resumeTopActivityLocked(null);
2212 }
2213 ActivityOptions.abort(options);
2214 return ActivityManager.START_DELIVERED_TO_TOP;
2215 }
2216 } else if (!addingToTask &&
2217 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2218 // In this case, we are launching an activity in our own task
2219 // that may already be running somewhere in the history, and
2220 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002221 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002222 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002223 final TaskRecord task = top.task;
2224 task.moveActivityToFrontLocked(top);
2225 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002226 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002227 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002228 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002229 if (doResume) {
2230 targetStack.resumeTopActivityLocked(null);
2231 }
2232 return ActivityManager.START_DELIVERED_TO_TOP;
2233 }
2234 }
2235 // An existing activity is starting this new activity, so we want
2236 // to keep the new one in the same task as the one that is starting
2237 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002238 r.setTask(sourceTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002239 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002240 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002241
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002242 } else if (inTask != null) {
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002243 // The caller is asking that the new activity be started in an explicit
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002244 // task it has provided to us.
2245 if (isLockTaskModeViolation(inTask)) {
2246 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2247 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2248 }
2249 targetStack = inTask.stack;
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002250 targetStack.moveTaskToFrontLocked(inTask, noAnimation, options, r.appTimeTracker,
2251 "inTaskToFront");
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002252
2253 // Check whether we should actually launch the new activity in to the task,
2254 // or just reuse the current activity on top.
2255 ActivityRecord top = inTask.getTopActivity();
2256 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2257 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2258 || launchSingleTop || launchSingleTask) {
2259 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2260 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2261 // We don't need to start a new activity, and
2262 // the client said not to do anything if that
2263 // is the case, so this is it!
2264 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2265 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002266 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002267 return ActivityManager.START_DELIVERED_TO_TOP;
2268 }
2269 }
2270
Dianne Hackborn962d5352014-08-29 16:27:40 -07002271 if (!addingToTask) {
2272 // We don't actually want to have this activity added to the task, so just
2273 // stop here but still tell the caller that we consumed the intent.
2274 ActivityOptions.abort(options);
2275 return ActivityManager.START_TASK_TO_FRONT;
2276 }
2277
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002278 r.setTask(inTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002279 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002280 + " in explicit task " + r.task);
2281
Craig Mautner8849a5e2013-04-02 16:41:03 -07002282 } else {
2283 // This not being started from an existing activity, and not part
2284 // of a new task... just put it in the top task, though these days
2285 // this case should never happen.
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002286 targetStack = computeStackFocus(r, newTask);
Craig Mautner299f9602015-01-26 09:47:33 -08002287 targetStack.moveToFront("addingToTopTask");
Craig Mautner1602ec22013-05-12 10:24:27 -07002288 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002289 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002290 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002291 mWindowManager.moveTaskToTop(r.task.taskId);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002292 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Craig Mautner8849a5e2013-04-02 16:41:03 -07002293 + " in new guessed " + r.task);
2294 }
2295
2296 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002297 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002298
Craig Mautner76e2a762014-06-24 09:05:43 -07002299 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2300 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2301 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002302 if (newTask) {
2303 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2304 }
2305 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002306 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002307 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Craig Mautnerbb742462014-07-07 15:28:55 -07002308 if (!launchTaskBehind) {
2309 // Don't set focus on an activity that's going to the back.
Craig Mautner299f9602015-01-26 09:47:33 -08002310 mService.setFocusedActivityLocked(r, "startedActivity");
Craig Mautnerbb742462014-07-07 15:28:55 -07002311 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002312 return ActivityManager.START_SUCCESS;
2313 }
2314
Craig Mautneree36c772014-07-16 14:56:05 -07002315 final void doPendingActivityLaunchesLocked(boolean doResume) {
2316 while (!mPendingActivityLaunches.isEmpty()) {
2317 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Craig Mautneraa9b0f12014-07-17 10:50:18 -07002318 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002319 doResume && mPendingActivityLaunches.isEmpty(), null, null);
Craig Mautneree36c772014-07-16 14:56:05 -07002320 }
2321 }
2322
Craig Mautner7f13ed32014-07-28 14:00:35 -07002323 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002324 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2325 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002326 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002327 mPendingActivityLaunches.remove(palNdx);
2328 }
2329 }
2330 }
2331
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002332 void setLaunchSource(int uid) {
2333 mLaunchingActivity.setWorkSource(new WorkSource(uid));
2334 }
2335
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002336 void acquireLaunchWakelock() {
2337 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2338 throw new IllegalStateException("Calling must be system uid");
2339 }
2340 mLaunchingActivity.acquire();
2341 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2342 // To be safe, don't allow the wake lock to be held for too long.
2343 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2344 }
2345 }
2346
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002347 /**
2348 * Called when the frontmost task is idle.
2349 * @return the state of mService.mBooting before this was called.
2350 */
2351 private boolean checkFinishBootingLocked() {
2352 final boolean booting = mService.mBooting;
2353 boolean enableScreen = false;
2354 mService.mBooting = false;
2355 if (!mService.mBooted) {
2356 mService.mBooted = true;
2357 enableScreen = true;
2358 }
2359 if (booting || enableScreen) {
2360 mService.postFinishBooting(booting, enableScreen);
2361 }
2362 return booting;
2363 }
2364
Craig Mautnerf3333272013-04-22 10:55:53 -07002365 // Checked.
2366 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2367 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002368 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002369
Craig Mautnerf3333272013-04-22 10:55:53 -07002370 ArrayList<ActivityRecord> stops = null;
2371 ArrayList<ActivityRecord> finishes = null;
2372 ArrayList<UserStartedState> startingUsers = null;
2373 int NS = 0;
2374 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002375 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002376 boolean activityRemoved = false;
2377
Wale Ogunwale7d701172015-03-11 15:36:30 -07002378 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002379 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002380 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
2381 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002382 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2383 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002384 if (fromTimeout) {
2385 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002386 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002387
2388 // This is a hack to semi-deal with a race condition
2389 // in the client where it can be constructed with a
2390 // newer configuration from when we asked it to launch.
2391 // We'll update with whatever configuration it now says
2392 // it used to launch.
2393 if (config != null) {
2394 r.configuration = config;
2395 }
2396
2397 // We are now idle. If someone is waiting for a thumbnail from
2398 // us, we can now deliver.
2399 r.idle = true;
2400
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002401 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Dianne Hackborn9449a612014-10-01 15:53:28 -07002402 if (isFrontStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002403 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002404 }
2405 }
2406
2407 if (allResumedActivitiesIdle()) {
2408 if (r != null) {
2409 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002410 }
2411
2412 if (mLaunchingActivity.isHeld()) {
2413 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2414 if (VALIDATE_WAKE_LOCK_CALLER &&
2415 Binder.getCallingUid() != Process.myUid()) {
2416 throw new IllegalStateException("Calling must be system uid");
2417 }
2418 mLaunchingActivity.release();
2419 }
2420 ensureActivitiesVisibleLocked(null, 0);
Craig Mautnerf3333272013-04-22 10:55:53 -07002421 }
2422
2423 // Atomically retrieve all of the other things to do.
2424 stops = processStoppingActivitiesLocked(true);
2425 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002426 if ((NF = mFinishingActivities.size()) > 0) {
2427 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002428 mFinishingActivities.clear();
2429 }
2430
Craig Mautnerf3333272013-04-22 10:55:53 -07002431 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002432 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07002433 mStartingUsers.clear();
2434 }
2435
Craig Mautnerf3333272013-04-22 10:55:53 -07002436 // Stop any activities that are scheduled to do so but have been
2437 // waiting for the next one to start.
2438 for (int i = 0; i < NS; i++) {
2439 r = stops.get(i);
2440 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002441 if (stack != null) {
2442 if (r.finishing) {
2443 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2444 } else {
2445 stack.stopActivityLocked(r);
2446 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002447 }
2448 }
2449
2450 // Finish any activities that are scheduled to do so but have been
2451 // waiting for the next one to start.
2452 for (int i = 0; i < NF; i++) {
2453 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07002454 final ActivityStack stack = r.task.stack;
2455 if (stack != null) {
2456 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
2457 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002458 }
2459
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002460 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002461 // Complete user switch
2462 if (startingUsers != null) {
2463 for (int i = 0; i < startingUsers.size(); i++) {
2464 mService.finishUserSwitch(startingUsers.get(i));
2465 }
2466 }
2467 // Complete starting up of background users
2468 if (mStartingBackgroundUsers.size() > 0) {
2469 startingUsers = new ArrayList<UserStartedState>(mStartingBackgroundUsers);
2470 mStartingBackgroundUsers.clear();
2471 for (int i = 0; i < startingUsers.size(); i++) {
2472 mService.finishUserBoot(startingUsers.get(i));
2473 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002474 }
2475 }
2476
2477 mService.trimApplications();
2478 //dump();
2479 //mWindowManager.dump();
2480
Craig Mautnerf3333272013-04-22 10:55:53 -07002481 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002482 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002483 }
2484
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002485 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002486 }
2487
Craig Mautner8e569572013-10-11 17:36:59 -07002488 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002489 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002490 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2491 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002492 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2493 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2494 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002495 }
Craig Mautner19091252013-10-05 00:03:53 -07002496 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002497 }
2498
2499 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002500 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2501 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002502 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2503 stacks.get(stackNdx).closeSystemDialogsLocked();
2504 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002505 }
2506 }
2507
Craig Mautner93529a42013-10-04 15:03:13 -07002508 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002509 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002510 }
2511
Craig Mautner8d341ef2013-03-26 09:03:27 -07002512 /**
2513 * @return true if some activity was finished (or would have finished if doit were true).
2514 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07002515 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
2516 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002517 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002518 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2519 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002520 final int numStacks = stacks.size();
2521 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2522 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07002523 if (stack.finishDisabledPackageActivitiesLocked(
2524 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002525 didSomething = true;
2526 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002527 }
2528 }
2529 return didSomething;
2530 }
2531
Dianne Hackborna413dc02013-07-12 12:02:55 -07002532 void updatePreviousProcessLocked(ActivityRecord r) {
2533 // Now that this process has stopped, we may want to consider
2534 // it to be the previous app to try to keep around in case
2535 // the user wants to return to it.
2536
2537 // First, found out what is currently the foreground app, so that
2538 // we don't blow away the previous app if this activity is being
2539 // hosted by the process that is actually still the foreground.
2540 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002541 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2542 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002543 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2544 final ActivityStack stack = stacks.get(stackNdx);
2545 if (isFrontStack(stack)) {
2546 if (stack.mResumedActivity != null) {
2547 fgApp = stack.mResumedActivity.app;
2548 } else if (stack.mPausingActivity != null) {
2549 fgApp = stack.mPausingActivity.app;
2550 }
2551 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002552 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002553 }
2554 }
2555
2556 // Now set this one as the previous process, only if that really
2557 // makes sense to.
2558 if (r.app != null && fgApp != null && r.app != fgApp
2559 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002560 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002561 mService.mPreviousProcess = r.app;
2562 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2563 }
2564 }
2565
Craig Mautner05d29032013-05-03 13:40:13 -07002566 boolean resumeTopActivitiesLocked() {
2567 return resumeTopActivitiesLocked(null, null, null);
2568 }
2569
2570 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2571 Bundle targetOptions) {
2572 if (targetStack == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002573 targetStack = mFocusedStack;
Craig Mautner05d29032013-05-03 13:40:13 -07002574 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002575 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002576 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002577 if (isFrontStack(targetStack)) {
2578 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2579 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002580
Craig Mautnere0a38842013-12-16 16:14:02 -08002581 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2582 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002583 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2584 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002585 if (stack == targetStack) {
2586 // Already started above.
2587 continue;
2588 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002589 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002590 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002591 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002592 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002593 }
Craig Mautner05d29032013-05-03 13:40:13 -07002594 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002595 }
2596
Todd Kennedy539db512014-12-15 09:57:55 -08002597 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002598 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2599 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002600 final int numStacks = stacks.size();
2601 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2602 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08002603 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002604 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002605 }
2606 }
2607
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002608 void finishVoiceTask(IVoiceInteractionSession session) {
2609 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2610 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2611 final int numStacks = stacks.size();
2612 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2613 final ActivityStack stack = stacks.get(stackNdx);
2614 stack.finishVoiceTask(session);
2615 }
2616 }
2617 }
2618
Craig Mautner299f9602015-01-26 09:47:33 -08002619 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002620 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2621 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002622 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002623 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2624 // Caller wants the home activity moved with it. To accomplish this,
2625 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002626 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002627 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07002628 if (task.stack == null) {
2629 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2630 + task + " to front. Stack is null");
2631 return;
2632 }
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002633 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
2634 task.getTopActivity() == null ? null : task.getTopActivity().appTimeTracker,
2635 reason);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002636 if (DEBUG_STACK) Slog.d(TAG_STACK,
2637 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002638 }
2639
Craig Mautner967212c2013-04-13 21:10:58 -07002640 ActivityStack getStack(int stackId) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002641 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2642 if (activityContainer != null) {
2643 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002644 }
2645 return null;
2646 }
2647
Craig Mautner967212c2013-04-13 21:10:58 -07002648 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002649 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002650 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2651 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002652 }
2653 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002654 }
2655
Craig Mautner4a1cb222013-12-04 16:14:06 -08002656 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002657 ActivityRecord homeActivity = getHomeActivity();
2658 if (homeActivity != null) {
2659 return homeActivity.appToken;
2660 }
2661 return null;
2662 }
2663
2664 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002665 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002666 }
2667
2668 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002669 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2670 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2671 final TaskRecord task = tasks.get(taskNdx);
2672 if (task.isHomeTask()) {
2673 final ArrayList<ActivityRecord> activities = task.mActivities;
2674 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2675 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002676 if (r.isHomeActivity()
2677 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002678 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002679 }
2680 }
2681 }
2682 }
2683 return null;
2684 }
2685
Todd Kennedyca4d8422015-01-15 15:19:22 -08002686 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002687 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002688 ActivityContainer activityContainer =
2689 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002690 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002691 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2692 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002693 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002694 return activityContainer;
2695 }
2696
Craig Mautner34b73df2014-01-12 21:11:08 -08002697 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002698 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2699 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2700 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002701 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2702 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002703 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002704 }
2705 }
2706
Craig Mautner95da1082014-02-24 17:54:35 -08002707 void deleteActivityContainer(IActivityContainer container) {
2708 ActivityContainer activityContainer = (ActivityContainer)container;
2709 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002710 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2711 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08002712 final int stackId = activityContainer.mStackId;
2713 mActivityContainers.remove(stackId);
2714 mWindowManager.removeStack(stackId);
2715 }
2716 }
2717
Wale Ogunwale60454db2015-01-23 16:05:07 -08002718 void resizeStackLocked(int stackId, Rect bounds) {
2719 final ActivityStack stack = getStack(stackId);
2720 if (stack == null) {
2721 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2722 return;
2723 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002724
2725 final ActivityRecord r = stack.topRunningActivityLocked(null);
Wale Ogunwale0e162182015-02-05 08:48:34 -08002726 if (r != null && !r.task.mResizeable) {
2727 Slog.w(TAG, "resizeStack: top task " + r.task + " not resizeable.");
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002728 return;
2729 }
2730
Wale Ogunwale60454db2015-01-23 16:05:07 -08002731 final Configuration overrideConfig = mWindowManager.resizeStack(stackId, bounds);
2732 if (stack.updateOverrideConfiguration(overrideConfig)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08002733 if (r != null) {
2734 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0);
2735 // And we need to make sure at this point that all other activities
2736 // are made visible with the correct configuration.
2737 ensureActivitiesVisibleLocked(r, 0);
2738 if (!updated) {
2739 resumeTopActivitiesLocked(stack, null, null);
2740 }
2741 }
2742 }
2743 }
2744
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002745 /** Makes sure the input task is in a stack with the specified bounds by either resizing the
2746 * current task stack if it only has one entry, moving the task to a stack that matches the
2747 * bounds, or creating a new stack with the required bounds. Also, makes the task resizeable.*/
2748 void resizeTaskLocked(TaskRecord task, Rect bounds) {
2749 task.mResizeable = true;
2750 final ActivityStack currentStack = task.stack;
2751 if (currentStack.isHomeStack()) {
2752 // Can't move task off the home stack. Sorry!
2753 return;
2754 }
2755
2756 final int matchingStackId = mWindowManager.getStackIdWithBounds(bounds);
2757 if (matchingStackId != -1) {
2758 // There is already a stack with the right bounds!
2759 if (currentStack != null && currentStack.mStackId == matchingStackId) {
2760 // Nothing to do here. Already in the right stack...
2761 return;
2762 }
2763 // Move task to stack with matching bounds.
2764 moveTaskToStackLocked(task.taskId, matchingStackId, true);
2765 return;
2766 }
2767
2768 if (currentStack != null && currentStack.numTasks() == 1) {
2769 // Just resize the current stack since this is the task in it.
2770 resizeStackLocked(currentStack.mStackId, bounds);
2771 return;
2772 }
2773
2774 // Create new stack and move the task to it.
2775 final int displayId = (currentStack != null && currentStack.mDisplayId != -1)
2776 ? currentStack.mDisplayId : Display.DEFAULT_DISPLAY;
2777 ActivityStack newStack = createStackOnDisplay(getNextStackId(), displayId);
2778
2779 if (newStack == null) {
2780 Slog.e(TAG, "resizeTaskLocked: Can't create stack for task=" + task);
2781 return;
2782 }
2783 moveTaskToStackLocked(task.taskId, newStack.mStackId, true);
2784 resizeStackLocked(newStack.mStackId, bounds);
2785 }
2786
Todd Kennedy4900bf92015-01-16 16:05:14 -08002787 ActivityStack createStackOnDisplay(int stackId, int displayId) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002788 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2789 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002790 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002791 }
2792
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002793 ActivityContainer activityContainer = new ActivityContainer(stackId);
2794 mActivityContainers.put(stackId, activityContainer);
Craig Mautnere0a38842013-12-16 16:14:02 -08002795 activityContainer.attachToDisplayLocked(activityDisplay);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002796 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002797 }
2798
2799 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002800 while (true) {
2801 if (++mLastStackId <= HOME_STACK_ID) {
2802 mLastStackId = HOME_STACK_ID + 1;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002803 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002804 if (getStack(mLastStackId) == null) {
2805 break;
2806 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002807 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002808 return mLastStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002809 }
2810
Wale Ogunwale7de05352014-12-12 15:21:33 -08002811 private boolean restoreRecentTaskLocked(TaskRecord task) {
2812 ActivityStack stack = null;
2813 // Determine stack to restore task to.
2814 if (mLeanbackOnlyDevice) {
2815 // There is only one stack for lean back devices.
2816 stack = mHomeStack;
2817 } else {
2818 // Look for the top stack on the home display that isn't the home stack.
2819 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
2820 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
2821 final ActivityStack tmpStack = homeDisplayStacks.get(stackNdx);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07002822 if (!tmpStack.isHomeStack() && tmpStack.mFullscreen) {
Wale Ogunwale7de05352014-12-12 15:21:33 -08002823 stack = tmpStack;
2824 break;
2825 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002826 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002827 }
Wale Ogunwale7de05352014-12-12 15:21:33 -08002828
2829 if (stack == null) {
2830 // We couldn't find a stack to restore the task to. Possible if are restoring recents
2831 // before an application stack is created...Go ahead and create one on the default
2832 // display.
Todd Kennedy4900bf92015-01-16 16:05:14 -08002833 stack = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002834 // Restore home stack to top.
Craig Mautner299f9602015-01-26 09:47:33 -08002835 moveHomeStack(true, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002836 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2837 "Created stack=" + stack + " for recents restoration.");
Wale Ogunwale7de05352014-12-12 15:21:33 -08002838 }
2839
2840 if (stack == null) {
2841 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002842 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2843 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002844 return false;
2845 }
2846
2847 stack.addTask(task, false, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002848 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2849 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002850 final ArrayList<ActivityRecord> activities = task.mActivities;
2851 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2852 final ActivityRecord r = activities.get(activityNdx);
2853 mWindowManager.addAppToken(0, r.appToken, task.taskId, stack.mStackId,
2854 r.info.screenOrientation, r.fullscreen,
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002855 (r.info.flags & ActivityInfo.FLAG_SHOW_FOR_ALL_USERS) != 0,
Wale Ogunwale7de05352014-12-12 15:21:33 -08002856 r.userId, r.info.configChanges, task.voiceSession != null,
2857 r.mLaunchTaskBehind);
2858 }
2859 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002860 }
2861
Craig Mautner299f9602015-01-26 09:47:33 -08002862 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002863 final TaskRecord task = anyTaskForIdLocked(taskId);
2864 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002865 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002866 return;
2867 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002868 final ActivityStack stack = getStack(stackId);
2869 if (stack == null) {
2870 Slog.w(TAG, "moveTaskToStack: no stack for id=" + stackId);
2871 return;
2872 }
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002873 mWindowManager.moveTaskToStack(taskId, stackId, toTop);
2874 if (task.stack != null) {
Wale Ogunwale000957c2015-04-03 08:19:12 -07002875 task.stack.removeTask(task, "moveTaskToStack", false /* notMoving */);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002876 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07002877 stack.addTask(task, toTop, true);
Stefan Kuhne54714cd2015-05-12 13:42:18 -07002878 // The task might have already been running and its visibility needs to be synchronized with
2879 // the visibility of the stack / windows.
2880 stack.ensureActivitiesVisibleLocked(null, 0);
Craig Mautner05d29032013-05-03 13:40:13 -07002881 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002882 }
2883
Craig Mautnerac6f8432013-07-17 13:24:59 -07002884 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002885 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002886 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2887 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002888 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2889 final ActivityStack stack = stacks.get(stackNdx);
2890 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002891 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002892 continue;
2893 }
2894 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002895 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2896 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002897 continue;
2898 }
2899 final ActivityRecord ar = stack.findTaskLocked(r);
2900 if (ar != null) {
2901 return ar;
2902 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002903 }
2904 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002905 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07002906 return null;
2907 }
2908
2909 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002910 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2911 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002912 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2913 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2914 if (ar != null) {
2915 return ar;
2916 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002917 }
2918 }
2919 return null;
2920 }
2921
Craig Mautner8d341ef2013-03-26 09:03:27 -07002922 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002923 scheduleSleepTimeout();
2924 if (!mGoingToSleep.isHeld()) {
2925 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002926 if (mLaunchingActivity.isHeld()) {
2927 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2928 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002929 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002930 mLaunchingActivity.release();
2931 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002932 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002933 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002934 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002935 }
2936
2937 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002938 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002939
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002940 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002941 final long endTime = System.currentTimeMillis() + timeout;
2942 while (true) {
2943 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002944 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2945 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002946 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2947 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2948 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002949 }
2950 if (cantShutdown) {
2951 long timeRemaining = endTime - System.currentTimeMillis();
2952 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002953 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002954 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002955 } catch (InterruptedException e) {
2956 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002957 } else {
2958 Slog.w(TAG, "Activity manager shutdown timed out");
2959 timedout = true;
2960 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002961 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002962 } else {
2963 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002964 }
2965 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002966
2967 // Force checkReadyForSleep to complete.
2968 mSleepTimeout = true;
2969 checkReadyForSleepLocked();
2970
Craig Mautner8d341ef2013-03-26 09:03:27 -07002971 return timedout;
2972 }
2973
2974 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002975 removeSleepTimeouts();
2976 if (mGoingToSleep.isHeld()) {
2977 mGoingToSleep.release();
2978 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002979 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2980 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002981 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2982 final ActivityStack stack = stacks.get(stackNdx);
2983 stack.awakeFromSleepingLocked();
2984 if (isFrontStack(stack)) {
2985 resumeTopActivitiesLocked();
2986 }
Craig Mautner5314a402013-09-26 12:40:16 -07002987 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002988 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002989 mGoingToSleepActivities.clear();
2990 }
2991
2992 void activitySleptLocked(ActivityRecord r) {
2993 mGoingToSleepActivities.remove(r);
2994 checkReadyForSleepLocked();
2995 }
2996
2997 void checkReadyForSleepLocked() {
2998 if (!mService.isSleepingOrShuttingDown()) {
2999 // Do not care.
3000 return;
3001 }
3002
3003 if (!mSleepTimeout) {
3004 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003005 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3006 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003007 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3008 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3009 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003010 }
3011
3012 if (mStoppingActivities.size() > 0) {
3013 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003014 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003015 + mStoppingActivities.size() + " activities");
3016 scheduleIdleLocked();
3017 dontSleep = true;
3018 }
3019
3020 if (mGoingToSleepActivities.size() > 0) {
3021 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003022 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003023 + mGoingToSleepActivities.size() + " activities");
3024 dontSleep = true;
3025 }
3026
3027 if (dontSleep) {
3028 return;
3029 }
3030 }
3031
Craig Mautnere0a38842013-12-16 16:14:02 -08003032 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3033 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003034 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3035 stacks.get(stackNdx).goToSleep();
3036 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003037 }
3038
3039 removeSleepTimeouts();
3040
3041 if (mGoingToSleep.isHeld()) {
3042 mGoingToSleep.release();
3043 }
3044 if (mService.mShuttingDown) {
3045 mService.notifyAll();
3046 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003047 }
3048
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003049 boolean reportResumedActivityLocked(ActivityRecord r) {
3050 final ActivityStack stack = r.task.stack;
3051 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003052 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003053 }
3054 if (allResumedActivitiesComplete()) {
3055 ensureActivitiesVisibleLocked(null, 0);
3056 mWindowManager.executeAppTransition();
3057 return true;
3058 }
3059 return false;
3060 }
3061
Craig Mautner8d341ef2013-03-26 09:03:27 -07003062 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003063 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3064 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003065 final int numStacks = stacks.size();
3066 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3067 final ActivityStack stack = stacks.get(stackNdx);
3068 stack.handleAppCrashLocked(app);
3069 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003070 }
3071 }
3072
Jose Lima4b6c6692014-08-12 17:41:12 -07003073 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003074 final ActivityStack stack = r.task.stack;
3075 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003076 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3077 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003078 return false;
3079 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003080 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003081 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3082 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003083
3084 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003085 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003086 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003087 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003088 return true;
3089 }
3090
3091 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003092 if (visible && top.fullscreen) {
3093 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003094 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3095 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3096 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3097 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003098 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003099 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3100 // Only the activity set as currently visible behind should actively reset its
3101 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003102 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3103 "requestVisibleBehind: returning visible=" + visible
3104 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3105 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003106 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003107 }
3108
Jose Lima4b6c6692014-08-12 17:41:12 -07003109 stack.setVisibleBehindActivity(visible ? r : null);
3110 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003111 // Make the activity immediately above r opaque.
3112 final ActivityRecord next = stack.findNextTranslucentActivity(r);
3113 if (next != null) {
3114 mService.convertFromTranslucent(next.appToken);
3115 }
3116 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003117 if (top.app != null && top.app.thread != null) {
3118 // Notify the top app of the change.
3119 try {
3120 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3121 } catch (RemoteException e) {
3122 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003123 }
3124 return true;
3125 }
3126
Craig Mautnerbb742462014-07-07 15:28:55 -07003127 // Called when WindowManager has finished animating the launchingBehind activity to the back.
3128 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
3129 r.mLaunchTaskBehind = false;
3130 final TaskRecord task = r.task;
3131 task.setLastThumbnail(task.stack.screenshotActivities(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003132 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08003133 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07003134 mWindowManager.setAppVisibility(r.appToken, false);
3135 }
3136
3137 void scheduleLaunchTaskBehindComplete(IBinder token) {
3138 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3139 }
3140
Craig Mautnerde4ef022013-04-07 19:01:33 -07003141 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
Craig Mautner580ea812013-04-25 12:58:38 -07003142 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08003143 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3144 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003145 final int topStackNdx = stacks.size() - 1;
3146 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3147 final ActivityStack stack = stacks.get(stackNdx);
Jose Lima729cb232014-05-05 15:59:40 -07003148 stack.ensureActivitiesVisibleLocked(starting, configChanges);
Craig Mautner580ea812013-04-25 12:58:38 -07003149 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003150 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003151 }
3152
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003153 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3154 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3155 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3156 final int topStackNdx = stacks.size() - 1;
3157 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3158 final ActivityStack stack = stacks.get(stackNdx);
3159 stack.clearOtherAppTimeTrackers(except);
3160 }
3161 }
3162 }
3163
Craig Mautner8d341ef2013-03-26 09:03:27 -07003164 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003165 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3166 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003167 final int numStacks = stacks.size();
3168 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3169 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003170 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003171 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003172 }
3173 }
3174
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003175 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3176 // Examine all activities currently running in the process.
3177 TaskRecord firstTask = null;
3178 // Tasks is non-null only if two or more tasks are found.
3179 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003180 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3181 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003182 ActivityRecord r = app.activities.get(i);
3183 // First, if we find an activity that is in the process of being destroyed,
3184 // then we just aren't going to do anything for now; we want things to settle
3185 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003186 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003187 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003188 return;
3189 }
3190 // Don't consider any activies that are currently not in a state where they
3191 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003192 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3193 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003194 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003195 continue;
3196 }
3197 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003198 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003199 + " from " + r);
3200 if (firstTask == null) {
3201 firstTask = r.task;
3202 } else if (firstTask != r.task) {
3203 if (tasks == null) {
3204 tasks = new ArraySet<>();
3205 tasks.add(firstTask);
3206 }
3207 tasks.add(r.task);
3208 }
3209 }
3210 }
3211 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003212 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003213 return;
3214 }
3215 // If we have activities in multiple tasks that are in a position to be destroyed,
3216 // let's iterate through the tasks and release the oldest one.
3217 final int numDisplays = mActivityDisplays.size();
3218 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3219 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3220 // Step through all stacks starting from behind, to hit the oldest things first.
3221 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3222 final ActivityStack stack = stacks.get(stackNdx);
3223 // Try to release activities in this stack; if we manage to, we are done.
3224 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3225 return;
3226 }
3227 }
3228 }
3229 }
3230
Craig Mautner8d341ef2013-03-26 09:03:27 -07003231 boolean switchUserLocked(int userId, UserStartedState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003232 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003233 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003234 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003235
Craig Mautner858d8a62013-04-23 17:08:34 -07003236 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003237 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3238 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003239 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003240 final ActivityStack stack = stacks.get(stackNdx);
3241 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003242 TaskRecord task = stack.topTask();
3243 if (task != null) {
3244 mWindowManager.moveTaskToTop(task.taskId);
3245 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003246 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003247 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003248
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003249 ActivityStack stack = getStack(restoreStackId);
3250 if (stack == null) {
3251 stack = mHomeStack;
3252 }
3253 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003254 if (stack.isOnHomeDisplay()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003255 moveHomeStack(homeInFront, "switchUserOnHomeDisplay");
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003256 TaskRecord task = stack.topTask();
3257 if (task != null) {
3258 mWindowManager.moveTaskToTop(task.taskId);
3259 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003260 } else {
3261 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003262 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003263 }
Craig Mautner93529a42013-10-04 15:03:13 -07003264 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003265 }
3266
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003267 /**
3268 * Add background users to send boot completed events to.
3269 * @param userId The user being started in the background
3270 * @param uss The state object for the user.
3271 */
3272 public void startBackgroundUserLocked(int userId, UserStartedState uss) {
3273 mStartingBackgroundUsers.add(uss);
3274 }
3275
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003276 /** Checks whether the userid is a profile of the current user. */
3277 boolean isCurrentProfileLocked(int userId) {
3278 if (userId == mCurrentUser) return true;
3279 for (int i = 0; i < mService.mCurrentProfileIds.length; i++) {
3280 if (mService.mCurrentProfileIds[i] == userId) return true;
3281 }
3282 return false;
3283 }
3284
Craig Mautnerde4ef022013-04-07 19:01:33 -07003285 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003286 ArrayList<ActivityRecord> stops = null;
3287
3288 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003289 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3290 ActivityRecord s = mStoppingActivities.get(activityNdx);
3291 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003292 if (DEBUG_ALL) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003293 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3294 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003295 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003296 if (s.finishing) {
3297 // If this activity is finishing, it is sitting on top of
3298 // everyone else but we now know it is no longer needed...
3299 // so get rid of it. Otherwise, we need to go through the
3300 // normal flow and hide it once we determine that it is
3301 // hidden by the activities in front of it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003302 if (DEBUG_ALL) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003303 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003304 }
3305 }
Craig Mautner8c14c152015-01-15 17:32:07 -08003306 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003307 if (DEBUG_ALL) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003308 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003309 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003310 }
3311 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003312 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003313 }
3314 }
3315
3316 return stops;
3317 }
3318
Craig Mautnercf910b02013-04-23 11:23:27 -07003319 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003320 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3321 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3322 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3323 final ActivityStack stack = stacks.get(stackNdx);
3324 final ActivityRecord r = stack.topRunningActivityLocked(null);
3325 final ActivityState state = r == null ? DESTROYED : r.state;
3326 if (isFrontStack(stack)) {
3327 if (r == null) Slog.e(TAG,
3328 "validateTop...: null top activity, stack=" + stack);
3329 else {
3330 final ActivityRecord pausing = stack.mPausingActivity;
3331 if (pausing != null && pausing == r) Slog.e(TAG,
3332 "validateTop...: top stack has pausing activity r=" + r
3333 + " state=" + state);
3334 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3335 "validateTop...: activity in front not resumed r=" + r
3336 + " state=" + state);
3337 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003338 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003339 final ActivityRecord resumed = stack.mResumedActivity;
3340 if (resumed != null && resumed == r) Slog.e(TAG,
3341 "validateTop...: back stack has resumed activity r=" + r
3342 + " state=" + state);
3343 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3344 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003345 }
3346 }
3347 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003348 }
3349
Craig Mautnere0570202015-05-13 13:06:11 -07003350 private String lockTaskModeToString() {
3351 switch (mLockTaskModeState) {
3352 case LOCK_TASK_MODE_LOCKED:
3353 return "LOCKED";
3354 case LOCK_TASK_MODE_PINNED:
3355 return "PINNED";
3356 case LOCK_TASK_MODE_NONE:
3357 return "NONE";
3358 default: return "unknown=" + mLockTaskModeState;
3359 }
3360 }
3361
Craig Mautner27084302013-03-25 08:05:25 -07003362 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003363 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003364 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003365 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
3366 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
3367 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003368 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003369 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
3370 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3371 if (packages.size() > 0) {
3372 pw.println(" mLockTaskPackages (userId:packages)=");
3373 for (int i = 0; i < packages.size(); ++i) {
3374 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3375 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3376 }
3377 }
3378 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003379 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003380
Craig Mautner20e72272013-04-01 13:45:53 -07003381 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003382 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003383 }
3384
Dianne Hackborn390517b2013-05-30 15:03:32 -07003385 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3386 boolean needSep, String prefix) {
3387 if (activity != null) {
3388 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3389 if (needSep) {
3390 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003391 }
3392 pw.print(prefix);
3393 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003394 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003395 }
3396 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003397 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003398 }
3399
Craig Mautner8d341ef2013-03-26 09:03:27 -07003400 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3401 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003402 boolean printed = false;
3403 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003404 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3405 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003406 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003407 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003408 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003409 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003410 final ActivityStack stack = stacks.get(stackNdx);
3411 StringBuilder stackHeader = new StringBuilder(128);
3412 stackHeader.append(" Stack #");
3413 stackHeader.append(stack.mStackId);
3414 stackHeader.append(":");
3415 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3416 needSep, stackHeader.toString());
3417 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3418 !dumpAll, false, dumpPackage, true,
3419 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003420
Craig Mautner4a1cb222013-12-04 16:14:06 -08003421 needSep = printed;
3422 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3423 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003424 if (pr) {
3425 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003426 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003427 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003428 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3429 " mResumedActivity: ");
3430 if (pr) {
3431 printed = true;
3432 needSep = false;
3433 }
3434 if (dumpAll) {
3435 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3436 " mLastPausedActivity: ");
3437 if (pr) {
3438 printed = true;
3439 needSep = true;
3440 }
3441 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3442 needSep, " mLastNoHistoryActivity: ");
3443 }
3444 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003445 }
3446 }
3447
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003448 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3449 false, dumpPackage, true, " Activities waiting to finish:", null);
3450 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3451 false, dumpPackage, true, " Activities waiting to stop:", null);
3452 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3453 false, dumpPackage, true, " Activities waiting for another to become visible:",
3454 null);
3455 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3456 false, dumpPackage, true, " Activities waiting to sleep:", null);
3457 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3458 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003459
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003460 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003461 }
3462
Dianne Hackborn390517b2013-05-30 15:03:32 -07003463 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003464 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003465 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003466 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003467 String innerPrefix = null;
3468 String[] args = null;
3469 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003470 for (int i=list.size()-1; i>=0; i--) {
3471 final ActivityRecord r = list.get(i);
3472 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3473 continue;
3474 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003475 if (innerPrefix == null) {
3476 innerPrefix = prefix + " ";
3477 args = new String[0];
3478 }
3479 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003480 final boolean full = !brief && (complete || !r.isInHistory());
3481 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003482 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003483 needNL = false;
3484 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003485 if (header1 != null) {
3486 pw.println(header1);
3487 header1 = null;
3488 }
3489 if (header2 != null) {
3490 pw.println(header2);
3491 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003492 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003493 if (lastTask != r.task) {
3494 lastTask = r.task;
3495 pw.print(prefix);
3496 pw.print(full ? "* " : " ");
3497 pw.println(lastTask);
3498 if (full) {
3499 lastTask.dump(pw, prefix + " ");
3500 } else if (complete) {
3501 // Complete + brief == give a summary. Isn't that obvious?!?
3502 if (lastTask.intent != null) {
3503 pw.print(prefix); pw.print(" ");
3504 pw.println(lastTask.intent.toInsecureStringWithClip());
3505 }
3506 }
3507 }
3508 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3509 pw.print(" #"); pw.print(i); pw.print(": ");
3510 pw.println(r);
3511 if (full) {
3512 r.dump(pw, innerPrefix);
3513 } else if (complete) {
3514 // Complete + brief == give a summary. Isn't that obvious?!?
3515 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3516 if (r.app != null) {
3517 pw.print(innerPrefix); pw.println(r.app);
3518 }
3519 }
3520 if (client && r.app != null && r.app.thread != null) {
3521 // flush anything that is already in the PrintWriter since the thread is going
3522 // to write to the file descriptor directly
3523 pw.flush();
3524 try {
3525 TransferPipe tp = new TransferPipe();
3526 try {
3527 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3528 r.appToken, innerPrefix, args);
3529 // Short timeout, since blocking here can
3530 // deadlock with the application.
3531 tp.go(fd, 2000);
3532 } finally {
3533 tp.kill();
3534 }
3535 } catch (IOException e) {
3536 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3537 } catch (RemoteException e) {
3538 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3539 }
3540 needNL = true;
3541 }
3542 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003543 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003544 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003545
Craig Mautnerf3333272013-04-22 10:55:53 -07003546 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003547 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3548 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003549 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3550 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003551 }
3552
3553 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003554 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003555 }
3556
3557 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003558 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3559 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003560 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3561 }
3562
Craig Mautner05d29032013-05-03 13:40:13 -07003563 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003564 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3565 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3566 }
Craig Mautner05d29032013-05-03 13:40:13 -07003567 }
3568
Craig Mautner0eea92c2013-05-16 13:35:39 -07003569 void removeSleepTimeouts() {
3570 mSleepTimeout = false;
3571 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3572 }
3573
3574 final void scheduleSleepTimeout() {
3575 removeSleepTimeouts();
3576 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3577 }
3578
Craig Mautner4a1cb222013-12-04 16:14:06 -08003579 @Override
3580 public void onDisplayAdded(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003581 Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003582 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3583 }
3584
3585 @Override
3586 public void onDisplayRemoved(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003587 Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003588 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3589 }
3590
3591 @Override
3592 public void onDisplayChanged(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003593 Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003594 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3595 }
3596
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003597 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003598 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003599 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003600 newDisplay = mActivityDisplays.get(displayId) == null;
3601 if (newDisplay) {
3602 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003603 if (activityDisplay.mDisplay == null) {
3604 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3605 return;
3606 }
Craig Mautner4504de52013-12-20 09:06:56 -08003607 mActivityDisplays.put(displayId, activityDisplay);
3608 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003609 }
Craig Mautner4504de52013-12-20 09:06:56 -08003610 if (newDisplay) {
3611 mWindowManager.onDisplayAdded(displayId);
3612 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003613 }
3614
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003615 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003616 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003617 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3618 if (activityDisplay != null) {
3619 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003620 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003621 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003622 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003623 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003624 }
3625 }
3626 mWindowManager.onDisplayRemoved(displayId);
3627 }
3628
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003629 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003630 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003631 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3632 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003633 // TODO: Update the bounds.
3634 }
3635 }
3636 mWindowManager.onDisplayChanged(displayId);
3637 }
3638
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003639 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003640 StackInfo info = new StackInfo();
3641 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3642 info.displayId = Display.DEFAULT_DISPLAY;
3643 info.stackId = stack.mStackId;
3644
3645 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3646 final int numTasks = tasks.size();
3647 int[] taskIds = new int[numTasks];
3648 String[] taskNames = new String[numTasks];
3649 for (int i = 0; i < numTasks; ++i) {
3650 final TaskRecord task = tasks.get(i);
3651 taskIds[i] = task.taskId;
3652 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3653 : task.realActivity != null ? task.realActivity.flattenToString()
3654 : task.getTopActivity() != null ? task.getTopActivity().packageName
3655 : "unknown";
3656 }
3657 info.taskIds = taskIds;
3658 info.taskNames = taskNames;
3659 return info;
3660 }
3661
3662 StackInfo getStackInfoLocked(int stackId) {
3663 ActivityStack stack = getStack(stackId);
3664 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003665 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003666 }
3667 return null;
3668 }
3669
3670 ArrayList<StackInfo> getAllStackInfosLocked() {
3671 ArrayList<StackInfo> list = new ArrayList<StackInfo>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003672 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3673 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003674 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003675 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003676 }
3677 }
3678 return list;
3679 }
3680
Craig Mautner15df08a2015-04-01 12:17:18 -07003681 TaskRecord getLockedTaskLocked() {
3682 final int top = mLockTaskModeTasks.size() - 1;
3683 if (top >= 0) {
3684 return mLockTaskModeTasks.get(top);
3685 }
3686 return null;
3687 }
3688
3689 boolean isLockedTask(TaskRecord task) {
3690 return mLockTaskModeTasks.contains(task);
3691 }
3692
3693 boolean isLastLockedTask(TaskRecord task) {
3694 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3695 }
3696
3697 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003698 if (!mLockTaskModeTasks.remove(task)) {
3699 return;
3700 }
3701 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3702 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003703 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003704 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3705 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003706 final Message lockTaskMsg = Message.obtain();
3707 lockTaskMsg.arg1 = task.userId;
3708 lockTaskMsg.what = LOCK_TASK_END_MSG;
3709 mHandler.sendMessage(lockTaskMsg);
3710 }
3711 }
3712
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003713 void showLockTaskToast() {
3714 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04003715 }
3716
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003717 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3718 if (mLockTaskModeTasks.contains(task)) {
3719 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3720 }
3721 }
3722
Craig Mautner432f64e2015-05-20 14:59:57 -07003723 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3724 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003725 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003726 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003727 final TaskRecord lockedTask = getLockedTaskLocked();
3728 if (lockedTask != null) {
3729 removeLockedTaskLocked(lockedTask);
3730 if (!mLockTaskModeTasks.isEmpty()) {
3731 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003732 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3733 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003734 lockedTask.performClearTaskLocked();
3735 resumeTopActivitiesLocked();
3736 return;
3737 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003738 }
Craig Mautnere0570202015-05-13 13:06:11 -07003739 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3740 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003741 return;
3742 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003743
3744 // Should have already been checked, but do it again.
3745 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003746 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3747 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003748 return;
3749 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003750 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003751 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003752 return;
3753 }
3754
3755 if (mLockTaskModeTasks.isEmpty()) {
3756 // First locktask.
3757 final Message lockTaskMsg = Message.obtain();
3758 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3759 lockTaskMsg.arg1 = task.userId;
3760 lockTaskMsg.what = LOCK_TASK_START_MSG;
3761 lockTaskMsg.arg2 = lockTaskModeState;
3762 mHandler.sendMessage(lockTaskMsg);
3763 }
3764 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003765 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3766 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003767 mLockTaskModeTasks.remove(task);
3768 mLockTaskModeTasks.add(task);
3769
3770 if (task.mLockTaskUid == -1) {
3771 task.mLockTaskUid = task.mCallingUid;
3772 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003773
3774 if (andResume) {
3775 findTaskToMoveToFrontLocked(task, 0, null, reason);
3776 resumeTopActivitiesLocked();
3777 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003778 }
3779
3780 boolean isLockTaskModeViolation(TaskRecord task) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003781 if (getLockedTaskLocked() == task) {
3782 return false;
3783 }
3784 final int lockTaskAuth = task.mLockTaskAuth;
3785 switch (lockTaskAuth) {
3786 case LOCK_TASK_AUTH_DONT_LOCK:
3787 return !mLockTaskModeTasks.isEmpty();
3788 case LOCK_TASK_AUTH_LAUNCHABLE:
3789 case LOCK_TASK_AUTH_WHITELISTED:
3790 return false;
3791 case LOCK_TASK_AUTH_PINNABLE:
3792 // Pinnable tasks can't be launched on top of locktask tasks.
3793 return !mLockTaskModeTasks.isEmpty();
3794 default:
3795 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3796 return true;
3797 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003798 }
3799
Craig Mautner15df08a2015-04-01 12:17:18 -07003800 void onLockTaskPackagesUpdatedLocked() {
3801 boolean didSomething = false;
3802 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3803 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
3804 if (lockedTask.mLockTaskMode != LOCK_TASK_LAUNCH_MODE_IF_WHITELISTED) {
3805 continue;
3806 }
3807 final boolean wasLaunchable = lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE;
3808 lockedTask.setLockTaskAuth();
3809 if (wasLaunchable && lockedTask.mLockTaskAuth != LOCK_TASK_AUTH_LAUNCHABLE) {
3810 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003811 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3812 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003813 removeLockedTaskLocked(lockedTask);
3814 lockedTask.performClearTaskLocked();
3815 didSomething = true;
3816 }
3817 }
3818 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3819 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3820 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3821 final ActivityStack stack = stacks.get(stackNdx);
3822 stack.onLockTaskPackagesUpdatedLocked();
3823 }
3824 }
Craig Mautnere0570202015-05-13 13:06:11 -07003825 final ActivityRecord r = topRunningActivityLocked();
3826 final TaskRecord task = r != null ? r.task : null;
3827 if (mLockTaskModeTasks.isEmpty() && task != null
3828 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3829 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07003830 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
3831 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
3832 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
3833 false);
3834 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07003835 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003836 if (didSomething) {
3837 resumeTopActivitiesLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003838 }
3839 }
3840
Benjamin Franz43261142015-02-11 15:59:44 +00003841 int getLockTaskModeState() {
3842 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003843 }
3844
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003845 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003846
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003847 public ActivityStackSupervisorHandler(Looper looper) {
3848 super(looper);
3849 }
3850
Craig Mautnerf3333272013-04-22 10:55:53 -07003851 void activityIdleInternal(ActivityRecord r) {
3852 synchronized (mService) {
3853 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3854 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003855 }
3856
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003857 @Override
3858 public void handleMessage(Message msg) {
3859 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003860 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003861 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3862 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003863 if (mService.mDidDexOpt) {
3864 mService.mDidDexOpt = false;
3865 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3866 nmsg.obj = msg.obj;
3867 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3868 return;
3869 }
3870 // We don't at this point know if the activity is fullscreen,
3871 // so we need to be conservative and assume it isn't.
3872 activityIdleInternal((ActivityRecord)msg.obj);
3873 } break;
3874 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003875 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003876 activityIdleInternal((ActivityRecord)msg.obj);
3877 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003878 case RESUME_TOP_ACTIVITY_MSG: {
3879 synchronized (mService) {
3880 resumeTopActivitiesLocked();
3881 }
3882 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003883 case SLEEP_TIMEOUT_MSG: {
3884 synchronized (mService) {
3885 if (mService.isSleepingOrShuttingDown()) {
3886 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3887 mSleepTimeout = true;
3888 checkReadyForSleepLocked();
3889 }
3890 }
3891 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003892 case LAUNCH_TIMEOUT_MSG: {
3893 if (mService.mDidDexOpt) {
3894 mService.mDidDexOpt = false;
3895 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3896 return;
3897 }
3898 synchronized (mService) {
3899 if (mLaunchingActivity.isHeld()) {
3900 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3901 if (VALIDATE_WAKE_LOCK_CALLER
3902 && Binder.getCallingUid() != Process.myUid()) {
3903 throw new IllegalStateException("Calling must be system uid");
3904 }
3905 mLaunchingActivity.release();
3906 }
3907 }
3908 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003909 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003910 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003911 } break;
3912 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003913 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003914 } break;
3915 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003916 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003917 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003918 case CONTAINER_CALLBACK_VISIBILITY: {
3919 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003920 final IActivityContainerCallback callback = container.mCallback;
3921 if (callback != null) {
3922 try {
3923 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3924 } catch (RemoteException e) {
3925 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003926 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003927 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003928 case LOCK_TASK_START_MSG: {
3929 // When lock task starts, we disable the status bars.
3930 try {
Jason Monk62515be2014-05-21 16:06:19 -04003931 if (mLockTaskNotify == null) {
3932 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003933 }
Jason Monk62515be2014-05-21 16:06:19 -04003934 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003935 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003936 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003937 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003938 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003939 flags = StatusBarManager.DISABLE_MASK
3940 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07003941 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003942 flags = StatusBarManager.DISABLE_MASK
3943 & (~StatusBarManager.DISABLE_BACK)
3944 & (~StatusBarManager.DISABLE_HOME)
3945 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003946 }
3947 getStatusBarService().disable(flags, mToken,
3948 mService.mContext.getPackageName());
3949 }
3950 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003951 if (getDevicePolicyManager() != null) {
3952 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003953 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003954 }
justinzhang5286d3f2014-05-12 17:06:01 -04003955 } catch (RemoteException ex) {
3956 throw new RuntimeException(ex);
3957 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003958 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003959 case LOCK_TASK_END_MSG: {
3960 // When lock task ends, we enable the status bars.
3961 try {
Jason Monk62515be2014-05-21 16:06:19 -04003962 if (getStatusBarService() != null) {
3963 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3964 mService.mContext.getPackageName());
3965 }
3966 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003967 if (getDevicePolicyManager() != null) {
3968 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3969 msg.arg1);
3970 }
Jason Monk62515be2014-05-21 16:06:19 -04003971 if (mLockTaskNotify == null) {
3972 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3973 }
3974 mLockTaskNotify.show(false);
3975 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003976 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003977 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003978 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003979 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003980 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003981 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003982 new LockPatternUtils(mService.mContext)
3983 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003984 }
3985 } catch (SettingNotFoundException e) {
3986 // No setting, don't lock.
3987 }
justinzhang5286d3f2014-05-12 17:06:01 -04003988 } catch (RemoteException ex) {
3989 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003990 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07003991 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003992 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003993 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003994 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
3995 if (mLockTaskNotify == null) {
3996 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3997 }
3998 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
3999 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004000 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4001 final ActivityContainer container = (ActivityContainer) msg.obj;
4002 final IActivityContainerCallback callback = container.mCallback;
4003 if (callback != null) {
4004 try {
4005 callback.onAllActivitiesComplete(container.asBinder());
4006 } catch (RemoteException e) {
4007 }
4008 }
4009 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004010 case LAUNCH_TASK_BEHIND_COMPLETE: {
4011 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004012 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004013 if (r != null) {
4014 handleLaunchTaskBehindCompleteLocked(r);
4015 }
4016 }
4017 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004018 }
4019 }
4020 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004021
Ying Wangb081a592014-04-22 15:20:16 -07004022 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07004023 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07004024 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004025 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004026 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004027 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004028 ActivityRecord mParentActivity = null;
4029 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004030
Craig Mautnere3a00d72014-04-16 08:31:19 -07004031 boolean mVisible = true;
4032
Craig Mautner4a1cb222013-12-04 16:14:06 -08004033 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004034 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004035
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004036 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4037 final static int CONTAINER_STATE_NO_SURFACE = 1;
4038 final static int CONTAINER_STATE_FINISHING = 2;
4039 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4040
4041 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004042 synchronized (mService) {
4043 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004044 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004045 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004046 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004047 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004048 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004049
Craig Mautnere0a38842013-12-16 16:14:02 -08004050 void attachToDisplayLocked(ActivityDisplay activityDisplay) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004051 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Craig Mautner34b73df2014-01-12 21:11:08 -08004052 + " to display=" + activityDisplay);
Craig Mautnere0a38842013-12-16 16:14:02 -08004053 mActivityDisplay = activityDisplay;
4054 mStack.mDisplayId = activityDisplay.mDisplayId;
4055 mStack.mStacks = activityDisplay.mStacks;
4056
4057 activityDisplay.attachActivities(mStack);
Craig Mautnerdf88d732014-01-27 09:21:32 -08004058 mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004059 }
4060
4061 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004062 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004063 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004064 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4065 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004066 return;
4067 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004068 attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004069 }
4070 }
4071
4072 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004073 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004074 synchronized (mService) {
4075 if (mActivityDisplay != null) {
4076 return mActivityDisplay.mDisplayId;
4077 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004078 }
4079 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004080 }
4081
Jeff Brownca9bc702014-02-11 14:32:56 -08004082 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004083 public int getStackId() {
4084 synchronized (mService) {
4085 return mStackId;
4086 }
4087 }
4088
4089 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004090 public boolean injectEvent(InputEvent event) {
4091 final long origId = Binder.clearCallingIdentity();
4092 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004093 synchronized (mService) {
4094 if (mActivityDisplay != null) {
4095 return mInputManagerInternal.injectInputEvent(event,
4096 mActivityDisplay.mDisplayId,
4097 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4098 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004099 }
4100 return false;
4101 } finally {
4102 Binder.restoreCallingIdentity(origId);
4103 }
4104 }
4105
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004106 @Override
4107 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004108 synchronized (mService) {
4109 if (mContainerState == CONTAINER_STATE_FINISHING) {
4110 return;
4111 }
4112 mContainerState = CONTAINER_STATE_FINISHING;
4113
Craig Mautnerd163e752014-06-13 17:18:47 -07004114 long origId = Binder.clearCallingIdentity();
4115 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004116 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07004117 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004118 } finally {
4119 Binder.restoreCallingIdentity(origId);
4120 }
4121 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004122 }
4123
Craig Mautner60257702014-09-17 15:02:33 -07004124 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004125 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004126 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004127 if (mActivityDisplay != null) {
4128 mActivityDisplay.detachActivitiesLocked(mStack);
4129 mActivityDisplay = null;
4130 mStack.mDisplayId = -1;
4131 mStack.mStacks = null;
Craig Mautnerdf88d732014-01-27 09:21:32 -08004132 mWindowManager.detachStack(mStackId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004133 }
4134 }
4135
4136 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004137 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004138 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnerb9168362015-02-26 20:40:19 -08004139 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004140 Binder.getCallingUid(), mCurrentUser, false,
4141 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004142
Craig Mautnere0a38842013-12-16 16:14:02 -08004143 // TODO: Switch to user app stacks here.
4144 String mimeType = intent.getType();
Craig Mautnerb9168362015-02-26 20:40:19 -08004145 final Uri data = intent.getData();
4146 if (mimeType == null && data != null && "content".equals(data.getScheme())) {
4147 mimeType = mService.getProviderMimeType(data, userId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004148 }
Craig Mautnerb9168362015-02-26 20:40:19 -08004149 checkEmbeddedAllowedInner(userId, intent, mimeType);
4150
4151 intent.addFlags(FORCE_NEW_TASK_FLAGS);
4152 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null,
4153 0, 0, null, null, null, null, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004154 }
4155
4156 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004157 public final int startActivityIntentSender(IIntentSender intentSender)
4158 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004159 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4160
4161 if (!(intentSender instanceof PendingIntentRecord)) {
4162 throw new IllegalArgumentException("Bad PendingIntent object");
4163 }
4164
Craig Mautnerb9168362015-02-26 20:40:19 -08004165 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004166 Binder.getCallingUid(), mCurrentUser, false,
4167 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004168
4169 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4170 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4171 pendingIntent.key.requestResolvedType);
4172
4173 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4174 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4175 }
4176
4177 private void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004178 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004179 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004180 throw new SecurityException(
4181 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4182 }
4183 }
4184
Craig Mautnerdf88d732014-01-27 09:21:32 -08004185 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004186 public IBinder asBinder() {
4187 return this;
4188 }
4189
Craig Mautner4504de52013-12-20 09:06:56 -08004190 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004191 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004192 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004193 }
4194
Craig Mautner4a1cb222013-12-04 16:14:06 -08004195 ActivityStackSupervisor getOuter() {
4196 return ActivityStackSupervisor.this;
4197 }
4198
Craig Mautnerd163e752014-06-13 17:18:47 -07004199 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004200 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004201 }
4202
4203 void getBounds(Point outBounds) {
Craig Mautnerd163e752014-06-13 17:18:47 -07004204 synchronized (mService) {
4205 if (mActivityDisplay != null) {
4206 mActivityDisplay.getBounds(outBounds);
4207 } else {
4208 outBounds.set(0, 0);
4209 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004210 }
4211 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004212
Craig Mautner6985bad2014-04-21 15:22:06 -07004213 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004214 void setVisible(boolean visible) {
4215 if (mVisible != visible) {
4216 mVisible = visible;
4217 if (mCallback != null) {
4218 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4219 0 /* unused */, this).sendToTarget();
4220 }
4221 }
4222 }
4223
Craig Mautner6985bad2014-04-21 15:22:06 -07004224 void setDrawn() {
4225 }
4226
Craig Mautner1b4bf852014-05-26 15:06:32 -07004227 // You can always start a new task on a regular ActivityStack.
4228 boolean isEligibleForNewTasks() {
4229 return true;
4230 }
4231
Craig Mautnerd163e752014-06-13 17:18:47 -07004232 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004233 detachLocked();
4234 deleteActivityContainer(this);
4235 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004236 }
4237
Craig Mautner34b73df2014-01-12 21:11:08 -08004238 @Override
4239 public String toString() {
4240 return mIdString + (mActivityDisplay == null ? "N" : "A");
4241 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004242 }
4243
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004244 private class VirtualActivityContainer extends ActivityContainer {
4245 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004246 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004247
4248 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4249 super(getNextStackId());
4250 mParentActivity = parent;
4251 mCallback = callback;
4252 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004253 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004254 }
4255
4256 @Override
4257 public void setSurface(Surface surface, int width, int height, int density) {
4258 super.setSurface(surface, width, height, density);
4259
4260 synchronized (mService) {
4261 final long origId = Binder.clearCallingIdentity();
4262 try {
4263 setSurfaceLocked(surface, width, height, density);
4264 } finally {
4265 Binder.restoreCallingIdentity(origId);
4266 }
4267 }
4268 }
4269
4270 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4271 if (mContainerState == CONTAINER_STATE_FINISHING) {
4272 return;
4273 }
4274 VirtualActivityDisplay virtualActivityDisplay =
4275 (VirtualActivityDisplay) mActivityDisplay;
4276 if (virtualActivityDisplay == null) {
4277 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004278 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004279 mActivityDisplay = virtualActivityDisplay;
4280 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
4281 attachToDisplayLocked(virtualActivityDisplay);
4282 }
4283
4284 if (mSurface != null) {
4285 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004286 }
4287
Craig Mautner6985bad2014-04-21 15:22:06 -07004288 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004289 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004290 mStack.resumeTopActivityLocked(null);
4291 } else {
4292 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004293 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004294 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004295 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004296 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004297 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004298
Craig Mautnerd163e752014-06-13 17:18:47 -07004299 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004300
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004301 if (DEBUG_STACK) Slog.d(TAG_STACK,
4302 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004303 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004304
Craig Mautner6985bad2014-04-21 15:22:06 -07004305 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004306 boolean isAttachedLocked() {
4307 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004308 }
4309
4310 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004311 void setDrawn() {
4312 synchronized (mService) {
4313 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004314 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004315 }
4316 }
4317
Craig Mautner1b4bf852014-05-26 15:06:32 -07004318 // Never start a new task on an ActivityView if it isn't explicitly specified.
4319 @Override
4320 boolean isEligibleForNewTasks() {
4321 return false;
4322 }
4323
Craig Mautnerd163e752014-06-13 17:18:47 -07004324 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004325 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004326 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4327 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4328 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4329 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4330 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004331 }
4332 }
4333
Craig Mautner4a1cb222013-12-04 16:14:06 -08004334 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4335 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004336 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004337 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004338 int mDisplayId;
4339 Display mDisplay;
4340 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004341
Craig Mautner4a1cb222013-12-04 16:14:06 -08004342 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4343 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08004344 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004345
Jose Lima4b6c6692014-08-12 17:41:12 -07004346 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004347
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004348 ActivityDisplay() {
4349 }
Craig Mautner4504de52013-12-20 09:06:56 -08004350
Craig Mautner1a70a162014-09-13 12:09:31 -07004351 // After instantiation, check that mDisplay is not null before using this. The alternative
4352 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004353 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004354 final Display display = mDisplayManager.getDisplay(displayId);
4355 if (display == null) {
4356 return;
4357 }
4358 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004359 }
4360
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004361 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004362 mDisplay = display;
4363 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004364 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004365 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004366
4367 void attachActivities(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004368 if (DEBUG_STACK) Slog.v(TAG_STACK,
4369 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004370 mStacks.add(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004371 }
4372
Craig Mautnere0a38842013-12-16 16:14:02 -08004373 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004374 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004375 + " from displayId=" + mDisplayId);
4376 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004377 }
4378
4379 void getBounds(Point bounds) {
4380 mDisplay.getDisplayInfo(mDisplayInfo);
4381 bounds.x = mDisplayInfo.appWidth;
4382 bounds.y = mDisplayInfo.appHeight;
4383 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004384
Jose Lima4b6c6692014-08-12 17:41:12 -07004385 void setVisibleBehindActivity(ActivityRecord r) {
4386 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004387 }
4388
Jose Lima4b6c6692014-08-12 17:41:12 -07004389 boolean hasVisibleBehindActivity() {
4390 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004391 }
4392
Craig Mautner34b73df2014-01-12 21:11:08 -08004393 @Override
4394 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004395 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4396 }
4397 }
4398
4399 class VirtualActivityDisplay extends ActivityDisplay {
4400 VirtualDisplay mVirtualDisplay;
4401
Craig Mautner6985bad2014-04-21 15:22:06 -07004402 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004403 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004404 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4405 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4406 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4407 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004408
4409 init(mVirtualDisplay.getDisplay());
4410
4411 mWindowManager.handleDisplayAdded(mDisplayId);
4412 }
4413
4414 void setSurface(Surface surface) {
4415 if (mVirtualDisplay != null) {
4416 mVirtualDisplay.setSurface(surface);
4417 }
4418 }
4419
4420 @Override
4421 void detachActivitiesLocked(ActivityStack stack) {
4422 super.detachActivitiesLocked(stack);
4423 if (mVirtualDisplay != null) {
4424 mVirtualDisplay.release();
4425 mVirtualDisplay = null;
4426 }
4427 }
4428
4429 @Override
4430 public String toString() {
4431 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004432 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004433 }
Jose Lima58e66d62014-05-27 20:00:27 -07004434
4435 private boolean isLeanbackOnlyDevice() {
4436 boolean onLeanbackOnly = false;
4437 try {
4438 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
4439 PackageManager.FEATURE_LEANBACK_ONLY);
4440 } catch (RemoteException e) {
4441 // noop
4442 }
4443
4444 return onLeanbackOnly;
4445 }
Craig Mautner27084302013-03-25 08:05:25 -07004446}