blob: 4e98576e6fb9f66e63a023fe2d267ad1c8df9888 [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) {
Jason Monk25d237b2015-06-19 10:39:39 -04001918 // When the flags NEW_TASK and CLEAR_TASK are set, then the task gets reused
1919 // but still needs to be a lock task mode violation since the task gets
1920 // cleared out and the device would otherwise leave the locked task.
1921 if (isLockTaskModeViolation(intentActivity.task,
1922 (launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
1923 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))) {
Craig Mautner6cd6cec2015-04-01 00:02:12 -07001924 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07001925 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08001926 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1927 }
Craig Mautner29219d92013-04-16 20:19:12 -07001928 if (r.task == null) {
1929 r.task = intentActivity.task;
1930 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001931 if (intentActivity.task.intent == null) {
1932 // This task was started because of movement of
1933 // the activity based on affinity... now that we
1934 // are actually launching it, we can assign the
1935 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07001936 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001937 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001938 targetStack = intentActivity.task.stack;
1939 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001940 // If the target task is not in the front, then we need
1941 // to bring it to the front... except... well, with
1942 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
1943 // to have the same behavior as if a new instance was
1944 // being started, which means not bringing it to the front
1945 // if the caller is not itself in the front.
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001946 final ActivityStack focusStack = getFocusedStack();
1947 ActivityRecord curTop = (focusStack == null)
1948 ? null : focusStack.topRunningNonDelayedActivityLocked(notTop);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001949 boolean movedToFront = false;
Craig Mautner7504d7b2013-09-17 10:50:53 -07001950 if (curTop != null && (curTop.task != intentActivity.task ||
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001951 curTop.task != focusStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001952 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07001953 if (sourceRecord == null || (sourceStack.topActivity() != null &&
1954 sourceStack.topActivity().task == sourceRecord.task)) {
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001955 // We really do want to push this one into the user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07001956 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07001957 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
1958 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001959 movedHome = true;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001960 targetStack.moveTaskToFrontLocked(intentActivity.task, noAnimation,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001961 options, r.appTimeTracker, "bringingFoundTaskToFront");
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001962 movedToFront = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001963 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07001964 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
1965 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07001966 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07001967 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001968 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001969 options = null;
1970 }
1971 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001972 if (!movedToFront) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001973 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Bring to front target: " + targetStack
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001974 + " from " + intentActivity);
1975 targetStack.moveToFront("intentActivityFound");
1976 }
1977
Craig Mautner8849a5e2013-04-02 16:41:03 -07001978 // If the caller has requested that the target task be
1979 // reset, then do so.
1980 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1981 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
1982 }
Craig Mautner15df08a2015-04-01 12:17:18 -07001983 if ((startFlags & ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001984 // We don't need to start a new activity, and
1985 // the client said not to do anything if that
1986 // is the case, so this is it! And for paranoia, make
1987 // sure we have correctly resumed the top activity.
1988 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07001989 resumeTopActivitiesLocked(targetStack, null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001990
1991 // Make sure to notify Keyguard as well if we are not running an app
1992 // transition later.
1993 if (!movedToFront) {
1994 notifyActivityDrawnForKeyguard();
1995 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001996 } else {
1997 ActivityOptions.abort(options);
1998 }
1999 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2000 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002001 if ((launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002002 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002003 // The caller has requested to completely replace any
2004 // existing task with its new activity. Well that should
2005 // not be too hard...
2006 reuseTask = intentActivity.task;
2007 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07002008 reuseTask.setIntent(r);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002009 } else if ((launchFlags & FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07002010 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002011 // In this situation we want to remove all activities
2012 // from the task up to the one being started. In most
2013 // cases this means we are resetting the task to its
2014 // initial state.
2015 ActivityRecord top =
2016 intentActivity.task.performClearTaskLocked(r, launchFlags);
2017 if (top != null) {
2018 if (top.frontOfTask) {
2019 // Activity aliases may mean we use different
2020 // intents for the top activity, so make sure
2021 // the task now has the identity of the new
2022 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07002023 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002024 }
2025 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
2026 r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002027 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002028 } else {
Wale Ogunwale86920fe2015-03-17 11:36:24 -07002029 // A special case: we need to start the activity because it is not
2030 // currently running, and the caller has asked to clear the current
2031 // task to have this activity at the top.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002032 addingToTask = true;
Wale Ogunwale86920fe2015-03-17 11:36:24 -07002033 // Now pretend like this activity is being started by the top of its
2034 // task, so it is put in the right place.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002035 sourceRecord = intentActivity;
Wale Ogunwale86920fe2015-03-17 11:36:24 -07002036 TaskRecord task = sourceRecord.task;
2037 if (task != null && task.stack == null) {
2038 // Target stack got cleared when we all activities were removed
2039 // above. Go ahead and reset it.
2040 targetStack = computeStackFocus(sourceRecord, false /* newTask */);
2041 targetStack.addTask(
2042 task, !launchTaskBehind /* toTop */, false /* moving */);
2043 }
2044
Craig Mautner8849a5e2013-04-02 16:41:03 -07002045 }
2046 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
2047 // In this case the top activity on the task is the
2048 // same as the one being launched, so we take that
2049 // as a request to bring the task to the foreground.
2050 // If the top activity in the task is the root
2051 // activity, deliver this new intent to it if it
2052 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07002053 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07002054 && intentActivity.realActivity.equals(r.realActivity)) {
2055 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
2056 intentActivity.task);
2057 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07002058 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002059 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002060 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
2061 r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002062 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
2063 // In this case we are launching the root activity
2064 // of the task, but with a different intent. We
2065 // should start a new instance on top.
2066 addingToTask = true;
2067 sourceRecord = intentActivity;
2068 }
2069 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
2070 // In this case an activity is being launched in to an
2071 // existing task, without resetting that task. This
2072 // is typically the situation of launching an activity
2073 // from a notification or shortcut. We want to place
2074 // the new activity on top of the current task.
2075 addingToTask = true;
2076 sourceRecord = intentActivity;
2077 } else if (!intentActivity.task.rootWasReset) {
2078 // In this case we are launching in to an existing task
2079 // that has not yet been started from its front door.
2080 // The current task has been brought to the front.
2081 // Ideally, we'd probably like to place this new task
2082 // at the bottom of its stack, but that's a little hard
2083 // to do with the current organization of the code so
2084 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07002085 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002086 }
2087 if (!addingToTask && reuseTask == null) {
2088 // We didn't do anything... but it was needed (a.k.a., client
2089 // don't use that intent!) And for paranoia, make
2090 // sure we have correctly resumed the top activity.
2091 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002092 targetStack.resumeTopActivityLocked(null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002093 if (!movedToFront) {
2094 // Make sure to notify Keyguard as well if we are not running an app
2095 // transition later.
2096 notifyActivityDrawnForKeyguard();
2097 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002098 } else {
2099 ActivityOptions.abort(options);
2100 }
2101 return ActivityManager.START_TASK_TO_FRONT;
2102 }
2103 }
2104 }
2105 }
2106
2107 //String uri = r.intent.toURI();
2108 //Intent intent2 = new Intent(uri);
2109 //Slog.i(TAG, "Given intent: " + r.intent);
2110 //Slog.i(TAG, "URI is: " + uri);
2111 //Slog.i(TAG, "To intent: " + intent2);
2112
2113 if (r.packageName != null) {
2114 // If the activity being launched is the same as the one currently
2115 // at the top, then we need to check if it should only be launched
2116 // once.
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002117 ActivityStack topStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002118 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002119 if (top != null && r.resultTo == null) {
2120 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2121 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07002122 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07002123 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002124 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
2125 top.task);
2126 // For paranoia, make sure we have correctly
2127 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002128 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002129 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002130 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002131 }
2132 ActivityOptions.abort(options);
2133 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2134 // We don't need to start a new activity, and
2135 // the client said not to do anything if that
2136 // is the case, so this is it!
2137 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2138 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002139 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002140 return ActivityManager.START_DELIVERED_TO_TOP;
2141 }
2142 }
2143 }
2144 }
2145
2146 } else {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002147 if (r.resultTo != null && r.resultTo.task.stack != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002148 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2149 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002150 }
2151 ActivityOptions.abort(options);
2152 return ActivityManager.START_CLASS_NOT_FOUND;
2153 }
2154
2155 boolean newTask = false;
2156 boolean keepCurTransition = false;
2157
Craig Mautnerbb742462014-07-07 15:28:55 -07002158 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002159 sourceRecord.task : null;
2160
Craig Mautner8849a5e2013-04-02 16:41:03 -07002161 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002162 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002163 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002164 newTask = true;
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002165 targetStack = computeStackFocus(r, newTask);
2166 targetStack.moveToFront("startingNewTask");
2167
Craig Mautner8849a5e2013-04-02 16:41:03 -07002168 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002169 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2170 newTaskInfo != null ? newTaskInfo : r.info,
2171 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002172 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2173 taskToAffiliate);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002174 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2175 "Starting new activity " + r + " in new task " + r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002176 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002177 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002178 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002179 if (isLockTaskModeViolation(r.task)) {
2180 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2181 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2182 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002183 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002184 if ((launchFlags &
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002185 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2186 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002187 // Caller wants to appear on home activity, so before starting
2188 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002189 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002190 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002191 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002192 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002193 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002194 if (isLockTaskModeViolation(sourceTask)) {
2195 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2196 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2197 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002198 targetStack = sourceTask.stack;
Craig Mautner299f9602015-01-26 09:47:33 -08002199 targetStack.moveToFront("sourceStackToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002200 final TaskRecord topTask = targetStack.topTask();
2201 if (topTask != sourceTask) {
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002202 targetStack.moveTaskToFrontLocked(sourceTask, noAnimation, options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002203 r.appTimeTracker, "sourceTaskToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002204 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002205 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002206 // In this case, we are adding the activity to an existing
2207 // task, but the caller has asked to clear that task if the
2208 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002209 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002210 keepCurTransition = true;
2211 if (top != null) {
2212 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002213 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002214 // For paranoia, make sure we have correctly
2215 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002216 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002217 if (doResume) {
2218 targetStack.resumeTopActivityLocked(null);
2219 }
2220 ActivityOptions.abort(options);
2221 return ActivityManager.START_DELIVERED_TO_TOP;
2222 }
2223 } else if (!addingToTask &&
2224 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2225 // In this case, we are launching an activity in our own task
2226 // that may already be running somewhere in the history, and
2227 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002228 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002229 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002230 final TaskRecord task = top.task;
2231 task.moveActivityToFrontLocked(top);
2232 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002233 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002234 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002235 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002236 if (doResume) {
2237 targetStack.resumeTopActivityLocked(null);
2238 }
2239 return ActivityManager.START_DELIVERED_TO_TOP;
2240 }
2241 }
2242 // An existing activity is starting this new activity, so we want
2243 // to keep the new one in the same task as the one that is starting
2244 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002245 r.setTask(sourceTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002246 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002247 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002248
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002249 } else if (inTask != null) {
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002250 // The caller is asking that the new activity be started in an explicit
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002251 // task it has provided to us.
2252 if (isLockTaskModeViolation(inTask)) {
2253 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2254 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2255 }
2256 targetStack = inTask.stack;
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002257 targetStack.moveTaskToFrontLocked(inTask, noAnimation, options, r.appTimeTracker,
2258 "inTaskToFront");
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002259
2260 // Check whether we should actually launch the new activity in to the task,
2261 // or just reuse the current activity on top.
2262 ActivityRecord top = inTask.getTopActivity();
2263 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2264 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2265 || launchSingleTop || launchSingleTask) {
2266 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2267 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2268 // We don't need to start a new activity, and
2269 // the client said not to do anything if that
2270 // is the case, so this is it!
2271 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2272 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002273 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002274 return ActivityManager.START_DELIVERED_TO_TOP;
2275 }
2276 }
2277
Dianne Hackborn962d5352014-08-29 16:27:40 -07002278 if (!addingToTask) {
2279 // We don't actually want to have this activity added to the task, so just
2280 // stop here but still tell the caller that we consumed the intent.
2281 ActivityOptions.abort(options);
2282 return ActivityManager.START_TASK_TO_FRONT;
2283 }
2284
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002285 r.setTask(inTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002286 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002287 + " in explicit task " + r.task);
2288
Craig Mautner8849a5e2013-04-02 16:41:03 -07002289 } else {
2290 // This not being started from an existing activity, and not part
2291 // of a new task... just put it in the top task, though these days
2292 // this case should never happen.
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002293 targetStack = computeStackFocus(r, newTask);
Craig Mautner299f9602015-01-26 09:47:33 -08002294 targetStack.moveToFront("addingToTopTask");
Craig Mautner1602ec22013-05-12 10:24:27 -07002295 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002296 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002297 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002298 mWindowManager.moveTaskToTop(r.task.taskId);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002299 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Craig Mautner8849a5e2013-04-02 16:41:03 -07002300 + " in new guessed " + r.task);
2301 }
2302
2303 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002304 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002305
Craig Mautner76e2a762014-06-24 09:05:43 -07002306 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2307 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2308 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002309 if (newTask) {
2310 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2311 }
2312 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002313 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002314 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Craig Mautnerbb742462014-07-07 15:28:55 -07002315 if (!launchTaskBehind) {
2316 // Don't set focus on an activity that's going to the back.
Craig Mautner299f9602015-01-26 09:47:33 -08002317 mService.setFocusedActivityLocked(r, "startedActivity");
Craig Mautnerbb742462014-07-07 15:28:55 -07002318 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002319 return ActivityManager.START_SUCCESS;
2320 }
2321
Craig Mautneree36c772014-07-16 14:56:05 -07002322 final void doPendingActivityLaunchesLocked(boolean doResume) {
2323 while (!mPendingActivityLaunches.isEmpty()) {
2324 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Craig Mautneraa9b0f12014-07-17 10:50:18 -07002325 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002326 doResume && mPendingActivityLaunches.isEmpty(), null, null);
Craig Mautneree36c772014-07-16 14:56:05 -07002327 }
2328 }
2329
Craig Mautner7f13ed32014-07-28 14:00:35 -07002330 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002331 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2332 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002333 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002334 mPendingActivityLaunches.remove(palNdx);
2335 }
2336 }
2337 }
2338
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002339 void setLaunchSource(int uid) {
2340 mLaunchingActivity.setWorkSource(new WorkSource(uid));
2341 }
2342
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002343 void acquireLaunchWakelock() {
2344 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2345 throw new IllegalStateException("Calling must be system uid");
2346 }
2347 mLaunchingActivity.acquire();
2348 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2349 // To be safe, don't allow the wake lock to be held for too long.
2350 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2351 }
2352 }
2353
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002354 /**
2355 * Called when the frontmost task is idle.
2356 * @return the state of mService.mBooting before this was called.
2357 */
2358 private boolean checkFinishBootingLocked() {
2359 final boolean booting = mService.mBooting;
2360 boolean enableScreen = false;
2361 mService.mBooting = false;
2362 if (!mService.mBooted) {
2363 mService.mBooted = true;
2364 enableScreen = true;
2365 }
2366 if (booting || enableScreen) {
2367 mService.postFinishBooting(booting, enableScreen);
2368 }
2369 return booting;
2370 }
2371
Craig Mautnerf3333272013-04-22 10:55:53 -07002372 // Checked.
2373 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2374 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002375 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002376
Craig Mautnerf3333272013-04-22 10:55:53 -07002377 ArrayList<ActivityRecord> stops = null;
2378 ArrayList<ActivityRecord> finishes = null;
2379 ArrayList<UserStartedState> startingUsers = null;
2380 int NS = 0;
2381 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002382 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002383 boolean activityRemoved = false;
2384
Wale Ogunwale7d701172015-03-11 15:36:30 -07002385 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002386 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002387 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
2388 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002389 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2390 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002391 if (fromTimeout) {
2392 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002393 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002394
2395 // This is a hack to semi-deal with a race condition
2396 // in the client where it can be constructed with a
2397 // newer configuration from when we asked it to launch.
2398 // We'll update with whatever configuration it now says
2399 // it used to launch.
2400 if (config != null) {
2401 r.configuration = config;
2402 }
2403
2404 // We are now idle. If someone is waiting for a thumbnail from
2405 // us, we can now deliver.
2406 r.idle = true;
2407
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002408 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Dianne Hackborn9449a612014-10-01 15:53:28 -07002409 if (isFrontStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002410 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002411 }
2412 }
2413
2414 if (allResumedActivitiesIdle()) {
2415 if (r != null) {
2416 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002417 }
2418
2419 if (mLaunchingActivity.isHeld()) {
2420 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2421 if (VALIDATE_WAKE_LOCK_CALLER &&
2422 Binder.getCallingUid() != Process.myUid()) {
2423 throw new IllegalStateException("Calling must be system uid");
2424 }
2425 mLaunchingActivity.release();
2426 }
2427 ensureActivitiesVisibleLocked(null, 0);
Craig Mautnerf3333272013-04-22 10:55:53 -07002428 }
2429
2430 // Atomically retrieve all of the other things to do.
2431 stops = processStoppingActivitiesLocked(true);
2432 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002433 if ((NF = mFinishingActivities.size()) > 0) {
2434 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002435 mFinishingActivities.clear();
2436 }
2437
Craig Mautnerf3333272013-04-22 10:55:53 -07002438 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002439 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07002440 mStartingUsers.clear();
2441 }
2442
Craig Mautnerf3333272013-04-22 10:55:53 -07002443 // Stop any activities that are scheduled to do so but have been
2444 // waiting for the next one to start.
2445 for (int i = 0; i < NS; i++) {
2446 r = stops.get(i);
2447 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002448 if (stack != null) {
2449 if (r.finishing) {
2450 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2451 } else {
2452 stack.stopActivityLocked(r);
2453 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002454 }
2455 }
2456
2457 // Finish any activities that are scheduled to do so but have been
2458 // waiting for the next one to start.
2459 for (int i = 0; i < NF; i++) {
2460 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07002461 final ActivityStack stack = r.task.stack;
2462 if (stack != null) {
2463 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
2464 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002465 }
2466
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002467 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002468 // Complete user switch
2469 if (startingUsers != null) {
2470 for (int i = 0; i < startingUsers.size(); i++) {
2471 mService.finishUserSwitch(startingUsers.get(i));
2472 }
2473 }
2474 // Complete starting up of background users
2475 if (mStartingBackgroundUsers.size() > 0) {
2476 startingUsers = new ArrayList<UserStartedState>(mStartingBackgroundUsers);
2477 mStartingBackgroundUsers.clear();
2478 for (int i = 0; i < startingUsers.size(); i++) {
2479 mService.finishUserBoot(startingUsers.get(i));
2480 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002481 }
2482 }
2483
2484 mService.trimApplications();
2485 //dump();
2486 //mWindowManager.dump();
2487
Craig Mautnerf3333272013-04-22 10:55:53 -07002488 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002489 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002490 }
2491
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002492 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002493 }
2494
Craig Mautner8e569572013-10-11 17:36:59 -07002495 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002496 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002497 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2498 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002499 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2500 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2501 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002502 }
Craig Mautner19091252013-10-05 00:03:53 -07002503 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002504 }
2505
2506 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002507 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2508 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002509 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2510 stacks.get(stackNdx).closeSystemDialogsLocked();
2511 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002512 }
2513 }
2514
Craig Mautner93529a42013-10-04 15:03:13 -07002515 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002516 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002517 }
2518
Craig Mautner8d341ef2013-03-26 09:03:27 -07002519 /**
2520 * @return true if some activity was finished (or would have finished if doit were true).
2521 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07002522 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
2523 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002524 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002525 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2526 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002527 final int numStacks = stacks.size();
2528 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2529 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07002530 if (stack.finishDisabledPackageActivitiesLocked(
2531 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002532 didSomething = true;
2533 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002534 }
2535 }
2536 return didSomething;
2537 }
2538
Dianne Hackborna413dc02013-07-12 12:02:55 -07002539 void updatePreviousProcessLocked(ActivityRecord r) {
2540 // Now that this process has stopped, we may want to consider
2541 // it to be the previous app to try to keep around in case
2542 // the user wants to return to it.
2543
2544 // First, found out what is currently the foreground app, so that
2545 // we don't blow away the previous app if this activity is being
2546 // hosted by the process that is actually still the foreground.
2547 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002548 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2549 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002550 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2551 final ActivityStack stack = stacks.get(stackNdx);
2552 if (isFrontStack(stack)) {
2553 if (stack.mResumedActivity != null) {
2554 fgApp = stack.mResumedActivity.app;
2555 } else if (stack.mPausingActivity != null) {
2556 fgApp = stack.mPausingActivity.app;
2557 }
2558 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002559 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002560 }
2561 }
2562
2563 // Now set this one as the previous process, only if that really
2564 // makes sense to.
2565 if (r.app != null && fgApp != null && r.app != fgApp
2566 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002567 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002568 mService.mPreviousProcess = r.app;
2569 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2570 }
2571 }
2572
Craig Mautner05d29032013-05-03 13:40:13 -07002573 boolean resumeTopActivitiesLocked() {
2574 return resumeTopActivitiesLocked(null, null, null);
2575 }
2576
2577 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2578 Bundle targetOptions) {
2579 if (targetStack == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002580 targetStack = mFocusedStack;
Craig Mautner05d29032013-05-03 13:40:13 -07002581 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002582 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002583 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002584 if (isFrontStack(targetStack)) {
2585 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2586 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002587
Craig Mautnere0a38842013-12-16 16:14:02 -08002588 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2589 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002590 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2591 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002592 if (stack == targetStack) {
2593 // Already started above.
2594 continue;
2595 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002596 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002597 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002598 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002599 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002600 }
Craig Mautner05d29032013-05-03 13:40:13 -07002601 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002602 }
2603
Todd Kennedy539db512014-12-15 09:57:55 -08002604 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002605 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2606 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002607 final int numStacks = stacks.size();
2608 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2609 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08002610 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002611 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002612 }
2613 }
2614
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002615 void finishVoiceTask(IVoiceInteractionSession session) {
2616 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2617 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2618 final int numStacks = stacks.size();
2619 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2620 final ActivityStack stack = stacks.get(stackNdx);
2621 stack.finishVoiceTask(session);
2622 }
2623 }
2624 }
2625
Craig Mautner299f9602015-01-26 09:47:33 -08002626 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002627 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2628 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002629 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002630 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2631 // Caller wants the home activity moved with it. To accomplish this,
2632 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002633 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002634 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07002635 if (task.stack == null) {
2636 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2637 + task + " to front. Stack is null");
2638 return;
2639 }
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002640 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
2641 task.getTopActivity() == null ? null : task.getTopActivity().appTimeTracker,
2642 reason);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002643 if (DEBUG_STACK) Slog.d(TAG_STACK,
2644 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002645 }
2646
Craig Mautner967212c2013-04-13 21:10:58 -07002647 ActivityStack getStack(int stackId) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002648 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2649 if (activityContainer != null) {
2650 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002651 }
2652 return null;
2653 }
2654
Craig Mautner967212c2013-04-13 21:10:58 -07002655 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002656 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002657 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2658 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002659 }
2660 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002661 }
2662
Craig Mautner4a1cb222013-12-04 16:14:06 -08002663 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002664 ActivityRecord homeActivity = getHomeActivity();
2665 if (homeActivity != null) {
2666 return homeActivity.appToken;
2667 }
2668 return null;
2669 }
2670
2671 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002672 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002673 }
2674
2675 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002676 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2677 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2678 final TaskRecord task = tasks.get(taskNdx);
2679 if (task.isHomeTask()) {
2680 final ArrayList<ActivityRecord> activities = task.mActivities;
2681 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2682 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002683 if (r.isHomeActivity()
2684 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002685 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002686 }
2687 }
2688 }
2689 }
2690 return null;
2691 }
2692
Todd Kennedyca4d8422015-01-15 15:19:22 -08002693 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002694 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002695 ActivityContainer activityContainer =
2696 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002697 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002698 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2699 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002700 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002701 return activityContainer;
2702 }
2703
Craig Mautner34b73df2014-01-12 21:11:08 -08002704 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002705 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2706 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2707 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002708 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2709 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002710 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002711 }
2712 }
2713
Craig Mautner95da1082014-02-24 17:54:35 -08002714 void deleteActivityContainer(IActivityContainer container) {
2715 ActivityContainer activityContainer = (ActivityContainer)container;
2716 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002717 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2718 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08002719 final int stackId = activityContainer.mStackId;
2720 mActivityContainers.remove(stackId);
2721 mWindowManager.removeStack(stackId);
2722 }
2723 }
2724
Wale Ogunwale60454db2015-01-23 16:05:07 -08002725 void resizeStackLocked(int stackId, Rect bounds) {
2726 final ActivityStack stack = getStack(stackId);
2727 if (stack == null) {
2728 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2729 return;
2730 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002731
2732 final ActivityRecord r = stack.topRunningActivityLocked(null);
Wale Ogunwale0e162182015-02-05 08:48:34 -08002733 if (r != null && !r.task.mResizeable) {
2734 Slog.w(TAG, "resizeStack: top task " + r.task + " not resizeable.");
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002735 return;
2736 }
2737
Wale Ogunwale60454db2015-01-23 16:05:07 -08002738 final Configuration overrideConfig = mWindowManager.resizeStack(stackId, bounds);
2739 if (stack.updateOverrideConfiguration(overrideConfig)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08002740 if (r != null) {
2741 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0);
2742 // And we need to make sure at this point that all other activities
2743 // are made visible with the correct configuration.
2744 ensureActivitiesVisibleLocked(r, 0);
2745 if (!updated) {
2746 resumeTopActivitiesLocked(stack, null, null);
2747 }
2748 }
2749 }
2750 }
2751
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002752 /** Makes sure the input task is in a stack with the specified bounds by either resizing the
2753 * current task stack if it only has one entry, moving the task to a stack that matches the
2754 * bounds, or creating a new stack with the required bounds. Also, makes the task resizeable.*/
2755 void resizeTaskLocked(TaskRecord task, Rect bounds) {
2756 task.mResizeable = true;
2757 final ActivityStack currentStack = task.stack;
2758 if (currentStack.isHomeStack()) {
2759 // Can't move task off the home stack. Sorry!
2760 return;
2761 }
2762
2763 final int matchingStackId = mWindowManager.getStackIdWithBounds(bounds);
2764 if (matchingStackId != -1) {
2765 // There is already a stack with the right bounds!
2766 if (currentStack != null && currentStack.mStackId == matchingStackId) {
2767 // Nothing to do here. Already in the right stack...
2768 return;
2769 }
2770 // Move task to stack with matching bounds.
2771 moveTaskToStackLocked(task.taskId, matchingStackId, true);
2772 return;
2773 }
2774
2775 if (currentStack != null && currentStack.numTasks() == 1) {
2776 // Just resize the current stack since this is the task in it.
2777 resizeStackLocked(currentStack.mStackId, bounds);
2778 return;
2779 }
2780
2781 // Create new stack and move the task to it.
2782 final int displayId = (currentStack != null && currentStack.mDisplayId != -1)
2783 ? currentStack.mDisplayId : Display.DEFAULT_DISPLAY;
2784 ActivityStack newStack = createStackOnDisplay(getNextStackId(), displayId);
2785
2786 if (newStack == null) {
2787 Slog.e(TAG, "resizeTaskLocked: Can't create stack for task=" + task);
2788 return;
2789 }
2790 moveTaskToStackLocked(task.taskId, newStack.mStackId, true);
2791 resizeStackLocked(newStack.mStackId, bounds);
2792 }
2793
Todd Kennedy4900bf92015-01-16 16:05:14 -08002794 ActivityStack createStackOnDisplay(int stackId, int displayId) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002795 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2796 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002797 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002798 }
2799
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002800 ActivityContainer activityContainer = new ActivityContainer(stackId);
2801 mActivityContainers.put(stackId, activityContainer);
Craig Mautnere0a38842013-12-16 16:14:02 -08002802 activityContainer.attachToDisplayLocked(activityDisplay);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002803 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002804 }
2805
2806 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002807 while (true) {
2808 if (++mLastStackId <= HOME_STACK_ID) {
2809 mLastStackId = HOME_STACK_ID + 1;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002810 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002811 if (getStack(mLastStackId) == null) {
2812 break;
2813 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002814 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002815 return mLastStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002816 }
2817
Wale Ogunwale7de05352014-12-12 15:21:33 -08002818 private boolean restoreRecentTaskLocked(TaskRecord task) {
2819 ActivityStack stack = null;
2820 // Determine stack to restore task to.
2821 if (mLeanbackOnlyDevice) {
2822 // There is only one stack for lean back devices.
2823 stack = mHomeStack;
2824 } else {
2825 // Look for the top stack on the home display that isn't the home stack.
2826 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
2827 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
2828 final ActivityStack tmpStack = homeDisplayStacks.get(stackNdx);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07002829 if (!tmpStack.isHomeStack() && tmpStack.mFullscreen) {
Wale Ogunwale7de05352014-12-12 15:21:33 -08002830 stack = tmpStack;
2831 break;
2832 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002833 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002834 }
Wale Ogunwale7de05352014-12-12 15:21:33 -08002835
2836 if (stack == null) {
2837 // We couldn't find a stack to restore the task to. Possible if are restoring recents
2838 // before an application stack is created...Go ahead and create one on the default
2839 // display.
Todd Kennedy4900bf92015-01-16 16:05:14 -08002840 stack = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002841 // Restore home stack to top.
Craig Mautner299f9602015-01-26 09:47:33 -08002842 moveHomeStack(true, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002843 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2844 "Created stack=" + stack + " for recents restoration.");
Wale Ogunwale7de05352014-12-12 15:21:33 -08002845 }
2846
2847 if (stack == null) {
2848 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002849 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2850 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002851 return false;
2852 }
2853
2854 stack.addTask(task, false, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002855 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2856 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002857 final ArrayList<ActivityRecord> activities = task.mActivities;
2858 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2859 final ActivityRecord r = activities.get(activityNdx);
2860 mWindowManager.addAppToken(0, r.appToken, task.taskId, stack.mStackId,
2861 r.info.screenOrientation, r.fullscreen,
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002862 (r.info.flags & ActivityInfo.FLAG_SHOW_FOR_ALL_USERS) != 0,
Wale Ogunwale7de05352014-12-12 15:21:33 -08002863 r.userId, r.info.configChanges, task.voiceSession != null,
2864 r.mLaunchTaskBehind);
2865 }
2866 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002867 }
2868
Craig Mautner299f9602015-01-26 09:47:33 -08002869 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002870 final TaskRecord task = anyTaskForIdLocked(taskId);
2871 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002872 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002873 return;
2874 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002875 final ActivityStack stack = getStack(stackId);
2876 if (stack == null) {
2877 Slog.w(TAG, "moveTaskToStack: no stack for id=" + stackId);
2878 return;
2879 }
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002880 mWindowManager.moveTaskToStack(taskId, stackId, toTop);
2881 if (task.stack != null) {
Wale Ogunwale000957c2015-04-03 08:19:12 -07002882 task.stack.removeTask(task, "moveTaskToStack", false /* notMoving */);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002883 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07002884 stack.addTask(task, toTop, true);
Stefan Kuhne54714cd2015-05-12 13:42:18 -07002885 // The task might have already been running and its visibility needs to be synchronized with
2886 // the visibility of the stack / windows.
2887 stack.ensureActivitiesVisibleLocked(null, 0);
Craig Mautner05d29032013-05-03 13:40:13 -07002888 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002889 }
2890
Craig Mautnerac6f8432013-07-17 13:24:59 -07002891 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002892 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002893 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2894 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002895 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2896 final ActivityStack stack = stacks.get(stackNdx);
2897 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002898 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002899 continue;
2900 }
2901 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002902 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2903 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002904 continue;
2905 }
2906 final ActivityRecord ar = stack.findTaskLocked(r);
2907 if (ar != null) {
2908 return ar;
2909 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002910 }
2911 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002912 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07002913 return null;
2914 }
2915
2916 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002917 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2918 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002919 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2920 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2921 if (ar != null) {
2922 return ar;
2923 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002924 }
2925 }
2926 return null;
2927 }
2928
Craig Mautner8d341ef2013-03-26 09:03:27 -07002929 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002930 scheduleSleepTimeout();
2931 if (!mGoingToSleep.isHeld()) {
2932 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002933 if (mLaunchingActivity.isHeld()) {
2934 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2935 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002936 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002937 mLaunchingActivity.release();
2938 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002939 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002940 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002941 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002942 }
2943
2944 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002945 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002946
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002947 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002948 final long endTime = System.currentTimeMillis() + timeout;
2949 while (true) {
2950 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002951 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2952 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002953 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2954 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2955 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002956 }
2957 if (cantShutdown) {
2958 long timeRemaining = endTime - System.currentTimeMillis();
2959 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002960 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002961 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002962 } catch (InterruptedException e) {
2963 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002964 } else {
2965 Slog.w(TAG, "Activity manager shutdown timed out");
2966 timedout = true;
2967 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002968 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002969 } else {
2970 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002971 }
2972 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002973
2974 // Force checkReadyForSleep to complete.
2975 mSleepTimeout = true;
2976 checkReadyForSleepLocked();
2977
Craig Mautner8d341ef2013-03-26 09:03:27 -07002978 return timedout;
2979 }
2980
2981 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002982 removeSleepTimeouts();
2983 if (mGoingToSleep.isHeld()) {
2984 mGoingToSleep.release();
2985 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002986 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2987 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002988 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2989 final ActivityStack stack = stacks.get(stackNdx);
2990 stack.awakeFromSleepingLocked();
2991 if (isFrontStack(stack)) {
2992 resumeTopActivitiesLocked();
2993 }
Craig Mautner5314a402013-09-26 12:40:16 -07002994 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002995 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002996 mGoingToSleepActivities.clear();
2997 }
2998
2999 void activitySleptLocked(ActivityRecord r) {
3000 mGoingToSleepActivities.remove(r);
3001 checkReadyForSleepLocked();
3002 }
3003
3004 void checkReadyForSleepLocked() {
3005 if (!mService.isSleepingOrShuttingDown()) {
3006 // Do not care.
3007 return;
3008 }
3009
3010 if (!mSleepTimeout) {
3011 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003012 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3013 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003014 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3015 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3016 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003017 }
3018
3019 if (mStoppingActivities.size() > 0) {
3020 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003021 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003022 + mStoppingActivities.size() + " activities");
3023 scheduleIdleLocked();
3024 dontSleep = true;
3025 }
3026
3027 if (mGoingToSleepActivities.size() > 0) {
3028 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003029 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003030 + mGoingToSleepActivities.size() + " activities");
3031 dontSleep = true;
3032 }
3033
3034 if (dontSleep) {
3035 return;
3036 }
3037 }
3038
Craig Mautnere0a38842013-12-16 16:14:02 -08003039 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3040 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003041 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3042 stacks.get(stackNdx).goToSleep();
3043 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003044 }
3045
3046 removeSleepTimeouts();
3047
3048 if (mGoingToSleep.isHeld()) {
3049 mGoingToSleep.release();
3050 }
3051 if (mService.mShuttingDown) {
3052 mService.notifyAll();
3053 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003054 }
3055
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003056 boolean reportResumedActivityLocked(ActivityRecord r) {
3057 final ActivityStack stack = r.task.stack;
3058 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003059 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003060 }
3061 if (allResumedActivitiesComplete()) {
3062 ensureActivitiesVisibleLocked(null, 0);
3063 mWindowManager.executeAppTransition();
3064 return true;
3065 }
3066 return false;
3067 }
3068
Craig Mautner8d341ef2013-03-26 09:03:27 -07003069 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003070 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3071 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003072 final int numStacks = stacks.size();
3073 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3074 final ActivityStack stack = stacks.get(stackNdx);
3075 stack.handleAppCrashLocked(app);
3076 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003077 }
3078 }
3079
Jose Lima4b6c6692014-08-12 17:41:12 -07003080 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003081 final ActivityStack stack = r.task.stack;
3082 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003083 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3084 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003085 return false;
3086 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003087 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003088 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3089 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003090
3091 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003092 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003093 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003094 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003095 return true;
3096 }
3097
3098 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003099 if (visible && top.fullscreen) {
3100 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003101 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3102 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3103 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3104 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003105 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003106 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3107 // Only the activity set as currently visible behind should actively reset its
3108 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003109 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3110 "requestVisibleBehind: returning visible=" + visible
3111 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3112 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003113 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003114 }
3115
Jose Lima4b6c6692014-08-12 17:41:12 -07003116 stack.setVisibleBehindActivity(visible ? r : null);
3117 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003118 // Make the activity immediately above r opaque.
3119 final ActivityRecord next = stack.findNextTranslucentActivity(r);
3120 if (next != null) {
3121 mService.convertFromTranslucent(next.appToken);
3122 }
3123 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003124 if (top.app != null && top.app.thread != null) {
3125 // Notify the top app of the change.
3126 try {
3127 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3128 } catch (RemoteException e) {
3129 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003130 }
3131 return true;
3132 }
3133
Craig Mautnerbb742462014-07-07 15:28:55 -07003134 // Called when WindowManager has finished animating the launchingBehind activity to the back.
3135 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
3136 r.mLaunchTaskBehind = false;
3137 final TaskRecord task = r.task;
3138 task.setLastThumbnail(task.stack.screenshotActivities(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003139 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08003140 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07003141 mWindowManager.setAppVisibility(r.appToken, false);
3142 }
3143
3144 void scheduleLaunchTaskBehindComplete(IBinder token) {
3145 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3146 }
3147
Craig Mautnerde4ef022013-04-07 19:01:33 -07003148 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
Craig Mautner580ea812013-04-25 12:58:38 -07003149 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08003150 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3151 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003152 final int topStackNdx = stacks.size() - 1;
3153 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3154 final ActivityStack stack = stacks.get(stackNdx);
Jose Lima729cb232014-05-05 15:59:40 -07003155 stack.ensureActivitiesVisibleLocked(starting, configChanges);
Craig Mautner580ea812013-04-25 12:58:38 -07003156 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003157 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003158 }
3159
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003160 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3161 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3162 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3163 final int topStackNdx = stacks.size() - 1;
3164 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3165 final ActivityStack stack = stacks.get(stackNdx);
3166 stack.clearOtherAppTimeTrackers(except);
3167 }
3168 }
3169 }
3170
Craig Mautner8d341ef2013-03-26 09:03:27 -07003171 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003172 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3173 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003174 final int numStacks = stacks.size();
3175 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3176 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003177 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003178 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003179 }
3180 }
3181
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003182 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3183 // Examine all activities currently running in the process.
3184 TaskRecord firstTask = null;
3185 // Tasks is non-null only if two or more tasks are found.
3186 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003187 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3188 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003189 ActivityRecord r = app.activities.get(i);
3190 // First, if we find an activity that is in the process of being destroyed,
3191 // then we just aren't going to do anything for now; we want things to settle
3192 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003193 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003194 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003195 return;
3196 }
3197 // Don't consider any activies that are currently not in a state where they
3198 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003199 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3200 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003201 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003202 continue;
3203 }
3204 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003205 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003206 + " from " + r);
3207 if (firstTask == null) {
3208 firstTask = r.task;
3209 } else if (firstTask != r.task) {
3210 if (tasks == null) {
3211 tasks = new ArraySet<>();
3212 tasks.add(firstTask);
3213 }
3214 tasks.add(r.task);
3215 }
3216 }
3217 }
3218 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003219 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003220 return;
3221 }
3222 // If we have activities in multiple tasks that are in a position to be destroyed,
3223 // let's iterate through the tasks and release the oldest one.
3224 final int numDisplays = mActivityDisplays.size();
3225 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3226 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3227 // Step through all stacks starting from behind, to hit the oldest things first.
3228 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3229 final ActivityStack stack = stacks.get(stackNdx);
3230 // Try to release activities in this stack; if we manage to, we are done.
3231 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3232 return;
3233 }
3234 }
3235 }
3236 }
3237
Craig Mautner8d341ef2013-03-26 09:03:27 -07003238 boolean switchUserLocked(int userId, UserStartedState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003239 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003240 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003241 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003242
Craig Mautner858d8a62013-04-23 17:08:34 -07003243 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003244 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3245 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003246 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003247 final ActivityStack stack = stacks.get(stackNdx);
3248 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003249 TaskRecord task = stack.topTask();
3250 if (task != null) {
3251 mWindowManager.moveTaskToTop(task.taskId);
3252 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003253 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003254 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003255
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003256 ActivityStack stack = getStack(restoreStackId);
3257 if (stack == null) {
3258 stack = mHomeStack;
3259 }
3260 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003261 if (stack.isOnHomeDisplay()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003262 moveHomeStack(homeInFront, "switchUserOnHomeDisplay");
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003263 TaskRecord task = stack.topTask();
3264 if (task != null) {
3265 mWindowManager.moveTaskToTop(task.taskId);
3266 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003267 } else {
3268 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003269 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003270 }
Craig Mautner93529a42013-10-04 15:03:13 -07003271 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003272 }
3273
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003274 /**
3275 * Add background users to send boot completed events to.
3276 * @param userId The user being started in the background
3277 * @param uss The state object for the user.
3278 */
3279 public void startBackgroundUserLocked(int userId, UserStartedState uss) {
3280 mStartingBackgroundUsers.add(uss);
3281 }
3282
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003283 /** Checks whether the userid is a profile of the current user. */
3284 boolean isCurrentProfileLocked(int userId) {
3285 if (userId == mCurrentUser) return true;
3286 for (int i = 0; i < mService.mCurrentProfileIds.length; i++) {
3287 if (mService.mCurrentProfileIds[i] == userId) return true;
3288 }
3289 return false;
3290 }
3291
Craig Mautnerde4ef022013-04-07 19:01:33 -07003292 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003293 ArrayList<ActivityRecord> stops = null;
3294
3295 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003296 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3297 ActivityRecord s = mStoppingActivities.get(activityNdx);
3298 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003299 if (DEBUG_ALL) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003300 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3301 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003302 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003303 if (s.finishing) {
3304 // If this activity is finishing, it is sitting on top of
3305 // everyone else but we now know it is no longer needed...
3306 // so get rid of it. Otherwise, we need to go through the
3307 // normal flow and hide it once we determine that it is
3308 // hidden by the activities in front of it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003309 if (DEBUG_ALL) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003310 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003311 }
3312 }
Craig Mautner8c14c152015-01-15 17:32:07 -08003313 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003314 if (DEBUG_ALL) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003315 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003316 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003317 }
3318 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003319 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003320 }
3321 }
3322
3323 return stops;
3324 }
3325
Craig Mautnercf910b02013-04-23 11:23:27 -07003326 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003327 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3328 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3329 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3330 final ActivityStack stack = stacks.get(stackNdx);
3331 final ActivityRecord r = stack.topRunningActivityLocked(null);
3332 final ActivityState state = r == null ? DESTROYED : r.state;
3333 if (isFrontStack(stack)) {
3334 if (r == null) Slog.e(TAG,
3335 "validateTop...: null top activity, stack=" + stack);
3336 else {
3337 final ActivityRecord pausing = stack.mPausingActivity;
3338 if (pausing != null && pausing == r) Slog.e(TAG,
3339 "validateTop...: top stack has pausing activity r=" + r
3340 + " state=" + state);
3341 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3342 "validateTop...: activity in front not resumed r=" + r
3343 + " state=" + state);
3344 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003345 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003346 final ActivityRecord resumed = stack.mResumedActivity;
3347 if (resumed != null && resumed == r) Slog.e(TAG,
3348 "validateTop...: back stack has resumed activity r=" + r
3349 + " state=" + state);
3350 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3351 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003352 }
3353 }
3354 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003355 }
3356
Craig Mautnere0570202015-05-13 13:06:11 -07003357 private String lockTaskModeToString() {
3358 switch (mLockTaskModeState) {
3359 case LOCK_TASK_MODE_LOCKED:
3360 return "LOCKED";
3361 case LOCK_TASK_MODE_PINNED:
3362 return "PINNED";
3363 case LOCK_TASK_MODE_NONE:
3364 return "NONE";
3365 default: return "unknown=" + mLockTaskModeState;
3366 }
3367 }
3368
Craig Mautner27084302013-03-25 08:05:25 -07003369 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003370 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003371 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003372 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
3373 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
3374 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003375 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003376 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
3377 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3378 if (packages.size() > 0) {
3379 pw.println(" mLockTaskPackages (userId:packages)=");
3380 for (int i = 0; i < packages.size(); ++i) {
3381 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3382 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3383 }
3384 }
3385 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003386 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003387
Craig Mautner20e72272013-04-01 13:45:53 -07003388 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003389 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003390 }
3391
Dianne Hackborn390517b2013-05-30 15:03:32 -07003392 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3393 boolean needSep, String prefix) {
3394 if (activity != null) {
3395 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3396 if (needSep) {
3397 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003398 }
3399 pw.print(prefix);
3400 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003401 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003402 }
3403 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003404 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003405 }
3406
Craig Mautner8d341ef2013-03-26 09:03:27 -07003407 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3408 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003409 boolean printed = false;
3410 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003411 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3412 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003413 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003414 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003415 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003416 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003417 final ActivityStack stack = stacks.get(stackNdx);
3418 StringBuilder stackHeader = new StringBuilder(128);
3419 stackHeader.append(" Stack #");
3420 stackHeader.append(stack.mStackId);
3421 stackHeader.append(":");
3422 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3423 needSep, stackHeader.toString());
3424 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3425 !dumpAll, false, dumpPackage, true,
3426 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003427
Craig Mautner4a1cb222013-12-04 16:14:06 -08003428 needSep = printed;
3429 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3430 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003431 if (pr) {
3432 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003433 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003434 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003435 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3436 " mResumedActivity: ");
3437 if (pr) {
3438 printed = true;
3439 needSep = false;
3440 }
3441 if (dumpAll) {
3442 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3443 " mLastPausedActivity: ");
3444 if (pr) {
3445 printed = true;
3446 needSep = true;
3447 }
3448 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3449 needSep, " mLastNoHistoryActivity: ");
3450 }
3451 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003452 }
3453 }
3454
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003455 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3456 false, dumpPackage, true, " Activities waiting to finish:", null);
3457 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3458 false, dumpPackage, true, " Activities waiting to stop:", null);
3459 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3460 false, dumpPackage, true, " Activities waiting for another to become visible:",
3461 null);
3462 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3463 false, dumpPackage, true, " Activities waiting to sleep:", null);
3464 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3465 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003466
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003467 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003468 }
3469
Dianne Hackborn390517b2013-05-30 15:03:32 -07003470 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003471 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003472 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003473 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003474 String innerPrefix = null;
3475 String[] args = null;
3476 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003477 for (int i=list.size()-1; i>=0; i--) {
3478 final ActivityRecord r = list.get(i);
3479 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3480 continue;
3481 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003482 if (innerPrefix == null) {
3483 innerPrefix = prefix + " ";
3484 args = new String[0];
3485 }
3486 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003487 final boolean full = !brief && (complete || !r.isInHistory());
3488 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003489 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003490 needNL = false;
3491 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003492 if (header1 != null) {
3493 pw.println(header1);
3494 header1 = null;
3495 }
3496 if (header2 != null) {
3497 pw.println(header2);
3498 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003499 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003500 if (lastTask != r.task) {
3501 lastTask = r.task;
3502 pw.print(prefix);
3503 pw.print(full ? "* " : " ");
3504 pw.println(lastTask);
3505 if (full) {
3506 lastTask.dump(pw, prefix + " ");
3507 } else if (complete) {
3508 // Complete + brief == give a summary. Isn't that obvious?!?
3509 if (lastTask.intent != null) {
3510 pw.print(prefix); pw.print(" ");
3511 pw.println(lastTask.intent.toInsecureStringWithClip());
3512 }
3513 }
3514 }
3515 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3516 pw.print(" #"); pw.print(i); pw.print(": ");
3517 pw.println(r);
3518 if (full) {
3519 r.dump(pw, innerPrefix);
3520 } else if (complete) {
3521 // Complete + brief == give a summary. Isn't that obvious?!?
3522 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3523 if (r.app != null) {
3524 pw.print(innerPrefix); pw.println(r.app);
3525 }
3526 }
3527 if (client && r.app != null && r.app.thread != null) {
3528 // flush anything that is already in the PrintWriter since the thread is going
3529 // to write to the file descriptor directly
3530 pw.flush();
3531 try {
3532 TransferPipe tp = new TransferPipe();
3533 try {
3534 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3535 r.appToken, innerPrefix, args);
3536 // Short timeout, since blocking here can
3537 // deadlock with the application.
3538 tp.go(fd, 2000);
3539 } finally {
3540 tp.kill();
3541 }
3542 } catch (IOException e) {
3543 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3544 } catch (RemoteException e) {
3545 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3546 }
3547 needNL = true;
3548 }
3549 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003550 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003551 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003552
Craig Mautnerf3333272013-04-22 10:55:53 -07003553 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003554 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3555 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003556 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3557 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003558 }
3559
3560 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003561 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003562 }
3563
3564 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003565 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3566 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003567 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3568 }
3569
Craig Mautner05d29032013-05-03 13:40:13 -07003570 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003571 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3572 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3573 }
Craig Mautner05d29032013-05-03 13:40:13 -07003574 }
3575
Craig Mautner0eea92c2013-05-16 13:35:39 -07003576 void removeSleepTimeouts() {
3577 mSleepTimeout = false;
3578 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3579 }
3580
3581 final void scheduleSleepTimeout() {
3582 removeSleepTimeouts();
3583 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3584 }
3585
Craig Mautner4a1cb222013-12-04 16:14:06 -08003586 @Override
3587 public void onDisplayAdded(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003588 Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003589 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3590 }
3591
3592 @Override
3593 public void onDisplayRemoved(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003594 Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003595 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3596 }
3597
3598 @Override
3599 public void onDisplayChanged(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003600 Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003601 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3602 }
3603
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003604 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003605 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003606 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003607 newDisplay = mActivityDisplays.get(displayId) == null;
3608 if (newDisplay) {
3609 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003610 if (activityDisplay.mDisplay == null) {
3611 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3612 return;
3613 }
Craig Mautner4504de52013-12-20 09:06:56 -08003614 mActivityDisplays.put(displayId, activityDisplay);
3615 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003616 }
Craig Mautner4504de52013-12-20 09:06:56 -08003617 if (newDisplay) {
3618 mWindowManager.onDisplayAdded(displayId);
3619 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003620 }
3621
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003622 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003623 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003624 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3625 if (activityDisplay != null) {
3626 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003627 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003628 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003629 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003630 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003631 }
3632 }
3633 mWindowManager.onDisplayRemoved(displayId);
3634 }
3635
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003636 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003637 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003638 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3639 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003640 // TODO: Update the bounds.
3641 }
3642 }
3643 mWindowManager.onDisplayChanged(displayId);
3644 }
3645
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003646 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003647 StackInfo info = new StackInfo();
3648 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3649 info.displayId = Display.DEFAULT_DISPLAY;
3650 info.stackId = stack.mStackId;
3651
3652 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3653 final int numTasks = tasks.size();
3654 int[] taskIds = new int[numTasks];
3655 String[] taskNames = new String[numTasks];
3656 for (int i = 0; i < numTasks; ++i) {
3657 final TaskRecord task = tasks.get(i);
3658 taskIds[i] = task.taskId;
3659 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3660 : task.realActivity != null ? task.realActivity.flattenToString()
3661 : task.getTopActivity() != null ? task.getTopActivity().packageName
3662 : "unknown";
3663 }
3664 info.taskIds = taskIds;
3665 info.taskNames = taskNames;
3666 return info;
3667 }
3668
3669 StackInfo getStackInfoLocked(int stackId) {
3670 ActivityStack stack = getStack(stackId);
3671 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003672 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003673 }
3674 return null;
3675 }
3676
3677 ArrayList<StackInfo> getAllStackInfosLocked() {
3678 ArrayList<StackInfo> list = new ArrayList<StackInfo>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003679 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3680 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003681 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003682 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003683 }
3684 }
3685 return list;
3686 }
3687
Craig Mautner15df08a2015-04-01 12:17:18 -07003688 TaskRecord getLockedTaskLocked() {
3689 final int top = mLockTaskModeTasks.size() - 1;
3690 if (top >= 0) {
3691 return mLockTaskModeTasks.get(top);
3692 }
3693 return null;
3694 }
3695
3696 boolean isLockedTask(TaskRecord task) {
3697 return mLockTaskModeTasks.contains(task);
3698 }
3699
3700 boolean isLastLockedTask(TaskRecord task) {
3701 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3702 }
3703
3704 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003705 if (!mLockTaskModeTasks.remove(task)) {
3706 return;
3707 }
3708 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3709 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003710 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003711 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3712 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003713 final Message lockTaskMsg = Message.obtain();
3714 lockTaskMsg.arg1 = task.userId;
3715 lockTaskMsg.what = LOCK_TASK_END_MSG;
3716 mHandler.sendMessage(lockTaskMsg);
3717 }
3718 }
3719
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003720 void showLockTaskToast() {
3721 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04003722 }
3723
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003724 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3725 if (mLockTaskModeTasks.contains(task)) {
3726 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3727 }
3728 }
3729
Craig Mautner432f64e2015-05-20 14:59:57 -07003730 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3731 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003732 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003733 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003734 final TaskRecord lockedTask = getLockedTaskLocked();
3735 if (lockedTask != null) {
3736 removeLockedTaskLocked(lockedTask);
3737 if (!mLockTaskModeTasks.isEmpty()) {
3738 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003739 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3740 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003741 lockedTask.performClearTaskLocked();
3742 resumeTopActivitiesLocked();
3743 return;
3744 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003745 }
Craig Mautnere0570202015-05-13 13:06:11 -07003746 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3747 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003748 return;
3749 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003750
3751 // Should have already been checked, but do it again.
3752 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003753 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3754 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003755 return;
3756 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003757 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003758 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003759 return;
3760 }
3761
3762 if (mLockTaskModeTasks.isEmpty()) {
3763 // First locktask.
3764 final Message lockTaskMsg = Message.obtain();
3765 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3766 lockTaskMsg.arg1 = task.userId;
3767 lockTaskMsg.what = LOCK_TASK_START_MSG;
3768 lockTaskMsg.arg2 = lockTaskModeState;
3769 mHandler.sendMessage(lockTaskMsg);
3770 }
3771 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003772 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3773 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003774 mLockTaskModeTasks.remove(task);
3775 mLockTaskModeTasks.add(task);
3776
3777 if (task.mLockTaskUid == -1) {
3778 task.mLockTaskUid = task.mCallingUid;
3779 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003780
3781 if (andResume) {
3782 findTaskToMoveToFrontLocked(task, 0, null, reason);
3783 resumeTopActivitiesLocked();
3784 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003785 }
3786
3787 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003788 return isLockTaskModeViolation(task, false);
3789 }
3790
3791 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3792 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003793 return false;
3794 }
3795 final int lockTaskAuth = task.mLockTaskAuth;
3796 switch (lockTaskAuth) {
3797 case LOCK_TASK_AUTH_DONT_LOCK:
3798 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003799 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003800 case LOCK_TASK_AUTH_LAUNCHABLE:
3801 case LOCK_TASK_AUTH_WHITELISTED:
3802 return false;
3803 case LOCK_TASK_AUTH_PINNABLE:
3804 // Pinnable tasks can't be launched on top of locktask tasks.
3805 return !mLockTaskModeTasks.isEmpty();
3806 default:
3807 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3808 return true;
3809 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003810 }
3811
Craig Mautner15df08a2015-04-01 12:17:18 -07003812 void onLockTaskPackagesUpdatedLocked() {
3813 boolean didSomething = false;
3814 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3815 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01003816 final boolean wasWhitelisted =
3817 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3818 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07003819 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01003820 final boolean isWhitelisted =
3821 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3822 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3823 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003824 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003825 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3826 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003827 removeLockedTaskLocked(lockedTask);
3828 lockedTask.performClearTaskLocked();
3829 didSomething = true;
3830 }
3831 }
3832 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3833 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3834 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3835 final ActivityStack stack = stacks.get(stackNdx);
3836 stack.onLockTaskPackagesUpdatedLocked();
3837 }
3838 }
Craig Mautnere0570202015-05-13 13:06:11 -07003839 final ActivityRecord r = topRunningActivityLocked();
3840 final TaskRecord task = r != null ? r.task : null;
3841 if (mLockTaskModeTasks.isEmpty() && task != null
3842 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3843 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07003844 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
3845 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
3846 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
3847 false);
3848 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07003849 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003850 if (didSomething) {
3851 resumeTopActivitiesLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003852 }
3853 }
3854
Benjamin Franz43261142015-02-11 15:59:44 +00003855 int getLockTaskModeState() {
3856 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003857 }
3858
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003859 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003860
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003861 public ActivityStackSupervisorHandler(Looper looper) {
3862 super(looper);
3863 }
3864
Craig Mautnerf3333272013-04-22 10:55:53 -07003865 void activityIdleInternal(ActivityRecord r) {
3866 synchronized (mService) {
3867 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3868 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003869 }
3870
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003871 @Override
3872 public void handleMessage(Message msg) {
3873 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003874 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003875 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3876 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003877 if (mService.mDidDexOpt) {
3878 mService.mDidDexOpt = false;
3879 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3880 nmsg.obj = msg.obj;
3881 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3882 return;
3883 }
3884 // We don't at this point know if the activity is fullscreen,
3885 // so we need to be conservative and assume it isn't.
3886 activityIdleInternal((ActivityRecord)msg.obj);
3887 } break;
3888 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003889 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003890 activityIdleInternal((ActivityRecord)msg.obj);
3891 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003892 case RESUME_TOP_ACTIVITY_MSG: {
3893 synchronized (mService) {
3894 resumeTopActivitiesLocked();
3895 }
3896 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003897 case SLEEP_TIMEOUT_MSG: {
3898 synchronized (mService) {
3899 if (mService.isSleepingOrShuttingDown()) {
3900 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3901 mSleepTimeout = true;
3902 checkReadyForSleepLocked();
3903 }
3904 }
3905 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003906 case LAUNCH_TIMEOUT_MSG: {
3907 if (mService.mDidDexOpt) {
3908 mService.mDidDexOpt = false;
3909 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3910 return;
3911 }
3912 synchronized (mService) {
3913 if (mLaunchingActivity.isHeld()) {
3914 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3915 if (VALIDATE_WAKE_LOCK_CALLER
3916 && Binder.getCallingUid() != Process.myUid()) {
3917 throw new IllegalStateException("Calling must be system uid");
3918 }
3919 mLaunchingActivity.release();
3920 }
3921 }
3922 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003923 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003924 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003925 } break;
3926 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003927 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003928 } break;
3929 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003930 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003931 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003932 case CONTAINER_CALLBACK_VISIBILITY: {
3933 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003934 final IActivityContainerCallback callback = container.mCallback;
3935 if (callback != null) {
3936 try {
3937 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3938 } catch (RemoteException e) {
3939 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003940 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003941 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003942 case LOCK_TASK_START_MSG: {
3943 // When lock task starts, we disable the status bars.
3944 try {
Jason Monk62515be2014-05-21 16:06:19 -04003945 if (mLockTaskNotify == null) {
3946 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003947 }
Jason Monk62515be2014-05-21 16:06:19 -04003948 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003949 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003950 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003951 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003952 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003953 flags = StatusBarManager.DISABLE_MASK
3954 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07003955 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003956 flags = StatusBarManager.DISABLE_MASK
3957 & (~StatusBarManager.DISABLE_BACK)
3958 & (~StatusBarManager.DISABLE_HOME)
3959 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003960 }
3961 getStatusBarService().disable(flags, mToken,
3962 mService.mContext.getPackageName());
3963 }
3964 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003965 if (getDevicePolicyManager() != null) {
3966 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003967 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003968 }
justinzhang5286d3f2014-05-12 17:06:01 -04003969 } catch (RemoteException ex) {
3970 throw new RuntimeException(ex);
3971 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003972 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003973 case LOCK_TASK_END_MSG: {
3974 // When lock task ends, we enable the status bars.
3975 try {
Jason Monk62515be2014-05-21 16:06:19 -04003976 if (getStatusBarService() != null) {
3977 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3978 mService.mContext.getPackageName());
3979 }
3980 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003981 if (getDevicePolicyManager() != null) {
3982 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3983 msg.arg1);
3984 }
Jason Monk62515be2014-05-21 16:06:19 -04003985 if (mLockTaskNotify == null) {
3986 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3987 }
3988 mLockTaskNotify.show(false);
3989 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003990 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003991 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003992 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003993 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003994 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003995 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003996 new LockPatternUtils(mService.mContext)
3997 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003998 }
3999 } catch (SettingNotFoundException e) {
4000 // No setting, don't lock.
4001 }
justinzhang5286d3f2014-05-12 17:06:01 -04004002 } catch (RemoteException ex) {
4003 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004004 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004005 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004006 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004007 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004008 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4009 if (mLockTaskNotify == null) {
4010 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4011 }
4012 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4013 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004014 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4015 final ActivityContainer container = (ActivityContainer) msg.obj;
4016 final IActivityContainerCallback callback = container.mCallback;
4017 if (callback != null) {
4018 try {
4019 callback.onAllActivitiesComplete(container.asBinder());
4020 } catch (RemoteException e) {
4021 }
4022 }
4023 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004024 case LAUNCH_TASK_BEHIND_COMPLETE: {
4025 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004026 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004027 if (r != null) {
4028 handleLaunchTaskBehindCompleteLocked(r);
4029 }
4030 }
4031 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004032 }
4033 }
4034 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004035
Ying Wangb081a592014-04-22 15:20:16 -07004036 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07004037 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07004038 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004039 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004040 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004041 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004042 ActivityRecord mParentActivity = null;
4043 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004044
Craig Mautnere3a00d72014-04-16 08:31:19 -07004045 boolean mVisible = true;
4046
Craig Mautner4a1cb222013-12-04 16:14:06 -08004047 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004048 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004049
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004050 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4051 final static int CONTAINER_STATE_NO_SURFACE = 1;
4052 final static int CONTAINER_STATE_FINISHING = 2;
4053 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4054
4055 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004056 synchronized (mService) {
4057 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004058 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004059 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004060 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004061 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004062 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004063
Craig Mautnere0a38842013-12-16 16:14:02 -08004064 void attachToDisplayLocked(ActivityDisplay activityDisplay) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004065 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Craig Mautner34b73df2014-01-12 21:11:08 -08004066 + " to display=" + activityDisplay);
Craig Mautnere0a38842013-12-16 16:14:02 -08004067 mActivityDisplay = activityDisplay;
4068 mStack.mDisplayId = activityDisplay.mDisplayId;
4069 mStack.mStacks = activityDisplay.mStacks;
4070
4071 activityDisplay.attachActivities(mStack);
Craig Mautnerdf88d732014-01-27 09:21:32 -08004072 mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004073 }
4074
4075 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004076 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004077 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004078 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4079 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004080 return;
4081 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004082 attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004083 }
4084 }
4085
4086 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004087 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004088 synchronized (mService) {
4089 if (mActivityDisplay != null) {
4090 return mActivityDisplay.mDisplayId;
4091 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004092 }
4093 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004094 }
4095
Jeff Brownca9bc702014-02-11 14:32:56 -08004096 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004097 public int getStackId() {
4098 synchronized (mService) {
4099 return mStackId;
4100 }
4101 }
4102
4103 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004104 public boolean injectEvent(InputEvent event) {
4105 final long origId = Binder.clearCallingIdentity();
4106 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004107 synchronized (mService) {
4108 if (mActivityDisplay != null) {
4109 return mInputManagerInternal.injectInputEvent(event,
4110 mActivityDisplay.mDisplayId,
4111 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4112 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004113 }
4114 return false;
4115 } finally {
4116 Binder.restoreCallingIdentity(origId);
4117 }
4118 }
4119
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004120 @Override
4121 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004122 synchronized (mService) {
4123 if (mContainerState == CONTAINER_STATE_FINISHING) {
4124 return;
4125 }
4126 mContainerState = CONTAINER_STATE_FINISHING;
4127
Craig Mautnerd163e752014-06-13 17:18:47 -07004128 long origId = Binder.clearCallingIdentity();
4129 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004130 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07004131 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004132 } finally {
4133 Binder.restoreCallingIdentity(origId);
4134 }
4135 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004136 }
4137
Craig Mautner60257702014-09-17 15:02:33 -07004138 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004139 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004140 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004141 if (mActivityDisplay != null) {
4142 mActivityDisplay.detachActivitiesLocked(mStack);
4143 mActivityDisplay = null;
4144 mStack.mDisplayId = -1;
4145 mStack.mStacks = null;
Craig Mautnerdf88d732014-01-27 09:21:32 -08004146 mWindowManager.detachStack(mStackId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004147 }
4148 }
4149
4150 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004151 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004152 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnerb9168362015-02-26 20:40:19 -08004153 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004154 Binder.getCallingUid(), mCurrentUser, false,
4155 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004156
Craig Mautnere0a38842013-12-16 16:14:02 -08004157 // TODO: Switch to user app stacks here.
4158 String mimeType = intent.getType();
Craig Mautnerb9168362015-02-26 20:40:19 -08004159 final Uri data = intent.getData();
4160 if (mimeType == null && data != null && "content".equals(data.getScheme())) {
4161 mimeType = mService.getProviderMimeType(data, userId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004162 }
Craig Mautnerb9168362015-02-26 20:40:19 -08004163 checkEmbeddedAllowedInner(userId, intent, mimeType);
4164
4165 intent.addFlags(FORCE_NEW_TASK_FLAGS);
4166 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null,
4167 0, 0, null, null, null, null, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004168 }
4169
4170 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004171 public final int startActivityIntentSender(IIntentSender intentSender)
4172 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004173 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4174
4175 if (!(intentSender instanceof PendingIntentRecord)) {
4176 throw new IllegalArgumentException("Bad PendingIntent object");
4177 }
4178
Craig Mautnerb9168362015-02-26 20:40:19 -08004179 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004180 Binder.getCallingUid(), mCurrentUser, false,
4181 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004182
4183 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4184 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4185 pendingIntent.key.requestResolvedType);
4186
4187 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4188 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4189 }
4190
4191 private void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004192 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004193 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004194 throw new SecurityException(
4195 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4196 }
4197 }
4198
Craig Mautnerdf88d732014-01-27 09:21:32 -08004199 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004200 public IBinder asBinder() {
4201 return this;
4202 }
4203
Craig Mautner4504de52013-12-20 09:06:56 -08004204 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004205 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004206 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004207 }
4208
Craig Mautner4a1cb222013-12-04 16:14:06 -08004209 ActivityStackSupervisor getOuter() {
4210 return ActivityStackSupervisor.this;
4211 }
4212
Craig Mautnerd163e752014-06-13 17:18:47 -07004213 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004214 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004215 }
4216
4217 void getBounds(Point outBounds) {
Craig Mautnerd163e752014-06-13 17:18:47 -07004218 synchronized (mService) {
4219 if (mActivityDisplay != null) {
4220 mActivityDisplay.getBounds(outBounds);
4221 } else {
4222 outBounds.set(0, 0);
4223 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004224 }
4225 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004226
Craig Mautner6985bad2014-04-21 15:22:06 -07004227 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004228 void setVisible(boolean visible) {
4229 if (mVisible != visible) {
4230 mVisible = visible;
4231 if (mCallback != null) {
4232 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4233 0 /* unused */, this).sendToTarget();
4234 }
4235 }
4236 }
4237
Craig Mautner6985bad2014-04-21 15:22:06 -07004238 void setDrawn() {
4239 }
4240
Craig Mautner1b4bf852014-05-26 15:06:32 -07004241 // You can always start a new task on a regular ActivityStack.
4242 boolean isEligibleForNewTasks() {
4243 return true;
4244 }
4245
Craig Mautnerd163e752014-06-13 17:18:47 -07004246 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004247 detachLocked();
4248 deleteActivityContainer(this);
4249 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004250 }
4251
Craig Mautner34b73df2014-01-12 21:11:08 -08004252 @Override
4253 public String toString() {
4254 return mIdString + (mActivityDisplay == null ? "N" : "A");
4255 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004256 }
4257
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004258 private class VirtualActivityContainer extends ActivityContainer {
4259 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004260 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004261
4262 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4263 super(getNextStackId());
4264 mParentActivity = parent;
4265 mCallback = callback;
4266 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004267 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004268 }
4269
4270 @Override
4271 public void setSurface(Surface surface, int width, int height, int density) {
4272 super.setSurface(surface, width, height, density);
4273
4274 synchronized (mService) {
4275 final long origId = Binder.clearCallingIdentity();
4276 try {
4277 setSurfaceLocked(surface, width, height, density);
4278 } finally {
4279 Binder.restoreCallingIdentity(origId);
4280 }
4281 }
4282 }
4283
4284 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4285 if (mContainerState == CONTAINER_STATE_FINISHING) {
4286 return;
4287 }
4288 VirtualActivityDisplay virtualActivityDisplay =
4289 (VirtualActivityDisplay) mActivityDisplay;
4290 if (virtualActivityDisplay == null) {
4291 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004292 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004293 mActivityDisplay = virtualActivityDisplay;
4294 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
4295 attachToDisplayLocked(virtualActivityDisplay);
4296 }
4297
4298 if (mSurface != null) {
4299 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004300 }
4301
Craig Mautner6985bad2014-04-21 15:22:06 -07004302 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004303 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004304 mStack.resumeTopActivityLocked(null);
4305 } else {
4306 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004307 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004308 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004309 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004310 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004311 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004312
Craig Mautnerd163e752014-06-13 17:18:47 -07004313 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004314
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004315 if (DEBUG_STACK) Slog.d(TAG_STACK,
4316 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004317 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004318
Craig Mautner6985bad2014-04-21 15:22:06 -07004319 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004320 boolean isAttachedLocked() {
4321 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004322 }
4323
4324 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004325 void setDrawn() {
4326 synchronized (mService) {
4327 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004328 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004329 }
4330 }
4331
Craig Mautner1b4bf852014-05-26 15:06:32 -07004332 // Never start a new task on an ActivityView if it isn't explicitly specified.
4333 @Override
4334 boolean isEligibleForNewTasks() {
4335 return false;
4336 }
4337
Craig Mautnerd163e752014-06-13 17:18:47 -07004338 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004339 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004340 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4341 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4342 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4343 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4344 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004345 }
4346 }
4347
Craig Mautner4a1cb222013-12-04 16:14:06 -08004348 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4349 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004350 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004351 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004352 int mDisplayId;
4353 Display mDisplay;
4354 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004355
Craig Mautner4a1cb222013-12-04 16:14:06 -08004356 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4357 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08004358 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004359
Jose Lima4b6c6692014-08-12 17:41:12 -07004360 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004361
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004362 ActivityDisplay() {
4363 }
Craig Mautner4504de52013-12-20 09:06:56 -08004364
Craig Mautner1a70a162014-09-13 12:09:31 -07004365 // After instantiation, check that mDisplay is not null before using this. The alternative
4366 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004367 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004368 final Display display = mDisplayManager.getDisplay(displayId);
4369 if (display == null) {
4370 return;
4371 }
4372 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004373 }
4374
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004375 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004376 mDisplay = display;
4377 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004378 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004379 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004380
4381 void attachActivities(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004382 if (DEBUG_STACK) Slog.v(TAG_STACK,
4383 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004384 mStacks.add(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004385 }
4386
Craig Mautnere0a38842013-12-16 16:14:02 -08004387 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004388 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004389 + " from displayId=" + mDisplayId);
4390 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004391 }
4392
4393 void getBounds(Point bounds) {
4394 mDisplay.getDisplayInfo(mDisplayInfo);
4395 bounds.x = mDisplayInfo.appWidth;
4396 bounds.y = mDisplayInfo.appHeight;
4397 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004398
Jose Lima4b6c6692014-08-12 17:41:12 -07004399 void setVisibleBehindActivity(ActivityRecord r) {
4400 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004401 }
4402
Jose Lima4b6c6692014-08-12 17:41:12 -07004403 boolean hasVisibleBehindActivity() {
4404 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004405 }
4406
Craig Mautner34b73df2014-01-12 21:11:08 -08004407 @Override
4408 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004409 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4410 }
4411 }
4412
4413 class VirtualActivityDisplay extends ActivityDisplay {
4414 VirtualDisplay mVirtualDisplay;
4415
Craig Mautner6985bad2014-04-21 15:22:06 -07004416 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004417 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004418 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4419 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4420 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4421 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004422
4423 init(mVirtualDisplay.getDisplay());
4424
4425 mWindowManager.handleDisplayAdded(mDisplayId);
4426 }
4427
4428 void setSurface(Surface surface) {
4429 if (mVirtualDisplay != null) {
4430 mVirtualDisplay.setSurface(surface);
4431 }
4432 }
4433
4434 @Override
4435 void detachActivitiesLocked(ActivityStack stack) {
4436 super.detachActivitiesLocked(stack);
4437 if (mVirtualDisplay != null) {
4438 mVirtualDisplay.release();
4439 mVirtualDisplay = null;
4440 }
4441 }
4442
4443 @Override
4444 public String toString() {
4445 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004446 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004447 }
Jose Lima58e66d62014-05-27 20:00:27 -07004448
4449 private boolean isLeanbackOnlyDevice() {
4450 boolean onLeanbackOnly = false;
4451 try {
4452 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
4453 PackageManager.FEATURE_LEANBACK_ONLY);
4454 } catch (RemoteException e) {
4455 // noop
4456 }
4457
4458 return onLeanbackOnly;
4459 }
Craig Mautner27084302013-03-25 08:05:25 -07004460}