blob: 7f7638b0a68dbf7ad0c11549fc34570cf2e103a2 [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
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700467 void setFocusStack(String reason, ActivityStack lastFocusedStack) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800468 ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautnerde313752015-01-22 14:28:03 -0800469 final int topNdx = stacks.size() - 1;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800470 if (topNdx <= 0) {
471 return;
472 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700473
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700474 final ActivityStack topStack = stacks.get(topNdx);
475 mFocusedStack = topStack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800476 if (lastFocusedStack != null) {
477 mLastFocusedStack = lastFocusedStack;
478 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800479
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700480 EventLogTags.writeAmFocusedStack(
481 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
482 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
Craig Mautnerde313752015-01-22 14:28:03 -0800483
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800484 if (mService.mBooting || !mService.mBooted) {
485 final ActivityRecord r = topRunningActivityLocked();
486 if (r != null && r.idle) {
487 checkFinishBootingLocked();
488 }
489 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700490 }
491
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700492 void moveHomeStackToFront(String reason) {
493 mHomeStack.moveToFront(reason);
494 }
495
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700496 /** Returns true if the focus activity was adjusted to the home stack top activity. */
497 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700498 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
499 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700500 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700501 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700502
Craig Mautner84984fa2014-06-19 11:19:20 -0700503 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700504
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700505 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700506 if (top == null) {
507 return false;
508 }
509 mService.setFocusedActivityLocked(top, reason);
510 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700511 }
512
Craig Mautner299f9602015-01-26 09:47:33 -0800513 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700514 if (!mService.mBooting && !mService.mBooted) {
515 // Not ready yet!
516 return false;
517 }
518
Craig Mautner84984fa2014-06-19 11:19:20 -0700519 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
520 mWindowManager.showRecentApps();
521 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700522 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700523
Craig Mautner84984fa2014-06-19 11:19:20 -0700524 if (prev != null) {
525 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
526 }
527
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700528 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
529 ActivityRecord r = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700530 if (r != null) {
Craig Mautner299f9602015-01-26 09:47:33 -0800531 mService.setFocusedActivityLocked(r, reason);
Craig Mautner05d29032013-05-03 13:40:13 -0700532 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700533 }
Craig Mautner299f9602015-01-26 09:47:33 -0800534 return mService.startHomeActivityLocked(mCurrentUser, reason);
Craig Mautner69ada552013-04-18 13:51:51 -0700535 }
536
Craig Mautner8d341ef2013-03-26 09:03:27 -0700537 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700538 return anyTaskForIdLocked(id, true);
539 }
540
541 /**
542 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
543 * @param id Id of the task we would like returned.
544 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
545 * restore the task from recents to the active list.
546 */
547 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800548 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800549 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800550 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800551 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
552 ActivityStack stack = stacks.get(stackNdx);
553 TaskRecord task = stack.taskForIdLocked(id);
554 if (task != null) {
555 return task;
556 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700557 }
558 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800559
560 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700561 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800562 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800563 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700564 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800565 return null;
566 }
567
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700568 if (!restoreFromRecents) {
569 return task;
570 }
571
Chong Zhang5dcb2752015-08-18 13:50:26 -0700572 if (!restoreRecentTaskLocked(task, INVALID_STACK_ID)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700573 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
574 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800575 return null;
576 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700577 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800578 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700579 }
580
Craig Mautner6170f732013-04-02 13:05:23 -0700581 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800582 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800583 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800584 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800585 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
586 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
587 if (r != null) {
588 return r;
589 }
Craig Mautner6170f732013-04-02 13:05:23 -0700590 }
591 }
592 return null;
593 }
594
Craig Mautneref73ee12014-04-23 11:45:37 -0700595 void setNextTaskId(int taskId) {
596 if (taskId > mCurTaskId) {
597 mCurTaskId = taskId;
598 }
599 }
600
Craig Mautner8d341ef2013-03-26 09:03:27 -0700601 int getNextTaskId() {
602 do {
603 mCurTaskId++;
604 if (mCurTaskId <= 0) {
605 mCurTaskId = 1;
606 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700607 } while (anyTaskForIdLocked(mCurTaskId, false) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700608 return mCurTaskId;
609 }
610
Craig Mautnerde4ef022013-04-07 19:01:33 -0700611 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800612 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700613 if (stack == null) {
614 return null;
615 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700616 ActivityRecord resumedActivity = stack.mResumedActivity;
617 if (resumedActivity == null || resumedActivity.app == null) {
618 resumedActivity = stack.mPausingActivity;
619 if (resumedActivity == null || resumedActivity.app == null) {
620 resumedActivity = stack.topRunningActivityLocked(null);
621 }
622 }
623 return resumedActivity;
624 }
625
Dianne Hackbornff072722014-09-24 10:56:28 -0700626 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700627 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800628 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800629 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
630 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800631 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
632 final ActivityStack stack = stacks.get(stackNdx);
633 if (!isFrontStack(stack)) {
634 continue;
635 }
636 ActivityRecord hr = stack.topRunningActivityLocked(null);
637 if (hr != null) {
638 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
639 && processName.equals(hr.processName)) {
640 try {
George Mount2c92c972014-03-20 09:38:23 -0700641 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800642 didSomething = true;
643 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700644 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800645 Slog.w(TAG, "Exception in new application when starting activity "
646 + hr.intent.getComponent().flattenToShortString(), e);
647 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700648 }
Craig Mautner20e72272013-04-01 13:45:53 -0700649 }
Craig Mautner20e72272013-04-01 13:45:53 -0700650 }
651 }
652 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700653 if (!didSomething) {
654 ensureActivitiesVisibleLocked(null, 0);
655 }
Craig Mautner20e72272013-04-01 13:45:53 -0700656 return didSomething;
657 }
658
659 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800660 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
661 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800662 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
663 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner34b73df2014-01-12 21:11:08 -0800664 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800665 continue;
666 }
667 final ActivityRecord resumedActivity = stack.mResumedActivity;
668 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700669 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800670 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800671 return false;
672 }
Craig Mautner20e72272013-04-01 13:45:53 -0700673 }
674 }
675 return true;
676 }
677
Craig Mautnerde4ef022013-04-07 19:01:33 -0700678 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800679 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
680 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800681 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
682 final ActivityStack stack = stacks.get(stackNdx);
683 if (isFrontStack(stack)) {
684 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700685 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800686 return false;
687 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700688 }
689 }
690 }
691 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700692 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800693 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
694 mLastFocusedStack + " to=" + mFocusedStack);
695 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700696 return true;
697 }
698
699 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800700 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800701 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
702 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800703 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
704 final ActivityStack stack = stacks.get(stackNdx);
705 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800706 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700707 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800708 return false;
709 }
710 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800711 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700712 }
713 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800714 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700715 }
716
Craig Mautner2acc3892013-09-23 10:28:14 -0700717 /**
718 * Pause all activities in either all of the stacks or just the back stacks.
719 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700720 * @return true if any activity was paused as a result of this call.
721 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700722 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700723 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800724 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
725 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800726 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
727 final ActivityStack stack = stacks.get(stackNdx);
728 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700729 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800730 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700731 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
732 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800733 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700734 }
735 }
736 return someActivityPaused;
737 }
738
Craig Mautnerde4ef022013-04-07 19:01:33 -0700739 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700740 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800741 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
742 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800743 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
744 final ActivityStack stack = stacks.get(stackNdx);
745 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700746 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800747 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700748 Slog.d(TAG_STATES,
749 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800750 pausing = false;
751 } else {
752 return false;
753 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700754 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700755 }
756 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700757 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700758 }
759
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700760 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
761 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500762 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800763 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
764 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
765 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
766 final ActivityStack stack = stacks.get(stackNdx);
767 if (stack.mResumedActivity != null &&
768 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700769 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800770 }
771 }
772 }
773 }
774
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700775 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700776 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700777 }
778
779 void sendWaitingVisibleReportLocked(ActivityRecord r) {
780 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700781 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700782 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700783 if (w.who == null) {
784 changed = true;
785 w.timeout = false;
786 if (r != null) {
787 w.who = new ComponentName(r.info.packageName, r.info.name);
788 }
789 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
790 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700791 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700792 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700793 if (changed) {
794 mService.notifyAll();
795 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700796 }
797
798 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
799 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700800 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700801 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700802 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700803 if (w.who == null) {
804 changed = true;
805 w.timeout = timeout;
806 if (r != null) {
807 w.who = new ComponentName(r.info.packageName, r.info.name);
808 }
809 w.thisTime = thisTime;
810 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700811 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700812 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700813 if (changed) {
814 mService.notifyAll();
815 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700816 }
817
Craig Mautner29219d92013-04-16 20:19:12 -0700818 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800819 final ActivityStack focusedStack = mFocusedStack;
Craig Mautner1602ec22013-05-12 10:24:27 -0700820 ActivityRecord r = focusedStack.topRunningActivityLocked(null);
821 if (r != null) {
822 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700823 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700824
Craig Mautner4a1cb222013-12-04 16:14:06 -0800825 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800826 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800827 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
828 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700829 if (stack != focusedStack && isFrontStack(stack)) {
Craig Mautner29219d92013-04-16 20:19:12 -0700830 r = stack.topRunningActivityLocked(null);
831 if (r != null) {
832 return r;
833 }
834 }
835 }
836 return null;
837 }
838
Dianne Hackborn09233282014-04-30 11:33:59 -0700839 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700840 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800841 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
842 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800843 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800844 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800845 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800846 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
847 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700848 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800849 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700850 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700851 }
852 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700853
854 // The lists are already sorted from most recent to oldest. Just pull the most recent off
855 // each list and add it to list. Stop when all lists are empty or maxNum reached.
856 while (maxNum > 0) {
857 long mostRecentActiveTime = Long.MIN_VALUE;
858 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800859 final int numTaskLists = runningTaskLists.size();
860 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
861 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700862 if (!stackTaskList.isEmpty()) {
863 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
864 if (lastActiveTime > mostRecentActiveTime) {
865 mostRecentActiveTime = lastActiveTime;
866 selectedStackList = stackTaskList;
867 }
868 }
869 }
870 if (selectedStackList != null) {
871 list.add(selectedStackList.remove(0));
872 --maxNum;
873 } else {
874 break;
875 }
876 }
Craig Mautner20e72272013-04-01 13:45:53 -0700877 }
878
Craig Mautner23ac33b2013-04-01 16:26:35 -0700879 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700880 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700881 // Collect information about the target of the Intent.
882 ActivityInfo aInfo;
883 try {
884 ResolveInfo rInfo =
885 AppGlobals.getPackageManager().resolveIntent(
886 intent, resolvedType,
887 PackageManager.MATCH_DEFAULT_ONLY
888 | ActivityManagerService.STOCK_PM_FLAGS, userId);
889 aInfo = rInfo != null ? rInfo.activityInfo : null;
890 } catch (RemoteException e) {
891 aInfo = null;
892 }
893
894 if (aInfo != null) {
895 // Store the found target back into the intent, because now that
896 // we have it we never want to do this again. For example, if the
897 // user navigates back to this point in the history, we should
898 // always restart the exact same activity.
899 intent.setComponent(new ComponentName(
900 aInfo.applicationInfo.packageName, aInfo.name));
901
902 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -0700903 if (!aInfo.processName.equals("system")) {
904 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700905 mService.setDebugApp(aInfo.processName, true, false);
906 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700907
Man Caocfa78b22015-06-11 20:14:34 -0700908 if ((startFlags & ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700909 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
910 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700911
Man Caocfa78b22015-06-11 20:14:34 -0700912 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
913 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
914 }
915
916 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700917 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700918 }
919 }
920 }
921 return aInfo;
922 }
923
Craig Mautner299f9602015-01-26 09:47:33 -0800924 void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
925 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700926 startActivityLocked(null /* caller */, intent, null /* resolvedType */, aInfo,
927 null /* voiceSession */, null /* voiceInteractor */, null /* resultTo */,
928 null /* resultWho */, 0 /* requestCode */, 0 /* callingPid */, 0 /* callingUid */,
929 null /* callingPackage */, 0 /* realCallingPid */, 0 /* realCallingUid */,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700930 0 /* startFlags */, null /* options */, false /* ignoreTargetSecurity */,
931 false /* componentSpecified */,
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700932 null /* outActivity */, null /* container */, null /* inTask */);
933 if (inResumeTopActivity) {
934 // If we are in resume section already, home activity will be initialized, but not
935 // resumed (to avoid recursive resume) and will stay that way until something pokes it
936 // again. We need to schedule another resume.
937 scheduleResumeTopActivities();
938 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700939 }
940
Craig Mautner23ac33b2013-04-01 16:26:35 -0700941 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700942 String callingPackage, Intent intent, String resolvedType,
943 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700944 IBinder resultTo, String resultWho, int requestCode, int startFlags,
945 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700946 Bundle options, boolean ignoreTargetSecurity, int userId,
947 IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700948 // Refuse possible leaked file descriptors
949 if (intent != null && intent.hasFileDescriptors()) {
950 throw new IllegalArgumentException("File descriptors passed in Intent");
951 }
952 boolean componentSpecified = intent.getComponent() != null;
953
954 // Don't modify the client's object!
955 intent = new Intent(intent);
956
957 // Collect information about the target of the Intent.
Craig Mautner15df08a2015-04-01 12:17:18 -0700958 ActivityInfo aInfo =
959 resolveActivity(intent, resolvedType, startFlags, profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700960
Craig Mautnere0a38842013-12-16 16:14:02 -0800961 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700962 synchronized (mService) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800963 if (container != null && container.mParentActivity != null &&
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700964 container.mParentActivity.state != RESUMED) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800965 // Cannot start a child activity if the parent is not resumed.
966 return ActivityManager.START_CANCELED;
967 }
Dianne Hackborn95465202014-09-15 16:21:55 -0700968 final int realCallingPid = Binder.getCallingPid();
969 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -0700970 int callingPid;
971 if (callingUid >= 0) {
972 callingPid = -1;
973 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700974 callingPid = realCallingPid;
975 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700976 } else {
977 callingPid = callingUid = -1;
978 }
979
Craig Mautnere0a38842013-12-16 16:14:02 -0800980 final ActivityStack stack;
981 if (container == null || container.mStack.isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800982 stack = mFocusedStack;
Craig Mautnere0a38842013-12-16 16:14:02 -0800983 } else {
984 stack = container.mStack;
985 }
Wale Ogunwale60454db2015-01-23 16:05:07 -0800986 stack.mConfigWillChange = config != null && mService.mConfiguration.diff(config) != 0;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700987 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700988 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700989
990 final long origId = Binder.clearCallingIdentity();
991
992 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800993 (aInfo.applicationInfo.privateFlags
994 &ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700995 // This may be a heavy-weight process! Check to see if we already
996 // have another, different heavy-weight process running.
997 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
998 if (mService.mHeavyWeightProcess != null &&
999 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
1000 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001001 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001002 if (caller != null) {
1003 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
1004 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001005 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001006 } else {
1007 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -07001008 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -07001009 + intent.toString());
1010 ActivityOptions.abort(options);
1011 return ActivityManager.START_PERMISSION_DENIED;
1012 }
1013 }
1014
1015 IIntentSender target = mService.getIntentSenderLocked(
1016 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -07001017 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -07001018 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
1019 | PendingIntent.FLAG_ONE_SHOT, null);
1020
1021 Intent newIntent = new Intent();
1022 if (requestCode >= 0) {
1023 // Caller is requesting a result.
1024 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
1025 }
1026 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
1027 new IntentSender(target));
1028 if (mService.mHeavyWeightProcess.activities.size() > 0) {
1029 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
1030 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
1031 hist.packageName);
1032 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
1033 hist.task.taskId);
1034 }
1035 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
1036 aInfo.packageName);
1037 newIntent.setFlags(intent.getFlags());
1038 newIntent.setClassName("android",
1039 HeavyWeightSwitcherActivity.class.getName());
1040 intent = newIntent;
1041 resolvedType = null;
1042 caller = null;
1043 callingUid = Binder.getCallingUid();
1044 callingPid = Binder.getCallingPid();
1045 componentSpecified = true;
1046 try {
1047 ResolveInfo rInfo =
1048 AppGlobals.getPackageManager().resolveIntent(
1049 intent, null,
1050 PackageManager.MATCH_DEFAULT_ONLY
1051 | ActivityManagerService.STOCK_PM_FLAGS, userId);
1052 aInfo = rInfo != null ? rInfo.activityInfo : null;
1053 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1054 } catch (RemoteException e) {
1055 aInfo = null;
1056 }
1057 }
1058 }
1059 }
1060
Dianne Hackborn91097de2014-04-04 18:02:06 -07001061 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
1062 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -07001063 requestCode, callingPid, callingUid, callingPackage,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001064 realCallingPid, realCallingUid, startFlags, options, ignoreTargetSecurity,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001065 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001066
Craig Mautner85c11a82014-07-17 12:38:18 -07001067 Binder.restoreCallingIdentity(origId);
1068
Craig Mautnerde4ef022013-04-07 19:01:33 -07001069 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001070 // If the caller also wants to switch to a new configuration,
1071 // do so now. This allows a clean switch, as we are waiting
1072 // for the current activity to pause (so we will not destroy
1073 // it), and have not yet started the next activity.
1074 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
1075 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -07001076 stack.mConfigWillChange = false;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001077 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautner23ac33b2013-04-01 16:26:35 -07001078 "Updating to new configuration after starting activity.");
Maxim Bogatov05075302015-05-19 18:33:08 -07001079 mService.updateConfigurationLocked(config, null, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001080 }
1081
Craig Mautner23ac33b2013-04-01 16:26:35 -07001082 if (outResult != null) {
1083 outResult.result = res;
1084 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001085 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001086 do {
1087 try {
1088 mService.wait();
1089 } catch (InterruptedException e) {
1090 }
1091 } while (!outResult.timeout && outResult.who == null);
1092 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001093 ActivityRecord r = stack.topRunningActivityLocked(null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001094 if (r.nowVisible && r.state == RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001095 outResult.timeout = false;
1096 outResult.who = new ComponentName(r.info.packageName, r.info.name);
1097 outResult.totalTime = 0;
1098 outResult.thisTime = 0;
1099 } else {
1100 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001101 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001102 do {
1103 try {
1104 mService.wait();
1105 } catch (InterruptedException e) {
1106 }
1107 } while (!outResult.timeout && outResult.who == null);
1108 }
1109 }
1110 }
1111
1112 return res;
1113 }
1114 }
1115
1116 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1117 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
1118 Bundle options, int userId) {
1119 if (intents == null) {
1120 throw new NullPointerException("intents is null");
1121 }
1122 if (resolvedTypes == null) {
1123 throw new NullPointerException("resolvedTypes is null");
1124 }
1125 if (intents.length != resolvedTypes.length) {
1126 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1127 }
1128
Craig Mautner23ac33b2013-04-01 16:26:35 -07001129
1130 int callingPid;
1131 if (callingUid >= 0) {
1132 callingPid = -1;
1133 } else if (caller == null) {
1134 callingPid = Binder.getCallingPid();
1135 callingUid = Binder.getCallingUid();
1136 } else {
1137 callingPid = callingUid = -1;
1138 }
1139 final long origId = Binder.clearCallingIdentity();
1140 try {
1141 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001142 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001143 for (int i=0; i<intents.length; i++) {
1144 Intent intent = intents[i];
1145 if (intent == null) {
1146 continue;
1147 }
1148
1149 // Refuse possible leaked file descriptors
1150 if (intent != null && intent.hasFileDescriptors()) {
1151 throw new IllegalArgumentException("File descriptors passed in Intent");
1152 }
1153
1154 boolean componentSpecified = intent.getComponent() != null;
1155
1156 // Don't modify the client's object!
1157 intent = new Intent(intent);
1158
1159 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001160 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001161 // TODO: New, check if this is correct
1162 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1163
1164 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001165 (aInfo.applicationInfo.privateFlags
1166 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001167 throw new IllegalArgumentException(
1168 "FLAG_CANT_SAVE_STATE not supported here");
1169 }
1170
1171 Bundle theseOptions;
1172 if (options != null && i == intents.length-1) {
1173 theseOptions = options;
1174 } else {
1175 theseOptions = null;
1176 }
Craig Mautner6170f732013-04-02 13:05:23 -07001177 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001178 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1179 callingPackage, callingPid, callingUid,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001180 0, theseOptions, false, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001181 if (res < 0) {
1182 return res;
1183 }
1184
1185 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1186 }
1187 }
1188 } finally {
1189 Binder.restoreCallingIdentity(origId);
1190 }
1191
1192 return ActivityManager.START_SUCCESS;
1193 }
1194
Craig Mautner2420ead2013-04-01 17:13:20 -07001195 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001196 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001197 throws RemoteException {
1198
Craig Mautner2568c3a2015-03-26 14:22:34 -07001199 if (andResume) {
1200 r.startFreezingScreenLocked(app, 0);
1201 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001202
Craig Mautner2568c3a2015-03-26 14:22:34 -07001203 // schedule launch ticks to collect information about slow apps.
1204 r.startLaunchTickingLocked();
1205 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001206
1207 // Have the window manager re-evaluate the orientation of
1208 // the screen based on the new activity order. Note that
1209 // as a result of this, it can call back into the activity
1210 // manager with a new orientation. We don't care about that,
1211 // because the activity is not currently running so we are
1212 // just restarting it anyway.
1213 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001214 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001215 mService.mConfiguration,
1216 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001217 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001218 }
1219
1220 r.app = app;
1221 app.waitingToKill = null;
1222 r.launchCount++;
1223 r.lastLaunchTime = SystemClock.uptimeMillis();
1224
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001225 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001226
1227 int idx = app.activities.indexOf(r);
1228 if (idx < 0) {
1229 app.activities.add(r);
1230 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001231 mService.updateLruProcessLocked(app, true, null);
1232 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001233
Craig Mautner15df08a2015-04-01 12:17:18 -07001234 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001235 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1236 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001237 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001238 }
1239
1240 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001241 try {
1242 if (app.thread == null) {
1243 throw new RemoteException();
1244 }
1245 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001246 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001247 if (andResume) {
1248 results = r.results;
1249 newIntents = r.newIntents;
1250 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001251 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1252 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1253 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001254 if (andResume) {
1255 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1256 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001257 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001258 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001259 if (r.isHomeActivity() && r.isNotResolverActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001260 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001261 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001262 }
1263 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1264 r.sleeping = false;
1265 r.forceNewConfig = false;
1266 mService.showAskCompatModeDialogLocked(r);
1267 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001268 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001269 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1270 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1271 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001272 final String profileFile = mService.mProfileFile;
1273 if (profileFile != null) {
1274 ParcelFileDescriptor profileFd = mService.mProfileFd;
1275 if (profileFd != null) {
1276 try {
1277 profileFd = profileFd.dup();
1278 } catch (IOException e) {
1279 if (profileFd != null) {
1280 try {
1281 profileFd.close();
1282 } catch (IOException o) {
1283 }
1284 profileFd = null;
1285 }
1286 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001287 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001288
1289 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1290 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001291 }
1292 }
1293 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001294
Craig Mautner2568c3a2015-03-26 14:22:34 -07001295 if (andResume) {
1296 app.hasShownUi = true;
1297 app.pendingUiClean = true;
1298 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001299 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001300 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001301 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001302 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001303 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001304 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001305
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001306 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001307 // This may be a heavy-weight process! Note that the package
1308 // manager will ensure that only activity can run in the main
1309 // process of the .apk, which is the only thing that will be
1310 // considered heavy-weight.
1311 if (app.processName.equals(app.info.packageName)) {
1312 if (mService.mHeavyWeightProcess != null
1313 && mService.mHeavyWeightProcess != app) {
1314 Slog.w(TAG, "Starting new heavy weight process " + app
1315 + " when already running "
1316 + mService.mHeavyWeightProcess);
1317 }
1318 mService.mHeavyWeightProcess = app;
1319 Message msg = mService.mHandler.obtainMessage(
1320 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1321 msg.obj = r;
1322 mService.mHandler.sendMessage(msg);
1323 }
1324 }
1325
1326 } catch (RemoteException e) {
1327 if (r.launchFailed) {
1328 // This is the second time we failed -- finish activity
1329 // and give up.
1330 Slog.e(TAG, "Second failure launching "
1331 + r.intent.getComponent().flattenToShortString()
1332 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001333 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001334 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1335 "2nd-crash", false);
1336 return false;
1337 }
1338
1339 // This is the first time we failed -- restart process and
1340 // retry.
1341 app.activities.remove(r);
1342 throw e;
1343 }
1344
1345 r.launchFailed = false;
1346 if (stack.updateLRUListLocked(r)) {
1347 Slog.w(TAG, "Activity " + r
1348 + " being launched, but already in LRU list");
1349 }
1350
1351 if (andResume) {
1352 // As part of the process of launching, ActivityThread also performs
1353 // a resume.
1354 stack.minimalResumeActivityLocked(r);
1355 } else {
1356 // This activity is not starting in the resumed state... which
1357 // should look like we asked it to pause+stop (but remain visible),
1358 // and it has done so and reported back the current icicle and
1359 // other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001360 if (DEBUG_STATES) Slog.v(TAG_STATES,
1361 "Moving to STOPPED: " + r + " (starting in stopped state)");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001362 r.state = STOPPED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001363 r.stopped = true;
1364 }
1365
1366 // Launch the new version setup screen if needed. We do this -after-
1367 // launching the initial activity (that is, home), so that it can have
1368 // a chance to initialize itself while in the background, making the
1369 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001370 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001371 mService.startSetupActivityLocked();
1372 }
1373
Dianne Hackborn465fa392014-09-14 14:21:18 -07001374 // Update any services we are bound to that might care about whether
1375 // their client may have activities.
1376 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1377
Craig Mautner2420ead2013-04-01 17:13:20 -07001378 return true;
1379 }
1380
Craig Mautnere79d42682013-04-01 19:01:53 -07001381 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001382 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001383 // Is this activity's application already running?
1384 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001385 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001386
1387 r.task.stack.setLaunchTime(r);
1388
1389 if (app != null && app.thread != null) {
1390 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001391 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1392 || !"android".equals(r.info.packageName)) {
1393 // Don't add this if it is a platform component that is marked
1394 // to run in multiple processes, because this is actually
1395 // part of the framework so doesn't make sense to track as a
1396 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001397 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1398 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001399 }
George Mount2c92c972014-03-20 09:38:23 -07001400 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001401 return;
1402 } catch (RemoteException e) {
1403 Slog.w(TAG, "Exception when starting activity "
1404 + r.intent.getComponent().flattenToShortString(), e);
1405 }
1406
1407 // If a dead object exception was thrown -- fall through to
1408 // restart the application.
1409 }
1410
1411 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001412 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001413 }
1414
Craig Mautner6170f732013-04-02 13:05:23 -07001415 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001416 Intent intent, String resolvedType, ActivityInfo aInfo,
1417 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1418 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001419 int callingPid, int callingUid, String callingPackage,
1420 int realCallingPid, int realCallingUid, int startFlags, Bundle options,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001421 boolean ignoreTargetSecurity, boolean componentSpecified, ActivityRecord[] outActivity,
1422 ActivityContainer container, TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001423 int err = ActivityManager.START_SUCCESS;
1424
1425 ProcessRecord callerApp = null;
1426 if (caller != null) {
1427 callerApp = mService.getRecordForAppLocked(caller);
1428 if (callerApp != null) {
1429 callingPid = callerApp.pid;
1430 callingUid = callerApp.info.uid;
1431 } else {
1432 Slog.w(TAG, "Unable to find app for caller " + caller
1433 + " (pid=" + callingPid + ") when starting: "
1434 + intent.toString());
1435 err = ActivityManager.START_PERMISSION_DENIED;
1436 }
1437 }
1438
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001439 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1440
Craig Mautner6170f732013-04-02 13:05:23 -07001441 if (err == ActivityManager.START_SUCCESS) {
Craig Mautner6170f732013-04-02 13:05:23 -07001442 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001443 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001444 + " on display " + (container == null ? (mFocusedStack == null ?
1445 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1446 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1447 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001448 }
1449
1450 ActivityRecord sourceRecord = null;
1451 ActivityRecord resultRecord = null;
1452 if (resultTo != null) {
1453 sourceRecord = isInAnyStackLocked(resultTo);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001454 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
1455 "Will send result to " + resultTo + " " + sourceRecord);
Craig Mautner6170f732013-04-02 13:05:23 -07001456 if (sourceRecord != null) {
1457 if (requestCode >= 0 && !sourceRecord.finishing) {
1458 resultRecord = sourceRecord;
1459 }
1460 }
1461 }
Craig Mautner6170f732013-04-02 13:05:23 -07001462
Dianne Hackborn91097de2014-04-04 18:02:06 -07001463 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001464
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001465 if ((launchFlags & Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001466 // Transfer the result target from the source activity to the new
1467 // one being started, including any failures.
1468 if (requestCode >= 0) {
1469 ActivityOptions.abort(options);
1470 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1471 }
1472 resultRecord = sourceRecord.resultTo;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001473 if (resultRecord != null && !resultRecord.isInStackLocked()) {
1474 resultRecord = null;
1475 }
Craig Mautner6170f732013-04-02 13:05:23 -07001476 resultWho = sourceRecord.resultWho;
1477 requestCode = sourceRecord.requestCode;
1478 sourceRecord.resultTo = null;
1479 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001480 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001481 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001482 if (sourceRecord.launchedFromUid == callingUid) {
1483 // The new activity is being launched from the same uid as the previous
1484 // activity in the flow, and asking to forward its result back to the
1485 // previous. In this case the activity is serving as a trampoline between
1486 // the two, so we also want to update its launchedFromPackage to be the
1487 // same as the previous activity. Note that this is safe, since we know
1488 // these two packages come from the same uid; the caller could just as
1489 // well have supplied that same package name itself. This specifially
1490 // deals with the case of an intent picker/chooser being launched in the app
1491 // flow to redirect to an activity picked by the user, where we want the final
1492 // activity to consider it to have been launched by the previous app activity.
1493 callingPackage = sourceRecord.launchedFromPackage;
1494 }
Craig Mautner6170f732013-04-02 13:05:23 -07001495 }
1496
1497 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1498 // We couldn't find a class that can handle the given Intent.
1499 // That's the end of that!
1500 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1501 }
1502
1503 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1504 // We couldn't find the specific class specified in the Intent.
1505 // Also the end of the line.
1506 err = ActivityManager.START_CLASS_NOT_FOUND;
1507 }
1508
Dianne Hackborn91097de2014-04-04 18:02:06 -07001509 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1510 && sourceRecord.task.voiceSession != null) {
1511 // If this activity is being launched as part of a voice session, we need
1512 // to ensure that it is safe to do so. If the upcoming activity will also
1513 // be part of the voice session, we can only launch it if it has explicitly
1514 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001515 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001516 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1517 try {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001518 intent.addCategory(Intent.CATEGORY_VOICE);
Dianne Hackborn95465202014-09-15 16:21:55 -07001519 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1520 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001521 Slog.w(TAG,
1522 "Activity being started in current voice task does not support voice: "
1523 + intent);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001524 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1525 }
1526 } catch (RemoteException e) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001527 Slog.w(TAG, "Failure checking voice capabilities", e);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001528 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1529 }
1530 }
1531 }
1532
1533 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1534 // If the caller is starting a new voice session, just make sure the target
1535 // is actually allowing it to run this way.
1536 try {
1537 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1538 intent, resolvedType)) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001539 Slog.w(TAG,
1540 "Activity being started in new voice task does not support: "
1541 + intent);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001542 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1543 }
1544 } catch (RemoteException e) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001545 Slog.w(TAG, "Failure checking voice capabilities", e);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001546 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1547 }
1548 }
1549
louis_changcd5d1982014-08-01 10:09:08 +08001550 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1551
Craig Mautner6170f732013-04-02 13:05:23 -07001552 if (err != ActivityManager.START_SUCCESS) {
1553 if (resultRecord != null) {
1554 resultStack.sendActivityResultLocked(-1,
1555 resultRecord, resultWho, requestCode,
1556 Activity.RESULT_CANCELED, null);
1557 }
Craig Mautner6170f732013-04-02 13:05:23 -07001558 ActivityOptions.abort(options);
1559 return err;
1560 }
1561
Svetoslav7008b512015-06-24 18:47:07 -07001562 boolean abort = false;
1563
Svet Ganov99b60432015-06-27 13:15:22 -07001564 final int startAnyPerm = mService.checkPermission(
1565 START_ANY_ACTIVITY, callingPid, callingUid);
1566
1567 if (startAnyPerm != PERMISSION_GRANTED) {
1568 final int componentRestriction = getComponentRestrictionForCallingPackage(
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001569 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
Svet Ganov99b60432015-06-27 13:15:22 -07001570 final int actionRestriction = getActionRestrictionForCallingPackage(
1571 intent.getAction(), callingPackage, callingPid, callingUid);
1572
1573 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1574 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1575 if (resultRecord != null) {
1576 resultStack.sendActivityResultLocked(-1,
1577 resultRecord, resultWho, requestCode,
1578 Activity.RESULT_CANCELED, null);
1579 }
1580 String msg;
1581 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1582 msg = "Permission Denial: starting " + intent.toString()
1583 + " from " + callerApp + " (pid=" + callingPid
1584 + ", uid=" + callingUid + ")" + " with revoked permission "
1585 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1586 } else if (!aInfo.exported) {
1587 msg = "Permission Denial: starting " + intent.toString()
1588 + " from " + callerApp + " (pid=" + callingPid
1589 + ", uid=" + callingUid + ")"
1590 + " not exported from uid " + aInfo.applicationInfo.uid;
1591 } else {
1592 msg = "Permission Denial: starting " + intent.toString()
1593 + " from " + callerApp + " (pid=" + callingPid
1594 + ", uid=" + callingUid + ")"
1595 + " requires " + aInfo.permission;
1596 }
1597 Slog.w(TAG, msg);
1598 throw new SecurityException(msg);
1599 }
1600
1601 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1602 String message = "Appop Denial: starting " + intent.toString()
1603 + " from " + callerApp + " (pid=" + callingPid
1604 + ", uid=" + callingUid + ")"
1605 + " requires " + AppOpsManager.permissionToOp(
1606 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1607 Slog.w(TAG, message);
1608 abort = true;
1609 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1610 String message = "Appop Denial: starting " + intent.toString()
1611 + " from " + callerApp + " (pid=" + callingPid
1612 + ", uid=" + callingUid + ")"
1613 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1614 Slog.w(TAG, message);
1615 abort = true;
1616 }
Svetoslav7008b512015-06-24 18:47:07 -07001617 }
1618
1619 abort |= !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001620 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001621
Craig Mautner6170f732013-04-02 13:05:23 -07001622 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001623 try {
1624 // The Intent we give to the watcher has the extra data
1625 // stripped off, since it can contain private information.
1626 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001627 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001628 aInfo.applicationInfo.packageName);
1629 } catch (RemoteException e) {
1630 mService.mController = null;
1631 }
Ben Gruver5e207332013-04-03 17:41:37 -07001632 }
Craig Mautner6170f732013-04-02 13:05:23 -07001633
Ben Gruver5e207332013-04-03 17:41:37 -07001634 if (abort) {
1635 if (resultRecord != null) {
1636 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001637 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001638 }
Ben Gruver5e207332013-04-03 17:41:37 -07001639 // We pretend to the caller that it was really started, but
1640 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001641 ActivityOptions.abort(options);
1642 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001643 }
1644
1645 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001646 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001647 requestCode, componentSpecified, voiceSession != null, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001648 if (outActivity != null) {
1649 outActivity[0] = r;
1650 }
1651
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001652 if (r.appTimeTracker == null && sourceRecord != null) {
1653 // If the caller didn't specify an explicit time tracker, we want to continue
1654 // tracking under any it has.
1655 r.appTimeTracker = sourceRecord.appTimeTracker;
1656 }
1657
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001658 final ActivityStack stack = mFocusedStack;
Dianne Hackborn91097de2014-04-04 18:02:06 -07001659 if (voiceSession == null && (stack.mResumedActivity == null
1660 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001661 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1662 realCallingPid, realCallingUid, "Activity start")) {
Craig Mautner6170f732013-04-02 13:05:23 -07001663 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001664 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautneree36c772014-07-16 14:56:05 -07001665 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001666 ActivityOptions.abort(options);
1667 return ActivityManager.START_SWITCHES_CANCELED;
1668 }
1669 }
1670
1671 if (mService.mDidAppSwitch) {
1672 // This is the second allowed switch since we stopped switches,
1673 // so now just generally allow switches. Use case: user presses
1674 // home (switches disabled, switch to home, mDidAppSwitch now true);
1675 // user taps a home icon (coming from home so allowed, we hit here
1676 // and now allow anyone to switch again).
1677 mService.mAppSwitchesAllowedTime = 0;
1678 } else {
1679 mService.mDidAppSwitch = true;
1680 }
1681
Craig Mautneree36c772014-07-16 14:56:05 -07001682 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001683
Dianne Hackborn91097de2014-04-04 18:02:06 -07001684 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001685 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001686
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001687 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001688 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001689 // activity start, but we are not actually doing an activity
1690 // switch... just dismiss the keyguard now, because we
1691 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001692 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001693 }
1694 return err;
1695 }
1696
Svet Ganov99b60432015-06-27 13:15:22 -07001697 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001698 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001699 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1700 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001701 == PackageManager.PERMISSION_DENIED) {
1702 return ACTIVITY_RESTRICTION_PERMISSION;
1703 }
1704
Christopher Tateff7add02015-08-17 10:23:22 -07001705 if (activityInfo.permission == null) {
1706 return ACTIVITY_RESTRICTION_NONE;
1707 }
1708
Svet Ganov99b60432015-06-27 13:15:22 -07001709 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1710 if (opCode == AppOpsManager.OP_NONE) {
1711 return ACTIVITY_RESTRICTION_NONE;
1712 }
1713
1714 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1715 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001716 if (!ignoreTargetSecurity) {
1717 return ACTIVITY_RESTRICTION_APPOP;
1718 }
Svet Ganov99b60432015-06-27 13:15:22 -07001719 }
1720
1721 return ACTIVITY_RESTRICTION_NONE;
1722 }
1723
Svetoslav7008b512015-06-24 18:47:07 -07001724 private int getActionRestrictionForCallingPackage(String action,
1725 String callingPackage, int callingPid, int callingUid) {
1726 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001727 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001728 }
1729
1730 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1731 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001732 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001733 }
1734
1735 final PackageInfo packageInfo;
1736 try {
1737 packageInfo = mService.mContext.getPackageManager()
1738 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1739 } catch (PackageManager.NameNotFoundException e) {
1740 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001741 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001742 }
1743
1744 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001745 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001746 }
1747
1748 if (mService.checkPermission(permission, callingPid, callingUid) ==
1749 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001750 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001751 }
1752
1753 final int opCode = AppOpsManager.permissionToOpCode(permission);
1754 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001755 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001756 }
1757
1758 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1759 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001760 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001761 }
1762
Svet Ganov99b60432015-06-27 13:15:22 -07001763 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001764 }
1765
Chong Zhang0fa656b2015-08-31 15:17:21 -07001766 ActivityStack computeStackFocus(ActivityRecord r, boolean newTask, Rect bounds) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001767 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001768
1769 // On leanback only devices we should keep all activities in the same stack.
1770 if (!mLeanbackOnlyDevice &&
1771 (r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001772
1773 ActivityStack stack;
1774
Wale Ogunwale7d701172015-03-11 15:36:30 -07001775 if (task != null && task.stack != null) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001776 stack = task.stack;
1777 if (stack.isOnHomeDisplay()) {
1778 if (mFocusedStack != stack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001779 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1780 "computeStackFocus: Setting " + "focused stack to r=" + r
1781 + " task=" + task);
Craig Mautnere0a38842013-12-16 16:14:02 -08001782 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001783 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001784 "computeStackFocus: Focused stack already=" + mFocusedStack);
Craig Mautnere0a38842013-12-16 16:14:02 -08001785 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001786 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001787 return stack;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001788 }
1789
Craig Mautnere0a38842013-12-16 16:14:02 -08001790 final ActivityContainer container = r.mInitialActivityContainer;
1791 if (container != null) {
1792 // The first time put it on the desired stack, after this put on task stack.
1793 r.mInitialActivityContainer = null;
1794 return container.mStack;
1795 }
1796
Craig Mautner1b4bf852014-05-26 15:06:32 -07001797 if (mFocusedStack != mHomeStack && (!newTask ||
1798 mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001799 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001800 "computeStackFocus: Have a focused stack=" + mFocusedStack);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001801 return mFocusedStack;
1802 }
1803
Wale Ogunwale706ed792015-08-02 10:29:44 -07001804 // We first try to put the task in the first dynamic stack.
Craig Mautnere0a38842013-12-16 16:14:02 -08001805 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1806 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001807 stack = homeDisplayStacks.get(stackNdx);
Wale Ogunwale706ed792015-08-02 10:29:44 -07001808 final boolean isDynamicStack = stack.mStackId >= FIRST_DYNAMIC_STACK_ID;
1809 if (isDynamicStack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001810 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001811 "computeStackFocus: Setting focused stack=" + stack);
1812 return stack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001813 }
1814 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001815
Wale Ogunwale706ed792015-08-02 10:29:44 -07001816 // If there is no suitable dynamic stack then we figure out which static stack to use.
Chong Zhang0fa656b2015-08-31 15:17:21 -07001817 int stackId = task != null ? task.getLaunchStackId() :
1818 bounds != null ? FREEFORM_WORKSPACE_STACK_ID :
1819 FULLSCREEN_WORKSPACE_STACK_ID;
1820 stack = getStack(stackId, CREATE_IF_NEEDED, ON_TOP);
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001821 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS, "computeStackFocus: New stack r="
1822 + r + " stackId=" + stack.mStackId);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001823 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001824 }
1825 return mHomeStack;
1826 }
1827
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001828 boolean setFocusedStack(ActivityRecord r, String reason) {
1829 if (r == null) {
1830 // Not sure what you are trying to do, but it is not going to work...
1831 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001832 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001833 final TaskRecord task = r.task;
1834 if (task == null || task.stack == null) {
1835 Slog.w(TAG, "Can't set focus stack for r=" + r + " task=" + task);
1836 return false;
1837 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001838 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001839 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001840 }
1841
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001842 final int startActivityUncheckedLocked(final ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001843 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001844 boolean doResume, Bundle options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001845 final Intent intent = r.intent;
1846 final int callingUid = r.launchedFromUid;
1847
Chong Zhang0fa656b2015-08-31 15:17:21 -07001848 boolean overrideBounds = false;
1849 Rect newBounds = null;
1850 if (r.info.resizeable || (inTask != null && inTask.mResizeable)) {
1851 if (intent.hasExtra(ActivityOptions.KEY_BOUNDS)) {
1852 overrideBounds = true;
1853 newBounds = Rect.unflattenFromString(
1854 intent.getStringExtra(ActivityOptions.KEY_BOUNDS));
1855 } else if (options != null) {
1856 ActivityOptions opts = new ActivityOptions(options);
1857 if (opts.hasBounds()) {
1858 overrideBounds = true;
1859 newBounds = opts.getBounds();
1860 }
1861 }
1862 }
1863
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001864 // In some flows in to this function, we retrieve the task record and hold on to it
1865 // without a lock before calling back in to here... so the task at this point may
1866 // not actually be in recents. Check for that, and if it isn't in recents just
1867 // consider it invalid.
1868 if (inTask != null && !inTask.inRecents) {
1869 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1870 inTask = null;
1871 }
1872
Craig Mautnerad400af2014-08-13 16:41:36 -07001873 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001874 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1875 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001876
Craig Mautnera228ae92014-07-09 05:44:55 -07001877 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001878 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001879 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001880 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1881 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1882 "\"singleInstance\" or \"singleTask\"");
1883 launchFlags &=
1884 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1885 } else {
1886 switch (r.info.documentLaunchMode) {
1887 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1888 break;
1889 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1890 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1891 break;
1892 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1893 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1894 break;
1895 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1896 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1897 break;
1898 }
1899 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001900
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001901 final boolean launchTaskBehind = r.mLaunchTaskBehind
1902 && !launchSingleTask && !launchSingleInstance
1903 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001904
Wale Ogunwale7d701172015-03-11 15:36:30 -07001905 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0
1906 && r.resultTo.task.stack != null) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001907 // For whatever reason this activity is being launched into a new
1908 // task... yet the caller has requested a result back. Well, that
1909 // is pretty messed up, so instead immediately send back a cancel
1910 // and let the new task continue launched as normal without a
1911 // dependency on its originator.
1912 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1913 r.resultTo.task.stack.sendActivityResultLocked(-1,
1914 r.resultTo, r.resultWho, r.requestCode,
1915 Activity.RESULT_CANCELED, null);
1916 r.resultTo = null;
1917 }
1918
1919 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1920 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1921 }
1922
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001923 // If we are actually going to launch in to a new task, there are some cases where
1924 // we further want to do multiple task.
1925 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1926 if (launchTaskBehind
1927 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
1928 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1929 }
1930 }
1931
Craig Mautner8849a5e2013-04-02 16:41:03 -07001932 // We'll invoke onUserLeaving before onPause only if the launching
1933 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001934 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001935 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
1936 "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001937
1938 // If the caller has asked not to resume at this point, we make note
1939 // of this in the record so that we can skip it when trying to find
1940 // the top running activity.
Chong Zhang45c25ce2015-08-10 22:18:26 -07001941 if (!doResume || !okToShowLocked(r)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001942 r.delayedResume = true;
Chong Zhang45c25ce2015-08-10 22:18:26 -07001943 doResume = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001944 }
1945
Craig Mautnera254cd72014-05-25 16:47:39 -07001946 ActivityRecord notTop =
1947 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001948
1949 // If the onlyIfNeeded flag is set, then we can do this if the activity
1950 // being launched is the same as the one making the call... or, as
1951 // a special case, if we do not know the caller then we count the
1952 // current top activity as the caller.
1953 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1954 ActivityRecord checkedCaller = sourceRecord;
1955 if (checkedCaller == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001956 checkedCaller = mFocusedStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001957 }
1958 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1959 // Caller is not the same as launcher, so always needed.
1960 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1961 }
1962 }
1963
Craig Mautner8849a5e2013-04-02 16:41:03 -07001964 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001965 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001966
1967 // If the caller is not coming from another activity, but has given us an
1968 // explicit task into which they would like us to launch the new activity,
1969 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001970 if (sourceRecord == null && inTask != null && inTask.stack != null) {
1971 final Intent baseIntent = inTask.getBaseIntent();
1972 final ActivityRecord root = inTask.getRootActivity();
1973 if (baseIntent == null) {
1974 ActivityOptions.abort(options);
1975 throw new IllegalArgumentException("Launching into task without base intent: "
1976 + inTask);
1977 }
1978
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001979 // If this task is empty, then we are adding the first activity -- it
1980 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001981 if (launchSingleInstance || launchSingleTask) {
1982 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
1983 ActivityOptions.abort(options);
1984 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
1985 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001986 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001987 if (root != null) {
1988 ActivityOptions.abort(options);
1989 throw new IllegalArgumentException("Caller with inTask " + inTask
1990 + " has root " + root + " but target is singleInstance/Task");
1991 }
1992 }
1993
1994 // If task is empty, then adopt the interesting intent launch flags in to the
1995 // activity being started.
1996 if (root == null) {
1997 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
1998 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
1999 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
2000 launchFlags = (launchFlags&~flagsOfInterest)
2001 | (baseIntent.getFlags()&flagsOfInterest);
2002 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002003 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07002004 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002005
Dianne Hackborn962d5352014-08-29 16:27:40 -07002006 // If the task is not empty and the caller is asking to start it as the root
2007 // of a new task, then we don't actually want to start this on the task. We
2008 // will bring the task to the front, and possibly give it a new intent.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002009 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07002010 addingToTask = false;
2011
2012 } else {
2013 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002014 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07002015
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002016 reuseTask = inTask;
2017 } else {
2018 inTask = null;
2019 }
2020
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002021 if (inTask == null) {
2022 if (sourceRecord == null) {
2023 // This activity is not being started from another... in this
2024 // case we -always- start a new task.
2025 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
2026 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
2027 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
2028 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2029 }
2030 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
2031 // The original activity who is starting us is running as a single
2032 // instance... this new activity it is starting must go on its
2033 // own task.
2034 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2035 } else if (launchSingleInstance || launchSingleTask) {
2036 // The activity being started is a single instance... it always
2037 // gets launched into its own task.
2038 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2039 }
2040 }
2041
2042 ActivityInfo newTaskInfo = null;
2043 Intent newTaskIntent = null;
2044 ActivityStack sourceStack;
2045 if (sourceRecord != null) {
2046 if (sourceRecord.finishing) {
2047 // If the source is finishing, we can't further count it as our source. This
2048 // is because the task it is associated with may now be empty and on its way out,
2049 // so we don't want to blindly throw it in to that task. Instead we will take
2050 // the NEW_TASK flow and try to find a task for it. But save the task information
2051 // so it can be used when creating the new task.
2052 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
2053 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
2054 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
2055 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2056 newTaskInfo = sourceRecord.info;
2057 newTaskIntent = sourceRecord.task.intent;
2058 }
2059 sourceRecord = null;
2060 sourceStack = null;
2061 } else {
2062 sourceStack = sourceRecord.task.stack;
2063 }
2064 } else {
2065 sourceStack = null;
2066 }
2067
2068 boolean movedHome = false;
2069 ActivityStack targetStack;
2070
2071 intent.setFlags(launchFlags);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002072 final boolean noAnimation = (launchFlags & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002073
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002074 // We may want to try to place the new activity in to an existing task. We always
2075 // do this if the target activity is singleTask or singleInstance; we will also do
2076 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
2077 // us to still place it in a new task: multi task, always doc mode, or being asked to
2078 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07002079 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
2080 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07002081 || launchSingleInstance || launchSingleTask) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002082 // If bring to front is requested, and no result is requested and we have not
2083 // been given an explicit task to launch in to, and
Craig Mautner8849a5e2013-04-02 16:41:03 -07002084 // we can find a task that was started with this same
2085 // component, then instead of launching bring that one to the front.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002086 if (inTask == null && r.resultTo == null) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002087 // See if there is a task to bring to the front. If this is
2088 // a SINGLE_INSTANCE activity, there can be one and only one
2089 // instance of it in the history, and it is always in its own
2090 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07002091 ActivityRecord intentActivity = !launchSingleInstance ?
2092 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002093 if (intentActivity != null) {
Jason Monk25d237b2015-06-19 10:39:39 -04002094 // When the flags NEW_TASK and CLEAR_TASK are set, then the task gets reused
2095 // but still needs to be a lock task mode violation since the task gets
2096 // cleared out and the device would otherwise leave the locked task.
2097 if (isLockTaskModeViolation(intentActivity.task,
2098 (launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
2099 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))) {
Craig Mautner6cd6cec2015-04-01 00:02:12 -07002100 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07002101 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08002102 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2103 }
Craig Mautner29219d92013-04-16 20:19:12 -07002104 if (r.task == null) {
2105 r.task = intentActivity.task;
2106 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002107 if (intentActivity.task.intent == null) {
2108 // This task was started because of movement of
2109 // the activity based on affinity... now that we
2110 // are actually launching it, we can assign the
2111 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07002112 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002113 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002114 targetStack = intentActivity.task.stack;
2115 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002116 // If the target task is not in the front, then we need
2117 // to bring it to the front... except... well, with
2118 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
2119 // to have the same behavior as if a new instance was
2120 // being started, which means not bringing it to the front
2121 // if the caller is not itself in the front.
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002122 final ActivityStack focusStack = getFocusedStack();
2123 ActivityRecord curTop = (focusStack == null)
2124 ? null : focusStack.topRunningNonDelayedActivityLocked(notTop);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002125 boolean movedToFront = false;
Craig Mautner7504d7b2013-09-17 10:50:53 -07002126 if (curTop != null && (curTop.task != intentActivity.task ||
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002127 curTop.task != focusStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002128 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07002129 if (sourceRecord == null || (sourceStack.topActivity() != null &&
2130 sourceStack.topActivity().task == sourceRecord.task)) {
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002131 // We really do want to push this one into the user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07002132 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002133 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
2134 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002135 movedHome = true;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002136 targetStack.moveTaskToFrontLocked(intentActivity.task, noAnimation,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002137 options, r.appTimeTracker, "bringingFoundTaskToFront");
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002138 movedToFront = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002139 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07002140 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2141 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07002142 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07002143 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002144 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002145 options = null;
2146 }
2147 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002148 if (!movedToFront && doResume) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002149 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Bring to front target: " + targetStack
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002150 + " from " + intentActivity);
2151 targetStack.moveToFront("intentActivityFound");
2152 }
2153
Craig Mautner8849a5e2013-04-02 16:41:03 -07002154 // If the caller has requested that the target task be
2155 // reset, then do so.
2156 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
2157 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
2158 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002159 if ((startFlags & ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002160 // We don't need to start a new activity, and
2161 // the client said not to do anything if that
2162 // is the case, so this is it! And for paranoia, make
2163 // sure we have correctly resumed the top activity.
2164 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002165 resumeTopActivitiesLocked(targetStack, null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002166
2167 // Make sure to notify Keyguard as well if we are not running an app
2168 // transition later.
2169 if (!movedToFront) {
2170 notifyActivityDrawnForKeyguard();
2171 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002172 } else {
2173 ActivityOptions.abort(options);
2174 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002175 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002176 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2177 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002178 if ((launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002179 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002180 // The caller has requested to completely replace any
2181 // existing task with its new activity. Well that should
2182 // not be too hard...
2183 reuseTask = intentActivity.task;
2184 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07002185 reuseTask.setIntent(r);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002186 } else if ((launchFlags & FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07002187 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002188 // In this situation we want to remove all activities
2189 // from the task up to the one being started. In most
2190 // cases this means we are resetting the task to its
2191 // initial state.
2192 ActivityRecord top =
2193 intentActivity.task.performClearTaskLocked(r, launchFlags);
2194 if (top != null) {
2195 if (top.frontOfTask) {
2196 // Activity aliases may mean we use different
2197 // intents for the top activity, so make sure
2198 // the task now has the identity of the new
2199 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07002200 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002201 }
2202 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
2203 r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002204 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002205 } else {
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002206 // A special case: we need to start the activity because it is not
2207 // currently running, and the caller has asked to clear the current
2208 // task to have this activity at the top.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002209 addingToTask = true;
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002210 // Now pretend like this activity is being started by the top of its
2211 // task, so it is put in the right place.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002212 sourceRecord = intentActivity;
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002213 TaskRecord task = sourceRecord.task;
2214 if (task != null && task.stack == null) {
2215 // Target stack got cleared when we all activities were removed
2216 // above. Go ahead and reset it.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002217 targetStack = computeStackFocus(
2218 sourceRecord, false /* newTask */, null /* bounds */);
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002219 targetStack.addTask(
2220 task, !launchTaskBehind /* toTop */, false /* moving */);
2221 }
2222
Craig Mautner8849a5e2013-04-02 16:41:03 -07002223 }
2224 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
2225 // In this case the top activity on the task is the
2226 // same as the one being launched, so we take that
2227 // as a request to bring the task to the foreground.
2228 // If the top activity in the task is the root
2229 // activity, deliver this new intent to it if it
2230 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07002231 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07002232 && intentActivity.realActivity.equals(r.realActivity)) {
2233 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
2234 intentActivity.task);
2235 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07002236 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002237 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002238 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
2239 r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002240 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
2241 // In this case we are launching the root activity
2242 // of the task, but with a different intent. We
2243 // should start a new instance on top.
2244 addingToTask = true;
2245 sourceRecord = intentActivity;
2246 }
2247 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
2248 // In this case an activity is being launched in to an
2249 // existing task, without resetting that task. This
2250 // is typically the situation of launching an activity
2251 // from a notification or shortcut. We want to place
2252 // the new activity on top of the current task.
2253 addingToTask = true;
2254 sourceRecord = intentActivity;
2255 } else if (!intentActivity.task.rootWasReset) {
2256 // In this case we are launching in to an existing task
2257 // that has not yet been started from its front door.
2258 // The current task has been brought to the front.
2259 // Ideally, we'd probably like to place this new task
2260 // at the bottom of its stack, but that's a little hard
2261 // to do with the current organization of the code so
2262 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07002263 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002264 }
2265 if (!addingToTask && reuseTask == null) {
2266 // We didn't do anything... but it was needed (a.k.a., client
2267 // don't use that intent!) And for paranoia, make
2268 // sure we have correctly resumed the top activity.
2269 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002270 targetStack.resumeTopActivityLocked(null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002271 if (!movedToFront) {
2272 // Make sure to notify Keyguard as well if we are not running an app
2273 // transition later.
2274 notifyActivityDrawnForKeyguard();
2275 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002276 } else {
2277 ActivityOptions.abort(options);
2278 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002279 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002280 return ActivityManager.START_TASK_TO_FRONT;
2281 }
2282 }
2283 }
2284 }
2285
2286 //String uri = r.intent.toURI();
2287 //Intent intent2 = new Intent(uri);
2288 //Slog.i(TAG, "Given intent: " + r.intent);
2289 //Slog.i(TAG, "URI is: " + uri);
2290 //Slog.i(TAG, "To intent: " + intent2);
2291
2292 if (r.packageName != null) {
2293 // If the activity being launched is the same as the one currently
2294 // at the top, then we need to check if it should only be launched
2295 // once.
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002296 ActivityStack topStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002297 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002298 if (top != null && r.resultTo == null) {
2299 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2300 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07002301 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07002302 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002303 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
2304 top.task);
2305 // For paranoia, make sure we have correctly
2306 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002307 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002308 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002309 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002310 }
2311 ActivityOptions.abort(options);
2312 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2313 // We don't need to start a new activity, and
2314 // the client said not to do anything if that
2315 // is the case, so this is it!
2316 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2317 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002318 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002319 return ActivityManager.START_DELIVERED_TO_TOP;
2320 }
2321 }
2322 }
2323 }
2324
2325 } else {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002326 if (r.resultTo != null && r.resultTo.task.stack != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002327 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2328 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002329 }
2330 ActivityOptions.abort(options);
2331 return ActivityManager.START_CLASS_NOT_FOUND;
2332 }
2333
2334 boolean newTask = false;
2335 boolean keepCurTransition = false;
2336
Craig Mautnerbb742462014-07-07 15:28:55 -07002337 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002338 sourceRecord.task : null;
2339
Craig Mautner8849a5e2013-04-02 16:41:03 -07002340 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002341 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002342 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002343 newTask = true;
Chong Zhang0fa656b2015-08-31 15:17:21 -07002344 targetStack = computeStackFocus(r, newTask, newBounds);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002345 if (doResume) {
2346 targetStack.moveToFront("startingNewTask");
2347 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002348
Craig Mautner8849a5e2013-04-02 16:41:03 -07002349 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002350 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2351 newTaskInfo != null ? newTaskInfo : r.info,
2352 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002353 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2354 taskToAffiliate);
Chong Zhang0fa656b2015-08-31 15:17:21 -07002355 if (overrideBounds) {
2356 r.task.updateOverrideConfiguration(newBounds);
2357 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002358 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2359 "Starting new activity " + r + " in new task " + r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002360 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002361 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002362 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002363 if (isLockTaskModeViolation(r.task)) {
2364 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2365 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2366 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002367 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002368 if ((launchFlags &
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002369 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2370 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002371 // Caller wants to appear on home activity, so before starting
2372 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002373 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002374 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002375 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002376 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002377 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002378 if (isLockTaskModeViolation(sourceTask)) {
2379 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2380 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2381 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002382 targetStack = sourceTask.stack;
Chong Zhang45c25ce2015-08-10 22:18:26 -07002383 if (doResume) {
2384 targetStack.moveToFront("sourceStackToFront");
2385 }
Craig Mautner737fae22014-10-15 12:52:10 -07002386 final TaskRecord topTask = targetStack.topTask();
2387 if (topTask != sourceTask) {
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002388 targetStack.moveTaskToFrontLocked(sourceTask, noAnimation, options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002389 r.appTimeTracker, "sourceTaskToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002390 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002391 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002392 // In this case, we are adding the activity to an existing
2393 // task, but the caller has asked to clear that task if the
2394 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002395 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002396 keepCurTransition = true;
2397 if (top != null) {
2398 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002399 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002400 // For paranoia, make sure we have correctly
2401 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002402 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002403 if (doResume) {
2404 targetStack.resumeTopActivityLocked(null);
2405 }
2406 ActivityOptions.abort(options);
2407 return ActivityManager.START_DELIVERED_TO_TOP;
2408 }
2409 } else if (!addingToTask &&
2410 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2411 // In this case, we are launching an activity in our own task
2412 // that may already be running somewhere in the history, and
2413 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002414 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002415 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002416 final TaskRecord task = top.task;
2417 task.moveActivityToFrontLocked(top);
2418 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002419 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002420 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002421 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002422 if (doResume) {
2423 targetStack.resumeTopActivityLocked(null);
2424 }
2425 return ActivityManager.START_DELIVERED_TO_TOP;
2426 }
2427 }
2428 // An existing activity is starting this new activity, so we want
2429 // to keep the new one in the same task as the one that is starting
2430 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002431 r.setTask(sourceTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002432 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002433 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002434
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002435 } else if (inTask != null) {
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002436 // The caller is asking that the new activity be started in an explicit
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002437 // task it has provided to us.
2438 if (isLockTaskModeViolation(inTask)) {
2439 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2440 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2441 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002442 if (overrideBounds) {
2443 inTask.updateOverrideConfiguration(newBounds);
2444 int stackId = inTask.getLaunchStackId();
2445 if (stackId != inTask.stack.mStackId) {
2446 moveTaskToStackUncheckedLocked(
2447 inTask, stackId, ON_TOP, !FORCE_FOCUS, "inTaskToFront");
2448 }
2449 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002450 targetStack = inTask.stack;
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002451 targetStack.moveTaskToFrontLocked(inTask, noAnimation, options, r.appTimeTracker,
2452 "inTaskToFront");
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002453
2454 // Check whether we should actually launch the new activity in to the task,
2455 // or just reuse the current activity on top.
2456 ActivityRecord top = inTask.getTopActivity();
2457 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2458 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2459 || launchSingleTop || launchSingleTask) {
2460 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2461 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2462 // We don't need to start a new activity, and
2463 // the client said not to do anything if that
2464 // is the case, so this is it!
2465 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2466 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002467 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002468 return ActivityManager.START_DELIVERED_TO_TOP;
2469 }
2470 }
2471
Dianne Hackborn962d5352014-08-29 16:27:40 -07002472 if (!addingToTask) {
2473 // We don't actually want to have this activity added to the task, so just
2474 // stop here but still tell the caller that we consumed the intent.
2475 ActivityOptions.abort(options);
2476 return ActivityManager.START_TASK_TO_FRONT;
2477 }
2478
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002479 r.setTask(inTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002480 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002481 + " in explicit task " + r.task);
2482
Craig Mautner8849a5e2013-04-02 16:41:03 -07002483 } else {
2484 // This not being started from an existing activity, and not part
2485 // of a new task... just put it in the top task, though these days
2486 // this case should never happen.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002487 targetStack = computeStackFocus(r, newTask, null /* bounds */);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002488 if (doResume) {
2489 targetStack.moveToFront("addingToTopTask");
2490 }
Craig Mautner1602ec22013-05-12 10:24:27 -07002491 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002492 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002493 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002494 mWindowManager.moveTaskToTop(r.task.taskId);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002495 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Craig Mautner8849a5e2013-04-02 16:41:03 -07002496 + " in new guessed " + r.task);
2497 }
2498
2499 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002500 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002501
Craig Mautner76e2a762014-06-24 09:05:43 -07002502 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2503 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2504 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002505 if (newTask) {
2506 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2507 }
2508 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002509 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002510 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002511 if (!launchTaskBehind && doResume) {
Craig Mautner299f9602015-01-26 09:47:33 -08002512 mService.setFocusedActivityLocked(r, "startedActivity");
Craig Mautnerbb742462014-07-07 15:28:55 -07002513 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002514 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002515 return ActivityManager.START_SUCCESS;
2516 }
2517
Craig Mautneree36c772014-07-16 14:56:05 -07002518 final void doPendingActivityLaunchesLocked(boolean doResume) {
2519 while (!mPendingActivityLaunches.isEmpty()) {
2520 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Craig Mautneraa9b0f12014-07-17 10:50:18 -07002521 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002522 doResume && mPendingActivityLaunches.isEmpty(), null, null);
Craig Mautneree36c772014-07-16 14:56:05 -07002523 }
2524 }
2525
Craig Mautner7f13ed32014-07-28 14:00:35 -07002526 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002527 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2528 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002529 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002530 mPendingActivityLaunches.remove(palNdx);
2531 }
2532 }
2533 }
2534
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002535 void setLaunchSource(int uid) {
2536 mLaunchingActivity.setWorkSource(new WorkSource(uid));
2537 }
2538
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002539 void acquireLaunchWakelock() {
2540 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2541 throw new IllegalStateException("Calling must be system uid");
2542 }
2543 mLaunchingActivity.acquire();
2544 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2545 // To be safe, don't allow the wake lock to be held for too long.
2546 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2547 }
2548 }
2549
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002550 /**
2551 * Called when the frontmost task is idle.
2552 * @return the state of mService.mBooting before this was called.
2553 */
2554 private boolean checkFinishBootingLocked() {
2555 final boolean booting = mService.mBooting;
2556 boolean enableScreen = false;
2557 mService.mBooting = false;
2558 if (!mService.mBooted) {
2559 mService.mBooted = true;
2560 enableScreen = true;
2561 }
2562 if (booting || enableScreen) {
2563 mService.postFinishBooting(booting, enableScreen);
2564 }
2565 return booting;
2566 }
2567
Craig Mautnerf3333272013-04-22 10:55:53 -07002568 // Checked.
2569 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2570 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002571 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002572
Craig Mautnerf3333272013-04-22 10:55:53 -07002573 ArrayList<ActivityRecord> stops = null;
2574 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07002575 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07002576 int NS = 0;
2577 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002578 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002579 boolean activityRemoved = false;
2580
Wale Ogunwale7d701172015-03-11 15:36:30 -07002581 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002582 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002583 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
2584 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002585 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2586 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002587 if (fromTimeout) {
2588 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002589 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002590
2591 // This is a hack to semi-deal with a race condition
2592 // in the client where it can be constructed with a
2593 // newer configuration from when we asked it to launch.
2594 // We'll update with whatever configuration it now says
2595 // it used to launch.
2596 if (config != null) {
2597 r.configuration = config;
2598 }
2599
2600 // We are now idle. If someone is waiting for a thumbnail from
2601 // us, we can now deliver.
2602 r.idle = true;
2603
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002604 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Dianne Hackborn9449a612014-10-01 15:53:28 -07002605 if (isFrontStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002606 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002607 }
2608 }
2609
2610 if (allResumedActivitiesIdle()) {
2611 if (r != null) {
2612 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002613 }
2614
2615 if (mLaunchingActivity.isHeld()) {
2616 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2617 if (VALIDATE_WAKE_LOCK_CALLER &&
2618 Binder.getCallingUid() != Process.myUid()) {
2619 throw new IllegalStateException("Calling must be system uid");
2620 }
2621 mLaunchingActivity.release();
2622 }
2623 ensureActivitiesVisibleLocked(null, 0);
Craig Mautnerf3333272013-04-22 10:55:53 -07002624 }
2625
2626 // Atomically retrieve all of the other things to do.
2627 stops = processStoppingActivitiesLocked(true);
2628 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002629 if ((NF = mFinishingActivities.size()) > 0) {
2630 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002631 mFinishingActivities.clear();
2632 }
2633
Craig Mautnerf3333272013-04-22 10:55:53 -07002634 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002635 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07002636 mStartingUsers.clear();
2637 }
2638
Craig Mautnerf3333272013-04-22 10:55:53 -07002639 // Stop any activities that are scheduled to do so but have been
2640 // waiting for the next one to start.
2641 for (int i = 0; i < NS; i++) {
2642 r = stops.get(i);
2643 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002644 if (stack != null) {
2645 if (r.finishing) {
2646 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2647 } else {
2648 stack.stopActivityLocked(r);
2649 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002650 }
2651 }
2652
2653 // Finish any activities that are scheduled to do so but have been
2654 // waiting for the next one to start.
2655 for (int i = 0; i < NF; i++) {
2656 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07002657 final ActivityStack stack = r.task.stack;
2658 if (stack != null) {
2659 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
2660 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002661 }
2662
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002663 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002664 // Complete user switch
2665 if (startingUsers != null) {
2666 for (int i = 0; i < startingUsers.size(); i++) {
2667 mService.finishUserSwitch(startingUsers.get(i));
2668 }
2669 }
2670 // Complete starting up of background users
2671 if (mStartingBackgroundUsers.size() > 0) {
Amith Yamasani37a40c22015-06-17 13:25:42 -07002672 startingUsers = new ArrayList<UserState>(mStartingBackgroundUsers);
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002673 mStartingBackgroundUsers.clear();
2674 for (int i = 0; i < startingUsers.size(); i++) {
2675 mService.finishUserBoot(startingUsers.get(i));
2676 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002677 }
2678 }
2679
2680 mService.trimApplications();
2681 //dump();
2682 //mWindowManager.dump();
2683
Craig Mautnerf3333272013-04-22 10:55:53 -07002684 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002685 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002686 }
2687
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002688 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002689 }
2690
Craig Mautner8e569572013-10-11 17:36:59 -07002691 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002692 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002693 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2694 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002695 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2696 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2697 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002698 }
Craig Mautner19091252013-10-05 00:03:53 -07002699 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002700 }
2701
2702 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002703 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2704 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002705 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2706 stacks.get(stackNdx).closeSystemDialogsLocked();
2707 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002708 }
2709 }
2710
Craig Mautner93529a42013-10-04 15:03:13 -07002711 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002712 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002713 }
2714
Craig Mautner8d341ef2013-03-26 09:03:27 -07002715 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07002716 * Update the last used stack id for non-current user (current user's last
2717 * used stack is the focused stack)
2718 */
2719 void updateUserStackLocked(int userId, ActivityStack stack) {
2720 if (userId != mCurrentUser) {
2721 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
2722 }
2723 }
2724
2725 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07002726 * @return true if some activity was finished (or would have finished if doit were true).
2727 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07002728 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
2729 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002730 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002731 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2732 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002733 final int numStacks = stacks.size();
2734 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2735 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07002736 if (stack.finishDisabledPackageActivitiesLocked(
2737 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002738 didSomething = true;
2739 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002740 }
2741 }
2742 return didSomething;
2743 }
2744
Dianne Hackborna413dc02013-07-12 12:02:55 -07002745 void updatePreviousProcessLocked(ActivityRecord r) {
2746 // Now that this process has stopped, we may want to consider
2747 // it to be the previous app to try to keep around in case
2748 // the user wants to return to it.
2749
2750 // First, found out what is currently the foreground app, so that
2751 // we don't blow away the previous app if this activity is being
2752 // hosted by the process that is actually still the foreground.
2753 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002754 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2755 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002756 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2757 final ActivityStack stack = stacks.get(stackNdx);
2758 if (isFrontStack(stack)) {
2759 if (stack.mResumedActivity != null) {
2760 fgApp = stack.mResumedActivity.app;
2761 } else if (stack.mPausingActivity != null) {
2762 fgApp = stack.mPausingActivity.app;
2763 }
2764 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002765 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002766 }
2767 }
2768
2769 // Now set this one as the previous process, only if that really
2770 // makes sense to.
2771 if (r.app != null && fgApp != null && r.app != fgApp
2772 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002773 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002774 mService.mPreviousProcess = r.app;
2775 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2776 }
2777 }
2778
Craig Mautner05d29032013-05-03 13:40:13 -07002779 boolean resumeTopActivitiesLocked() {
2780 return resumeTopActivitiesLocked(null, null, null);
2781 }
2782
2783 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2784 Bundle targetOptions) {
2785 if (targetStack == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002786 targetStack = mFocusedStack;
Craig Mautner05d29032013-05-03 13:40:13 -07002787 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002788 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002789 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002790 if (isFrontStack(targetStack)) {
2791 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2792 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002793
Craig Mautnere0a38842013-12-16 16:14:02 -08002794 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2795 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002796 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2797 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002798 if (stack == targetStack) {
2799 // Already started above.
2800 continue;
2801 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002802 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002803 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002804 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002805 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002806 }
Craig Mautner05d29032013-05-03 13:40:13 -07002807 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002808 }
2809
Todd Kennedy539db512014-12-15 09:57:55 -08002810 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002811 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2812 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002813 final int numStacks = stacks.size();
2814 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2815 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08002816 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002817 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002818 }
2819 }
2820
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002821 void finishVoiceTask(IVoiceInteractionSession session) {
2822 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2823 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2824 final int numStacks = stacks.size();
2825 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2826 final ActivityStack stack = stacks.get(stackNdx);
2827 stack.finishVoiceTask(session);
2828 }
2829 }
2830 }
2831
Craig Mautner299f9602015-01-26 09:47:33 -08002832 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002833 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2834 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002835 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002836 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2837 // Caller wants the home activity moved with it. To accomplish this,
2838 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002839 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002840 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07002841 if (task.stack == null) {
2842 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2843 + task + " to front. Stack is null");
2844 return;
2845 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002846
2847 int stackId = task.stack.mStackId;
2848 if (task.mResizeable && options != null) {
2849 ActivityOptions opts = new ActivityOptions(options);
2850 if (opts.hasBounds()) {
2851 Rect bounds = opts.getBounds();
2852 task.updateOverrideConfiguration(bounds);
Chong Zhang87b21722015-09-21 15:39:51 -07002853 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig,
2854 false /*relayout*/, false /*forced*/);
Chong Zhang0fa656b2015-08-31 15:17:21 -07002855 stackId = task.getLaunchStackId();
2856 }
2857 }
2858
2859 if (stackId != task.stack.mStackId) {
2860 moveTaskToStackUncheckedLocked(task, stackId, ON_TOP, FORCE_FOCUS, reason);
2861 } else {
2862 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002863 task.getTopActivity() == null ? null : task.getTopActivity().appTimeTracker,
2864 reason);
Chong Zhang0fa656b2015-08-31 15:17:21 -07002865 }
2866
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002867 if (DEBUG_STACK) Slog.d(TAG_STACK,
2868 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002869 }
2870
Craig Mautner967212c2013-04-13 21:10:58 -07002871 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07002872 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002873 }
2874
2875 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002876 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2877 if (activityContainer != null) {
2878 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002879 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002880 if (!createStaticStackIfNeeded
2881 || (stackId < FIRST_STATIC_STACK_ID || stackId > LAST_STATIC_STACK_ID)) {
2882 return null;
2883 }
2884 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002885 }
2886
Craig Mautner967212c2013-04-13 21:10:58 -07002887 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002888 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002889 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2890 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002891 }
2892 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002893 }
2894
Craig Mautner4a1cb222013-12-04 16:14:06 -08002895 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002896 ActivityRecord homeActivity = getHomeActivity();
2897 if (homeActivity != null) {
2898 return homeActivity.appToken;
2899 }
2900 return null;
2901 }
2902
2903 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002904 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002905 }
2906
2907 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002908 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2909 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2910 final TaskRecord task = tasks.get(taskNdx);
2911 if (task.isHomeTask()) {
2912 final ArrayList<ActivityRecord> activities = task.mActivities;
2913 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2914 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002915 if (r.isHomeActivity()
2916 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002917 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002918 }
2919 }
2920 }
2921 }
2922 return null;
2923 }
2924
Todd Kennedyca4d8422015-01-15 15:19:22 -08002925 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002926 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002927 ActivityContainer activityContainer =
2928 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002929 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002930 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2931 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002932 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002933 return activityContainer;
2934 }
2935
Craig Mautner34b73df2014-01-12 21:11:08 -08002936 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002937 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2938 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2939 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002940 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2941 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002942 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002943 }
2944 }
2945
Craig Mautner95da1082014-02-24 17:54:35 -08002946 void deleteActivityContainer(IActivityContainer container) {
2947 ActivityContainer activityContainer = (ActivityContainer)container;
2948 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002949 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2950 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08002951 final int stackId = activityContainer.mStackId;
2952 mActivityContainers.remove(stackId);
2953 mWindowManager.removeStack(stackId);
2954 }
2955 }
2956
Wale Ogunwale60454db2015-01-23 16:05:07 -08002957 void resizeStackLocked(int stackId, Rect bounds) {
2958 final ActivityStack stack = getStack(stackId);
2959 if (stack == null) {
2960 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2961 return;
2962 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002963
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002964 ActivityRecord r = stack.topRunningActivityLocked(null);
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002965
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002966 mTmpBounds.clear();
2967 mTmpConfigs.clear();
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07002968 ArrayList<TaskRecord> tasks = stack.getAllTasks();
2969 for (int i = tasks.size() - 1; i >= 0; i--) {
2970 TaskRecord task = tasks.get(i);
2971 if (task.mResizeable) {
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07002972 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07002973 // For freeform stack we don't adjust the size of the tasks to match that
2974 // of the stack, but we do try to make sure the tasks are still contained
2975 // with the bounds of the stack.
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07002976 tempRect2.set(task.mBounds);
2977 fitWithinBounds(tempRect2, bounds);
2978 task.updateOverrideConfiguration(tempRect2);
2979 } else {
2980 task.updateOverrideConfiguration(bounds);
2981 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002982 }
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07002983
2984 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
2985 mTmpBounds.put(task.taskId, task.mBounds);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002986 }
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07002987 stack.mFullscreen = mWindowManager.resizeStack(stackId, bounds, mTmpConfigs, mTmpBounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002988 if (stack.mStackId == DOCKED_STACK_ID) {
2989 // Dock stack funness...Yay!
2990 if (stack.mFullscreen) {
2991 // The dock stack went fullscreen which is kinda like dismissing it.
2992 // In this case we make all other static stacks fullscreen and move all
2993 // docked stack tasks to the fullscreen stack.
2994 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2995 if (i != DOCKED_STACK_ID) {
2996 resizeStackLocked(i, null);
2997 }
2998 }
2999
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003000 final int count = tasks.size();
3001 for (int i = 0; i < count; i++) {
3002 moveTaskToStackLocked(tasks.get(i).taskId,
3003 FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP, FORCE_FOCUS);
3004 }
3005
3006 // stack shouldn't contain anymore activities, so nothing to resume.
3007 r = null;
3008 } else {
3009 // Docked stacks occupy a dedicated region on screen so the size of all other
3010 // static stacks need to be adjusted so they don't overlap with the docked stack.
3011 final int leftChange = stack.mBounds.left - bounds.left;
3012 final int rightChange = stack.mBounds.right - bounds.right;
3013 final int topChange = stack.mBounds.top - bounds.top;
3014 final int bottomChange = stack.mBounds.bottom - bounds.bottom;
3015
3016 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
3017 if (i != DOCKED_STACK_ID) {
3018 ActivityStack otherStack = getStack(i);
3019 if (otherStack != null) {
3020 tempRect.set(otherStack.mBounds);
3021 // We adjust the opposing sides of the other stacks to
3022 // the side in the dock stack that changed.
3023 tempRect.left -= rightChange;
3024 tempRect.right -= leftChange;
3025 tempRect.top -= bottomChange;
3026 tempRect.bottom -= topChange;
3027 resizeStackLocked(i, tempRect);
3028 }
3029 }
3030 }
3031
3032 }
3033 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003034 stack.setBounds(bounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003035
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003036 if (r != null) {
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003037 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0, false);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003038 // And we need to make sure at this point that all other activities
3039 // are made visible with the correct configuration.
3040 ensureActivitiesVisibleLocked(r, 0);
3041 if (!updated) {
3042 resumeTopActivitiesLocked(stack, null, null);
3043 }
3044 }
3045 }
3046
Chong Zhang87b21722015-09-21 15:39:51 -07003047 void resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003048 if (!task.mResizeable) {
3049 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
3050 return;
3051 }
3052
Chong Zhang87b21722015-09-21 15:39:51 -07003053 // If this is a forced resize, let it go through even if the bounds is not changing,
3054 // as we might need a relayout due to surface size change (to/from fullscreen).
3055 final boolean forced = (resizeMode == RESIZE_MODE_FORCED);
3056 if (task.mBounds != null && task.mBounds.equals(bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003057 // Nothing to do here...
3058 return;
3059 }
3060
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003061 if (!mWindowManager.isValidTaskId(task.taskId)) {
3062 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07003063 // All we can do for now is update the bounds so it can be used when the task is
3064 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07003065 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003066 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
3067 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07003068 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003069 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003070 return;
3071 }
3072
Wale Ogunwale040b4702015-08-06 18:10:50 -07003073 // The stack of a task is determined by its size (fullscreen vs non-fullscreen).
3074 // Place the task in the right stack if it isn't there already based on the requested
3075 // bounds.
3076 int stackId = task.stack.mStackId;
3077 if (bounds == null && stackId != FULLSCREEN_WORKSPACE_STACK_ID) {
3078 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003079 } else if (bounds != null
3080 && stackId != FREEFORM_WORKSPACE_STACK_ID && stackId != DOCKED_STACK_ID) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07003081 stackId = FREEFORM_WORKSPACE_STACK_ID;
3082 }
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003083 final boolean changedStacks = stackId != task.stack.mStackId;
3084 if (changedStacks) {
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003085 moveTaskToStackUncheckedLocked(task, stackId, ON_TOP, !FORCE_FOCUS, "resizeTask");
Wale Ogunwale040b4702015-08-06 18:10:50 -07003086 }
3087
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07003088 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003089 // This variable holds information whether the configuration didn't change in a signficant
3090 // way and the activity was kept the way it was. If it's false, it means the activity had
3091 // to be relaunched due to configuration change.
3092 boolean kept = true;
3093 if (overrideConfig != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003094 ActivityRecord r = task.topRunningActivityLocked(null);
Wale Ogunwale60454db2015-01-23 16:05:07 -08003095 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003096 final ActivityStack stack = task.stack;
Chong Zhang87b21722015-09-21 15:39:51 -07003097 final boolean resizedByUser = resizeMode == RESIZE_MODE_USER;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003098 final boolean preserveWindow = resizedByUser && !changedStacks;
3099 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003100 // All other activities must be made visible with their correct configuration.
Wale Ogunwale60454db2015-01-23 16:05:07 -08003101 ensureActivitiesVisibleLocked(r, 0);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003102 if (!kept) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08003103 resumeTopActivitiesLocked(stack, null, null);
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003104 if (changedStacks && stackId == FULLSCREEN_WORKSPACE_STACK_ID) {
3105 // We are about to relaunch the activity because its configuration changed
3106 // due to being maximized, i.e. size change. The activity will first
3107 // remove the old window and then add a new one. This call will tell window
3108 // manager about this, so it can preserve the old window until the new
3109 // one is drawn. This prevents having a gap between the removal and
3110 // addition, in which no window is visible. We also want the entrace of the
3111 // new window to be properly animated.
3112 mWindowManager.setReplacingWindow(r.appToken, true /* animate */);
3113 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08003114 }
3115 }
3116 }
Chong Zhang87b21722015-09-21 15:39:51 -07003117 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig, kept, forced);
Wale Ogunwale60454db2015-01-23 16:05:07 -08003118 }
3119
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003120 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003121 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3122 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08003123 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003124 }
3125
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003126 ActivityContainer activityContainer = new ActivityContainer(stackId);
3127 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003128 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08003129 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003130 }
3131
3132 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07003133 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003134 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
3135 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07003136 break;
3137 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003138 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003139 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003140 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003141 }
3142
Chong Zhang5dcb2752015-08-18 13:50:26 -07003143 /**
3144 * Restores a recent task to a stack
3145 * @param task The recent task to be restored.
3146 * @param stackId The stack to restore the task to (default launch stack will be used
3147 * if stackId is invalid).
3148 * @return true if the task has been restored successfully.
3149 */
3150 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
3151 if (stackId == INVALID_STACK_ID) {
Chong Zhang0fa656b2015-08-31 15:17:21 -07003152 stackId = mLeanbackOnlyDevice ? mHomeStack.mStackId : task.getLaunchStackId();
Chong Zhang5dcb2752015-08-18 13:50:26 -07003153 }
Wale Ogunwale706ed792015-08-02 10:29:44 -07003154 if (task.stack != null) {
3155 // Task has already been restored once. See if we need to do anything more
3156 if (task.stack.mStackId == stackId) {
3157 // Nothing else to do since it is already restored in the right stack.
3158 return true;
3159 }
3160 // Remove current stack association, so we can re-associate the task with the
3161 // right stack below.
Wale Ogunwale040b4702015-08-06 18:10:50 -07003162 task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003163 }
3164
3165 ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07003166 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003167
3168 if (stack == null) {
3169 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003170 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3171 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003172 return false;
3173 }
3174
3175 stack.addTask(task, false, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003176 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3177 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003178 final ArrayList<ActivityRecord> activities = task.mActivities;
3179 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003180 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003181 }
3182 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07003183 }
3184
Wale Ogunwale040b4702015-08-06 18:10:50 -07003185 /**
3186 * Moves the specified task record to the input stack id.
3187 * WARNING: This method performs an unchecked/raw move of the task and
3188 * can leave the system in an unstable state if used incorrectly.
3189 * Use {@link #moveTaskToStackLocked} to perform safe task movement
3190 * to a stack.
3191 * @param task Task to move.
3192 * @param stackId Id of stack to move task to.
3193 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07003194 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07003195 * @param reason Reason the task is been moved.
3196 * @return The stack the task was moved to.
3197 */
3198 private ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07003199 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
3200 final ActivityRecord r = task.getTopActivity();
3201 final boolean wasFocused = isFrontStack(task.stack) && (topRunningActivityLocked() == r);
3202 final boolean wasResumed = wasFocused && (task.stack.mResumedActivity == r);
3203
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003204 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwale040b4702015-08-06 18:10:50 -07003205 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
3206 if (task.stack != null) {
3207 task.stack.removeTask(task, reason, MOVING);
3208 }
3209 stack.addTask(task, toTop, MOVING);
Chong Zhang02898352015-08-21 17:27:14 -07003210
3211 // If the task had focus before (or we're requested to move focus),
3212 // move focus to the new stack.
3213 if (forceFocus || wasFocused) {
3214 // If the task owns the last resumed activity, transfer that together,
3215 // so that we don't resume the same activity again in the new stack.
3216 // Apps may depend on onResume()/onPause() being called in pairs.
3217 if (wasResumed) {
3218 stack.mResumedActivity = r;
3219 }
3220 // move the stack in which we are placing the task to the front.
3221 stack.moveToFront(reason);
3222 }
3223
Wale Ogunwale040b4702015-08-06 18:10:50 -07003224 return stack;
3225 }
3226
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003227 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003228 final TaskRecord task = anyTaskForIdLocked(taskId);
3229 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08003230 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003231 return;
3232 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003233 final String reason = "moveTaskToStack";
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003234 final ActivityStack stack =
Chong Zhang02898352015-08-21 17:27:14 -07003235 moveTaskToStackUncheckedLocked(task, stackId, toTop, forceFocus, reason);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003236
3237 // Make sure the task has the appropriate bounds/size for the stack it is in.
3238 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Chong Zhang87b21722015-09-21 15:39:51 -07003239 resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003240 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID
3241 && task.mBounds == null && task.mLastNonFullscreenBounds != null) {
Chong Zhang87b21722015-09-21 15:39:51 -07003242 resizeTaskLocked(task, task.mLastNonFullscreenBounds, RESIZE_MODE_SYSTEM);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003243 } else if (stackId == DOCKED_STACK_ID) {
Chong Zhang87b21722015-09-21 15:39:51 -07003244 resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003245 }
3246
Stefan Kuhne54714cd2015-05-12 13:42:18 -07003247 // The task might have already been running and its visibility needs to be synchronized with
3248 // the visibility of the stack / windows.
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003249 ensureActivitiesVisibleLocked(null, 0);
Craig Mautner05d29032013-05-03 13:40:13 -07003250 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003251 }
3252
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003253 void positionTaskInStackLocked(int taskId, int stackId, int position) {
3254 final TaskRecord task = anyTaskForIdLocked(taskId);
3255 if (task == null) {
3256 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
3257 return;
3258 }
3259 ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07003260 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003261 mWindowManager.positionTaskInStack(taskId, stackId, position);
3262 final boolean stackChanged = task.stack != null && task.stack != stack;
3263 if (stackChanged) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07003264 task.stack.removeTask(task, "moveTaskToStack", MOVING);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003265 }
3266 stack.positionTask(task, position, stackChanged);
3267 // The task might have already been running and its visibility needs to be synchronized with
3268 // the visibility of the stack / windows.
3269 stack.ensureActivitiesVisibleLocked(null, 0);
3270 resumeTopActivitiesLocked();
3271 }
3272
Craig Mautnerac6f8432013-07-17 13:24:59 -07003273 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003274 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003275 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3276 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003277 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3278 final ActivityStack stack = stacks.get(stackNdx);
3279 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003280 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003281 continue;
3282 }
3283 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003284 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
3285 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003286 continue;
3287 }
3288 final ActivityRecord ar = stack.findTaskLocked(r);
3289 if (ar != null) {
3290 return ar;
3291 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003292 }
3293 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003294 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07003295 return null;
3296 }
3297
3298 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003299 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3300 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003301 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3302 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
3303 if (ar != null) {
3304 return ar;
3305 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003306 }
3307 }
3308 return null;
3309 }
3310
Craig Mautner8d341ef2013-03-26 09:03:27 -07003311 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003312 scheduleSleepTimeout();
3313 if (!mGoingToSleep.isHeld()) {
3314 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003315 if (mLaunchingActivity.isHeld()) {
3316 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3317 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003318 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003319 mLaunchingActivity.release();
3320 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003321 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003322 }
Amith Yamasanice15e152013-09-19 12:30:32 -07003323 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003324 }
3325
3326 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003327 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003328
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003329 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003330 final long endTime = System.currentTimeMillis() + timeout;
3331 while (true) {
3332 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003333 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3334 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003335 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3336 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
3337 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003338 }
3339 if (cantShutdown) {
3340 long timeRemaining = endTime - System.currentTimeMillis();
3341 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003342 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003343 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003344 } catch (InterruptedException e) {
3345 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003346 } else {
3347 Slog.w(TAG, "Activity manager shutdown timed out");
3348 timedout = true;
3349 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003350 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003351 } else {
3352 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003353 }
3354 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003355
3356 // Force checkReadyForSleep to complete.
3357 mSleepTimeout = true;
3358 checkReadyForSleepLocked();
3359
Craig Mautner8d341ef2013-03-26 09:03:27 -07003360 return timedout;
3361 }
3362
3363 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003364 removeSleepTimeouts();
3365 if (mGoingToSleep.isHeld()) {
3366 mGoingToSleep.release();
3367 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003368 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3369 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003370 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3371 final ActivityStack stack = stacks.get(stackNdx);
3372 stack.awakeFromSleepingLocked();
3373 if (isFrontStack(stack)) {
3374 resumeTopActivitiesLocked();
3375 }
Craig Mautner5314a402013-09-26 12:40:16 -07003376 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003377 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003378 mGoingToSleepActivities.clear();
3379 }
3380
3381 void activitySleptLocked(ActivityRecord r) {
3382 mGoingToSleepActivities.remove(r);
3383 checkReadyForSleepLocked();
3384 }
3385
3386 void checkReadyForSleepLocked() {
3387 if (!mService.isSleepingOrShuttingDown()) {
3388 // Do not care.
3389 return;
3390 }
3391
3392 if (!mSleepTimeout) {
3393 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003394 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3395 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003396 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3397 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3398 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003399 }
3400
3401 if (mStoppingActivities.size() > 0) {
3402 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003403 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003404 + mStoppingActivities.size() + " activities");
3405 scheduleIdleLocked();
3406 dontSleep = true;
3407 }
3408
3409 if (mGoingToSleepActivities.size() > 0) {
3410 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003411 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003412 + mGoingToSleepActivities.size() + " activities");
3413 dontSleep = true;
3414 }
3415
3416 if (dontSleep) {
3417 return;
3418 }
3419 }
3420
Craig Mautnere0a38842013-12-16 16:14:02 -08003421 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3422 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003423 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3424 stacks.get(stackNdx).goToSleep();
3425 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003426 }
3427
3428 removeSleepTimeouts();
3429
3430 if (mGoingToSleep.isHeld()) {
3431 mGoingToSleep.release();
3432 }
3433 if (mService.mShuttingDown) {
3434 mService.notifyAll();
3435 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003436 }
3437
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003438 boolean reportResumedActivityLocked(ActivityRecord r) {
3439 final ActivityStack stack = r.task.stack;
3440 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003441 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003442 }
3443 if (allResumedActivitiesComplete()) {
3444 ensureActivitiesVisibleLocked(null, 0);
3445 mWindowManager.executeAppTransition();
3446 return true;
3447 }
3448 return false;
3449 }
3450
Craig Mautner8d341ef2013-03-26 09:03:27 -07003451 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003452 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3453 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003454 int stackNdx = stacks.size() - 1;
3455 while (stackNdx >= 0) {
3456 stacks.get(stackNdx).handleAppCrashLocked(app);
3457 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003458 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003459 }
3460 }
3461
Jose Lima4b6c6692014-08-12 17:41:12 -07003462 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003463 final ActivityStack stack = r.task.stack;
3464 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003465 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3466 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003467 return false;
3468 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003469 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003470 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3471 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003472
3473 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003474 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003475 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003476 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003477 return true;
3478 }
3479
3480 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003481 if (visible && top.fullscreen) {
3482 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003483 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3484 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3485 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3486 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003487 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003488 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3489 // Only the activity set as currently visible behind should actively reset its
3490 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003491 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3492 "requestVisibleBehind: returning visible=" + visible
3493 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3494 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003495 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003496 }
3497
Jose Lima4b6c6692014-08-12 17:41:12 -07003498 stack.setVisibleBehindActivity(visible ? r : null);
3499 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003500 // Make the activity immediately above r opaque.
3501 final ActivityRecord next = stack.findNextTranslucentActivity(r);
3502 if (next != null) {
3503 mService.convertFromTranslucent(next.appToken);
3504 }
3505 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003506 if (top.app != null && top.app.thread != null) {
3507 // Notify the top app of the change.
3508 try {
3509 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3510 } catch (RemoteException e) {
3511 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003512 }
3513 return true;
3514 }
3515
Craig Mautnerbb742462014-07-07 15:28:55 -07003516 // Called when WindowManager has finished animating the launchingBehind activity to the back.
3517 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
3518 r.mLaunchTaskBehind = false;
3519 final TaskRecord task = r.task;
3520 task.setLastThumbnail(task.stack.screenshotActivities(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003521 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08003522 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07003523 mWindowManager.setAppVisibility(r.appToken, false);
3524 }
3525
3526 void scheduleLaunchTaskBehindComplete(IBinder token) {
3527 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3528 }
3529
Craig Mautnerde4ef022013-04-07 19:01:33 -07003530 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
Craig Mautner580ea812013-04-25 12:58:38 -07003531 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08003532 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3533 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003534 final int topStackNdx = stacks.size() - 1;
3535 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3536 final ActivityStack stack = stacks.get(stackNdx);
Jose Lima729cb232014-05-05 15:59:40 -07003537 stack.ensureActivitiesVisibleLocked(starting, configChanges);
Craig Mautner580ea812013-04-25 12:58:38 -07003538 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003539 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003540 }
3541
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003542 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3543 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3544 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3545 final int topStackNdx = stacks.size() - 1;
3546 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3547 final ActivityStack stack = stacks.get(stackNdx);
3548 stack.clearOtherAppTimeTrackers(except);
3549 }
3550 }
3551 }
3552
Craig Mautner8d341ef2013-03-26 09:03:27 -07003553 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003554 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3555 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003556 final int numStacks = stacks.size();
3557 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3558 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003559 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003560 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003561 }
3562 }
3563
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003564 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3565 // Examine all activities currently running in the process.
3566 TaskRecord firstTask = null;
3567 // Tasks is non-null only if two or more tasks are found.
3568 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003569 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3570 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003571 ActivityRecord r = app.activities.get(i);
3572 // First, if we find an activity that is in the process of being destroyed,
3573 // then we just aren't going to do anything for now; we want things to settle
3574 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003575 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003576 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003577 return;
3578 }
3579 // Don't consider any activies that are currently not in a state where they
3580 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003581 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3582 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003583 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003584 continue;
3585 }
3586 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003587 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003588 + " from " + r);
3589 if (firstTask == null) {
3590 firstTask = r.task;
3591 } else if (firstTask != r.task) {
3592 if (tasks == null) {
3593 tasks = new ArraySet<>();
3594 tasks.add(firstTask);
3595 }
3596 tasks.add(r.task);
3597 }
3598 }
3599 }
3600 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003601 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003602 return;
3603 }
3604 // If we have activities in multiple tasks that are in a position to be destroyed,
3605 // let's iterate through the tasks and release the oldest one.
3606 final int numDisplays = mActivityDisplays.size();
3607 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3608 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3609 // Step through all stacks starting from behind, to hit the oldest things first.
3610 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3611 final ActivityStack stack = stacks.get(stackNdx);
3612 // Try to release activities in this stack; if we manage to, we are done.
3613 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3614 return;
3615 }
3616 }
3617 }
3618 }
3619
Amith Yamasani37a40c22015-06-17 13:25:42 -07003620 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003621 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003622 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003623 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003624
Craig Mautner858d8a62013-04-23 17:08:34 -07003625 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003626 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3627 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003628 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003629 final ActivityStack stack = stacks.get(stackNdx);
3630 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003631 TaskRecord task = stack.topTask();
3632 if (task != null) {
3633 mWindowManager.moveTaskToTop(task.taskId);
3634 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003635 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003636 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003637
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003638 ActivityStack stack = getStack(restoreStackId);
3639 if (stack == null) {
3640 stack = mHomeStack;
3641 }
3642 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003643 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003644 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003645 } else {
3646 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003647 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003648 }
Craig Mautner93529a42013-10-04 15:03:13 -07003649 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003650 }
3651
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003652 /**
3653 * Add background users to send boot completed events to.
3654 * @param userId The user being started in the background
3655 * @param uss The state object for the user.
3656 */
Amith Yamasani37a40c22015-06-17 13:25:42 -07003657 public void startBackgroundUserLocked(int userId, UserState uss) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003658 mStartingBackgroundUsers.add(uss);
3659 }
3660
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003661 /** Checks whether the userid is a profile of the current user. */
3662 boolean isCurrentProfileLocked(int userId) {
3663 if (userId == mCurrentUser) return true;
3664 for (int i = 0; i < mService.mCurrentProfileIds.length; i++) {
3665 if (mService.mCurrentProfileIds[i] == userId) return true;
3666 }
3667 return false;
3668 }
3669
Chong Zhang45c25ce2015-08-10 22:18:26 -07003670 /** Checks whether the activity should be shown for current user. */
3671 boolean okToShowLocked(ActivityRecord r) {
3672 return r != null && (isCurrentProfileLocked(r.userId)
3673 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
3674 }
3675
Craig Mautnerde4ef022013-04-07 19:01:33 -07003676 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003677 ArrayList<ActivityRecord> stops = null;
3678
3679 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003680 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3681 ActivityRecord s = mStoppingActivities.get(activityNdx);
3682 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003683 if (DEBUG_ALL) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003684 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3685 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003686 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003687 if (s.finishing) {
3688 // If this activity is finishing, it is sitting on top of
3689 // everyone else but we now know it is no longer needed...
3690 // so get rid of it. Otherwise, we need to go through the
3691 // normal flow and hide it once we determine that it is
3692 // hidden by the activities in front of it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003693 if (DEBUG_ALL) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003694 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003695 }
3696 }
Craig Mautner8c14c152015-01-15 17:32:07 -08003697 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003698 if (DEBUG_ALL) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003699 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003700 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003701 }
3702 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003703 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003704 }
3705 }
3706
3707 return stops;
3708 }
3709
Craig Mautnercf910b02013-04-23 11:23:27 -07003710 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003711 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3712 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3713 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3714 final ActivityStack stack = stacks.get(stackNdx);
3715 final ActivityRecord r = stack.topRunningActivityLocked(null);
3716 final ActivityState state = r == null ? DESTROYED : r.state;
3717 if (isFrontStack(stack)) {
3718 if (r == null) Slog.e(TAG,
3719 "validateTop...: null top activity, stack=" + stack);
3720 else {
3721 final ActivityRecord pausing = stack.mPausingActivity;
3722 if (pausing != null && pausing == r) Slog.e(TAG,
3723 "validateTop...: top stack has pausing activity r=" + r
3724 + " state=" + state);
3725 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3726 "validateTop...: activity in front not resumed r=" + r
3727 + " state=" + state);
3728 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003729 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003730 final ActivityRecord resumed = stack.mResumedActivity;
3731 if (resumed != null && resumed == r) Slog.e(TAG,
3732 "validateTop...: back stack has resumed activity r=" + r
3733 + " state=" + state);
3734 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3735 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003736 }
3737 }
3738 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003739 }
3740
Craig Mautnere0570202015-05-13 13:06:11 -07003741 private String lockTaskModeToString() {
3742 switch (mLockTaskModeState) {
3743 case LOCK_TASK_MODE_LOCKED:
3744 return "LOCKED";
3745 case LOCK_TASK_MODE_PINNED:
3746 return "PINNED";
3747 case LOCK_TASK_MODE_NONE:
3748 return "NONE";
3749 default: return "unknown=" + mLockTaskModeState;
3750 }
3751 }
3752
Craig Mautner27084302013-03-25 08:05:25 -07003753 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003754 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003755 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003756 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
3757 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
3758 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003759 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003760 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
3761 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3762 if (packages.size() > 0) {
3763 pw.println(" mLockTaskPackages (userId:packages)=");
3764 for (int i = 0; i < packages.size(); ++i) {
3765 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3766 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3767 }
3768 }
3769 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003770 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003771
Craig Mautner20e72272013-04-01 13:45:53 -07003772 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003773 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003774 }
3775
Dianne Hackborn390517b2013-05-30 15:03:32 -07003776 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3777 boolean needSep, String prefix) {
3778 if (activity != null) {
3779 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3780 if (needSep) {
3781 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003782 }
3783 pw.print(prefix);
3784 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003785 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003786 }
3787 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003788 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003789 }
3790
Craig Mautner8d341ef2013-03-26 09:03:27 -07003791 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3792 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003793 boolean printed = false;
3794 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003795 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3796 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003797 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003798 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003799 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003800 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003801 final ActivityStack stack = stacks.get(stackNdx);
3802 StringBuilder stackHeader = new StringBuilder(128);
3803 stackHeader.append(" Stack #");
3804 stackHeader.append(stack.mStackId);
3805 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003806 stackHeader.append("\n");
3807 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3808 stackHeader.append("\n");
3809 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003810 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3811 needSep, stackHeader.toString());
3812 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3813 !dumpAll, false, dumpPackage, true,
3814 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003815
Craig Mautner4a1cb222013-12-04 16:14:06 -08003816 needSep = printed;
3817 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3818 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003819 if (pr) {
3820 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003821 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003822 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003823 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3824 " mResumedActivity: ");
3825 if (pr) {
3826 printed = true;
3827 needSep = false;
3828 }
3829 if (dumpAll) {
3830 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3831 " mLastPausedActivity: ");
3832 if (pr) {
3833 printed = true;
3834 needSep = true;
3835 }
3836 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3837 needSep, " mLastNoHistoryActivity: ");
3838 }
3839 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003840 }
3841 }
3842
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003843 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3844 false, dumpPackage, true, " Activities waiting to finish:", null);
3845 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3846 false, dumpPackage, true, " Activities waiting to stop:", null);
3847 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3848 false, dumpPackage, true, " Activities waiting for another to become visible:",
3849 null);
3850 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3851 false, dumpPackage, true, " Activities waiting to sleep:", null);
3852 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3853 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003854
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003855 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003856 }
3857
Dianne Hackborn390517b2013-05-30 15:03:32 -07003858 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003859 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003860 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003861 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003862 String innerPrefix = null;
3863 String[] args = null;
3864 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003865 for (int i=list.size()-1; i>=0; i--) {
3866 final ActivityRecord r = list.get(i);
3867 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3868 continue;
3869 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003870 if (innerPrefix == null) {
3871 innerPrefix = prefix + " ";
3872 args = new String[0];
3873 }
3874 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003875 final boolean full = !brief && (complete || !r.isInHistory());
3876 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003877 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003878 needNL = false;
3879 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003880 if (header1 != null) {
3881 pw.println(header1);
3882 header1 = null;
3883 }
3884 if (header2 != null) {
3885 pw.println(header2);
3886 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003887 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003888 if (lastTask != r.task) {
3889 lastTask = r.task;
3890 pw.print(prefix);
3891 pw.print(full ? "* " : " ");
3892 pw.println(lastTask);
3893 if (full) {
3894 lastTask.dump(pw, prefix + " ");
3895 } else if (complete) {
3896 // Complete + brief == give a summary. Isn't that obvious?!?
3897 if (lastTask.intent != null) {
3898 pw.print(prefix); pw.print(" ");
3899 pw.println(lastTask.intent.toInsecureStringWithClip());
3900 }
3901 }
3902 }
3903 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3904 pw.print(" #"); pw.print(i); pw.print(": ");
3905 pw.println(r);
3906 if (full) {
3907 r.dump(pw, innerPrefix);
3908 } else if (complete) {
3909 // Complete + brief == give a summary. Isn't that obvious?!?
3910 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3911 if (r.app != null) {
3912 pw.print(innerPrefix); pw.println(r.app);
3913 }
3914 }
3915 if (client && r.app != null && r.app.thread != null) {
3916 // flush anything that is already in the PrintWriter since the thread is going
3917 // to write to the file descriptor directly
3918 pw.flush();
3919 try {
3920 TransferPipe tp = new TransferPipe();
3921 try {
3922 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3923 r.appToken, innerPrefix, args);
3924 // Short timeout, since blocking here can
3925 // deadlock with the application.
3926 tp.go(fd, 2000);
3927 } finally {
3928 tp.kill();
3929 }
3930 } catch (IOException e) {
3931 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3932 } catch (RemoteException e) {
3933 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3934 }
3935 needNL = true;
3936 }
3937 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003938 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003939 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003940
Craig Mautnerf3333272013-04-22 10:55:53 -07003941 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003942 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3943 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003944 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3945 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003946 }
3947
3948 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003949 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003950 }
3951
3952 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003953 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3954 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003955 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3956 }
3957
Craig Mautner05d29032013-05-03 13:40:13 -07003958 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003959 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3960 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3961 }
Craig Mautner05d29032013-05-03 13:40:13 -07003962 }
3963
Craig Mautner0eea92c2013-05-16 13:35:39 -07003964 void removeSleepTimeouts() {
3965 mSleepTimeout = false;
3966 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3967 }
3968
3969 final void scheduleSleepTimeout() {
3970 removeSleepTimeouts();
3971 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3972 }
3973
Craig Mautner4a1cb222013-12-04 16:14:06 -08003974 @Override
3975 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003976 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003977 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3978 }
3979
3980 @Override
3981 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003982 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003983 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3984 }
3985
3986 @Override
3987 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003988 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003989 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3990 }
3991
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003992 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003993 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003994 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003995 newDisplay = mActivityDisplays.get(displayId) == null;
3996 if (newDisplay) {
3997 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003998 if (activityDisplay.mDisplay == null) {
3999 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
4000 return;
4001 }
Craig Mautner4504de52013-12-20 09:06:56 -08004002 mActivityDisplays.put(displayId, activityDisplay);
4003 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004004 }
Craig Mautner4504de52013-12-20 09:06:56 -08004005 if (newDisplay) {
4006 mWindowManager.onDisplayAdded(displayId);
4007 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004008 }
4009
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004010 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004011 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004012 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4013 if (activityDisplay != null) {
4014 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004015 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004016 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004017 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004018 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004019 }
4020 }
4021 mWindowManager.onDisplayRemoved(displayId);
4022 }
4023
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004024 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004025 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004026 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4027 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004028 // TODO: Update the bounds.
4029 }
4030 }
4031 mWindowManager.onDisplayChanged(displayId);
4032 }
4033
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004034 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004035 StackInfo info = new StackInfo();
4036 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
4037 info.displayId = Display.DEFAULT_DISPLAY;
4038 info.stackId = stack.mStackId;
4039
4040 ArrayList<TaskRecord> tasks = stack.getAllTasks();
4041 final int numTasks = tasks.size();
4042 int[] taskIds = new int[numTasks];
4043 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004044 Rect[] taskBounds = new Rect[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08004045 for (int i = 0; i < numTasks; ++i) {
4046 final TaskRecord task = tasks.get(i);
4047 taskIds[i] = task.taskId;
4048 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
4049 : task.realActivity != null ? task.realActivity.flattenToString()
4050 : task.getTopActivity() != null ? task.getTopActivity().packageName
4051 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004052 taskBounds[i] = new Rect();
4053 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004054 }
4055 info.taskIds = taskIds;
4056 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07004057 info.taskBounds = taskBounds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004058 return info;
4059 }
4060
4061 StackInfo getStackInfoLocked(int stackId) {
4062 ActivityStack stack = getStack(stackId);
4063 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004064 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004065 }
4066 return null;
4067 }
4068
4069 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004070 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08004071 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
4072 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004073 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004074 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08004075 }
4076 }
4077 return list;
4078 }
4079
Craig Mautner15df08a2015-04-01 12:17:18 -07004080 TaskRecord getLockedTaskLocked() {
4081 final int top = mLockTaskModeTasks.size() - 1;
4082 if (top >= 0) {
4083 return mLockTaskModeTasks.get(top);
4084 }
4085 return null;
4086 }
4087
4088 boolean isLockedTask(TaskRecord task) {
4089 return mLockTaskModeTasks.contains(task);
4090 }
4091
4092 boolean isLastLockedTask(TaskRecord task) {
4093 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
4094 }
4095
4096 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07004097 if (!mLockTaskModeTasks.remove(task)) {
4098 return;
4099 }
4100 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
4101 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004102 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07004103 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
4104 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07004105 final Message lockTaskMsg = Message.obtain();
4106 lockTaskMsg.arg1 = task.userId;
4107 lockTaskMsg.what = LOCK_TASK_END_MSG;
4108 mHandler.sendMessage(lockTaskMsg);
4109 }
4110 }
4111
Craig Mautner6cd6cec2015-04-01 00:02:12 -07004112 void showLockTaskToast() {
4113 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04004114 }
4115
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004116 void showLockTaskEscapeMessageLocked(TaskRecord task) {
4117 if (mLockTaskModeTasks.contains(task)) {
4118 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
4119 }
4120 }
4121
Craig Mautner432f64e2015-05-20 14:59:57 -07004122 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
4123 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004124 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07004125 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07004126 final TaskRecord lockedTask = getLockedTaskLocked();
4127 if (lockedTask != null) {
4128 removeLockedTaskLocked(lockedTask);
4129 if (!mLockTaskModeTasks.isEmpty()) {
4130 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07004131 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4132 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07004133 lockedTask.performClearTaskLocked();
4134 resumeTopActivitiesLocked();
4135 return;
4136 }
Christopher Tate3bd90612014-06-18 16:37:52 -07004137 }
Craig Mautnere0570202015-05-13 13:06:11 -07004138 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4139 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08004140 return;
4141 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004142
4143 // Should have already been checked, but do it again.
4144 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07004145 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4146 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08004147 return;
4148 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004149 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07004150 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07004151 return;
4152 }
4153
4154 if (mLockTaskModeTasks.isEmpty()) {
4155 // First locktask.
4156 final Message lockTaskMsg = Message.obtain();
4157 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
4158 lockTaskMsg.arg1 = task.userId;
4159 lockTaskMsg.what = LOCK_TASK_START_MSG;
4160 lockTaskMsg.arg2 = lockTaskModeState;
4161 mHandler.sendMessage(lockTaskMsg);
4162 }
4163 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07004164 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
4165 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07004166 mLockTaskModeTasks.remove(task);
4167 mLockTaskModeTasks.add(task);
4168
4169 if (task.mLockTaskUid == -1) {
4170 task.mLockTaskUid = task.mCallingUid;
4171 }
Craig Mautner432f64e2015-05-20 14:59:57 -07004172
4173 if (andResume) {
4174 findTaskToMoveToFrontLocked(task, 0, null, reason);
4175 resumeTopActivitiesLocked();
4176 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004177 }
4178
4179 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04004180 return isLockTaskModeViolation(task, false);
4181 }
4182
4183 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
4184 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004185 return false;
4186 }
4187 final int lockTaskAuth = task.mLockTaskAuth;
4188 switch (lockTaskAuth) {
4189 case LOCK_TASK_AUTH_DONT_LOCK:
4190 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01004191 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07004192 case LOCK_TASK_AUTH_LAUNCHABLE:
4193 case LOCK_TASK_AUTH_WHITELISTED:
4194 return false;
4195 case LOCK_TASK_AUTH_PINNABLE:
4196 // Pinnable tasks can't be launched on top of locktask tasks.
4197 return !mLockTaskModeTasks.isEmpty();
4198 default:
4199 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
4200 return true;
4201 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004202 }
4203
Craig Mautner15df08a2015-04-01 12:17:18 -07004204 void onLockTaskPackagesUpdatedLocked() {
4205 boolean didSomething = false;
4206 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
4207 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01004208 final boolean wasWhitelisted =
4209 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4210 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07004211 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01004212 final boolean isWhitelisted =
4213 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4214 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
4215 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004216 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07004217 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
4218 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07004219 removeLockedTaskLocked(lockedTask);
4220 lockedTask.performClearTaskLocked();
4221 didSomething = true;
4222 }
4223 }
4224 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4225 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4226 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4227 final ActivityStack stack = stacks.get(stackNdx);
4228 stack.onLockTaskPackagesUpdatedLocked();
4229 }
4230 }
Craig Mautnere0570202015-05-13 13:06:11 -07004231 final ActivityRecord r = topRunningActivityLocked();
4232 final TaskRecord task = r != null ? r.task : null;
4233 if (mLockTaskModeTasks.isEmpty() && task != null
4234 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4235 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004236 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4237 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4238 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4239 false);
4240 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004241 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004242 if (didSomething) {
4243 resumeTopActivitiesLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004244 }
4245 }
4246
Benjamin Franz43261142015-02-11 15:59:44 +00004247 int getLockTaskModeState() {
4248 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004249 }
4250
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004251 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004252
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004253 public ActivityStackSupervisorHandler(Looper looper) {
4254 super(looper);
4255 }
4256
Craig Mautnerf3333272013-04-22 10:55:53 -07004257 void activityIdleInternal(ActivityRecord r) {
4258 synchronized (mService) {
4259 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
4260 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004261 }
4262
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004263 @Override
4264 public void handleMessage(Message msg) {
4265 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004266 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004267 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4268 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004269 if (mService.mDidDexOpt) {
4270 mService.mDidDexOpt = false;
4271 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4272 nmsg.obj = msg.obj;
4273 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4274 return;
4275 }
4276 // We don't at this point know if the activity is fullscreen,
4277 // so we need to be conservative and assume it isn't.
4278 activityIdleInternal((ActivityRecord)msg.obj);
4279 } break;
4280 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004281 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004282 activityIdleInternal((ActivityRecord)msg.obj);
4283 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004284 case RESUME_TOP_ACTIVITY_MSG: {
4285 synchronized (mService) {
4286 resumeTopActivitiesLocked();
4287 }
4288 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004289 case SLEEP_TIMEOUT_MSG: {
4290 synchronized (mService) {
4291 if (mService.isSleepingOrShuttingDown()) {
4292 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4293 mSleepTimeout = true;
4294 checkReadyForSleepLocked();
4295 }
4296 }
4297 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004298 case LAUNCH_TIMEOUT_MSG: {
4299 if (mService.mDidDexOpt) {
4300 mService.mDidDexOpt = false;
4301 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4302 return;
4303 }
4304 synchronized (mService) {
4305 if (mLaunchingActivity.isHeld()) {
4306 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4307 if (VALIDATE_WAKE_LOCK_CALLER
4308 && Binder.getCallingUid() != Process.myUid()) {
4309 throw new IllegalStateException("Calling must be system uid");
4310 }
4311 mLaunchingActivity.release();
4312 }
4313 }
4314 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004315 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004316 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004317 } break;
4318 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004319 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004320 } break;
4321 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004322 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004323 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004324 case CONTAINER_CALLBACK_VISIBILITY: {
4325 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004326 final IActivityContainerCallback callback = container.mCallback;
4327 if (callback != null) {
4328 try {
4329 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4330 } catch (RemoteException e) {
4331 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004332 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004333 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004334 case LOCK_TASK_START_MSG: {
4335 // When lock task starts, we disable the status bars.
4336 try {
Jason Monk62515be2014-05-21 16:06:19 -04004337 if (mLockTaskNotify == null) {
4338 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004339 }
Jason Monk62515be2014-05-21 16:06:19 -04004340 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004341 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004342 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004343 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004344 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004345 flags = StatusBarManager.DISABLE_MASK
4346 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004347 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004348 flags = StatusBarManager.DISABLE_MASK
4349 & (~StatusBarManager.DISABLE_BACK)
4350 & (~StatusBarManager.DISABLE_HOME)
4351 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004352 }
4353 getStatusBarService().disable(flags, mToken,
4354 mService.mContext.getPackageName());
4355 }
4356 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004357 if (getDevicePolicyManager() != null) {
4358 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004359 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004360 }
justinzhang5286d3f2014-05-12 17:06:01 -04004361 } catch (RemoteException ex) {
4362 throw new RuntimeException(ex);
4363 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004364 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004365 case LOCK_TASK_END_MSG: {
4366 // When lock task ends, we enable the status bars.
4367 try {
Jason Monk62515be2014-05-21 16:06:19 -04004368 if (getStatusBarService() != null) {
4369 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4370 mService.mContext.getPackageName());
4371 }
4372 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004373 if (getDevicePolicyManager() != null) {
4374 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4375 msg.arg1);
4376 }
Jason Monk62515be2014-05-21 16:06:19 -04004377 if (mLockTaskNotify == null) {
4378 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4379 }
4380 mLockTaskNotify.show(false);
4381 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004382 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004383 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004384 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004385 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004386 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04004387 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04004388 new LockPatternUtils(mService.mContext)
4389 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004390 }
4391 } catch (SettingNotFoundException e) {
4392 // No setting, don't lock.
4393 }
justinzhang5286d3f2014-05-12 17:06:01 -04004394 } catch (RemoteException ex) {
4395 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004396 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004397 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004398 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004399 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004400 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4401 if (mLockTaskNotify == null) {
4402 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4403 }
4404 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4405 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004406 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4407 final ActivityContainer container = (ActivityContainer) msg.obj;
4408 final IActivityContainerCallback callback = container.mCallback;
4409 if (callback != null) {
4410 try {
4411 callback.onAllActivitiesComplete(container.asBinder());
4412 } catch (RemoteException e) {
4413 }
4414 }
4415 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004416 case LAUNCH_TASK_BEHIND_COMPLETE: {
4417 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004418 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004419 if (r != null) {
4420 handleLaunchTaskBehindCompleteLocked(r);
4421 }
4422 }
4423 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004424 }
4425 }
4426 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004427
Ying Wangb081a592014-04-22 15:20:16 -07004428 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07004429 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07004430 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004431 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004432 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004433 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004434 ActivityRecord mParentActivity = null;
4435 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004436
Craig Mautnere3a00d72014-04-16 08:31:19 -07004437 boolean mVisible = true;
4438
Craig Mautner4a1cb222013-12-04 16:14:06 -08004439 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004440 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004441
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004442 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4443 final static int CONTAINER_STATE_NO_SURFACE = 1;
4444 final static int CONTAINER_STATE_FINISHING = 2;
4445 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4446
4447 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004448 synchronized (mService) {
4449 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004450 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004451 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004452 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004453 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004454 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004455
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004456 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004457 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004458 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08004459 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004460 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004461 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004462 }
4463
4464 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004465 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004466 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004467 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4468 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004469 return;
4470 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004471 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004472 }
4473 }
4474
4475 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004476 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004477 synchronized (mService) {
4478 if (mActivityDisplay != null) {
4479 return mActivityDisplay.mDisplayId;
4480 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004481 }
4482 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004483 }
4484
Jeff Brownca9bc702014-02-11 14:32:56 -08004485 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004486 public int getStackId() {
4487 synchronized (mService) {
4488 return mStackId;
4489 }
4490 }
4491
4492 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004493 public boolean injectEvent(InputEvent event) {
4494 final long origId = Binder.clearCallingIdentity();
4495 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004496 synchronized (mService) {
4497 if (mActivityDisplay != null) {
4498 return mInputManagerInternal.injectInputEvent(event,
4499 mActivityDisplay.mDisplayId,
4500 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4501 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004502 }
4503 return false;
4504 } finally {
4505 Binder.restoreCallingIdentity(origId);
4506 }
4507 }
4508
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004509 @Override
4510 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004511 synchronized (mService) {
4512 if (mContainerState == CONTAINER_STATE_FINISHING) {
4513 return;
4514 }
4515 mContainerState = CONTAINER_STATE_FINISHING;
4516
Craig Mautnerd163e752014-06-13 17:18:47 -07004517 long origId = Binder.clearCallingIdentity();
4518 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004519 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07004520 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004521 } finally {
4522 Binder.restoreCallingIdentity(origId);
4523 }
4524 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004525 }
4526
Craig Mautner60257702014-09-17 15:02:33 -07004527 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004528 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004529 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004530 if (mActivityDisplay != null) {
4531 mActivityDisplay.detachActivitiesLocked(mStack);
4532 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004533 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004534 }
4535 }
4536
4537 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004538 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004539 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnerb9168362015-02-26 20:40:19 -08004540 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004541 Binder.getCallingUid(), mCurrentUser, false,
4542 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004543
Craig Mautnere0a38842013-12-16 16:14:02 -08004544 // TODO: Switch to user app stacks here.
4545 String mimeType = intent.getType();
Craig Mautnerb9168362015-02-26 20:40:19 -08004546 final Uri data = intent.getData();
4547 if (mimeType == null && data != null && "content".equals(data.getScheme())) {
4548 mimeType = mService.getProviderMimeType(data, userId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004549 }
Craig Mautnerb9168362015-02-26 20:40:19 -08004550 checkEmbeddedAllowedInner(userId, intent, mimeType);
4551
4552 intent.addFlags(FORCE_NEW_TASK_FLAGS);
4553 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07004554 0, 0, null, null, null, null, false, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004555 }
4556
4557 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004558 public final int startActivityIntentSender(IIntentSender intentSender)
4559 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004560 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4561
4562 if (!(intentSender instanceof PendingIntentRecord)) {
4563 throw new IllegalArgumentException("Bad PendingIntent object");
4564 }
4565
Craig Mautnerb9168362015-02-26 20:40:19 -08004566 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004567 Binder.getCallingUid(), mCurrentUser, false,
4568 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004569
4570 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4571 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4572 pendingIntent.key.requestResolvedType);
4573
4574 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4575 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4576 }
4577
4578 private void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004579 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004580 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004581 throw new SecurityException(
4582 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4583 }
4584 }
4585
Craig Mautnerdf88d732014-01-27 09:21:32 -08004586 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004587 public IBinder asBinder() {
4588 return this;
4589 }
4590
Craig Mautner4504de52013-12-20 09:06:56 -08004591 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004592 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004593 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004594 }
4595
Craig Mautner4a1cb222013-12-04 16:14:06 -08004596 ActivityStackSupervisor getOuter() {
4597 return ActivityStackSupervisor.this;
4598 }
4599
Craig Mautnerd163e752014-06-13 17:18:47 -07004600 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004601 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004602 }
4603
4604 void getBounds(Point outBounds) {
Craig Mautnerd163e752014-06-13 17:18:47 -07004605 synchronized (mService) {
4606 if (mActivityDisplay != null) {
4607 mActivityDisplay.getBounds(outBounds);
4608 } else {
4609 outBounds.set(0, 0);
4610 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004611 }
4612 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004613
Craig Mautner6985bad2014-04-21 15:22:06 -07004614 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004615 void setVisible(boolean visible) {
4616 if (mVisible != visible) {
4617 mVisible = visible;
4618 if (mCallback != null) {
4619 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4620 0 /* unused */, this).sendToTarget();
4621 }
4622 }
4623 }
4624
Craig Mautner6985bad2014-04-21 15:22:06 -07004625 void setDrawn() {
4626 }
4627
Craig Mautner1b4bf852014-05-26 15:06:32 -07004628 // You can always start a new task on a regular ActivityStack.
4629 boolean isEligibleForNewTasks() {
4630 return true;
4631 }
4632
Craig Mautnerd163e752014-06-13 17:18:47 -07004633 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004634 detachLocked();
4635 deleteActivityContainer(this);
4636 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004637 }
4638
Craig Mautner34b73df2014-01-12 21:11:08 -08004639 @Override
4640 public String toString() {
4641 return mIdString + (mActivityDisplay == null ? "N" : "A");
4642 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004643 }
4644
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004645 private class VirtualActivityContainer extends ActivityContainer {
4646 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004647 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004648
4649 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4650 super(getNextStackId());
4651 mParentActivity = parent;
4652 mCallback = callback;
4653 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004654 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004655 }
4656
4657 @Override
4658 public void setSurface(Surface surface, int width, int height, int density) {
4659 super.setSurface(surface, width, height, density);
4660
4661 synchronized (mService) {
4662 final long origId = Binder.clearCallingIdentity();
4663 try {
4664 setSurfaceLocked(surface, width, height, density);
4665 } finally {
4666 Binder.restoreCallingIdentity(origId);
4667 }
4668 }
4669 }
4670
4671 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4672 if (mContainerState == CONTAINER_STATE_FINISHING) {
4673 return;
4674 }
4675 VirtualActivityDisplay virtualActivityDisplay =
4676 (VirtualActivityDisplay) mActivityDisplay;
4677 if (virtualActivityDisplay == null) {
4678 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004679 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004680 mActivityDisplay = virtualActivityDisplay;
4681 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004682 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004683 }
4684
4685 if (mSurface != null) {
4686 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004687 }
4688
Craig Mautner6985bad2014-04-21 15:22:06 -07004689 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004690 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004691 mStack.resumeTopActivityLocked(null);
4692 } else {
4693 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004694 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004695 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004696 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004697 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004698 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004699
Craig Mautnerd163e752014-06-13 17:18:47 -07004700 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004701
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004702 if (DEBUG_STACK) Slog.d(TAG_STACK,
4703 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004704 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004705
Craig Mautner6985bad2014-04-21 15:22:06 -07004706 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004707 boolean isAttachedLocked() {
4708 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004709 }
4710
4711 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004712 void setDrawn() {
4713 synchronized (mService) {
4714 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004715 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004716 }
4717 }
4718
Craig Mautner1b4bf852014-05-26 15:06:32 -07004719 // Never start a new task on an ActivityView if it isn't explicitly specified.
4720 @Override
4721 boolean isEligibleForNewTasks() {
4722 return false;
4723 }
4724
Craig Mautnerd163e752014-06-13 17:18:47 -07004725 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004726 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004727 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4728 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4729 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4730 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4731 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004732 }
4733 }
4734
Craig Mautner4a1cb222013-12-04 16:14:06 -08004735 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4736 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004737 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004738 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004739 int mDisplayId;
4740 Display mDisplay;
4741 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004742
Craig Mautner4a1cb222013-12-04 16:14:06 -08004743 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4744 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08004745 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004746
Jose Lima4b6c6692014-08-12 17:41:12 -07004747 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004748
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004749 ActivityDisplay() {
4750 }
Craig Mautner4504de52013-12-20 09:06:56 -08004751
Craig Mautner1a70a162014-09-13 12:09:31 -07004752 // After instantiation, check that mDisplay is not null before using this. The alternative
4753 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004754 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004755 final Display display = mDisplayManager.getDisplay(displayId);
4756 if (display == null) {
4757 return;
4758 }
4759 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004760 }
4761
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004762 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004763 mDisplay = display;
4764 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004765 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004766 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004767
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004768 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004769 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004770 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
4771 + " onTop=" + onTop);
4772 if (onTop) {
4773 mStacks.add(stack);
4774 } else {
4775 mStacks.add(0, stack);
4776 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004777 }
4778
Craig Mautnere0a38842013-12-16 16:14:02 -08004779 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004780 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004781 + " from displayId=" + mDisplayId);
4782 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004783 }
4784
4785 void getBounds(Point bounds) {
4786 mDisplay.getDisplayInfo(mDisplayInfo);
4787 bounds.x = mDisplayInfo.appWidth;
4788 bounds.y = mDisplayInfo.appHeight;
4789 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004790
Jose Lima4b6c6692014-08-12 17:41:12 -07004791 void setVisibleBehindActivity(ActivityRecord r) {
4792 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004793 }
4794
Jose Lima4b6c6692014-08-12 17:41:12 -07004795 boolean hasVisibleBehindActivity() {
4796 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004797 }
4798
Craig Mautner34b73df2014-01-12 21:11:08 -08004799 @Override
4800 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004801 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4802 }
4803 }
4804
4805 class VirtualActivityDisplay extends ActivityDisplay {
4806 VirtualDisplay mVirtualDisplay;
4807
Craig Mautner6985bad2014-04-21 15:22:06 -07004808 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004809 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004810 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4811 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4812 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4813 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004814
4815 init(mVirtualDisplay.getDisplay());
4816
4817 mWindowManager.handleDisplayAdded(mDisplayId);
4818 }
4819
4820 void setSurface(Surface surface) {
4821 if (mVirtualDisplay != null) {
4822 mVirtualDisplay.setSurface(surface);
4823 }
4824 }
4825
4826 @Override
4827 void detachActivitiesLocked(ActivityStack stack) {
4828 super.detachActivitiesLocked(stack);
4829 if (mVirtualDisplay != null) {
4830 mVirtualDisplay.release();
4831 mVirtualDisplay = null;
4832 }
4833 }
4834
4835 @Override
4836 public String toString() {
4837 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004838 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004839 }
Jose Lima58e66d62014-05-27 20:00:27 -07004840
4841 private boolean isLeanbackOnlyDevice() {
4842 boolean onLeanbackOnly = false;
4843 try {
4844 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
4845 PackageManager.FEATURE_LEANBACK_ONLY);
4846 } catch (RemoteException e) {
4847 // noop
4848 }
4849
4850 return onLeanbackOnly;
4851 }
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07004852
4853 /**
4854 * Adjust bounds to stay within stack bounds.
4855 *
4856 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
4857 * that keep them unchanged, but be contained within the stack bounds.
4858 *
4859 * @param bounds Bounds to be adjusted.
4860 * @param stackBounds Bounds within which the other bounds should remain.
4861 */
4862 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
4863 if (stackBounds == null || stackBounds.contains(bounds)) {
4864 return;
4865 }
4866
4867 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
4868 final int maxRight = stackBounds.right
4869 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
4870 int horizontalDiff = stackBounds.left - bounds.left;
4871 if ((horizontalDiff < 0 && bounds.left >= maxRight)
4872 || (bounds.left + horizontalDiff >= maxRight)) {
4873 horizontalDiff = maxRight - bounds.left;
4874 }
4875 bounds.left += horizontalDiff;
4876 bounds.right += horizontalDiff;
4877 }
4878
4879 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
4880 final int maxBottom = stackBounds.bottom
4881 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
4882 int verticalDiff = stackBounds.top - bounds.top;
4883 if ((verticalDiff < 0 && bounds.top >= maxBottom)
4884 || (bounds.top + verticalDiff >= maxBottom)) {
4885 verticalDiff = maxBottom - bounds.top;
4886 }
4887 bounds.top += verticalDiff;
4888 bounds.bottom += verticalDiff;
4889 }
4890 }
4891
Craig Mautner27084302013-03-25 08:05:25 -07004892}