blob: 8ab4ae59a2010fba9fe1e710b3f9e20481b92eb2 [file] [log] [blame]
Craig Mautner27084302013-03-25 08:05:25 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Craig Mautner6170f732013-04-02 13:05:23 -070019import static android.Manifest.permission.START_ANY_ACTIVITY;
Wale Ogunwale868a5e12015-08-02 16:19:20 -070020import static android.app.ActivityManager.*;
Wale Ogunwaled80c2632015-03-13 10:26:26 -070021import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK;
22import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
Craig Mautner29219d92013-04-16 20:19:12 -070023import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
24import static android.content.Intent.FLAG_ACTIVITY_TASK_ON_HOME;
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -070025import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Craig Mautner6170f732013-04-02 13:05:23 -070026import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Wale Ogunwaled57969f2014-11-15 19:37:29 -080027import static com.android.server.am.ActivityManagerDebugConfig.*;
Craig Mautner05d29032013-05-03 13:40:13 -070028import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Craig Mautner84984fa2014-06-19 11:19:20 -070029import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
30import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
31import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -070032import static com.android.server.am.ActivityStack.ActivityState.*;
Craig Mautner15df08a2015-04-01 12:17:18 -070033import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
34import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
Benjamin Franz469dd582015-06-09 14:24:36 +010035import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
Craig Mautner15df08a2015-04-01 12:17:18 -070036import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
37import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Craig Mautner8d341ef2013-03-26 09:03:27 -070038
Svetoslav7008b512015-06-24 18:47:07 -070039import android.Manifest;
Craig Mautner2420ead2013-04-01 17:13:20 -070040import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070041import android.app.ActivityManager;
Craig Mautnered6649f2013-12-02 14:08:25 -080042import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070043import android.app.ActivityOptions;
44import android.app.AppGlobals;
Svetoslav7008b512015-06-24 18:47:07 -070045import android.app.AppOpsManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -080046import android.app.IActivityContainer;
47import android.app.IActivityContainerCallback;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070048import android.app.IActivityManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070049import android.app.IApplicationThread;
Craig Mautner23ac33b2013-04-01 16:26:35 -070050import android.app.PendingIntent;
Jeff Hao1b012d32014-08-20 10:35:34 -070051import android.app.ProfilerInfo;
Craig Mautner20e72272013-04-01 13:45:53 -070052import android.app.ActivityManager.RunningTaskInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070053import android.app.IActivityManager.WaitResult;
Craig Mautner2420ead2013-04-01 17:13:20 -070054import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -040055import android.app.StatusBarManager;
Jason Monk35c62a42014-06-17 10:24:47 -040056import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070057import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -070058import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -070059import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070060import android.content.Intent;
Craig Mautner23ac33b2013-04-01 16:26:35 -070061import android.content.IntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070062import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070063import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -070064import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070065import android.content.pm.PackageManager;
66import android.content.pm.ResolveInfo;
67import android.content.res.Configuration;
Craig Mautner4a1cb222013-12-04 16:14:06 -080068import android.graphics.Point;
Wale Ogunwale60454db2015-01-23 16:05:07 -080069import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -080070import android.hardware.display.DisplayManager;
71import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080072import android.hardware.display.DisplayManagerGlobal;
73import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080074import android.hardware.input.InputManager;
75import android.hardware.input.InputManagerInternal;
Craig Mautnerb9168362015-02-26 20:40:19 -080076import android.net.Uri;
Craig Mautner23ac33b2013-04-01 16:26:35 -070077import android.os.Binder;
Craig Mautner8d341ef2013-03-26 09:03:27 -070078import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070079import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070080import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070081import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070082import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070083import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070084import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070085import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070086import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070087import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -040088import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070089import android.os.SystemClock;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -070090import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -070091import android.os.UserHandle;
Dianne Hackborn3d07c942015-03-13 18:02:54 -070092import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -070093import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -040094import android.provider.Settings;
95import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -070096import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -070097import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070098import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -070099import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700100import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800101import android.util.SparseArray;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700102
Craig Mautner4a1cb222013-12-04 16:14:06 -0800103import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800104import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800105import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -0800106import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800107import android.view.Surface;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700108import com.android.internal.app.HeavyWeightSwitcherActivity;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700109import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800110import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700111import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400112import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -0700113import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -0400114import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800115import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700116import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700117import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700118
justinzhang5286d3f2014-05-12 17:06:01 -0400119
Craig Mautner8d341ef2013-03-26 09:03:27 -0700120import java.io.FileDescriptor;
121import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700122import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700123import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700124import java.util.Arrays;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700125import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700126import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700127
Craig Mautner4a1cb222013-12-04 16:14:06 -0800128public final class ActivityStackSupervisor implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800129 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700130 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700131 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700132 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700133 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700134 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700135 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700136 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700137 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
138 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700139 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700140 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700141 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
142 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700143 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700144 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800145
Craig Mautnerf3333272013-04-22 10:55:53 -0700146 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700147 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700148
Craig Mautner0eea92c2013-05-16 13:35:39 -0700149 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700150 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700151
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700152 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700153 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700154
Craig Mautner05d29032013-05-03 13:40:13 -0700155 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
156 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
157 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700158 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700159 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800160 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
161 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
162 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700163 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400164 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
165 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700166 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700167 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700168 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800169
Wale Ogunwale040b4702015-08-06 18:10:50 -0700170 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700171
Jason Monk62515be2014-05-21 16:06:19 -0400172 private static final String LOCK_TASK_TAG = "Lock-to-App";
173
Wale Ogunwale040b4702015-08-06 18:10:50 -0700174 // Used to indicate if an object (e.g. stack) that we are trying to get
175 // should be created if it doesn't exist already.
176 private static final boolean CREATE_IF_NEEDED = true;
177
178 // Used to indicate if an object (e.g. task) should be moved/created
179 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700180 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700181
182 // Used to indicate that an objects (e.g. task) removal from its container
183 // (e.g. stack) is due to it moving to another container.
184 static final boolean MOVING = true;
185
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700186 // Force the focus to change to the stack we are moving a task to..
187 static final boolean FORCE_FOCUS = true;
188
Svetoslav7008b512015-06-24 18:47:07 -0700189 // Activity actions an app cannot start if it uses a permission which is not granted.
190 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
191 new ArrayMap<>();
192 static {
193 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
194 Manifest.permission.CAMERA);
195 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
196 Manifest.permission.CAMERA);
197 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
198 Manifest.permission.CALL_PHONE);
199 }
200
Svet Ganov99b60432015-06-27 13:15:22 -0700201 /** Action restriction: launching the activity is not restricted. */
202 private static final int ACTIVITY_RESTRICTION_NONE = 0;
203 /** Action restriction: launching the activity is restricted by a permission. */
204 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
205 /** Action restriction: launching the activity is restricted by an app op. */
206 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700207
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700208 // The height/width divide used when fitting a task within a bounds with method
209 // {@link #fitWithinBounds}.
210 // We always want the task to to be visible in the bounds without affecting its size when
211 // fitting. To make sure this is the case, we don't adjust the task left or top side pass
212 // the input bounds right or bottom side minus the width or height divided by this value.
213 private static final int FIT_WITHIN_BOUNDS_DIVIDER = 3;
214
justinzhang5286d3f2014-05-12 17:06:01 -0400215 /** Status Bar Service **/
216 private IBinder mToken = new Binder();
217 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400218 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400219
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700220 // For debugging to make sure the caller when acquiring/releasing our
221 // wake lock is the system process.
222 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700223
Craig Mautner27084302013-03-25 08:05:25 -0700224 final ActivityManagerService mService;
225
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800226 private final RecentTasks mRecentTasks;
227
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700228 final ActivityStackSupervisorHandler mHandler;
229
230 /** Short cut */
231 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800232 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700233
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700234 /** Counter for next free stack ID to use for dynamic activity stacks. */
235 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700236
237 /** Task identifier that activities are currently being started in. Incremented each time a
238 * new task is created. */
239 private int mCurTaskId = 0;
240
Craig Mautner2420ead2013-04-01 17:13:20 -0700241 /** The current user */
242 private int mCurrentUser;
243
Craig Mautnere0a38842013-12-16 16:14:02 -0800244 /** The stack containing the launcher app. Assumed to always be attached to
245 * Display.DEFAULT_DISPLAY. */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700246 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700247
Craig Mautnere0a38842013-12-16 16:14:02 -0800248 /** The stack currently receiving input or launching the next activity. */
Craig Mautner29219d92013-04-16 20:19:12 -0700249 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700250
Craig Mautner4a1cb222013-12-04 16:14:06 -0800251 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
252 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800253 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800254 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700255
256 /** List of activities that are waiting for a new activity to become visible before completing
257 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800258 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700259
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700260 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800261 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700262
263 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800264 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700265
Craig Mautnerde4ef022013-04-07 19:01:33 -0700266 /** List of activities that are ready to be stopped, but waiting for the next activity to
267 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800268 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700269
Craig Mautnerf3333272013-04-22 10:55:53 -0700270 /** List of activities that are ready to be finished, but waiting for the previous activity to
271 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800272 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700273
Craig Mautner0eea92c2013-05-16 13:35:39 -0700274 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800275 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700276
Craig Mautnerf3333272013-04-22 10:55:53 -0700277 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700278 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700279
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700280 /** Used to queue up any background users being started */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700281 final ArrayList<UserState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700282
Craig Mautnerde4ef022013-04-07 19:01:33 -0700283 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
284 * is being brought in front of us. */
285 boolean mUserLeaving = false;
286
Craig Mautner0eea92c2013-05-16 13:35:39 -0700287 /** Set when we have taken too long waiting to go to sleep. */
288 boolean mSleepTimeout = false;
289
Jose Lima58e66d62014-05-27 20:00:27 -0700290 /** Indicates if we are running on a Leanback-only (TV) device. Only initialized after
291 * setWindowManager is called. **/
292 private boolean mLeanbackOnlyDevice;
293
Craig Mautner0eea92c2013-05-16 13:35:39 -0700294 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700295 * We don't want to allow the device to go to sleep while in the process
296 * of launching an activity. This is primarily to allow alarm intent
297 * receivers to launch an activity and get that to run before the device
298 * goes back to sleep.
299 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700300 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700301
302 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700303 * Set when the system is going to sleep, until we have
304 * successfully paused the current activity and released our wake lock.
305 * At that point the system is allowed to actually sleep.
306 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700307 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700308
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700309 /** Stack id of the front stack when user switched, indexed by userId. */
310 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700311
Craig Mautner4504de52013-12-20 09:06:56 -0800312 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800313 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700314 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800315
316 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700317 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800318
Jeff Brownca9bc702014-02-11 14:32:56 -0800319 InputManagerInternal mInputManagerInternal;
320
Craig Mautner15df08a2015-04-01 12:17:18 -0700321 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
322 * may be finished until there is only one entry left. If this is empty the system is not
323 * in lockTask mode. */
324 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000325 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700326 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
327 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000328 */
329 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400330 /**
331 * Notifies the user when entering/exiting lock-task.
332 */
333 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800334
Craig Mautner15df08a2015-04-01 12:17:18 -0700335 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches = new ArrayList<>();
Craig Mautneree36c772014-07-16 14:56:05 -0700336
Craig Mautner42d04db2014-11-06 12:13:23 -0800337 /** Used to keep resumeTopActivityLocked() from being entered recursively */
338 boolean inResumeTopActivity;
339
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700340 // temp. rects used during resize calculation so we don't need to create a new object each time.
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700341 private final Rect tempRect = new Rect();
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700342 private final Rect tempRect2 = new Rect();
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700343
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700344 private final SparseArray<Configuration> mTmpConfigs = new SparseArray<>();
345 private final SparseArray<Rect> mTmpBounds = new SparseArray<>();
346
Craig Mautneree36c772014-07-16 14:56:05 -0700347 /**
348 * Description of a request to start a new activity, which has been held
349 * due to app switches being disabled.
350 */
351 static class PendingActivityLaunch {
352 final ActivityRecord r;
353 final ActivityRecord sourceRecord;
354 final int startFlags;
355 final ActivityStack stack;
356
357 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
358 int _startFlags, ActivityStack _stack) {
359 r = _r;
360 sourceRecord = _sourceRecord;
361 startFlags = _startFlags;
362 stack = _stack;
363 }
364 }
365
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800366 public ActivityStackSupervisor(ActivityManagerService service, RecentTasks recentTasks) {
Craig Mautner27084302013-03-25 08:05:25 -0700367 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800368 mRecentTasks = recentTasks;
Jeff Brown2c43c332014-06-12 22:38:59 -0700369 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
370 }
371
372 /**
373 * At the time when the constructor runs, the power manager has not yet been
374 * initialized. So we initialize our wakelocks afterwards.
375 */
376 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800377 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700378 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700379 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700380 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700381 }
382
justinzhang5286d3f2014-05-12 17:06:01 -0400383 // This function returns a IStatusBarService. The value is from ServiceManager.
384 // getService and is cached.
385 private IStatusBarService getStatusBarService() {
386 synchronized (mService) {
387 if (mStatusBarService == null) {
388 mStatusBarService = IStatusBarService.Stub.asInterface(
389 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
390 if (mStatusBarService == null) {
391 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
392 }
393 }
394 return mStatusBarService;
395 }
396 }
397
Jason Monk35c62a42014-06-17 10:24:47 -0400398 private IDevicePolicyManager getDevicePolicyManager() {
399 synchronized (mService) {
400 if (mDevicePolicyManager == null) {
401 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
402 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
403 if (mDevicePolicyManager == null) {
404 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
405 }
406 }
407 return mDevicePolicyManager;
408 }
409 }
410
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700411 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800412 synchronized (mService) {
413 mWindowManager = wm;
414
415 mDisplayManager =
416 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
417 mDisplayManager.registerDisplayListener(this, null);
418
419 Display[] displays = mDisplayManager.getDisplays();
420 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
421 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800422 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700423 if (activityDisplay.mDisplay == null) {
424 throw new IllegalStateException("Default Display does not exist");
425 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800426 mActivityDisplays.put(displayId, activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800427 }
428
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700429 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800430 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800431
432 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Jose Lima58e66d62014-05-27 20:00:27 -0700433
434 // Initialize this here, now that we can get a valid reference to PackageManager.
435 mLeanbackOnlyDevice = isLeanbackOnlyDevice();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800436 }
Craig Mautner27084302013-03-25 08:05:25 -0700437 }
438
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200439 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700440 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200441 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700442 }
443
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700444 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800445 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700446 }
447
Craig Mautnerde4ef022013-04-07 19:01:33 -0700448 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800449 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700450 }
451
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800452 /** Top of all visible stacks is/should always be equal to the focused stack.
453 * Use {@link ActivityStack#isStackVisibleLocked} to determine if a specific
454 * stack is visible or not. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700455 boolean isFrontStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700456 if (stack == null) {
457 return false;
458 }
459
Craig Mautnerdf88d732014-01-27 09:21:32 -0800460 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
461 if (parent != null) {
462 stack = parent.task.stack;
463 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800464 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700465 }
466
Craig Mautner299f9602015-01-26 09:47:33 -0800467 void moveHomeStack(boolean toFront, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800468 moveHomeStack(toFront, reason, null);
469 }
470
471 void moveHomeStack(boolean toFront, String reason, ActivityStack lastFocusedStack) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800472 ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautnerde313752015-01-22 14:28:03 -0800473 final int topNdx = stacks.size() - 1;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800474 if (topNdx <= 0) {
475 return;
476 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700477
478 // The home stack should either be at the top or bottom of the stack list.
479 if ((toFront && (stacks.get(topNdx) != mHomeStack))
480 || (!toFront && (stacks.get(0) != mHomeStack))) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700481 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveHomeTask: topStack old="
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700482 + ((lastFocusedStack != null) ? lastFocusedStack : stacks.get(topNdx))
483 + " new=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800484 stacks.remove(mHomeStack);
485 stacks.add(toFront ? topNdx : 0, mHomeStack);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700486 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800487
488 if (lastFocusedStack != null) {
489 mLastFocusedStack = lastFocusedStack;
490 }
491 mFocusedStack = stacks.get(topNdx);
492
Craig Mautnerde313752015-01-22 14:28:03 -0800493 EventLog.writeEvent(EventLogTags.AM_HOME_STACK_MOVED,
494 mCurrentUser, toFront ? 1 : 0, stacks.get(topNdx).getStackId(),
Craig Mautner299f9602015-01-26 09:47:33 -0800495 mFocusedStack == null ? -1 : mFocusedStack.getStackId(), reason);
Craig Mautnerde313752015-01-22 14:28:03 -0800496
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800497 if (mService.mBooting || !mService.mBooted) {
498 final ActivityRecord r = topRunningActivityLocked();
499 if (r != null && r.idle) {
500 checkFinishBootingLocked();
501 }
502 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700503 }
504
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700505 /** Returns true if the focus activity was adjusted to the home stack top activity. */
506 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700507 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
508 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700509 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700510 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700511
Craig Mautner84984fa2014-06-19 11:19:20 -0700512 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700513
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700514 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700515 if (top == null) {
516 return false;
517 }
518 mService.setFocusedActivityLocked(top, reason);
519 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700520 }
521
Craig Mautner299f9602015-01-26 09:47:33 -0800522 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700523 if (!mService.mBooting && !mService.mBooted) {
524 // Not ready yet!
525 return false;
526 }
527
Craig Mautner84984fa2014-06-19 11:19:20 -0700528 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
529 mWindowManager.showRecentApps();
530 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700531 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700532
Craig Mautner84984fa2014-06-19 11:19:20 -0700533 if (prev != null) {
534 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
535 }
536
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700537 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
538 ActivityRecord r = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700539 if (r != null) {
Craig Mautner299f9602015-01-26 09:47:33 -0800540 mService.setFocusedActivityLocked(r, reason);
Craig Mautner05d29032013-05-03 13:40:13 -0700541 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700542 }
Craig Mautner299f9602015-01-26 09:47:33 -0800543 return mService.startHomeActivityLocked(mCurrentUser, reason);
Craig Mautner69ada552013-04-18 13:51:51 -0700544 }
545
Craig Mautner8d341ef2013-03-26 09:03:27 -0700546 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700547 return anyTaskForIdLocked(id, true);
548 }
549
550 /**
551 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
552 * @param id Id of the task we would like returned.
553 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
554 * restore the task from recents to the active list.
555 */
556 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800557 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800558 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800559 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800560 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
561 ActivityStack stack = stacks.get(stackNdx);
562 TaskRecord task = stack.taskForIdLocked(id);
563 if (task != null) {
564 return task;
565 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700566 }
567 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800568
569 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700570 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800571 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800572 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700573 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800574 return null;
575 }
576
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700577 if (!restoreFromRecents) {
578 return task;
579 }
580
Chong Zhang5dcb2752015-08-18 13:50:26 -0700581 if (!restoreRecentTaskLocked(task, INVALID_STACK_ID)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700582 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
583 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800584 return null;
585 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700586 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800587 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700588 }
589
Craig Mautner6170f732013-04-02 13:05:23 -0700590 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800591 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800592 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800593 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800594 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
595 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
596 if (r != null) {
597 return r;
598 }
Craig Mautner6170f732013-04-02 13:05:23 -0700599 }
600 }
601 return null;
602 }
603
Craig Mautneref73ee12014-04-23 11:45:37 -0700604 void setNextTaskId(int taskId) {
605 if (taskId > mCurTaskId) {
606 mCurTaskId = taskId;
607 }
608 }
609
Craig Mautner8d341ef2013-03-26 09:03:27 -0700610 int getNextTaskId() {
611 do {
612 mCurTaskId++;
613 if (mCurTaskId <= 0) {
614 mCurTaskId = 1;
615 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700616 } while (anyTaskForIdLocked(mCurTaskId, false) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700617 return mCurTaskId;
618 }
619
Craig Mautnerde4ef022013-04-07 19:01:33 -0700620 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800621 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700622 if (stack == null) {
623 return null;
624 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700625 ActivityRecord resumedActivity = stack.mResumedActivity;
626 if (resumedActivity == null || resumedActivity.app == null) {
627 resumedActivity = stack.mPausingActivity;
628 if (resumedActivity == null || resumedActivity.app == null) {
629 resumedActivity = stack.topRunningActivityLocked(null);
630 }
631 }
632 return resumedActivity;
633 }
634
Dianne Hackbornff072722014-09-24 10:56:28 -0700635 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700636 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800637 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800638 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
639 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800640 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
641 final ActivityStack stack = stacks.get(stackNdx);
642 if (!isFrontStack(stack)) {
643 continue;
644 }
645 ActivityRecord hr = stack.topRunningActivityLocked(null);
646 if (hr != null) {
647 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
648 && processName.equals(hr.processName)) {
649 try {
George Mount2c92c972014-03-20 09:38:23 -0700650 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800651 didSomething = true;
652 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700653 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800654 Slog.w(TAG, "Exception in new application when starting activity "
655 + hr.intent.getComponent().flattenToShortString(), e);
656 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700657 }
Craig Mautner20e72272013-04-01 13:45:53 -0700658 }
Craig Mautner20e72272013-04-01 13:45:53 -0700659 }
660 }
661 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700662 if (!didSomething) {
663 ensureActivitiesVisibleLocked(null, 0);
664 }
Craig Mautner20e72272013-04-01 13:45:53 -0700665 return didSomething;
666 }
667
668 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800669 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
670 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800671 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
672 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner34b73df2014-01-12 21:11:08 -0800673 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800674 continue;
675 }
676 final ActivityRecord resumedActivity = stack.mResumedActivity;
677 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700678 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800679 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800680 return false;
681 }
Craig Mautner20e72272013-04-01 13:45:53 -0700682 }
683 }
684 return true;
685 }
686
Craig Mautnerde4ef022013-04-07 19:01:33 -0700687 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800688 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
689 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800690 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
691 final ActivityStack stack = stacks.get(stackNdx);
692 if (isFrontStack(stack)) {
693 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700694 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800695 return false;
696 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700697 }
698 }
699 }
700 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700701 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800702 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
703 mLastFocusedStack + " to=" + mFocusedStack);
704 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700705 return true;
706 }
707
708 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800709 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800710 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
711 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800712 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
713 final ActivityStack stack = stacks.get(stackNdx);
714 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800715 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700716 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800717 return false;
718 }
719 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800720 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700721 }
722 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800723 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700724 }
725
Craig Mautner2acc3892013-09-23 10:28:14 -0700726 /**
727 * Pause all activities in either all of the stacks or just the back stacks.
728 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700729 * @return true if any activity was paused as a result of this call.
730 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700731 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700732 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800733 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
734 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800735 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
736 final ActivityStack stack = stacks.get(stackNdx);
737 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700738 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800739 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700740 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
741 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800742 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700743 }
744 }
745 return someActivityPaused;
746 }
747
Craig Mautnerde4ef022013-04-07 19:01:33 -0700748 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700749 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800750 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
751 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800752 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
753 final ActivityStack stack = stacks.get(stackNdx);
754 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700755 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800756 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700757 Slog.d(TAG_STATES,
758 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800759 pausing = false;
760 } else {
761 return false;
762 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700763 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700764 }
765 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700766 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700767 }
768
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700769 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
770 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500771 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800772 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
773 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
774 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
775 final ActivityStack stack = stacks.get(stackNdx);
776 if (stack.mResumedActivity != null &&
777 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700778 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800779 }
780 }
781 }
782 }
783
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700784 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700785 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700786 }
787
788 void sendWaitingVisibleReportLocked(ActivityRecord r) {
789 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700790 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700791 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700792 if (w.who == null) {
793 changed = true;
794 w.timeout = false;
795 if (r != null) {
796 w.who = new ComponentName(r.info.packageName, r.info.name);
797 }
798 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
799 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700800 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700801 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700802 if (changed) {
803 mService.notifyAll();
804 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700805 }
806
807 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
808 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700809 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700810 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700811 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700812 if (w.who == null) {
813 changed = true;
814 w.timeout = timeout;
815 if (r != null) {
816 w.who = new ComponentName(r.info.packageName, r.info.name);
817 }
818 w.thisTime = thisTime;
819 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700820 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700821 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700822 if (changed) {
823 mService.notifyAll();
824 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700825 }
826
Craig Mautner29219d92013-04-16 20:19:12 -0700827 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800828 final ActivityStack focusedStack = mFocusedStack;
Craig Mautner1602ec22013-05-12 10:24:27 -0700829 ActivityRecord r = focusedStack.topRunningActivityLocked(null);
830 if (r != null) {
831 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700832 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700833
Craig Mautner4a1cb222013-12-04 16:14:06 -0800834 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800835 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800836 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
837 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700838 if (stack != focusedStack && isFrontStack(stack)) {
Craig Mautner29219d92013-04-16 20:19:12 -0700839 r = stack.topRunningActivityLocked(null);
840 if (r != null) {
841 return r;
842 }
843 }
844 }
845 return null;
846 }
847
Dianne Hackborn09233282014-04-30 11:33:59 -0700848 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700849 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800850 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
851 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800852 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800853 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800854 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800855 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
856 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700857 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800858 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700859 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700860 }
861 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700862
863 // The lists are already sorted from most recent to oldest. Just pull the most recent off
864 // each list and add it to list. Stop when all lists are empty or maxNum reached.
865 while (maxNum > 0) {
866 long mostRecentActiveTime = Long.MIN_VALUE;
867 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800868 final int numTaskLists = runningTaskLists.size();
869 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
870 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700871 if (!stackTaskList.isEmpty()) {
872 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
873 if (lastActiveTime > mostRecentActiveTime) {
874 mostRecentActiveTime = lastActiveTime;
875 selectedStackList = stackTaskList;
876 }
877 }
878 }
879 if (selectedStackList != null) {
880 list.add(selectedStackList.remove(0));
881 --maxNum;
882 } else {
883 break;
884 }
885 }
Craig Mautner20e72272013-04-01 13:45:53 -0700886 }
887
Craig Mautner23ac33b2013-04-01 16:26:35 -0700888 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700889 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700890 // Collect information about the target of the Intent.
891 ActivityInfo aInfo;
892 try {
893 ResolveInfo rInfo =
894 AppGlobals.getPackageManager().resolveIntent(
895 intent, resolvedType,
896 PackageManager.MATCH_DEFAULT_ONLY
897 | ActivityManagerService.STOCK_PM_FLAGS, userId);
898 aInfo = rInfo != null ? rInfo.activityInfo : null;
899 } catch (RemoteException e) {
900 aInfo = null;
901 }
902
903 if (aInfo != null) {
904 // Store the found target back into the intent, because now that
905 // we have it we never want to do this again. For example, if the
906 // user navigates back to this point in the history, we should
907 // always restart the exact same activity.
908 intent.setComponent(new ComponentName(
909 aInfo.applicationInfo.packageName, aInfo.name));
910
911 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -0700912 if (!aInfo.processName.equals("system")) {
913 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700914 mService.setDebugApp(aInfo.processName, true, false);
915 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700916
Man Caocfa78b22015-06-11 20:14:34 -0700917 if ((startFlags & ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700918 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
919 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700920
Man Caocfa78b22015-06-11 20:14:34 -0700921 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
922 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
923 }
924
925 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700926 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700927 }
928 }
929 }
930 return aInfo;
931 }
932
Craig Mautner299f9602015-01-26 09:47:33 -0800933 void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
934 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700935 startActivityLocked(null /* caller */, intent, null /* resolvedType */, aInfo,
936 null /* voiceSession */, null /* voiceInteractor */, null /* resultTo */,
937 null /* resultWho */, 0 /* requestCode */, 0 /* callingPid */, 0 /* callingUid */,
938 null /* callingPackage */, 0 /* realCallingPid */, 0 /* realCallingUid */,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700939 0 /* startFlags */, null /* options */, false /* ignoreTargetSecurity */,
940 false /* componentSpecified */,
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700941 null /* outActivity */, null /* container */, null /* inTask */);
942 if (inResumeTopActivity) {
943 // If we are in resume section already, home activity will be initialized, but not
944 // resumed (to avoid recursive resume) and will stay that way until something pokes it
945 // again. We need to schedule another resume.
946 scheduleResumeTopActivities();
947 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700948 }
949
Craig Mautner23ac33b2013-04-01 16:26:35 -0700950 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700951 String callingPackage, Intent intent, String resolvedType,
952 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700953 IBinder resultTo, String resultWho, int requestCode, int startFlags,
954 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700955 Bundle options, boolean ignoreTargetSecurity, int userId,
956 IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700957 // Refuse possible leaked file descriptors
958 if (intent != null && intent.hasFileDescriptors()) {
959 throw new IllegalArgumentException("File descriptors passed in Intent");
960 }
961 boolean componentSpecified = intent.getComponent() != null;
962
963 // Don't modify the client's object!
964 intent = new Intent(intent);
965
966 // Collect information about the target of the Intent.
Craig Mautner15df08a2015-04-01 12:17:18 -0700967 ActivityInfo aInfo =
968 resolveActivity(intent, resolvedType, startFlags, profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700969
Craig Mautnere0a38842013-12-16 16:14:02 -0800970 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700971 synchronized (mService) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800972 if (container != null && container.mParentActivity != null &&
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700973 container.mParentActivity.state != RESUMED) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800974 // Cannot start a child activity if the parent is not resumed.
975 return ActivityManager.START_CANCELED;
976 }
Dianne Hackborn95465202014-09-15 16:21:55 -0700977 final int realCallingPid = Binder.getCallingPid();
978 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -0700979 int callingPid;
980 if (callingUid >= 0) {
981 callingPid = -1;
982 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700983 callingPid = realCallingPid;
984 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700985 } else {
986 callingPid = callingUid = -1;
987 }
988
Craig Mautnere0a38842013-12-16 16:14:02 -0800989 final ActivityStack stack;
990 if (container == null || container.mStack.isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800991 stack = mFocusedStack;
Craig Mautnere0a38842013-12-16 16:14:02 -0800992 } else {
993 stack = container.mStack;
994 }
Wale Ogunwale60454db2015-01-23 16:05:07 -0800995 stack.mConfigWillChange = config != null && mService.mConfiguration.diff(config) != 0;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700996 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700997 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700998
999 final long origId = Binder.clearCallingIdentity();
1000
1001 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001002 (aInfo.applicationInfo.privateFlags
1003 &ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001004 // This may be a heavy-weight process! Check to see if we already
1005 // have another, different heavy-weight process running.
1006 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
1007 if (mService.mHeavyWeightProcess != null &&
1008 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
1009 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001010 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001011 if (caller != null) {
1012 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
1013 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001014 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001015 } else {
1016 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -07001017 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -07001018 + intent.toString());
1019 ActivityOptions.abort(options);
1020 return ActivityManager.START_PERMISSION_DENIED;
1021 }
1022 }
1023
1024 IIntentSender target = mService.getIntentSenderLocked(
1025 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -07001026 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -07001027 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
1028 | PendingIntent.FLAG_ONE_SHOT, null);
1029
1030 Intent newIntent = new Intent();
1031 if (requestCode >= 0) {
1032 // Caller is requesting a result.
1033 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
1034 }
1035 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
1036 new IntentSender(target));
1037 if (mService.mHeavyWeightProcess.activities.size() > 0) {
1038 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
1039 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
1040 hist.packageName);
1041 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
1042 hist.task.taskId);
1043 }
1044 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
1045 aInfo.packageName);
1046 newIntent.setFlags(intent.getFlags());
1047 newIntent.setClassName("android",
1048 HeavyWeightSwitcherActivity.class.getName());
1049 intent = newIntent;
1050 resolvedType = null;
1051 caller = null;
1052 callingUid = Binder.getCallingUid();
1053 callingPid = Binder.getCallingPid();
1054 componentSpecified = true;
1055 try {
1056 ResolveInfo rInfo =
1057 AppGlobals.getPackageManager().resolveIntent(
1058 intent, null,
1059 PackageManager.MATCH_DEFAULT_ONLY
1060 | ActivityManagerService.STOCK_PM_FLAGS, userId);
1061 aInfo = rInfo != null ? rInfo.activityInfo : null;
1062 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1063 } catch (RemoteException e) {
1064 aInfo = null;
1065 }
1066 }
1067 }
1068 }
1069
Dianne Hackborn91097de2014-04-04 18:02:06 -07001070 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
1071 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -07001072 requestCode, callingPid, callingUid, callingPackage,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001073 realCallingPid, realCallingUid, startFlags, options, ignoreTargetSecurity,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001074 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001075
Craig Mautner85c11a82014-07-17 12:38:18 -07001076 Binder.restoreCallingIdentity(origId);
1077
Craig Mautnerde4ef022013-04-07 19:01:33 -07001078 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001079 // If the caller also wants to switch to a new configuration,
1080 // do so now. This allows a clean switch, as we are waiting
1081 // for the current activity to pause (so we will not destroy
1082 // it), and have not yet started the next activity.
1083 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
1084 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -07001085 stack.mConfigWillChange = false;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001086 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautner23ac33b2013-04-01 16:26:35 -07001087 "Updating to new configuration after starting activity.");
Maxim Bogatov05075302015-05-19 18:33:08 -07001088 mService.updateConfigurationLocked(config, null, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001089 }
1090
Craig Mautner23ac33b2013-04-01 16:26:35 -07001091 if (outResult != null) {
1092 outResult.result = res;
1093 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001094 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001095 do {
1096 try {
1097 mService.wait();
1098 } catch (InterruptedException e) {
1099 }
1100 } while (!outResult.timeout && outResult.who == null);
1101 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001102 ActivityRecord r = stack.topRunningActivityLocked(null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001103 if (r.nowVisible && r.state == RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001104 outResult.timeout = false;
1105 outResult.who = new ComponentName(r.info.packageName, r.info.name);
1106 outResult.totalTime = 0;
1107 outResult.thisTime = 0;
1108 } else {
1109 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001110 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001111 do {
1112 try {
1113 mService.wait();
1114 } catch (InterruptedException e) {
1115 }
1116 } while (!outResult.timeout && outResult.who == null);
1117 }
1118 }
1119 }
1120
1121 return res;
1122 }
1123 }
1124
1125 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1126 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
1127 Bundle options, int userId) {
1128 if (intents == null) {
1129 throw new NullPointerException("intents is null");
1130 }
1131 if (resolvedTypes == null) {
1132 throw new NullPointerException("resolvedTypes is null");
1133 }
1134 if (intents.length != resolvedTypes.length) {
1135 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1136 }
1137
Craig Mautner23ac33b2013-04-01 16:26:35 -07001138
1139 int callingPid;
1140 if (callingUid >= 0) {
1141 callingPid = -1;
1142 } else if (caller == null) {
1143 callingPid = Binder.getCallingPid();
1144 callingUid = Binder.getCallingUid();
1145 } else {
1146 callingPid = callingUid = -1;
1147 }
1148 final long origId = Binder.clearCallingIdentity();
1149 try {
1150 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001151 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001152 for (int i=0; i<intents.length; i++) {
1153 Intent intent = intents[i];
1154 if (intent == null) {
1155 continue;
1156 }
1157
1158 // Refuse possible leaked file descriptors
1159 if (intent != null && intent.hasFileDescriptors()) {
1160 throw new IllegalArgumentException("File descriptors passed in Intent");
1161 }
1162
1163 boolean componentSpecified = intent.getComponent() != null;
1164
1165 // Don't modify the client's object!
1166 intent = new Intent(intent);
1167
1168 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001169 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001170 // TODO: New, check if this is correct
1171 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1172
1173 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001174 (aInfo.applicationInfo.privateFlags
1175 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001176 throw new IllegalArgumentException(
1177 "FLAG_CANT_SAVE_STATE not supported here");
1178 }
1179
1180 Bundle theseOptions;
1181 if (options != null && i == intents.length-1) {
1182 theseOptions = options;
1183 } else {
1184 theseOptions = null;
1185 }
Craig Mautner6170f732013-04-02 13:05:23 -07001186 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001187 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1188 callingPackage, callingPid, callingUid,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001189 0, theseOptions, false, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001190 if (res < 0) {
1191 return res;
1192 }
1193
1194 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1195 }
1196 }
1197 } finally {
1198 Binder.restoreCallingIdentity(origId);
1199 }
1200
1201 return ActivityManager.START_SUCCESS;
1202 }
1203
Craig Mautner2420ead2013-04-01 17:13:20 -07001204 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001205 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001206 throws RemoteException {
1207
Craig Mautner2568c3a2015-03-26 14:22:34 -07001208 if (andResume) {
1209 r.startFreezingScreenLocked(app, 0);
1210 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001211
Craig Mautner2568c3a2015-03-26 14:22:34 -07001212 // schedule launch ticks to collect information about slow apps.
1213 r.startLaunchTickingLocked();
1214 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001215
1216 // Have the window manager re-evaluate the orientation of
1217 // the screen based on the new activity order. Note that
1218 // as a result of this, it can call back into the activity
1219 // manager with a new orientation. We don't care about that,
1220 // because the activity is not currently running so we are
1221 // just restarting it anyway.
1222 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001223 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001224 mService.mConfiguration,
1225 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001226 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001227 }
1228
1229 r.app = app;
1230 app.waitingToKill = null;
1231 r.launchCount++;
1232 r.lastLaunchTime = SystemClock.uptimeMillis();
1233
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001234 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001235
1236 int idx = app.activities.indexOf(r);
1237 if (idx < 0) {
1238 app.activities.add(r);
1239 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001240 mService.updateLruProcessLocked(app, true, null);
1241 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001242
Craig Mautner15df08a2015-04-01 12:17:18 -07001243 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001244 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1245 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001246 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001247 }
1248
1249 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001250 try {
1251 if (app.thread == null) {
1252 throw new RemoteException();
1253 }
1254 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001255 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001256 if (andResume) {
1257 results = r.results;
1258 newIntents = r.newIntents;
1259 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001260 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1261 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1262 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001263 if (andResume) {
1264 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1265 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001266 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001267 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001268 if (r.isHomeActivity() && r.isNotResolverActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001269 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001270 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001271 }
1272 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1273 r.sleeping = false;
1274 r.forceNewConfig = false;
1275 mService.showAskCompatModeDialogLocked(r);
1276 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001277 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001278 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1279 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1280 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001281 final String profileFile = mService.mProfileFile;
1282 if (profileFile != null) {
1283 ParcelFileDescriptor profileFd = mService.mProfileFd;
1284 if (profileFd != null) {
1285 try {
1286 profileFd = profileFd.dup();
1287 } catch (IOException e) {
1288 if (profileFd != null) {
1289 try {
1290 profileFd.close();
1291 } catch (IOException o) {
1292 }
1293 profileFd = null;
1294 }
1295 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001296 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001297
1298 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1299 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001300 }
1301 }
1302 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001303
Craig Mautner2568c3a2015-03-26 14:22:34 -07001304 if (andResume) {
1305 app.hasShownUi = true;
1306 app.pendingUiClean = true;
1307 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001308 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001309 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001310 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001311 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001312 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001313 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001314
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001315 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001316 // This may be a heavy-weight process! Note that the package
1317 // manager will ensure that only activity can run in the main
1318 // process of the .apk, which is the only thing that will be
1319 // considered heavy-weight.
1320 if (app.processName.equals(app.info.packageName)) {
1321 if (mService.mHeavyWeightProcess != null
1322 && mService.mHeavyWeightProcess != app) {
1323 Slog.w(TAG, "Starting new heavy weight process " + app
1324 + " when already running "
1325 + mService.mHeavyWeightProcess);
1326 }
1327 mService.mHeavyWeightProcess = app;
1328 Message msg = mService.mHandler.obtainMessage(
1329 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1330 msg.obj = r;
1331 mService.mHandler.sendMessage(msg);
1332 }
1333 }
1334
1335 } catch (RemoteException e) {
1336 if (r.launchFailed) {
1337 // This is the second time we failed -- finish activity
1338 // and give up.
1339 Slog.e(TAG, "Second failure launching "
1340 + r.intent.getComponent().flattenToShortString()
1341 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001342 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001343 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1344 "2nd-crash", false);
1345 return false;
1346 }
1347
1348 // This is the first time we failed -- restart process and
1349 // retry.
1350 app.activities.remove(r);
1351 throw e;
1352 }
1353
1354 r.launchFailed = false;
1355 if (stack.updateLRUListLocked(r)) {
1356 Slog.w(TAG, "Activity " + r
1357 + " being launched, but already in LRU list");
1358 }
1359
1360 if (andResume) {
1361 // As part of the process of launching, ActivityThread also performs
1362 // a resume.
1363 stack.minimalResumeActivityLocked(r);
1364 } else {
1365 // This activity is not starting in the resumed state... which
1366 // should look like we asked it to pause+stop (but remain visible),
1367 // and it has done so and reported back the current icicle and
1368 // other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001369 if (DEBUG_STATES) Slog.v(TAG_STATES,
1370 "Moving to STOPPED: " + r + " (starting in stopped state)");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001371 r.state = STOPPED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001372 r.stopped = true;
1373 }
1374
1375 // Launch the new version setup screen if needed. We do this -after-
1376 // launching the initial activity (that is, home), so that it can have
1377 // a chance to initialize itself while in the background, making the
1378 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001379 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001380 mService.startSetupActivityLocked();
1381 }
1382
Dianne Hackborn465fa392014-09-14 14:21:18 -07001383 // Update any services we are bound to that might care about whether
1384 // their client may have activities.
1385 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1386
Craig Mautner2420ead2013-04-01 17:13:20 -07001387 return true;
1388 }
1389
Craig Mautnere79d42682013-04-01 19:01:53 -07001390 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001391 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001392 // Is this activity's application already running?
1393 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001394 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001395
1396 r.task.stack.setLaunchTime(r);
1397
1398 if (app != null && app.thread != null) {
1399 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001400 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1401 || !"android".equals(r.info.packageName)) {
1402 // Don't add this if it is a platform component that is marked
1403 // to run in multiple processes, because this is actually
1404 // part of the framework so doesn't make sense to track as a
1405 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001406 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1407 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001408 }
George Mount2c92c972014-03-20 09:38:23 -07001409 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001410 return;
1411 } catch (RemoteException e) {
1412 Slog.w(TAG, "Exception when starting activity "
1413 + r.intent.getComponent().flattenToShortString(), e);
1414 }
1415
1416 // If a dead object exception was thrown -- fall through to
1417 // restart the application.
1418 }
1419
1420 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001421 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001422 }
1423
Craig Mautner6170f732013-04-02 13:05:23 -07001424 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001425 Intent intent, String resolvedType, ActivityInfo aInfo,
1426 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1427 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001428 int callingPid, int callingUid, String callingPackage,
1429 int realCallingPid, int realCallingUid, int startFlags, Bundle options,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001430 boolean ignoreTargetSecurity, boolean componentSpecified, ActivityRecord[] outActivity,
1431 ActivityContainer container, TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001432 int err = ActivityManager.START_SUCCESS;
1433
1434 ProcessRecord callerApp = null;
1435 if (caller != null) {
1436 callerApp = mService.getRecordForAppLocked(caller);
1437 if (callerApp != null) {
1438 callingPid = callerApp.pid;
1439 callingUid = callerApp.info.uid;
1440 } else {
1441 Slog.w(TAG, "Unable to find app for caller " + caller
1442 + " (pid=" + callingPid + ") when starting: "
1443 + intent.toString());
1444 err = ActivityManager.START_PERMISSION_DENIED;
1445 }
1446 }
1447
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001448 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1449
Craig Mautner6170f732013-04-02 13:05:23 -07001450 if (err == ActivityManager.START_SUCCESS) {
Craig Mautner6170f732013-04-02 13:05:23 -07001451 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001452 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001453 + " on display " + (container == null ? (mFocusedStack == null ?
1454 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1455 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1456 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001457 }
1458
1459 ActivityRecord sourceRecord = null;
1460 ActivityRecord resultRecord = null;
1461 if (resultTo != null) {
1462 sourceRecord = isInAnyStackLocked(resultTo);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001463 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
1464 "Will send result to " + resultTo + " " + sourceRecord);
Craig Mautner6170f732013-04-02 13:05:23 -07001465 if (sourceRecord != null) {
1466 if (requestCode >= 0 && !sourceRecord.finishing) {
1467 resultRecord = sourceRecord;
1468 }
1469 }
1470 }
Craig Mautner6170f732013-04-02 13:05:23 -07001471
Dianne Hackborn91097de2014-04-04 18:02:06 -07001472 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001473
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001474 if ((launchFlags & Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001475 // Transfer the result target from the source activity to the new
1476 // one being started, including any failures.
1477 if (requestCode >= 0) {
1478 ActivityOptions.abort(options);
1479 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1480 }
1481 resultRecord = sourceRecord.resultTo;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001482 if (resultRecord != null && !resultRecord.isInStackLocked()) {
1483 resultRecord = null;
1484 }
Craig Mautner6170f732013-04-02 13:05:23 -07001485 resultWho = sourceRecord.resultWho;
1486 requestCode = sourceRecord.requestCode;
1487 sourceRecord.resultTo = null;
1488 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001489 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001490 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001491 if (sourceRecord.launchedFromUid == callingUid) {
1492 // The new activity is being launched from the same uid as the previous
1493 // activity in the flow, and asking to forward its result back to the
1494 // previous. In this case the activity is serving as a trampoline between
1495 // the two, so we also want to update its launchedFromPackage to be the
1496 // same as the previous activity. Note that this is safe, since we know
1497 // these two packages come from the same uid; the caller could just as
1498 // well have supplied that same package name itself. This specifially
1499 // deals with the case of an intent picker/chooser being launched in the app
1500 // flow to redirect to an activity picked by the user, where we want the final
1501 // activity to consider it to have been launched by the previous app activity.
1502 callingPackage = sourceRecord.launchedFromPackage;
1503 }
Craig Mautner6170f732013-04-02 13:05:23 -07001504 }
1505
1506 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1507 // We couldn't find a class that can handle the given Intent.
1508 // That's the end of that!
1509 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1510 }
1511
1512 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1513 // We couldn't find the specific class specified in the Intent.
1514 // Also the end of the line.
1515 err = ActivityManager.START_CLASS_NOT_FOUND;
1516 }
1517
Dianne Hackborn91097de2014-04-04 18:02:06 -07001518 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1519 && sourceRecord.task.voiceSession != null) {
1520 // If this activity is being launched as part of a voice session, we need
1521 // to ensure that it is safe to do so. If the upcoming activity will also
1522 // be part of the voice session, we can only launch it if it has explicitly
1523 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001524 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001525 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1526 try {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001527 intent.addCategory(Intent.CATEGORY_VOICE);
Dianne Hackborn95465202014-09-15 16:21:55 -07001528 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1529 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001530 Slog.w(TAG,
1531 "Activity being started in current voice task does not support voice: "
1532 + intent);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001533 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1534 }
1535 } catch (RemoteException e) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001536 Slog.w(TAG, "Failure checking voice capabilities", e);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001537 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1538 }
1539 }
1540 }
1541
1542 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1543 // If the caller is starting a new voice session, just make sure the target
1544 // is actually allowing it to run this way.
1545 try {
1546 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1547 intent, resolvedType)) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001548 Slog.w(TAG,
1549 "Activity being started in new voice task does not support: "
1550 + intent);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001551 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1552 }
1553 } catch (RemoteException e) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001554 Slog.w(TAG, "Failure checking voice capabilities", e);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001555 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1556 }
1557 }
1558
louis_changcd5d1982014-08-01 10:09:08 +08001559 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1560
Craig Mautner6170f732013-04-02 13:05:23 -07001561 if (err != ActivityManager.START_SUCCESS) {
1562 if (resultRecord != null) {
1563 resultStack.sendActivityResultLocked(-1,
1564 resultRecord, resultWho, requestCode,
1565 Activity.RESULT_CANCELED, null);
1566 }
Craig Mautner6170f732013-04-02 13:05:23 -07001567 ActivityOptions.abort(options);
1568 return err;
1569 }
1570
Svetoslav7008b512015-06-24 18:47:07 -07001571 boolean abort = false;
1572
Svet Ganov99b60432015-06-27 13:15:22 -07001573 final int startAnyPerm = mService.checkPermission(
1574 START_ANY_ACTIVITY, callingPid, callingUid);
1575
1576 if (startAnyPerm != PERMISSION_GRANTED) {
1577 final int componentRestriction = getComponentRestrictionForCallingPackage(
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001578 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
Svet Ganov99b60432015-06-27 13:15:22 -07001579 final int actionRestriction = getActionRestrictionForCallingPackage(
1580 intent.getAction(), callingPackage, callingPid, callingUid);
1581
1582 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1583 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1584 if (resultRecord != null) {
1585 resultStack.sendActivityResultLocked(-1,
1586 resultRecord, resultWho, requestCode,
1587 Activity.RESULT_CANCELED, null);
1588 }
1589 String msg;
1590 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1591 msg = "Permission Denial: starting " + intent.toString()
1592 + " from " + callerApp + " (pid=" + callingPid
1593 + ", uid=" + callingUid + ")" + " with revoked permission "
1594 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1595 } else if (!aInfo.exported) {
1596 msg = "Permission Denial: starting " + intent.toString()
1597 + " from " + callerApp + " (pid=" + callingPid
1598 + ", uid=" + callingUid + ")"
1599 + " not exported from uid " + aInfo.applicationInfo.uid;
1600 } else {
1601 msg = "Permission Denial: starting " + intent.toString()
1602 + " from " + callerApp + " (pid=" + callingPid
1603 + ", uid=" + callingUid + ")"
1604 + " requires " + aInfo.permission;
1605 }
1606 Slog.w(TAG, msg);
1607 throw new SecurityException(msg);
1608 }
1609
1610 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1611 String message = "Appop Denial: starting " + intent.toString()
1612 + " from " + callerApp + " (pid=" + callingPid
1613 + ", uid=" + callingUid + ")"
1614 + " requires " + AppOpsManager.permissionToOp(
1615 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1616 Slog.w(TAG, message);
1617 abort = true;
1618 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1619 String message = "Appop Denial: starting " + intent.toString()
1620 + " from " + callerApp + " (pid=" + callingPid
1621 + ", uid=" + callingUid + ")"
1622 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1623 Slog.w(TAG, message);
1624 abort = true;
1625 }
Svetoslav7008b512015-06-24 18:47:07 -07001626 }
1627
1628 abort |= !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001629 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001630
Craig Mautner6170f732013-04-02 13:05:23 -07001631 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001632 try {
1633 // The Intent we give to the watcher has the extra data
1634 // stripped off, since it can contain private information.
1635 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001636 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001637 aInfo.applicationInfo.packageName);
1638 } catch (RemoteException e) {
1639 mService.mController = null;
1640 }
Ben Gruver5e207332013-04-03 17:41:37 -07001641 }
Craig Mautner6170f732013-04-02 13:05:23 -07001642
Ben Gruver5e207332013-04-03 17:41:37 -07001643 if (abort) {
1644 if (resultRecord != null) {
1645 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001646 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001647 }
Ben Gruver5e207332013-04-03 17:41:37 -07001648 // We pretend to the caller that it was really started, but
1649 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001650 ActivityOptions.abort(options);
1651 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001652 }
1653
1654 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001655 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001656 requestCode, componentSpecified, voiceSession != null, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001657 if (outActivity != null) {
1658 outActivity[0] = r;
1659 }
1660
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001661 if (r.appTimeTracker == null && sourceRecord != null) {
1662 // If the caller didn't specify an explicit time tracker, we want to continue
1663 // tracking under any it has.
1664 r.appTimeTracker = sourceRecord.appTimeTracker;
1665 }
1666
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001667 final ActivityStack stack = mFocusedStack;
Dianne Hackborn91097de2014-04-04 18:02:06 -07001668 if (voiceSession == null && (stack.mResumedActivity == null
1669 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001670 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1671 realCallingPid, realCallingUid, "Activity start")) {
Craig Mautner6170f732013-04-02 13:05:23 -07001672 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001673 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautneree36c772014-07-16 14:56:05 -07001674 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001675 ActivityOptions.abort(options);
1676 return ActivityManager.START_SWITCHES_CANCELED;
1677 }
1678 }
1679
1680 if (mService.mDidAppSwitch) {
1681 // This is the second allowed switch since we stopped switches,
1682 // so now just generally allow switches. Use case: user presses
1683 // home (switches disabled, switch to home, mDidAppSwitch now true);
1684 // user taps a home icon (coming from home so allowed, we hit here
1685 // and now allow anyone to switch again).
1686 mService.mAppSwitchesAllowedTime = 0;
1687 } else {
1688 mService.mDidAppSwitch = true;
1689 }
1690
Craig Mautneree36c772014-07-16 14:56:05 -07001691 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001692
Dianne Hackborn91097de2014-04-04 18:02:06 -07001693 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001694 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001695
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001696 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001697 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001698 // activity start, but we are not actually doing an activity
1699 // switch... just dismiss the keyguard now, because we
1700 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001701 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001702 }
1703 return err;
1704 }
1705
Svet Ganov99b60432015-06-27 13:15:22 -07001706 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001707 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001708 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1709 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001710 == PackageManager.PERMISSION_DENIED) {
1711 return ACTIVITY_RESTRICTION_PERMISSION;
1712 }
1713
Christopher Tateff7add02015-08-17 10:23:22 -07001714 if (activityInfo.permission == null) {
1715 return ACTIVITY_RESTRICTION_NONE;
1716 }
1717
Svet Ganov99b60432015-06-27 13:15:22 -07001718 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1719 if (opCode == AppOpsManager.OP_NONE) {
1720 return ACTIVITY_RESTRICTION_NONE;
1721 }
1722
1723 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1724 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001725 if (!ignoreTargetSecurity) {
1726 return ACTIVITY_RESTRICTION_APPOP;
1727 }
Svet Ganov99b60432015-06-27 13:15:22 -07001728 }
1729
1730 return ACTIVITY_RESTRICTION_NONE;
1731 }
1732
Svetoslav7008b512015-06-24 18:47:07 -07001733 private int getActionRestrictionForCallingPackage(String action,
1734 String callingPackage, int callingPid, int callingUid) {
1735 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001736 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001737 }
1738
1739 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1740 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001741 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001742 }
1743
1744 final PackageInfo packageInfo;
1745 try {
1746 packageInfo = mService.mContext.getPackageManager()
1747 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1748 } catch (PackageManager.NameNotFoundException e) {
1749 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001750 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001751 }
1752
1753 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001754 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001755 }
1756
1757 if (mService.checkPermission(permission, callingPid, callingUid) ==
1758 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001759 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001760 }
1761
1762 final int opCode = AppOpsManager.permissionToOpCode(permission);
1763 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001764 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001765 }
1766
1767 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1768 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001769 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001770 }
1771
Svet Ganov99b60432015-06-27 13:15:22 -07001772 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001773 }
1774
Chong Zhang0fa656b2015-08-31 15:17:21 -07001775 ActivityStack computeStackFocus(ActivityRecord r, boolean newTask, Rect bounds) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001776 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001777
1778 // On leanback only devices we should keep all activities in the same stack.
1779 if (!mLeanbackOnlyDevice &&
1780 (r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001781
1782 ActivityStack stack;
1783
Wale Ogunwale7d701172015-03-11 15:36:30 -07001784 if (task != null && task.stack != null) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001785 stack = task.stack;
1786 if (stack.isOnHomeDisplay()) {
1787 if (mFocusedStack != stack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001788 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1789 "computeStackFocus: Setting " + "focused stack to r=" + r
1790 + " task=" + task);
Craig Mautnere0a38842013-12-16 16:14:02 -08001791 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001792 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001793 "computeStackFocus: Focused stack already=" + mFocusedStack);
Craig Mautnere0a38842013-12-16 16:14:02 -08001794 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001795 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001796 return stack;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001797 }
1798
Craig Mautnere0a38842013-12-16 16:14:02 -08001799 final ActivityContainer container = r.mInitialActivityContainer;
1800 if (container != null) {
1801 // The first time put it on the desired stack, after this put on task stack.
1802 r.mInitialActivityContainer = null;
1803 return container.mStack;
1804 }
1805
Craig Mautner1b4bf852014-05-26 15:06:32 -07001806 if (mFocusedStack != mHomeStack && (!newTask ||
1807 mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001808 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001809 "computeStackFocus: Have a focused stack=" + mFocusedStack);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001810 return mFocusedStack;
1811 }
1812
Wale Ogunwale706ed792015-08-02 10:29:44 -07001813 // We first try to put the task in the first dynamic stack.
Craig Mautnere0a38842013-12-16 16:14:02 -08001814 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1815 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001816 stack = homeDisplayStacks.get(stackNdx);
Wale Ogunwale706ed792015-08-02 10:29:44 -07001817 final boolean isDynamicStack = stack.mStackId >= FIRST_DYNAMIC_STACK_ID;
1818 if (isDynamicStack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001819 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001820 "computeStackFocus: Setting focused stack=" + stack);
1821 return stack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001822 }
1823 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001824
Wale Ogunwale706ed792015-08-02 10:29:44 -07001825 // If there is no suitable dynamic stack then we figure out which static stack to use.
Chong Zhang0fa656b2015-08-31 15:17:21 -07001826 int stackId = task != null ? task.getLaunchStackId() :
1827 bounds != null ? FREEFORM_WORKSPACE_STACK_ID :
1828 FULLSCREEN_WORKSPACE_STACK_ID;
1829 stack = getStack(stackId, CREATE_IF_NEEDED, ON_TOP);
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001830 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS, "computeStackFocus: New stack r="
1831 + r + " stackId=" + stack.mStackId);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001832 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001833 }
1834 return mHomeStack;
1835 }
1836
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001837 boolean setFocusedStack(ActivityRecord r, String reason) {
1838 if (r == null) {
1839 // Not sure what you are trying to do, but it is not going to work...
1840 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001841 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001842 final TaskRecord task = r.task;
1843 if (task == null || task.stack == null) {
1844 Slog.w(TAG, "Can't set focus stack for r=" + r + " task=" + task);
1845 return false;
1846 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001847 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001848 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001849 }
1850
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001851 final int startActivityUncheckedLocked(final ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001852 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001853 boolean doResume, Bundle options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001854 final Intent intent = r.intent;
1855 final int callingUid = r.launchedFromUid;
1856
Chong Zhang0fa656b2015-08-31 15:17:21 -07001857 boolean overrideBounds = false;
1858 Rect newBounds = null;
1859 if (r.info.resizeable || (inTask != null && inTask.mResizeable)) {
1860 if (intent.hasExtra(ActivityOptions.KEY_BOUNDS)) {
1861 overrideBounds = true;
1862 newBounds = Rect.unflattenFromString(
1863 intent.getStringExtra(ActivityOptions.KEY_BOUNDS));
1864 } else if (options != null) {
1865 ActivityOptions opts = new ActivityOptions(options);
1866 if (opts.hasBounds()) {
1867 overrideBounds = true;
1868 newBounds = opts.getBounds();
1869 }
1870 }
1871 }
1872
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001873 // In some flows in to this function, we retrieve the task record and hold on to it
1874 // without a lock before calling back in to here... so the task at this point may
1875 // not actually be in recents. Check for that, and if it isn't in recents just
1876 // consider it invalid.
1877 if (inTask != null && !inTask.inRecents) {
1878 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1879 inTask = null;
1880 }
1881
Craig Mautnerad400af2014-08-13 16:41:36 -07001882 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001883 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1884 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001885
Craig Mautnera228ae92014-07-09 05:44:55 -07001886 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001887 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001888 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001889 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1890 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1891 "\"singleInstance\" or \"singleTask\"");
1892 launchFlags &=
1893 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1894 } else {
1895 switch (r.info.documentLaunchMode) {
1896 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1897 break;
1898 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1899 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1900 break;
1901 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1902 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1903 break;
1904 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1905 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1906 break;
1907 }
1908 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001909
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001910 final boolean launchTaskBehind = r.mLaunchTaskBehind
1911 && !launchSingleTask && !launchSingleInstance
1912 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001913
Wale Ogunwale7d701172015-03-11 15:36:30 -07001914 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0
1915 && r.resultTo.task.stack != null) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001916 // For whatever reason this activity is being launched into a new
1917 // task... yet the caller has requested a result back. Well, that
1918 // is pretty messed up, so instead immediately send back a cancel
1919 // and let the new task continue launched as normal without a
1920 // dependency on its originator.
1921 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1922 r.resultTo.task.stack.sendActivityResultLocked(-1,
1923 r.resultTo, r.resultWho, r.requestCode,
1924 Activity.RESULT_CANCELED, null);
1925 r.resultTo = null;
1926 }
1927
1928 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1929 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1930 }
1931
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001932 // If we are actually going to launch in to a new task, there are some cases where
1933 // we further want to do multiple task.
1934 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1935 if (launchTaskBehind
1936 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
1937 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1938 }
1939 }
1940
Craig Mautner8849a5e2013-04-02 16:41:03 -07001941 // We'll invoke onUserLeaving before onPause only if the launching
1942 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001943 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001944 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
1945 "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001946
1947 // If the caller has asked not to resume at this point, we make note
1948 // of this in the record so that we can skip it when trying to find
1949 // the top running activity.
Chong Zhang45c25ce2015-08-10 22:18:26 -07001950 if (!doResume || !okToShowLocked(r)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001951 r.delayedResume = true;
Chong Zhang45c25ce2015-08-10 22:18:26 -07001952 doResume = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001953 }
1954
Craig Mautnera254cd72014-05-25 16:47:39 -07001955 ActivityRecord notTop =
1956 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001957
1958 // If the onlyIfNeeded flag is set, then we can do this if the activity
1959 // being launched is the same as the one making the call... or, as
1960 // a special case, if we do not know the caller then we count the
1961 // current top activity as the caller.
1962 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1963 ActivityRecord checkedCaller = sourceRecord;
1964 if (checkedCaller == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001965 checkedCaller = mFocusedStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001966 }
1967 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1968 // Caller is not the same as launcher, so always needed.
1969 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1970 }
1971 }
1972
Craig Mautner8849a5e2013-04-02 16:41:03 -07001973 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001974 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001975
1976 // If the caller is not coming from another activity, but has given us an
1977 // explicit task into which they would like us to launch the new activity,
1978 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001979 if (sourceRecord == null && inTask != null && inTask.stack != null) {
1980 final Intent baseIntent = inTask.getBaseIntent();
1981 final ActivityRecord root = inTask.getRootActivity();
1982 if (baseIntent == null) {
1983 ActivityOptions.abort(options);
1984 throw new IllegalArgumentException("Launching into task without base intent: "
1985 + inTask);
1986 }
1987
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001988 // If this task is empty, then we are adding the first activity -- it
1989 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001990 if (launchSingleInstance || launchSingleTask) {
1991 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
1992 ActivityOptions.abort(options);
1993 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
1994 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001995 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001996 if (root != null) {
1997 ActivityOptions.abort(options);
1998 throw new IllegalArgumentException("Caller with inTask " + inTask
1999 + " has root " + root + " but target is singleInstance/Task");
2000 }
2001 }
2002
2003 // If task is empty, then adopt the interesting intent launch flags in to the
2004 // activity being started.
2005 if (root == null) {
2006 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
2007 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
2008 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
2009 launchFlags = (launchFlags&~flagsOfInterest)
2010 | (baseIntent.getFlags()&flagsOfInterest);
2011 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002012 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07002013 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002014
Dianne Hackborn962d5352014-08-29 16:27:40 -07002015 // If the task is not empty and the caller is asking to start it as the root
2016 // of a new task, then we don't actually want to start this on the task. We
2017 // will bring the task to the front, and possibly give it a new intent.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002018 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07002019 addingToTask = false;
2020
2021 } else {
2022 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002023 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07002024
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002025 reuseTask = inTask;
2026 } else {
2027 inTask = null;
2028 }
2029
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002030 if (inTask == null) {
2031 if (sourceRecord == null) {
2032 // This activity is not being started from another... in this
2033 // case we -always- start a new task.
2034 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
2035 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
2036 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
2037 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2038 }
2039 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
2040 // The original activity who is starting us is running as a single
2041 // instance... this new activity it is starting must go on its
2042 // own task.
2043 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2044 } else if (launchSingleInstance || launchSingleTask) {
2045 // The activity being started is a single instance... it always
2046 // gets launched into its own task.
2047 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2048 }
2049 }
2050
2051 ActivityInfo newTaskInfo = null;
2052 Intent newTaskIntent = null;
2053 ActivityStack sourceStack;
2054 if (sourceRecord != null) {
2055 if (sourceRecord.finishing) {
2056 // If the source is finishing, we can't further count it as our source. This
2057 // is because the task it is associated with may now be empty and on its way out,
2058 // so we don't want to blindly throw it in to that task. Instead we will take
2059 // the NEW_TASK flow and try to find a task for it. But save the task information
2060 // so it can be used when creating the new task.
2061 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
2062 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
2063 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
2064 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2065 newTaskInfo = sourceRecord.info;
2066 newTaskIntent = sourceRecord.task.intent;
2067 }
2068 sourceRecord = null;
2069 sourceStack = null;
2070 } else {
2071 sourceStack = sourceRecord.task.stack;
2072 }
2073 } else {
2074 sourceStack = null;
2075 }
2076
2077 boolean movedHome = false;
2078 ActivityStack targetStack;
2079
2080 intent.setFlags(launchFlags);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002081 final boolean noAnimation = (launchFlags & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002082
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002083 // We may want to try to place the new activity in to an existing task. We always
2084 // do this if the target activity is singleTask or singleInstance; we will also do
2085 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
2086 // us to still place it in a new task: multi task, always doc mode, or being asked to
2087 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07002088 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
2089 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07002090 || launchSingleInstance || launchSingleTask) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002091 // If bring to front is requested, and no result is requested and we have not
2092 // been given an explicit task to launch in to, and
Craig Mautner8849a5e2013-04-02 16:41:03 -07002093 // we can find a task that was started with this same
2094 // component, then instead of launching bring that one to the front.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002095 if (inTask == null && r.resultTo == null) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002096 // See if there is a task to bring to the front. If this is
2097 // a SINGLE_INSTANCE activity, there can be one and only one
2098 // instance of it in the history, and it is always in its own
2099 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07002100 ActivityRecord intentActivity = !launchSingleInstance ?
2101 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002102 if (intentActivity != null) {
Jason Monk25d237b2015-06-19 10:39:39 -04002103 // When the flags NEW_TASK and CLEAR_TASK are set, then the task gets reused
2104 // but still needs to be a lock task mode violation since the task gets
2105 // cleared out and the device would otherwise leave the locked task.
2106 if (isLockTaskModeViolation(intentActivity.task,
2107 (launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
2108 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))) {
Craig Mautner6cd6cec2015-04-01 00:02:12 -07002109 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07002110 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08002111 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2112 }
Craig Mautner29219d92013-04-16 20:19:12 -07002113 if (r.task == null) {
2114 r.task = intentActivity.task;
2115 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002116 if (intentActivity.task.intent == null) {
2117 // This task was started because of movement of
2118 // the activity based on affinity... now that we
2119 // are actually launching it, we can assign the
2120 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07002121 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002122 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002123 targetStack = intentActivity.task.stack;
2124 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002125 // If the target task is not in the front, then we need
2126 // to bring it to the front... except... well, with
2127 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
2128 // to have the same behavior as if a new instance was
2129 // being started, which means not bringing it to the front
2130 // if the caller is not itself in the front.
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002131 final ActivityStack focusStack = getFocusedStack();
2132 ActivityRecord curTop = (focusStack == null)
2133 ? null : focusStack.topRunningNonDelayedActivityLocked(notTop);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002134 boolean movedToFront = false;
Craig Mautner7504d7b2013-09-17 10:50:53 -07002135 if (curTop != null && (curTop.task != intentActivity.task ||
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002136 curTop.task != focusStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002137 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07002138 if (sourceRecord == null || (sourceStack.topActivity() != null &&
2139 sourceStack.topActivity().task == sourceRecord.task)) {
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002140 // We really do want to push this one into the user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07002141 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002142 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
2143 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002144 movedHome = true;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002145 targetStack.moveTaskToFrontLocked(intentActivity.task, noAnimation,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002146 options, r.appTimeTracker, "bringingFoundTaskToFront");
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002147 movedToFront = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002148 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07002149 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2150 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07002151 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07002152 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002153 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002154 options = null;
2155 }
2156 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002157 if (!movedToFront && doResume) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002158 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Bring to front target: " + targetStack
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002159 + " from " + intentActivity);
2160 targetStack.moveToFront("intentActivityFound");
2161 }
2162
Craig Mautner8849a5e2013-04-02 16:41:03 -07002163 // If the caller has requested that the target task be
2164 // reset, then do so.
2165 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
2166 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
2167 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002168 if ((startFlags & ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002169 // We don't need to start a new activity, and
2170 // the client said not to do anything if that
2171 // is the case, so this is it! And for paranoia, make
2172 // sure we have correctly resumed the top activity.
2173 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002174 resumeTopActivitiesLocked(targetStack, null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002175
2176 // Make sure to notify Keyguard as well if we are not running an app
2177 // transition later.
2178 if (!movedToFront) {
2179 notifyActivityDrawnForKeyguard();
2180 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002181 } else {
2182 ActivityOptions.abort(options);
2183 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002184 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002185 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2186 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002187 if ((launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002188 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002189 // The caller has requested to completely replace any
2190 // existing task with its new activity. Well that should
2191 // not be too hard...
2192 reuseTask = intentActivity.task;
2193 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07002194 reuseTask.setIntent(r);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002195 } else if ((launchFlags & FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07002196 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002197 // In this situation we want to remove all activities
2198 // from the task up to the one being started. In most
2199 // cases this means we are resetting the task to its
2200 // initial state.
2201 ActivityRecord top =
2202 intentActivity.task.performClearTaskLocked(r, launchFlags);
2203 if (top != null) {
2204 if (top.frontOfTask) {
2205 // Activity aliases may mean we use different
2206 // intents for the top activity, so make sure
2207 // the task now has the identity of the new
2208 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07002209 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002210 }
2211 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
2212 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 } else {
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002215 // A special case: we need to start the activity because it is not
2216 // currently running, and the caller has asked to clear the current
2217 // task to have this activity at the top.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002218 addingToTask = true;
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002219 // Now pretend like this activity is being started by the top of its
2220 // task, so it is put in the right place.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002221 sourceRecord = intentActivity;
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002222 TaskRecord task = sourceRecord.task;
2223 if (task != null && task.stack == null) {
2224 // Target stack got cleared when we all activities were removed
2225 // above. Go ahead and reset it.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002226 targetStack = computeStackFocus(
2227 sourceRecord, false /* newTask */, null /* bounds */);
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002228 targetStack.addTask(
2229 task, !launchTaskBehind /* toTop */, false /* moving */);
2230 }
2231
Craig Mautner8849a5e2013-04-02 16:41:03 -07002232 }
2233 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
2234 // In this case the top activity on the task is the
2235 // same as the one being launched, so we take that
2236 // as a request to bring the task to the foreground.
2237 // If the top activity in the task is the root
2238 // activity, deliver this new intent to it if it
2239 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07002240 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07002241 && intentActivity.realActivity.equals(r.realActivity)) {
2242 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
2243 intentActivity.task);
2244 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07002245 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002246 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002247 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
2248 r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002249 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
2250 // In this case we are launching the root activity
2251 // of the task, but with a different intent. We
2252 // should start a new instance on top.
2253 addingToTask = true;
2254 sourceRecord = intentActivity;
2255 }
2256 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
2257 // In this case an activity is being launched in to an
2258 // existing task, without resetting that task. This
2259 // is typically the situation of launching an activity
2260 // from a notification or shortcut. We want to place
2261 // the new activity on top of the current task.
2262 addingToTask = true;
2263 sourceRecord = intentActivity;
2264 } else if (!intentActivity.task.rootWasReset) {
2265 // In this case we are launching in to an existing task
2266 // that has not yet been started from its front door.
2267 // The current task has been brought to the front.
2268 // Ideally, we'd probably like to place this new task
2269 // at the bottom of its stack, but that's a little hard
2270 // to do with the current organization of the code so
2271 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07002272 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002273 }
2274 if (!addingToTask && reuseTask == null) {
2275 // We didn't do anything... but it was needed (a.k.a., client
2276 // don't use that intent!) And for paranoia, make
2277 // sure we have correctly resumed the top activity.
2278 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002279 targetStack.resumeTopActivityLocked(null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002280 if (!movedToFront) {
2281 // Make sure to notify Keyguard as well if we are not running an app
2282 // transition later.
2283 notifyActivityDrawnForKeyguard();
2284 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002285 } else {
2286 ActivityOptions.abort(options);
2287 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002288 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002289 return ActivityManager.START_TASK_TO_FRONT;
2290 }
2291 }
2292 }
2293 }
2294
2295 //String uri = r.intent.toURI();
2296 //Intent intent2 = new Intent(uri);
2297 //Slog.i(TAG, "Given intent: " + r.intent);
2298 //Slog.i(TAG, "URI is: " + uri);
2299 //Slog.i(TAG, "To intent: " + intent2);
2300
2301 if (r.packageName != null) {
2302 // If the activity being launched is the same as the one currently
2303 // at the top, then we need to check if it should only be launched
2304 // once.
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002305 ActivityStack topStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002306 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002307 if (top != null && r.resultTo == null) {
2308 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2309 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07002310 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07002311 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002312 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
2313 top.task);
2314 // For paranoia, make sure we have correctly
2315 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002316 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002317 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002318 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002319 }
2320 ActivityOptions.abort(options);
2321 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2322 // We don't need to start a new activity, and
2323 // the client said not to do anything if that
2324 // is the case, so this is it!
2325 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2326 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002327 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002328 return ActivityManager.START_DELIVERED_TO_TOP;
2329 }
2330 }
2331 }
2332 }
2333
2334 } else {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002335 if (r.resultTo != null && r.resultTo.task.stack != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002336 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2337 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002338 }
2339 ActivityOptions.abort(options);
2340 return ActivityManager.START_CLASS_NOT_FOUND;
2341 }
2342
2343 boolean newTask = false;
2344 boolean keepCurTransition = false;
2345
Craig Mautnerbb742462014-07-07 15:28:55 -07002346 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002347 sourceRecord.task : null;
2348
Craig Mautner8849a5e2013-04-02 16:41:03 -07002349 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002350 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002351 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002352 newTask = true;
Chong Zhang0fa656b2015-08-31 15:17:21 -07002353 targetStack = computeStackFocus(r, newTask, newBounds);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002354 if (doResume) {
2355 targetStack.moveToFront("startingNewTask");
2356 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002357
Craig Mautner8849a5e2013-04-02 16:41:03 -07002358 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002359 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2360 newTaskInfo != null ? newTaskInfo : r.info,
2361 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002362 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2363 taskToAffiliate);
Chong Zhang0fa656b2015-08-31 15:17:21 -07002364 if (overrideBounds) {
2365 r.task.updateOverrideConfiguration(newBounds);
2366 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002367 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2368 "Starting new activity " + r + " in new task " + r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002369 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002370 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002371 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002372 if (isLockTaskModeViolation(r.task)) {
2373 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2374 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2375 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002376 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002377 if ((launchFlags &
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002378 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2379 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002380 // Caller wants to appear on home activity, so before starting
2381 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002382 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002383 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002384 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002385 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002386 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002387 if (isLockTaskModeViolation(sourceTask)) {
2388 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2389 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2390 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002391 targetStack = sourceTask.stack;
Chong Zhang45c25ce2015-08-10 22:18:26 -07002392 if (doResume) {
2393 targetStack.moveToFront("sourceStackToFront");
2394 }
Craig Mautner737fae22014-10-15 12:52:10 -07002395 final TaskRecord topTask = targetStack.topTask();
2396 if (topTask != sourceTask) {
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002397 targetStack.moveTaskToFrontLocked(sourceTask, noAnimation, options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002398 r.appTimeTracker, "sourceTaskToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002399 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002400 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002401 // In this case, we are adding the activity to an existing
2402 // task, but the caller has asked to clear that task if the
2403 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002404 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002405 keepCurTransition = true;
2406 if (top != null) {
2407 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002408 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002409 // For paranoia, make sure we have correctly
2410 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002411 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002412 if (doResume) {
2413 targetStack.resumeTopActivityLocked(null);
2414 }
2415 ActivityOptions.abort(options);
2416 return ActivityManager.START_DELIVERED_TO_TOP;
2417 }
2418 } else if (!addingToTask &&
2419 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2420 // In this case, we are launching an activity in our own task
2421 // that may already be running somewhere in the history, and
2422 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002423 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002424 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002425 final TaskRecord task = top.task;
2426 task.moveActivityToFrontLocked(top);
2427 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002428 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002429 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002430 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002431 if (doResume) {
2432 targetStack.resumeTopActivityLocked(null);
2433 }
2434 return ActivityManager.START_DELIVERED_TO_TOP;
2435 }
2436 }
2437 // An existing activity is starting this new activity, so we want
2438 // to keep the new one in the same task as the one that is starting
2439 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002440 r.setTask(sourceTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002441 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002442 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002443
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002444 } else if (inTask != null) {
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002445 // The caller is asking that the new activity be started in an explicit
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002446 // task it has provided to us.
2447 if (isLockTaskModeViolation(inTask)) {
2448 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2449 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2450 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002451 if (overrideBounds) {
2452 inTask.updateOverrideConfiguration(newBounds);
2453 int stackId = inTask.getLaunchStackId();
2454 if (stackId != inTask.stack.mStackId) {
2455 moveTaskToStackUncheckedLocked(
2456 inTask, stackId, ON_TOP, !FORCE_FOCUS, "inTaskToFront");
2457 }
2458 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002459 targetStack = inTask.stack;
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002460 targetStack.moveTaskToFrontLocked(inTask, noAnimation, options, r.appTimeTracker,
2461 "inTaskToFront");
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002462
2463 // Check whether we should actually launch the new activity in to the task,
2464 // or just reuse the current activity on top.
2465 ActivityRecord top = inTask.getTopActivity();
2466 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2467 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2468 || launchSingleTop || launchSingleTask) {
2469 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2470 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2471 // We don't need to start a new activity, and
2472 // the client said not to do anything if that
2473 // is the case, so this is it!
2474 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2475 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002476 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002477 return ActivityManager.START_DELIVERED_TO_TOP;
2478 }
2479 }
2480
Dianne Hackborn962d5352014-08-29 16:27:40 -07002481 if (!addingToTask) {
2482 // We don't actually want to have this activity added to the task, so just
2483 // stop here but still tell the caller that we consumed the intent.
2484 ActivityOptions.abort(options);
2485 return ActivityManager.START_TASK_TO_FRONT;
2486 }
2487
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002488 r.setTask(inTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002489 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002490 + " in explicit task " + r.task);
2491
Craig Mautner8849a5e2013-04-02 16:41:03 -07002492 } else {
2493 // This not being started from an existing activity, and not part
2494 // of a new task... just put it in the top task, though these days
2495 // this case should never happen.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002496 targetStack = computeStackFocus(r, newTask, null /* bounds */);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002497 if (doResume) {
2498 targetStack.moveToFront("addingToTopTask");
2499 }
Craig Mautner1602ec22013-05-12 10:24:27 -07002500 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002501 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002502 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002503 mWindowManager.moveTaskToTop(r.task.taskId);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002504 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Craig Mautner8849a5e2013-04-02 16:41:03 -07002505 + " in new guessed " + r.task);
2506 }
2507
2508 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002509 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002510
Craig Mautner76e2a762014-06-24 09:05:43 -07002511 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2512 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2513 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002514 if (newTask) {
2515 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2516 }
2517 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002518 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002519 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002520 if (!launchTaskBehind && doResume) {
Craig Mautner299f9602015-01-26 09:47:33 -08002521 mService.setFocusedActivityLocked(r, "startedActivity");
Craig Mautnerbb742462014-07-07 15:28:55 -07002522 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002523 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002524 return ActivityManager.START_SUCCESS;
2525 }
2526
Craig Mautneree36c772014-07-16 14:56:05 -07002527 final void doPendingActivityLaunchesLocked(boolean doResume) {
2528 while (!mPendingActivityLaunches.isEmpty()) {
2529 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Craig Mautneraa9b0f12014-07-17 10:50:18 -07002530 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002531 doResume && mPendingActivityLaunches.isEmpty(), null, null);
Craig Mautneree36c772014-07-16 14:56:05 -07002532 }
2533 }
2534
Craig Mautner7f13ed32014-07-28 14:00:35 -07002535 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002536 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2537 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002538 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002539 mPendingActivityLaunches.remove(palNdx);
2540 }
2541 }
2542 }
2543
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002544 void setLaunchSource(int uid) {
2545 mLaunchingActivity.setWorkSource(new WorkSource(uid));
2546 }
2547
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002548 void acquireLaunchWakelock() {
2549 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2550 throw new IllegalStateException("Calling must be system uid");
2551 }
2552 mLaunchingActivity.acquire();
2553 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2554 // To be safe, don't allow the wake lock to be held for too long.
2555 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2556 }
2557 }
2558
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002559 /**
2560 * Called when the frontmost task is idle.
2561 * @return the state of mService.mBooting before this was called.
2562 */
2563 private boolean checkFinishBootingLocked() {
2564 final boolean booting = mService.mBooting;
2565 boolean enableScreen = false;
2566 mService.mBooting = false;
2567 if (!mService.mBooted) {
2568 mService.mBooted = true;
2569 enableScreen = true;
2570 }
2571 if (booting || enableScreen) {
2572 mService.postFinishBooting(booting, enableScreen);
2573 }
2574 return booting;
2575 }
2576
Craig Mautnerf3333272013-04-22 10:55:53 -07002577 // Checked.
2578 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2579 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002580 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002581
Craig Mautnerf3333272013-04-22 10:55:53 -07002582 ArrayList<ActivityRecord> stops = null;
2583 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07002584 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07002585 int NS = 0;
2586 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002587 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002588 boolean activityRemoved = false;
2589
Wale Ogunwale7d701172015-03-11 15:36:30 -07002590 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002591 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002592 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
2593 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002594 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2595 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002596 if (fromTimeout) {
2597 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002598 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002599
2600 // This is a hack to semi-deal with a race condition
2601 // in the client where it can be constructed with a
2602 // newer configuration from when we asked it to launch.
2603 // We'll update with whatever configuration it now says
2604 // it used to launch.
2605 if (config != null) {
2606 r.configuration = config;
2607 }
2608
2609 // We are now idle. If someone is waiting for a thumbnail from
2610 // us, we can now deliver.
2611 r.idle = true;
2612
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002613 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Dianne Hackborn9449a612014-10-01 15:53:28 -07002614 if (isFrontStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002615 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002616 }
2617 }
2618
2619 if (allResumedActivitiesIdle()) {
2620 if (r != null) {
2621 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002622 }
2623
2624 if (mLaunchingActivity.isHeld()) {
2625 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2626 if (VALIDATE_WAKE_LOCK_CALLER &&
2627 Binder.getCallingUid() != Process.myUid()) {
2628 throw new IllegalStateException("Calling must be system uid");
2629 }
2630 mLaunchingActivity.release();
2631 }
2632 ensureActivitiesVisibleLocked(null, 0);
Craig Mautnerf3333272013-04-22 10:55:53 -07002633 }
2634
2635 // Atomically retrieve all of the other things to do.
2636 stops = processStoppingActivitiesLocked(true);
2637 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002638 if ((NF = mFinishingActivities.size()) > 0) {
2639 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002640 mFinishingActivities.clear();
2641 }
2642
Craig Mautnerf3333272013-04-22 10:55:53 -07002643 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002644 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07002645 mStartingUsers.clear();
2646 }
2647
Craig Mautnerf3333272013-04-22 10:55:53 -07002648 // Stop any activities that are scheduled to do so but have been
2649 // waiting for the next one to start.
2650 for (int i = 0; i < NS; i++) {
2651 r = stops.get(i);
2652 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002653 if (stack != null) {
2654 if (r.finishing) {
2655 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2656 } else {
2657 stack.stopActivityLocked(r);
2658 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002659 }
2660 }
2661
2662 // Finish any activities that are scheduled to do so but have been
2663 // waiting for the next one to start.
2664 for (int i = 0; i < NF; i++) {
2665 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07002666 final ActivityStack stack = r.task.stack;
2667 if (stack != null) {
2668 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
2669 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002670 }
2671
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002672 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002673 // Complete user switch
2674 if (startingUsers != null) {
2675 for (int i = 0; i < startingUsers.size(); i++) {
2676 mService.finishUserSwitch(startingUsers.get(i));
2677 }
2678 }
2679 // Complete starting up of background users
2680 if (mStartingBackgroundUsers.size() > 0) {
Amith Yamasani37a40c22015-06-17 13:25:42 -07002681 startingUsers = new ArrayList<UserState>(mStartingBackgroundUsers);
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002682 mStartingBackgroundUsers.clear();
2683 for (int i = 0; i < startingUsers.size(); i++) {
2684 mService.finishUserBoot(startingUsers.get(i));
2685 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002686 }
2687 }
2688
2689 mService.trimApplications();
2690 //dump();
2691 //mWindowManager.dump();
2692
Craig Mautnerf3333272013-04-22 10:55:53 -07002693 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002694 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002695 }
2696
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002697 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002698 }
2699
Craig Mautner8e569572013-10-11 17:36:59 -07002700 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002701 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002702 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2703 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002704 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2705 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2706 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002707 }
Craig Mautner19091252013-10-05 00:03:53 -07002708 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002709 }
2710
2711 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002712 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2713 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002714 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2715 stacks.get(stackNdx).closeSystemDialogsLocked();
2716 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002717 }
2718 }
2719
Craig Mautner93529a42013-10-04 15:03:13 -07002720 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002721 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002722 }
2723
Craig Mautner8d341ef2013-03-26 09:03:27 -07002724 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07002725 * Update the last used stack id for non-current user (current user's last
2726 * used stack is the focused stack)
2727 */
2728 void updateUserStackLocked(int userId, ActivityStack stack) {
2729 if (userId != mCurrentUser) {
2730 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
2731 }
2732 }
2733
2734 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07002735 * @return true if some activity was finished (or would have finished if doit were true).
2736 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07002737 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
2738 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002739 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002740 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2741 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002742 final int numStacks = stacks.size();
2743 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2744 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07002745 if (stack.finishDisabledPackageActivitiesLocked(
2746 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002747 didSomething = true;
2748 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002749 }
2750 }
2751 return didSomething;
2752 }
2753
Dianne Hackborna413dc02013-07-12 12:02:55 -07002754 void updatePreviousProcessLocked(ActivityRecord r) {
2755 // Now that this process has stopped, we may want to consider
2756 // it to be the previous app to try to keep around in case
2757 // the user wants to return to it.
2758
2759 // First, found out what is currently the foreground app, so that
2760 // we don't blow away the previous app if this activity is being
2761 // hosted by the process that is actually still the foreground.
2762 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002763 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2764 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002765 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2766 final ActivityStack stack = stacks.get(stackNdx);
2767 if (isFrontStack(stack)) {
2768 if (stack.mResumedActivity != null) {
2769 fgApp = stack.mResumedActivity.app;
2770 } else if (stack.mPausingActivity != null) {
2771 fgApp = stack.mPausingActivity.app;
2772 }
2773 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002774 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002775 }
2776 }
2777
2778 // Now set this one as the previous process, only if that really
2779 // makes sense to.
2780 if (r.app != null && fgApp != null && r.app != fgApp
2781 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002782 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002783 mService.mPreviousProcess = r.app;
2784 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2785 }
2786 }
2787
Craig Mautner05d29032013-05-03 13:40:13 -07002788 boolean resumeTopActivitiesLocked() {
2789 return resumeTopActivitiesLocked(null, null, null);
2790 }
2791
2792 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2793 Bundle targetOptions) {
2794 if (targetStack == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002795 targetStack = mFocusedStack;
Craig Mautner05d29032013-05-03 13:40:13 -07002796 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002797 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002798 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002799 if (isFrontStack(targetStack)) {
2800 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2801 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002802
Craig Mautnere0a38842013-12-16 16:14:02 -08002803 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2804 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002805 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2806 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002807 if (stack == targetStack) {
2808 // Already started above.
2809 continue;
2810 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002811 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002812 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002813 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002814 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002815 }
Craig Mautner05d29032013-05-03 13:40:13 -07002816 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002817 }
2818
Todd Kennedy539db512014-12-15 09:57:55 -08002819 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002820 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2821 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002822 final int numStacks = stacks.size();
2823 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2824 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08002825 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002826 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002827 }
2828 }
2829
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002830 void finishVoiceTask(IVoiceInteractionSession session) {
2831 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2832 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2833 final int numStacks = stacks.size();
2834 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2835 final ActivityStack stack = stacks.get(stackNdx);
2836 stack.finishVoiceTask(session);
2837 }
2838 }
2839 }
2840
Craig Mautner299f9602015-01-26 09:47:33 -08002841 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002842 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2843 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002844 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002845 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2846 // Caller wants the home activity moved with it. To accomplish this,
2847 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002848 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002849 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07002850 if (task.stack == null) {
2851 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2852 + task + " to front. Stack is null");
2853 return;
2854 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002855
2856 int stackId = task.stack.mStackId;
2857 if (task.mResizeable && options != null) {
2858 ActivityOptions opts = new ActivityOptions(options);
2859 if (opts.hasBounds()) {
2860 Rect bounds = opts.getBounds();
2861 task.updateOverrideConfiguration(bounds);
2862 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig, false);
2863 stackId = task.getLaunchStackId();
2864 }
2865 }
2866
2867 if (stackId != task.stack.mStackId) {
2868 moveTaskToStackUncheckedLocked(task, stackId, ON_TOP, FORCE_FOCUS, reason);
2869 } else {
2870 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002871 task.getTopActivity() == null ? null : task.getTopActivity().appTimeTracker,
2872 reason);
Chong Zhang0fa656b2015-08-31 15:17:21 -07002873 }
2874
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002875 if (DEBUG_STACK) Slog.d(TAG_STACK,
2876 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002877 }
2878
Craig Mautner967212c2013-04-13 21:10:58 -07002879 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07002880 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002881 }
2882
2883 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002884 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2885 if (activityContainer != null) {
2886 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002887 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002888 if (!createStaticStackIfNeeded
2889 || (stackId < FIRST_STATIC_STACK_ID || stackId > LAST_STATIC_STACK_ID)) {
2890 return null;
2891 }
2892 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002893 }
2894
Craig Mautner967212c2013-04-13 21:10:58 -07002895 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002896 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002897 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2898 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002899 }
2900 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002901 }
2902
Craig Mautner4a1cb222013-12-04 16:14:06 -08002903 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002904 ActivityRecord homeActivity = getHomeActivity();
2905 if (homeActivity != null) {
2906 return homeActivity.appToken;
2907 }
2908 return null;
2909 }
2910
2911 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002912 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002913 }
2914
2915 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002916 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2917 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2918 final TaskRecord task = tasks.get(taskNdx);
2919 if (task.isHomeTask()) {
2920 final ArrayList<ActivityRecord> activities = task.mActivities;
2921 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2922 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002923 if (r.isHomeActivity()
2924 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002925 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002926 }
2927 }
2928 }
2929 }
2930 return null;
2931 }
2932
Todd Kennedyca4d8422015-01-15 15:19:22 -08002933 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002934 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002935 ActivityContainer activityContainer =
2936 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002937 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002938 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2939 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002940 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002941 return activityContainer;
2942 }
2943
Craig Mautner34b73df2014-01-12 21:11:08 -08002944 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002945 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2946 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2947 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002948 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2949 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002950 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002951 }
2952 }
2953
Craig Mautner95da1082014-02-24 17:54:35 -08002954 void deleteActivityContainer(IActivityContainer container) {
2955 ActivityContainer activityContainer = (ActivityContainer)container;
2956 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002957 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2958 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08002959 final int stackId = activityContainer.mStackId;
2960 mActivityContainers.remove(stackId);
2961 mWindowManager.removeStack(stackId);
2962 }
2963 }
2964
Wale Ogunwale60454db2015-01-23 16:05:07 -08002965 void resizeStackLocked(int stackId, Rect bounds) {
2966 final ActivityStack stack = getStack(stackId);
2967 if (stack == null) {
2968 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2969 return;
2970 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002971
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002972 ActivityRecord r = stack.topRunningActivityLocked(null);
2973 final boolean resizeTasks = r != null && r.task.mResizeable;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002974
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002975 mTmpBounds.clear();
2976 mTmpConfigs.clear();
2977 if (resizeTasks) {
2978 ArrayList<TaskRecord> tasks = stack.getAllTasks();
2979 for (int i = tasks.size() - 1; i >= 0; i--) {
2980 TaskRecord task = tasks.get(i);
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07002981 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
2982 // For freeform stack we don't adjust the size of the tasks to match that of
2983 // the stack, but we do try to make sure the tasks are still contained with the
2984 // bounds of the stack.
2985 tempRect2.set(task.mBounds);
2986 fitWithinBounds(tempRect2, bounds);
2987 task.updateOverrideConfiguration(tempRect2);
2988 } else {
2989 task.updateOverrideConfiguration(bounds);
2990 }
2991
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002992 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
2993 mTmpBounds.put(task.taskId, task.mBounds);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002994 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002995 }
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002996 stack.mFullscreen = mWindowManager.resizeStack(stackId, bounds, resizeTasks, mTmpConfigs,
2997 mTmpBounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002998 if (stack.mStackId == DOCKED_STACK_ID) {
2999 // Dock stack funness...Yay!
3000 if (stack.mFullscreen) {
3001 // The dock stack went fullscreen which is kinda like dismissing it.
3002 // In this case we make all other static stacks fullscreen and move all
3003 // docked stack tasks to the fullscreen stack.
3004 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
3005 if (i != DOCKED_STACK_ID) {
3006 resizeStackLocked(i, null);
3007 }
3008 }
3009
3010 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
3011 final int count = tasks.size();
3012 for (int i = 0; i < count; i++) {
3013 moveTaskToStackLocked(tasks.get(i).taskId,
3014 FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP, FORCE_FOCUS);
3015 }
3016
3017 // stack shouldn't contain anymore activities, so nothing to resume.
3018 r = null;
3019 } else {
3020 // Docked stacks occupy a dedicated region on screen so the size of all other
3021 // static stacks need to be adjusted so they don't overlap with the docked stack.
3022 final int leftChange = stack.mBounds.left - bounds.left;
3023 final int rightChange = stack.mBounds.right - bounds.right;
3024 final int topChange = stack.mBounds.top - bounds.top;
3025 final int bottomChange = stack.mBounds.bottom - bounds.bottom;
3026
3027 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
3028 if (i != DOCKED_STACK_ID) {
3029 ActivityStack otherStack = getStack(i);
3030 if (otherStack != null) {
3031 tempRect.set(otherStack.mBounds);
3032 // We adjust the opposing sides of the other stacks to
3033 // the side in the dock stack that changed.
3034 tempRect.left -= rightChange;
3035 tempRect.right -= leftChange;
3036 tempRect.top -= bottomChange;
3037 tempRect.bottom -= topChange;
3038 resizeStackLocked(i, tempRect);
3039 }
3040 }
3041 }
3042
3043 }
3044 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003045 stack.setBounds(bounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003046
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003047 if (r != null) {
3048 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0);
3049 // And we need to make sure at this point that all other activities
3050 // are made visible with the correct configuration.
3051 ensureActivitiesVisibleLocked(r, 0);
3052 if (!updated) {
3053 resumeTopActivitiesLocked(stack, null, null);
3054 }
3055 }
3056 }
3057
3058 void resizeTaskLocked(TaskRecord task, Rect bounds) {
3059 if (!task.mResizeable) {
3060 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
3061 return;
3062 }
3063
3064 if (task.mBounds != null && task.mBounds.equals(bounds)) {
3065 // Nothing to do here...
3066 return;
3067 }
3068
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003069 if (!mWindowManager.isValidTaskId(task.taskId)) {
3070 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07003071 // All we can do for now is update the bounds so it can be used when the task is
3072 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07003073 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003074 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
3075 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07003076 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003077 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003078 return;
3079 }
3080
Wale Ogunwale040b4702015-08-06 18:10:50 -07003081 // The stack of a task is determined by its size (fullscreen vs non-fullscreen).
3082 // Place the task in the right stack if it isn't there already based on the requested
3083 // bounds.
3084 int stackId = task.stack.mStackId;
3085 if (bounds == null && stackId != FULLSCREEN_WORKSPACE_STACK_ID) {
3086 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003087 } else if (bounds != null
3088 && stackId != FREEFORM_WORKSPACE_STACK_ID && stackId != DOCKED_STACK_ID) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07003089 stackId = FREEFORM_WORKSPACE_STACK_ID;
3090 }
3091 if (stackId != task.stack.mStackId) {
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003092 moveTaskToStackUncheckedLocked(task, stackId, ON_TOP, !FORCE_FOCUS, "resizeTask");
Wale Ogunwale040b4702015-08-06 18:10:50 -07003093 }
3094
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07003095 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003096 // This variable holds information whether the configuration didn't change in a signficant
3097 // way and the activity was kept the way it was. If it's false, it means the activity had
3098 // to be relaunched due to configuration change.
3099 boolean kept = true;
3100 if (overrideConfig != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003101 ActivityRecord r = task.topRunningActivityLocked(null);
Wale Ogunwale60454db2015-01-23 16:05:07 -08003102 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003103 final ActivityStack stack = task.stack;
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003104 kept = stack.ensureActivityConfigurationLocked(r, 0);
3105 // All other activities must be made visible with their correct configuration.
Wale Ogunwale60454db2015-01-23 16:05:07 -08003106 ensureActivitiesVisibleLocked(r, 0);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003107 if (!kept) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08003108 resumeTopActivitiesLocked(stack, null, null);
Filip Gruszczynski76cc44f2015-09-03 16:03:10 -07003109 // We are about to relaunch the activity because its configuration changed due
3110 // to size change. The activity will first remove the old window and then add a
3111 // new one. This call will tell window manager about this, so it can preserve
3112 // the old window until the new one is drawn. This prevents having a gap between
Filip Gruszczynski55a309f2015-09-04 17:15:01 -07003113 // the removal and addition, in which no window is visible. If we also changed
3114 // the stack to the fullscreen stack, i.e. maximized the window, we will animate
3115 // the transition.
3116 mWindowManager.setReplacingWindow(r.appToken,
3117 stackId == FULLSCREEN_WORKSPACE_STACK_ID /* animate */);
Wale Ogunwale60454db2015-01-23 16:05:07 -08003118 }
3119 }
3120 }
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003121 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig, kept);
Wale Ogunwale60454db2015-01-23 16:05:07 -08003122 }
3123
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003124 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003125 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3126 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08003127 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003128 }
3129
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003130 ActivityContainer activityContainer = new ActivityContainer(stackId);
3131 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003132 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08003133 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003134 }
3135
3136 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07003137 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003138 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
3139 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07003140 break;
3141 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003142 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003143 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003144 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003145 }
3146
Chong Zhang5dcb2752015-08-18 13:50:26 -07003147 /**
3148 * Restores a recent task to a stack
3149 * @param task The recent task to be restored.
3150 * @param stackId The stack to restore the task to (default launch stack will be used
3151 * if stackId is invalid).
3152 * @return true if the task has been restored successfully.
3153 */
3154 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
3155 if (stackId == INVALID_STACK_ID) {
Chong Zhang0fa656b2015-08-31 15:17:21 -07003156 stackId = mLeanbackOnlyDevice ? mHomeStack.mStackId : task.getLaunchStackId();
Chong Zhang5dcb2752015-08-18 13:50:26 -07003157 }
Wale Ogunwale706ed792015-08-02 10:29:44 -07003158 if (task.stack != null) {
3159 // Task has already been restored once. See if we need to do anything more
3160 if (task.stack.mStackId == stackId) {
3161 // Nothing else to do since it is already restored in the right stack.
3162 return true;
3163 }
3164 // Remove current stack association, so we can re-associate the task with the
3165 // right stack below.
Wale Ogunwale040b4702015-08-06 18:10:50 -07003166 task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003167 }
3168
3169 ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07003170 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003171
3172 if (stack == null) {
3173 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003174 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3175 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003176 return false;
3177 }
3178
3179 stack.addTask(task, false, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003180 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3181 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003182 final ArrayList<ActivityRecord> activities = task.mActivities;
3183 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003184 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003185 }
3186 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07003187 }
3188
Wale Ogunwale040b4702015-08-06 18:10:50 -07003189 /**
3190 * Moves the specified task record to the input stack id.
3191 * WARNING: This method performs an unchecked/raw move of the task and
3192 * can leave the system in an unstable state if used incorrectly.
3193 * Use {@link #moveTaskToStackLocked} to perform safe task movement
3194 * to a stack.
3195 * @param task Task to move.
3196 * @param stackId Id of stack to move task to.
3197 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07003198 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07003199 * @param reason Reason the task is been moved.
3200 * @return The stack the task was moved to.
3201 */
3202 private ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07003203 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
3204 final ActivityRecord r = task.getTopActivity();
3205 final boolean wasFocused = isFrontStack(task.stack) && (topRunningActivityLocked() == r);
3206 final boolean wasResumed = wasFocused && (task.stack.mResumedActivity == r);
3207
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003208 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwale040b4702015-08-06 18:10:50 -07003209 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
3210 if (task.stack != null) {
3211 task.stack.removeTask(task, reason, MOVING);
3212 }
3213 stack.addTask(task, toTop, MOVING);
Chong Zhang02898352015-08-21 17:27:14 -07003214
3215 // If the task had focus before (or we're requested to move focus),
3216 // move focus to the new stack.
3217 if (forceFocus || wasFocused) {
3218 // If the task owns the last resumed activity, transfer that together,
3219 // so that we don't resume the same activity again in the new stack.
3220 // Apps may depend on onResume()/onPause() being called in pairs.
3221 if (wasResumed) {
3222 stack.mResumedActivity = r;
3223 }
3224 // move the stack in which we are placing the task to the front.
3225 stack.moveToFront(reason);
3226 }
3227
Wale Ogunwale040b4702015-08-06 18:10:50 -07003228 return stack;
3229 }
3230
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003231 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003232 final TaskRecord task = anyTaskForIdLocked(taskId);
3233 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08003234 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003235 return;
3236 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003237 final String reason = "moveTaskToStack";
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003238 final ActivityStack stack =
Chong Zhang02898352015-08-21 17:27:14 -07003239 moveTaskToStackUncheckedLocked(task, stackId, toTop, forceFocus, reason);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003240
3241 // Make sure the task has the appropriate bounds/size for the stack it is in.
3242 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003243 resizeTaskLocked(task, stack.mBounds);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003244 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID
3245 && task.mBounds == null && task.mLastNonFullscreenBounds != null) {
3246 resizeTaskLocked(task, task.mLastNonFullscreenBounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003247 } else if (stackId == DOCKED_STACK_ID) {
3248 resizeTaskLocked(task, stack.mBounds);
3249 }
3250
Stefan Kuhne54714cd2015-05-12 13:42:18 -07003251 // The task might have already been running and its visibility needs to be synchronized with
3252 // the visibility of the stack / windows.
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003253 ensureActivitiesVisibleLocked(null, 0);
Craig Mautner05d29032013-05-03 13:40:13 -07003254 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003255 }
3256
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003257 void positionTaskInStackLocked(int taskId, int stackId, int position) {
3258 final TaskRecord task = anyTaskForIdLocked(taskId);
3259 if (task == null) {
3260 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
3261 return;
3262 }
3263 ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07003264 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003265 mWindowManager.positionTaskInStack(taskId, stackId, position);
3266 final boolean stackChanged = task.stack != null && task.stack != stack;
3267 if (stackChanged) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07003268 task.stack.removeTask(task, "moveTaskToStack", MOVING);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003269 }
3270 stack.positionTask(task, position, stackChanged);
3271 // The task might have already been running and its visibility needs to be synchronized with
3272 // the visibility of the stack / windows.
3273 stack.ensureActivitiesVisibleLocked(null, 0);
3274 resumeTopActivitiesLocked();
3275 }
3276
Craig Mautnerac6f8432013-07-17 13:24:59 -07003277 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003278 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003279 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3280 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003281 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3282 final ActivityStack stack = stacks.get(stackNdx);
3283 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003284 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003285 continue;
3286 }
3287 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003288 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
3289 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003290 continue;
3291 }
3292 final ActivityRecord ar = stack.findTaskLocked(r);
3293 if (ar != null) {
3294 return ar;
3295 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003296 }
3297 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003298 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07003299 return null;
3300 }
3301
3302 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003303 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3304 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003305 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3306 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
3307 if (ar != null) {
3308 return ar;
3309 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003310 }
3311 }
3312 return null;
3313 }
3314
Craig Mautner8d341ef2013-03-26 09:03:27 -07003315 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003316 scheduleSleepTimeout();
3317 if (!mGoingToSleep.isHeld()) {
3318 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003319 if (mLaunchingActivity.isHeld()) {
3320 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3321 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003322 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003323 mLaunchingActivity.release();
3324 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003325 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003326 }
Amith Yamasanice15e152013-09-19 12:30:32 -07003327 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003328 }
3329
3330 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003331 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003332
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003333 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003334 final long endTime = System.currentTimeMillis() + timeout;
3335 while (true) {
3336 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003337 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3338 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003339 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3340 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
3341 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003342 }
3343 if (cantShutdown) {
3344 long timeRemaining = endTime - System.currentTimeMillis();
3345 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003346 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003347 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003348 } catch (InterruptedException e) {
3349 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003350 } else {
3351 Slog.w(TAG, "Activity manager shutdown timed out");
3352 timedout = true;
3353 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003354 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003355 } else {
3356 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003357 }
3358 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003359
3360 // Force checkReadyForSleep to complete.
3361 mSleepTimeout = true;
3362 checkReadyForSleepLocked();
3363
Craig Mautner8d341ef2013-03-26 09:03:27 -07003364 return timedout;
3365 }
3366
3367 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003368 removeSleepTimeouts();
3369 if (mGoingToSleep.isHeld()) {
3370 mGoingToSleep.release();
3371 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003372 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3373 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003374 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3375 final ActivityStack stack = stacks.get(stackNdx);
3376 stack.awakeFromSleepingLocked();
3377 if (isFrontStack(stack)) {
3378 resumeTopActivitiesLocked();
3379 }
Craig Mautner5314a402013-09-26 12:40:16 -07003380 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003381 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003382 mGoingToSleepActivities.clear();
3383 }
3384
3385 void activitySleptLocked(ActivityRecord r) {
3386 mGoingToSleepActivities.remove(r);
3387 checkReadyForSleepLocked();
3388 }
3389
3390 void checkReadyForSleepLocked() {
3391 if (!mService.isSleepingOrShuttingDown()) {
3392 // Do not care.
3393 return;
3394 }
3395
3396 if (!mSleepTimeout) {
3397 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003398 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3399 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003400 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3401 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3402 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003403 }
3404
3405 if (mStoppingActivities.size() > 0) {
3406 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003407 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003408 + mStoppingActivities.size() + " activities");
3409 scheduleIdleLocked();
3410 dontSleep = true;
3411 }
3412
3413 if (mGoingToSleepActivities.size() > 0) {
3414 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003415 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003416 + mGoingToSleepActivities.size() + " activities");
3417 dontSleep = true;
3418 }
3419
3420 if (dontSleep) {
3421 return;
3422 }
3423 }
3424
Craig Mautnere0a38842013-12-16 16:14:02 -08003425 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3426 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003427 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3428 stacks.get(stackNdx).goToSleep();
3429 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003430 }
3431
3432 removeSleepTimeouts();
3433
3434 if (mGoingToSleep.isHeld()) {
3435 mGoingToSleep.release();
3436 }
3437 if (mService.mShuttingDown) {
3438 mService.notifyAll();
3439 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003440 }
3441
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003442 boolean reportResumedActivityLocked(ActivityRecord r) {
3443 final ActivityStack stack = r.task.stack;
3444 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003445 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003446 }
3447 if (allResumedActivitiesComplete()) {
3448 ensureActivitiesVisibleLocked(null, 0);
3449 mWindowManager.executeAppTransition();
3450 return true;
3451 }
3452 return false;
3453 }
3454
Craig Mautner8d341ef2013-03-26 09:03:27 -07003455 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003456 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3457 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003458 int stackNdx = stacks.size() - 1;
3459 while (stackNdx >= 0) {
3460 stacks.get(stackNdx).handleAppCrashLocked(app);
3461 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003462 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003463 }
3464 }
3465
Jose Lima4b6c6692014-08-12 17:41:12 -07003466 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003467 final ActivityStack stack = r.task.stack;
3468 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003469 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3470 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003471 return false;
3472 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003473 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003474 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3475 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003476
3477 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003478 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003479 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003480 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003481 return true;
3482 }
3483
3484 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003485 if (visible && top.fullscreen) {
3486 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003487 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3488 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3489 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3490 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003491 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003492 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3493 // Only the activity set as currently visible behind should actively reset its
3494 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003495 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3496 "requestVisibleBehind: returning visible=" + visible
3497 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3498 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003499 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003500 }
3501
Jose Lima4b6c6692014-08-12 17:41:12 -07003502 stack.setVisibleBehindActivity(visible ? r : null);
3503 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003504 // Make the activity immediately above r opaque.
3505 final ActivityRecord next = stack.findNextTranslucentActivity(r);
3506 if (next != null) {
3507 mService.convertFromTranslucent(next.appToken);
3508 }
3509 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003510 if (top.app != null && top.app.thread != null) {
3511 // Notify the top app of the change.
3512 try {
3513 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3514 } catch (RemoteException e) {
3515 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003516 }
3517 return true;
3518 }
3519
Craig Mautnerbb742462014-07-07 15:28:55 -07003520 // Called when WindowManager has finished animating the launchingBehind activity to the back.
3521 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
3522 r.mLaunchTaskBehind = false;
3523 final TaskRecord task = r.task;
3524 task.setLastThumbnail(task.stack.screenshotActivities(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003525 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08003526 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07003527 mWindowManager.setAppVisibility(r.appToken, false);
3528 }
3529
3530 void scheduleLaunchTaskBehindComplete(IBinder token) {
3531 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3532 }
3533
Craig Mautnerde4ef022013-04-07 19:01:33 -07003534 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
Craig Mautner580ea812013-04-25 12:58:38 -07003535 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08003536 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3537 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003538 final int topStackNdx = stacks.size() - 1;
3539 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3540 final ActivityStack stack = stacks.get(stackNdx);
Jose Lima729cb232014-05-05 15:59:40 -07003541 stack.ensureActivitiesVisibleLocked(starting, configChanges);
Craig Mautner580ea812013-04-25 12:58:38 -07003542 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003543 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003544 }
3545
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003546 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3547 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3548 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3549 final int topStackNdx = stacks.size() - 1;
3550 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3551 final ActivityStack stack = stacks.get(stackNdx);
3552 stack.clearOtherAppTimeTrackers(except);
3553 }
3554 }
3555 }
3556
Craig Mautner8d341ef2013-03-26 09:03:27 -07003557 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003558 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3559 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003560 final int numStacks = stacks.size();
3561 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3562 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003563 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003564 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003565 }
3566 }
3567
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003568 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3569 // Examine all activities currently running in the process.
3570 TaskRecord firstTask = null;
3571 // Tasks is non-null only if two or more tasks are found.
3572 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003573 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3574 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003575 ActivityRecord r = app.activities.get(i);
3576 // First, if we find an activity that is in the process of being destroyed,
3577 // then we just aren't going to do anything for now; we want things to settle
3578 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003579 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003580 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003581 return;
3582 }
3583 // Don't consider any activies that are currently not in a state where they
3584 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003585 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3586 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003587 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003588 continue;
3589 }
3590 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003591 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003592 + " from " + r);
3593 if (firstTask == null) {
3594 firstTask = r.task;
3595 } else if (firstTask != r.task) {
3596 if (tasks == null) {
3597 tasks = new ArraySet<>();
3598 tasks.add(firstTask);
3599 }
3600 tasks.add(r.task);
3601 }
3602 }
3603 }
3604 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003605 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003606 return;
3607 }
3608 // If we have activities in multiple tasks that are in a position to be destroyed,
3609 // let's iterate through the tasks and release the oldest one.
3610 final int numDisplays = mActivityDisplays.size();
3611 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3612 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3613 // Step through all stacks starting from behind, to hit the oldest things first.
3614 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3615 final ActivityStack stack = stacks.get(stackNdx);
3616 // Try to release activities in this stack; if we manage to, we are done.
3617 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3618 return;
3619 }
3620 }
3621 }
3622 }
3623
Amith Yamasani37a40c22015-06-17 13:25:42 -07003624 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003625 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003626 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003627 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003628
Craig Mautner858d8a62013-04-23 17:08:34 -07003629 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003630 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3631 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003632 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003633 final ActivityStack stack = stacks.get(stackNdx);
3634 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003635 TaskRecord task = stack.topTask();
3636 if (task != null) {
3637 mWindowManager.moveTaskToTop(task.taskId);
3638 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003639 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003640 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003641
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003642 ActivityStack stack = getStack(restoreStackId);
3643 if (stack == null) {
3644 stack = mHomeStack;
3645 }
3646 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003647 if (stack.isOnHomeDisplay()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003648 moveHomeStack(homeInFront, "switchUserOnHomeDisplay");
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003649 TaskRecord task = stack.topTask();
3650 if (task != null) {
3651 mWindowManager.moveTaskToTop(task.taskId);
3652 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003653 } else {
3654 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003655 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003656 }
Craig Mautner93529a42013-10-04 15:03:13 -07003657 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003658 }
3659
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003660 /**
3661 * Add background users to send boot completed events to.
3662 * @param userId The user being started in the background
3663 * @param uss The state object for the user.
3664 */
Amith Yamasani37a40c22015-06-17 13:25:42 -07003665 public void startBackgroundUserLocked(int userId, UserState uss) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003666 mStartingBackgroundUsers.add(uss);
3667 }
3668
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003669 /** Checks whether the userid is a profile of the current user. */
3670 boolean isCurrentProfileLocked(int userId) {
3671 if (userId == mCurrentUser) return true;
3672 for (int i = 0; i < mService.mCurrentProfileIds.length; i++) {
3673 if (mService.mCurrentProfileIds[i] == userId) return true;
3674 }
3675 return false;
3676 }
3677
Chong Zhang45c25ce2015-08-10 22:18:26 -07003678 /** Checks whether the activity should be shown for current user. */
3679 boolean okToShowLocked(ActivityRecord r) {
3680 return r != null && (isCurrentProfileLocked(r.userId)
3681 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
3682 }
3683
Craig Mautnerde4ef022013-04-07 19:01:33 -07003684 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003685 ArrayList<ActivityRecord> stops = null;
3686
3687 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003688 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3689 ActivityRecord s = mStoppingActivities.get(activityNdx);
3690 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003691 if (DEBUG_ALL) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003692 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3693 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003694 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003695 if (s.finishing) {
3696 // If this activity is finishing, it is sitting on top of
3697 // everyone else but we now know it is no longer needed...
3698 // so get rid of it. Otherwise, we need to go through the
3699 // normal flow and hide it once we determine that it is
3700 // hidden by the activities in front of it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003701 if (DEBUG_ALL) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003702 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003703 }
3704 }
Craig Mautner8c14c152015-01-15 17:32:07 -08003705 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003706 if (DEBUG_ALL) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003707 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003708 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003709 }
3710 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003711 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003712 }
3713 }
3714
3715 return stops;
3716 }
3717
Craig Mautnercf910b02013-04-23 11:23:27 -07003718 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003719 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3720 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3721 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3722 final ActivityStack stack = stacks.get(stackNdx);
3723 final ActivityRecord r = stack.topRunningActivityLocked(null);
3724 final ActivityState state = r == null ? DESTROYED : r.state;
3725 if (isFrontStack(stack)) {
3726 if (r == null) Slog.e(TAG,
3727 "validateTop...: null top activity, stack=" + stack);
3728 else {
3729 final ActivityRecord pausing = stack.mPausingActivity;
3730 if (pausing != null && pausing == r) Slog.e(TAG,
3731 "validateTop...: top stack has pausing activity r=" + r
3732 + " state=" + state);
3733 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3734 "validateTop...: activity in front not resumed r=" + r
3735 + " state=" + state);
3736 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003737 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003738 final ActivityRecord resumed = stack.mResumedActivity;
3739 if (resumed != null && resumed == r) Slog.e(TAG,
3740 "validateTop...: back stack has resumed activity r=" + r
3741 + " state=" + state);
3742 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3743 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003744 }
3745 }
3746 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003747 }
3748
Craig Mautnere0570202015-05-13 13:06:11 -07003749 private String lockTaskModeToString() {
3750 switch (mLockTaskModeState) {
3751 case LOCK_TASK_MODE_LOCKED:
3752 return "LOCKED";
3753 case LOCK_TASK_MODE_PINNED:
3754 return "PINNED";
3755 case LOCK_TASK_MODE_NONE:
3756 return "NONE";
3757 default: return "unknown=" + mLockTaskModeState;
3758 }
3759 }
3760
Craig Mautner27084302013-03-25 08:05:25 -07003761 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003762 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003763 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003764 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
3765 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
3766 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003767 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003768 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
3769 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3770 if (packages.size() > 0) {
3771 pw.println(" mLockTaskPackages (userId:packages)=");
3772 for (int i = 0; i < packages.size(); ++i) {
3773 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3774 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3775 }
3776 }
3777 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003778 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003779
Craig Mautner20e72272013-04-01 13:45:53 -07003780 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003781 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003782 }
3783
Dianne Hackborn390517b2013-05-30 15:03:32 -07003784 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3785 boolean needSep, String prefix) {
3786 if (activity != null) {
3787 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3788 if (needSep) {
3789 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003790 }
3791 pw.print(prefix);
3792 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003793 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003794 }
3795 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003796 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003797 }
3798
Craig Mautner8d341ef2013-03-26 09:03:27 -07003799 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3800 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003801 boolean printed = false;
3802 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003803 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3804 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003805 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003806 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003807 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003808 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003809 final ActivityStack stack = stacks.get(stackNdx);
3810 StringBuilder stackHeader = new StringBuilder(128);
3811 stackHeader.append(" Stack #");
3812 stackHeader.append(stack.mStackId);
3813 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003814 stackHeader.append("\n");
3815 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3816 stackHeader.append("\n");
3817 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003818 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3819 needSep, stackHeader.toString());
3820 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3821 !dumpAll, false, dumpPackage, true,
3822 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003823
Craig Mautner4a1cb222013-12-04 16:14:06 -08003824 needSep = printed;
3825 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3826 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003827 if (pr) {
3828 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003829 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003830 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003831 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3832 " mResumedActivity: ");
3833 if (pr) {
3834 printed = true;
3835 needSep = false;
3836 }
3837 if (dumpAll) {
3838 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3839 " mLastPausedActivity: ");
3840 if (pr) {
3841 printed = true;
3842 needSep = true;
3843 }
3844 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3845 needSep, " mLastNoHistoryActivity: ");
3846 }
3847 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003848 }
3849 }
3850
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003851 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3852 false, dumpPackage, true, " Activities waiting to finish:", null);
3853 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3854 false, dumpPackage, true, " Activities waiting to stop:", null);
3855 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3856 false, dumpPackage, true, " Activities waiting for another to become visible:",
3857 null);
3858 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3859 false, dumpPackage, true, " Activities waiting to sleep:", null);
3860 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3861 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003862
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003863 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003864 }
3865
Dianne Hackborn390517b2013-05-30 15:03:32 -07003866 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003867 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003868 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003869 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003870 String innerPrefix = null;
3871 String[] args = null;
3872 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003873 for (int i=list.size()-1; i>=0; i--) {
3874 final ActivityRecord r = list.get(i);
3875 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3876 continue;
3877 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003878 if (innerPrefix == null) {
3879 innerPrefix = prefix + " ";
3880 args = new String[0];
3881 }
3882 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003883 final boolean full = !brief && (complete || !r.isInHistory());
3884 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003885 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003886 needNL = false;
3887 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003888 if (header1 != null) {
3889 pw.println(header1);
3890 header1 = null;
3891 }
3892 if (header2 != null) {
3893 pw.println(header2);
3894 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003895 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003896 if (lastTask != r.task) {
3897 lastTask = r.task;
3898 pw.print(prefix);
3899 pw.print(full ? "* " : " ");
3900 pw.println(lastTask);
3901 if (full) {
3902 lastTask.dump(pw, prefix + " ");
3903 } else if (complete) {
3904 // Complete + brief == give a summary. Isn't that obvious?!?
3905 if (lastTask.intent != null) {
3906 pw.print(prefix); pw.print(" ");
3907 pw.println(lastTask.intent.toInsecureStringWithClip());
3908 }
3909 }
3910 }
3911 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3912 pw.print(" #"); pw.print(i); pw.print(": ");
3913 pw.println(r);
3914 if (full) {
3915 r.dump(pw, innerPrefix);
3916 } else if (complete) {
3917 // Complete + brief == give a summary. Isn't that obvious?!?
3918 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3919 if (r.app != null) {
3920 pw.print(innerPrefix); pw.println(r.app);
3921 }
3922 }
3923 if (client && r.app != null && r.app.thread != null) {
3924 // flush anything that is already in the PrintWriter since the thread is going
3925 // to write to the file descriptor directly
3926 pw.flush();
3927 try {
3928 TransferPipe tp = new TransferPipe();
3929 try {
3930 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3931 r.appToken, innerPrefix, args);
3932 // Short timeout, since blocking here can
3933 // deadlock with the application.
3934 tp.go(fd, 2000);
3935 } finally {
3936 tp.kill();
3937 }
3938 } catch (IOException e) {
3939 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3940 } catch (RemoteException e) {
3941 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3942 }
3943 needNL = true;
3944 }
3945 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003946 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003947 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003948
Craig Mautnerf3333272013-04-22 10:55:53 -07003949 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003950 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3951 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003952 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3953 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003954 }
3955
3956 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003957 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003958 }
3959
3960 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003961 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3962 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003963 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3964 }
3965
Craig Mautner05d29032013-05-03 13:40:13 -07003966 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003967 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3968 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3969 }
Craig Mautner05d29032013-05-03 13:40:13 -07003970 }
3971
Craig Mautner0eea92c2013-05-16 13:35:39 -07003972 void removeSleepTimeouts() {
3973 mSleepTimeout = false;
3974 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3975 }
3976
3977 final void scheduleSleepTimeout() {
3978 removeSleepTimeouts();
3979 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3980 }
3981
Craig Mautner4a1cb222013-12-04 16:14:06 -08003982 @Override
3983 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003984 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003985 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3986 }
3987
3988 @Override
3989 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003990 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003991 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3992 }
3993
3994 @Override
3995 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003996 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003997 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3998 }
3999
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004000 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08004001 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004002 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08004003 newDisplay = mActivityDisplays.get(displayId) == null;
4004 if (newDisplay) {
4005 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07004006 if (activityDisplay.mDisplay == null) {
4007 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
4008 return;
4009 }
Craig Mautner4504de52013-12-20 09:06:56 -08004010 mActivityDisplays.put(displayId, activityDisplay);
4011 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004012 }
Craig Mautner4504de52013-12-20 09:06:56 -08004013 if (newDisplay) {
4014 mWindowManager.onDisplayAdded(displayId);
4015 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004016 }
4017
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004018 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004019 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004020 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4021 if (activityDisplay != null) {
4022 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004023 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004024 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004025 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004026 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004027 }
4028 }
4029 mWindowManager.onDisplayRemoved(displayId);
4030 }
4031
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004032 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004033 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004034 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4035 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004036 // TODO: Update the bounds.
4037 }
4038 }
4039 mWindowManager.onDisplayChanged(displayId);
4040 }
4041
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004042 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004043 StackInfo info = new StackInfo();
4044 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
4045 info.displayId = Display.DEFAULT_DISPLAY;
4046 info.stackId = stack.mStackId;
4047
4048 ArrayList<TaskRecord> tasks = stack.getAllTasks();
4049 final int numTasks = tasks.size();
4050 int[] taskIds = new int[numTasks];
4051 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004052 Rect[] taskBounds = new Rect[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08004053 for (int i = 0; i < numTasks; ++i) {
4054 final TaskRecord task = tasks.get(i);
4055 taskIds[i] = task.taskId;
4056 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
4057 : task.realActivity != null ? task.realActivity.flattenToString()
4058 : task.getTopActivity() != null ? task.getTopActivity().packageName
4059 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004060 taskBounds[i] = new Rect();
4061 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004062 }
4063 info.taskIds = taskIds;
4064 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07004065 info.taskBounds = taskBounds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004066 return info;
4067 }
4068
4069 StackInfo getStackInfoLocked(int stackId) {
4070 ActivityStack stack = getStack(stackId);
4071 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004072 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004073 }
4074 return null;
4075 }
4076
4077 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004078 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08004079 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
4080 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004081 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004082 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08004083 }
4084 }
4085 return list;
4086 }
4087
Craig Mautner15df08a2015-04-01 12:17:18 -07004088 TaskRecord getLockedTaskLocked() {
4089 final int top = mLockTaskModeTasks.size() - 1;
4090 if (top >= 0) {
4091 return mLockTaskModeTasks.get(top);
4092 }
4093 return null;
4094 }
4095
4096 boolean isLockedTask(TaskRecord task) {
4097 return mLockTaskModeTasks.contains(task);
4098 }
4099
4100 boolean isLastLockedTask(TaskRecord task) {
4101 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
4102 }
4103
4104 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07004105 if (!mLockTaskModeTasks.remove(task)) {
4106 return;
4107 }
4108 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
4109 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004110 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07004111 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
4112 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07004113 final Message lockTaskMsg = Message.obtain();
4114 lockTaskMsg.arg1 = task.userId;
4115 lockTaskMsg.what = LOCK_TASK_END_MSG;
4116 mHandler.sendMessage(lockTaskMsg);
4117 }
4118 }
4119
Craig Mautner6cd6cec2015-04-01 00:02:12 -07004120 void showLockTaskToast() {
4121 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04004122 }
4123
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004124 void showLockTaskEscapeMessageLocked(TaskRecord task) {
4125 if (mLockTaskModeTasks.contains(task)) {
4126 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
4127 }
4128 }
4129
Craig Mautner432f64e2015-05-20 14:59:57 -07004130 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
4131 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004132 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07004133 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07004134 final TaskRecord lockedTask = getLockedTaskLocked();
4135 if (lockedTask != null) {
4136 removeLockedTaskLocked(lockedTask);
4137 if (!mLockTaskModeTasks.isEmpty()) {
4138 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07004139 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4140 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07004141 lockedTask.performClearTaskLocked();
4142 resumeTopActivitiesLocked();
4143 return;
4144 }
Christopher Tate3bd90612014-06-18 16:37:52 -07004145 }
Craig Mautnere0570202015-05-13 13:06:11 -07004146 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4147 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08004148 return;
4149 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004150
4151 // Should have already been checked, but do it again.
4152 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07004153 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4154 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08004155 return;
4156 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004157 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07004158 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07004159 return;
4160 }
4161
4162 if (mLockTaskModeTasks.isEmpty()) {
4163 // First locktask.
4164 final Message lockTaskMsg = Message.obtain();
4165 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
4166 lockTaskMsg.arg1 = task.userId;
4167 lockTaskMsg.what = LOCK_TASK_START_MSG;
4168 lockTaskMsg.arg2 = lockTaskModeState;
4169 mHandler.sendMessage(lockTaskMsg);
4170 }
4171 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07004172 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
4173 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07004174 mLockTaskModeTasks.remove(task);
4175 mLockTaskModeTasks.add(task);
4176
4177 if (task.mLockTaskUid == -1) {
4178 task.mLockTaskUid = task.mCallingUid;
4179 }
Craig Mautner432f64e2015-05-20 14:59:57 -07004180
4181 if (andResume) {
4182 findTaskToMoveToFrontLocked(task, 0, null, reason);
4183 resumeTopActivitiesLocked();
4184 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004185 }
4186
4187 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04004188 return isLockTaskModeViolation(task, false);
4189 }
4190
4191 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
4192 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004193 return false;
4194 }
4195 final int lockTaskAuth = task.mLockTaskAuth;
4196 switch (lockTaskAuth) {
4197 case LOCK_TASK_AUTH_DONT_LOCK:
4198 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01004199 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07004200 case LOCK_TASK_AUTH_LAUNCHABLE:
4201 case LOCK_TASK_AUTH_WHITELISTED:
4202 return false;
4203 case LOCK_TASK_AUTH_PINNABLE:
4204 // Pinnable tasks can't be launched on top of locktask tasks.
4205 return !mLockTaskModeTasks.isEmpty();
4206 default:
4207 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
4208 return true;
4209 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004210 }
4211
Craig Mautner15df08a2015-04-01 12:17:18 -07004212 void onLockTaskPackagesUpdatedLocked() {
4213 boolean didSomething = false;
4214 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
4215 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01004216 final boolean wasWhitelisted =
4217 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4218 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07004219 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01004220 final boolean isWhitelisted =
4221 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4222 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
4223 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004224 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07004225 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
4226 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07004227 removeLockedTaskLocked(lockedTask);
4228 lockedTask.performClearTaskLocked();
4229 didSomething = true;
4230 }
4231 }
4232 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4233 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4234 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4235 final ActivityStack stack = stacks.get(stackNdx);
4236 stack.onLockTaskPackagesUpdatedLocked();
4237 }
4238 }
Craig Mautnere0570202015-05-13 13:06:11 -07004239 final ActivityRecord r = topRunningActivityLocked();
4240 final TaskRecord task = r != null ? r.task : null;
4241 if (mLockTaskModeTasks.isEmpty() && task != null
4242 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4243 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004244 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4245 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4246 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4247 false);
4248 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004249 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004250 if (didSomething) {
4251 resumeTopActivitiesLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004252 }
4253 }
4254
Benjamin Franz43261142015-02-11 15:59:44 +00004255 int getLockTaskModeState() {
4256 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004257 }
4258
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004259 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004260
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004261 public ActivityStackSupervisorHandler(Looper looper) {
4262 super(looper);
4263 }
4264
Craig Mautnerf3333272013-04-22 10:55:53 -07004265 void activityIdleInternal(ActivityRecord r) {
4266 synchronized (mService) {
4267 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
4268 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004269 }
4270
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004271 @Override
4272 public void handleMessage(Message msg) {
4273 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004274 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004275 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4276 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004277 if (mService.mDidDexOpt) {
4278 mService.mDidDexOpt = false;
4279 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4280 nmsg.obj = msg.obj;
4281 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4282 return;
4283 }
4284 // We don't at this point know if the activity is fullscreen,
4285 // so we need to be conservative and assume it isn't.
4286 activityIdleInternal((ActivityRecord)msg.obj);
4287 } break;
4288 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004289 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004290 activityIdleInternal((ActivityRecord)msg.obj);
4291 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004292 case RESUME_TOP_ACTIVITY_MSG: {
4293 synchronized (mService) {
4294 resumeTopActivitiesLocked();
4295 }
4296 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004297 case SLEEP_TIMEOUT_MSG: {
4298 synchronized (mService) {
4299 if (mService.isSleepingOrShuttingDown()) {
4300 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4301 mSleepTimeout = true;
4302 checkReadyForSleepLocked();
4303 }
4304 }
4305 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004306 case LAUNCH_TIMEOUT_MSG: {
4307 if (mService.mDidDexOpt) {
4308 mService.mDidDexOpt = false;
4309 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4310 return;
4311 }
4312 synchronized (mService) {
4313 if (mLaunchingActivity.isHeld()) {
4314 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4315 if (VALIDATE_WAKE_LOCK_CALLER
4316 && Binder.getCallingUid() != Process.myUid()) {
4317 throw new IllegalStateException("Calling must be system uid");
4318 }
4319 mLaunchingActivity.release();
4320 }
4321 }
4322 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004323 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004324 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004325 } break;
4326 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004327 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004328 } break;
4329 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004330 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004331 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004332 case CONTAINER_CALLBACK_VISIBILITY: {
4333 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004334 final IActivityContainerCallback callback = container.mCallback;
4335 if (callback != null) {
4336 try {
4337 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4338 } catch (RemoteException e) {
4339 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004340 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004341 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004342 case LOCK_TASK_START_MSG: {
4343 // When lock task starts, we disable the status bars.
4344 try {
Jason Monk62515be2014-05-21 16:06:19 -04004345 if (mLockTaskNotify == null) {
4346 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004347 }
Jason Monk62515be2014-05-21 16:06:19 -04004348 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004349 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004350 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004351 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004352 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004353 flags = StatusBarManager.DISABLE_MASK
4354 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004355 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004356 flags = StatusBarManager.DISABLE_MASK
4357 & (~StatusBarManager.DISABLE_BACK)
4358 & (~StatusBarManager.DISABLE_HOME)
4359 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004360 }
4361 getStatusBarService().disable(flags, mToken,
4362 mService.mContext.getPackageName());
4363 }
4364 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004365 if (getDevicePolicyManager() != null) {
4366 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004367 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004368 }
justinzhang5286d3f2014-05-12 17:06:01 -04004369 } catch (RemoteException ex) {
4370 throw new RuntimeException(ex);
4371 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004372 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004373 case LOCK_TASK_END_MSG: {
4374 // When lock task ends, we enable the status bars.
4375 try {
Jason Monk62515be2014-05-21 16:06:19 -04004376 if (getStatusBarService() != null) {
4377 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4378 mService.mContext.getPackageName());
4379 }
4380 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004381 if (getDevicePolicyManager() != null) {
4382 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4383 msg.arg1);
4384 }
Jason Monk62515be2014-05-21 16:06:19 -04004385 if (mLockTaskNotify == null) {
4386 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4387 }
4388 mLockTaskNotify.show(false);
4389 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004390 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004391 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004392 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004393 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004394 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04004395 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04004396 new LockPatternUtils(mService.mContext)
4397 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004398 }
4399 } catch (SettingNotFoundException e) {
4400 // No setting, don't lock.
4401 }
justinzhang5286d3f2014-05-12 17:06:01 -04004402 } catch (RemoteException ex) {
4403 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004404 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004405 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004406 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004407 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004408 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4409 if (mLockTaskNotify == null) {
4410 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4411 }
4412 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4413 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004414 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4415 final ActivityContainer container = (ActivityContainer) msg.obj;
4416 final IActivityContainerCallback callback = container.mCallback;
4417 if (callback != null) {
4418 try {
4419 callback.onAllActivitiesComplete(container.asBinder());
4420 } catch (RemoteException e) {
4421 }
4422 }
4423 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004424 case LAUNCH_TASK_BEHIND_COMPLETE: {
4425 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004426 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004427 if (r != null) {
4428 handleLaunchTaskBehindCompleteLocked(r);
4429 }
4430 }
4431 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004432 }
4433 }
4434 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004435
Ying Wangb081a592014-04-22 15:20:16 -07004436 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07004437 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07004438 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004439 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004440 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004441 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004442 ActivityRecord mParentActivity = null;
4443 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004444
Craig Mautnere3a00d72014-04-16 08:31:19 -07004445 boolean mVisible = true;
4446
Craig Mautner4a1cb222013-12-04 16:14:06 -08004447 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004448 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004449
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004450 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4451 final static int CONTAINER_STATE_NO_SURFACE = 1;
4452 final static int CONTAINER_STATE_FINISHING = 2;
4453 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4454
4455 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004456 synchronized (mService) {
4457 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004458 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004459 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004460 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004461 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004462 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004463
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004464 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004465 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004466 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08004467 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004468 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004469 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004470 }
4471
4472 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004473 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004474 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004475 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4476 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004477 return;
4478 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004479 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004480 }
4481 }
4482
4483 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004484 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004485 synchronized (mService) {
4486 if (mActivityDisplay != null) {
4487 return mActivityDisplay.mDisplayId;
4488 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004489 }
4490 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004491 }
4492
Jeff Brownca9bc702014-02-11 14:32:56 -08004493 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004494 public int getStackId() {
4495 synchronized (mService) {
4496 return mStackId;
4497 }
4498 }
4499
4500 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004501 public boolean injectEvent(InputEvent event) {
4502 final long origId = Binder.clearCallingIdentity();
4503 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004504 synchronized (mService) {
4505 if (mActivityDisplay != null) {
4506 return mInputManagerInternal.injectInputEvent(event,
4507 mActivityDisplay.mDisplayId,
4508 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4509 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004510 }
4511 return false;
4512 } finally {
4513 Binder.restoreCallingIdentity(origId);
4514 }
4515 }
4516
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004517 @Override
4518 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004519 synchronized (mService) {
4520 if (mContainerState == CONTAINER_STATE_FINISHING) {
4521 return;
4522 }
4523 mContainerState = CONTAINER_STATE_FINISHING;
4524
Craig Mautnerd163e752014-06-13 17:18:47 -07004525 long origId = Binder.clearCallingIdentity();
4526 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004527 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07004528 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004529 } finally {
4530 Binder.restoreCallingIdentity(origId);
4531 }
4532 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004533 }
4534
Craig Mautner60257702014-09-17 15:02:33 -07004535 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004536 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004537 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004538 if (mActivityDisplay != null) {
4539 mActivityDisplay.detachActivitiesLocked(mStack);
4540 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004541 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004542 }
4543 }
4544
4545 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004546 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004547 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnerb9168362015-02-26 20:40:19 -08004548 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004549 Binder.getCallingUid(), mCurrentUser, false,
4550 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004551
Craig Mautnere0a38842013-12-16 16:14:02 -08004552 // TODO: Switch to user app stacks here.
4553 String mimeType = intent.getType();
Craig Mautnerb9168362015-02-26 20:40:19 -08004554 final Uri data = intent.getData();
4555 if (mimeType == null && data != null && "content".equals(data.getScheme())) {
4556 mimeType = mService.getProviderMimeType(data, userId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004557 }
Craig Mautnerb9168362015-02-26 20:40:19 -08004558 checkEmbeddedAllowedInner(userId, intent, mimeType);
4559
4560 intent.addFlags(FORCE_NEW_TASK_FLAGS);
4561 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07004562 0, 0, null, null, null, null, false, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004563 }
4564
4565 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004566 public final int startActivityIntentSender(IIntentSender intentSender)
4567 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004568 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4569
4570 if (!(intentSender instanceof PendingIntentRecord)) {
4571 throw new IllegalArgumentException("Bad PendingIntent object");
4572 }
4573
Craig Mautnerb9168362015-02-26 20:40:19 -08004574 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004575 Binder.getCallingUid(), mCurrentUser, false,
4576 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004577
4578 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4579 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4580 pendingIntent.key.requestResolvedType);
4581
4582 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4583 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4584 }
4585
4586 private void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004587 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004588 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004589 throw new SecurityException(
4590 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4591 }
4592 }
4593
Craig Mautnerdf88d732014-01-27 09:21:32 -08004594 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004595 public IBinder asBinder() {
4596 return this;
4597 }
4598
Craig Mautner4504de52013-12-20 09:06:56 -08004599 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004600 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004601 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004602 }
4603
Craig Mautner4a1cb222013-12-04 16:14:06 -08004604 ActivityStackSupervisor getOuter() {
4605 return ActivityStackSupervisor.this;
4606 }
4607
Craig Mautnerd163e752014-06-13 17:18:47 -07004608 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004609 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004610 }
4611
4612 void getBounds(Point outBounds) {
Craig Mautnerd163e752014-06-13 17:18:47 -07004613 synchronized (mService) {
4614 if (mActivityDisplay != null) {
4615 mActivityDisplay.getBounds(outBounds);
4616 } else {
4617 outBounds.set(0, 0);
4618 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004619 }
4620 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004621
Craig Mautner6985bad2014-04-21 15:22:06 -07004622 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004623 void setVisible(boolean visible) {
4624 if (mVisible != visible) {
4625 mVisible = visible;
4626 if (mCallback != null) {
4627 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4628 0 /* unused */, this).sendToTarget();
4629 }
4630 }
4631 }
4632
Craig Mautner6985bad2014-04-21 15:22:06 -07004633 void setDrawn() {
4634 }
4635
Craig Mautner1b4bf852014-05-26 15:06:32 -07004636 // You can always start a new task on a regular ActivityStack.
4637 boolean isEligibleForNewTasks() {
4638 return true;
4639 }
4640
Craig Mautnerd163e752014-06-13 17:18:47 -07004641 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004642 detachLocked();
4643 deleteActivityContainer(this);
4644 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004645 }
4646
Craig Mautner34b73df2014-01-12 21:11:08 -08004647 @Override
4648 public String toString() {
4649 return mIdString + (mActivityDisplay == null ? "N" : "A");
4650 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004651 }
4652
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004653 private class VirtualActivityContainer extends ActivityContainer {
4654 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004655 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004656
4657 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4658 super(getNextStackId());
4659 mParentActivity = parent;
4660 mCallback = callback;
4661 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004662 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004663 }
4664
4665 @Override
4666 public void setSurface(Surface surface, int width, int height, int density) {
4667 super.setSurface(surface, width, height, density);
4668
4669 synchronized (mService) {
4670 final long origId = Binder.clearCallingIdentity();
4671 try {
4672 setSurfaceLocked(surface, width, height, density);
4673 } finally {
4674 Binder.restoreCallingIdentity(origId);
4675 }
4676 }
4677 }
4678
4679 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4680 if (mContainerState == CONTAINER_STATE_FINISHING) {
4681 return;
4682 }
4683 VirtualActivityDisplay virtualActivityDisplay =
4684 (VirtualActivityDisplay) mActivityDisplay;
4685 if (virtualActivityDisplay == null) {
4686 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004687 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004688 mActivityDisplay = virtualActivityDisplay;
4689 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004690 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004691 }
4692
4693 if (mSurface != null) {
4694 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004695 }
4696
Craig Mautner6985bad2014-04-21 15:22:06 -07004697 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004698 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004699 mStack.resumeTopActivityLocked(null);
4700 } else {
4701 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004702 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004703 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004704 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004705 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004706 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004707
Craig Mautnerd163e752014-06-13 17:18:47 -07004708 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004709
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004710 if (DEBUG_STACK) Slog.d(TAG_STACK,
4711 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004712 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004713
Craig Mautner6985bad2014-04-21 15:22:06 -07004714 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004715 boolean isAttachedLocked() {
4716 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004717 }
4718
4719 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004720 void setDrawn() {
4721 synchronized (mService) {
4722 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004723 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004724 }
4725 }
4726
Craig Mautner1b4bf852014-05-26 15:06:32 -07004727 // Never start a new task on an ActivityView if it isn't explicitly specified.
4728 @Override
4729 boolean isEligibleForNewTasks() {
4730 return false;
4731 }
4732
Craig Mautnerd163e752014-06-13 17:18:47 -07004733 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004734 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004735 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4736 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4737 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4738 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4739 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004740 }
4741 }
4742
Craig Mautner4a1cb222013-12-04 16:14:06 -08004743 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4744 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004745 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004746 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004747 int mDisplayId;
4748 Display mDisplay;
4749 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004750
Craig Mautner4a1cb222013-12-04 16:14:06 -08004751 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4752 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08004753 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004754
Jose Lima4b6c6692014-08-12 17:41:12 -07004755 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004756
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004757 ActivityDisplay() {
4758 }
Craig Mautner4504de52013-12-20 09:06:56 -08004759
Craig Mautner1a70a162014-09-13 12:09:31 -07004760 // After instantiation, check that mDisplay is not null before using this. The alternative
4761 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004762 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004763 final Display display = mDisplayManager.getDisplay(displayId);
4764 if (display == null) {
4765 return;
4766 }
4767 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004768 }
4769
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004770 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004771 mDisplay = display;
4772 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004773 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004774 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004775
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004776 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004777 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004778 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
4779 + " onTop=" + onTop);
4780 if (onTop) {
4781 mStacks.add(stack);
4782 } else {
4783 mStacks.add(0, stack);
4784 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004785 }
4786
Craig Mautnere0a38842013-12-16 16:14:02 -08004787 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004788 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004789 + " from displayId=" + mDisplayId);
4790 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004791 }
4792
4793 void getBounds(Point bounds) {
4794 mDisplay.getDisplayInfo(mDisplayInfo);
4795 bounds.x = mDisplayInfo.appWidth;
4796 bounds.y = mDisplayInfo.appHeight;
4797 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004798
Jose Lima4b6c6692014-08-12 17:41:12 -07004799 void setVisibleBehindActivity(ActivityRecord r) {
4800 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004801 }
4802
Jose Lima4b6c6692014-08-12 17:41:12 -07004803 boolean hasVisibleBehindActivity() {
4804 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004805 }
4806
Craig Mautner34b73df2014-01-12 21:11:08 -08004807 @Override
4808 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004809 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4810 }
4811 }
4812
4813 class VirtualActivityDisplay extends ActivityDisplay {
4814 VirtualDisplay mVirtualDisplay;
4815
Craig Mautner6985bad2014-04-21 15:22:06 -07004816 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004817 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004818 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4819 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4820 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4821 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004822
4823 init(mVirtualDisplay.getDisplay());
4824
4825 mWindowManager.handleDisplayAdded(mDisplayId);
4826 }
4827
4828 void setSurface(Surface surface) {
4829 if (mVirtualDisplay != null) {
4830 mVirtualDisplay.setSurface(surface);
4831 }
4832 }
4833
4834 @Override
4835 void detachActivitiesLocked(ActivityStack stack) {
4836 super.detachActivitiesLocked(stack);
4837 if (mVirtualDisplay != null) {
4838 mVirtualDisplay.release();
4839 mVirtualDisplay = null;
4840 }
4841 }
4842
4843 @Override
4844 public String toString() {
4845 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004846 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004847 }
Jose Lima58e66d62014-05-27 20:00:27 -07004848
4849 private boolean isLeanbackOnlyDevice() {
4850 boolean onLeanbackOnly = false;
4851 try {
4852 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
4853 PackageManager.FEATURE_LEANBACK_ONLY);
4854 } catch (RemoteException e) {
4855 // noop
4856 }
4857
4858 return onLeanbackOnly;
4859 }
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07004860
4861 /**
4862 * Adjust bounds to stay within stack bounds.
4863 *
4864 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
4865 * that keep them unchanged, but be contained within the stack bounds.
4866 *
4867 * @param bounds Bounds to be adjusted.
4868 * @param stackBounds Bounds within which the other bounds should remain.
4869 */
4870 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
4871 if (stackBounds == null || stackBounds.contains(bounds)) {
4872 return;
4873 }
4874
4875 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
4876 final int maxRight = stackBounds.right
4877 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
4878 int horizontalDiff = stackBounds.left - bounds.left;
4879 if ((horizontalDiff < 0 && bounds.left >= maxRight)
4880 || (bounds.left + horizontalDiff >= maxRight)) {
4881 horizontalDiff = maxRight - bounds.left;
4882 }
4883 bounds.left += horizontalDiff;
4884 bounds.right += horizontalDiff;
4885 }
4886
4887 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
4888 final int maxBottom = stackBounds.bottom
4889 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
4890 int verticalDiff = stackBounds.top - bounds.top;
4891 if ((verticalDiff < 0 && bounds.top >= maxBottom)
4892 || (bounds.top + verticalDiff >= maxBottom)) {
4893 verticalDiff = maxBottom - bounds.top;
4894 }
4895 bounds.top += verticalDiff;
4896 bounds.bottom += verticalDiff;
4897 }
4898 }
4899
Craig Mautner27084302013-03-25 08:05:25 -07004900}