blob: 8455c6d04b3d86e806b06cb9244fa67eccb4abf5 [file] [log] [blame]
Kenny Guyb1b30262016-02-09 16:02:35 +00001/*
2 * Copyright (C) 2016 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
Wale Ogunwale59507092018-10-29 09:00:30 -070017package com.android.server.wm;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -080018
Michal Karpinski7b97a022018-12-14 15:17:29 +000019import static android.Manifest.permission.START_ACTIVITIES_FROM_BACKGROUND;
Wale Ogunwale01d66562015-12-29 08:19:19 -080020import static android.app.Activity.RESULT_CANCELED;
Bryce Leef9d49542017-06-26 16:27:32 -070021import static android.app.ActivityManager.START_ABORTED;
Bryce Leeaa5e8c32017-03-01 16:01:06 -080022import static android.app.ActivityManager.START_CANCELED;
Wale Ogunwale01d66562015-12-29 08:19:19 -080023import static android.app.ActivityManager.START_CLASS_NOT_FOUND;
24import static android.app.ActivityManager.START_DELIVERED_TO_TOP;
25import static android.app.ActivityManager.START_FLAG_ONLY_IF_NEEDED;
26import static android.app.ActivityManager.START_RETURN_INTENT_TO_CALLER;
27import static android.app.ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
28import static android.app.ActivityManager.START_SUCCESS;
29import static android.app.ActivityManager.START_TASK_TO_FRONT;
Louis Chang0513a942019-03-06 12:38:13 +080030import static android.app.WaitResult.LAUNCH_STATE_COLD;
31import static android.app.WaitResult.LAUNCH_STATE_HOT;
Wale Ogunwale68278562017-09-23 17:13:55 -070032import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
33import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
Wale Ogunwale7d7973a2018-04-05 10:25:59 -070034import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -070035import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
36import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_SECONDARY;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070037import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -080038import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK;
39import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
Wale Ogunwale2a25a622016-01-30 11:27:21 -080040import static android.content.Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -080041import static android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
Wale Ogunwale01d66562015-12-29 08:19:19 -080042import static android.content.Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -080043import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
Wale Ogunwale01d66562015-12-29 08:19:19 -080044import static android.content.Intent.FLAG_ACTIVITY_NO_ANIMATION;
45import static android.content.Intent.FLAG_ACTIVITY_NO_USER_ACTION;
Wale Ogunwale01d66562015-12-29 08:19:19 -080046import static android.content.Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP;
47import static android.content.Intent.FLAG_ACTIVITY_REORDER_TO_FRONT;
48import static android.content.Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED;
49import static android.content.Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
50import static android.content.Intent.FLAG_ACTIVITY_SINGLE_TOP;
Louis Changb45ee7e2019-01-17 10:36:56 +080051import static android.content.Intent.FLAG_ACTIVITY_TASK_ON_HOME;
Wale Ogunwale01d66562015-12-29 08:19:19 -080052import static android.content.pm.ActivityInfo.DOCUMENT_LAUNCH_ALWAYS;
Wale Ogunwale2322bed2019-10-10 17:24:19 +020053import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Bryce Leef65ee7e2018-03-26 16:03:47 -070054import static android.content.pm.ActivityInfo.LAUNCH_MULTIPLE;
Wale Ogunwale01d66562015-12-29 08:19:19 -080055import static android.content.pm.ActivityInfo.LAUNCH_SINGLE_INSTANCE;
56import static android.content.pm.ActivityInfo.LAUNCH_SINGLE_TASK;
57import static android.content.pm.ActivityInfo.LAUNCH_SINGLE_TOP;
Michal Karpinski7b97a022018-12-14 15:17:29 +000058import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Andrii Kulian79d67982019-08-19 11:56:16 -070059import static android.os.Process.INVALID_UID;
David Stevensc6b91c62017-02-08 14:23:58 -080060import static android.view.Display.DEFAULT_DISPLAY;
Andrii Kulian16802aa2016-11-02 12:21:33 -070061import static android.view.Display.INVALID_DISPLAY;
Riddle Hsub70b36d2018-09-11 21:20:02 +080062
Louis Changdd3592a2018-11-05 11:04:14 +080063import static com.android.server.am.EventLogTags.AM_NEW_INTENT;
Wale Ogunwale59507092018-10-29 09:00:30 -070064import static com.android.server.wm.ActivityStack.ActivityState.RESUMED;
65import static com.android.server.wm.ActivityStackSupervisor.DEFER_RESUME;
66import static com.android.server.wm.ActivityStackSupervisor.ON_TOP;
67import static com.android.server.wm.ActivityStackSupervisor.PRESERVE_WINDOWS;
68import static com.android.server.wm.ActivityStackSupervisor.TAG_TASKS;
69import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_CONFIGURATION;
70import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_FOCUS;
71import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_PERMISSIONS_REVIEW;
72import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_RESULTS;
73import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_STACK;
74import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_TASKS;
75import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_USER_LEAVING;
76import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_CONFIGURATION;
77import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_FOCUS;
78import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_RESULTS;
79import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_USER_LEAVING;
80import static com.android.server.wm.ActivityTaskManagerDebugConfig.TAG_ATM;
81import static com.android.server.wm.ActivityTaskManagerDebugConfig.TAG_WITH_CLASS_NAME;
82import static com.android.server.wm.ActivityTaskManagerService.ANIMATE;
Louis Chang6fb1e842018-12-03 16:07:50 +080083import static com.android.server.wm.LaunchParamsController.LaunchParamsModifier.PHASE_BOUNDS;
84import static com.android.server.wm.LaunchParamsController.LaunchParamsModifier.PHASE_DISPLAY;
Louis Changcdec0802019-11-11 11:45:07 +080085import static com.android.server.wm.Task.REPARENT_MOVE_STACK_TO_FRONT;
Winson Chung74666102017-02-22 17:49:24 -080086
Todd Kennedye9910222017-02-21 16:00:11 -080087import android.annotation.NonNull;
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +010088import android.annotation.Nullable;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -080089import android.app.ActivityManager;
90import android.app.ActivityOptions;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -080091import android.app.IApplicationThread;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -080092import android.app.PendingIntent;
93import android.app.ProfilerInfo;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -070094import android.app.WaitResult;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -080095import android.content.IIntentSender;
96import android.content.Intent;
97import android.content.IntentSender;
98import android.content.pm.ActivityInfo;
99import android.content.pm.ApplicationInfo;
Todd Kennedye9910222017-02-21 16:00:11 -0800100import android.content.pm.AuxiliaryResolveInfo;
Kenny Guyb1b30262016-02-09 16:02:35 +0000101import android.content.pm.PackageManager;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800102import android.content.pm.ResolveInfo;
Kenny Guyb1b30262016-02-09 16:02:35 +0000103import android.content.pm.UserInfo;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800104import android.content.res.Configuration;
105import android.graphics.Rect;
106import android.os.Binder;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800107import android.os.Bundle;
108import android.os.IBinder;
Michal Karpinski8596ded2018-11-14 14:43:48 +0000109import android.os.Process;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800110import android.os.RemoteException;
Michal Karpinski201bc0c2018-07-20 15:32:00 +0100111import android.os.Trace;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800112import android.os.UserHandle;
Kenny Guyb1b30262016-02-09 16:02:35 +0000113import android.os.UserManager;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800114import android.service.voice.IVoiceInteractionSession;
Wale Ogunwale692dcd62017-06-20 13:38:14 -0700115import android.text.TextUtils;
Michal Karpinskib7daac22019-03-25 10:12:41 +0000116import android.util.ArraySet;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800117import android.util.EventLog;
Bryce Leedaa91e42017-12-06 14:13:01 -0800118import android.util.Pools.SynchronizedPool;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800119import android.util.Slog;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800120
Bryce Leed3624e12017-11-30 08:51:45 -0800121import com.android.internal.annotations.VisibleForTesting;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800122import com.android.internal.app.HeavyWeightSwitcherActivity;
123import com.android.internal.app.IVoiceInteractor;
Wale Ogunwale59507092018-10-29 09:00:30 -0700124import com.android.server.am.EventLogTags;
125import com.android.server.am.PendingIntentRecord;
Louis Changdd3592a2018-11-05 11:04:14 +0800126import com.android.server.pm.InstantAppResolver;
Wale Ogunwale59507092018-10-29 09:00:30 -0700127import com.android.server.wm.ActivityStackSupervisor.PendingActivityLaunch;
128import com.android.server.wm.LaunchParamsController.LaunchParams;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800129
Wale Ogunwale692dcd62017-06-20 13:38:14 -0700130import java.io.PrintWriter;
131import java.text.DateFormat;
Wale Ogunwale692dcd62017-06-20 13:38:14 -0700132import java.util.Date;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800133
134/**
Bryce Leed3624e12017-11-30 08:51:45 -0800135 * Controller for interpreting how and then launching an activity.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800136 *
137 * This class collects all the logic for determining how an intent and flags should be turned into
138 * an activity and associated task and stack.
139 */
Wale Ogunwale01d66562015-12-29 08:19:19 -0800140class ActivityStarter {
Wale Ogunwale98875612018-10-12 07:53:02 -0700141 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStarter" : TAG_ATM;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800142 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
143 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
144 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
145 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
Bryce Lee7daee392017-10-12 13:46:18 -0700146 private static final int INVALID_LAUNCH_MODE = -1;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800147
Wale Ogunwalec9e57de2018-05-08 14:28:07 -0700148 private final ActivityTaskManagerService mService;
Wale Ogunwaled32da472018-11-16 07:19:28 -0800149 private final RootActivityContainer mRootActivityContainer;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800150 private final ActivityStackSupervisor mSupervisor;
Benjamin Franz563707b2017-06-29 15:06:13 +0100151 private final ActivityStartInterceptor mInterceptor;
Bryce Leed3624e12017-11-30 08:51:45 -0800152 private final ActivityStartController mController;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800153
Wale Ogunwale01d66562015-12-29 08:19:19 -0800154 // Share state variable among methods when starting an activity.
155 private ActivityRecord mStartActivity;
156 private Intent mIntent;
157 private int mCallingUid;
158 private ActivityOptions mOptions;
159
Ricky Waib147fa12019-04-25 16:08:30 +0100160 // If it is true, background activity can only be started in an existing task that contains
Alan Stokes07389b62019-05-20 15:22:54 +0100161 // an activity with same uid, or if activity starts are enabled in developer options.
Ricky Waib147fa12019-04-25 16:08:30 +0100162 private boolean mRestrictedBgActivity;
163
Bryce Lee7daee392017-10-12 13:46:18 -0700164 private int mLaunchMode;
Wale Ogunwale01d66562015-12-29 08:19:19 -0800165 private boolean mLaunchTaskBehind;
166 private int mLaunchFlags;
167
Bryce Leeec55eb02017-12-05 20:51:27 -0800168 private LaunchParams mLaunchParams = new LaunchParams();
Wale Ogunwale01d66562015-12-29 08:19:19 -0800169
170 private ActivityRecord mNotTop;
171 private boolean mDoResume;
172 private int mStartFlags;
173 private ActivityRecord mSourceRecord;
Bryce Lee7daee392017-10-12 13:46:18 -0700174
David Stevense5a7b642017-05-22 13:18:23 -0700175 // The display to launch the activity onto, barring any strong reason to do otherwise.
176 private int mPreferredDisplayId;
Wale Ogunwale01d66562015-12-29 08:19:19 -0800177
Louis Changcdec0802019-11-11 11:45:07 +0800178 private Task mInTask;
Wale Ogunwale01d66562015-12-29 08:19:19 -0800179 private boolean mAddingToTask;
Louis Changcdec0802019-11-11 11:45:07 +0800180 private Task mReuseTask;
Wale Ogunwale01d66562015-12-29 08:19:19 -0800181
182 private ActivityInfo mNewTaskInfo;
183 private Intent mNewTaskIntent;
184 private ActivityStack mSourceStack;
185 private ActivityStack mTargetStack;
Wale Ogunwale01d66562015-12-29 08:19:19 -0800186 private boolean mMovedToFront;
187 private boolean mNoAnimation;
188 private boolean mKeepCurTransition;
Jorim Jaggic875ae72016-04-26 22:41:06 -0700189 private boolean mAvoidMoveToFront;
Winson Chunge219ae12019-07-18 13:43:23 -0700190 private boolean mFrozeTaskList;
Wale Ogunwale01d66562015-12-29 08:19:19 -0800191
Bryce Lee325e09682017-10-05 17:20:25 -0700192 // We must track when we deliver the new intent since multiple code paths invoke
193 // {@link #deliverNewIntent}. This is due to early returns in the code path. This flag is used
194 // inside {@link #deliverNewIntent} to suppress duplicate requests and ensure the intent is
195 // delivered at most once.
196 private boolean mIntentDelivered;
197
Wale Ogunwale01d66562015-12-29 08:19:19 -0800198 private IVoiceInteractionSession mVoiceSession;
199 private IVoiceInteractor mVoiceInteractor;
200
Wale Ogunwale692dcd62017-06-20 13:38:14 -0700201 // Last activity record we attempted to start
Louis Chang54fbb052019-10-16 17:10:17 +0800202 private ActivityRecord mLastStartActivityRecord;
Wale Ogunwale692dcd62017-06-20 13:38:14 -0700203 // The result of the last activity we attempted to start.
204 private int mLastStartActivityResult;
205 // Time in milli seconds we attempted to start the last activity.
206 private long mLastStartActivityTimeMs;
207 // The reason we were trying to start the last activity
208 private String mLastStartReason;
Wale Ogunwale59bcba62017-06-16 12:42:51 -0700209
Bryce Lee4c9a5972017-12-01 22:14:24 -0800210 /*
211 * Request details provided through setter methods. Should be reset after {@link #execute()}
212 * to avoid unnecessarily retaining parameters. Note that the request is ignored when
213 * {@link #startResolvedActivity} is invoked directly.
214 */
Louis Chang54fbb052019-10-16 17:10:17 +0800215 @VisibleForTesting
216 Request mRequest = new Request();
Bryce Lee4c9a5972017-12-01 22:14:24 -0800217
Bryce Leed3624e12017-11-30 08:51:45 -0800218 /**
219 * An interface that to provide {@link ActivityStarter} instances to the controller. This is
220 * used by tests to inject their own starter implementations for verification purposes.
221 */
222 @VisibleForTesting
223 interface Factory {
224 /**
Bryce Lee4c9a5972017-12-01 22:14:24 -0800225 * Sets the {@link ActivityStartController} to be passed to {@link ActivityStarter}.
226 */
227 void setController(ActivityStartController controller);
228
229 /**
Bryce Leed3624e12017-11-30 08:51:45 -0800230 * Generates an {@link ActivityStarter} that is ready to handle a new start request.
231 * @param controller The {@link ActivityStartController} which the starter who will own
232 * this instance.
233 * @return an {@link ActivityStarter}
234 */
Bryce Leedaa91e42017-12-06 14:13:01 -0800235 ActivityStarter obtain();
236
237 /**
238 * Recycles a starter for reuse.
239 */
240 void recycle(ActivityStarter starter);
Wale Ogunwale01d66562015-12-29 08:19:19 -0800241 }
242
Bryce Leed3624e12017-11-30 08:51:45 -0800243 /**
244 * Default implementation of {@link StarterFactory}.
245 */
246 static class DefaultFactory implements Factory {
Bryce Leedaa91e42017-12-06 14:13:01 -0800247 /**
248 * The maximum count of starters that should be active at one time:
249 * 1. last ran starter (for logging and post activity processing)
250 * 2. current running starter
251 * 3. starter from re-entry in (2)
252 */
253 private final int MAX_STARTER_COUNT = 3;
254
Bryce Lee4c9a5972017-12-01 22:14:24 -0800255 private ActivityStartController mController;
Wale Ogunwalec9e57de2018-05-08 14:28:07 -0700256 private ActivityTaskManagerService mService;
Bryce Lee4c9a5972017-12-01 22:14:24 -0800257 private ActivityStackSupervisor mSupervisor;
258 private ActivityStartInterceptor mInterceptor;
259
Bryce Leedaa91e42017-12-06 14:13:01 -0800260 private SynchronizedPool<ActivityStarter> mStarterPool =
261 new SynchronizedPool<>(MAX_STARTER_COUNT);
262
Wale Ogunwalec9e57de2018-05-08 14:28:07 -0700263 DefaultFactory(ActivityTaskManagerService service,
Bryce Lee4c9a5972017-12-01 22:14:24 -0800264 ActivityStackSupervisor supervisor, ActivityStartInterceptor interceptor) {
265 mService = service;
266 mSupervisor = supervisor;
267 mInterceptor = interceptor;
Bryce Leed3624e12017-11-30 08:51:45 -0800268 }
Bryce Lee4c9a5972017-12-01 22:14:24 -0800269
270 @Override
271 public void setController(ActivityStartController controller) {
272 mController = controller;
273 }
274
275 @Override
Bryce Leedaa91e42017-12-06 14:13:01 -0800276 public ActivityStarter obtain() {
277 ActivityStarter starter = mStarterPool.acquire();
278
279 if (starter == null) {
280 starter = new ActivityStarter(mController, mService, mSupervisor, mInterceptor);
281 }
282
283 return starter;
284 }
285
286 @Override
287 public void recycle(ActivityStarter starter) {
288 starter.reset(true /* clearRequest*/);
289 mStarterPool.release(starter);
Bryce Lee4c9a5972017-12-01 22:14:24 -0800290 }
291 }
292
293 /**
294 * Container for capturing initial start request details. This information is NOT reset until
295 * the {@link ActivityStarter} is recycled, allowing for multiple invocations with the same
296 * parameters.
297 *
298 * TODO(b/64750076): Investigate consolidating member variables of {@link ActivityStarter} with
299 * the request object. Note that some member variables are referenced in
300 * {@link #dump(PrintWriter, String)} and therefore cannot be cleared immediately after
301 * execution.
302 */
Louis Chang54fbb052019-10-16 17:10:17 +0800303 @VisibleForTesting
304 static class Request {
Bryce Lee4c9a5972017-12-01 22:14:24 -0800305 private static final int DEFAULT_CALLING_UID = -1;
306 private static final int DEFAULT_CALLING_PID = 0;
Michal Karpinski84d9ebd2019-01-17 18:28:59 +0000307 static final int DEFAULT_REAL_CALLING_UID = -1;
308 static final int DEFAULT_REAL_CALLING_PID = 0;
Bryce Lee4c9a5972017-12-01 22:14:24 -0800309
310 IApplicationThread caller;
311 Intent intent;
Louis Chang54fbb052019-10-16 17:10:17 +0800312 // A copy of the original requested intent, in case for ephemeral app launch.
Bryce Lee4c9a5972017-12-01 22:14:24 -0800313 Intent ephemeralIntent;
314 String resolvedType;
315 ActivityInfo activityInfo;
316 ResolveInfo resolveInfo;
317 IVoiceInteractionSession voiceSession;
318 IVoiceInteractor voiceInteractor;
319 IBinder resultTo;
320 String resultWho;
321 int requestCode;
Michal Karpinski84d9ebd2019-01-17 18:28:59 +0000322 int callingPid = DEFAULT_CALLING_PID;
323 int callingUid = DEFAULT_CALLING_UID;
Bryce Lee4c9a5972017-12-01 22:14:24 -0800324 String callingPackage;
Michal Karpinski84d9ebd2019-01-17 18:28:59 +0000325 int realCallingPid = DEFAULT_REAL_CALLING_PID;
326 int realCallingUid = DEFAULT_REAL_CALLING_UID;
Bryce Lee4c9a5972017-12-01 22:14:24 -0800327 int startFlags;
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +0100328 SafeActivityOptions activityOptions;
Bryce Lee4c9a5972017-12-01 22:14:24 -0800329 boolean ignoreTargetSecurity;
330 boolean componentSpecified;
Winson Chunge2d72172018-01-25 17:46:20 +0000331 boolean avoidMoveToFront;
Bryce Lee4c9a5972017-12-01 22:14:24 -0800332 ActivityRecord[] outActivity;
Louis Changcdec0802019-11-11 11:45:07 +0800333 Task inTask;
Bryce Lee4c9a5972017-12-01 22:14:24 -0800334 String reason;
335 ProfilerInfo profilerInfo;
336 Configuration globalConfig;
Bryce Lee4c9a5972017-12-01 22:14:24 -0800337 int userId;
338 WaitResult waitResult;
Patrick Baumann31426b22018-05-21 13:46:40 -0700339 int filterCallingUid;
Michal Karpinski201bc0c2018-07-20 15:32:00 +0100340 PendingIntentRecord originatingPendingIntent;
Michal Karpinskiac116df2018-12-10 17:51:42 +0000341 boolean allowBackgroundActivityStart;
Bryce Lee4c9a5972017-12-01 22:14:24 -0800342
343 /**
Jorim Jaggi6fa41c32018-04-23 18:35:00 +0200344 * If set to {@code true}, allows this activity start to look into
345 * {@link PendingRemoteAnimationRegistry}
346 */
347 boolean allowPendingRemoteAnimationRegistryLookup;
348
349 /**
Bryce Leea3cd8e02018-01-09 15:44:24 -0800350 * Ensure constructed request matches reset instance.
351 */
352 Request() {
353 reset();
354 }
355
356 /**
Bryce Leedaa91e42017-12-06 14:13:01 -0800357 * Sets values back to the initial state, clearing any held references.
358 */
359 void reset() {
360 caller = null;
361 intent = null;
362 ephemeralIntent = null;
363 resolvedType = null;
364 activityInfo = null;
365 resolveInfo = null;
366 voiceSession = null;
367 voiceInteractor = null;
368 resultTo = null;
369 resultWho = null;
370 requestCode = 0;
Bryce Leea3cd8e02018-01-09 15:44:24 -0800371 callingPid = DEFAULT_CALLING_PID;
372 callingUid = DEFAULT_CALLING_UID;
Bryce Leedaa91e42017-12-06 14:13:01 -0800373 callingPackage = null;
Michal Karpinski84d9ebd2019-01-17 18:28:59 +0000374 realCallingPid = DEFAULT_REAL_CALLING_PID;
375 realCallingUid = DEFAULT_REAL_CALLING_UID;
Bryce Leedaa91e42017-12-06 14:13:01 -0800376 startFlags = 0;
377 activityOptions = null;
378 ignoreTargetSecurity = false;
379 componentSpecified = false;
380 outActivity = null;
381 inTask = null;
382 reason = null;
383 profilerInfo = null;
384 globalConfig = null;
Bryce Leedaa91e42017-12-06 14:13:01 -0800385 userId = 0;
386 waitResult = null;
Winson Chunge2d72172018-01-25 17:46:20 +0000387 avoidMoveToFront = false;
Jorim Jaggi6fa41c32018-04-23 18:35:00 +0200388 allowPendingRemoteAnimationRegistryLookup = true;
Patrick Baumann31426b22018-05-21 13:46:40 -0700389 filterCallingUid = UserHandle.USER_NULL;
Michal Karpinski201bc0c2018-07-20 15:32:00 +0100390 originatingPendingIntent = null;
Michal Karpinskiac116df2018-12-10 17:51:42 +0000391 allowBackgroundActivityStart = false;
Bryce Leedaa91e42017-12-06 14:13:01 -0800392 }
393
394 /**
395 * Adopts all values from passed in request.
396 */
397 void set(Request request) {
398 caller = request.caller;
399 intent = request.intent;
400 ephemeralIntent = request.ephemeralIntent;
401 resolvedType = request.resolvedType;
402 activityInfo = request.activityInfo;
403 resolveInfo = request.resolveInfo;
404 voiceSession = request.voiceSession;
405 voiceInteractor = request.voiceInteractor;
406 resultTo = request.resultTo;
407 resultWho = request.resultWho;
408 requestCode = request.requestCode;
409 callingPid = request.callingPid;
410 callingUid = request.callingUid;
411 callingPackage = request.callingPackage;
412 realCallingPid = request.realCallingPid;
413 realCallingUid = request.realCallingUid;
414 startFlags = request.startFlags;
415 activityOptions = request.activityOptions;
416 ignoreTargetSecurity = request.ignoreTargetSecurity;
417 componentSpecified = request.componentSpecified;
418 outActivity = request.outActivity;
419 inTask = request.inTask;
420 reason = request.reason;
421 profilerInfo = request.profilerInfo;
422 globalConfig = request.globalConfig;
Bryce Leedaa91e42017-12-06 14:13:01 -0800423 userId = request.userId;
424 waitResult = request.waitResult;
Winson Chunge2d72172018-01-25 17:46:20 +0000425 avoidMoveToFront = request.avoidMoveToFront;
Jorim Jaggi6fa41c32018-04-23 18:35:00 +0200426 allowPendingRemoteAnimationRegistryLookup
427 = request.allowPendingRemoteAnimationRegistryLookup;
Patrick Baumann31426b22018-05-21 13:46:40 -0700428 filterCallingUid = request.filterCallingUid;
Michal Karpinski201bc0c2018-07-20 15:32:00 +0100429 originatingPendingIntent = request.originatingPendingIntent;
Michal Karpinskiac116df2018-12-10 17:51:42 +0000430 allowBackgroundActivityStart = request.allowBackgroundActivityStart;
Bryce Leedaa91e42017-12-06 14:13:01 -0800431 }
Louis Chang54fbb052019-10-16 17:10:17 +0800432
433 /**
434 * Resolve activity from the given intent for this launch.
435 */
436 void resolveActivity(ActivityStackSupervisor supervisor) {
437 if (realCallingPid == Request.DEFAULT_REAL_CALLING_PID) {
438 realCallingPid = Binder.getCallingPid();
439 }
440 if (realCallingUid == Request.DEFAULT_REAL_CALLING_UID) {
441 realCallingUid = Binder.getCallingUid();
442 }
443
444 if (callingUid >= 0) {
445 callingPid = -1;
446 } else if (caller == null) {
447 callingPid = realCallingPid;
448 callingUid = realCallingUid;
449 } else {
450 callingPid = callingUid = -1;
451 }
452
453 // Save a copy in case ephemeral needs it
454 ephemeralIntent = new Intent(intent);
455 // Don't modify the client's object!
456 intent = new Intent(intent);
457 if (intent.getComponent() != null
458 && !(Intent.ACTION_VIEW.equals(intent.getAction()) && intent.getData() == null)
459 && !Intent.ACTION_INSTALL_INSTANT_APP_PACKAGE.equals(intent.getAction())
460 && !Intent.ACTION_RESOLVE_INSTANT_APP_PACKAGE.equals(intent.getAction())
461 && supervisor.mService.getPackageManagerInternalLocked()
462 .isInstantAppInstallerComponent(intent.getComponent())) {
463 // Intercept intents targeted directly to the ephemeral installer the ephemeral
464 // installer should never be started with a raw Intent; instead adjust the intent
465 // so it looks like a "normal" instant app launch.
466 intent.setComponent(null /* component */);
467 }
468
469 resolveInfo = supervisor.resolveIntent(intent, resolvedType, userId,
470 0 /* matchFlags */,
471 computeResolveFilterUid(callingUid, realCallingUid, filterCallingUid));
472 if (resolveInfo == null) {
473 final UserInfo userInfo = supervisor.getUserInfo(userId);
474 if (userInfo != null && userInfo.isManagedProfile()) {
475 // Special case for managed profiles, if attempting to launch non-cryto aware
476 // app in a locked managed profile from an unlocked parent allow it to resolve
477 // as user will be sent via confirm credentials to unlock the profile.
478 final UserManager userManager = UserManager.get(supervisor.mService.mContext);
479 boolean profileLockedAndParentUnlockingOrUnlocked = false;
480 final long token = Binder.clearCallingIdentity();
481 try {
482 final UserInfo parent = userManager.getProfileParent(userId);
483 profileLockedAndParentUnlockingOrUnlocked = (parent != null)
484 && userManager.isUserUnlockingOrUnlocked(parent.id)
485 && !userManager.isUserUnlockingOrUnlocked(userId);
486 } finally {
487 Binder.restoreCallingIdentity(token);
488 }
489 if (profileLockedAndParentUnlockingOrUnlocked) {
490 resolveInfo = supervisor.resolveIntent(intent, resolvedType, userId,
491 PackageManager.MATCH_DIRECT_BOOT_AWARE
492 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE,
493 computeResolveFilterUid(callingUid, realCallingUid,
494 filterCallingUid));
495 }
496 }
497 }
498
499 // Collect information about the target of the Intent.
500 activityInfo = supervisor.resolveActivity(intent, resolveInfo, startFlags,
501 profilerInfo);
502 }
Bryce Leed3624e12017-11-30 08:51:45 -0800503 }
504
Wale Ogunwalec9e57de2018-05-08 14:28:07 -0700505 ActivityStarter(ActivityStartController controller, ActivityTaskManagerService service,
Bryce Leed3624e12017-11-30 08:51:45 -0800506 ActivityStackSupervisor supervisor, ActivityStartInterceptor interceptor) {
507 mController = controller;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800508 mService = service;
Wale Ogunwaled32da472018-11-16 07:19:28 -0800509 mRootActivityContainer = service.mRootActivityContainer;
Bryce Leed3624e12017-11-30 08:51:45 -0800510 mSupervisor = supervisor;
511 mInterceptor = interceptor;
Bryce Leedaa91e42017-12-06 14:13:01 -0800512 reset(true);
513 }
514
515 /**
516 * Effectively duplicates the starter passed in. All state and request values will be
517 * mirrored.
518 * @param starter
519 */
520 void set(ActivityStarter starter) {
521 mStartActivity = starter.mStartActivity;
522 mIntent = starter.mIntent;
523 mCallingUid = starter.mCallingUid;
524 mOptions = starter.mOptions;
Ricky Waib147fa12019-04-25 16:08:30 +0100525 mRestrictedBgActivity = starter.mRestrictedBgActivity;
Bryce Leedaa91e42017-12-06 14:13:01 -0800526
527 mLaunchTaskBehind = starter.mLaunchTaskBehind;
528 mLaunchFlags = starter.mLaunchFlags;
529 mLaunchMode = starter.mLaunchMode;
530
Bryce Leeec55eb02017-12-05 20:51:27 -0800531 mLaunchParams.set(starter.mLaunchParams);
Bryce Leedaa91e42017-12-06 14:13:01 -0800532
533 mNotTop = starter.mNotTop;
534 mDoResume = starter.mDoResume;
535 mStartFlags = starter.mStartFlags;
536 mSourceRecord = starter.mSourceRecord;
537 mPreferredDisplayId = starter.mPreferredDisplayId;
538
539 mInTask = starter.mInTask;
540 mAddingToTask = starter.mAddingToTask;
541 mReuseTask = starter.mReuseTask;
542
543 mNewTaskInfo = starter.mNewTaskInfo;
544 mNewTaskIntent = starter.mNewTaskIntent;
545 mSourceStack = starter.mSourceStack;
546
547 mTargetStack = starter.mTargetStack;
548 mMovedToFront = starter.mMovedToFront;
549 mNoAnimation = starter.mNoAnimation;
550 mKeepCurTransition = starter.mKeepCurTransition;
551 mAvoidMoveToFront = starter.mAvoidMoveToFront;
Winson Chunge219ae12019-07-18 13:43:23 -0700552 mFrozeTaskList = starter.mFrozeTaskList;
Bryce Leedaa91e42017-12-06 14:13:01 -0800553
554 mVoiceSession = starter.mVoiceSession;
555 mVoiceInteractor = starter.mVoiceInteractor;
556
557 mIntentDelivered = starter.mIntentDelivered;
558
559 mRequest.set(starter.mRequest);
Bryce Leed3624e12017-11-30 08:51:45 -0800560 }
561
Bryce Lee4c9a5972017-12-01 22:14:24 -0800562 boolean relatedToPackage(String packageName) {
Louis Chang54fbb052019-10-16 17:10:17 +0800563 return (mLastStartActivityRecord != null
564 && packageName.equals(mLastStartActivityRecord.packageName))
Bryce Lee4c9a5972017-12-01 22:14:24 -0800565 || (mStartActivity != null && packageName.equals(mStartActivity.packageName));
566 }
567
568 /**
Louis Chang54fbb052019-10-16 17:10:17 +0800569 * Starts an activity based on the provided {@link ActivityRecord} and environment parameters.
570 * Note that this method is called internally as well as part of {@link #executeRequest}.
571 */
572 void startResolvedActivity(final ActivityRecord r, ActivityRecord sourceRecord,
573 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Louis Changcdec0802019-11-11 11:45:07 +0800574 int startFlags, boolean doResume, ActivityOptions options, Task inTask) {
Louis Chang54fbb052019-10-16 17:10:17 +0800575 try {
576 mSupervisor.getActivityMetricsLogger().notifyActivityLaunching(r.intent);
577 mLastStartReason = "startResolvedActivity";
578 mLastStartActivityTimeMs = System.currentTimeMillis();
579 mLastStartActivityRecord = r;
580 mLastStartActivityResult = startActivityUnchecked(r, sourceRecord, voiceSession,
581 voiceInteractor, startFlags, doResume, options, inTask,
582 false /* restrictedBgActivity */);
583 mSupervisor.getActivityMetricsLogger().notifyActivityLaunched(mLastStartActivityResult,
584 mLastStartActivityRecord);
585 } finally {
586 onExecutionComplete();
587 }
588 }
589
590 /**
591 * Resolve necessary information according the request parameters provided earlier, and execute
592 * the request which begin the journey of starting an activity.
Bryce Lee4c9a5972017-12-01 22:14:24 -0800593 * @return The starter result.
594 */
595 int execute() {
Bryce Leedaa91e42017-12-06 14:13:01 -0800596 try {
Louis Chang54fbb052019-10-16 17:10:17 +0800597 // Refuse possible leaked file descriptors
598 if (mRequest.intent != null && mRequest.intent.hasFileDescriptors()) {
599 throw new IllegalArgumentException("File descriptors passed in Intent");
600 }
601
602 mSupervisor.getActivityMetricsLogger().notifyActivityLaunching(mRequest.intent);
603
604 if (mRequest.activityInfo == null) {
605 mRequest.resolveActivity(mSupervisor);
606 }
607
608 int res;
609 synchronized (mService.mGlobalLock) {
610 final ActivityStack stack = mRootActivityContainer.getTopDisplayFocusedStack();
611 stack.mConfigWillChange = mRequest.globalConfig != null
612 && mService.getGlobalConfiguration().diff(mRequest.globalConfig) != 0;
613 if (DEBUG_CONFIGURATION) {
614 Slog.v(TAG_CONFIGURATION, "Starting activity when config will change = "
615 + stack.mConfigWillChange);
616 }
617
618 final long origId = Binder.clearCallingIdentity();
619
620 res = resolveToHeavyWeightSwitcherIfNeeded();
621 if (res != START_SUCCESS) {
622 return res;
623 }
624 res = executeRequest(mRequest);
625
626 Binder.restoreCallingIdentity(origId);
627
628 if (stack.mConfigWillChange) {
629 // If the caller also wants to switch to a new configuration, do so now.
630 // This allows a clean switch, as we are waiting for the current activity
631 // to pause (so we will not destroy it), and have not yet started the
632 // next activity.
633 mService.mAmInternal.enforceCallingPermission(
634 android.Manifest.permission.CHANGE_CONFIGURATION,
635 "updateConfiguration()");
636 stack.mConfigWillChange = false;
637 if (DEBUG_CONFIGURATION) {
638 Slog.v(TAG_CONFIGURATION,
639 "Updating to new configuration after starting activity.");
640 }
641 mService.updateConfigurationLocked(mRequest.globalConfig, null, false);
642 }
643
644 // Notify ActivityMetricsLogger that the activity has launched.
645 // ActivityMetricsLogger will then wait for the windows to be drawn and populate
646 // WaitResult.
647 mSupervisor.getActivityMetricsLogger().notifyActivityLaunched(res,
648 mLastStartActivityRecord);
649 return getExternalResult(mRequest.waitResult == null ? res
650 : waitForResult(res, mLastStartActivityRecord));
Bryce Leedaa91e42017-12-06 14:13:01 -0800651 }
652 } finally {
653 onExecutionComplete();
654 }
655 }
656
657 /**
Louis Chang54fbb052019-10-16 17:10:17 +0800658 * Updates the request to heavy-weight switch if this is a heavy-weight process while there
659 * already have another, different heavy-weight process running.
Bryce Leedaa91e42017-12-06 14:13:01 -0800660 */
Louis Chang54fbb052019-10-16 17:10:17 +0800661 private int resolveToHeavyWeightSwitcherIfNeeded() {
662 if (mRequest.activityInfo == null || !mService.mHasHeavyWeightFeature
663 || (mRequest.activityInfo.applicationInfo.privateFlags
664 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) == 0) {
665 return START_SUCCESS;
Wale Ogunwale692dcd62017-06-20 13:38:14 -0700666 }
Bryce Leef9d49542017-06-26 16:27:32 -0700667
Louis Chang54fbb052019-10-16 17:10:17 +0800668 if (!mRequest.activityInfo.processName.equals(
669 mRequest.activityInfo.applicationInfo.packageName)) {
670 return START_SUCCESS;
671 }
Bryce Lee93e7f792017-10-25 15:54:55 -0700672
Louis Chang54fbb052019-10-16 17:10:17 +0800673 final WindowProcessController heavy = mService.mHeavyWeightProcess;
674 if (heavy == null || (heavy.mInfo.uid == mRequest.activityInfo.applicationInfo.uid
675 && heavy.mName.equals(mRequest.activityInfo.processName))) {
676 return START_SUCCESS;
677 }
678
679 int appCallingUid = mRequest.callingUid;
680 if (mRequest.caller != null) {
681 WindowProcessController callerApp = mService.getProcessController(mRequest.caller);
682 if (callerApp != null) {
683 appCallingUid = callerApp.mInfo.uid;
684 } else {
685 Slog.w(TAG, "Unable to find app for caller " + mRequest.caller + " (pid="
686 + mRequest.callingPid + ") when starting: " + mRequest.intent.toString());
687 SafeActivityOptions.abort(mRequest.activityOptions);
688 return ActivityManager.START_PERMISSION_DENIED;
689 }
690 }
691
692 final IIntentSender target = mService.getIntentSenderLocked(
693 ActivityManager.INTENT_SENDER_ACTIVITY, "android" /* packageName */, appCallingUid,
694 mRequest.userId, null /* token */, null /* resultWho*/, 0 /* requestCode*/,
695 new Intent[] { mRequest.intent }, new String[] { mRequest.resolvedType },
696 PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT,
697 null /* bOptions */);
698
699 final Intent newIntent = new Intent();
700 if (mRequest.requestCode >= 0) {
701 // Caller is requesting a result.
702 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
703 }
704 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT, new IntentSender(target));
705 heavy.updateIntentForHeavyWeightActivity(newIntent);
706 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
707 mRequest.activityInfo.packageName);
708 newIntent.setFlags(mRequest.intent.getFlags());
709 newIntent.setClassName("android" /* packageName */,
710 HeavyWeightSwitcherActivity.class.getName());
711 mRequest.intent = newIntent;
712 mRequest.resolvedType = null;
713 mRequest.caller = null;
714 mRequest.callingUid = Binder.getCallingUid();
715 mRequest.callingPid = Binder.getCallingPid();
716 mRequest.componentSpecified = true;
717 mRequest.resolveInfo = mSupervisor.resolveIntent(mRequest.intent, null /* resolvedType */,
718 mRequest.userId, 0 /* matchFlags */,
719 computeResolveFilterUid(mRequest.callingUid, mRequest.realCallingUid,
720 mRequest.filterCallingUid));
721 mRequest.activityInfo =
722 mRequest.resolveInfo != null ? mRequest.resolveInfo.activityInfo : null;
723 if (mRequest.activityInfo != null) {
724 mRequest.activityInfo = mService.mAmInternal.getActivityInfoForUser(
725 mRequest.activityInfo, mRequest.userId);
726 }
727
728 return START_SUCCESS;
Wale Ogunwale692dcd62017-06-20 13:38:14 -0700729 }
730
Bryce Leedaa91e42017-12-06 14:13:01 -0800731 /**
Louis Chang54fbb052019-10-16 17:10:17 +0800732 * Wait for activity launch completes.
Bryce Leedaa91e42017-12-06 14:13:01 -0800733 */
Louis Chang54fbb052019-10-16 17:10:17 +0800734 private int waitForResult(int res, ActivityRecord r) {
735 mRequest.waitResult.result = res;
736 switch(res) {
737 case START_SUCCESS: {
738 mSupervisor.mWaitingActivityLaunched.add(mRequest.waitResult);
739 do {
740 try {
741 mService.mGlobalLock.wait();
742 } catch (InterruptedException e) {
743 }
744 } while (mRequest.waitResult.result != START_TASK_TO_FRONT
745 && !mRequest.waitResult.timeout && mRequest.waitResult.who == null);
746 if (mRequest.waitResult.result == START_TASK_TO_FRONT) {
747 res = START_TASK_TO_FRONT;
748 }
749 break;
750 }
751 case START_DELIVERED_TO_TOP: {
752 mRequest.waitResult.timeout = false;
753 mRequest.waitResult.who = r.mActivityComponent;
754 mRequest.waitResult.totalTime = 0;
755 break;
756 }
757 case START_TASK_TO_FRONT: {
758 mRequest.waitResult.launchState =
759 r.attachedToProcess() ? LAUNCH_STATE_HOT : LAUNCH_STATE_COLD;
760 // ActivityRecord may represent a different activity, but it should not be
761 // in the resumed state.
762 if (r.nowVisible && r.isState(RESUMED)) {
763 mRequest.waitResult.timeout = false;
764 mRequest.waitResult.who = r.mActivityComponent;
765 mRequest.waitResult.totalTime = 0;
766 } else {
Riddle Hsuc48c8912019-10-31 13:34:27 +0800767 mSupervisor.waitActivityVisible(r.mActivityComponent, mRequest.waitResult);
Louis Chang54fbb052019-10-16 17:10:17 +0800768 // Note: the timeout variable is not currently not ever set.
769 do {
770 try {
771 mService.mGlobalLock.wait();
772 } catch (InterruptedException e) {
773 }
774 } while (!mRequest.waitResult.timeout && mRequest.waitResult.who == null);
775 }
776 break;
777 }
778 }
779 return res;
Bryce Leedaa91e42017-12-06 14:13:01 -0800780 }
781
Louis Chang54fbb052019-10-16 17:10:17 +0800782 /**
783 * Executing activity start request and starts the journey of starting an activity. Here
784 * begins with performing several preliminary checks. The normally activity launch flow will
785 * go through {@link #startActivityUnchecked} to {@link #startActivityInner}.
786 */
787 private int executeRequest(Request request) {
788 if (TextUtils.isEmpty(request.reason)) {
789 throw new IllegalArgumentException("Need to specify a reason.");
790 }
791 mLastStartReason = request.reason;
792 mLastStartActivityTimeMs = System.currentTimeMillis();
793 mLastStartActivityRecord = null;
794
795 final IApplicationThread caller = request.caller;
796 Intent intent = request.intent;
797 String resolvedType = request.resolvedType;
798 ActivityInfo aInfo = request.activityInfo;
799 ResolveInfo rInfo = request.resolveInfo;
800 final IVoiceInteractionSession voiceSession = request.voiceSession;
801 final IBinder resultTo = request.resultTo;
802 String resultWho = request.resultWho;
803 int requestCode = request.requestCode;
804 int callingPid = request.callingPid;
805 int callingUid = request.callingUid;
806 String callingPackage = request.callingPackage;
807 final int realCallingPid = request.realCallingPid;
808 final int realCallingUid = request.realCallingUid;
809 final int startFlags = request.startFlags;
810 final SafeActivityOptions options = request.activityOptions;
Louis Changcdec0802019-11-11 11:45:07 +0800811 Task inTask = request.inTask;
Louis Chang54fbb052019-10-16 17:10:17 +0800812
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800813 int err = ActivityManager.START_SUCCESS;
Chad Brubaker06068612017-04-06 09:43:47 -0700814 // Pull the optional Ephemeral Installer-only bundle out of the options early.
Louis Chang54fbb052019-10-16 17:10:17 +0800815 final Bundle verificationBundle =
816 options != null ? options.popAppVerificationBundle() : null;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800817
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700818 WindowProcessController callerApp = null;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800819 if (caller != null) {
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700820 callerApp = mService.getProcessController(caller);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800821 if (callerApp != null) {
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700822 callingPid = callerApp.getPid();
823 callingUid = callerApp.mInfo.uid;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800824 } else {
Louis Chang54fbb052019-10-16 17:10:17 +0800825 Slog.w(TAG, "Unable to find app for caller " + caller + " (pid=" + callingPid
826 + ") when starting: " + intent.toString());
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800827 err = ActivityManager.START_PERMISSION_DENIED;
828 }
829 }
830
Bryce Lee93e7f792017-10-25 15:54:55 -0700831 final int userId = aInfo != null && aInfo.applicationInfo != null
832 ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800833 if (err == ActivityManager.START_SUCCESS) {
834 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Andrii Kulian03c403d2016-11-11 11:14:12 -0800835 + "} from uid " + callingUid);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800836 }
837
838 ActivityRecord sourceRecord = null;
839 ActivityRecord resultRecord = null;
840 if (resultTo != null) {
Wale Ogunwaled32da472018-11-16 07:19:28 -0800841 sourceRecord = mRootActivityContainer.isInAnyStack(resultTo);
Louis Chang54fbb052019-10-16 17:10:17 +0800842 if (DEBUG_RESULTS) {
843 Slog.v(TAG_RESULTS, "Will send result to " + resultTo + " " + sourceRecord);
844 }
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800845 if (sourceRecord != null) {
846 if (requestCode >= 0 && !sourceRecord.finishing) {
847 resultRecord = sourceRecord;
848 }
849 }
850 }
851
852 final int launchFlags = intent.getFlags();
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800853 if ((launchFlags & Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Louis Chang54fbb052019-10-16 17:10:17 +0800854 // Transfer the result target from the source activity to the new one being started,
855 // including any failures.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800856 if (requestCode >= 0) {
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +0100857 SafeActivityOptions.abort(options);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800858 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
859 }
860 resultRecord = sourceRecord.resultTo;
861 if (resultRecord != null && !resultRecord.isInStackLocked()) {
862 resultRecord = null;
863 }
864 resultWho = sourceRecord.resultWho;
865 requestCode = sourceRecord.requestCode;
866 sourceRecord.resultTo = null;
867 if (resultRecord != null) {
868 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
869 }
870 if (sourceRecord.launchedFromUid == callingUid) {
Louis Chang54fbb052019-10-16 17:10:17 +0800871 // The new activity is being launched from the same uid as the previous activity
872 // in the flow, and asking to forward its result back to the previous. In this
873 // case the activity is serving as a trampoline between the two, so we also want
874 // to update its launchedFromPackage to be the same as the previous activity.
875 // Note that this is safe, since we know these two packages come from the same
876 // uid; the caller could just as well have supplied that same package name itself
877 // . This specifially deals with the case of an intent picker/chooser being
878 // launched in the app flow to redirect to an activity picked by the user, where
879 // we want the final activity to consider it to have been launched by the
880 // previous app activity.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800881 callingPackage = sourceRecord.launchedFromPackage;
882 }
883 }
884
885 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
886 // We couldn't find a class that can handle the given Intent.
887 // That's the end of that!
888 err = ActivityManager.START_INTENT_NOT_RESOLVED;
889 }
890
891 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
892 // We couldn't find the specific class specified in the Intent.
893 // Also the end of the line.
894 err = ActivityManager.START_CLASS_NOT_FOUND;
895 }
896
897 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
Louis Changcdec0802019-11-11 11:45:07 +0800898 && sourceRecord.getTask().voiceSession != null) {
Louis Chang54fbb052019-10-16 17:10:17 +0800899 // If this activity is being launched as part of a voice session, we need to ensure
900 // that it is safe to do so. If the upcoming activity will also be part of the voice
901 // session, we can only launch it if it has explicitly said it supports the VOICE
902 // category, or it is a part of the calling app.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800903 if ((launchFlags & FLAG_ACTIVITY_NEW_TASK) == 0
904 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
905 try {
906 intent.addCategory(Intent.CATEGORY_VOICE);
Wale Ogunwale906f9c62018-07-23 11:23:44 -0700907 if (!mService.getPackageManager().activitySupportsIntent(
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800908 intent.getComponent(), intent, resolvedType)) {
Louis Chang54fbb052019-10-16 17:10:17 +0800909 Slog.w(TAG, "Activity being started in current voice task does not support "
910 + "voice: " + intent);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800911 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
912 }
913 } catch (RemoteException e) {
914 Slog.w(TAG, "Failure checking voice capabilities", e);
915 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
916 }
917 }
918 }
919
920 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
921 // If the caller is starting a new voice session, just make sure the target
922 // is actually allowing it to run this way.
923 try {
Wale Ogunwale906f9c62018-07-23 11:23:44 -0700924 if (!mService.getPackageManager().activitySupportsIntent(intent.getComponent(),
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800925 intent, resolvedType)) {
926 Slog.w(TAG,
Louis Chang54fbb052019-10-16 17:10:17 +0800927 "Activity being started in new voice task does not support: " + intent);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800928 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
929 }
930 } catch (RemoteException e) {
931 Slog.w(TAG, "Failure checking voice capabilities", e);
932 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
933 }
934 }
935
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800936 final ActivityStack resultStack = resultRecord == null
937 ? null : resultRecord.getActivityStack();
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800938
Wale Ogunwale01d66562015-12-29 08:19:19 -0800939 if (err != START_SUCCESS) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800940 if (resultRecord != null) {
Andrii Kulian79d67982019-08-19 11:56:16 -0700941 resultRecord.sendResult(INVALID_UID, resultWho, requestCode, RESULT_CANCELED,
942 null /* data */);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800943 }
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +0100944 SafeActivityOptions.abort(options);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800945 return err;
946 }
947
948 boolean abort = !mSupervisor.checkStartAnyActivityPermission(intent, aInfo, resultWho,
Louis Chang54fbb052019-10-16 17:10:17 +0800949 requestCode, callingPid, callingUid, callingPackage, request.ignoreTargetSecurity,
Winson Chungc9804e72018-05-15 11:01:44 -0700950 inTask != null, callerApp, resultRecord, resultStack);
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700951 abort |= !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800952 callingPid, resolvedType, aInfo.applicationInfo);
Hai Zhangf4da9be2019-05-01 13:46:06 +0800953 abort |= !mService.getPermissionPolicyInternal().checkStartActivity(intent, callingUid,
954 callingPackage);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800955
Ricky Waib147fa12019-04-25 16:08:30 +0100956 boolean restrictedBgActivity = false;
Michal Karpinski8596ded2018-11-14 14:43:48 +0000957 if (!abort) {
Michal Karpinski4fd5b842019-01-28 15:13:32 +0000958 try {
Riddle Hsu2ca561b2019-10-08 21:58:58 +0800959 Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER,
Michal Karpinski4fd5b842019-01-28 15:13:32 +0000960 "shouldAbortBackgroundActivityStart");
Ricky Waib147fa12019-04-25 16:08:30 +0100961 restrictedBgActivity = shouldAbortBackgroundActivityStart(callingUid,
Ricky Waiaca8a772019-04-04 16:01:06 +0100962 callingPid, callingPackage, realCallingUid, realCallingPid, callerApp,
Louis Chang54fbb052019-10-16 17:10:17 +0800963 request.originatingPendingIntent, request.allowBackgroundActivityStart,
964 intent);
Michal Karpinski4fd5b842019-01-28 15:13:32 +0000965 } finally {
Riddle Hsu2ca561b2019-10-08 21:58:58 +0800966 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
Michal Karpinski4fd5b842019-01-28 15:13:32 +0000967 }
Michal Karpinski8596ded2018-11-14 14:43:48 +0000968 }
969
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +0100970 // Merge the two options bundles, while realCallerOptions takes precedence.
971 ActivityOptions checkedOptions = options != null
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700972 ? options.getOptions(intent, aInfo, callerApp, mSupervisor) : null;
Louis Chang54fbb052019-10-16 17:10:17 +0800973 if (request.allowPendingRemoteAnimationRegistryLookup) {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -0700974 checkedOptions = mService.getActivityStartController()
Jorim Jaggi6fa41c32018-04-23 18:35:00 +0200975 .getPendingRemoteAnimationRegistry()
976 .overrideOptionsIfNeeded(callingPackage, checkedOptions);
977 }
Wale Ogunwalea6191b42018-05-09 07:41:32 -0700978 if (mService.mController != null) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800979 try {
Louis Chang54fbb052019-10-16 17:10:17 +0800980 // The Intent we give to the watcher has the extra data stripped off, since it
981 // can contain private information.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800982 Intent watchIntent = intent.cloneFilter();
Wale Ogunwalea6191b42018-05-09 07:41:32 -0700983 abort |= !mService.mController.activityStarting(watchIntent,
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800984 aInfo.applicationInfo.packageName);
985 } catch (RemoteException e) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -0700986 mService.mController = null;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800987 }
988 }
989
Rubin Xu58d25992016-01-21 17:47:13 +0000990 mInterceptor.setStates(userId, realCallingPid, realCallingUid, startFlags, callingPackage);
Benjamin Franz563707b2017-06-29 15:06:13 +0100991 if (mInterceptor.intercept(intent, rInfo, aInfo, resolvedType, inTask, callingPid,
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +0100992 callingUid, checkedOptions)) {
Benjamin Franz563707b2017-06-29 15:06:13 +0100993 // activity start was intercepted, e.g. because the target user is currently in quiet
994 // mode (turn off work) or the target application is suspended
995 intent = mInterceptor.mIntent;
996 rInfo = mInterceptor.mRInfo;
997 aInfo = mInterceptor.mAInfo;
998 resolvedType = mInterceptor.mResolvedType;
999 inTask = mInterceptor.mInTask;
1000 callingPid = mInterceptor.mCallingPid;
1001 callingUid = mInterceptor.mCallingUid;
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01001002 checkedOptions = mInterceptor.mActivityOptions;
Benjamin Franz563707b2017-06-29 15:06:13 +01001003 }
1004
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001005 if (abort) {
1006 if (resultRecord != null) {
Andrii Kulian79d67982019-08-19 11:56:16 -07001007 resultRecord.sendResult(INVALID_UID, resultWho, requestCode, RESULT_CANCELED,
1008 null /* data */);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001009 }
Louis Chang54fbb052019-10-16 17:10:17 +08001010 // We pretend to the caller that it was really started, but they will just get a
1011 // cancel result.
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01001012 ActivityOptions.abort(checkedOptions);
Bryce Leef9d49542017-06-26 16:27:32 -07001013 return START_ABORTED;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001014 }
1015
1016 // If permissions need a review before any of the app components can run, we
1017 // launch the review activity and pass a pending intent to start the activity
1018 // we are to launching now after the review is completed.
Philip P. Moltmann6c644e62018-07-18 15:41:24 -07001019 if (aInfo != null) {
Wale Ogunwale906f9c62018-07-23 11:23:44 -07001020 if (mService.getPackageManagerInternalLocked().isPermissionsReviewRequired(
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001021 aInfo.packageName, userId)) {
Louis Chang54fbb052019-10-16 17:10:17 +08001022 final IIntentSender target = mService.getIntentSenderLocked(
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001023 ActivityManager.INTENT_SENDER_ACTIVITY, callingPackage,
1024 callingUid, userId, null, null, 0, new Intent[]{intent},
1025 new String[]{resolvedType}, PendingIntent.FLAG_CANCEL_CURRENT
1026 | PendingIntent.FLAG_ONE_SHOT, null);
1027
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001028 Intent newIntent = new Intent(Intent.ACTION_REVIEW_PERMISSIONS);
Philip P. Moltmannc3e66d02019-01-31 15:56:18 -08001029
1030 int flags = intent.getFlags();
1031 flags |= Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS;
1032
1033 /*
1034 * Prevent reuse of review activity: Each app needs their own review activity. By
1035 * default activities launched with NEW_TASK or NEW_DOCUMENT try to reuse activities
1036 * with the same launch parameters (extras are ignored). Hence to avoid possible
1037 * reuse force a new activity via the MULTIPLE_TASK flag.
1038 *
1039 * Activities that are not launched with NEW_TASK or NEW_DOCUMENT are not re-used,
1040 * hence no need to add the flag in this case.
1041 */
1042 if ((flags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_NEW_DOCUMENT)) != 0) {
1043 flags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1044 }
1045 newIntent.setFlags(flags);
1046
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001047 newIntent.putExtra(Intent.EXTRA_PACKAGE_NAME, aInfo.packageName);
1048 newIntent.putExtra(Intent.EXTRA_INTENT, new IntentSender(target));
1049 if (resultRecord != null) {
1050 newIntent.putExtra(Intent.EXTRA_RESULT_NEEDED, true);
1051 }
1052 intent = newIntent;
1053
1054 resolvedType = null;
1055 callingUid = realCallingUid;
1056 callingPid = realCallingPid;
1057
Svet Ganovcbcbf662018-05-10 17:25:29 -07001058 rInfo = mSupervisor.resolveIntent(intent, resolvedType, userId, 0,
Patrick Baumann31426b22018-05-21 13:46:40 -07001059 computeResolveFilterUid(
Louis Chang54fbb052019-10-16 17:10:17 +08001060 callingUid, realCallingUid, request.filterCallingUid));
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001061 aInfo = mSupervisor.resolveActivity(intent, rInfo, startFlags,
1062 null /*profilerInfo*/);
1063
1064 if (DEBUG_PERMISSIONS_REVIEW) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08001065 final ActivityStack focusedStack =
1066 mRootActivityContainer.getTopDisplayFocusedStack();
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001067 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true,
1068 true, false) + "} from uid " + callingUid + " on display "
Andrii Kulian52d255c2018-07-13 11:32:19 -07001069 + (focusedStack == null ? DEFAULT_DISPLAY : focusedStack.mDisplayId));
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001070 }
1071 }
1072 }
1073
1074 // If we have an ephemeral app, abort the process of launching the resolved intent.
1075 // Instead, launch the ephemeral installer. Once the installer is finished, it
1076 // starts either the intent we resolved here [on install error] or the ephemeral
1077 // app [on install success].
Patrick Baumanna89a1722018-02-07 15:26:52 -08001078 if (rInfo != null && rInfo.auxiliaryInfo != null) {
Louis Chang54fbb052019-10-16 17:10:17 +08001079 intent = createLaunchIntent(rInfo.auxiliaryInfo, request.ephemeralIntent,
Chad Brubaker06068612017-04-06 09:43:47 -07001080 callingPackage, verificationBundle, resolvedType, userId);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001081 resolvedType = null;
1082 callingUid = realCallingUid;
1083 callingPid = realCallingPid;
1084
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001085 aInfo = mSupervisor.resolveActivity(intent, rInfo, startFlags, null /*profilerInfo*/);
1086 }
1087
Louis Chang54fbb052019-10-16 17:10:17 +08001088 final ActivityRecord r = new ActivityRecord(mService, callerApp, callingPid, callingUid,
Wale Ogunwalef6733932018-06-27 05:14:34 -07001089 callingPackage, intent, resolvedType, aInfo, mService.getGlobalConfiguration(),
Louis Chang54fbb052019-10-16 17:10:17 +08001090 resultRecord, resultWho, requestCode, request.componentSpecified,
1091 voiceSession != null, mSupervisor, checkedOptions, sourceRecord);
1092 mLastStartActivityRecord = r;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001093
1094 if (r.appTimeTracker == null && sourceRecord != null) {
1095 // If the caller didn't specify an explicit time tracker, we want to continue
1096 // tracking under any it has.
1097 r.appTimeTracker = sourceRecord.appTimeTracker;
1098 }
1099
Wale Ogunwaled32da472018-11-16 07:19:28 -08001100 final ActivityStack stack = mRootActivityContainer.getTopDisplayFocusedStack();
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01001101
1102 // If we are starting an activity that is not from the same uid as the currently resumed
1103 // one, check whether app switches are allowed.
Bryce Leec4ab62a2018-03-05 14:19:26 -08001104 if (voiceSession == null && (stack.getResumedActivity() == null
1105 || stack.getResumedActivity().info.applicationInfo.uid != realCallingUid)) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001106 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001107 realCallingPid, realCallingUid, "Activity start")) {
Alan Stokes07389b62019-05-20 15:22:54 +01001108 if (!(restrictedBgActivity && handleBackgroundActivityAbort(r))) {
Ricky Waib147fa12019-04-25 16:08:30 +01001109 mController.addPendingActivityLaunch(new PendingActivityLaunch(r,
Wale Ogunwale586a8ee2019-06-04 13:44:14 +00001110 sourceRecord, startFlags, stack, callerApp));
Ricky Waib147fa12019-04-25 16:08:30 +01001111 }
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01001112 ActivityOptions.abort(checkedOptions);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001113 return ActivityManager.START_SWITCHES_CANCELED;
1114 }
1115 }
1116
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001117 mService.onStartActivitySetDidAppSwitch();
Bryce Leed3624e12017-11-30 08:51:45 -08001118 mController.doPendingActivityLaunches(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001119
Louis Chang54fbb052019-10-16 17:10:17 +08001120 mLastStartActivityResult = startActivityUnchecked(r, sourceRecord, voiceSession,
1121 request.voiceInteractor, startFlags, true /* doResume */, checkedOptions, inTask,
1122 restrictedBgActivity);
1123
1124 if (request.outActivity != null) {
1125 request.outActivity[0] = mLastStartActivityRecord;
1126 }
1127
1128 return getExternalResult(mLastStartActivityResult);
1129 }
1130
1131 /**
1132 * Return true if background activity is really aborted.
1133 *
1134 * TODO(b/131748165): Refactor the logic so we don't need to call this method everywhere.
1135 */
1136 private boolean handleBackgroundActivityAbort(ActivityRecord r) {
1137 // TODO(b/131747138): Remove toast and refactor related code in R release.
1138 final boolean abort = !mService.isBackgroundActivityStartsEnabled();
1139 if (!abort) {
1140 return false;
1141 }
1142 final ActivityRecord resultRecord = r.resultTo;
1143 final String resultWho = r.resultWho;
1144 int requestCode = r.requestCode;
1145 if (resultRecord != null) {
1146 resultRecord.sendResult(INVALID_UID, resultWho, requestCode, RESULT_CANCELED,
1147 null /* data */);
1148 }
1149 // We pretend to the caller that it was really started to make it backward compatible, but
1150 // they will just get a cancel result.
1151 ActivityOptions.abort(r.pendingOptions);
1152 return true;
1153 }
1154
1155 static int getExternalResult(int result) {
1156 // Aborted results are treated as successes externally, but we must track them internally.
1157 return result != START_ABORTED ? result : START_SUCCESS;
1158 }
1159
1160 /**
1161 * Called when execution is complete. Sets state indicating completion and proceeds with
1162 * recycling if appropriate.
1163 */
1164 private void onExecutionComplete() {
1165 mController.onExecutionComplete(this);
Wale Ogunwalecc25a8a2016-01-23 14:31:37 -08001166 }
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001167
Ricky Waiaca8a772019-04-04 16:01:06 +01001168 boolean shouldAbortBackgroundActivityStart(int callingUid, int callingPid,
Michal Karpinski9cbb20b2019-02-05 17:31:50 +00001169 final String callingPackage, int realCallingUid, int realCallingPid,
1170 WindowProcessController callerApp, PendingIntentRecord originatingPendingIntent,
1171 boolean allowBackgroundActivityStart, Intent intent) {
Michal Karpinski8596ded2018-11-14 14:43:48 +00001172 // don't abort for the most important UIDs
Alan Stokes2f4a4ed2019-05-08 16:56:45 +01001173 final int callingAppId = UserHandle.getAppId(callingUid);
1174 if (callingUid == Process.ROOT_UID || callingAppId == Process.SYSTEM_UID
1175 || callingAppId == Process.NFC_UID) {
Michal Karpinski8596ded2018-11-14 14:43:48 +00001176 return false;
1177 }
Alan Stokeseea8d3e2019-04-10 17:37:25 +01001178 // don't abort if the callingUid has a visible window or is a persistent system process
Riddle Hsua0536432019-02-16 00:38:59 +08001179 final int callingUidProcState = mService.getUidState(callingUid);
Michal Karpinski4fd5b842019-01-28 15:13:32 +00001180 final boolean callingUidHasAnyVisibleWindow =
1181 mService.mWindowManager.mRoot.isAnyNonToastWindowVisibleForUid(callingUid);
1182 final boolean isCallingUidForeground = callingUidHasAnyVisibleWindow
Amith Yamasanif235d0b2019-03-20 22:49:43 -07001183 || callingUidProcState == ActivityManager.PROCESS_STATE_TOP
1184 || callingUidProcState == ActivityManager.PROCESS_STATE_BOUND_TOP;
Alan Stokes2f4a4ed2019-05-08 16:56:45 +01001185 final boolean isCallingUidPersistentSystemProcess =
1186 callingUidProcState <= ActivityManager.PROCESS_STATE_PERSISTENT_UI;
Alan Stokeseea8d3e2019-04-10 17:37:25 +01001187 if (callingUidHasAnyVisibleWindow || isCallingUidPersistentSystemProcess) {
Michal Karpinski8596ded2018-11-14 14:43:48 +00001188 return false;
1189 }
Michal Karpinskiac116df2018-12-10 17:51:42 +00001190 // take realCallingUid into consideration
Michal Karpinski4fd5b842019-01-28 15:13:32 +00001191 final int realCallingUidProcState = (callingUid == realCallingUid)
1192 ? callingUidProcState
Riddle Hsua0536432019-02-16 00:38:59 +08001193 : mService.getUidState(realCallingUid);
Michal Karpinski4fd5b842019-01-28 15:13:32 +00001194 final boolean realCallingUidHasAnyVisibleWindow = (callingUid == realCallingUid)
1195 ? callingUidHasAnyVisibleWindow
1196 : mService.mWindowManager.mRoot.isAnyNonToastWindowVisibleForUid(realCallingUid);
1197 final boolean isRealCallingUidForeground = (callingUid == realCallingUid)
1198 ? isCallingUidForeground
1199 : realCallingUidHasAnyVisibleWindow
1200 || realCallingUidProcState == ActivityManager.PROCESS_STATE_TOP;
Alan Stokes2f4a4ed2019-05-08 16:56:45 +01001201 final int realCallingAppId = UserHandle.getAppId(realCallingUid);
Michal Karpinski4fd5b842019-01-28 15:13:32 +00001202 final boolean isRealCallingUidPersistentSystemProcess = (callingUid == realCallingUid)
1203 ? isCallingUidPersistentSystemProcess
Alan Stokes2f4a4ed2019-05-08 16:56:45 +01001204 : (realCallingAppId == Process.SYSTEM_UID)
Michal Karpinski4fd5b842019-01-28 15:13:32 +00001205 || realCallingUidProcState <= ActivityManager.PROCESS_STATE_PERSISTENT_UI;
Michal Karpinskiac116df2018-12-10 17:51:42 +00001206 if (realCallingUid != callingUid) {
Alan Stokes6ac9efd2019-05-09 12:50:37 +00001207 // don't abort if the realCallingUid has a visible window
1208 if (realCallingUidHasAnyVisibleWindow) {
Michal Karpinskiac116df2018-12-10 17:51:42 +00001209 return false;
1210 }
1211 // if the realCallingUid is a persistent system process, abort if the IntentSender
1212 // wasn't whitelisted to start an activity
Michal Karpinskid0162852019-01-15 16:05:25 +00001213 if (isRealCallingUidPersistentSystemProcess && allowBackgroundActivityStart) {
Michal Karpinskiac116df2018-12-10 17:51:42 +00001214 return false;
1215 }
Michal Karpinskida34cd42019-04-02 19:46:52 +01001216 // don't abort if the realCallingUid is an associated companion app
1217 if (mService.isAssociatedCompanionApp(UserHandle.getUserId(realCallingUid),
1218 realCallingUid)) {
1219 return false;
1220 }
Michal Karpinskiac116df2018-12-10 17:51:42 +00001221 }
Michal Karpinski7b97a022018-12-14 15:17:29 +00001222 // don't abort if the callingUid has START_ACTIVITIES_FROM_BACKGROUND permission
1223 if (mService.checkPermission(START_ACTIVITIES_FROM_BACKGROUND, callingPid, callingUid)
1224 == PERMISSION_GRANTED) {
1225 return false;
1226 }
Michal Karpinski82bb5902018-11-28 15:52:52 +00001227 // don't abort if the caller has the same uid as the recents component
1228 if (mSupervisor.mRecentTasks.isCallerRecents(callingUid)) {
1229 return false;
1230 }
Ricky Wai96f5c352019-04-10 18:40:17 +01001231 // don't abort if the callingUid is the device owner
1232 if (mService.isDeviceOwner(callingUid)) {
Michal Karpinski302dcec2019-02-01 11:48:25 +00001233 return false;
1234 }
Ricky Wai96f5c352019-04-10 18:40:17 +01001235 // don't abort if the callingUid has companion device
Ricky Wai2452e2d2019-03-18 19:19:08 +00001236 final int callingUserId = UserHandle.getUserId(callingUid);
Michal Karpinskida34cd42019-04-02 19:46:52 +01001237 if (mService.isAssociatedCompanionApp(callingUserId, callingUid)) {
Ricky Wai2452e2d2019-03-18 19:19:08 +00001238 return false;
1239 }
Michal Karpinski2e0aad22019-04-12 16:22:55 +01001240 // If we don't have callerApp at this point, no caller was provided to startActivity().
1241 // That's the case for PendingIntent-based starts, since the creator's process might not be
1242 // up and alive. If that's the case, we retrieve the WindowProcessController for the send()
1243 // caller, so that we can make the decision based on its foreground/whitelisted state.
1244 int callerAppUid = callingUid;
1245 if (callerApp == null) {
1246 callerApp = mService.getProcessController(realCallingPid, realCallingUid);
1247 callerAppUid = realCallingUid;
1248 }
1249 // don't abort if the callerApp or other processes of that uid are whitelisted in any way
1250 if (callerApp != null) {
1251 // first check the original calling process
1252 if (callerApp.areBackgroundActivityStartsAllowed()) {
1253 return false;
1254 }
1255 // only if that one wasn't whitelisted, check the other ones
1256 final ArraySet<WindowProcessController> uidProcesses =
1257 mService.mProcessMap.getProcesses(callerAppUid);
1258 if (uidProcesses != null) {
1259 for (int i = uidProcesses.size() - 1; i >= 0; i--) {
1260 final WindowProcessController proc = uidProcesses.valueAt(i);
1261 if (proc != callerApp && proc.areBackgroundActivityStartsAllowed()) {
1262 return false;
1263 }
1264 }
1265 }
1266 }
Michal Karpinski15486842019-04-25 17:33:42 +01001267 // don't abort if the callingUid has SYSTEM_ALERT_WINDOW permission
1268 if (mService.hasSystemAlertWindowPermission(callingUid, callingPid, callingPackage)) {
1269 Slog.w(TAG, "Background activity start for " + callingPackage
1270 + " allowed because SYSTEM_ALERT_WINDOW permission is granted.");
1271 return false;
1272 }
Michal Karpinskic02364c2019-01-22 13:00:04 +00001273 // anything that has fallen through would currently be aborted
1274 Slog.w(TAG, "Background activity start [callingPackage: " + callingPackage
Michal Karpinskid0162852019-01-15 16:05:25 +00001275 + "; callingUid: " + callingUid
1276 + "; isCallingUidForeground: " + isCallingUidForeground
1277 + "; isCallingUidPersistentSystemProcess: " + isCallingUidPersistentSystemProcess
1278 + "; realCallingUid: " + realCallingUid
1279 + "; isRealCallingUidForeground: " + isRealCallingUidForeground
1280 + "; isRealCallingUidPersistentSystemProcess: "
Ricky Waiaca8a772019-04-04 16:01:06 +01001281 + isRealCallingUidPersistentSystemProcess
Michal Karpinskid0162852019-01-15 16:05:25 +00001282 + "; originatingPendingIntent: " + originatingPendingIntent
1283 + "; isBgStartWhitelisted: " + allowBackgroundActivityStart
1284 + "; intent: " + intent
Michal Karpinski9cbb20b2019-02-05 17:31:50 +00001285 + "; callerApp: " + callerApp
Michal Karpinskid0162852019-01-15 16:05:25 +00001286 + "]");
Michal Karpinski4fd5b842019-01-28 15:13:32 +00001287 // log aborted activity start to TRON
1288 if (mService.isActivityStartsLoggingEnabled()) {
1289 mSupervisor.getActivityMetricsLogger().logAbortedBgActivityStart(intent, callerApp,
1290 callingUid, callingPackage, callingUidProcState, callingUidHasAnyVisibleWindow,
1291 realCallingUid, realCallingUidProcState, realCallingUidHasAnyVisibleWindow,
Michal Karpinski201bc0c2018-07-20 15:32:00 +01001292 (originatingPendingIntent != null));
Michal Karpinski201bc0c2018-07-20 15:32:00 +01001293 }
Michal Karpinski4fd5b842019-01-28 15:13:32 +00001294 return true;
Michal Karpinski201bc0c2018-07-20 15:32:00 +01001295 }
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01001296
Bryce Leeaa5e8c32017-03-01 16:01:06 -08001297 /**
1298 * Creates a launch intent for the given auxiliary resolution data.
1299 */
Patrick Baumann577d4022018-01-31 16:55:10 +00001300 private @NonNull Intent createLaunchIntent(@Nullable AuxiliaryResolveInfo auxiliaryResponse,
Chad Brubaker06068612017-04-06 09:43:47 -07001301 Intent originalIntent, String callingPackage, Bundle verificationBundle,
1302 String resolvedType, int userId) {
Patrick Baumann577d4022018-01-31 16:55:10 +00001303 if (auxiliaryResponse != null && auxiliaryResponse.needsPhaseTwo) {
Todd Kennedye9910222017-02-21 16:00:11 -08001304 // request phase two resolution
Wale Ogunwale906f9c62018-07-23 11:23:44 -07001305 mService.getPackageManagerInternalLocked().requestInstantAppResolutionPhaseTwo(
Chad Brubaker06068612017-04-06 09:43:47 -07001306 auxiliaryResponse, originalIntent, resolvedType, callingPackage,
1307 verificationBundle, userId);
Todd Kennedye9910222017-02-21 16:00:11 -08001308 }
Todd Kennedydfc27c62017-05-17 15:32:10 -07001309 return InstantAppResolver.buildEphemeralInstallerIntent(
Patrick Baumann577d4022018-01-31 16:55:10 +00001310 originalIntent,
1311 InstantAppResolver.sanitizeIntent(originalIntent),
1312 auxiliaryResponse == null ? null : auxiliaryResponse.failureIntent,
1313 callingPackage,
1314 verificationBundle,
1315 resolvedType,
1316 userId,
1317 auxiliaryResponse == null ? null : auxiliaryResponse.installFailureActivity,
1318 auxiliaryResponse == null ? null : auxiliaryResponse.token,
1319 auxiliaryResponse != null && auxiliaryResponse.needsPhaseTwo,
1320 auxiliaryResponse == null ? null : auxiliaryResponse.filters);
Todd Kennedye9910222017-02-21 16:00:11 -08001321 }
1322
Riddle Hsu16567132018-08-16 21:37:47 +08001323 void postStartActivityProcessing(ActivityRecord r, int result,
1324 ActivityStack startedActivityStack) {
Winson Chunge219ae12019-07-18 13:43:23 -07001325 if (!ActivityManager.isStartResultSuccessful(result)) {
1326 if (mFrozeTaskList) {
1327 // If we specifically froze the task list as part of starting an activity, then
1328 // reset the frozen list state if it failed to start. This is normally otherwise
1329 // called when the freeze-timeout has elapsed.
1330 mSupervisor.mRecentTasks.resetFreezeTaskListReorderingOnTimeout();
1331 }
1332 }
Bryce Lee7f936862017-05-09 15:33:18 -07001333 if (ActivityManager.isStartResultFatalError(result)) {
Wale Ogunwalecc25a8a2016-01-23 14:31:37 -08001334 return;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001335 }
Filip Gruszczynski303210b2016-01-08 16:28:08 -08001336
Chong Zhang5022da32016-06-21 16:31:37 -07001337 // We're waiting for an activity launch to finish, but that activity simply
Bryce Lee5f0e28f2018-01-30 16:00:03 -08001338 // brought another activity to front. We must also handle the case where the task is already
1339 // in the front as a result of the trampoline activity being in the same task (it will be
Louis Chang54fbb052019-10-16 17:10:17 +08001340 // considered focused as the trampoline will be finished). Let them know about this, so
1341 // it waits for the new activity to become visible instead, {@link #waitResultIfNeeded}.
Bryce Lee5f0e28f2018-01-30 16:00:03 -08001342 mSupervisor.reportWaitingActivityLaunchedIfNeeded(r, result);
Chong Zhang5022da32016-06-21 16:31:37 -07001343
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001344 if (startedActivityStack == null) {
1345 return;
1346 }
1347
Wale Ogunwaleac36e4d2017-11-29 13:30:26 -08001348 final int clearTaskFlags = FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK;
1349 boolean clearedTask = (mLaunchFlags & clearTaskFlags) == clearTaskFlags
1350 && mReuseTask != null;
Wale Ogunwale7d7973a2018-04-05 10:25:59 -07001351 if (result == START_TASK_TO_FRONT || result == START_DELIVERED_TO_TOP || clearedTask) {
1352 // The activity was already running so it wasn't started, but either brought to the
1353 // front or the new intent was delivered to it since it was already in front. Notify
1354 // anyone interested in this piece of information.
1355 switch (startedActivityStack.getWindowingMode()) {
1356 case WINDOWING_MODE_PINNED:
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07001357 mService.getTaskChangeNotificationController().notifyPinnedActivityRestartAttempt(
Wale Ogunwale7d7973a2018-04-05 10:25:59 -07001358 clearedTask);
1359 break;
1360 case WINDOWING_MODE_SPLIT_SCREEN_PRIMARY:
Louis Changbd48dca2018-08-29 17:44:34 +08001361 final ActivityStack homeStack =
1362 startedActivityStack.getDisplay().getHomeStack();
Wale Ogunwale7d7973a2018-04-05 10:25:59 -07001363 if (homeStack != null && homeStack.shouldBeVisible(null /* starting */)) {
1364 mService.mWindowManager.showRecentApps();
1365 }
1366 break;
1367 }
Wale Ogunwalecc25a8a2016-01-23 14:31:37 -08001368 }
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001369 }
1370
Svet Ganovcbcbf662018-05-10 17:25:29 -07001371 /**
1372 * Compute the logical UID based on which the package manager would filter
1373 * app components i.e. based on which the instant app policy would be applied
1374 * because it is the logical calling UID.
1375 *
1376 * @param customCallingUid The UID on whose behalf to make the call.
1377 * @param actualCallingUid The UID actually making the call.
Patrick Baumann31426b22018-05-21 13:46:40 -07001378 * @param filterCallingUid The UID to be used to filter for instant apps.
Svet Ganovcbcbf662018-05-10 17:25:29 -07001379 * @return The logical UID making the call.
1380 */
Patrick Baumann31426b22018-05-21 13:46:40 -07001381 static int computeResolveFilterUid(int customCallingUid, int actualCallingUid,
1382 int filterCallingUid) {
1383 return filterCallingUid != UserHandle.USER_NULL
1384 ? filterCallingUid
1385 : (customCallingUid >= 0 ? customCallingUid : actualCallingUid);
Svet Ganovcbcbf662018-05-10 17:25:29 -07001386 }
1387
Louis Chang54fbb052019-10-16 17:10:17 +08001388 /**
1389 * Start an activity while most of preliminary checks has been done and caller has been
1390 * confirmed that holds necessary permissions to do so.
1391 * Here also ensures that the starting activity is removed if the start wasn't successful.
1392 */
1393 private int startActivityUnchecked(final ActivityRecord r, ActivityRecord sourceRecord,
Bryce Leedaa91e42017-12-06 14:13:01 -08001394 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Louis Changcdec0802019-11-11 11:45:07 +08001395 int startFlags, boolean doResume, ActivityOptions options, Task inTask,
Louis Chang54fbb052019-10-16 17:10:17 +08001396 boolean restrictedBgActivity) {
Bryce Leeaa5e8c32017-03-01 16:01:06 -08001397 int result = START_CANCELED;
Riddle Hsu16567132018-08-16 21:37:47 +08001398 final ActivityStack startedActivityStack;
Bryce Leeaa5e8c32017-03-01 16:01:06 -08001399 try {
Riddle Hsua0022cd2019-09-09 21:12:41 +08001400 mService.deferWindowLayout();
Riddle Hsu2ca561b2019-10-08 21:58:58 +08001401 Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "startActivityInner");
1402 result = startActivityInner(r, sourceRecord, voiceSession, voiceInteractor,
Louis Chang54fbb052019-10-16 17:10:17 +08001403 startFlags, doResume, options, inTask, restrictedBgActivity);
Bryce Leeaa5e8c32017-03-01 16:01:06 -08001404 } finally {
Riddle Hsu2ca561b2019-10-08 21:58:58 +08001405 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
1406 startedActivityStack = handleStartResult(r, result);
Riddle Hsua0022cd2019-09-09 21:12:41 +08001407 mService.continueWindowLayout();
Bryce Leeaa5e8c32017-03-01 16:01:06 -08001408 }
1409
Riddle Hsu16567132018-08-16 21:37:47 +08001410 postStartActivityProcessing(r, result, startedActivityStack);
Bryce Leeaa5e8c32017-03-01 16:01:06 -08001411
1412 return result;
1413 }
1414
Ricky Waib147fa12019-04-25 16:08:30 +01001415 /**
Riddle Hsu2ca561b2019-10-08 21:58:58 +08001416 * If the start result is success, ensure that the configuration of the started activity matches
1417 * the current display. Otherwise clean up unassociated containers to avoid leakage.
1418 *
1419 * @return the stack where the successful started activity resides.
1420 */
1421 private @Nullable ActivityStack handleStartResult(@NonNull ActivityRecord started, int result) {
1422 final ActivityStack currentStack = started.getActivityStack();
1423 ActivityStack startedActivityStack = currentStack != null ? currentStack : mTargetStack;
1424
1425 if (ActivityManager.isStartResultSuccessful(result)) {
1426 if (startedActivityStack != null) {
1427 // If there is no state change (e.g. a resumed activity is reparented to top of
1428 // another display) to trigger a visibility/configuration checking, we have to
1429 // update the configuration for changing to different display.
1430 final ActivityRecord currentTop = startedActivityStack.topRunningActivityLocked();
1431 if (currentTop != null && currentTop.shouldUpdateConfigForDisplayChanged()) {
1432 mRootActivityContainer.ensureVisibilityAndConfig(
1433 currentTop, currentTop.getDisplayId(),
1434 true /* markFrozenIfConfigChanged */, false /* deferResume */);
1435 }
1436 }
1437 return startedActivityStack;
1438 }
1439
1440 // If we are not able to proceed, disassociate the activity from the task. Leaving an
1441 // activity in an incomplete state can lead to issues, such as performing operations
1442 // without a window container.
1443 final ActivityStack stack = mStartActivity.getActivityStack();
1444 if (stack != null) {
1445 mStartActivity.finishIfPossible("startActivity", true /* oomAdj */);
1446 }
1447
1448 // Stack should also be detached from display and be removed if it's empty.
1449 if (startedActivityStack != null && startedActivityStack.isAttached()
1450 && startedActivityStack.numActivities() == 0
1451 && !startedActivityStack.isActivityTypeHome()) {
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -07001452 startedActivityStack.removeIfPossible();
Riddle Hsu2ca561b2019-10-08 21:58:58 +08001453 startedActivityStack = null;
1454 }
1455 return startedActivityStack;
1456 }
1457
1458 /**
Louis Chang54fbb052019-10-16 17:10:17 +08001459 * Start an activity and determine if the activity should be adding to the top of an existing
1460 * task or delivered new intent to an existing activity. Also manipulating the activity task
1461 * onto requested or valid stack/display.
Ricky Waib147fa12019-04-25 16:08:30 +01001462 *
Louis Chang54fbb052019-10-16 17:10:17 +08001463 * Note: This method should only be called from {@link #startActivityUnchecked}.
Ricky Waib147fa12019-04-25 16:08:30 +01001464 */
Riddle Hsu2ca561b2019-10-08 21:58:58 +08001465 private int startActivityInner(final ActivityRecord r, ActivityRecord sourceRecord,
Wale Ogunwale01d66562015-12-29 08:19:19 -08001466 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Louis Changcdec0802019-11-11 11:45:07 +08001467 int startFlags, boolean doResume, ActivityOptions options, Task inTask,
Louis Chang54fbb052019-10-16 17:10:17 +08001468 boolean restrictedBgActivity) {
Wale Ogunwale01d66562015-12-29 08:19:19 -08001469 setInitialState(r, options, inTask, doResume, startFlags, sourceRecord, voiceSession,
Ricky Waib147fa12019-04-25 16:08:30 +01001470 voiceInteractor, restrictedBgActivity);
1471
Louis Chang39ba54b2018-10-18 11:28:57 +08001472 final int preferredWindowingMode = mLaunchParams.mWindowingMode;
Wale Ogunwale01d66562015-12-29 08:19:19 -08001473
Louis Chang6fb1e842018-12-03 16:07:50 +08001474 computeLaunchingTaskFlags();
1475
1476 computeSourceStack();
1477
1478 mIntent.setFlags(mLaunchFlags);
1479
Louis Changcdec0802019-11-11 11:45:07 +08001480 final Task reusedTask = getReusableTask();
Louis Changf7dd7f22019-11-05 11:59:56 +08001481 mSupervisor.getLaunchParamsController().calculate(reusedTask != null ? reusedTask : mInTask,
Louis Chang6fb1e842018-12-03 16:07:50 +08001482 r.info.windowLayout, r, sourceRecord, options, PHASE_BOUNDS, mLaunchParams);
1483 mPreferredDisplayId =
1484 mLaunchParams.hasPreferredDisplay() ? mLaunchParams.mPreferredDisplayId
1485 : DEFAULT_DISPLAY;
1486
Winson Chunge219ae12019-07-18 13:43:23 -07001487 // If requested, freeze the task list
1488 if (mOptions != null && mOptions.freezeRecentTasksReordering()
1489 && mSupervisor.mRecentTasks.isCallerRecents(r.launchedFromUid)
1490 && !mSupervisor.mRecentTasks.isFreezeTaskListReorderingSet()) {
1491 mFrozeTaskList = true;
1492 mSupervisor.mRecentTasks.setFreezeTaskListReordering();
1493 }
1494
Louis Changbde91e92019-08-16 17:19:47 +08001495 // Compute if there is an existing task that should be used for.
Louis Changcdec0802019-11-11 11:45:07 +08001496 final Task targetTask = reusedTask != null ? reusedTask : computeTargetTask();
Louis Changbde91e92019-08-16 17:19:47 +08001497 final boolean newTask = targetTask == null;
1498
1499 // Check if starting activity on given task or on a new task is allowed.
1500 int startResult = isAllowedToStart(r, newTask, targetTask);
1501 if (startResult != START_SUCCESS) {
1502 return startResult;
Louis Changbd48dca2018-08-29 17:44:34 +08001503 }
1504
Wale Ogunwale21e06482019-11-18 05:14:15 -08001505 final ActivityRecord targetTaskTop = newTask
1506 ? null : targetTask.getTopNonFinishingActivity();
Louis Changbde91e92019-08-16 17:19:47 +08001507 if (targetTaskTop != null) {
1508 // Recycle the target task for this launch.
Louis Changf7dd7f22019-11-05 11:59:56 +08001509 startResult = recycleTask(targetTask, targetTaskTop, reusedTask);
Louis Changbde91e92019-08-16 17:19:47 +08001510 if (startResult != START_SUCCESS) {
1511 return startResult;
Wale Ogunwale01d66562015-12-29 08:19:19 -08001512 }
Louis Changf7dd7f22019-11-05 11:59:56 +08001513 } else {
1514 mAddingToTask = true;
Wale Ogunwale01d66562015-12-29 08:19:19 -08001515 }
1516
1517 // If the activity being launched is the same as the one currently at the top, then
1518 // we need to check if it should only be launched once.
Wale Ogunwaled32da472018-11-16 07:19:28 -08001519 final ActivityStack topStack = mRootActivityContainer.getTopDisplayFocusedStack();
Louis Changbde91e92019-08-16 17:19:47 +08001520 startResult = deliverToCurrentTopIfNeeded(topStack);
1521 if (startResult != START_SUCCESS) {
1522 return startResult;
Wale Ogunwale01d66562015-12-29 08:19:19 -08001523 }
1524
Louis Changbde91e92019-08-16 17:19:47 +08001525 if (mTargetStack == null) {
1526 mTargetStack = computeStackFocus(mStartActivity, true, mLaunchFlags, mOptions);
Wale Ogunwale01d66562015-12-29 08:19:19 -08001527 }
Louis Changbde91e92019-08-16 17:19:47 +08001528 if (newTask) {
Louis Changcdec0802019-11-11 11:45:07 +08001529 final Task taskToAffiliate = (mLaunchTaskBehind && mSourceRecord != null)
1530 ? mSourceRecord.getTask() : null;
Louis Changbde91e92019-08-16 17:19:47 +08001531 setNewTask(taskToAffiliate);
Louis Changa3e6b892019-09-16 10:39:00 +08001532 if (mService.getLockTaskController().isLockTaskModeViolation(
Louis Changcdec0802019-11-11 11:45:07 +08001533 mStartActivity.getTask())) {
Louis Changa3e6b892019-09-16 10:39:00 +08001534 Slog.e(TAG, "Attempted Lock Task Mode violation mStartActivity=" + mStartActivity);
1535 return START_RETURN_LOCK_TASK_MODE_VIOLATION;
1536 }
Louis Changbde91e92019-08-16 17:19:47 +08001537 } else if (mAddingToTask) {
1538 addOrReparentStartingActivity(targetTask, "adding to task");
1539 }
1540
1541 if (!mAvoidMoveToFront && mDoResume) {
1542 mTargetStack.moveToFront("reuseOrNewTask");
Chong Zhang6cda19c2016-06-14 19:07:56 -07001543 }
Wale Ogunwale01d66562015-12-29 08:19:19 -08001544
Wale Ogunwale586a8ee2019-06-04 13:44:14 +00001545 mService.mUgmInternal.grantUriPermissionFromIntent(mCallingUid, mStartActivity.packageName,
1546 mIntent, mStartActivity.getUriPermissionsLocked(), mStartActivity.mUserId);
Patrick Baumannde37e432019-08-28 09:51:29 -07001547 mService.getPackageManagerInternalLocked().grantImplicitAccess(
Andrii Kulianeceebbf2019-06-26 17:36:51 -07001548 mStartActivity.mUserId, mIntent,
Patrick Baumannb6e72972019-09-20 07:54:47 -07001549 mCallingUid,
Patrick Baumannde37e432019-08-28 09:51:29 -07001550 UserHandle.getAppId(mStartActivity.info.applicationInfo.uid)
1551 );
Wale Ogunwale01d66562015-12-29 08:19:19 -08001552 if (newTask) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001553 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, mStartActivity.mUserId,
Louis Changcdec0802019-11-11 11:45:07 +08001554 mStartActivity.getTask().mTaskId);
Wale Ogunwale01d66562015-12-29 08:19:19 -08001555 }
Andrii Kulian79d67982019-08-19 11:56:16 -07001556 mStartActivity.logStartActivity(
Louis Changcdec0802019-11-11 11:45:07 +08001557 EventLogTags.AM_CREATE_ACTIVITY, mStartActivity.getTask());
Wale Ogunwale01d66562015-12-29 08:19:19 -08001558 mTargetStack.mLastPausedActivity = null;
Wei Wang98f03f92016-05-18 11:32:52 -07001559
Wale Ogunwaled32da472018-11-16 07:19:28 -08001560 mRootActivityContainer.sendPowerHintForLaunchStartIfNeeded(
1561 false /* forceSend */, mStartActivity);
Wei Wang98f03f92016-05-18 11:32:52 -07001562
Wale Ogunwale21e06482019-11-18 05:14:15 -08001563 mTargetStack.startActivityLocked(mStartActivity, topStack.getTopNonFinishingActivity(),
1564 newTask, mKeepCurTransition, mOptions);
Wale Ogunwale01d66562015-12-29 08:19:19 -08001565 if (mDoResume) {
Bryce Leeaf691c02017-03-20 14:20:22 -07001566 final ActivityRecord topTaskActivity =
Louis Changcdec0802019-11-11 11:45:07 +08001567 mStartActivity.getTask().topRunningActivityLocked();
Wale Ogunwale3b232392016-05-13 15:37:13 -07001568 if (!mTargetStack.isFocusable()
Wale Ogunwale68741142016-05-17 09:40:02 -07001569 || (topTaskActivity != null && topTaskActivity.mTaskOverlay
1570 && mStartActivity != topTaskActivity)) {
Filip Gruszczynski3d7fdc12016-01-31 17:33:29 -08001571 // If the activity is not focusable, we can't resume it, but still would like to
1572 // make sure it becomes visible as it starts (this will also trigger entry
1573 // animation). An example of this are PIP activities.
Wale Ogunwale3b232392016-05-13 15:37:13 -07001574 // Also, we don't want to resume activities in a task that currently has an overlay
1575 // as the starting activity just needs to be in the visible paused state until the
1576 // over is removed.
Wale Ogunwale076c3b12019-11-20 12:17:22 -08001577 mTargetStack.ensureActivitiesVisible(mStartActivity, 0, !PRESERVE_WINDOWS);
Wale Ogunwaleae846f42016-02-22 14:00:56 -08001578 // Go ahead and tell window manager to execute app transition for this activity
1579 // since the app transition will not be triggered through the resume channel.
Wale Ogunwale3a256e62018-12-06 14:41:18 -08001580 mTargetStack.getDisplay().mDisplayContent.executeAppTransition();
Wale Ogunwale3b232392016-05-13 15:37:13 -07001581 } else {
Winson Chung32066032016-11-04 11:55:21 -07001582 // If the target stack was not previously focusable (previous top running activity
1583 // on that stack was not visible) then any prior calls to move the stack to the
1584 // will not update the focused stack. If starting the new activity now allows the
1585 // task stack to be focusable, then ensure that we now update the focused stack
1586 // accordingly.
Andrii Kulian5f750bc2018-07-17 08:57:23 -07001587 if (mTargetStack.isFocusable()
Wale Ogunwaled32da472018-11-16 07:19:28 -08001588 && !mRootActivityContainer.isTopDisplayFocusedStack(mTargetStack)) {
Riddle Hsu2ca561b2019-10-08 21:58:58 +08001589 mTargetStack.moveToFront("startActivityInner");
Winson Chung32066032016-11-04 11:55:21 -07001590 }
Wale Ogunwaled32da472018-11-16 07:19:28 -08001591 mRootActivityContainer.resumeFocusedStacksTopActivities(
1592 mTargetStack, mStartActivity, mOptions);
Filip Gruszczynski3d7fdc12016-01-31 17:33:29 -08001593 }
Winson Chung1dbc8112017-09-28 18:05:31 -07001594 } else if (mStartActivity != null) {
Louis Changcdec0802019-11-11 11:45:07 +08001595 mSupervisor.mRecentTasks.add(mStartActivity.getTask());
Wale Ogunwale01d66562015-12-29 08:19:19 -08001596 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001597 mRootActivityContainer.updateUserStack(mStartActivity.mUserId, mTargetStack);
Wale Ogunwale01d66562015-12-29 08:19:19 -08001598
Louis Changcdec0802019-11-11 11:45:07 +08001599 mSupervisor.handleNonResizableTaskIfNeeded(mStartActivity.getTask(),
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001600 preferredWindowingMode, mPreferredDisplayId, mTargetStack);
Wale Ogunwale01d66562015-12-29 08:19:19 -08001601
1602 return START_SUCCESS;
1603 }
1604
Louis Changcdec0802019-11-11 11:45:07 +08001605 private Task computeTargetTask() {
Louis Changf7dd7f22019-11-05 11:59:56 +08001606 if (mStartActivity.resultTo == null && mInTask == null && !mAddingToTask
Louis Changbde91e92019-08-16 17:19:47 +08001607 && (mLaunchFlags & FLAG_ACTIVITY_NEW_TASK) != 0) {
1608 // A new task should be created instead of using existing one.
1609 return null;
1610 } else if (mSourceRecord != null) {
Louis Changcdec0802019-11-11 11:45:07 +08001611 return mSourceRecord.getTask();
Louis Changbde91e92019-08-16 17:19:47 +08001612 } else if (mInTask != null) {
1613 return mInTask;
1614 } else {
1615 final ActivityRecord top = computeStackFocus(mStartActivity, false /* newTask */,
Wale Ogunwale21e06482019-11-18 05:14:15 -08001616 mLaunchFlags, mOptions).getTopNonFinishingActivity();
Louis Changbde91e92019-08-16 17:19:47 +08001617 if (top != null) {
Louis Changcdec0802019-11-11 11:45:07 +08001618 return top.getTask();
Louis Changbde91e92019-08-16 17:19:47 +08001619 }
1620 }
1621 return null;
1622 }
1623
Louis Changcdec0802019-11-11 11:45:07 +08001624 private int isAllowedToStart(ActivityRecord r, boolean newTask, Task targetTask) {
Louis Changbde91e92019-08-16 17:19:47 +08001625 if (mStartActivity.packageName == null) {
1626 if (mStartActivity.resultTo != null) {
1627 mStartActivity.resultTo.sendResult(INVALID_UID, mStartActivity.resultWho,
1628 mStartActivity.requestCode, RESULT_CANCELED, null /* data */);
1629 }
1630 ActivityOptions.abort(mOptions);
1631 return START_CLASS_NOT_FOUND;
1632 }
1633
1634 // Do not start home activity if it cannot be launched on preferred display. We are not
1635 // doing this in ActivityStackSupervisor#canPlaceEntityOnDisplay because it might
1636 // fallback to launch on other displays.
1637 if (r.isActivityTypeHome() && !mRootActivityContainer.canStartHomeOnDisplay(r.info,
1638 mPreferredDisplayId, true /* allowInstrumenting */)) {
1639 Slog.w(TAG, "Cannot launch home on display " + mPreferredDisplayId);
1640 return START_CANCELED;
1641 }
1642
1643 if (mRestrictedBgActivity && (newTask || !targetTask.containsAppUid(mCallingUid))
1644 && handleBackgroundActivityAbort(mStartActivity)) {
1645 Slog.e(TAG, "Abort background activity starts from " + mCallingUid);
1646 return START_ABORTED;
1647 }
1648
1649 // When the flags NEW_TASK and CLEAR_TASK are set, then the task gets reused but still
1650 // needs to be a lock task mode violation since the task gets cleared out and the device
1651 // would otherwise leave the locked task.
1652 final boolean isNewClearTask =
1653 (mLaunchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
1654 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK);
Louis Changa3e6b892019-09-16 10:39:00 +08001655 if (!newTask && mService.getLockTaskController().isLockTaskModeViolation(targetTask,
1656 isNewClearTask)) {
Louis Changbde91e92019-08-16 17:19:47 +08001657 Slog.e(TAG, "Attempted Lock Task Mode violation mStartActivity=" + mStartActivity);
1658 return START_RETURN_LOCK_TASK_MODE_VIOLATION;
1659 }
1660
1661 return START_SUCCESS;
1662 }
1663
1664 /**
1665 * Prepare the target task to be reused for this launch, which including:
1666 * - Position the target task on valid stack on preferred display.
1667 * - Comply to the specified activity launch flags
1668 * - Determine whether need to add a new activity on top or just brought the task to front.
1669 */
Louis Changcdec0802019-11-11 11:45:07 +08001670 private int recycleTask(Task targetTask, ActivityRecord targetTaskTop, Task reusedTask) {
Louis Changbde91e92019-08-16 17:19:47 +08001671 // True if we are clearing top and resetting of a standard (default) launch mode
1672 // ({@code LAUNCH_MULTIPLE}) activity. The existing activity will be finished.
1673 final boolean clearTopAndResetStandardLaunchMode =
1674 (mLaunchFlags & (FLAG_ACTIVITY_CLEAR_TOP | FLAG_ACTIVITY_RESET_TASK_IF_NEEDED))
1675 == (FLAG_ACTIVITY_CLEAR_TOP | FLAG_ACTIVITY_RESET_TASK_IF_NEEDED)
1676 && mLaunchMode == LAUNCH_MULTIPLE;
1677
Wale Ogunwaleda8b8272018-11-29 19:37:37 -08001678 boolean clearTaskForReuse = false;
Louis Changf7dd7f22019-11-05 11:59:56 +08001679 if (reusedTask != null) {
Wale Ogunwaleda8b8272018-11-29 19:37:37 -08001680 // If mStartActivity does not have a task associated with it, associate it with the
1681 // reused activity's task. Do not do so if we're clearing top and resetting for a
1682 // standard launchMode activity.
Louis Changcdec0802019-11-11 11:45:07 +08001683 if (mStartActivity.getTask() == null && !clearTopAndResetStandardLaunchMode) {
Louis Changf7dd7f22019-11-05 11:59:56 +08001684 mStartActivity.setTaskForReuse(reusedTask);
Wale Ogunwaleda8b8272018-11-29 19:37:37 -08001685 clearTaskForReuse = true;
1686 }
1687
Louis Changbde91e92019-08-16 17:19:47 +08001688 if (targetTask.intent == null) {
1689 // This task was started because of movement of the activity based on
1690 // affinity...
1691 // Now that we are actually launching it, we can assign the base intent.
1692 targetTask.setIntent(mStartActivity);
1693 } else {
1694 final boolean taskOnHome =
1695 (mStartActivity.intent.getFlags() & FLAG_ACTIVITY_TASK_ON_HOME) != 0;
1696 if (taskOnHome) {
1697 targetTask.intent.addFlags(FLAG_ACTIVITY_TASK_ON_HOME);
1698 } else {
1699 targetTask.intent.removeFlags(FLAG_ACTIVITY_TASK_ON_HOME);
1700 }
1701 }
1702 }
1703
1704 mRootActivityContainer.sendPowerHintForLaunchStartIfNeeded(false /* forceSend */,
1705 targetTaskTop);
1706
1707 setTargetStackIfNeeded(targetTaskTop);
1708
Louis Changbde91e92019-08-16 17:19:47 +08001709 // When there is a reused activity and the current result is a trampoline activity,
1710 // set the reused activity as the result.
Louis Chang54fbb052019-10-16 17:10:17 +08001711 if (mLastStartActivityRecord != null
1712 && (mLastStartActivityRecord.finishing || mLastStartActivityRecord.noDisplay)) {
1713 mLastStartActivityRecord = targetTaskTop;
Louis Changbde91e92019-08-16 17:19:47 +08001714 }
1715
1716 if ((mStartFlags & START_FLAG_ONLY_IF_NEEDED) != 0) {
1717 // We don't need to start a new activity, and the client said not to do anything
1718 // if that is the case, so this is it! And for paranoia, make sure we have
1719 // correctly resumed the top activity.
1720 if (!mMovedToFront && mDoResume) {
1721 if (DEBUG_TASKS) {
1722 Slog.d(TAG_TASKS, "Bring to front target: " + mTargetStack
1723 + " from " + targetTaskTop);
1724 }
1725 mTargetStack.moveToFront("intentActivityFound");
1726 }
1727 resumeTargetStackIfNeeded();
1728 return START_RETURN_INTENT_TO_CALLER;
1729 }
1730
Wale Ogunwale21e06482019-11-18 05:14:15 -08001731 complyActivityFlags(targetTask,
1732 reusedTask != null ? reusedTask.getTopNonFinishingActivity() : null);
Louis Changbde91e92019-08-16 17:19:47 +08001733
Wale Ogunwaleda8b8272018-11-29 19:37:37 -08001734 if (clearTaskForReuse) {
1735 // Clear task for re-use so later code to methods
1736 // {@link #setTaskFromReuseOrCreateNewTask}, {@link #setTaskFromSourceRecord}, or
1737 // {@link #setTaskFromInTask} can parent it to the task.
1738 mStartActivity.setTaskForReuse(null);
1739 }
1740
Louis Changbde91e92019-08-16 17:19:47 +08001741 if (mAddingToTask) {
1742 return START_SUCCESS;
1743 }
1744
Louis Chang3ee5fc02019-09-23 11:32:10 +08001745 if (mMovedToFront) {
1746 // We moved the task to front, use starting window to hide initial drawn delay.
1747 targetTaskTop.showStartingWindow(null /* prev */, false /* newTask */,
1748 true /* taskSwitch */);
1749 } else if (mDoResume) {
1750 // Make sure the stack and its belonging display are moved to topmost.
Louis Changbde91e92019-08-16 17:19:47 +08001751 mTargetStack.moveToFront("intentActivityFound");
1752 }
1753 // We didn't do anything... but it was needed (a.k.a., client don't use that intent!)
1754 // And for paranoia, make sure we have correctly resumed the top activity.
1755 resumeTargetStackIfNeeded();
Louis Chang54fbb052019-10-16 17:10:17 +08001756 // The reusedActivity could be finishing, for example of starting an activity with
1757 // FLAG_ACTIVITY_CLEAR_TOP flag. In that case, return the top running activity in the
1758 // task instead.
1759 mLastStartActivityRecord =
Wale Ogunwale21e06482019-11-18 05:14:15 -08001760 targetTaskTop.finishing ? targetTask.getTopNonFinishingActivity() : targetTaskTop;
Louis Changbde91e92019-08-16 17:19:47 +08001761 return mMovedToFront ? START_TASK_TO_FRONT : START_DELIVERED_TO_TOP;
1762 }
1763
1764 /**
1765 * Check if the activity being launched is the same as the one currently at the top and it
1766 * should only be launched once.
1767 */
1768 private int deliverToCurrentTopIfNeeded(ActivityStack topStack) {
1769 final ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(mNotTop);
1770 final boolean dontStart = top != null && mStartActivity.resultTo == null
1771 && top.mActivityComponent.equals(mStartActivity.mActivityComponent)
1772 && top.mUserId == mStartActivity.mUserId
1773 && top.attachedToProcess()
1774 && ((mLaunchFlags & FLAG_ACTIVITY_SINGLE_TOP) != 0
1775 || isLaunchModeOneOf(LAUNCH_SINGLE_TOP, LAUNCH_SINGLE_TASK))
1776 // This allows home activity to automatically launch on secondary display when
1777 // display added, if home was the top activity on default display, instead of
1778 // sending new intent to the home activity on default display.
1779 && (!top.isActivityTypeHome() || top.getDisplayId() == mPreferredDisplayId);
1780 if (!dontStart) {
1781 return START_SUCCESS;
1782 }
1783
1784 // For paranoia, make sure we have correctly resumed the top activity.
1785 topStack.mLastPausedActivity = null;
1786 if (mDoResume) {
1787 mRootActivityContainer.resumeFocusedStacksTopActivities();
1788 }
1789 ActivityOptions.abort(mOptions);
1790 if ((mStartFlags & START_FLAG_ONLY_IF_NEEDED) != 0) {
1791 // We don't need to start a new activity, and the client said not to do anything if
1792 // that is the case, so this is it!
1793 return START_RETURN_INTENT_TO_CALLER;
1794 }
1795
1796 deliverNewIntent(top);
1797
1798 // Don't use mStartActivity.task to show the toast. We're not starting a new activity but
1799 // reusing 'top'. Fields in mStartActivity may not be fully initialized.
Louis Changcdec0802019-11-11 11:45:07 +08001800 mSupervisor.handleNonResizableTaskIfNeeded(top.getTask(),
Louis Changbde91e92019-08-16 17:19:47 +08001801 mLaunchParams.mWindowingMode, mPreferredDisplayId, topStack);
1802
1803 return START_DELIVERED_TO_TOP;
1804 }
1805
1806 /**
1807 * Applying the launching flags to the task, which might clear few or all the activities in the
1808 * task.
1809 */
Louis Changcdec0802019-11-11 11:45:07 +08001810 private void complyActivityFlags(Task targetTask, ActivityRecord reusedActivity) {
Wale Ogunwale21e06482019-11-18 05:14:15 -08001811 ActivityRecord targetTaskTop = targetTask.getTopNonFinishingActivity();
Louis Chang2dcb1272019-09-27 15:01:19 +08001812 final boolean resetTask =
1813 reusedActivity != null && (mLaunchFlags & FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0;
1814 if (resetTask) {
Wale Ogunwaledfbeed72019-11-20 08:57:39 -08001815 targetTaskTop = mTargetStack.resetTaskIfNeeded(targetTaskTop, mStartActivity);
Louis Changbde91e92019-08-16 17:19:47 +08001816 }
1817
1818 if ((mLaunchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
1819 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK)) {
1820 // The caller has requested to completely replace any existing task with its new
1821 // activity. Well that should not be too hard...
Louis Changcdec0802019-11-11 11:45:07 +08001822 // Note: we must persist the {@link Task} first as intentActivity could be
Louis Changbde91e92019-08-16 17:19:47 +08001823 // removed from calling performClearTaskLocked (For example, if it is being brought out
1824 // of history or if it is finished immediately), thus disassociating the task. Also note
Louis Changcdec0802019-11-11 11:45:07 +08001825 // that mReuseTask is reset as a result of {@link Task#performClearTaskLocked}
Louis Changbde91e92019-08-16 17:19:47 +08001826 // launching another activity.
1827 // TODO(b/36119896): We shouldn't trigger activity launches in this path since we are
1828 // already launching one.
1829 targetTask.performClearTaskLocked();
1830 targetTask.setIntent(mStartActivity);
1831 mAddingToTask = true;
1832 } else if ((mLaunchFlags & FLAG_ACTIVITY_CLEAR_TOP) != 0
1833 || isDocumentLaunchesIntoExisting(mLaunchFlags)
1834 || isLaunchModeOneOf(LAUNCH_SINGLE_INSTANCE, LAUNCH_SINGLE_TASK)) {
1835 // In this situation we want to remove all activities from the task up to the one
1836 // being started. In most cases this means we are resetting the task to its initial
1837 // state.
1838 final ActivityRecord top = targetTask.performClearTaskForReuseLocked(mStartActivity,
1839 mLaunchFlags);
1840
1841 // The above code can remove {@code reusedActivity} from the task, leading to the
Louis Changcdec0802019-11-11 11:45:07 +08001842 // {@code ActivityRecord} removing its reference to the {@code Task}. The task
Wale Ogunwalec17418e2019-10-13 23:00:40 +02001843 // reference is needed in the call below to {@link setTargetStackAndMoveToFrontIfNeeded}
Louis Changcdec0802019-11-11 11:45:07 +08001844 if (targetTaskTop.getTask() == null) {
Wale Ogunwale2322bed2019-10-10 17:24:19 +02001845 targetTask.addChild(targetTaskTop);
Louis Changbde91e92019-08-16 17:19:47 +08001846 }
1847
1848 if (top != null) {
1849 if (top.isRootOfTask()) {
1850 // Activity aliases may mean we use different intents for the top activity,
1851 // so make sure the task now has the identity of the new intent.
Louis Changcdec0802019-11-11 11:45:07 +08001852 top.getTask().setIntent(mStartActivity);
Louis Changbde91e92019-08-16 17:19:47 +08001853 }
1854 deliverNewIntent(top);
1855 } else {
1856 // A special case: we need to start the activity because it is not currently
1857 // running, and the caller has asked to clear the current task to have this
1858 // activity at the top.
1859 mAddingToTask = true;
1860 if (targetTask.getStack() == null) {
1861 // Target stack got cleared when we all activities were removed above.
1862 // Go ahead and reset it.
1863 mTargetStack = computeStackFocus(mSourceRecord, false /* newTask */,
1864 mLaunchFlags, mOptions);
Wale Ogunwale2322bed2019-10-10 17:24:19 +02001865 mTargetStack.addChild(targetTask, !mLaunchTaskBehind /* toTop */,
1866 (mStartActivity.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
Louis Changbde91e92019-08-16 17:19:47 +08001867 }
1868 }
1869 } else if ((mLaunchFlags & FLAG_ACTIVITY_CLEAR_TOP) == 0 && !mAddingToTask
1870 && (mLaunchFlags & FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
1871 // In this case, we are launching an activity in our own task that may
1872 // already be running somewhere in the history, and we want to shuffle it to
1873 // the front of the stack if so.
1874 final ActivityRecord act = targetTask.findActivityInHistoryLocked(
1875 mStartActivity);
1876 if (act != null) {
Louis Changcdec0802019-11-11 11:45:07 +08001877 final Task task = act.getTask();
Louis Changbde91e92019-08-16 17:19:47 +08001878 task.moveActivityToFrontLocked(act);
1879 act.updateOptionsLocked(mOptions);
1880 deliverNewIntent(act);
1881 mTargetStack.mLastPausedActivity = null;
1882 } else {
1883 mAddingToTask = true;
1884 }
1885 } else if (mStartActivity.mActivityComponent.equals(targetTask.realActivity)) {
Louis Chang382419b2019-10-03 21:43:38 +08001886 if (targetTask == mInTask) {
1887 // In this case we are bringing up an existing activity from a recent task. We
1888 // don't need to add a new activity instance on top.
1889 } else if (((mLaunchFlags & FLAG_ACTIVITY_SINGLE_TOP) != 0
1890 || LAUNCH_SINGLE_TOP == mLaunchMode)
1891 && targetTaskTop.mActivityComponent.equals(mStartActivity.mActivityComponent)
1892 && mStartActivity.resultTo == null) {
1893 // In this case the top activity on the task is the same as the one being launched,
1894 // so we take that as a request to bring the task to the foreground. If the top
1895 // activity in the task is the root activity, deliver this new intent to it if it
1896 // desires.
Louis Changbde91e92019-08-16 17:19:47 +08001897 if (targetTaskTop.isRootOfTask()) {
Louis Changcdec0802019-11-11 11:45:07 +08001898 targetTaskTop.getTask().setIntent(mStartActivity);
Louis Changbde91e92019-08-16 17:19:47 +08001899 }
1900 deliverNewIntent(targetTaskTop);
1901 } else if (!targetTask.isSameIntentFilter(mStartActivity)) {
1902 // In this case we are launching the root activity of the task, but with a
1903 // different intent. We should start a new instance on top.
1904 mAddingToTask = true;
1905 } else if (reusedActivity == null) {
1906 mAddingToTask = true;
1907 }
Louis Chang2dcb1272019-09-27 15:01:19 +08001908 } else if (!resetTask) {
Louis Changbde91e92019-08-16 17:19:47 +08001909 // In this case an activity is being launched in to an existing task, without
1910 // resetting that task. This is typically the situation of launching an activity
1911 // from a notification or shortcut. We want to place the new activity on top of the
1912 // current task.
1913 mAddingToTask = true;
1914 } else if (!targetTask.rootWasReset) {
1915 // In this case we are launching into an existing task that has not yet been started
1916 // from its front door. The current task has been brought to the front. Ideally,
1917 // we'd probably like to place this new task at the bottom of its stack, but that's
1918 // a little hard to do with the current organization of the code so for now we'll
1919 // just drop it.
1920 targetTask.setIntent(mStartActivity);
1921 }
1922 }
1923
Bryce Leedaa91e42017-12-06 14:13:01 -08001924 /**
1925 * Resets the {@link ActivityStarter} state.
1926 * @param clearRequest whether the request should be reset to default values.
1927 */
1928 void reset(boolean clearRequest) {
1929 mStartActivity = null;
1930 mIntent = null;
1931 mCallingUid = -1;
1932 mOptions = null;
Ricky Waib147fa12019-04-25 16:08:30 +01001933 mRestrictedBgActivity = false;
Bryce Leedaa91e42017-12-06 14:13:01 -08001934
1935 mLaunchTaskBehind = false;
1936 mLaunchFlags = 0;
1937 mLaunchMode = INVALID_LAUNCH_MODE;
1938
Bryce Leeec55eb02017-12-05 20:51:27 -08001939 mLaunchParams.reset();
Bryce Leedaa91e42017-12-06 14:13:01 -08001940
1941 mNotTop = null;
1942 mDoResume = false;
1943 mStartFlags = 0;
1944 mSourceRecord = null;
1945 mPreferredDisplayId = INVALID_DISPLAY;
1946
1947 mInTask = null;
1948 mAddingToTask = false;
1949 mReuseTask = null;
1950
1951 mNewTaskInfo = null;
1952 mNewTaskIntent = null;
1953 mSourceStack = null;
1954
1955 mTargetStack = null;
1956 mMovedToFront = false;
1957 mNoAnimation = false;
1958 mKeepCurTransition = false;
1959 mAvoidMoveToFront = false;
Winson Chunge219ae12019-07-18 13:43:23 -07001960 mFrozeTaskList = false;
Bryce Leedaa91e42017-12-06 14:13:01 -08001961
1962 mVoiceSession = null;
1963 mVoiceInteractor = null;
1964
1965 mIntentDelivered = false;
1966
1967 if (clearRequest) {
1968 mRequest.reset();
1969 }
1970 }
1971
Louis Changcdec0802019-11-11 11:45:07 +08001972 private void setInitialState(ActivityRecord r, ActivityOptions options, Task inTask,
Wale Ogunwale01d66562015-12-29 08:19:19 -08001973 boolean doResume, int startFlags, ActivityRecord sourceRecord,
Ricky Waib147fa12019-04-25 16:08:30 +01001974 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1975 boolean restrictedBgActivity) {
Bryce Leedaa91e42017-12-06 14:13:01 -08001976 reset(false /* clearRequest */);
1977
Wale Ogunwale01d66562015-12-29 08:19:19 -08001978 mStartActivity = r;
1979 mIntent = r.intent;
1980 mOptions = options;
1981 mCallingUid = r.launchedFromUid;
1982 mSourceRecord = sourceRecord;
1983 mVoiceSession = voiceSession;
1984 mVoiceInteractor = voiceInteractor;
Ricky Waib147fa12019-04-25 16:08:30 +01001985 mRestrictedBgActivity = restrictedBgActivity;
Wale Ogunwale01d66562015-12-29 08:19:19 -08001986
Bryce Leeec55eb02017-12-05 20:51:27 -08001987 mLaunchParams.reset();
Bryce Leedacefc42017-10-10 12:56:02 -07001988
Louis Chang6fb1e842018-12-03 16:07:50 +08001989 // Preferred display id is the only state we need for now and it could be updated again
1990 // after we located a reusable task (which might be resided in another display).
Garfield Tan706dbcb2018-10-15 11:33:02 -07001991 mSupervisor.getLaunchParamsController().calculate(inTask, r.info.windowLayout, r,
Louis Chang6fb1e842018-12-03 16:07:50 +08001992 sourceRecord, options, PHASE_DISPLAY, mLaunchParams);
1993 mPreferredDisplayId =
1994 mLaunchParams.hasPreferredDisplay() ? mLaunchParams.mPreferredDisplayId
1995 : DEFAULT_DISPLAY;
Garfield Tanb5cc09f2018-09-28 10:06:52 -07001996
Bryce Lee7daee392017-10-12 13:46:18 -07001997 mLaunchMode = r.launchMode;
1998
Wale Ogunwale01d66562015-12-29 08:19:19 -08001999 mLaunchFlags = adjustLaunchFlagsToDocumentMode(
Bryce Lee7daee392017-10-12 13:46:18 -07002000 r, LAUNCH_SINGLE_INSTANCE == mLaunchMode,
2001 LAUNCH_SINGLE_TASK == mLaunchMode, mIntent.getFlags());
Wale Ogunwale01d66562015-12-29 08:19:19 -08002002 mLaunchTaskBehind = r.mLaunchTaskBehind
Bryce Lee7daee392017-10-12 13:46:18 -07002003 && !isLaunchModeOneOf(LAUNCH_SINGLE_TASK, LAUNCH_SINGLE_INSTANCE)
Wale Ogunwale01d66562015-12-29 08:19:19 -08002004 && (mLaunchFlags & FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
2005
2006 sendNewTaskResultRequestIfNeeded();
2007
2008 if ((mLaunchFlags & FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
2009 mLaunchFlags |= FLAG_ACTIVITY_NEW_TASK;
2010 }
2011
2012 // If we are actually going to launch in to a new task, there are some cases where
2013 // we further want to do multiple task.
2014 if ((mLaunchFlags & FLAG_ACTIVITY_NEW_TASK) != 0) {
2015 if (mLaunchTaskBehind
2016 || r.info.documentLaunchMode == DOCUMENT_LAUNCH_ALWAYS) {
2017 mLaunchFlags |= FLAG_ACTIVITY_MULTIPLE_TASK;
2018 }
2019 }
2020
2021 // We'll invoke onUserLeaving before onPause only if the launching
2022 // activity did not explicitly state that this is an automated launch.
2023 mSupervisor.mUserLeaving = (mLaunchFlags & FLAG_ACTIVITY_NO_USER_ACTION) == 0;
2024 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
2025 "startActivity() => mUserLeaving=" + mSupervisor.mUserLeaving);
2026
2027 // If the caller has asked not to resume at this point, we make note
2028 // of this in the record so that we can skip it when trying to find
2029 // the top running activity.
2030 mDoResume = doResume;
Louis Chang37317152019-05-09 09:53:58 +08002031 if (!doResume || !r.okToShowLocked() || mLaunchTaskBehind) {
Wale Ogunwale01d66562015-12-29 08:19:19 -08002032 r.delayedResume = true;
2033 mDoResume = false;
2034 }
2035
Winson Chunge2d72172018-01-25 17:46:20 +00002036 if (mOptions != null) {
2037 if (mOptions.getLaunchTaskId() != -1 && mOptions.getTaskOverlay()) {
2038 r.mTaskOverlay = true;
2039 if (!mOptions.canTaskOverlayResume()) {
Louis Changcdec0802019-11-11 11:45:07 +08002040 final Task task = mRootActivityContainer.anyTaskForId(
Winson Chunge2d72172018-01-25 17:46:20 +00002041 mOptions.getLaunchTaskId());
Wale Ogunwale21e06482019-11-18 05:14:15 -08002042 final ActivityRecord top = task != null
2043 ? task.getTopNonFinishingActivity() : null;
Bryce Lee7ace3952018-02-16 14:34:32 -08002044 if (top != null && !top.isState(RESUMED)) {
Jorim Jaggic875ae72016-04-26 22:41:06 -07002045
Winson Chunge2d72172018-01-25 17:46:20 +00002046 // The caller specifies that we'd like to be avoided to be moved to the
2047 // front, so be it!
2048 mDoResume = false;
2049 mAvoidMoveToFront = true;
2050 }
Winson Chungcbcadc92017-01-12 15:54:12 -08002051 }
Winson Chunge2d72172018-01-25 17:46:20 +00002052 } else if (mOptions.getAvoidMoveToFront()) {
Winson Chungba40d3a2018-05-16 09:40:16 -07002053 mDoResume = false;
Winson Chunge2d72172018-01-25 17:46:20 +00002054 mAvoidMoveToFront = true;
Jorim Jaggic875ae72016-04-26 22:41:06 -07002055 }
2056 }
2057
Louis Chang2f4e9b462019-03-05 16:43:15 +08002058 mNotTop = (mLaunchFlags & FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? sourceRecord : null;
Wale Ogunwale01d66562015-12-29 08:19:19 -08002059
2060 mInTask = inTask;
2061 // In some flows in to this function, we retrieve the task record and hold on to it
2062 // without a lock before calling back in to here... so the task at this point may
2063 // not actually be in recents. Check for that, and if it isn't in recents just
2064 // consider it invalid.
2065 if (inTask != null && !inTask.inRecents) {
2066 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
2067 mInTask = null;
2068 }
2069
2070 mStartFlags = startFlags;
2071 // If the onlyIfNeeded flag is set, then we can do this if the activity being launched
2072 // is the same as the one making the call... or, as a special case, if we do not know
2073 // the caller then we count the current top activity as the caller.
2074 if ((startFlags & START_FLAG_ONLY_IF_NEEDED) != 0) {
2075 ActivityRecord checkedCaller = sourceRecord;
2076 if (checkedCaller == null) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08002077 checkedCaller = mRootActivityContainer.getTopDisplayFocusedStack()
Andrii Kulian5f750bc2018-07-17 08:57:23 -07002078 .topRunningNonDelayedActivityLocked(mNotTop);
Wale Ogunwale01d66562015-12-29 08:19:19 -08002079 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002080 if (!checkedCaller.mActivityComponent.equals(r.mActivityComponent)) {
Wale Ogunwale01d66562015-12-29 08:19:19 -08002081 // Caller is not the same as launcher, so always needed.
2082 mStartFlags &= ~START_FLAG_ONLY_IF_NEEDED;
2083 }
2084 }
2085
2086 mNoAnimation = (mLaunchFlags & FLAG_ACTIVITY_NO_ANIMATION) != 0;
Ricky Waib147fa12019-04-25 16:08:30 +01002087
Alan Stokes07389b62019-05-20 15:22:54 +01002088 if (mRestrictedBgActivity && !mService.isBackgroundActivityStartsEnabled()) {
Ricky Waib147fa12019-04-25 16:08:30 +01002089 mAvoidMoveToFront = true;
2090 mDoResume = false;
2091 }
Wale Ogunwale01d66562015-12-29 08:19:19 -08002092 }
2093
2094 private void sendNewTaskResultRequestIfNeeded() {
Andrii Kulian79d67982019-08-19 11:56:16 -07002095 if (mStartActivity.resultTo != null && (mLaunchFlags & FLAG_ACTIVITY_NEW_TASK) != 0) {
Wale Ogunwale01d66562015-12-29 08:19:19 -08002096 // For whatever reason this activity is being launched into a new task...
2097 // yet the caller has requested a result back. Well, that is pretty messed up,
2098 // so instead immediately send back a cancel and let the new task continue launched
2099 // as normal without a dependency on its originator.
2100 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
Andrii Kulian79d67982019-08-19 11:56:16 -07002101 mStartActivity.resultTo.sendResult(INVALID_UID, mStartActivity.resultWho,
2102 mStartActivity.requestCode, RESULT_CANCELED, null /* data */);
Wale Ogunwale01d66562015-12-29 08:19:19 -08002103 mStartActivity.resultTo = null;
2104 }
2105 }
2106
2107 private void computeLaunchingTaskFlags() {
2108 // If the caller is not coming from another activity, but has given us an explicit task into
2109 // which they would like us to launch the new activity, then let's see about doing that.
Andrii Kulian02b7a832016-10-06 23:11:56 -07002110 if (mSourceRecord == null && mInTask != null && mInTask.getStack() != null) {
Wale Ogunwale01d66562015-12-29 08:19:19 -08002111 final Intent baseIntent = mInTask.getBaseIntent();
2112 final ActivityRecord root = mInTask.getRootActivity();
2113 if (baseIntent == null) {
2114 ActivityOptions.abort(mOptions);
2115 throw new IllegalArgumentException("Launching into task without base intent: "
2116 + mInTask);
2117 }
2118
2119 // If this task is empty, then we are adding the first activity -- it
2120 // determines the root, and must be launching as a NEW_TASK.
Bryce Lee7daee392017-10-12 13:46:18 -07002121 if (isLaunchModeOneOf(LAUNCH_SINGLE_INSTANCE, LAUNCH_SINGLE_TASK)) {
Wale Ogunwale01d66562015-12-29 08:19:19 -08002122 if (!baseIntent.getComponent().equals(mStartActivity.intent.getComponent())) {
2123 ActivityOptions.abort(mOptions);
2124 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
2125 + mStartActivity + " into different task " + mInTask);
2126 }
2127 if (root != null) {
2128 ActivityOptions.abort(mOptions);
2129 throw new IllegalArgumentException("Caller with mInTask " + mInTask
2130 + " has root " + root + " but target is singleInstance/Task");
2131 }
2132 }
2133
2134 // If task is empty, then adopt the interesting intent launch flags in to the
2135 // activity being started.
2136 if (root == null) {
2137 final int flagsOfInterest = FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_MULTIPLE_TASK
2138 | FLAG_ACTIVITY_NEW_DOCUMENT | FLAG_ACTIVITY_RETAIN_IN_RECENTS;
2139 mLaunchFlags = (mLaunchFlags & ~flagsOfInterest)
2140 | (baseIntent.getFlags() & flagsOfInterest);
2141 mIntent.setFlags(mLaunchFlags);
2142 mInTask.setIntent(mStartActivity);
2143 mAddingToTask = true;
2144
2145 // If the task is not empty and the caller is asking to start it as the root of
2146 // a new task, then we don't actually want to start this on the task. We will
2147 // bring the task to the front, and possibly give it a new intent.
2148 } else if ((mLaunchFlags & FLAG_ACTIVITY_NEW_TASK) != 0) {
2149 mAddingToTask = false;
2150
2151 } else {
2152 mAddingToTask = true;
2153 }
2154
2155 mReuseTask = mInTask;
2156 } else {
2157 mInTask = null;
2158 // Launch ResolverActivity in the source task, so that it stays in the task bounds
2159 // when in freeform workspace.
2160 // Also put noDisplay activities in the source task. These by itself can be placed
2161 // in any task/stack, however it could launch other activities like ResolverActivity,
2162 // and we want those to stay in the original task.
Louis Chang6a9be162019-07-15 10:41:32 +08002163 if ((mStartActivity.isResolverOrDelegateActivity() || mStartActivity.noDisplay)
2164 && mSourceRecord != null && mSourceRecord.inFreeformWindowingMode()) {
Wale Ogunwale01d66562015-12-29 08:19:19 -08002165 mAddingToTask = true;
2166 }
2167 }
2168
2169 if (mInTask == null) {
2170 if (mSourceRecord == null) {
2171 // This activity is not being started from another... in this
2172 // case we -always- start a new task.
2173 if ((mLaunchFlags & FLAG_ACTIVITY_NEW_TASK) == 0 && mInTask == null) {
2174 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
2175 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + mIntent);
2176 mLaunchFlags |= FLAG_ACTIVITY_NEW_TASK;
2177 }
2178 } else if (mSourceRecord.launchMode == LAUNCH_SINGLE_INSTANCE) {
2179 // The original activity who is starting us is running as a single
2180 // instance... this new activity it is starting must go on its
2181 // own task.
2182 mLaunchFlags |= FLAG_ACTIVITY_NEW_TASK;
Bryce Lee7daee392017-10-12 13:46:18 -07002183 } else if (isLaunchModeOneOf(LAUNCH_SINGLE_INSTANCE, LAUNCH_SINGLE_TASK)) {
Wale Ogunwale01d66562015-12-29 08:19:19 -08002184 // The activity being started is a single instance... it always
2185 // gets launched into its own task.
2186 mLaunchFlags |= FLAG_ACTIVITY_NEW_TASK;
2187 }
2188 }
2189 }
2190
2191 private void computeSourceStack() {
2192 if (mSourceRecord == null) {
2193 mSourceStack = null;
2194 return;
2195 }
2196 if (!mSourceRecord.finishing) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002197 mSourceStack = mSourceRecord.getActivityStack();
Wale Ogunwale01d66562015-12-29 08:19:19 -08002198 return;
2199 }
2200
2201 // If the source is finishing, we can't further count it as our source. This is because the
2202 // task it is associated with may now be empty and on its way out, so we don't want to
2203 // blindly throw it in to that task. Instead we will take the NEW_TASK flow and try to find
2204 // a task for it. But save the task information so it can be used when creating the new task.
2205 if ((mLaunchFlags & FLAG_ACTIVITY_NEW_TASK) == 0) {
2206 Slog.w(TAG, "startActivity called from finishing " + mSourceRecord
2207 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + mIntent);
2208 mLaunchFlags |= FLAG_ACTIVITY_NEW_TASK;
2209 mNewTaskInfo = mSourceRecord.info;
Bryce Leed9ed45d2017-05-22 15:57:24 -07002210
2211 // It is not guaranteed that the source record will have a task associated with it. For,
2212 // example, if this method is being called for processing a pending activity launch, it
2213 // is possible that the activity has been removed from the task after the launch was
2214 // enqueued.
Louis Changcdec0802019-11-11 11:45:07 +08002215 final Task sourceTask = mSourceRecord.getTask();
Bryce Leed9ed45d2017-05-22 15:57:24 -07002216 mNewTaskIntent = sourceTask != null ? sourceTask.intent : null;
Wale Ogunwale01d66562015-12-29 08:19:19 -08002217 }
2218 mSourceRecord = null;
2219 mSourceStack = null;
2220 }
2221
2222 /**
2223 * Decide whether the new activity should be inserted into an existing task. Returns null
2224 * if not or an ActivityRecord with the task into which the new activity should be added.
2225 */
Louis Changcdec0802019-11-11 11:45:07 +08002226 private Task getReusableTask() {
Wale Ogunwale01d66562015-12-29 08:19:19 -08002227 // We may want to try to place the new activity in to an existing task. We always
2228 // do this if the target activity is singleTask or singleInstance; we will also do
2229 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
2230 // us to still place it in a new task: multi task, always doc mode, or being asked to
2231 // launch this as a new task behind the current one.
2232 boolean putIntoExistingTask = ((mLaunchFlags & FLAG_ACTIVITY_NEW_TASK) != 0 &&
2233 (mLaunchFlags & FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Bryce Lee7daee392017-10-12 13:46:18 -07002234 || isLaunchModeOneOf(LAUNCH_SINGLE_INSTANCE, LAUNCH_SINGLE_TASK);
Wale Ogunwale01d66562015-12-29 08:19:19 -08002235 // If bring to front is requested, and no result is requested and we have not been given
2236 // an explicit task to launch in to, and we can find a task that was started with this
2237 // same component, then instead of launching bring that one to the front.
2238 putIntoExistingTask &= mInTask == null && mStartActivity.resultTo == null;
2239 ActivityRecord intentActivity = null;
Jorim Jaggi2adba072016-03-03 13:43:39 +01002240 if (mOptions != null && mOptions.getLaunchTaskId() != -1) {
Louis Changcdec0802019-11-11 11:45:07 +08002241 Task launchTask = mRootActivityContainer.anyTaskForId(mOptions.getLaunchTaskId());
Louis Changf7dd7f22019-11-05 11:59:56 +08002242 if (launchTask != null) {
2243 return launchTask;
2244 }
Jorim Jaggi2adba072016-03-03 13:43:39 +01002245 } else if (putIntoExistingTask) {
Bryce Lee7daee392017-10-12 13:46:18 -07002246 if (LAUNCH_SINGLE_INSTANCE == mLaunchMode) {
Wale Ogunwale13dbfff2016-05-20 08:50:15 -07002247 // There can be one and only one instance of single instance activity in the
2248 // history, and it is always in its own unique task, so we do a special search.
Wale Ogunwaled32da472018-11-16 07:19:28 -08002249 intentActivity = mRootActivityContainer.findActivity(mIntent, mStartActivity.info,
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002250 mStartActivity.isActivityTypeHome());
Wale Ogunwale13dbfff2016-05-20 08:50:15 -07002251 } else if ((mLaunchFlags & FLAG_ACTIVITY_LAUNCH_ADJACENT) != 0) {
2252 // For the launch adjacent case we only want to put the activity in an existing
2253 // task if the activity already exists in the history.
Wale Ogunwaled32da472018-11-16 07:19:28 -08002254 intentActivity = mRootActivityContainer.findActivity(mIntent, mStartActivity.info,
Bryce Lee7daee392017-10-12 13:46:18 -07002255 !(LAUNCH_SINGLE_TASK == mLaunchMode));
Wale Ogunwale13dbfff2016-05-20 08:50:15 -07002256 } else {
2257 // Otherwise find the best task to put the activity in.
Wale Ogunwaled32da472018-11-16 07:19:28 -08002258 intentActivity =
2259 mRootActivityContainer.findTask(mStartActivity, mPreferredDisplayId);
Wale Ogunwale13dbfff2016-05-20 08:50:15 -07002260 }
Wale Ogunwale01d66562015-12-29 08:19:19 -08002261 }
Louis Changbd48dca2018-08-29 17:44:34 +08002262
Louis Chang54506cb2018-11-23 11:03:41 +08002263 if (intentActivity != null
2264 && (mStartActivity.isActivityTypeHome() || intentActivity.isActivityTypeHome())
Louis Changbd48dca2018-08-29 17:44:34 +08002265 && intentActivity.getDisplayId() != mPreferredDisplayId) {
2266 // Do not reuse home activity on other displays.
2267 intentActivity = null;
2268 }
2269
Louis Changcdec0802019-11-11 11:45:07 +08002270 return intentActivity != null ? intentActivity.getTask() : null;
Wale Ogunwale01d66562015-12-29 08:19:19 -08002271 }
2272
Andrii Kulianfab9cd82017-03-21 19:37:09 -07002273 /**
2274 * Figure out which task and activity to bring to front when we have found an existing matching
2275 * activity record in history. May also clear the task if needed.
2276 * @param intentActivity Existing matching activity.
2277 * @return {@link ActivityRecord} brought to front.
2278 */
Louis Changbde91e92019-08-16 17:19:47 +08002279 private void setTargetStackIfNeeded(ActivityRecord intentActivity) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002280 mTargetStack = intentActivity.getActivityStack();
Wale Ogunwale01d66562015-12-29 08:19:19 -08002281 mTargetStack.mLastPausedActivity = null;
2282 // If the target task is not in the front, then we need to bring it to the front...
2283 // except... well, with SINGLE_TASK_LAUNCH it's not entirely clear. We'd like to have
2284 // the same behavior as if a new instance was being started, which means not bringing it
2285 // to the front if the caller is not itself in the front.
Riddle Hsub70b36d2018-09-11 21:20:02 +08002286 final boolean differentTopTask;
2287 if (mPreferredDisplayId == mTargetStack.mDisplayId) {
2288 final ActivityStack focusStack = mTargetStack.getDisplay().getFocusedStack();
2289 final ActivityRecord curTop = (focusStack == null)
2290 ? null : focusStack.topRunningNonDelayedActivityLocked(mNotTop);
Louis Changcdec0802019-11-11 11:45:07 +08002291 final Task topTask = curTop != null ? curTop.getTask() : null;
2292 differentTopTask = topTask != intentActivity.getTask()
Riddle Hsu273e9992019-04-29 22:40:59 +08002293 || (focusStack != null && topTask != focusStack.topTask());
Riddle Hsub70b36d2018-09-11 21:20:02 +08002294 } else {
2295 // The existing task should always be different from those in other displays.
2296 differentTopTask = true;
2297 }
Wale Ogunwale01d66562015-12-29 08:19:19 -08002298
Riddle Hsub70b36d2018-09-11 21:20:02 +08002299 if (differentTopTask && !mAvoidMoveToFront) {
Wale Ogunwale01d66562015-12-29 08:19:19 -08002300 mStartActivity.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Wale Ogunwale21e06482019-11-18 05:14:15 -08002301 if (mSourceRecord == null || (mSourceStack.getTopNonFinishingActivity() != null &&
2302 mSourceStack.getTopNonFinishingActivity().getTask()
Louis Changcdec0802019-11-11 11:45:07 +08002303 == mSourceRecord.getTask())) {
Wale Ogunwale01d66562015-12-29 08:19:19 -08002304 // We really do want to push this one into the user's face, right now.
2305 if (mLaunchTaskBehind && mSourceRecord != null) {
Louis Changcdec0802019-11-11 11:45:07 +08002306 intentActivity.setTaskToAffiliateWith(mSourceRecord.getTask());
Wale Ogunwale01d66562015-12-29 08:19:19 -08002307 }
Chong Zhangdea4bd92016-03-15 12:50:03 -07002308
Louis Changcdec0802019-11-11 11:45:07 +08002309 final Task intentTask = intentActivity.getTask();
Louis Changf3070c52019-10-09 15:57:30 +08002310 final ActivityStack launchStack =
2311 getLaunchStack(mStartActivity, mLaunchFlags, intentTask, mOptions);
Louis Changbde91e92019-08-16 17:19:47 +08002312 if (launchStack == null || launchStack == mTargetStack) {
2313 // We only want to move to the front, if we aren't going to launch on a
2314 // different stack. If we launch on a different stack, we will put the
2315 // task on top there.
2316 mTargetStack.moveTaskToFrontLocked(intentTask, mNoAnimation, mOptions,
2317 mStartActivity.appTimeTracker, "bringingFoundTaskToFront");
2318 mMovedToFront = true;
2319 } else if (launchStack.inSplitScreenWindowingMode()) {
2320 if ((mLaunchFlags & FLAG_ACTIVITY_LAUNCH_ADJACENT) != 0) {
2321 // If we want to launch adjacent and mTargetStack is not the computed
2322 // launch stack - move task to top of computed stack.
2323 intentTask.reparent(launchStack, ON_TOP,
Andrii Kulianfab9cd82017-03-21 19:37:09 -07002324 REPARENT_MOVE_STACK_TO_FRONT, ANIMATE, DEFER_RESUME,
Louis Changbde91e92019-08-16 17:19:47 +08002325 "launchToSide");
2326 } else {
2327 // TODO: This should be reevaluated in MW v2.
2328 // We choose to move task to front instead of launching it adjacent
2329 // when specific stack was requested explicitly and it appeared to be
2330 // adjacent stack, but FLAG_ACTIVITY_LAUNCH_ADJACENT was not set.
2331 mTargetStack.moveTaskToFrontLocked(intentTask,
2332 mNoAnimation, mOptions, mStartActivity.appTimeTracker,
2333 "bringToFrontInsteadOfAdjacentLaunch");
Chong Zhangdea4bd92016-03-15 12:50:03 -07002334 }
Louis Changbde91e92019-08-16 17:19:47 +08002335 mMovedToFront = launchStack != launchStack.getDisplay()
2336 .getTopStackInWindowingMode(launchStack.getWindowingMode());
2337 } else if (launchStack.mDisplayId != mTargetStack.mDisplayId) {
2338 // Target and computed stacks are on different displays and we've
2339 // found a matching task - move the existing instance to that display and
2340 // move it to front.
Louis Changcdec0802019-11-11 11:45:07 +08002341 intentActivity.getTask().reparent(launchStack, ON_TOP,
Louis Changbde91e92019-08-16 17:19:47 +08002342 REPARENT_MOVE_STACK_TO_FRONT, ANIMATE, DEFER_RESUME,
2343 "reparentToDisplay");
2344 mMovedToFront = true;
2345 } else if (launchStack.isActivityTypeHome()
2346 && !mTargetStack.isActivityTypeHome()) {
2347 // It is possible for the home activity to be in another stack initially.
2348 // For example, the activity may have been initially started with an intent
2349 // which placed it in the fullscreen stack. To ensure the proper handling of
2350 // the activity based on home stack assumptions, we must move it over.
Louis Changcdec0802019-11-11 11:45:07 +08002351 intentActivity.getTask().reparent(launchStack, ON_TOP,
Louis Changbde91e92019-08-16 17:19:47 +08002352 REPARENT_MOVE_STACK_TO_FRONT, ANIMATE, DEFER_RESUME,
2353 "reparentingHome");
2354 mMovedToFront = true;
Louis Changf3070c52019-10-09 15:57:30 +08002355 }
2356
Alan Stokes80912002019-10-29 14:53:23 +00002357 if (launchStack != null && launchStack.topTask() == null) {
Louis Changfe0dfcb2019-09-02 15:59:38 +08002358 // The task does not need to be reparented to the launch stack. Remove the
2359 // launch stack if there is no activity in it.
Louis Changf3070c52019-10-09 15:57:30 +08002360 Slog.w(TAG, "Removing an empty stack: " + launchStack);
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -07002361 launchStack.removeIfPossible();
Wale Ogunwale01d66562015-12-29 08:19:19 -08002362 }
Louis Changfe0dfcb2019-09-02 15:59:38 +08002363
Louis Changbde91e92019-08-16 17:19:47 +08002364 mOptions = null;
Wale Ogunwale01d66562015-12-29 08:19:19 -08002365 }
2366 }
Andrii Kulianb850ea52017-12-12 23:49:10 -08002367 // Need to update mTargetStack because if task was moved out of it, the original stack may
2368 // be destroyed.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002369 mTargetStack = intentActivity.getActivityStack();
Louis Changcdec0802019-11-11 11:45:07 +08002370 mSupervisor.handleNonResizableTaskIfNeeded(intentActivity.getTask(),
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002371 WINDOWING_MODE_UNDEFINED, DEFAULT_DISPLAY, mTargetStack);
Wale Ogunwale01d66562015-12-29 08:19:19 -08002372 }
2373
2374 private void resumeTargetStackIfNeeded() {
2375 if (mDoResume) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08002376 mRootActivityContainer.resumeFocusedStacksTopActivities(mTargetStack, null, mOptions);
Wale Ogunwale01d66562015-12-29 08:19:19 -08002377 } else {
2378 ActivityOptions.abort(mOptions);
2379 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002380 mRootActivityContainer.updateUserStack(mStartActivity.mUserId, mTargetStack);
Wale Ogunwale01d66562015-12-29 08:19:19 -08002381 }
2382
Louis Changcdec0802019-11-11 11:45:07 +08002383 private void setNewTask(Task taskToAffiliate) {
Louis Changbde91e92019-08-16 17:19:47 +08002384 final boolean toTop = !mLaunchTaskBehind && !mAvoidMoveToFront;
Louis Changcdec0802019-11-11 11:45:07 +08002385 final Task task = mTargetStack.createTask(
Louis Changbde91e92019-08-16 17:19:47 +08002386 mSupervisor.getNextTaskIdForUserLocked(mStartActivity.mUserId),
2387 mNewTaskInfo != null ? mNewTaskInfo : mStartActivity.info,
2388 mNewTaskIntent != null ? mNewTaskIntent : mIntent, mVoiceSession,
2389 mVoiceInteractor, toTop, mStartActivity, mSourceRecord, mOptions);
2390 addOrReparentStartingActivity(task, "setTaskFromReuseOrCreateNewTask - mReuseTask");
Louis Changcdec0802019-11-11 11:45:07 +08002391 updateBounds(mStartActivity.getTask(), mLaunchParams.mBounds);
Louis Change8902452019-06-10 10:49:28 +08002392
Louis Changbde91e92019-08-16 17:19:47 +08002393 if (DEBUG_TASKS) {
2394 Slog.v(TAG_TASKS, "Starting new activity " + mStartActivity
Louis Changcdec0802019-11-11 11:45:07 +08002395 + " in new task " + mStartActivity.getTask());
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08002396 }
2397
2398 if (taskToAffiliate != null) {
2399 mStartActivity.setTaskToAffiliateWith(taskToAffiliate);
Wale Ogunwale01d66562015-12-29 08:19:19 -08002400 }
2401 }
2402
Bryce Lee325e09682017-10-05 17:20:25 -07002403 private void deliverNewIntent(ActivityRecord activity) {
2404 if (mIntentDelivered) {
2405 return;
2406 }
2407
Louis Changcdec0802019-11-11 11:45:07 +08002408 activity.logStartActivity(AM_NEW_INTENT, activity.getTask());
Wale Ogunwale586a8ee2019-06-04 13:44:14 +00002409 activity.deliverNewIntentLocked(mCallingUid, mStartActivity.intent,
Bryce Lee325e09682017-10-05 17:20:25 -07002410 mStartActivity.launchedFromPackage);
2411 mIntentDelivered = true;
2412 }
2413
Bryce Leed3624e12017-11-30 08:51:45 -08002414 @VisibleForTesting
Louis Changcdec0802019-11-11 11:45:07 +08002415 void updateBounds(Task task, Rect bounds) {
Bryce Leedacefc42017-10-10 12:56:02 -07002416 if (bounds.isEmpty()) {
Bryce Lee4e4a3ec2017-09-27 08:25:03 -07002417 return;
2418 }
2419
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002420 final ActivityStack stack = task.getStack();
Evan Roskydbe2ce52019-07-18 11:13:17 -07002421 if (stack != null && stack.inPinnedWindowingMode()) {
2422 mService.animateResizePinnedStack(stack.mStackId, bounds, -1);
Bryce Lee4e4a3ec2017-09-27 08:25:03 -07002423 } else {
Evan Roskya4cc3a92019-06-28 13:25:01 -07002424 task.setBounds(bounds);
Bryce Lee4e4a3ec2017-09-27 08:25:03 -07002425 }
2426 }
2427
Louis Changcdec0802019-11-11 11:45:07 +08002428 private void addOrReparentStartingActivity(Task parent, String reason) {
2429 if (mStartActivity.getTask() == null || mStartActivity.getTask() == parent) {
Wale Ogunwalec17418e2019-10-13 23:00:40 +02002430 parent.addChild(mStartActivity);
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08002431 } else {
Wale Ogunwale1a06f152019-10-11 11:26:30 +02002432 mStartActivity.reparent(parent, parent.getChildCount() /* top */, reason);
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08002433 }
Wale Ogunwale01d66562015-12-29 08:19:19 -08002434 }
2435
2436 private int adjustLaunchFlagsToDocumentMode(ActivityRecord r, boolean launchSingleInstance,
2437 boolean launchSingleTask, int launchFlags) {
2438 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
2439 (launchSingleInstance || launchSingleTask)) {
2440 // We have a conflict between the Intent and the Activity manifest, manifest wins.
2441 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
2442 "\"singleInstance\" or \"singleTask\"");
2443 launchFlags &=
2444 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | FLAG_ACTIVITY_MULTIPLE_TASK);
2445 } else {
2446 switch (r.info.documentLaunchMode) {
2447 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
2448 break;
2449 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
2450 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
2451 break;
2452 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
2453 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
2454 break;
2455 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
2456 launchFlags &= ~FLAG_ACTIVITY_MULTIPLE_TASK;
2457 break;
2458 }
2459 }
2460 return launchFlags;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08002461 }
2462
Bryce Leedacefc42017-10-10 12:56:02 -07002463 private ActivityStack computeStackFocus(ActivityRecord r, boolean newTask, int launchFlags,
2464 ActivityOptions aOptions) {
Louis Changcdec0802019-11-11 11:45:07 +08002465 final Task task = r.getTask();
Jorim Jaggi4ad98562016-04-19 20:30:47 -07002466 ActivityStack stack = getLaunchStack(r, launchFlags, task, aOptions);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08002467 if (stack != null) {
2468 return stack;
2469 }
2470
Andrii Kulian02b7a832016-10-06 23:11:56 -07002471 final ActivityStack currentStack = task != null ? task.getStack() : null;
Wale Ogunwaled32da472018-11-16 07:19:28 -08002472 final ActivityStack focusedStack = mRootActivityContainer.getTopDisplayFocusedStack();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002473 if (currentStack != null) {
Andrii Kulian52d255c2018-07-13 11:32:19 -07002474 if (focusedStack != currentStack) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08002475 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
2476 "computeStackFocus: Setting " + "focused stack to r=" + r
2477 + " task=" + task);
2478 } else {
2479 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Andrii Kulian52d255c2018-07-13 11:32:19 -07002480 "computeStackFocus: Focused stack already=" + focusedStack);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08002481 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002482 return currentStack;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08002483 }
2484
Andrii Kulianfb1bf692017-01-17 11:17:34 -08002485 if (canLaunchIntoFocusedStack(r, newTask)) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08002486 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Andrii Kulian52d255c2018-07-13 11:32:19 -07002487 "computeStackFocus: Have a focused stack=" + focusedStack);
2488 return focusedStack;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08002489 }
2490
David Stevense5a7b642017-05-22 13:18:23 -07002491 if (mPreferredDisplayId != DEFAULT_DISPLAY) {
Andrii Kuliana8fe3df2017-06-16 15:29:26 -07002492 // Try to put the activity in a stack on a secondary display.
Wale Ogunwaled32da472018-11-16 07:19:28 -08002493 stack = mRootActivityContainer.getValidLaunchStackOnDisplay(
2494 mPreferredDisplayId, r, aOptions, mLaunchParams);
Andrii Kuliana8fe3df2017-06-16 15:29:26 -07002495 if (stack == null) {
2496 // If source display is not suitable - look for topmost valid stack in the system.
2497 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
David Stevense5a7b642017-05-22 13:18:23 -07002498 "computeStackFocus: Can't launch on mPreferredDisplayId="
2499 + mPreferredDisplayId + ", looking on all displays.");
Wale Ogunwaled32da472018-11-16 07:19:28 -08002500 stack = mRootActivityContainer.getNextValidLaunchStack(r, mPreferredDisplayId);
Andrii Kuliana8fe3df2017-06-16 15:29:26 -07002501 }
2502 }
2503 if (stack == null) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08002504 stack = mRootActivityContainer.getLaunchStack(r, aOptions, task, ON_TOP);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08002505 }
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08002506 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS, "computeStackFocus: New stack r="
2507 + r + " stackId=" + stack.mStackId);
2508 return stack;
2509 }
2510
Andrii Kulianfb1bf692017-01-17 11:17:34 -08002511 /** Check if provided activity record can launch in currently focused stack. */
Wale Ogunwale68278562017-09-23 17:13:55 -07002512 // TODO: This method can probably be consolidated into getLaunchStack() below.
Andrii Kulianfb1bf692017-01-17 11:17:34 -08002513 private boolean canLaunchIntoFocusedStack(ActivityRecord r, boolean newTask) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08002514 final ActivityStack focusedStack = mRootActivityContainer.getTopDisplayFocusedStack();
Andrii Kulianfb1bf692017-01-17 11:17:34 -08002515 final boolean canUseFocusedStack;
Wale Ogunwale68278562017-09-23 17:13:55 -07002516 if (focusedStack.isActivityTypeAssistant()) {
2517 canUseFocusedStack = r.isActivityTypeAssistant();
2518 } else {
2519 switch (focusedStack.getWindowingMode()) {
2520 case WINDOWING_MODE_FULLSCREEN:
2521 // The fullscreen stack can contain any task regardless of if the task is
2522 // resizeable or not. So, we let the task go in the fullscreen task if it is the
2523 // focus stack.
2524 canUseFocusedStack = true;
2525 break;
2526 case WINDOWING_MODE_SPLIT_SCREEN_PRIMARY:
2527 case WINDOWING_MODE_SPLIT_SCREEN_SECONDARY:
2528 // Any activity which supports split screen can go in the docked stack.
2529 canUseFocusedStack = r.supportsSplitScreenWindowingMode();
2530 break;
2531 case WINDOWING_MODE_FREEFORM:
2532 // Any activity which supports freeform can go in the freeform stack.
2533 canUseFocusedStack = r.supportsFreeform();
2534 break;
2535 default:
2536 // Dynamic stacks behave similarly to the fullscreen stack and can contain any
2537 // resizeable task.
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002538 canUseFocusedStack = !focusedStack.isOnHomeDisplay()
Wale Ogunwale68278562017-09-23 17:13:55 -07002539 && r.canBeLaunchedOnDisplay(focusedStack.mDisplayId);
2540 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08002541 }
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07002542 return canUseFocusedStack && !newTask
Wale Ogunwale68278562017-09-23 17:13:55 -07002543 // Using the focus stack isn't important enough to override the preferred display.
David Stevense5a7b642017-05-22 13:18:23 -07002544 && (mPreferredDisplayId == focusedStack.mDisplayId);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08002545 }
2546
Louis Changcdec0802019-11-11 11:45:07 +08002547 private ActivityStack getLaunchStack(ActivityRecord r, int launchFlags, Task task,
Jorim Jaggi4ad98562016-04-19 20:30:47 -07002548 ActivityOptions aOptions) {
Bryce Leea19b5ad2017-06-07 16:54:11 -07002549 // We are reusing a task, keep the stack!
2550 if (mReuseTask != null) {
2551 return mReuseTask.getStack();
2552 }
Jorim Jaggib8c58762016-04-20 17:58:29 -07002553
Karthik Ravi Shankar99493db2017-03-08 18:30:19 -08002554 if (((launchFlags & FLAG_ACTIVITY_LAUNCH_ADJACENT) == 0)
David Stevense5a7b642017-05-22 13:18:23 -07002555 || mPreferredDisplayId != DEFAULT_DISPLAY) {
Louis Chang37317152019-05-09 09:53:58 +08002556 final boolean onTop =
2557 (aOptions == null || !aOptions.getAvoidMoveToFront()) && !mLaunchTaskBehind;
Wale Ogunwaled32da472018-11-16 07:19:28 -08002558 final ActivityStack stack =
lumarkf65e02d2019-09-14 19:25:21 +08002559 mRootActivityContainer.getLaunchStack(r, aOptions, task, onTop, mLaunchParams,
2560 mRequest.realCallingPid, mRequest.realCallingUid);
Garfield Tan20d9e2f2018-11-16 15:42:29 -08002561 return stack;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08002562 }
Andrii Kulian4ac2a582016-03-25 00:07:38 -07002563 // Otherwise handle adjacent launch.
Wale Ogunwale854809c2015-12-27 16:18:19 -08002564
Wale Ogunwaled32da472018-11-16 07:19:28 -08002565 final ActivityStack focusedStack = mRootActivityContainer.getTopDisplayFocusedStack();
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08002566 // The parent activity doesn't want to launch the activity on top of itself, but
2567 // instead tries to put it onto other side in side-by-side mode.
Andrii Kulian52d255c2018-07-13 11:32:19 -07002568 final ActivityStack parentStack = task != null ? task.getStack(): focusedStack;
Andrii Kulian4ac2a582016-03-25 00:07:38 -07002569
Andrii Kulian52d255c2018-07-13 11:32:19 -07002570 if (parentStack != focusedStack) {
Andrii Kulian4ac2a582016-03-25 00:07:38 -07002571 // If task's parent stack is not focused - use it during adjacent launch.
2572 return parentStack;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08002573 } else {
Andrii Kulian52d255c2018-07-13 11:32:19 -07002574 if (focusedStack != null && task == focusedStack.topTask()) {
Andrii Kulian4ac2a582016-03-25 00:07:38 -07002575 // If task is already on top of focused stack - use it. We don't want to move the
2576 // existing focused task to adjacent stack, just deliver new intent in this case.
Andrii Kulian52d255c2018-07-13 11:32:19 -07002577 return focusedStack;
Andrii Kulian4ac2a582016-03-25 00:07:38 -07002578 }
2579
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002580 if (parentStack != null && parentStack.inSplitScreenPrimaryWindowingMode()) {
Andrii Kulian4ac2a582016-03-25 00:07:38 -07002581 // If parent was in docked stack, the natural place to launch another activity
2582 // will be fullscreen, so it can appear alongside the docked window.
Wale Ogunwaled32da472018-11-16 07:19:28 -08002583 final int activityType =
2584 mRootActivityContainer.resolveActivityType(r, mOptions, task);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002585 return parentStack.getDisplay().getOrCreateStack(
2586 WINDOWING_MODE_SPLIT_SCREEN_SECONDARY, activityType, ON_TOP);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08002587 } else {
Andrii Kulian4ac2a582016-03-25 00:07:38 -07002588 // If the parent is not in the docked stack, we check if there is docked window
2589 // and if yes, we will launch into that stack. If not, we just put the new
2590 // activity into parent's stack, because we can't find a better place.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002591 final ActivityStack dockedStack =
Wale Ogunwaled32da472018-11-16 07:19:28 -08002592 mRootActivityContainer.getDefaultDisplay().getSplitScreenPrimaryStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002593 if (dockedStack != null && !dockedStack.shouldBeVisible(r)) {
Andrii Kulian4ac2a582016-03-25 00:07:38 -07002594 // There is a docked stack, but it isn't visible, so we can't launch into that.
Wale Ogunwaled32da472018-11-16 07:19:28 -08002595 return mRootActivityContainer.getLaunchStack(r, aOptions, task, ON_TOP);
Andrii Kulian4ac2a582016-03-25 00:07:38 -07002596 } else {
2597 return dockedStack;
2598 }
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08002599 }
2600 }
2601 }
2602
Bryce Lee7daee392017-10-12 13:46:18 -07002603 private boolean isLaunchModeOneOf(int mode1, int mode2) {
2604 return mode1 == mLaunchMode || mode2 == mLaunchMode;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08002605 }
2606
Daichi Hirono15a02992016-04-27 18:47:01 +09002607 static boolean isDocumentLaunchesIntoExisting(int flags) {
2608 return (flags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
2609 (flags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0;
2610 }
liulvpingcfa825f2016-09-26 20:00:15 +08002611
Bryce Lee4c9a5972017-12-01 22:14:24 -08002612 ActivityStarter setIntent(Intent intent) {
2613 mRequest.intent = intent;
2614 return this;
2615 }
2616
Bryce Lee32e09ef2018-03-19 15:29:49 -07002617 @VisibleForTesting
2618 Intent getIntent() {
2619 return mRequest.intent;
2620 }
2621
Bryce Lee4c9a5972017-12-01 22:14:24 -08002622 ActivityStarter setReason(String reason) {
2623 mRequest.reason = reason;
2624 return this;
2625 }
2626
2627 ActivityStarter setCaller(IApplicationThread caller) {
2628 mRequest.caller = caller;
2629 return this;
2630 }
2631
Bryce Lee4c9a5972017-12-01 22:14:24 -08002632 ActivityStarter setResolvedType(String type) {
2633 mRequest.resolvedType = type;
2634 return this;
2635 }
2636
2637 ActivityStarter setActivityInfo(ActivityInfo info) {
2638 mRequest.activityInfo = info;
2639 return this;
2640 }
2641
2642 ActivityStarter setResolveInfo(ResolveInfo info) {
2643 mRequest.resolveInfo = info;
2644 return this;
2645 }
2646
2647 ActivityStarter setVoiceSession(IVoiceInteractionSession voiceSession) {
2648 mRequest.voiceSession = voiceSession;
2649 return this;
2650 }
2651
2652 ActivityStarter setVoiceInteractor(IVoiceInteractor voiceInteractor) {
2653 mRequest.voiceInteractor = voiceInteractor;
2654 return this;
2655 }
2656
2657 ActivityStarter setResultTo(IBinder resultTo) {
2658 mRequest.resultTo = resultTo;
2659 return this;
2660 }
2661
2662 ActivityStarter setResultWho(String resultWho) {
2663 mRequest.resultWho = resultWho;
2664 return this;
2665 }
2666
2667 ActivityStarter setRequestCode(int requestCode) {
2668 mRequest.requestCode = requestCode;
2669 return this;
2670 }
2671
lumarkf65e02d2019-09-14 19:25:21 +08002672 /**
2673 * Sets the pid of the caller who originally started the activity.
2674 *
2675 * Normally, the pid/uid would be the calling pid from the binder call.
2676 * However, in case of a {@link PendingIntent}, the pid/uid pair of the caller is considered
2677 * the original entity that created the pending intent, in contrast to setRealCallingPid/Uid,
2678 * which represents the entity who invoked pending intent via {@link PendingIntent#send}.
2679 */
Bryce Lee4c9a5972017-12-01 22:14:24 -08002680 ActivityStarter setCallingPid(int pid) {
2681 mRequest.callingPid = pid;
2682 return this;
2683 }
2684
lumarkf65e02d2019-09-14 19:25:21 +08002685 /**
2686 * Sets the uid of the caller who originally started the activity.
2687 *
2688 * @see #setCallingPid
2689 */
Bryce Lee4c9a5972017-12-01 22:14:24 -08002690 ActivityStarter setCallingUid(int uid) {
2691 mRequest.callingUid = uid;
2692 return this;
2693 }
2694
2695 ActivityStarter setCallingPackage(String callingPackage) {
2696 mRequest.callingPackage = callingPackage;
2697 return this;
2698 }
2699
lumarkf65e02d2019-09-14 19:25:21 +08002700 /**
2701 * Sets the pid of the caller who requested to launch the activity.
2702 *
2703 * The pid/uid represents the caller who launches the activity in this request.
2704 * It will almost same as setCallingPid/Uid except when processing {@link PendingIntent}:
2705 * the pid/uid will be the caller who called {@link PendingIntent#send()}.
2706 *
2707 * @see #setCallingPid
2708 */
Bryce Lee4c9a5972017-12-01 22:14:24 -08002709 ActivityStarter setRealCallingPid(int pid) {
2710 mRequest.realCallingPid = pid;
2711 return this;
2712 }
2713
lumarkf65e02d2019-09-14 19:25:21 +08002714 /**
2715 * Sets the uid of the caller who requested to launch the activity.
2716 *
2717 * @see #setRealCallingPid
2718 */
Bryce Lee4c9a5972017-12-01 22:14:24 -08002719 ActivityStarter setRealCallingUid(int uid) {
2720 mRequest.realCallingUid = uid;
2721 return this;
2722 }
2723
2724 ActivityStarter setStartFlags(int startFlags) {
2725 mRequest.startFlags = startFlags;
2726 return this;
2727 }
2728
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01002729 ActivityStarter setActivityOptions(SafeActivityOptions options) {
Bryce Lee4c9a5972017-12-01 22:14:24 -08002730 mRequest.activityOptions = options;
2731 return this;
2732 }
2733
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01002734 ActivityStarter setActivityOptions(Bundle bOptions) {
2735 return setActivityOptions(SafeActivityOptions.fromBundle(bOptions));
2736 }
2737
Bryce Lee4c9a5972017-12-01 22:14:24 -08002738 ActivityStarter setIgnoreTargetSecurity(boolean ignoreTargetSecurity) {
2739 mRequest.ignoreTargetSecurity = ignoreTargetSecurity;
2740 return this;
2741 }
2742
Patrick Baumann31426b22018-05-21 13:46:40 -07002743 ActivityStarter setFilterCallingUid(int filterCallingUid) {
2744 mRequest.filterCallingUid = filterCallingUid;
2745 return this;
2746 }
2747
Bryce Lee4c9a5972017-12-01 22:14:24 -08002748 ActivityStarter setComponentSpecified(boolean componentSpecified) {
2749 mRequest.componentSpecified = componentSpecified;
2750 return this;
2751 }
2752
2753 ActivityStarter setOutActivity(ActivityRecord[] outActivity) {
2754 mRequest.outActivity = outActivity;
2755 return this;
2756 }
2757
Louis Changcdec0802019-11-11 11:45:07 +08002758 ActivityStarter setInTask(Task inTask) {
Bryce Lee4c9a5972017-12-01 22:14:24 -08002759 mRequest.inTask = inTask;
2760 return this;
2761 }
2762
2763 ActivityStarter setWaitResult(WaitResult result) {
2764 mRequest.waitResult = result;
2765 return this;
2766 }
2767
2768 ActivityStarter setProfilerInfo(ProfilerInfo info) {
2769 mRequest.profilerInfo = info;
2770 return this;
2771 }
2772
2773 ActivityStarter setGlobalConfiguration(Configuration config) {
2774 mRequest.globalConfig = config;
2775 return this;
2776 }
2777
Bryce Lee4c9a5972017-12-01 22:14:24 -08002778 ActivityStarter setUserId(int userId) {
2779 mRequest.userId = userId;
2780 return this;
2781 }
2782
Jorim Jaggi6fa41c32018-04-23 18:35:00 +02002783 ActivityStarter setAllowPendingRemoteAnimationRegistryLookup(boolean allowLookup) {
2784 mRequest.allowPendingRemoteAnimationRegistryLookup = allowLookup;
2785 return this;
2786 }
2787
Michal Karpinski201bc0c2018-07-20 15:32:00 +01002788 ActivityStarter setOriginatingPendingIntent(PendingIntentRecord originatingPendingIntent) {
2789 mRequest.originatingPendingIntent = originatingPendingIntent;
2790 return this;
2791 }
2792
Michal Karpinskiac116df2018-12-10 17:51:42 +00002793 ActivityStarter setAllowBackgroundActivityStart(boolean allowBackgroundActivityStart) {
2794 mRequest.allowBackgroundActivityStart = allowBackgroundActivityStart;
2795 return this;
2796 }
2797
Bryce Leed3624e12017-11-30 08:51:45 -08002798 void dump(PrintWriter pw, String prefix) {
Wale Ogunwale692dcd62017-06-20 13:38:14 -07002799 prefix = prefix + " ";
Dianne Hackborne676ec72017-07-25 10:55:08 -07002800 pw.print(prefix);
2801 pw.print("mCurrentUser=");
Wale Ogunwaled32da472018-11-16 07:19:28 -08002802 pw.println(mRootActivityContainer.mCurrentUser);
Dianne Hackborne676ec72017-07-25 10:55:08 -07002803 pw.print(prefix);
2804 pw.print("mLastStartReason=");
2805 pw.println(mLastStartReason);
2806 pw.print(prefix);
2807 pw.print("mLastStartActivityTimeMs=");
2808 pw.println(DateFormat.getDateTimeInstance().format(new Date(mLastStartActivityTimeMs)));
2809 pw.print(prefix);
2810 pw.print("mLastStartActivityResult=");
2811 pw.println(mLastStartActivityResult);
Louis Chang54fbb052019-10-16 17:10:17 +08002812 if (mLastStartActivityRecord != null) {
Dianne Hackborne676ec72017-07-25 10:55:08 -07002813 pw.print(prefix);
2814 pw.println("mLastStartActivityRecord:");
Louis Chang54fbb052019-10-16 17:10:17 +08002815 mLastStartActivityRecord.dump(pw, prefix + " ", true /* dumpAll */);
Wale Ogunwale692dcd62017-06-20 13:38:14 -07002816 }
Wale Ogunwale692dcd62017-06-20 13:38:14 -07002817 if (mStartActivity != null) {
Dianne Hackborne676ec72017-07-25 10:55:08 -07002818 pw.print(prefix);
2819 pw.println("mStartActivity:");
Garfield Tane8d84ab2019-10-11 09:49:40 -07002820 mStartActivity.dump(pw, prefix + " ", true /* dumpAll */);
Wale Ogunwale692dcd62017-06-20 13:38:14 -07002821 }
2822 if (mIntent != null) {
Dianne Hackborne676ec72017-07-25 10:55:08 -07002823 pw.print(prefix);
2824 pw.print("mIntent=");
2825 pw.println(mIntent);
Wale Ogunwale692dcd62017-06-20 13:38:14 -07002826 }
2827 if (mOptions != null) {
Dianne Hackborne676ec72017-07-25 10:55:08 -07002828 pw.print(prefix);
2829 pw.print("mOptions=");
2830 pw.println(mOptions);
Wale Ogunwale692dcd62017-06-20 13:38:14 -07002831 }
Dianne Hackborne676ec72017-07-25 10:55:08 -07002832 pw.print(prefix);
2833 pw.print("mLaunchSingleTop=");
Bryce Lee7daee392017-10-12 13:46:18 -07002834 pw.print(LAUNCH_SINGLE_TOP == mLaunchMode);
Dianne Hackborne676ec72017-07-25 10:55:08 -07002835 pw.print(" mLaunchSingleInstance=");
Bryce Lee7daee392017-10-12 13:46:18 -07002836 pw.print(LAUNCH_SINGLE_INSTANCE == mLaunchMode);
Dianne Hackborne676ec72017-07-25 10:55:08 -07002837 pw.print(" mLaunchSingleTask=");
Bryce Lee7daee392017-10-12 13:46:18 -07002838 pw.println(LAUNCH_SINGLE_TASK == mLaunchMode);
Dianne Hackborne676ec72017-07-25 10:55:08 -07002839 pw.print(prefix);
2840 pw.print("mLaunchFlags=0x");
2841 pw.print(Integer.toHexString(mLaunchFlags));
2842 pw.print(" mDoResume=");
2843 pw.print(mDoResume);
2844 pw.print(" mAddingToTask=");
2845 pw.println(mAddingToTask);
Wale Ogunwale692dcd62017-06-20 13:38:14 -07002846 }
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08002847}