blob: 9e33f2a99607e1c0372f0b4da36b4b3fdc80668f [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;
Benjamin Franz469dd582015-06-09 14:24:36 +010038import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
Craig Mautner15df08a2015-04-01 12:17:18 -070039import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
40import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Craig Mautner8d341ef2013-03-26 09:03:27 -070041
Craig Mautner2420ead2013-04-01 17:13:20 -070042import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070043import android.app.ActivityManager;
Craig Mautnered6649f2013-12-02 14:08:25 -080044import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070045import android.app.ActivityOptions;
46import android.app.AppGlobals;
Craig Mautner4a1cb222013-12-04 16:14:06 -080047import android.app.IActivityContainer;
48import android.app.IActivityContainerCallback;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070049import android.app.IActivityManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070050import android.app.IApplicationThread;
Craig Mautner23ac33b2013-04-01 16:26:35 -070051import android.app.PendingIntent;
Jeff Hao1b012d32014-08-20 10:35:34 -070052import android.app.ProfilerInfo;
Craig Mautner20e72272013-04-01 13:45:53 -070053import android.app.ActivityManager.RunningTaskInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070054import android.app.IActivityManager.WaitResult;
Craig Mautner2420ead2013-04-01 17:13:20 -070055import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -040056import android.app.StatusBarManager;
Jason Monk35c62a42014-06-17 10:24:47 -040057import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070058import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -070059import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -070060import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070061import android.content.Intent;
Craig Mautner23ac33b2013-04-01 16:26:35 -070062import android.content.IntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070063import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070064import android.content.pm.ApplicationInfo;
65import android.content.pm.PackageManager;
66import android.content.pm.ResolveInfo;
67import android.content.res.Configuration;
Craig Mautner4a1cb222013-12-04 16:14:06 -080068import android.graphics.Point;
Wale Ogunwale60454db2015-01-23 16:05:07 -080069import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -080070import android.hardware.display.DisplayManager;
71import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080072import android.hardware.display.DisplayManagerGlobal;
73import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080074import android.hardware.input.InputManager;
75import android.hardware.input.InputManagerInternal;
Craig Mautnerb9168362015-02-26 20:40:19 -080076import android.net.Uri;
Craig Mautner23ac33b2013-04-01 16:26:35 -070077import android.os.Binder;
Craig Mautner8d341ef2013-03-26 09:03:27 -070078import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070079import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070080import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070081import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070082import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070083import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070084import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070085import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070086import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070087import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -040088import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070089import android.os.SystemClock;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -070090import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -070091import android.os.UserHandle;
Dianne Hackborn3d07c942015-03-13 18:02:54 -070092import android.os.WorkSource;
Jason Monk62515be2014-05-21 16:06:19 -040093import android.provider.Settings;
94import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -070095import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070096import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -070097import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -070098import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -080099import android.util.SparseArray;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700100
Craig Mautner4a1cb222013-12-04 16:14:06 -0800101import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800102import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800103import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -0800104import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800105import android.view.Surface;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700106import com.android.internal.app.HeavyWeightSwitcherActivity;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700107import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800108import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700109import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400110import com.android.internal.statusbar.IStatusBarService;
Jason Monke0697792014-08-04 16:28:09 -0400111import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800112import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700113import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700114import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700115
justinzhang5286d3f2014-05-12 17:06:01 -0400116
Craig Mautner8d341ef2013-03-26 09:03:27 -0700117import java.io.FileDescriptor;
118import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700119import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700120import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700121import java.util.Arrays;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700122import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700123import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700124
Craig Mautner4a1cb222013-12-04 16:14:06 -0800125public final class ActivityStackSupervisor implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800126 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700127 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700128 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700129 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700130 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700131 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700132 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700133 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700134 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
135 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700136 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700137 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700138 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
139 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700140 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700141 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800142
Craig Mautner2219a1b2013-03-25 09:44:30 -0700143 public static final int HOME_STACK_ID = 0;
Craig Mautner27084302013-03-25 08:05:25 -0700144
Craig Mautnerf3333272013-04-22 10:55:53 -0700145 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700146 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700147
Craig Mautner0eea92c2013-05-16 13:35:39 -0700148 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700149 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700150
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700151 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700152 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700153
Craig Mautner05d29032013-05-03 13:40:13 -0700154 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
155 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
156 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700157 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700158 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800159 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
160 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
161 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700162 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400163 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
164 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700165 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700166 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700167 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800168
Craig Mautner4504de52013-12-20 09:06:56 -0800169 private final static String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700170
Jason Monk62515be2014-05-21 16:06:19 -0400171 private static final String LOCK_TASK_TAG = "Lock-to-App";
172
justinzhang5286d3f2014-05-12 17:06:01 -0400173 /** Status Bar Service **/
174 private IBinder mToken = new Binder();
175 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400176 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400177
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700178 // For debugging to make sure the caller when acquiring/releasing our
179 // wake lock is the system process.
180 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700181
Craig Mautner27084302013-03-25 08:05:25 -0700182 final ActivityManagerService mService;
183
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800184 private final RecentTasks mRecentTasks;
185
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700186 final ActivityStackSupervisorHandler mHandler;
187
188 /** Short cut */
189 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800190 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700191
Craig Mautner8d341ef2013-03-26 09:03:27 -0700192 /** Identifier counter for all ActivityStacks */
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700193 private int mLastStackId = HOME_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700194
195 /** Task identifier that activities are currently being started in. Incremented each time a
196 * new task is created. */
197 private int mCurTaskId = 0;
198
Craig Mautner2420ead2013-04-01 17:13:20 -0700199 /** The current user */
200 private int mCurrentUser;
201
Craig Mautnere0a38842013-12-16 16:14:02 -0800202 /** The stack containing the launcher app. Assumed to always be attached to
203 * Display.DEFAULT_DISPLAY. */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700204 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700205
Craig Mautnere0a38842013-12-16 16:14:02 -0800206 /** The stack currently receiving input or launching the next activity. */
Craig Mautner29219d92013-04-16 20:19:12 -0700207 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700208
Craig Mautner4a1cb222013-12-04 16:14:06 -0800209 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
210 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800211 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800212 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700213
214 /** List of activities that are waiting for a new activity to become visible before completing
215 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800216 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700217
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700218 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800219 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700220
221 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800222 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700223
Craig Mautnerde4ef022013-04-07 19:01:33 -0700224 /** List of activities that are ready to be stopped, but waiting for the next activity to
225 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800226 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700227
Craig Mautnerf3333272013-04-22 10:55:53 -0700228 /** List of activities that are ready to be finished, but waiting for the previous activity to
229 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800230 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700231
Craig Mautner0eea92c2013-05-16 13:35:39 -0700232 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800233 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700234
Craig Mautnerf3333272013-04-22 10:55:53 -0700235 /** Used on user changes */
Craig Mautner8c14c152015-01-15 17:32:07 -0800236 final ArrayList<UserStartedState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700237
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700238 /** Used to queue up any background users being started */
Craig Mautner8c14c152015-01-15 17:32:07 -0800239 final ArrayList<UserStartedState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700240
Craig Mautnerde4ef022013-04-07 19:01:33 -0700241 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
242 * is being brought in front of us. */
243 boolean mUserLeaving = false;
244
Craig Mautner0eea92c2013-05-16 13:35:39 -0700245 /** Set when we have taken too long waiting to go to sleep. */
246 boolean mSleepTimeout = false;
247
Jose Lima58e66d62014-05-27 20:00:27 -0700248 /** Indicates if we are running on a Leanback-only (TV) device. Only initialized after
249 * setWindowManager is called. **/
250 private boolean mLeanbackOnlyDevice;
251
Craig Mautner0eea92c2013-05-16 13:35:39 -0700252 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700253 * We don't want to allow the device to go to sleep while in the process
254 * of launching an activity. This is primarily to allow alarm intent
255 * receivers to launch an activity and get that to run before the device
256 * goes back to sleep.
257 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700258 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700259
260 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700261 * Set when the system is going to sleep, until we have
262 * successfully paused the current activity and released our wake lock.
263 * At that point the system is allowed to actually sleep.
264 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700265 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700266
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700267 /** Stack id of the front stack when user switched, indexed by userId. */
268 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700269
Craig Mautner4504de52013-12-20 09:06:56 -0800270 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800271 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700272 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800273
274 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700275 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800276
Jeff Brownca9bc702014-02-11 14:32:56 -0800277 InputManagerInternal mInputManagerInternal;
278
Craig Mautner15df08a2015-04-01 12:17:18 -0700279 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
280 * may be finished until there is only one entry left. If this is empty the system is not
281 * in lockTask mode. */
282 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000283 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700284 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
285 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000286 */
287 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400288 /**
289 * Notifies the user when entering/exiting lock-task.
290 */
291 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800292
Craig Mautner15df08a2015-04-01 12:17:18 -0700293 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches = new ArrayList<>();
Craig Mautneree36c772014-07-16 14:56:05 -0700294
Craig Mautner42d04db2014-11-06 12:13:23 -0800295 /** Used to keep resumeTopActivityLocked() from being entered recursively */
296 boolean inResumeTopActivity;
297
Craig Mautneree36c772014-07-16 14:56:05 -0700298 /**
299 * Description of a request to start a new activity, which has been held
300 * due to app switches being disabled.
301 */
302 static class PendingActivityLaunch {
303 final ActivityRecord r;
304 final ActivityRecord sourceRecord;
305 final int startFlags;
306 final ActivityStack stack;
307
308 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
309 int _startFlags, ActivityStack _stack) {
310 r = _r;
311 sourceRecord = _sourceRecord;
312 startFlags = _startFlags;
313 stack = _stack;
314 }
315 }
316
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800317 public ActivityStackSupervisor(ActivityManagerService service, RecentTasks recentTasks) {
Craig Mautner27084302013-03-25 08:05:25 -0700318 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800319 mRecentTasks = recentTasks;
Jeff Brown2c43c332014-06-12 22:38:59 -0700320 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
321 }
322
323 /**
324 * At the time when the constructor runs, the power manager has not yet been
325 * initialized. So we initialize our wakelocks afterwards.
326 */
327 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800328 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700329 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700330 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700331 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700332 }
333
justinzhang5286d3f2014-05-12 17:06:01 -0400334 // This function returns a IStatusBarService. The value is from ServiceManager.
335 // getService and is cached.
336 private IStatusBarService getStatusBarService() {
337 synchronized (mService) {
338 if (mStatusBarService == null) {
339 mStatusBarService = IStatusBarService.Stub.asInterface(
340 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
341 if (mStatusBarService == null) {
342 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
343 }
344 }
345 return mStatusBarService;
346 }
347 }
348
Jason Monk35c62a42014-06-17 10:24:47 -0400349 private IDevicePolicyManager getDevicePolicyManager() {
350 synchronized (mService) {
351 if (mDevicePolicyManager == null) {
352 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
353 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
354 if (mDevicePolicyManager == null) {
355 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
356 }
357 }
358 return mDevicePolicyManager;
359 }
360 }
361
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700362 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800363 synchronized (mService) {
364 mWindowManager = wm;
365
366 mDisplayManager =
367 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
368 mDisplayManager.registerDisplayListener(this, null);
369
370 Display[] displays = mDisplayManager.getDisplays();
371 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
372 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800373 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700374 if (activityDisplay.mDisplay == null) {
375 throw new IllegalStateException("Default Display does not exist");
376 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800377 mActivityDisplays.put(displayId, activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800378 }
379
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700380 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800381 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800382
383 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Jose Lima58e66d62014-05-27 20:00:27 -0700384
385 // Initialize this here, now that we can get a valid reference to PackageManager.
386 mLeanbackOnlyDevice = isLeanbackOnlyDevice();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800387 }
Craig Mautner27084302013-03-25 08:05:25 -0700388 }
389
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200390 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700391 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200392 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700393 }
394
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700395 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800396 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700397 }
398
Craig Mautnerde4ef022013-04-07 19:01:33 -0700399 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800400 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700401 }
402
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800403 /** Top of all visible stacks is/should always be equal to the focused stack.
404 * Use {@link ActivityStack#isStackVisibleLocked} to determine if a specific
405 * stack is visible or not. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700406 boolean isFrontStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700407 if (stack == null) {
408 return false;
409 }
410
Craig Mautnerdf88d732014-01-27 09:21:32 -0800411 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
412 if (parent != null) {
413 stack = parent.task.stack;
414 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800415 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700416 }
417
Craig Mautner299f9602015-01-26 09:47:33 -0800418 void moveHomeStack(boolean toFront, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800419 moveHomeStack(toFront, reason, null);
420 }
421
422 void moveHomeStack(boolean toFront, String reason, ActivityStack lastFocusedStack) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800423 ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautnerde313752015-01-22 14:28:03 -0800424 final int topNdx = stacks.size() - 1;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800425 if (topNdx <= 0) {
426 return;
427 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700428
429 // The home stack should either be at the top or bottom of the stack list.
430 if ((toFront && (stacks.get(topNdx) != mHomeStack))
431 || (!toFront && (stacks.get(0) != mHomeStack))) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700432 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveHomeTask: topStack old="
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700433 + ((lastFocusedStack != null) ? lastFocusedStack : stacks.get(topNdx))
434 + " new=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800435 stacks.remove(mHomeStack);
436 stacks.add(toFront ? topNdx : 0, mHomeStack);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700437 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800438
439 if (lastFocusedStack != null) {
440 mLastFocusedStack = lastFocusedStack;
441 }
442 mFocusedStack = stacks.get(topNdx);
443
Craig Mautnerde313752015-01-22 14:28:03 -0800444 EventLog.writeEvent(EventLogTags.AM_HOME_STACK_MOVED,
445 mCurrentUser, toFront ? 1 : 0, stacks.get(topNdx).getStackId(),
Craig Mautner299f9602015-01-26 09:47:33 -0800446 mFocusedStack == null ? -1 : mFocusedStack.getStackId(), reason);
Craig Mautnerde313752015-01-22 14:28:03 -0800447
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800448 if (mService.mBooting || !mService.mBooted) {
449 final ActivityRecord r = topRunningActivityLocked();
450 if (r != null && r.idle) {
451 checkFinishBootingLocked();
452 }
453 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700454 }
455
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700456 /** Returns true if the focus activity was adjusted to the home stack top activity. */
457 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700458 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
459 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700460 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700461 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700462
Craig Mautner84984fa2014-06-19 11:19:20 -0700463 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700464
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700465 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700466 if (top == null) {
467 return false;
468 }
469 mService.setFocusedActivityLocked(top, reason);
470 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700471 }
472
Craig Mautner299f9602015-01-26 09:47:33 -0800473 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700474 if (!mService.mBooting && !mService.mBooted) {
475 // Not ready yet!
476 return false;
477 }
478
Craig Mautner84984fa2014-06-19 11:19:20 -0700479 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
480 mWindowManager.showRecentApps();
481 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700482 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700483
Craig Mautner84984fa2014-06-19 11:19:20 -0700484 if (prev != null) {
485 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
486 }
487
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700488 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
489 ActivityRecord r = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700490 if (r != null) {
Craig Mautner299f9602015-01-26 09:47:33 -0800491 mService.setFocusedActivityLocked(r, reason);
Craig Mautner05d29032013-05-03 13:40:13 -0700492 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700493 }
Craig Mautner299f9602015-01-26 09:47:33 -0800494 return mService.startHomeActivityLocked(mCurrentUser, reason);
Craig Mautner69ada552013-04-18 13:51:51 -0700495 }
496
Craig Mautner8d341ef2013-03-26 09:03:27 -0700497 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700498 return anyTaskForIdLocked(id, true);
499 }
500
501 /**
502 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
503 * @param id Id of the task we would like returned.
504 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
505 * restore the task from recents to the active list.
506 */
507 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800508 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800509 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800510 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800511 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
512 ActivityStack stack = stacks.get(stackNdx);
513 TaskRecord task = stack.taskForIdLocked(id);
514 if (task != null) {
515 return task;
516 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700517 }
518 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800519
520 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700521 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800522 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800523 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700524 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800525 return null;
526 }
527
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700528 if (!restoreFromRecents) {
529 return task;
530 }
531
Wale Ogunwale7de05352014-12-12 15:21:33 -0800532 if (!restoreRecentTaskLocked(task)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700533 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
534 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800535 return null;
536 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700537 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800538 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700539 }
540
Craig Mautner6170f732013-04-02 13:05:23 -0700541 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800542 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800543 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800544 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800545 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
546 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
547 if (r != null) {
548 return r;
549 }
Craig Mautner6170f732013-04-02 13:05:23 -0700550 }
551 }
552 return null;
553 }
554
Craig Mautneref73ee12014-04-23 11:45:37 -0700555 void setNextTaskId(int taskId) {
556 if (taskId > mCurTaskId) {
557 mCurTaskId = taskId;
558 }
559 }
560
Craig Mautner8d341ef2013-03-26 09:03:27 -0700561 int getNextTaskId() {
562 do {
563 mCurTaskId++;
564 if (mCurTaskId <= 0) {
565 mCurTaskId = 1;
566 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700567 } while (anyTaskForIdLocked(mCurTaskId, false) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700568 return mCurTaskId;
569 }
570
Craig Mautnerde4ef022013-04-07 19:01:33 -0700571 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800572 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700573 if (stack == null) {
574 return null;
575 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700576 ActivityRecord resumedActivity = stack.mResumedActivity;
577 if (resumedActivity == null || resumedActivity.app == null) {
578 resumedActivity = stack.mPausingActivity;
579 if (resumedActivity == null || resumedActivity.app == null) {
580 resumedActivity = stack.topRunningActivityLocked(null);
581 }
582 }
583 return resumedActivity;
584 }
585
Dianne Hackbornff072722014-09-24 10:56:28 -0700586 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700587 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800588 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800589 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
590 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800591 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
592 final ActivityStack stack = stacks.get(stackNdx);
593 if (!isFrontStack(stack)) {
594 continue;
595 }
596 ActivityRecord hr = stack.topRunningActivityLocked(null);
597 if (hr != null) {
598 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
599 && processName.equals(hr.processName)) {
600 try {
George Mount2c92c972014-03-20 09:38:23 -0700601 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800602 didSomething = true;
603 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700604 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800605 Slog.w(TAG, "Exception in new application when starting activity "
606 + hr.intent.getComponent().flattenToShortString(), e);
607 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700608 }
Craig Mautner20e72272013-04-01 13:45:53 -0700609 }
Craig Mautner20e72272013-04-01 13:45:53 -0700610 }
611 }
612 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700613 if (!didSomething) {
614 ensureActivitiesVisibleLocked(null, 0);
615 }
Craig Mautner20e72272013-04-01 13:45:53 -0700616 return didSomething;
617 }
618
619 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800620 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
621 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800622 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
623 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner34b73df2014-01-12 21:11:08 -0800624 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800625 continue;
626 }
627 final ActivityRecord resumedActivity = stack.mResumedActivity;
628 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700629 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800630 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800631 return false;
632 }
Craig Mautner20e72272013-04-01 13:45:53 -0700633 }
634 }
635 return true;
636 }
637
Craig Mautnerde4ef022013-04-07 19:01:33 -0700638 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800639 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
640 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800641 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
642 final ActivityStack stack = stacks.get(stackNdx);
643 if (isFrontStack(stack)) {
644 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700645 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800646 return false;
647 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700648 }
649 }
650 }
651 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700652 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800653 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
654 mLastFocusedStack + " to=" + mFocusedStack);
655 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700656 return true;
657 }
658
659 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800660 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800661 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
662 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800663 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
664 final ActivityStack stack = stacks.get(stackNdx);
665 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800666 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700667 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800668 return false;
669 }
670 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800671 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700672 }
673 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800674 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700675 }
676
Craig Mautner2acc3892013-09-23 10:28:14 -0700677 /**
678 * Pause all activities in either all of the stacks or just the back stacks.
679 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700680 * @return true if any activity was paused as a result of this call.
681 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700682 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700683 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800684 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
685 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800686 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
687 final ActivityStack stack = stacks.get(stackNdx);
688 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700689 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800690 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700691 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
692 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800693 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700694 }
695 }
696 return someActivityPaused;
697 }
698
Craig Mautnerde4ef022013-04-07 19:01:33 -0700699 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700700 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800701 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
702 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800703 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
704 final ActivityStack stack = stacks.get(stackNdx);
705 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700706 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800707 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700708 Slog.d(TAG_STATES,
709 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800710 pausing = false;
711 } else {
712 return false;
713 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700714 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700715 }
716 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700717 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700718 }
719
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700720 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
721 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500722 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800723 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
724 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
725 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
726 final ActivityStack stack = stacks.get(stackNdx);
727 if (stack.mResumedActivity != null &&
728 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700729 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800730 }
731 }
732 }
733 }
734
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700735 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700736 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700737 }
738
739 void sendWaitingVisibleReportLocked(ActivityRecord r) {
740 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700741 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700742 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700743 if (w.who == null) {
744 changed = true;
745 w.timeout = false;
746 if (r != null) {
747 w.who = new ComponentName(r.info.packageName, r.info.name);
748 }
749 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
750 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700751 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700752 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700753 if (changed) {
754 mService.notifyAll();
755 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700756 }
757
758 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
759 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700760 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700761 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700762 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700763 if (w.who == null) {
764 changed = true;
765 w.timeout = timeout;
766 if (r != null) {
767 w.who = new ComponentName(r.info.packageName, r.info.name);
768 }
769 w.thisTime = thisTime;
770 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700771 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700772 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700773 if (changed) {
774 mService.notifyAll();
775 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700776 }
777
Craig Mautner29219d92013-04-16 20:19:12 -0700778 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800779 final ActivityStack focusedStack = mFocusedStack;
Craig Mautner1602ec22013-05-12 10:24:27 -0700780 ActivityRecord r = focusedStack.topRunningActivityLocked(null);
781 if (r != null) {
782 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700783 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700784
Craig Mautner4a1cb222013-12-04 16:14:06 -0800785 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800786 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800787 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
788 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700789 if (stack != focusedStack && isFrontStack(stack)) {
Craig Mautner29219d92013-04-16 20:19:12 -0700790 r = stack.topRunningActivityLocked(null);
791 if (r != null) {
792 return r;
793 }
794 }
795 }
796 return null;
797 }
798
Dianne Hackborn09233282014-04-30 11:33:59 -0700799 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700800 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800801 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
802 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800803 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800804 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800805 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800806 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
807 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700808 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800809 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700810 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700811 }
812 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700813
814 // The lists are already sorted from most recent to oldest. Just pull the most recent off
815 // each list and add it to list. Stop when all lists are empty or maxNum reached.
816 while (maxNum > 0) {
817 long mostRecentActiveTime = Long.MIN_VALUE;
818 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800819 final int numTaskLists = runningTaskLists.size();
820 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
821 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700822 if (!stackTaskList.isEmpty()) {
823 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
824 if (lastActiveTime > mostRecentActiveTime) {
825 mostRecentActiveTime = lastActiveTime;
826 selectedStackList = stackTaskList;
827 }
828 }
829 }
830 if (selectedStackList != null) {
831 list.add(selectedStackList.remove(0));
832 --maxNum;
833 } else {
834 break;
835 }
836 }
Craig Mautner20e72272013-04-01 13:45:53 -0700837 }
838
Craig Mautner23ac33b2013-04-01 16:26:35 -0700839 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700840 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700841 // Collect information about the target of the Intent.
842 ActivityInfo aInfo;
843 try {
844 ResolveInfo rInfo =
845 AppGlobals.getPackageManager().resolveIntent(
846 intent, resolvedType,
847 PackageManager.MATCH_DEFAULT_ONLY
848 | ActivityManagerService.STOCK_PM_FLAGS, userId);
849 aInfo = rInfo != null ? rInfo.activityInfo : null;
850 } catch (RemoteException e) {
851 aInfo = null;
852 }
853
854 if (aInfo != null) {
855 // Store the found target back into the intent, because now that
856 // we have it we never want to do this again. For example, if the
857 // user navigates back to this point in the history, we should
858 // always restart the exact same activity.
859 intent.setComponent(new ComponentName(
860 aInfo.applicationInfo.packageName, aInfo.name));
861
862 // Don't debug things in the system process
863 if ((startFlags&ActivityManager.START_FLAG_DEBUG) != 0) {
864 if (!aInfo.processName.equals("system")) {
865 mService.setDebugApp(aInfo.processName, true, false);
866 }
867 }
868
869 if ((startFlags&ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
870 if (!aInfo.processName.equals("system")) {
871 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
872 }
873 }
874
Jeff Hao1b012d32014-08-20 10:35:34 -0700875 if (profilerInfo != null) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700876 if (!aInfo.processName.equals("system")) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700877 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700878 }
879 }
880 }
881 return aInfo;
882 }
883
Craig Mautner299f9602015-01-26 09:47:33 -0800884 void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
885 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
Dianne Hackborn95465202014-09-15 16:21:55 -0700886 startActivityLocked(null, intent, null, aInfo, null, null, null, null, 0, 0, 0, null,
887 0, 0, 0, null, false, null, null, null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700888 }
889
Craig Mautner23ac33b2013-04-01 16:26:35 -0700890 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700891 String callingPackage, Intent intent, String resolvedType,
892 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700893 IBinder resultTo, String resultWho, int requestCode, int startFlags,
894 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700895 Bundle options, int userId, IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700896 // Refuse possible leaked file descriptors
897 if (intent != null && intent.hasFileDescriptors()) {
898 throw new IllegalArgumentException("File descriptors passed in Intent");
899 }
900 boolean componentSpecified = intent.getComponent() != null;
901
902 // Don't modify the client's object!
903 intent = new Intent(intent);
904
905 // Collect information about the target of the Intent.
Craig Mautner15df08a2015-04-01 12:17:18 -0700906 ActivityInfo aInfo =
907 resolveActivity(intent, resolvedType, startFlags, profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700908
Craig Mautnere0a38842013-12-16 16:14:02 -0800909 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700910 synchronized (mService) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800911 if (container != null && container.mParentActivity != null &&
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700912 container.mParentActivity.state != RESUMED) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800913 // Cannot start a child activity if the parent is not resumed.
914 return ActivityManager.START_CANCELED;
915 }
Dianne Hackborn95465202014-09-15 16:21:55 -0700916 final int realCallingPid = Binder.getCallingPid();
917 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -0700918 int callingPid;
919 if (callingUid >= 0) {
920 callingPid = -1;
921 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700922 callingPid = realCallingPid;
923 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700924 } else {
925 callingPid = callingUid = -1;
926 }
927
Craig Mautnere0a38842013-12-16 16:14:02 -0800928 final ActivityStack stack;
929 if (container == null || container.mStack.isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800930 stack = mFocusedStack;
Craig Mautnere0a38842013-12-16 16:14:02 -0800931 } else {
932 stack = container.mStack;
933 }
Wale Ogunwale60454db2015-01-23 16:05:07 -0800934 stack.mConfigWillChange = config != null && mService.mConfiguration.diff(config) != 0;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700935 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700936 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700937
938 final long origId = Binder.clearCallingIdentity();
939
940 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800941 (aInfo.applicationInfo.privateFlags
942 &ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700943 // This may be a heavy-weight process! Check to see if we already
944 // have another, different heavy-weight process running.
945 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
946 if (mService.mHeavyWeightProcess != null &&
947 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
948 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700949 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700950 if (caller != null) {
951 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
952 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700953 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700954 } else {
955 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -0700956 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -0700957 + intent.toString());
958 ActivityOptions.abort(options);
959 return ActivityManager.START_PERMISSION_DENIED;
960 }
961 }
962
963 IIntentSender target = mService.getIntentSenderLocked(
964 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -0700965 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -0700966 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
967 | PendingIntent.FLAG_ONE_SHOT, null);
968
969 Intent newIntent = new Intent();
970 if (requestCode >= 0) {
971 // Caller is requesting a result.
972 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
973 }
974 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
975 new IntentSender(target));
976 if (mService.mHeavyWeightProcess.activities.size() > 0) {
977 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
978 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
979 hist.packageName);
980 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
981 hist.task.taskId);
982 }
983 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
984 aInfo.packageName);
985 newIntent.setFlags(intent.getFlags());
986 newIntent.setClassName("android",
987 HeavyWeightSwitcherActivity.class.getName());
988 intent = newIntent;
989 resolvedType = null;
990 caller = null;
991 callingUid = Binder.getCallingUid();
992 callingPid = Binder.getCallingPid();
993 componentSpecified = true;
994 try {
995 ResolveInfo rInfo =
996 AppGlobals.getPackageManager().resolveIntent(
997 intent, null,
998 PackageManager.MATCH_DEFAULT_ONLY
999 | ActivityManagerService.STOCK_PM_FLAGS, userId);
1000 aInfo = rInfo != null ? rInfo.activityInfo : null;
1001 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1002 } catch (RemoteException e) {
1003 aInfo = null;
1004 }
1005 }
1006 }
1007 }
1008
Dianne Hackborn91097de2014-04-04 18:02:06 -07001009 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
1010 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -07001011 requestCode, callingPid, callingUid, callingPackage,
1012 realCallingPid, realCallingUid, startFlags, options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001013 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001014
Craig Mautner85c11a82014-07-17 12:38:18 -07001015 Binder.restoreCallingIdentity(origId);
1016
Craig Mautnerde4ef022013-04-07 19:01:33 -07001017 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001018 // If the caller also wants to switch to a new configuration,
1019 // do so now. This allows a clean switch, as we are waiting
1020 // for the current activity to pause (so we will not destroy
1021 // it), and have not yet started the next activity.
1022 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
1023 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -07001024 stack.mConfigWillChange = false;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001025 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautner23ac33b2013-04-01 16:26:35 -07001026 "Updating to new configuration after starting activity.");
1027 mService.updateConfigurationLocked(config, null, false, false);
1028 }
1029
Craig Mautner23ac33b2013-04-01 16:26:35 -07001030 if (outResult != null) {
1031 outResult.result = res;
1032 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001033 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001034 do {
1035 try {
1036 mService.wait();
1037 } catch (InterruptedException e) {
1038 }
1039 } while (!outResult.timeout && outResult.who == null);
1040 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001041 ActivityRecord r = stack.topRunningActivityLocked(null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001042 if (r.nowVisible && r.state == RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001043 outResult.timeout = false;
1044 outResult.who = new ComponentName(r.info.packageName, r.info.name);
1045 outResult.totalTime = 0;
1046 outResult.thisTime = 0;
1047 } else {
1048 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001049 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001050 do {
1051 try {
1052 mService.wait();
1053 } catch (InterruptedException e) {
1054 }
1055 } while (!outResult.timeout && outResult.who == null);
1056 }
1057 }
1058 }
1059
1060 return res;
1061 }
1062 }
1063
1064 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1065 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
1066 Bundle options, int userId) {
1067 if (intents == null) {
1068 throw new NullPointerException("intents is null");
1069 }
1070 if (resolvedTypes == null) {
1071 throw new NullPointerException("resolvedTypes is null");
1072 }
1073 if (intents.length != resolvedTypes.length) {
1074 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1075 }
1076
Craig Mautner23ac33b2013-04-01 16:26:35 -07001077
1078 int callingPid;
1079 if (callingUid >= 0) {
1080 callingPid = -1;
1081 } else if (caller == null) {
1082 callingPid = Binder.getCallingPid();
1083 callingUid = Binder.getCallingUid();
1084 } else {
1085 callingPid = callingUid = -1;
1086 }
1087 final long origId = Binder.clearCallingIdentity();
1088 try {
1089 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001090 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001091 for (int i=0; i<intents.length; i++) {
1092 Intent intent = intents[i];
1093 if (intent == null) {
1094 continue;
1095 }
1096
1097 // Refuse possible leaked file descriptors
1098 if (intent != null && intent.hasFileDescriptors()) {
1099 throw new IllegalArgumentException("File descriptors passed in Intent");
1100 }
1101
1102 boolean componentSpecified = intent.getComponent() != null;
1103
1104 // Don't modify the client's object!
1105 intent = new Intent(intent);
1106
1107 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001108 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001109 // TODO: New, check if this is correct
1110 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1111
1112 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001113 (aInfo.applicationInfo.privateFlags
1114 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001115 throw new IllegalArgumentException(
1116 "FLAG_CANT_SAVE_STATE not supported here");
1117 }
1118
1119 Bundle theseOptions;
1120 if (options != null && i == intents.length-1) {
1121 theseOptions = options;
1122 } else {
1123 theseOptions = null;
1124 }
Craig Mautner6170f732013-04-02 13:05:23 -07001125 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001126 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1127 callingPackage, callingPid, callingUid,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001128 0, theseOptions, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001129 if (res < 0) {
1130 return res;
1131 }
1132
1133 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1134 }
1135 }
1136 } finally {
1137 Binder.restoreCallingIdentity(origId);
1138 }
1139
1140 return ActivityManager.START_SUCCESS;
1141 }
1142
Craig Mautner2420ead2013-04-01 17:13:20 -07001143 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001144 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001145 throws RemoteException {
1146
Craig Mautner2568c3a2015-03-26 14:22:34 -07001147 if (andResume) {
1148 r.startFreezingScreenLocked(app, 0);
1149 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001150
Craig Mautner2568c3a2015-03-26 14:22:34 -07001151 // schedule launch ticks to collect information about slow apps.
1152 r.startLaunchTickingLocked();
1153 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001154
1155 // Have the window manager re-evaluate the orientation of
1156 // the screen based on the new activity order. Note that
1157 // as a result of this, it can call back into the activity
1158 // manager with a new orientation. We don't care about that,
1159 // because the activity is not currently running so we are
1160 // just restarting it anyway.
1161 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001162 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001163 mService.mConfiguration,
1164 r.mayFreezeScreenLocked(app) ? r.appToken : null);
1165 mService.updateConfigurationLocked(config, r, false, false);
1166 }
1167
1168 r.app = app;
1169 app.waitingToKill = null;
1170 r.launchCount++;
1171 r.lastLaunchTime = SystemClock.uptimeMillis();
1172
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001173 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001174
1175 int idx = app.activities.indexOf(r);
1176 if (idx < 0) {
1177 app.activities.add(r);
1178 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001179 mService.updateLruProcessLocked(app, true, null);
1180 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001181
Craig Mautner15df08a2015-04-01 12:17:18 -07001182 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001183 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1184 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001185 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001186 }
1187
1188 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001189 try {
1190 if (app.thread == null) {
1191 throw new RemoteException();
1192 }
1193 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001194 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001195 if (andResume) {
1196 results = r.results;
1197 newIntents = r.newIntents;
1198 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001199 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1200 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1201 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001202 if (andResume) {
1203 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1204 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001205 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001206 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001207 if (r.isHomeActivity() && r.isNotResolverActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001208 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001209 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001210 }
1211 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1212 r.sleeping = false;
1213 r.forceNewConfig = false;
1214 mService.showAskCompatModeDialogLocked(r);
1215 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001216 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001217 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1218 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1219 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001220 final String profileFile = mService.mProfileFile;
1221 if (profileFile != null) {
1222 ParcelFileDescriptor profileFd = mService.mProfileFd;
1223 if (profileFd != null) {
1224 try {
1225 profileFd = profileFd.dup();
1226 } catch (IOException e) {
1227 if (profileFd != null) {
1228 try {
1229 profileFd.close();
1230 } catch (IOException o) {
1231 }
1232 profileFd = null;
1233 }
1234 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001235 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001236
1237 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1238 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001239 }
1240 }
1241 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001242
Craig Mautner2568c3a2015-03-26 14:22:34 -07001243 if (andResume) {
1244 app.hasShownUi = true;
1245 app.pendingUiClean = true;
1246 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001247 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001248 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001249 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwale60454db2015-01-23 16:05:07 -08001250 new Configuration(stack.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001251 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001252 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001253
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001254 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001255 // This may be a heavy-weight process! Note that the package
1256 // manager will ensure that only activity can run in the main
1257 // process of the .apk, which is the only thing that will be
1258 // considered heavy-weight.
1259 if (app.processName.equals(app.info.packageName)) {
1260 if (mService.mHeavyWeightProcess != null
1261 && mService.mHeavyWeightProcess != app) {
1262 Slog.w(TAG, "Starting new heavy weight process " + app
1263 + " when already running "
1264 + mService.mHeavyWeightProcess);
1265 }
1266 mService.mHeavyWeightProcess = app;
1267 Message msg = mService.mHandler.obtainMessage(
1268 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1269 msg.obj = r;
1270 mService.mHandler.sendMessage(msg);
1271 }
1272 }
1273
1274 } catch (RemoteException e) {
1275 if (r.launchFailed) {
1276 // This is the second time we failed -- finish activity
1277 // and give up.
1278 Slog.e(TAG, "Second failure launching "
1279 + r.intent.getComponent().flattenToShortString()
1280 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001281 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001282 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1283 "2nd-crash", false);
1284 return false;
1285 }
1286
1287 // This is the first time we failed -- restart process and
1288 // retry.
1289 app.activities.remove(r);
1290 throw e;
1291 }
1292
1293 r.launchFailed = false;
1294 if (stack.updateLRUListLocked(r)) {
1295 Slog.w(TAG, "Activity " + r
1296 + " being launched, but already in LRU list");
1297 }
1298
1299 if (andResume) {
1300 // As part of the process of launching, ActivityThread also performs
1301 // a resume.
1302 stack.minimalResumeActivityLocked(r);
1303 } else {
1304 // This activity is not starting in the resumed state... which
1305 // should look like we asked it to pause+stop (but remain visible),
1306 // and it has done so and reported back the current icicle and
1307 // other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001308 if (DEBUG_STATES) Slog.v(TAG_STATES,
1309 "Moving to STOPPED: " + r + " (starting in stopped state)");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001310 r.state = STOPPED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001311 r.stopped = true;
1312 }
1313
1314 // Launch the new version setup screen if needed. We do this -after-
1315 // launching the initial activity (that is, home), so that it can have
1316 // a chance to initialize itself while in the background, making the
1317 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001318 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001319 mService.startSetupActivityLocked();
1320 }
1321
Dianne Hackborn465fa392014-09-14 14:21:18 -07001322 // Update any services we are bound to that might care about whether
1323 // their client may have activities.
1324 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1325
Craig Mautner2420ead2013-04-01 17:13:20 -07001326 return true;
1327 }
1328
Craig Mautnere79d42682013-04-01 19:01:53 -07001329 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001330 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001331 // Is this activity's application already running?
1332 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001333 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001334
1335 r.task.stack.setLaunchTime(r);
1336
1337 if (app != null && app.thread != null) {
1338 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001339 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1340 || !"android".equals(r.info.packageName)) {
1341 // Don't add this if it is a platform component that is marked
1342 // to run in multiple processes, because this is actually
1343 // part of the framework so doesn't make sense to track as a
1344 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001345 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1346 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001347 }
George Mount2c92c972014-03-20 09:38:23 -07001348 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001349 return;
1350 } catch (RemoteException e) {
1351 Slog.w(TAG, "Exception when starting activity "
1352 + r.intent.getComponent().flattenToShortString(), e);
1353 }
1354
1355 // If a dead object exception was thrown -- fall through to
1356 // restart the application.
1357 }
1358
1359 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001360 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001361 }
1362
Craig Mautner6170f732013-04-02 13:05:23 -07001363 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001364 Intent intent, String resolvedType, ActivityInfo aInfo,
1365 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1366 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001367 int callingPid, int callingUid, String callingPackage,
1368 int realCallingPid, int realCallingUid, int startFlags, Bundle options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001369 boolean componentSpecified, ActivityRecord[] outActivity, ActivityContainer container,
1370 TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001371 int err = ActivityManager.START_SUCCESS;
1372
1373 ProcessRecord callerApp = null;
1374 if (caller != null) {
1375 callerApp = mService.getRecordForAppLocked(caller);
1376 if (callerApp != null) {
1377 callingPid = callerApp.pid;
1378 callingUid = callerApp.info.uid;
1379 } else {
1380 Slog.w(TAG, "Unable to find app for caller " + caller
1381 + " (pid=" + callingPid + ") when starting: "
1382 + intent.toString());
1383 err = ActivityManager.START_PERMISSION_DENIED;
1384 }
1385 }
1386
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001387 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1388
Craig Mautner6170f732013-04-02 13:05:23 -07001389 if (err == ActivityManager.START_SUCCESS) {
Craig Mautner6170f732013-04-02 13:05:23 -07001390 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001391 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001392 + " on display " + (container == null ? (mFocusedStack == null ?
1393 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1394 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1395 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001396 }
1397
1398 ActivityRecord sourceRecord = null;
1399 ActivityRecord resultRecord = null;
1400 if (resultTo != null) {
1401 sourceRecord = isInAnyStackLocked(resultTo);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001402 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
1403 "Will send result to " + resultTo + " " + sourceRecord);
Craig Mautner6170f732013-04-02 13:05:23 -07001404 if (sourceRecord != null) {
1405 if (requestCode >= 0 && !sourceRecord.finishing) {
1406 resultRecord = sourceRecord;
1407 }
1408 }
1409 }
Craig Mautner6170f732013-04-02 13:05:23 -07001410
Dianne Hackborn91097de2014-04-04 18:02:06 -07001411 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001412
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001413 if ((launchFlags & Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001414 // Transfer the result target from the source activity to the new
1415 // one being started, including any failures.
1416 if (requestCode >= 0) {
1417 ActivityOptions.abort(options);
1418 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1419 }
1420 resultRecord = sourceRecord.resultTo;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001421 if (resultRecord != null && !resultRecord.isInStackLocked()) {
1422 resultRecord = null;
1423 }
Craig Mautner6170f732013-04-02 13:05:23 -07001424 resultWho = sourceRecord.resultWho;
1425 requestCode = sourceRecord.requestCode;
1426 sourceRecord.resultTo = null;
1427 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001428 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001429 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001430 if (sourceRecord.launchedFromUid == callingUid) {
1431 // The new activity is being launched from the same uid as the previous
1432 // activity in the flow, and asking to forward its result back to the
1433 // previous. In this case the activity is serving as a trampoline between
1434 // the two, so we also want to update its launchedFromPackage to be the
1435 // same as the previous activity. Note that this is safe, since we know
1436 // these two packages come from the same uid; the caller could just as
1437 // well have supplied that same package name itself. This specifially
1438 // deals with the case of an intent picker/chooser being launched in the app
1439 // flow to redirect to an activity picked by the user, where we want the final
1440 // activity to consider it to have been launched by the previous app activity.
1441 callingPackage = sourceRecord.launchedFromPackage;
1442 }
Craig Mautner6170f732013-04-02 13:05:23 -07001443 }
1444
1445 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1446 // We couldn't find a class that can handle the given Intent.
1447 // That's the end of that!
1448 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1449 }
1450
1451 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1452 // We couldn't find the specific class specified in the Intent.
1453 // Also the end of the line.
1454 err = ActivityManager.START_CLASS_NOT_FOUND;
1455 }
1456
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001457 if (err == ActivityManager.START_SUCCESS
1458 && !isCurrentProfileLocked(userId)
1459 && (aInfo.flags & FLAG_SHOW_FOR_ALL_USERS) == 0) {
1460 // Trying to launch a background activity that doesn't show for all users.
1461 err = ActivityManager.START_NOT_CURRENT_USER_ACTIVITY;
1462 }
1463
Dianne Hackborn91097de2014-04-04 18:02:06 -07001464 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1465 && sourceRecord.task.voiceSession != null) {
1466 // If this activity is being launched as part of a voice session, we need
1467 // to ensure that it is safe to do so. If the upcoming activity will also
1468 // be part of the voice session, we can only launch it if it has explicitly
1469 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001470 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001471 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1472 try {
Dianne Hackborn95465202014-09-15 16:21:55 -07001473 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1474 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07001475 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1476 }
1477 } catch (RemoteException e) {
1478 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1479 }
1480 }
1481 }
1482
1483 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1484 // If the caller is starting a new voice session, just make sure the target
1485 // is actually allowing it to run this way.
1486 try {
1487 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1488 intent, resolvedType)) {
1489 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1490 }
1491 } catch (RemoteException e) {
1492 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1493 }
1494 }
1495
louis_changcd5d1982014-08-01 10:09:08 +08001496 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1497
Craig Mautner6170f732013-04-02 13:05:23 -07001498 if (err != ActivityManager.START_SUCCESS) {
1499 if (resultRecord != null) {
1500 resultStack.sendActivityResultLocked(-1,
1501 resultRecord, resultWho, requestCode,
1502 Activity.RESULT_CANCELED, null);
1503 }
Craig Mautner6170f732013-04-02 13:05:23 -07001504 ActivityOptions.abort(options);
1505 return err;
1506 }
1507
1508 final int startAnyPerm = mService.checkPermission(
1509 START_ANY_ACTIVITY, callingPid, callingUid);
1510 final int componentPerm = mService.checkComponentPermission(aInfo.permission, callingPid,
1511 callingUid, aInfo.applicationInfo.uid, aInfo.exported);
1512 if (startAnyPerm != PERMISSION_GRANTED && componentPerm != PERMISSION_GRANTED) {
1513 if (resultRecord != null) {
1514 resultStack.sendActivityResultLocked(-1,
1515 resultRecord, resultWho, requestCode,
1516 Activity.RESULT_CANCELED, null);
1517 }
Craig Mautner6170f732013-04-02 13:05:23 -07001518 String msg;
1519 if (!aInfo.exported) {
1520 msg = "Permission Denial: starting " + intent.toString()
1521 + " from " + callerApp + " (pid=" + callingPid
1522 + ", uid=" + callingUid + ")"
1523 + " not exported from uid " + aInfo.applicationInfo.uid;
1524 } else {
1525 msg = "Permission Denial: starting " + intent.toString()
1526 + " from " + callerApp + " (pid=" + callingPid
1527 + ", uid=" + callingUid + ")"
1528 + " requires " + aInfo.permission;
1529 }
1530 Slog.w(TAG, msg);
1531 throw new SecurityException(msg);
1532 }
1533
Ben Gruverdd72c9e2013-08-06 12:34:17 -07001534 boolean abort = !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001535 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001536
Craig Mautner6170f732013-04-02 13:05:23 -07001537 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001538 try {
1539 // The Intent we give to the watcher has the extra data
1540 // stripped off, since it can contain private information.
1541 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001542 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001543 aInfo.applicationInfo.packageName);
1544 } catch (RemoteException e) {
1545 mService.mController = null;
1546 }
Ben Gruver5e207332013-04-03 17:41:37 -07001547 }
Craig Mautner6170f732013-04-02 13:05:23 -07001548
Ben Gruver5e207332013-04-03 17:41:37 -07001549 if (abort) {
1550 if (resultRecord != null) {
1551 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001552 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001553 }
Ben Gruver5e207332013-04-03 17:41:37 -07001554 // We pretend to the caller that it was really started, but
1555 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001556 ActivityOptions.abort(options);
1557 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001558 }
1559
1560 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001561 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Craig Mautner233ceee2014-05-09 17:05:11 -07001562 requestCode, componentSpecified, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001563 if (outActivity != null) {
1564 outActivity[0] = r;
1565 }
1566
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001567 if (r.appTimeTracker == null && sourceRecord != null) {
1568 // If the caller didn't specify an explicit time tracker, we want to continue
1569 // tracking under any it has.
1570 r.appTimeTracker = sourceRecord.appTimeTracker;
1571 }
1572
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001573 final ActivityStack stack = mFocusedStack;
Dianne Hackborn91097de2014-04-04 18:02:06 -07001574 if (voiceSession == null && (stack.mResumedActivity == null
1575 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001576 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1577 realCallingPid, realCallingUid, "Activity start")) {
Craig Mautner6170f732013-04-02 13:05:23 -07001578 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001579 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautneree36c772014-07-16 14:56:05 -07001580 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001581 ActivityOptions.abort(options);
1582 return ActivityManager.START_SWITCHES_CANCELED;
1583 }
1584 }
1585
1586 if (mService.mDidAppSwitch) {
1587 // This is the second allowed switch since we stopped switches,
1588 // so now just generally allow switches. Use case: user presses
1589 // home (switches disabled, switch to home, mDidAppSwitch now true);
1590 // user taps a home icon (coming from home so allowed, we hit here
1591 // and now allow anyone to switch again).
1592 mService.mAppSwitchesAllowedTime = 0;
1593 } else {
1594 mService.mDidAppSwitch = true;
1595 }
1596
Craig Mautneree36c772014-07-16 14:56:05 -07001597 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001598
Dianne Hackborn91097de2014-04-04 18:02:06 -07001599 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001600 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001601
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001602 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001603 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001604 // activity start, but we are not actually doing an activity
1605 // switch... just dismiss the keyguard now, because we
1606 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001607 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001608 }
1609 return err;
1610 }
1611
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001612 ActivityStack computeStackFocus(ActivityRecord r, boolean newTask) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001613 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001614
1615 // On leanback only devices we should keep all activities in the same stack.
1616 if (!mLeanbackOnlyDevice &&
1617 (r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001618
1619 ActivityStack stack;
1620
Wale Ogunwale7d701172015-03-11 15:36:30 -07001621 if (task != null && task.stack != null) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001622 stack = task.stack;
1623 if (stack.isOnHomeDisplay()) {
1624 if (mFocusedStack != stack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001625 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1626 "computeStackFocus: Setting " + "focused stack to r=" + r
1627 + " task=" + task);
Craig Mautnere0a38842013-12-16 16:14:02 -08001628 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001629 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001630 "computeStackFocus: Focused stack already=" + mFocusedStack);
Craig Mautnere0a38842013-12-16 16:14:02 -08001631 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001632 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001633 return stack;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001634 }
1635
Craig Mautnere0a38842013-12-16 16:14:02 -08001636 final ActivityContainer container = r.mInitialActivityContainer;
1637 if (container != null) {
1638 // The first time put it on the desired stack, after this put on task stack.
1639 r.mInitialActivityContainer = null;
1640 return container.mStack;
1641 }
1642
Craig Mautner1b4bf852014-05-26 15:06:32 -07001643 if (mFocusedStack != mHomeStack && (!newTask ||
1644 mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001645 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001646 "computeStackFocus: Have a focused stack=" + mFocusedStack);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001647 return mFocusedStack;
1648 }
1649
Craig Mautnere0a38842013-12-16 16:14:02 -08001650 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1651 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001652 stack = homeDisplayStacks.get(stackNdx);
Craig Mautnere0a38842013-12-16 16:14:02 -08001653 if (!stack.isHomeStack()) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001654 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001655 "computeStackFocus: Setting focused stack=" + stack);
1656 return stack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001657 }
1658 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001659
Craig Mautner4a1cb222013-12-04 16:14:06 -08001660 // Need to create an app stack for this user.
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001661 stack = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001662 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS, "computeStackFocus: New stack r="
1663 + r + " stackId=" + stack.mStackId);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001664 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001665 }
1666 return mHomeStack;
1667 }
1668
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001669 boolean setFocusedStack(ActivityRecord r, String reason) {
1670 if (r == null) {
1671 // Not sure what you are trying to do, but it is not going to work...
1672 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001673 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001674 final TaskRecord task = r.task;
1675 if (task == null || task.stack == null) {
1676 Slog.w(TAG, "Can't set focus stack for r=" + r + " task=" + task);
1677 return false;
1678 }
1679 task.stack.moveToFront(reason);
1680 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001681 }
1682
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001683 final int startActivityUncheckedLocked(final ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001684 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001685 boolean doResume, Bundle options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001686 final Intent intent = r.intent;
1687 final int callingUid = r.launchedFromUid;
1688
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001689 // In some flows in to this function, we retrieve the task record and hold on to it
1690 // without a lock before calling back in to here... so the task at this point may
1691 // not actually be in recents. Check for that, and if it isn't in recents just
1692 // consider it invalid.
1693 if (inTask != null && !inTask.inRecents) {
1694 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1695 inTask = null;
1696 }
1697
Craig Mautnerad400af2014-08-13 16:41:36 -07001698 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001699 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1700 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001701
Craig Mautnera228ae92014-07-09 05:44:55 -07001702 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001703 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001704 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001705 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1706 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1707 "\"singleInstance\" or \"singleTask\"");
1708 launchFlags &=
1709 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1710 } else {
1711 switch (r.info.documentLaunchMode) {
1712 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1713 break;
1714 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1715 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1716 break;
1717 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1718 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1719 break;
1720 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1721 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1722 break;
1723 }
1724 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001725
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001726 final boolean launchTaskBehind = r.mLaunchTaskBehind
1727 && !launchSingleTask && !launchSingleInstance
1728 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001729
Wale Ogunwale7d701172015-03-11 15:36:30 -07001730 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0
1731 && r.resultTo.task.stack != null) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001732 // For whatever reason this activity is being launched into a new
1733 // task... yet the caller has requested a result back. Well, that
1734 // is pretty messed up, so instead immediately send back a cancel
1735 // and let the new task continue launched as normal without a
1736 // dependency on its originator.
1737 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1738 r.resultTo.task.stack.sendActivityResultLocked(-1,
1739 r.resultTo, r.resultWho, r.requestCode,
1740 Activity.RESULT_CANCELED, null);
1741 r.resultTo = null;
1742 }
1743
1744 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1745 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1746 }
1747
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001748 // If we are actually going to launch in to a new task, there are some cases where
1749 // we further want to do multiple task.
1750 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1751 if (launchTaskBehind
1752 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
1753 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1754 }
1755 }
1756
Craig Mautner8849a5e2013-04-02 16:41:03 -07001757 // We'll invoke onUserLeaving before onPause only if the launching
1758 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001759 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001760 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
1761 "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001762
1763 // If the caller has asked not to resume at this point, we make note
1764 // of this in the record so that we can skip it when trying to find
1765 // the top running activity.
1766 if (!doResume) {
1767 r.delayedResume = true;
1768 }
1769
Craig Mautnera254cd72014-05-25 16:47:39 -07001770 ActivityRecord notTop =
1771 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001772
1773 // If the onlyIfNeeded flag is set, then we can do this if the activity
1774 // being launched is the same as the one making the call... or, as
1775 // a special case, if we do not know the caller then we count the
1776 // current top activity as the caller.
1777 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1778 ActivityRecord checkedCaller = sourceRecord;
1779 if (checkedCaller == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001780 checkedCaller = mFocusedStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001781 }
1782 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1783 // Caller is not the same as launcher, so always needed.
1784 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1785 }
1786 }
1787
Craig Mautner8849a5e2013-04-02 16:41:03 -07001788 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001789 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001790
1791 // If the caller is not coming from another activity, but has given us an
1792 // explicit task into which they would like us to launch the new activity,
1793 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001794 if (sourceRecord == null && inTask != null && inTask.stack != null) {
1795 final Intent baseIntent = inTask.getBaseIntent();
1796 final ActivityRecord root = inTask.getRootActivity();
1797 if (baseIntent == null) {
1798 ActivityOptions.abort(options);
1799 throw new IllegalArgumentException("Launching into task without base intent: "
1800 + inTask);
1801 }
1802
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001803 // If this task is empty, then we are adding the first activity -- it
1804 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001805 if (launchSingleInstance || launchSingleTask) {
1806 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
1807 ActivityOptions.abort(options);
1808 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
1809 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001810 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001811 if (root != null) {
1812 ActivityOptions.abort(options);
1813 throw new IllegalArgumentException("Caller with inTask " + inTask
1814 + " has root " + root + " but target is singleInstance/Task");
1815 }
1816 }
1817
1818 // If task is empty, then adopt the interesting intent launch flags in to the
1819 // activity being started.
1820 if (root == null) {
1821 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
1822 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
1823 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
1824 launchFlags = (launchFlags&~flagsOfInterest)
1825 | (baseIntent.getFlags()&flagsOfInterest);
1826 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001827 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07001828 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001829
Dianne Hackborn962d5352014-08-29 16:27:40 -07001830 // If the task is not empty and the caller is asking to start it as the root
1831 // of a new task, then we don't actually want to start this on the task. We
1832 // will bring the task to the front, and possibly give it a new intent.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001833 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07001834 addingToTask = false;
1835
1836 } else {
1837 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001838 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07001839
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001840 reuseTask = inTask;
1841 } else {
1842 inTask = null;
1843 }
1844
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001845 if (inTask == null) {
1846 if (sourceRecord == null) {
1847 // This activity is not being started from another... in this
1848 // case we -always- start a new task.
1849 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
1850 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
1851 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1852 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1853 }
1854 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1855 // The original activity who is starting us is running as a single
1856 // instance... this new activity it is starting must go on its
1857 // own task.
1858 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1859 } else if (launchSingleInstance || launchSingleTask) {
1860 // The activity being started is a single instance... it always
1861 // gets launched into its own task.
1862 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1863 }
1864 }
1865
1866 ActivityInfo newTaskInfo = null;
1867 Intent newTaskIntent = null;
1868 ActivityStack sourceStack;
1869 if (sourceRecord != null) {
1870 if (sourceRecord.finishing) {
1871 // If the source is finishing, we can't further count it as our source. This
1872 // is because the task it is associated with may now be empty and on its way out,
1873 // so we don't want to blindly throw it in to that task. Instead we will take
1874 // the NEW_TASK flow and try to find a task for it. But save the task information
1875 // so it can be used when creating the new task.
1876 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
1877 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
1878 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1879 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1880 newTaskInfo = sourceRecord.info;
1881 newTaskIntent = sourceRecord.task.intent;
1882 }
1883 sourceRecord = null;
1884 sourceStack = null;
1885 } else {
1886 sourceStack = sourceRecord.task.stack;
1887 }
1888 } else {
1889 sourceStack = null;
1890 }
1891
1892 boolean movedHome = false;
1893 ActivityStack targetStack;
1894
1895 intent.setFlags(launchFlags);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001896 final boolean noAnimation = (launchFlags & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001897
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001898 // We may want to try to place the new activity in to an existing task. We always
1899 // do this if the target activity is singleTask or singleInstance; we will also do
1900 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
1901 // us to still place it in a new task: multi task, always doc mode, or being asked to
1902 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07001903 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
1904 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07001905 || launchSingleInstance || launchSingleTask) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001906 // If bring to front is requested, and no result is requested and we have not
1907 // been given an explicit task to launch in to, and
Craig Mautner8849a5e2013-04-02 16:41:03 -07001908 // we can find a task that was started with this same
1909 // component, then instead of launching bring that one to the front.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001910 if (inTask == null && r.resultTo == null) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001911 // See if there is a task to bring to the front. If this is
1912 // a SINGLE_INSTANCE activity, there can be one and only one
1913 // instance of it in the history, and it is always in its own
1914 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07001915 ActivityRecord intentActivity = !launchSingleInstance ?
1916 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001917 if (intentActivity != null) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001918 if (isLockTaskModeViolation(intentActivity.task)) {
Craig Mautner6cd6cec2015-04-01 00:02:12 -07001919 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07001920 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08001921 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1922 }
Craig Mautner29219d92013-04-16 20:19:12 -07001923 if (r.task == null) {
1924 r.task = intentActivity.task;
1925 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001926 if (intentActivity.task.intent == null) {
1927 // This task was started because of movement of
1928 // the activity based on affinity... now that we
1929 // are actually launching it, we can assign the
1930 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07001931 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001932 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001933 targetStack = intentActivity.task.stack;
1934 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001935 // If the target task is not in the front, then we need
1936 // to bring it to the front... except... well, with
1937 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
1938 // to have the same behavior as if a new instance was
1939 // being started, which means not bringing it to the front
1940 // if the caller is not itself in the front.
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001941 final ActivityStack focusStack = getFocusedStack();
1942 ActivityRecord curTop = (focusStack == null)
1943 ? null : focusStack.topRunningNonDelayedActivityLocked(notTop);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001944 boolean movedToFront = false;
Craig Mautner7504d7b2013-09-17 10:50:53 -07001945 if (curTop != null && (curTop.task != intentActivity.task ||
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001946 curTop.task != focusStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001947 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07001948 if (sourceRecord == null || (sourceStack.topActivity() != null &&
1949 sourceStack.topActivity().task == sourceRecord.task)) {
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001950 // We really do want to push this one into the user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07001951 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07001952 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
1953 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001954 movedHome = true;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001955 targetStack.moveTaskToFrontLocked(intentActivity.task, noAnimation,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001956 options, r.appTimeTracker, "bringingFoundTaskToFront");
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001957 movedToFront = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001958 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07001959 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
1960 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07001961 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07001962 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001963 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001964 options = null;
1965 }
1966 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001967 if (!movedToFront) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001968 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Bring to front target: " + targetStack
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001969 + " from " + intentActivity);
1970 targetStack.moveToFront("intentActivityFound");
1971 }
1972
Craig Mautner8849a5e2013-04-02 16:41:03 -07001973 // If the caller has requested that the target task be
1974 // reset, then do so.
1975 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1976 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
1977 }
Craig Mautner15df08a2015-04-01 12:17:18 -07001978 if ((startFlags & ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001979 // We don't need to start a new activity, and
1980 // the client said not to do anything if that
1981 // is the case, so this is it! And for paranoia, make
1982 // sure we have correctly resumed the top activity.
1983 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07001984 resumeTopActivitiesLocked(targetStack, null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001985
1986 // Make sure to notify Keyguard as well if we are not running an app
1987 // transition later.
1988 if (!movedToFront) {
1989 notifyActivityDrawnForKeyguard();
1990 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001991 } else {
1992 ActivityOptions.abort(options);
1993 }
1994 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
1995 }
Craig Mautner15df08a2015-04-01 12:17:18 -07001996 if ((launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001997 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001998 // The caller has requested to completely replace any
1999 // existing task with its new activity. Well that should
2000 // not be too hard...
2001 reuseTask = intentActivity.task;
2002 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07002003 reuseTask.setIntent(r);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002004 } else if ((launchFlags & FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07002005 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002006 // In this situation we want to remove all activities
2007 // from the task up to the one being started. In most
2008 // cases this means we are resetting the task to its
2009 // initial state.
2010 ActivityRecord top =
2011 intentActivity.task.performClearTaskLocked(r, launchFlags);
2012 if (top != null) {
2013 if (top.frontOfTask) {
2014 // Activity aliases may mean we use different
2015 // intents for the top activity, so make sure
2016 // the task now has the identity of the new
2017 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07002018 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002019 }
2020 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
2021 r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002022 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002023 } else {
Wale Ogunwale86920fe2015-03-17 11:36:24 -07002024 // A special case: we need to start the activity because it is not
2025 // currently running, and the caller has asked to clear the current
2026 // task to have this activity at the top.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002027 addingToTask = true;
Wale Ogunwale86920fe2015-03-17 11:36:24 -07002028 // Now pretend like this activity is being started by the top of its
2029 // task, so it is put in the right place.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002030 sourceRecord = intentActivity;
Wale Ogunwale86920fe2015-03-17 11:36:24 -07002031 TaskRecord task = sourceRecord.task;
2032 if (task != null && task.stack == null) {
2033 // Target stack got cleared when we all activities were removed
2034 // above. Go ahead and reset it.
2035 targetStack = computeStackFocus(sourceRecord, false /* newTask */);
2036 targetStack.addTask(
2037 task, !launchTaskBehind /* toTop */, false /* moving */);
2038 }
2039
Craig Mautner8849a5e2013-04-02 16:41:03 -07002040 }
2041 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
2042 // In this case the top activity on the task is the
2043 // same as the one being launched, so we take that
2044 // as a request to bring the task to the foreground.
2045 // If the top activity in the task is the root
2046 // activity, deliver this new intent to it if it
2047 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07002048 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07002049 && intentActivity.realActivity.equals(r.realActivity)) {
2050 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
2051 intentActivity.task);
2052 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07002053 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002054 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002055 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
2056 r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002057 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
2058 // In this case we are launching the root activity
2059 // of the task, but with a different intent. We
2060 // should start a new instance on top.
2061 addingToTask = true;
2062 sourceRecord = intentActivity;
2063 }
2064 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
2065 // In this case an activity is being launched in to an
2066 // existing task, without resetting that task. This
2067 // is typically the situation of launching an activity
2068 // from a notification or shortcut. We want to place
2069 // the new activity on top of the current task.
2070 addingToTask = true;
2071 sourceRecord = intentActivity;
2072 } else if (!intentActivity.task.rootWasReset) {
2073 // In this case we are launching in to an existing task
2074 // that has not yet been started from its front door.
2075 // The current task has been brought to the front.
2076 // Ideally, we'd probably like to place this new task
2077 // at the bottom of its stack, but that's a little hard
2078 // to do with the current organization of the code so
2079 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07002080 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002081 }
2082 if (!addingToTask && reuseTask == null) {
2083 // We didn't do anything... but it was needed (a.k.a., client
2084 // don't use that intent!) And for paranoia, make
2085 // sure we have correctly resumed the top activity.
2086 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002087 targetStack.resumeTopActivityLocked(null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002088 if (!movedToFront) {
2089 // Make sure to notify Keyguard as well if we are not running an app
2090 // transition later.
2091 notifyActivityDrawnForKeyguard();
2092 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002093 } else {
2094 ActivityOptions.abort(options);
2095 }
2096 return ActivityManager.START_TASK_TO_FRONT;
2097 }
2098 }
2099 }
2100 }
2101
2102 //String uri = r.intent.toURI();
2103 //Intent intent2 = new Intent(uri);
2104 //Slog.i(TAG, "Given intent: " + r.intent);
2105 //Slog.i(TAG, "URI is: " + uri);
2106 //Slog.i(TAG, "To intent: " + intent2);
2107
2108 if (r.packageName != null) {
2109 // If the activity being launched is the same as the one currently
2110 // at the top, then we need to check if it should only be launched
2111 // once.
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002112 ActivityStack topStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002113 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002114 if (top != null && r.resultTo == null) {
2115 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2116 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07002117 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07002118 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002119 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
2120 top.task);
2121 // For paranoia, make sure we have correctly
2122 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002123 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002124 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002125 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002126 }
2127 ActivityOptions.abort(options);
2128 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2129 // We don't need to start a new activity, and
2130 // the client said not to do anything if that
2131 // is the case, so this is it!
2132 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2133 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002134 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002135 return ActivityManager.START_DELIVERED_TO_TOP;
2136 }
2137 }
2138 }
2139 }
2140
2141 } else {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002142 if (r.resultTo != null && r.resultTo.task.stack != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002143 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2144 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002145 }
2146 ActivityOptions.abort(options);
2147 return ActivityManager.START_CLASS_NOT_FOUND;
2148 }
2149
2150 boolean newTask = false;
2151 boolean keepCurTransition = false;
2152
Craig Mautnerbb742462014-07-07 15:28:55 -07002153 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002154 sourceRecord.task : null;
2155
Craig Mautner8849a5e2013-04-02 16:41:03 -07002156 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002157 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002158 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002159 newTask = true;
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002160 targetStack = computeStackFocus(r, newTask);
2161 targetStack.moveToFront("startingNewTask");
2162
Craig Mautner8849a5e2013-04-02 16:41:03 -07002163 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002164 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2165 newTaskInfo != null ? newTaskInfo : r.info,
2166 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002167 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2168 taskToAffiliate);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002169 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2170 "Starting new activity " + r + " in new task " + r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002171 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002172 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002173 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002174 if (isLockTaskModeViolation(r.task)) {
2175 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2176 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2177 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002178 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002179 if ((launchFlags &
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002180 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2181 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002182 // Caller wants to appear on home activity, so before starting
2183 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002184 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002185 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002186 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002187 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002188 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002189 if (isLockTaskModeViolation(sourceTask)) {
2190 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2191 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2192 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002193 targetStack = sourceTask.stack;
Craig Mautner299f9602015-01-26 09:47:33 -08002194 targetStack.moveToFront("sourceStackToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002195 final TaskRecord topTask = targetStack.topTask();
2196 if (topTask != sourceTask) {
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002197 targetStack.moveTaskToFrontLocked(sourceTask, noAnimation, options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002198 r.appTimeTracker, "sourceTaskToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002199 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002200 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002201 // In this case, we are adding the activity to an existing
2202 // task, but the caller has asked to clear that task if the
2203 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002204 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002205 keepCurTransition = true;
2206 if (top != null) {
2207 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002208 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002209 // For paranoia, make sure we have correctly
2210 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002211 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002212 if (doResume) {
2213 targetStack.resumeTopActivityLocked(null);
2214 }
2215 ActivityOptions.abort(options);
2216 return ActivityManager.START_DELIVERED_TO_TOP;
2217 }
2218 } else if (!addingToTask &&
2219 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2220 // In this case, we are launching an activity in our own task
2221 // that may already be running somewhere in the history, and
2222 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002223 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002224 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002225 final TaskRecord task = top.task;
2226 task.moveActivityToFrontLocked(top);
2227 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002228 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002229 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002230 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002231 if (doResume) {
2232 targetStack.resumeTopActivityLocked(null);
2233 }
2234 return ActivityManager.START_DELIVERED_TO_TOP;
2235 }
2236 }
2237 // An existing activity is starting this new activity, so we want
2238 // to keep the new one in the same task as the one that is starting
2239 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002240 r.setTask(sourceTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002241 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002242 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002243
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002244 } else if (inTask != null) {
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002245 // The caller is asking that the new activity be started in an explicit
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002246 // task it has provided to us.
2247 if (isLockTaskModeViolation(inTask)) {
2248 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2249 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2250 }
2251 targetStack = inTask.stack;
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002252 targetStack.moveTaskToFrontLocked(inTask, noAnimation, options, r.appTimeTracker,
2253 "inTaskToFront");
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002254
2255 // Check whether we should actually launch the new activity in to the task,
2256 // or just reuse the current activity on top.
2257 ActivityRecord top = inTask.getTopActivity();
2258 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2259 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2260 || launchSingleTop || launchSingleTask) {
2261 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2262 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2263 // We don't need to start a new activity, and
2264 // the client said not to do anything if that
2265 // is the case, so this is it!
2266 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2267 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002268 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002269 return ActivityManager.START_DELIVERED_TO_TOP;
2270 }
2271 }
2272
Dianne Hackborn962d5352014-08-29 16:27:40 -07002273 if (!addingToTask) {
2274 // We don't actually want to have this activity added to the task, so just
2275 // stop here but still tell the caller that we consumed the intent.
2276 ActivityOptions.abort(options);
2277 return ActivityManager.START_TASK_TO_FRONT;
2278 }
2279
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002280 r.setTask(inTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002281 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002282 + " in explicit task " + r.task);
2283
Craig Mautner8849a5e2013-04-02 16:41:03 -07002284 } else {
2285 // This not being started from an existing activity, and not part
2286 // of a new task... just put it in the top task, though these days
2287 // this case should never happen.
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002288 targetStack = computeStackFocus(r, newTask);
Craig Mautner299f9602015-01-26 09:47:33 -08002289 targetStack.moveToFront("addingToTopTask");
Craig Mautner1602ec22013-05-12 10:24:27 -07002290 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002291 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002292 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002293 mWindowManager.moveTaskToTop(r.task.taskId);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002294 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Craig Mautner8849a5e2013-04-02 16:41:03 -07002295 + " in new guessed " + r.task);
2296 }
2297
2298 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002299 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002300
Craig Mautner76e2a762014-06-24 09:05:43 -07002301 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2302 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2303 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002304 if (newTask) {
2305 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2306 }
2307 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002308 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002309 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Craig Mautnerbb742462014-07-07 15:28:55 -07002310 if (!launchTaskBehind) {
2311 // Don't set focus on an activity that's going to the back.
Craig Mautner299f9602015-01-26 09:47:33 -08002312 mService.setFocusedActivityLocked(r, "startedActivity");
Craig Mautnerbb742462014-07-07 15:28:55 -07002313 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002314 return ActivityManager.START_SUCCESS;
2315 }
2316
Craig Mautneree36c772014-07-16 14:56:05 -07002317 final void doPendingActivityLaunchesLocked(boolean doResume) {
2318 while (!mPendingActivityLaunches.isEmpty()) {
2319 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Craig Mautneraa9b0f12014-07-17 10:50:18 -07002320 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002321 doResume && mPendingActivityLaunches.isEmpty(), null, null);
Craig Mautneree36c772014-07-16 14:56:05 -07002322 }
2323 }
2324
Craig Mautner7f13ed32014-07-28 14:00:35 -07002325 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002326 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2327 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002328 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002329 mPendingActivityLaunches.remove(palNdx);
2330 }
2331 }
2332 }
2333
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002334 void setLaunchSource(int uid) {
2335 mLaunchingActivity.setWorkSource(new WorkSource(uid));
2336 }
2337
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002338 void acquireLaunchWakelock() {
2339 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2340 throw new IllegalStateException("Calling must be system uid");
2341 }
2342 mLaunchingActivity.acquire();
2343 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2344 // To be safe, don't allow the wake lock to be held for too long.
2345 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2346 }
2347 }
2348
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002349 /**
2350 * Called when the frontmost task is idle.
2351 * @return the state of mService.mBooting before this was called.
2352 */
2353 private boolean checkFinishBootingLocked() {
2354 final boolean booting = mService.mBooting;
2355 boolean enableScreen = false;
2356 mService.mBooting = false;
2357 if (!mService.mBooted) {
2358 mService.mBooted = true;
2359 enableScreen = true;
2360 }
2361 if (booting || enableScreen) {
2362 mService.postFinishBooting(booting, enableScreen);
2363 }
2364 return booting;
2365 }
2366
Craig Mautnerf3333272013-04-22 10:55:53 -07002367 // Checked.
2368 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2369 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002370 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002371
Craig Mautnerf3333272013-04-22 10:55:53 -07002372 ArrayList<ActivityRecord> stops = null;
2373 ArrayList<ActivityRecord> finishes = null;
2374 ArrayList<UserStartedState> startingUsers = null;
2375 int NS = 0;
2376 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002377 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002378 boolean activityRemoved = false;
2379
Wale Ogunwale7d701172015-03-11 15:36:30 -07002380 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002381 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002382 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
2383 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002384 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2385 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002386 if (fromTimeout) {
2387 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002388 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002389
2390 // This is a hack to semi-deal with a race condition
2391 // in the client where it can be constructed with a
2392 // newer configuration from when we asked it to launch.
2393 // We'll update with whatever configuration it now says
2394 // it used to launch.
2395 if (config != null) {
2396 r.configuration = config;
2397 }
2398
2399 // We are now idle. If someone is waiting for a thumbnail from
2400 // us, we can now deliver.
2401 r.idle = true;
2402
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002403 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Dianne Hackborn9449a612014-10-01 15:53:28 -07002404 if (isFrontStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002405 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002406 }
2407 }
2408
2409 if (allResumedActivitiesIdle()) {
2410 if (r != null) {
2411 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002412 }
2413
2414 if (mLaunchingActivity.isHeld()) {
2415 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2416 if (VALIDATE_WAKE_LOCK_CALLER &&
2417 Binder.getCallingUid() != Process.myUid()) {
2418 throw new IllegalStateException("Calling must be system uid");
2419 }
2420 mLaunchingActivity.release();
2421 }
2422 ensureActivitiesVisibleLocked(null, 0);
Craig Mautnerf3333272013-04-22 10:55:53 -07002423 }
2424
2425 // Atomically retrieve all of the other things to do.
2426 stops = processStoppingActivitiesLocked(true);
2427 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002428 if ((NF = mFinishingActivities.size()) > 0) {
2429 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002430 mFinishingActivities.clear();
2431 }
2432
Craig Mautnerf3333272013-04-22 10:55:53 -07002433 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002434 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07002435 mStartingUsers.clear();
2436 }
2437
Craig Mautnerf3333272013-04-22 10:55:53 -07002438 // Stop any activities that are scheduled to do so but have been
2439 // waiting for the next one to start.
2440 for (int i = 0; i < NS; i++) {
2441 r = stops.get(i);
2442 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002443 if (stack != null) {
2444 if (r.finishing) {
2445 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2446 } else {
2447 stack.stopActivityLocked(r);
2448 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002449 }
2450 }
2451
2452 // Finish any activities that are scheduled to do so but have been
2453 // waiting for the next one to start.
2454 for (int i = 0; i < NF; i++) {
2455 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07002456 final ActivityStack stack = r.task.stack;
2457 if (stack != null) {
2458 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
2459 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002460 }
2461
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002462 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002463 // Complete user switch
2464 if (startingUsers != null) {
2465 for (int i = 0; i < startingUsers.size(); i++) {
2466 mService.finishUserSwitch(startingUsers.get(i));
2467 }
2468 }
2469 // Complete starting up of background users
2470 if (mStartingBackgroundUsers.size() > 0) {
2471 startingUsers = new ArrayList<UserStartedState>(mStartingBackgroundUsers);
2472 mStartingBackgroundUsers.clear();
2473 for (int i = 0; i < startingUsers.size(); i++) {
2474 mService.finishUserBoot(startingUsers.get(i));
2475 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002476 }
2477 }
2478
2479 mService.trimApplications();
2480 //dump();
2481 //mWindowManager.dump();
2482
Craig Mautnerf3333272013-04-22 10:55:53 -07002483 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002484 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002485 }
2486
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002487 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002488 }
2489
Craig Mautner8e569572013-10-11 17:36:59 -07002490 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002491 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002492 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2493 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002494 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2495 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2496 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002497 }
Craig Mautner19091252013-10-05 00:03:53 -07002498 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002499 }
2500
2501 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002502 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2503 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002504 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2505 stacks.get(stackNdx).closeSystemDialogsLocked();
2506 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002507 }
2508 }
2509
Craig Mautner93529a42013-10-04 15:03:13 -07002510 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002511 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002512 }
2513
Craig Mautner8d341ef2013-03-26 09:03:27 -07002514 /**
2515 * @return true if some activity was finished (or would have finished if doit were true).
2516 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07002517 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
2518 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002519 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002520 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2521 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002522 final int numStacks = stacks.size();
2523 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2524 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07002525 if (stack.finishDisabledPackageActivitiesLocked(
2526 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002527 didSomething = true;
2528 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002529 }
2530 }
2531 return didSomething;
2532 }
2533
Dianne Hackborna413dc02013-07-12 12:02:55 -07002534 void updatePreviousProcessLocked(ActivityRecord r) {
2535 // Now that this process has stopped, we may want to consider
2536 // it to be the previous app to try to keep around in case
2537 // the user wants to return to it.
2538
2539 // First, found out what is currently the foreground app, so that
2540 // we don't blow away the previous app if this activity is being
2541 // hosted by the process that is actually still the foreground.
2542 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002543 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2544 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002545 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2546 final ActivityStack stack = stacks.get(stackNdx);
2547 if (isFrontStack(stack)) {
2548 if (stack.mResumedActivity != null) {
2549 fgApp = stack.mResumedActivity.app;
2550 } else if (stack.mPausingActivity != null) {
2551 fgApp = stack.mPausingActivity.app;
2552 }
2553 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002554 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002555 }
2556 }
2557
2558 // Now set this one as the previous process, only if that really
2559 // makes sense to.
2560 if (r.app != null && fgApp != null && r.app != fgApp
2561 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002562 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002563 mService.mPreviousProcess = r.app;
2564 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2565 }
2566 }
2567
Craig Mautner05d29032013-05-03 13:40:13 -07002568 boolean resumeTopActivitiesLocked() {
2569 return resumeTopActivitiesLocked(null, null, null);
2570 }
2571
2572 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2573 Bundle targetOptions) {
2574 if (targetStack == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002575 targetStack = mFocusedStack;
Craig Mautner05d29032013-05-03 13:40:13 -07002576 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002577 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002578 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002579 if (isFrontStack(targetStack)) {
2580 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2581 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002582
Craig Mautnere0a38842013-12-16 16:14:02 -08002583 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2584 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002585 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2586 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002587 if (stack == targetStack) {
2588 // Already started above.
2589 continue;
2590 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002591 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002592 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002593 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002594 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002595 }
Craig Mautner05d29032013-05-03 13:40:13 -07002596 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002597 }
2598
Todd Kennedy539db512014-12-15 09:57:55 -08002599 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002600 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2601 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002602 final int numStacks = stacks.size();
2603 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2604 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08002605 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002606 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002607 }
2608 }
2609
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002610 void finishVoiceTask(IVoiceInteractionSession session) {
2611 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2612 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2613 final int numStacks = stacks.size();
2614 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2615 final ActivityStack stack = stacks.get(stackNdx);
2616 stack.finishVoiceTask(session);
2617 }
2618 }
2619 }
2620
Craig Mautner299f9602015-01-26 09:47:33 -08002621 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002622 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2623 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002624 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002625 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2626 // Caller wants the home activity moved with it. To accomplish this,
2627 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002628 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002629 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07002630 if (task.stack == null) {
2631 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2632 + task + " to front. Stack is null");
2633 return;
2634 }
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002635 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
2636 task.getTopActivity() == null ? null : task.getTopActivity().appTimeTracker,
2637 reason);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002638 if (DEBUG_STACK) Slog.d(TAG_STACK,
2639 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002640 }
2641
Craig Mautner967212c2013-04-13 21:10:58 -07002642 ActivityStack getStack(int stackId) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002643 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2644 if (activityContainer != null) {
2645 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002646 }
2647 return null;
2648 }
2649
Craig Mautner967212c2013-04-13 21:10:58 -07002650 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002651 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002652 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2653 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002654 }
2655 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002656 }
2657
Craig Mautner4a1cb222013-12-04 16:14:06 -08002658 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002659 ActivityRecord homeActivity = getHomeActivity();
2660 if (homeActivity != null) {
2661 return homeActivity.appToken;
2662 }
2663 return null;
2664 }
2665
2666 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002667 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002668 }
2669
2670 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002671 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2672 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2673 final TaskRecord task = tasks.get(taskNdx);
2674 if (task.isHomeTask()) {
2675 final ArrayList<ActivityRecord> activities = task.mActivities;
2676 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2677 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002678 if (r.isHomeActivity()
2679 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002680 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002681 }
2682 }
2683 }
2684 }
2685 return null;
2686 }
2687
Todd Kennedyca4d8422015-01-15 15:19:22 -08002688 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002689 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002690 ActivityContainer activityContainer =
2691 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002692 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002693 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2694 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002695 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002696 return activityContainer;
2697 }
2698
Craig Mautner34b73df2014-01-12 21:11:08 -08002699 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002700 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2701 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2702 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002703 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2704 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002705 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002706 }
2707 }
2708
Craig Mautner95da1082014-02-24 17:54:35 -08002709 void deleteActivityContainer(IActivityContainer container) {
2710 ActivityContainer activityContainer = (ActivityContainer)container;
2711 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002712 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2713 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08002714 final int stackId = activityContainer.mStackId;
2715 mActivityContainers.remove(stackId);
2716 mWindowManager.removeStack(stackId);
2717 }
2718 }
2719
Wale Ogunwale60454db2015-01-23 16:05:07 -08002720 void resizeStackLocked(int stackId, Rect bounds) {
2721 final ActivityStack stack = getStack(stackId);
2722 if (stack == null) {
2723 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2724 return;
2725 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002726
2727 final ActivityRecord r = stack.topRunningActivityLocked(null);
Wale Ogunwale0e162182015-02-05 08:48:34 -08002728 if (r != null && !r.task.mResizeable) {
2729 Slog.w(TAG, "resizeStack: top task " + r.task + " not resizeable.");
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002730 return;
2731 }
2732
Wale Ogunwale60454db2015-01-23 16:05:07 -08002733 final Configuration overrideConfig = mWindowManager.resizeStack(stackId, bounds);
2734 if (stack.updateOverrideConfiguration(overrideConfig)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08002735 if (r != null) {
2736 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0);
2737 // And we need to make sure at this point that all other activities
2738 // are made visible with the correct configuration.
2739 ensureActivitiesVisibleLocked(r, 0);
2740 if (!updated) {
2741 resumeTopActivitiesLocked(stack, null, null);
2742 }
2743 }
2744 }
2745 }
2746
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002747 /** Makes sure the input task is in a stack with the specified bounds by either resizing the
2748 * current task stack if it only has one entry, moving the task to a stack that matches the
2749 * bounds, or creating a new stack with the required bounds. Also, makes the task resizeable.*/
2750 void resizeTaskLocked(TaskRecord task, Rect bounds) {
2751 task.mResizeable = true;
2752 final ActivityStack currentStack = task.stack;
2753 if (currentStack.isHomeStack()) {
2754 // Can't move task off the home stack. Sorry!
2755 return;
2756 }
2757
2758 final int matchingStackId = mWindowManager.getStackIdWithBounds(bounds);
2759 if (matchingStackId != -1) {
2760 // There is already a stack with the right bounds!
2761 if (currentStack != null && currentStack.mStackId == matchingStackId) {
2762 // Nothing to do here. Already in the right stack...
2763 return;
2764 }
2765 // Move task to stack with matching bounds.
2766 moveTaskToStackLocked(task.taskId, matchingStackId, true);
2767 return;
2768 }
2769
2770 if (currentStack != null && currentStack.numTasks() == 1) {
2771 // Just resize the current stack since this is the task in it.
2772 resizeStackLocked(currentStack.mStackId, bounds);
2773 return;
2774 }
2775
2776 // Create new stack and move the task to it.
2777 final int displayId = (currentStack != null && currentStack.mDisplayId != -1)
2778 ? currentStack.mDisplayId : Display.DEFAULT_DISPLAY;
2779 ActivityStack newStack = createStackOnDisplay(getNextStackId(), displayId);
2780
2781 if (newStack == null) {
2782 Slog.e(TAG, "resizeTaskLocked: Can't create stack for task=" + task);
2783 return;
2784 }
2785 moveTaskToStackLocked(task.taskId, newStack.mStackId, true);
2786 resizeStackLocked(newStack.mStackId, bounds);
2787 }
2788
Todd Kennedy4900bf92015-01-16 16:05:14 -08002789 ActivityStack createStackOnDisplay(int stackId, int displayId) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002790 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2791 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002792 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002793 }
2794
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002795 ActivityContainer activityContainer = new ActivityContainer(stackId);
2796 mActivityContainers.put(stackId, activityContainer);
Craig Mautnere0a38842013-12-16 16:14:02 -08002797 activityContainer.attachToDisplayLocked(activityDisplay);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002798 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002799 }
2800
2801 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002802 while (true) {
2803 if (++mLastStackId <= HOME_STACK_ID) {
2804 mLastStackId = HOME_STACK_ID + 1;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002805 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002806 if (getStack(mLastStackId) == null) {
2807 break;
2808 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002809 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002810 return mLastStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002811 }
2812
Wale Ogunwale7de05352014-12-12 15:21:33 -08002813 private boolean restoreRecentTaskLocked(TaskRecord task) {
2814 ActivityStack stack = null;
2815 // Determine stack to restore task to.
2816 if (mLeanbackOnlyDevice) {
2817 // There is only one stack for lean back devices.
2818 stack = mHomeStack;
2819 } else {
2820 // Look for the top stack on the home display that isn't the home stack.
2821 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
2822 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
2823 final ActivityStack tmpStack = homeDisplayStacks.get(stackNdx);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07002824 if (!tmpStack.isHomeStack() && tmpStack.mFullscreen) {
Wale Ogunwale7de05352014-12-12 15:21:33 -08002825 stack = tmpStack;
2826 break;
2827 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002828 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002829 }
Wale Ogunwale7de05352014-12-12 15:21:33 -08002830
2831 if (stack == null) {
2832 // We couldn't find a stack to restore the task to. Possible if are restoring recents
2833 // before an application stack is created...Go ahead and create one on the default
2834 // display.
Todd Kennedy4900bf92015-01-16 16:05:14 -08002835 stack = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002836 // Restore home stack to top.
Craig Mautner299f9602015-01-26 09:47:33 -08002837 moveHomeStack(true, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002838 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2839 "Created stack=" + stack + " for recents restoration.");
Wale Ogunwale7de05352014-12-12 15:21:33 -08002840 }
2841
2842 if (stack == null) {
2843 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002844 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2845 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002846 return false;
2847 }
2848
2849 stack.addTask(task, false, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002850 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2851 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002852 final ArrayList<ActivityRecord> activities = task.mActivities;
2853 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2854 final ActivityRecord r = activities.get(activityNdx);
2855 mWindowManager.addAppToken(0, r.appToken, task.taskId, stack.mStackId,
2856 r.info.screenOrientation, r.fullscreen,
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002857 (r.info.flags & ActivityInfo.FLAG_SHOW_FOR_ALL_USERS) != 0,
Wale Ogunwale7de05352014-12-12 15:21:33 -08002858 r.userId, r.info.configChanges, task.voiceSession != null,
2859 r.mLaunchTaskBehind);
2860 }
2861 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002862 }
2863
Craig Mautner299f9602015-01-26 09:47:33 -08002864 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002865 final TaskRecord task = anyTaskForIdLocked(taskId);
2866 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002867 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002868 return;
2869 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002870 final ActivityStack stack = getStack(stackId);
2871 if (stack == null) {
2872 Slog.w(TAG, "moveTaskToStack: no stack for id=" + stackId);
2873 return;
2874 }
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002875 mWindowManager.moveTaskToStack(taskId, stackId, toTop);
2876 if (task.stack != null) {
Wale Ogunwale000957c2015-04-03 08:19:12 -07002877 task.stack.removeTask(task, "moveTaskToStack", false /* notMoving */);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002878 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07002879 stack.addTask(task, toTop, true);
Stefan Kuhne54714cd2015-05-12 13:42:18 -07002880 // The task might have already been running and its visibility needs to be synchronized with
2881 // the visibility of the stack / windows.
2882 stack.ensureActivitiesVisibleLocked(null, 0);
Craig Mautner05d29032013-05-03 13:40:13 -07002883 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002884 }
2885
Craig Mautnerac6f8432013-07-17 13:24:59 -07002886 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002887 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002888 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2889 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002890 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2891 final ActivityStack stack = stacks.get(stackNdx);
2892 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002893 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002894 continue;
2895 }
2896 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002897 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2898 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002899 continue;
2900 }
2901 final ActivityRecord ar = stack.findTaskLocked(r);
2902 if (ar != null) {
2903 return ar;
2904 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002905 }
2906 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002907 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07002908 return null;
2909 }
2910
2911 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002912 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2913 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002914 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2915 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2916 if (ar != null) {
2917 return ar;
2918 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002919 }
2920 }
2921 return null;
2922 }
2923
Craig Mautner8d341ef2013-03-26 09:03:27 -07002924 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002925 scheduleSleepTimeout();
2926 if (!mGoingToSleep.isHeld()) {
2927 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002928 if (mLaunchingActivity.isHeld()) {
2929 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2930 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002931 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002932 mLaunchingActivity.release();
2933 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002934 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002935 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002936 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002937 }
2938
2939 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002940 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002941
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002942 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002943 final long endTime = System.currentTimeMillis() + timeout;
2944 while (true) {
2945 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002946 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2947 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002948 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2949 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2950 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002951 }
2952 if (cantShutdown) {
2953 long timeRemaining = endTime - System.currentTimeMillis();
2954 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002955 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002956 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002957 } catch (InterruptedException e) {
2958 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002959 } else {
2960 Slog.w(TAG, "Activity manager shutdown timed out");
2961 timedout = true;
2962 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002963 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002964 } else {
2965 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002966 }
2967 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002968
2969 // Force checkReadyForSleep to complete.
2970 mSleepTimeout = true;
2971 checkReadyForSleepLocked();
2972
Craig Mautner8d341ef2013-03-26 09:03:27 -07002973 return timedout;
2974 }
2975
2976 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002977 removeSleepTimeouts();
2978 if (mGoingToSleep.isHeld()) {
2979 mGoingToSleep.release();
2980 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002981 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2982 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002983 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2984 final ActivityStack stack = stacks.get(stackNdx);
2985 stack.awakeFromSleepingLocked();
2986 if (isFrontStack(stack)) {
2987 resumeTopActivitiesLocked();
2988 }
Craig Mautner5314a402013-09-26 12:40:16 -07002989 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002990 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002991 mGoingToSleepActivities.clear();
2992 }
2993
2994 void activitySleptLocked(ActivityRecord r) {
2995 mGoingToSleepActivities.remove(r);
2996 checkReadyForSleepLocked();
2997 }
2998
2999 void checkReadyForSleepLocked() {
3000 if (!mService.isSleepingOrShuttingDown()) {
3001 // Do not care.
3002 return;
3003 }
3004
3005 if (!mSleepTimeout) {
3006 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003007 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3008 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003009 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3010 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3011 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003012 }
3013
3014 if (mStoppingActivities.size() > 0) {
3015 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003016 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003017 + mStoppingActivities.size() + " activities");
3018 scheduleIdleLocked();
3019 dontSleep = true;
3020 }
3021
3022 if (mGoingToSleepActivities.size() > 0) {
3023 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003024 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003025 + mGoingToSleepActivities.size() + " activities");
3026 dontSleep = true;
3027 }
3028
3029 if (dontSleep) {
3030 return;
3031 }
3032 }
3033
Craig Mautnere0a38842013-12-16 16:14:02 -08003034 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3035 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003036 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3037 stacks.get(stackNdx).goToSleep();
3038 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003039 }
3040
3041 removeSleepTimeouts();
3042
3043 if (mGoingToSleep.isHeld()) {
3044 mGoingToSleep.release();
3045 }
3046 if (mService.mShuttingDown) {
3047 mService.notifyAll();
3048 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003049 }
3050
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003051 boolean reportResumedActivityLocked(ActivityRecord r) {
3052 final ActivityStack stack = r.task.stack;
3053 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003054 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003055 }
3056 if (allResumedActivitiesComplete()) {
3057 ensureActivitiesVisibleLocked(null, 0);
3058 mWindowManager.executeAppTransition();
3059 return true;
3060 }
3061 return false;
3062 }
3063
Craig Mautner8d341ef2013-03-26 09:03:27 -07003064 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003065 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3066 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003067 final int numStacks = stacks.size();
3068 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3069 final ActivityStack stack = stacks.get(stackNdx);
3070 stack.handleAppCrashLocked(app);
3071 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003072 }
3073 }
3074
Jose Lima4b6c6692014-08-12 17:41:12 -07003075 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003076 final ActivityStack stack = r.task.stack;
3077 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003078 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3079 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003080 return false;
3081 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003082 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003083 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3084 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003085
3086 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003087 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003088 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003089 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003090 return true;
3091 }
3092
3093 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003094 if (visible && top.fullscreen) {
3095 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003096 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3097 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3098 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3099 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003100 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003101 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3102 // Only the activity set as currently visible behind should actively reset its
3103 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003104 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3105 "requestVisibleBehind: returning visible=" + visible
3106 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3107 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003108 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003109 }
3110
Jose Lima4b6c6692014-08-12 17:41:12 -07003111 stack.setVisibleBehindActivity(visible ? r : null);
3112 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003113 // Make the activity immediately above r opaque.
3114 final ActivityRecord next = stack.findNextTranslucentActivity(r);
3115 if (next != null) {
3116 mService.convertFromTranslucent(next.appToken);
3117 }
3118 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003119 if (top.app != null && top.app.thread != null) {
3120 // Notify the top app of the change.
3121 try {
3122 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3123 } catch (RemoteException e) {
3124 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003125 }
3126 return true;
3127 }
3128
Craig Mautnerbb742462014-07-07 15:28:55 -07003129 // Called when WindowManager has finished animating the launchingBehind activity to the back.
3130 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
3131 r.mLaunchTaskBehind = false;
3132 final TaskRecord task = r.task;
3133 task.setLastThumbnail(task.stack.screenshotActivities(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003134 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08003135 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07003136 mWindowManager.setAppVisibility(r.appToken, false);
3137 }
3138
3139 void scheduleLaunchTaskBehindComplete(IBinder token) {
3140 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3141 }
3142
Craig Mautnerde4ef022013-04-07 19:01:33 -07003143 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
Craig Mautner580ea812013-04-25 12:58:38 -07003144 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08003145 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3146 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003147 final int topStackNdx = stacks.size() - 1;
3148 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3149 final ActivityStack stack = stacks.get(stackNdx);
Jose Lima729cb232014-05-05 15:59:40 -07003150 stack.ensureActivitiesVisibleLocked(starting, configChanges);
Craig Mautner580ea812013-04-25 12:58:38 -07003151 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003152 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003153 }
3154
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003155 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3156 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3157 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3158 final int topStackNdx = stacks.size() - 1;
3159 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3160 final ActivityStack stack = stacks.get(stackNdx);
3161 stack.clearOtherAppTimeTrackers(except);
3162 }
3163 }
3164 }
3165
Craig Mautner8d341ef2013-03-26 09:03:27 -07003166 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003167 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3168 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003169 final int numStacks = stacks.size();
3170 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3171 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003172 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003173 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003174 }
3175 }
3176
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003177 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3178 // Examine all activities currently running in the process.
3179 TaskRecord firstTask = null;
3180 // Tasks is non-null only if two or more tasks are found.
3181 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003182 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3183 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003184 ActivityRecord r = app.activities.get(i);
3185 // First, if we find an activity that is in the process of being destroyed,
3186 // then we just aren't going to do anything for now; we want things to settle
3187 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003188 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003189 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003190 return;
3191 }
3192 // Don't consider any activies that are currently not in a state where they
3193 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003194 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3195 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003196 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003197 continue;
3198 }
3199 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003200 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003201 + " from " + r);
3202 if (firstTask == null) {
3203 firstTask = r.task;
3204 } else if (firstTask != r.task) {
3205 if (tasks == null) {
3206 tasks = new ArraySet<>();
3207 tasks.add(firstTask);
3208 }
3209 tasks.add(r.task);
3210 }
3211 }
3212 }
3213 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003214 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003215 return;
3216 }
3217 // If we have activities in multiple tasks that are in a position to be destroyed,
3218 // let's iterate through the tasks and release the oldest one.
3219 final int numDisplays = mActivityDisplays.size();
3220 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3221 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3222 // Step through all stacks starting from behind, to hit the oldest things first.
3223 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3224 final ActivityStack stack = stacks.get(stackNdx);
3225 // Try to release activities in this stack; if we manage to, we are done.
3226 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3227 return;
3228 }
3229 }
3230 }
3231 }
3232
Craig Mautner8d341ef2013-03-26 09:03:27 -07003233 boolean switchUserLocked(int userId, UserStartedState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003234 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003235 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003236 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003237
Craig Mautner858d8a62013-04-23 17:08:34 -07003238 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003239 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3240 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003241 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003242 final ActivityStack stack = stacks.get(stackNdx);
3243 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003244 TaskRecord task = stack.topTask();
3245 if (task != null) {
3246 mWindowManager.moveTaskToTop(task.taskId);
3247 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003248 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003249 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003250
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003251 ActivityStack stack = getStack(restoreStackId);
3252 if (stack == null) {
3253 stack = mHomeStack;
3254 }
3255 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003256 if (stack.isOnHomeDisplay()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003257 moveHomeStack(homeInFront, "switchUserOnHomeDisplay");
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003258 TaskRecord task = stack.topTask();
3259 if (task != null) {
3260 mWindowManager.moveTaskToTop(task.taskId);
3261 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003262 } else {
3263 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003264 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003265 }
Craig Mautner93529a42013-10-04 15:03:13 -07003266 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003267 }
3268
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003269 /**
3270 * Add background users to send boot completed events to.
3271 * @param userId The user being started in the background
3272 * @param uss The state object for the user.
3273 */
3274 public void startBackgroundUserLocked(int userId, UserStartedState uss) {
3275 mStartingBackgroundUsers.add(uss);
3276 }
3277
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003278 /** Checks whether the userid is a profile of the current user. */
3279 boolean isCurrentProfileLocked(int userId) {
3280 if (userId == mCurrentUser) return true;
3281 for (int i = 0; i < mService.mCurrentProfileIds.length; i++) {
3282 if (mService.mCurrentProfileIds[i] == userId) return true;
3283 }
3284 return false;
3285 }
3286
Craig Mautnerde4ef022013-04-07 19:01:33 -07003287 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003288 ArrayList<ActivityRecord> stops = null;
3289
3290 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003291 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3292 ActivityRecord s = mStoppingActivities.get(activityNdx);
3293 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003294 if (DEBUG_ALL) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003295 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3296 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003297 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003298 if (s.finishing) {
3299 // If this activity is finishing, it is sitting on top of
3300 // everyone else but we now know it is no longer needed...
3301 // so get rid of it. Otherwise, we need to go through the
3302 // normal flow and hide it once we determine that it is
3303 // hidden by the activities in front of it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003304 if (DEBUG_ALL) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003305 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003306 }
3307 }
Craig Mautner8c14c152015-01-15 17:32:07 -08003308 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003309 if (DEBUG_ALL) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003310 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003311 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003312 }
3313 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003314 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003315 }
3316 }
3317
3318 return stops;
3319 }
3320
Craig Mautnercf910b02013-04-23 11:23:27 -07003321 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003322 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3323 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3324 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3325 final ActivityStack stack = stacks.get(stackNdx);
3326 final ActivityRecord r = stack.topRunningActivityLocked(null);
3327 final ActivityState state = r == null ? DESTROYED : r.state;
3328 if (isFrontStack(stack)) {
3329 if (r == null) Slog.e(TAG,
3330 "validateTop...: null top activity, stack=" + stack);
3331 else {
3332 final ActivityRecord pausing = stack.mPausingActivity;
3333 if (pausing != null && pausing == r) Slog.e(TAG,
3334 "validateTop...: top stack has pausing activity r=" + r
3335 + " state=" + state);
3336 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3337 "validateTop...: activity in front not resumed r=" + r
3338 + " state=" + state);
3339 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003340 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003341 final ActivityRecord resumed = stack.mResumedActivity;
3342 if (resumed != null && resumed == r) Slog.e(TAG,
3343 "validateTop...: back stack has resumed activity r=" + r
3344 + " state=" + state);
3345 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3346 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003347 }
3348 }
3349 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003350 }
3351
Craig Mautnere0570202015-05-13 13:06:11 -07003352 private String lockTaskModeToString() {
3353 switch (mLockTaskModeState) {
3354 case LOCK_TASK_MODE_LOCKED:
3355 return "LOCKED";
3356 case LOCK_TASK_MODE_PINNED:
3357 return "PINNED";
3358 case LOCK_TASK_MODE_NONE:
3359 return "NONE";
3360 default: return "unknown=" + mLockTaskModeState;
3361 }
3362 }
3363
Craig Mautner27084302013-03-25 08:05:25 -07003364 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003365 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003366 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003367 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
3368 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
3369 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003370 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003371 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
3372 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3373 if (packages.size() > 0) {
3374 pw.println(" mLockTaskPackages (userId:packages)=");
3375 for (int i = 0; i < packages.size(); ++i) {
3376 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3377 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3378 }
3379 }
3380 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003381 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003382
Craig Mautner20e72272013-04-01 13:45:53 -07003383 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003384 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003385 }
3386
Dianne Hackborn390517b2013-05-30 15:03:32 -07003387 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3388 boolean needSep, String prefix) {
3389 if (activity != null) {
3390 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3391 if (needSep) {
3392 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003393 }
3394 pw.print(prefix);
3395 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003396 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003397 }
3398 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003399 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003400 }
3401
Craig Mautner8d341ef2013-03-26 09:03:27 -07003402 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3403 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003404 boolean printed = false;
3405 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003406 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3407 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003408 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003409 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003410 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003411 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003412 final ActivityStack stack = stacks.get(stackNdx);
3413 StringBuilder stackHeader = new StringBuilder(128);
3414 stackHeader.append(" Stack #");
3415 stackHeader.append(stack.mStackId);
3416 stackHeader.append(":");
3417 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3418 needSep, stackHeader.toString());
3419 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3420 !dumpAll, false, dumpPackage, true,
3421 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003422
Craig Mautner4a1cb222013-12-04 16:14:06 -08003423 needSep = printed;
3424 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3425 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003426 if (pr) {
3427 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003428 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003429 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003430 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3431 " mResumedActivity: ");
3432 if (pr) {
3433 printed = true;
3434 needSep = false;
3435 }
3436 if (dumpAll) {
3437 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3438 " mLastPausedActivity: ");
3439 if (pr) {
3440 printed = true;
3441 needSep = true;
3442 }
3443 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3444 needSep, " mLastNoHistoryActivity: ");
3445 }
3446 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003447 }
3448 }
3449
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003450 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3451 false, dumpPackage, true, " Activities waiting to finish:", null);
3452 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3453 false, dumpPackage, true, " Activities waiting to stop:", null);
3454 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3455 false, dumpPackage, true, " Activities waiting for another to become visible:",
3456 null);
3457 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3458 false, dumpPackage, true, " Activities waiting to sleep:", null);
3459 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3460 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003461
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003462 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003463 }
3464
Dianne Hackborn390517b2013-05-30 15:03:32 -07003465 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003466 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003467 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003468 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003469 String innerPrefix = null;
3470 String[] args = null;
3471 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003472 for (int i=list.size()-1; i>=0; i--) {
3473 final ActivityRecord r = list.get(i);
3474 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3475 continue;
3476 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003477 if (innerPrefix == null) {
3478 innerPrefix = prefix + " ";
3479 args = new String[0];
3480 }
3481 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003482 final boolean full = !brief && (complete || !r.isInHistory());
3483 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003484 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003485 needNL = false;
3486 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003487 if (header1 != null) {
3488 pw.println(header1);
3489 header1 = null;
3490 }
3491 if (header2 != null) {
3492 pw.println(header2);
3493 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003494 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003495 if (lastTask != r.task) {
3496 lastTask = r.task;
3497 pw.print(prefix);
3498 pw.print(full ? "* " : " ");
3499 pw.println(lastTask);
3500 if (full) {
3501 lastTask.dump(pw, prefix + " ");
3502 } else if (complete) {
3503 // Complete + brief == give a summary. Isn't that obvious?!?
3504 if (lastTask.intent != null) {
3505 pw.print(prefix); pw.print(" ");
3506 pw.println(lastTask.intent.toInsecureStringWithClip());
3507 }
3508 }
3509 }
3510 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3511 pw.print(" #"); pw.print(i); pw.print(": ");
3512 pw.println(r);
3513 if (full) {
3514 r.dump(pw, innerPrefix);
3515 } else if (complete) {
3516 // Complete + brief == give a summary. Isn't that obvious?!?
3517 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3518 if (r.app != null) {
3519 pw.print(innerPrefix); pw.println(r.app);
3520 }
3521 }
3522 if (client && r.app != null && r.app.thread != null) {
3523 // flush anything that is already in the PrintWriter since the thread is going
3524 // to write to the file descriptor directly
3525 pw.flush();
3526 try {
3527 TransferPipe tp = new TransferPipe();
3528 try {
3529 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3530 r.appToken, innerPrefix, args);
3531 // Short timeout, since blocking here can
3532 // deadlock with the application.
3533 tp.go(fd, 2000);
3534 } finally {
3535 tp.kill();
3536 }
3537 } catch (IOException e) {
3538 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3539 } catch (RemoteException e) {
3540 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3541 }
3542 needNL = true;
3543 }
3544 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003545 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003546 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003547
Craig Mautnerf3333272013-04-22 10:55:53 -07003548 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003549 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3550 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003551 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3552 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003553 }
3554
3555 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003556 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003557 }
3558
3559 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003560 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3561 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003562 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3563 }
3564
Craig Mautner05d29032013-05-03 13:40:13 -07003565 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003566 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3567 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3568 }
Craig Mautner05d29032013-05-03 13:40:13 -07003569 }
3570
Craig Mautner0eea92c2013-05-16 13:35:39 -07003571 void removeSleepTimeouts() {
3572 mSleepTimeout = false;
3573 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3574 }
3575
3576 final void scheduleSleepTimeout() {
3577 removeSleepTimeouts();
3578 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3579 }
3580
Craig Mautner4a1cb222013-12-04 16:14:06 -08003581 @Override
3582 public void onDisplayAdded(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003583 Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003584 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3585 }
3586
3587 @Override
3588 public void onDisplayRemoved(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003589 Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003590 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3591 }
3592
3593 @Override
3594 public void onDisplayChanged(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003595 Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003596 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3597 }
3598
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003599 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003600 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003601 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003602 newDisplay = mActivityDisplays.get(displayId) == null;
3603 if (newDisplay) {
3604 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003605 if (activityDisplay.mDisplay == null) {
3606 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3607 return;
3608 }
Craig Mautner4504de52013-12-20 09:06:56 -08003609 mActivityDisplays.put(displayId, activityDisplay);
3610 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003611 }
Craig Mautner4504de52013-12-20 09:06:56 -08003612 if (newDisplay) {
3613 mWindowManager.onDisplayAdded(displayId);
3614 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003615 }
3616
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003617 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003618 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003619 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3620 if (activityDisplay != null) {
3621 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003622 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003623 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003624 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003625 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003626 }
3627 }
3628 mWindowManager.onDisplayRemoved(displayId);
3629 }
3630
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003631 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003632 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003633 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3634 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003635 // TODO: Update the bounds.
3636 }
3637 }
3638 mWindowManager.onDisplayChanged(displayId);
3639 }
3640
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003641 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003642 StackInfo info = new StackInfo();
3643 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3644 info.displayId = Display.DEFAULT_DISPLAY;
3645 info.stackId = stack.mStackId;
3646
3647 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3648 final int numTasks = tasks.size();
3649 int[] taskIds = new int[numTasks];
3650 String[] taskNames = new String[numTasks];
3651 for (int i = 0; i < numTasks; ++i) {
3652 final TaskRecord task = tasks.get(i);
3653 taskIds[i] = task.taskId;
3654 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3655 : task.realActivity != null ? task.realActivity.flattenToString()
3656 : task.getTopActivity() != null ? task.getTopActivity().packageName
3657 : "unknown";
3658 }
3659 info.taskIds = taskIds;
3660 info.taskNames = taskNames;
3661 return info;
3662 }
3663
3664 StackInfo getStackInfoLocked(int stackId) {
3665 ActivityStack stack = getStack(stackId);
3666 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003667 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003668 }
3669 return null;
3670 }
3671
3672 ArrayList<StackInfo> getAllStackInfosLocked() {
3673 ArrayList<StackInfo> list = new ArrayList<StackInfo>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003674 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3675 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003676 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003677 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003678 }
3679 }
3680 return list;
3681 }
3682
Craig Mautner15df08a2015-04-01 12:17:18 -07003683 TaskRecord getLockedTaskLocked() {
3684 final int top = mLockTaskModeTasks.size() - 1;
3685 if (top >= 0) {
3686 return mLockTaskModeTasks.get(top);
3687 }
3688 return null;
3689 }
3690
3691 boolean isLockedTask(TaskRecord task) {
3692 return mLockTaskModeTasks.contains(task);
3693 }
3694
3695 boolean isLastLockedTask(TaskRecord task) {
3696 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3697 }
3698
3699 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003700 if (!mLockTaskModeTasks.remove(task)) {
3701 return;
3702 }
3703 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3704 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003705 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003706 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3707 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003708 final Message lockTaskMsg = Message.obtain();
3709 lockTaskMsg.arg1 = task.userId;
3710 lockTaskMsg.what = LOCK_TASK_END_MSG;
3711 mHandler.sendMessage(lockTaskMsg);
3712 }
3713 }
3714
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003715 void showLockTaskToast() {
3716 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04003717 }
3718
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003719 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3720 if (mLockTaskModeTasks.contains(task)) {
3721 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3722 }
3723 }
3724
Craig Mautner432f64e2015-05-20 14:59:57 -07003725 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3726 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003727 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003728 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003729 final TaskRecord lockedTask = getLockedTaskLocked();
3730 if (lockedTask != null) {
3731 removeLockedTaskLocked(lockedTask);
3732 if (!mLockTaskModeTasks.isEmpty()) {
3733 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003734 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3735 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003736 lockedTask.performClearTaskLocked();
3737 resumeTopActivitiesLocked();
3738 return;
3739 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003740 }
Craig Mautnere0570202015-05-13 13:06:11 -07003741 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3742 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003743 return;
3744 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003745
3746 // Should have already been checked, but do it again.
3747 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003748 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3749 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003750 return;
3751 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003752 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003753 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003754 return;
3755 }
3756
3757 if (mLockTaskModeTasks.isEmpty()) {
3758 // First locktask.
3759 final Message lockTaskMsg = Message.obtain();
3760 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3761 lockTaskMsg.arg1 = task.userId;
3762 lockTaskMsg.what = LOCK_TASK_START_MSG;
3763 lockTaskMsg.arg2 = lockTaskModeState;
3764 mHandler.sendMessage(lockTaskMsg);
3765 }
3766 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003767 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3768 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003769 mLockTaskModeTasks.remove(task);
3770 mLockTaskModeTasks.add(task);
3771
3772 if (task.mLockTaskUid == -1) {
3773 task.mLockTaskUid = task.mCallingUid;
3774 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003775
3776 if (andResume) {
3777 findTaskToMoveToFrontLocked(task, 0, null, reason);
3778 resumeTopActivitiesLocked();
3779 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003780 }
3781
3782 boolean isLockTaskModeViolation(TaskRecord task) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003783 if (getLockedTaskLocked() == task) {
3784 return false;
3785 }
3786 final int lockTaskAuth = task.mLockTaskAuth;
3787 switch (lockTaskAuth) {
3788 case LOCK_TASK_AUTH_DONT_LOCK:
3789 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003790 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003791 case LOCK_TASK_AUTH_LAUNCHABLE:
3792 case LOCK_TASK_AUTH_WHITELISTED:
3793 return false;
3794 case LOCK_TASK_AUTH_PINNABLE:
3795 // Pinnable tasks can't be launched on top of locktask tasks.
3796 return !mLockTaskModeTasks.isEmpty();
3797 default:
3798 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3799 return true;
3800 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003801 }
3802
Craig Mautner15df08a2015-04-01 12:17:18 -07003803 void onLockTaskPackagesUpdatedLocked() {
3804 boolean didSomething = false;
3805 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3806 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01003807 final boolean wasWhitelisted =
3808 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3809 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07003810 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01003811 final boolean isWhitelisted =
3812 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3813 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3814 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003815 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003816 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3817 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003818 removeLockedTaskLocked(lockedTask);
3819 lockedTask.performClearTaskLocked();
3820 didSomething = true;
3821 }
3822 }
3823 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3824 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3825 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3826 final ActivityStack stack = stacks.get(stackNdx);
3827 stack.onLockTaskPackagesUpdatedLocked();
3828 }
3829 }
Craig Mautnere0570202015-05-13 13:06:11 -07003830 final ActivityRecord r = topRunningActivityLocked();
3831 final TaskRecord task = r != null ? r.task : null;
3832 if (mLockTaskModeTasks.isEmpty() && task != null
3833 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3834 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07003835 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
3836 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
3837 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
3838 false);
3839 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07003840 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003841 if (didSomething) {
3842 resumeTopActivitiesLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003843 }
3844 }
3845
Benjamin Franz43261142015-02-11 15:59:44 +00003846 int getLockTaskModeState() {
3847 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003848 }
3849
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003850 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003851
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003852 public ActivityStackSupervisorHandler(Looper looper) {
3853 super(looper);
3854 }
3855
Craig Mautnerf3333272013-04-22 10:55:53 -07003856 void activityIdleInternal(ActivityRecord r) {
3857 synchronized (mService) {
3858 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3859 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003860 }
3861
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003862 @Override
3863 public void handleMessage(Message msg) {
3864 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003865 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003866 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3867 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003868 if (mService.mDidDexOpt) {
3869 mService.mDidDexOpt = false;
3870 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3871 nmsg.obj = msg.obj;
3872 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3873 return;
3874 }
3875 // We don't at this point know if the activity is fullscreen,
3876 // so we need to be conservative and assume it isn't.
3877 activityIdleInternal((ActivityRecord)msg.obj);
3878 } break;
3879 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003880 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003881 activityIdleInternal((ActivityRecord)msg.obj);
3882 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003883 case RESUME_TOP_ACTIVITY_MSG: {
3884 synchronized (mService) {
3885 resumeTopActivitiesLocked();
3886 }
3887 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003888 case SLEEP_TIMEOUT_MSG: {
3889 synchronized (mService) {
3890 if (mService.isSleepingOrShuttingDown()) {
3891 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3892 mSleepTimeout = true;
3893 checkReadyForSleepLocked();
3894 }
3895 }
3896 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003897 case LAUNCH_TIMEOUT_MSG: {
3898 if (mService.mDidDexOpt) {
3899 mService.mDidDexOpt = false;
3900 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3901 return;
3902 }
3903 synchronized (mService) {
3904 if (mLaunchingActivity.isHeld()) {
3905 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3906 if (VALIDATE_WAKE_LOCK_CALLER
3907 && Binder.getCallingUid() != Process.myUid()) {
3908 throw new IllegalStateException("Calling must be system uid");
3909 }
3910 mLaunchingActivity.release();
3911 }
3912 }
3913 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003914 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003915 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003916 } break;
3917 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003918 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003919 } break;
3920 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003921 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003922 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003923 case CONTAINER_CALLBACK_VISIBILITY: {
3924 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003925 final IActivityContainerCallback callback = container.mCallback;
3926 if (callback != null) {
3927 try {
3928 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3929 } catch (RemoteException e) {
3930 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003931 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003932 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003933 case LOCK_TASK_START_MSG: {
3934 // When lock task starts, we disable the status bars.
3935 try {
Jason Monk62515be2014-05-21 16:06:19 -04003936 if (mLockTaskNotify == null) {
3937 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003938 }
Jason Monk62515be2014-05-21 16:06:19 -04003939 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003940 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003941 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003942 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003943 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003944 flags = StatusBarManager.DISABLE_MASK
3945 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07003946 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003947 flags = StatusBarManager.DISABLE_MASK
3948 & (~StatusBarManager.DISABLE_BACK)
3949 & (~StatusBarManager.DISABLE_HOME)
3950 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003951 }
3952 getStatusBarService().disable(flags, mToken,
3953 mService.mContext.getPackageName());
3954 }
3955 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003956 if (getDevicePolicyManager() != null) {
3957 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003958 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003959 }
justinzhang5286d3f2014-05-12 17:06:01 -04003960 } catch (RemoteException ex) {
3961 throw new RuntimeException(ex);
3962 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003963 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003964 case LOCK_TASK_END_MSG: {
3965 // When lock task ends, we enable the status bars.
3966 try {
Jason Monk62515be2014-05-21 16:06:19 -04003967 if (getStatusBarService() != null) {
3968 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3969 mService.mContext.getPackageName());
3970 }
3971 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003972 if (getDevicePolicyManager() != null) {
3973 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3974 msg.arg1);
3975 }
Jason Monk62515be2014-05-21 16:06:19 -04003976 if (mLockTaskNotify == null) {
3977 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3978 }
3979 mLockTaskNotify.show(false);
3980 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003981 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003982 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003983 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003984 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003985 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003986 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003987 new LockPatternUtils(mService.mContext)
3988 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003989 }
3990 } catch (SettingNotFoundException e) {
3991 // No setting, don't lock.
3992 }
justinzhang5286d3f2014-05-12 17:06:01 -04003993 } catch (RemoteException ex) {
3994 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003995 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07003996 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003997 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003998 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003999 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4000 if (mLockTaskNotify == null) {
4001 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4002 }
4003 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4004 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004005 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4006 final ActivityContainer container = (ActivityContainer) msg.obj;
4007 final IActivityContainerCallback callback = container.mCallback;
4008 if (callback != null) {
4009 try {
4010 callback.onAllActivitiesComplete(container.asBinder());
4011 } catch (RemoteException e) {
4012 }
4013 }
4014 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004015 case LAUNCH_TASK_BEHIND_COMPLETE: {
4016 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004017 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004018 if (r != null) {
4019 handleLaunchTaskBehindCompleteLocked(r);
4020 }
4021 }
4022 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004023 }
4024 }
4025 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004026
Ying Wangb081a592014-04-22 15:20:16 -07004027 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07004028 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07004029 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004030 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004031 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004032 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004033 ActivityRecord mParentActivity = null;
4034 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004035
Craig Mautnere3a00d72014-04-16 08:31:19 -07004036 boolean mVisible = true;
4037
Craig Mautner4a1cb222013-12-04 16:14:06 -08004038 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004039 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004040
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004041 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4042 final static int CONTAINER_STATE_NO_SURFACE = 1;
4043 final static int CONTAINER_STATE_FINISHING = 2;
4044 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4045
4046 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004047 synchronized (mService) {
4048 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004049 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004050 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004051 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004052 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004053 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004054
Craig Mautnere0a38842013-12-16 16:14:02 -08004055 void attachToDisplayLocked(ActivityDisplay activityDisplay) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004056 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Craig Mautner34b73df2014-01-12 21:11:08 -08004057 + " to display=" + activityDisplay);
Craig Mautnere0a38842013-12-16 16:14:02 -08004058 mActivityDisplay = activityDisplay;
4059 mStack.mDisplayId = activityDisplay.mDisplayId;
4060 mStack.mStacks = activityDisplay.mStacks;
4061
4062 activityDisplay.attachActivities(mStack);
Craig Mautnerdf88d732014-01-27 09:21:32 -08004063 mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004064 }
4065
4066 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004067 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004068 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004069 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4070 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004071 return;
4072 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004073 attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004074 }
4075 }
4076
4077 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004078 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004079 synchronized (mService) {
4080 if (mActivityDisplay != null) {
4081 return mActivityDisplay.mDisplayId;
4082 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004083 }
4084 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004085 }
4086
Jeff Brownca9bc702014-02-11 14:32:56 -08004087 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004088 public int getStackId() {
4089 synchronized (mService) {
4090 return mStackId;
4091 }
4092 }
4093
4094 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004095 public boolean injectEvent(InputEvent event) {
4096 final long origId = Binder.clearCallingIdentity();
4097 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004098 synchronized (mService) {
4099 if (mActivityDisplay != null) {
4100 return mInputManagerInternal.injectInputEvent(event,
4101 mActivityDisplay.mDisplayId,
4102 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4103 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004104 }
4105 return false;
4106 } finally {
4107 Binder.restoreCallingIdentity(origId);
4108 }
4109 }
4110
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004111 @Override
4112 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004113 synchronized (mService) {
4114 if (mContainerState == CONTAINER_STATE_FINISHING) {
4115 return;
4116 }
4117 mContainerState = CONTAINER_STATE_FINISHING;
4118
Craig Mautnerd163e752014-06-13 17:18:47 -07004119 long origId = Binder.clearCallingIdentity();
4120 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004121 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07004122 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004123 } finally {
4124 Binder.restoreCallingIdentity(origId);
4125 }
4126 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004127 }
4128
Craig Mautner60257702014-09-17 15:02:33 -07004129 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004130 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004131 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004132 if (mActivityDisplay != null) {
4133 mActivityDisplay.detachActivitiesLocked(mStack);
4134 mActivityDisplay = null;
4135 mStack.mDisplayId = -1;
4136 mStack.mStacks = null;
Craig Mautnerdf88d732014-01-27 09:21:32 -08004137 mWindowManager.detachStack(mStackId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004138 }
4139 }
4140
4141 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004142 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004143 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnerb9168362015-02-26 20:40:19 -08004144 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004145 Binder.getCallingUid(), mCurrentUser, false,
4146 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004147
Craig Mautnere0a38842013-12-16 16:14:02 -08004148 // TODO: Switch to user app stacks here.
4149 String mimeType = intent.getType();
Craig Mautnerb9168362015-02-26 20:40:19 -08004150 final Uri data = intent.getData();
4151 if (mimeType == null && data != null && "content".equals(data.getScheme())) {
4152 mimeType = mService.getProviderMimeType(data, userId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004153 }
Craig Mautnerb9168362015-02-26 20:40:19 -08004154 checkEmbeddedAllowedInner(userId, intent, mimeType);
4155
4156 intent.addFlags(FORCE_NEW_TASK_FLAGS);
4157 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null,
4158 0, 0, null, null, null, null, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004159 }
4160
4161 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004162 public final int startActivityIntentSender(IIntentSender intentSender)
4163 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004164 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4165
4166 if (!(intentSender instanceof PendingIntentRecord)) {
4167 throw new IllegalArgumentException("Bad PendingIntent object");
4168 }
4169
Craig Mautnerb9168362015-02-26 20:40:19 -08004170 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004171 Binder.getCallingUid(), mCurrentUser, false,
4172 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004173
4174 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4175 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4176 pendingIntent.key.requestResolvedType);
4177
4178 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4179 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4180 }
4181
4182 private void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004183 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004184 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004185 throw new SecurityException(
4186 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4187 }
4188 }
4189
Craig Mautnerdf88d732014-01-27 09:21:32 -08004190 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004191 public IBinder asBinder() {
4192 return this;
4193 }
4194
Craig Mautner4504de52013-12-20 09:06:56 -08004195 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004196 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004197 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004198 }
4199
Craig Mautner4a1cb222013-12-04 16:14:06 -08004200 ActivityStackSupervisor getOuter() {
4201 return ActivityStackSupervisor.this;
4202 }
4203
Craig Mautnerd163e752014-06-13 17:18:47 -07004204 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004205 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004206 }
4207
4208 void getBounds(Point outBounds) {
Craig Mautnerd163e752014-06-13 17:18:47 -07004209 synchronized (mService) {
4210 if (mActivityDisplay != null) {
4211 mActivityDisplay.getBounds(outBounds);
4212 } else {
4213 outBounds.set(0, 0);
4214 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004215 }
4216 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004217
Craig Mautner6985bad2014-04-21 15:22:06 -07004218 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004219 void setVisible(boolean visible) {
4220 if (mVisible != visible) {
4221 mVisible = visible;
4222 if (mCallback != null) {
4223 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4224 0 /* unused */, this).sendToTarget();
4225 }
4226 }
4227 }
4228
Craig Mautner6985bad2014-04-21 15:22:06 -07004229 void setDrawn() {
4230 }
4231
Craig Mautner1b4bf852014-05-26 15:06:32 -07004232 // You can always start a new task on a regular ActivityStack.
4233 boolean isEligibleForNewTasks() {
4234 return true;
4235 }
4236
Craig Mautnerd163e752014-06-13 17:18:47 -07004237 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004238 detachLocked();
4239 deleteActivityContainer(this);
4240 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004241 }
4242
Craig Mautner34b73df2014-01-12 21:11:08 -08004243 @Override
4244 public String toString() {
4245 return mIdString + (mActivityDisplay == null ? "N" : "A");
4246 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004247 }
4248
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004249 private class VirtualActivityContainer extends ActivityContainer {
4250 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004251 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004252
4253 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4254 super(getNextStackId());
4255 mParentActivity = parent;
4256 mCallback = callback;
4257 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004258 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004259 }
4260
4261 @Override
4262 public void setSurface(Surface surface, int width, int height, int density) {
4263 super.setSurface(surface, width, height, density);
4264
4265 synchronized (mService) {
4266 final long origId = Binder.clearCallingIdentity();
4267 try {
4268 setSurfaceLocked(surface, width, height, density);
4269 } finally {
4270 Binder.restoreCallingIdentity(origId);
4271 }
4272 }
4273 }
4274
4275 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4276 if (mContainerState == CONTAINER_STATE_FINISHING) {
4277 return;
4278 }
4279 VirtualActivityDisplay virtualActivityDisplay =
4280 (VirtualActivityDisplay) mActivityDisplay;
4281 if (virtualActivityDisplay == null) {
4282 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004283 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004284 mActivityDisplay = virtualActivityDisplay;
4285 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
4286 attachToDisplayLocked(virtualActivityDisplay);
4287 }
4288
4289 if (mSurface != null) {
4290 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004291 }
4292
Craig Mautner6985bad2014-04-21 15:22:06 -07004293 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004294 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004295 mStack.resumeTopActivityLocked(null);
4296 } else {
4297 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004298 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004299 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004300 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004301 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004302 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004303
Craig Mautnerd163e752014-06-13 17:18:47 -07004304 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004305
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004306 if (DEBUG_STACK) Slog.d(TAG_STACK,
4307 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004308 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004309
Craig Mautner6985bad2014-04-21 15:22:06 -07004310 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004311 boolean isAttachedLocked() {
4312 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004313 }
4314
4315 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004316 void setDrawn() {
4317 synchronized (mService) {
4318 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004319 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004320 }
4321 }
4322
Craig Mautner1b4bf852014-05-26 15:06:32 -07004323 // Never start a new task on an ActivityView if it isn't explicitly specified.
4324 @Override
4325 boolean isEligibleForNewTasks() {
4326 return false;
4327 }
4328
Craig Mautnerd163e752014-06-13 17:18:47 -07004329 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004330 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004331 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4332 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4333 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4334 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4335 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004336 }
4337 }
4338
Craig Mautner4a1cb222013-12-04 16:14:06 -08004339 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4340 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004341 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004342 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004343 int mDisplayId;
4344 Display mDisplay;
4345 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004346
Craig Mautner4a1cb222013-12-04 16:14:06 -08004347 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4348 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08004349 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004350
Jose Lima4b6c6692014-08-12 17:41:12 -07004351 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004352
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004353 ActivityDisplay() {
4354 }
Craig Mautner4504de52013-12-20 09:06:56 -08004355
Craig Mautner1a70a162014-09-13 12:09:31 -07004356 // After instantiation, check that mDisplay is not null before using this. The alternative
4357 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004358 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004359 final Display display = mDisplayManager.getDisplay(displayId);
4360 if (display == null) {
4361 return;
4362 }
4363 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004364 }
4365
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004366 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004367 mDisplay = display;
4368 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004369 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004370 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004371
4372 void attachActivities(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004373 if (DEBUG_STACK) Slog.v(TAG_STACK,
4374 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004375 mStacks.add(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004376 }
4377
Craig Mautnere0a38842013-12-16 16:14:02 -08004378 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004379 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004380 + " from displayId=" + mDisplayId);
4381 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004382 }
4383
4384 void getBounds(Point bounds) {
4385 mDisplay.getDisplayInfo(mDisplayInfo);
4386 bounds.x = mDisplayInfo.appWidth;
4387 bounds.y = mDisplayInfo.appHeight;
4388 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004389
Jose Lima4b6c6692014-08-12 17:41:12 -07004390 void setVisibleBehindActivity(ActivityRecord r) {
4391 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004392 }
4393
Jose Lima4b6c6692014-08-12 17:41:12 -07004394 boolean hasVisibleBehindActivity() {
4395 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004396 }
4397
Craig Mautner34b73df2014-01-12 21:11:08 -08004398 @Override
4399 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004400 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4401 }
4402 }
4403
4404 class VirtualActivityDisplay extends ActivityDisplay {
4405 VirtualDisplay mVirtualDisplay;
4406
Craig Mautner6985bad2014-04-21 15:22:06 -07004407 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004408 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004409 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4410 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4411 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4412 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004413
4414 init(mVirtualDisplay.getDisplay());
4415
4416 mWindowManager.handleDisplayAdded(mDisplayId);
4417 }
4418
4419 void setSurface(Surface surface) {
4420 if (mVirtualDisplay != null) {
4421 mVirtualDisplay.setSurface(surface);
4422 }
4423 }
4424
4425 @Override
4426 void detachActivitiesLocked(ActivityStack stack) {
4427 super.detachActivitiesLocked(stack);
4428 if (mVirtualDisplay != null) {
4429 mVirtualDisplay.release();
4430 mVirtualDisplay = null;
4431 }
4432 }
4433
4434 @Override
4435 public String toString() {
4436 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004437 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004438 }
Jose Lima58e66d62014-05-27 20:00:27 -07004439
4440 private boolean isLeanbackOnlyDevice() {
4441 boolean onLeanbackOnly = false;
4442 try {
4443 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
4444 PackageManager.FEATURE_LEANBACK_ONLY);
4445 } catch (RemoteException e) {
4446 // noop
4447 }
4448
4449 return onLeanbackOnly;
4450 }
Craig Mautner27084302013-03-25 08:05:25 -07004451}