blob: baa295518c2d46121ea137bc2ceb17193ca62491 [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
Wale Ogunwale59507092018-10-29 09:00:30 -070063import static com.android.server.wm.ActivityStack.ActivityState.RESUMED;
64import static com.android.server.wm.ActivityStackSupervisor.DEFER_RESUME;
65import static com.android.server.wm.ActivityStackSupervisor.ON_TOP;
66import static com.android.server.wm.ActivityStackSupervisor.PRESERVE_WINDOWS;
67import static com.android.server.wm.ActivityStackSupervisor.TAG_TASKS;
68import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_CONFIGURATION;
69import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_FOCUS;
70import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_PERMISSIONS_REVIEW;
71import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_RESULTS;
72import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_STACK;
73import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_TASKS;
74import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_USER_LEAVING;
75import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_CONFIGURATION;
76import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_FOCUS;
77import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_RESULTS;
78import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_USER_LEAVING;
79import static com.android.server.wm.ActivityTaskManagerDebugConfig.TAG_ATM;
80import static com.android.server.wm.ActivityTaskManagerDebugConfig.TAG_WITH_CLASS_NAME;
81import static com.android.server.wm.ActivityTaskManagerService.ANIMATE;
Louis Chang6fb1e842018-12-03 16:07:50 +080082import static com.android.server.wm.LaunchParamsController.LaunchParamsModifier.PHASE_BOUNDS;
83import static com.android.server.wm.LaunchParamsController.LaunchParamsModifier.PHASE_DISPLAY;
Louis Changcdec0802019-11-11 11:45:07 +080084import static com.android.server.wm.Task.REPARENT_MOVE_STACK_TO_FRONT;
Winson Chung74666102017-02-22 17:49:24 -080085
Todd Kennedye9910222017-02-21 16:00:11 -080086import android.annotation.NonNull;
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +010087import android.annotation.Nullable;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -080088import android.app.ActivityManager;
89import android.app.ActivityOptions;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -080090import android.app.IApplicationThread;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -080091import android.app.PendingIntent;
92import android.app.ProfilerInfo;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -070093import android.app.WaitResult;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -080094import android.content.IIntentSender;
95import android.content.Intent;
96import android.content.IntentSender;
97import android.content.pm.ActivityInfo;
98import android.content.pm.ApplicationInfo;
Todd Kennedye9910222017-02-21 16:00:11 -080099import android.content.pm.AuxiliaryResolveInfo;
Kenny Guyb1b30262016-02-09 16:02:35 +0000100import android.content.pm.PackageManager;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800101import android.content.pm.ResolveInfo;
Kenny Guyb1b30262016-02-09 16:02:35 +0000102import android.content.pm.UserInfo;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800103import android.content.res.Configuration;
104import android.graphics.Rect;
105import android.os.Binder;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800106import android.os.Bundle;
107import android.os.IBinder;
Michal Karpinski8596ded2018-11-14 14:43:48 +0000108import android.os.Process;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800109import android.os.RemoteException;
Michal Karpinski201bc0c2018-07-20 15:32:00 +0100110import android.os.Trace;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800111import android.os.UserHandle;
Kenny Guyb1b30262016-02-09 16:02:35 +0000112import android.os.UserManager;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800113import android.service.voice.IVoiceInteractionSession;
Wale Ogunwale692dcd62017-06-20 13:38:14 -0700114import android.text.TextUtils;
Michal Karpinskib7daac22019-03-25 10:12:41 +0000115import android.util.ArraySet;
Bryce Leedaa91e42017-12-06 14:13:01 -0800116import android.util.Pools.SynchronizedPool;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800117import android.util.Slog;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800118
Bryce Leed3624e12017-11-30 08:51:45 -0800119import com.android.internal.annotations.VisibleForTesting;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800120import com.android.internal.app.HeavyWeightSwitcherActivity;
121import com.android.internal.app.IVoiceInteractor;
Wale Ogunwale59507092018-10-29 09:00:30 -0700122import com.android.server.am.PendingIntentRecord;
Louis Changdd3592a2018-11-05 11:04:14 +0800123import com.android.server.pm.InstantAppResolver;
Riddle Hsufd66d4d2019-11-14 10:35:55 +0800124import com.android.server.wm.ActivityMetricsLogger.LaunchingState;
Wale Ogunwale59507092018-10-29 09:00:30 -0700125import com.android.server.wm.ActivityStackSupervisor.PendingActivityLaunch;
126import com.android.server.wm.LaunchParamsController.LaunchParams;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800127
Wale Ogunwale692dcd62017-06-20 13:38:14 -0700128import java.io.PrintWriter;
129import java.text.DateFormat;
Wale Ogunwale692dcd62017-06-20 13:38:14 -0700130import java.util.Date;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800131
132/**
Bryce Leed3624e12017-11-30 08:51:45 -0800133 * Controller for interpreting how and then launching an activity.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800134 *
135 * This class collects all the logic for determining how an intent and flags should be turned into
136 * an activity and associated task and stack.
137 */
Wale Ogunwale01d66562015-12-29 08:19:19 -0800138class ActivityStarter {
Wale Ogunwale98875612018-10-12 07:53:02 -0700139 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStarter" : TAG_ATM;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800140 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
141 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
142 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
143 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
Bryce Lee7daee392017-10-12 13:46:18 -0700144 private static final int INVALID_LAUNCH_MODE = -1;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800145
Wale Ogunwalec9e57de2018-05-08 14:28:07 -0700146 private final ActivityTaskManagerService mService;
Wale Ogunwaled32da472018-11-16 07:19:28 -0800147 private final RootActivityContainer mRootActivityContainer;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800148 private final ActivityStackSupervisor mSupervisor;
Benjamin Franz563707b2017-06-29 15:06:13 +0100149 private final ActivityStartInterceptor mInterceptor;
Bryce Leed3624e12017-11-30 08:51:45 -0800150 private final ActivityStartController mController;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800151
Wale Ogunwale01d66562015-12-29 08:19:19 -0800152 // Share state variable among methods when starting an activity.
Louis Chang07b13002019-11-27 22:08:37 +0800153 @VisibleForTesting
154 ActivityRecord mStartActivity;
Wale Ogunwale01d66562015-12-29 08:19:19 -0800155 private Intent mIntent;
156 private int mCallingUid;
157 private ActivityOptions mOptions;
158
Ricky Waib147fa12019-04-25 16:08:30 +0100159 // If it is true, background activity can only be started in an existing task that contains
Alan Stokes07389b62019-05-20 15:22:54 +0100160 // an activity with same uid, or if activity starts are enabled in developer options.
Ricky Waib147fa12019-04-25 16:08:30 +0100161 private boolean mRestrictedBgActivity;
162
Bryce Lee7daee392017-10-12 13:46:18 -0700163 private int mLaunchMode;
Wale Ogunwale01d66562015-12-29 08:19:19 -0800164 private boolean mLaunchTaskBehind;
165 private int mLaunchFlags;
166
Bryce Leeec55eb02017-12-05 20:51:27 -0800167 private LaunchParams mLaunchParams = new LaunchParams();
Wale Ogunwale01d66562015-12-29 08:19:19 -0800168
169 private ActivityRecord mNotTop;
170 private boolean mDoResume;
171 private int mStartFlags;
172 private ActivityRecord mSourceRecord;
Bryce Lee7daee392017-10-12 13:46:18 -0700173
David Stevense5a7b642017-05-22 13:18:23 -0700174 // The display to launch the activity onto, barring any strong reason to do otherwise.
175 private int mPreferredDisplayId;
Wale Ogunwale01d66562015-12-29 08:19:19 -0800176
Louis Changcdec0802019-11-11 11:45:07 +0800177 private Task mInTask;
Louis Chang07b13002019-11-27 22:08:37 +0800178 @VisibleForTesting
179 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 {
Riddle Hsufd66d4d2019-11-14 10:35:55 +0800576 final LaunchingState launchingState = mSupervisor.getActivityMetricsLogger()
577 .notifyActivityLaunching(r.intent, r.resultTo);
Louis Chang54fbb052019-10-16 17:10:17 +0800578 mLastStartReason = "startResolvedActivity";
579 mLastStartActivityTimeMs = System.currentTimeMillis();
580 mLastStartActivityRecord = r;
581 mLastStartActivityResult = startActivityUnchecked(r, sourceRecord, voiceSession,
582 voiceInteractor, startFlags, doResume, options, inTask,
583 false /* restrictedBgActivity */);
Riddle Hsufd66d4d2019-11-14 10:35:55 +0800584 mSupervisor.getActivityMetricsLogger().notifyActivityLaunched(launchingState,
585 mLastStartActivityResult, mLastStartActivityRecord);
Louis Chang54fbb052019-10-16 17:10:17 +0800586 } finally {
587 onExecutionComplete();
588 }
589 }
590
591 /**
592 * Resolve necessary information according the request parameters provided earlier, and execute
593 * the request which begin the journey of starting an activity.
Bryce Lee4c9a5972017-12-01 22:14:24 -0800594 * @return The starter result.
595 */
596 int execute() {
Bryce Leedaa91e42017-12-06 14:13:01 -0800597 try {
Louis Chang54fbb052019-10-16 17:10:17 +0800598 // Refuse possible leaked file descriptors
599 if (mRequest.intent != null && mRequest.intent.hasFileDescriptors()) {
600 throw new IllegalArgumentException("File descriptors passed in Intent");
601 }
602
Riddle Hsufd66d4d2019-11-14 10:35:55 +0800603 final LaunchingState launchingState;
604 synchronized (mService.mGlobalLock) {
605 final ActivityRecord caller = ActivityRecord.forTokenLocked(mRequest.resultTo);
606 launchingState = mSupervisor.getActivityMetricsLogger().notifyActivityLaunching(
607 mRequest.intent, caller);
608 }
Louis Chang54fbb052019-10-16 17:10:17 +0800609
Riddle Hsufd66d4d2019-11-14 10:35:55 +0800610 // Do not lock the resolving to avoid potential deadlock.
Louis Chang54fbb052019-10-16 17:10:17 +0800611 if (mRequest.activityInfo == null) {
612 mRequest.resolveActivity(mSupervisor);
613 }
614
615 int res;
616 synchronized (mService.mGlobalLock) {
617 final ActivityStack stack = mRootActivityContainer.getTopDisplayFocusedStack();
618 stack.mConfigWillChange = mRequest.globalConfig != null
619 && mService.getGlobalConfiguration().diff(mRequest.globalConfig) != 0;
620 if (DEBUG_CONFIGURATION) {
621 Slog.v(TAG_CONFIGURATION, "Starting activity when config will change = "
622 + stack.mConfigWillChange);
623 }
624
625 final long origId = Binder.clearCallingIdentity();
626
627 res = resolveToHeavyWeightSwitcherIfNeeded();
628 if (res != START_SUCCESS) {
629 return res;
630 }
631 res = executeRequest(mRequest);
632
633 Binder.restoreCallingIdentity(origId);
634
635 if (stack.mConfigWillChange) {
636 // If the caller also wants to switch to a new configuration, do so now.
637 // This allows a clean switch, as we are waiting for the current activity
638 // to pause (so we will not destroy it), and have not yet started the
639 // next activity.
640 mService.mAmInternal.enforceCallingPermission(
641 android.Manifest.permission.CHANGE_CONFIGURATION,
642 "updateConfiguration()");
643 stack.mConfigWillChange = false;
644 if (DEBUG_CONFIGURATION) {
645 Slog.v(TAG_CONFIGURATION,
646 "Updating to new configuration after starting activity.");
647 }
648 mService.updateConfigurationLocked(mRequest.globalConfig, null, false);
649 }
650
651 // Notify ActivityMetricsLogger that the activity has launched.
652 // ActivityMetricsLogger will then wait for the windows to be drawn and populate
653 // WaitResult.
Riddle Hsufd66d4d2019-11-14 10:35:55 +0800654 mSupervisor.getActivityMetricsLogger().notifyActivityLaunched(launchingState, res,
Louis Chang54fbb052019-10-16 17:10:17 +0800655 mLastStartActivityRecord);
656 return getExternalResult(mRequest.waitResult == null ? res
657 : waitForResult(res, mLastStartActivityRecord));
Bryce Leedaa91e42017-12-06 14:13:01 -0800658 }
659 } finally {
660 onExecutionComplete();
661 }
662 }
663
664 /**
Louis Chang54fbb052019-10-16 17:10:17 +0800665 * Updates the request to heavy-weight switch if this is a heavy-weight process while there
666 * already have another, different heavy-weight process running.
Bryce Leedaa91e42017-12-06 14:13:01 -0800667 */
Louis Chang54fbb052019-10-16 17:10:17 +0800668 private int resolveToHeavyWeightSwitcherIfNeeded() {
669 if (mRequest.activityInfo == null || !mService.mHasHeavyWeightFeature
670 || (mRequest.activityInfo.applicationInfo.privateFlags
671 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) == 0) {
672 return START_SUCCESS;
Wale Ogunwale692dcd62017-06-20 13:38:14 -0700673 }
Bryce Leef9d49542017-06-26 16:27:32 -0700674
Louis Chang54fbb052019-10-16 17:10:17 +0800675 if (!mRequest.activityInfo.processName.equals(
676 mRequest.activityInfo.applicationInfo.packageName)) {
677 return START_SUCCESS;
678 }
Bryce Lee93e7f792017-10-25 15:54:55 -0700679
Louis Chang54fbb052019-10-16 17:10:17 +0800680 final WindowProcessController heavy = mService.mHeavyWeightProcess;
681 if (heavy == null || (heavy.mInfo.uid == mRequest.activityInfo.applicationInfo.uid
682 && heavy.mName.equals(mRequest.activityInfo.processName))) {
683 return START_SUCCESS;
684 }
685
686 int appCallingUid = mRequest.callingUid;
687 if (mRequest.caller != null) {
688 WindowProcessController callerApp = mService.getProcessController(mRequest.caller);
689 if (callerApp != null) {
690 appCallingUid = callerApp.mInfo.uid;
691 } else {
692 Slog.w(TAG, "Unable to find app for caller " + mRequest.caller + " (pid="
693 + mRequest.callingPid + ") when starting: " + mRequest.intent.toString());
694 SafeActivityOptions.abort(mRequest.activityOptions);
695 return ActivityManager.START_PERMISSION_DENIED;
696 }
697 }
698
699 final IIntentSender target = mService.getIntentSenderLocked(
700 ActivityManager.INTENT_SENDER_ACTIVITY, "android" /* packageName */, appCallingUid,
701 mRequest.userId, null /* token */, null /* resultWho*/, 0 /* requestCode*/,
702 new Intent[] { mRequest.intent }, new String[] { mRequest.resolvedType },
703 PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT,
704 null /* bOptions */);
705
706 final Intent newIntent = new Intent();
707 if (mRequest.requestCode >= 0) {
708 // Caller is requesting a result.
709 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
710 }
711 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT, new IntentSender(target));
712 heavy.updateIntentForHeavyWeightActivity(newIntent);
713 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
714 mRequest.activityInfo.packageName);
715 newIntent.setFlags(mRequest.intent.getFlags());
716 newIntent.setClassName("android" /* packageName */,
717 HeavyWeightSwitcherActivity.class.getName());
718 mRequest.intent = newIntent;
719 mRequest.resolvedType = null;
720 mRequest.caller = null;
721 mRequest.callingUid = Binder.getCallingUid();
722 mRequest.callingPid = Binder.getCallingPid();
723 mRequest.componentSpecified = true;
724 mRequest.resolveInfo = mSupervisor.resolveIntent(mRequest.intent, null /* resolvedType */,
725 mRequest.userId, 0 /* matchFlags */,
726 computeResolveFilterUid(mRequest.callingUid, mRequest.realCallingUid,
727 mRequest.filterCallingUid));
728 mRequest.activityInfo =
729 mRequest.resolveInfo != null ? mRequest.resolveInfo.activityInfo : null;
730 if (mRequest.activityInfo != null) {
731 mRequest.activityInfo = mService.mAmInternal.getActivityInfoForUser(
732 mRequest.activityInfo, mRequest.userId);
733 }
734
735 return START_SUCCESS;
Wale Ogunwale692dcd62017-06-20 13:38:14 -0700736 }
737
Bryce Leedaa91e42017-12-06 14:13:01 -0800738 /**
Louis Chang54fbb052019-10-16 17:10:17 +0800739 * Wait for activity launch completes.
Bryce Leedaa91e42017-12-06 14:13:01 -0800740 */
Louis Chang54fbb052019-10-16 17:10:17 +0800741 private int waitForResult(int res, ActivityRecord r) {
742 mRequest.waitResult.result = res;
743 switch(res) {
744 case START_SUCCESS: {
745 mSupervisor.mWaitingActivityLaunched.add(mRequest.waitResult);
746 do {
747 try {
748 mService.mGlobalLock.wait();
749 } catch (InterruptedException e) {
750 }
751 } while (mRequest.waitResult.result != START_TASK_TO_FRONT
752 && !mRequest.waitResult.timeout && mRequest.waitResult.who == null);
753 if (mRequest.waitResult.result == START_TASK_TO_FRONT) {
754 res = START_TASK_TO_FRONT;
755 }
756 break;
757 }
758 case START_DELIVERED_TO_TOP: {
759 mRequest.waitResult.timeout = false;
760 mRequest.waitResult.who = r.mActivityComponent;
761 mRequest.waitResult.totalTime = 0;
762 break;
763 }
764 case START_TASK_TO_FRONT: {
765 mRequest.waitResult.launchState =
766 r.attachedToProcess() ? LAUNCH_STATE_HOT : LAUNCH_STATE_COLD;
767 // ActivityRecord may represent a different activity, but it should not be
768 // in the resumed state.
769 if (r.nowVisible && r.isState(RESUMED)) {
770 mRequest.waitResult.timeout = false;
771 mRequest.waitResult.who = r.mActivityComponent;
772 mRequest.waitResult.totalTime = 0;
773 } else {
Riddle Hsuc48c8912019-10-31 13:34:27 +0800774 mSupervisor.waitActivityVisible(r.mActivityComponent, mRequest.waitResult);
Louis Chang54fbb052019-10-16 17:10:17 +0800775 // Note: the timeout variable is not currently not ever set.
776 do {
777 try {
778 mService.mGlobalLock.wait();
779 } catch (InterruptedException e) {
780 }
781 } while (!mRequest.waitResult.timeout && mRequest.waitResult.who == null);
782 }
783 break;
784 }
785 }
786 return res;
Bryce Leedaa91e42017-12-06 14:13:01 -0800787 }
788
Louis Chang54fbb052019-10-16 17:10:17 +0800789 /**
790 * Executing activity start request and starts the journey of starting an activity. Here
791 * begins with performing several preliminary checks. The normally activity launch flow will
792 * go through {@link #startActivityUnchecked} to {@link #startActivityInner}.
793 */
794 private int executeRequest(Request request) {
795 if (TextUtils.isEmpty(request.reason)) {
796 throw new IllegalArgumentException("Need to specify a reason.");
797 }
798 mLastStartReason = request.reason;
799 mLastStartActivityTimeMs = System.currentTimeMillis();
800 mLastStartActivityRecord = null;
801
802 final IApplicationThread caller = request.caller;
803 Intent intent = request.intent;
804 String resolvedType = request.resolvedType;
805 ActivityInfo aInfo = request.activityInfo;
806 ResolveInfo rInfo = request.resolveInfo;
807 final IVoiceInteractionSession voiceSession = request.voiceSession;
808 final IBinder resultTo = request.resultTo;
809 String resultWho = request.resultWho;
810 int requestCode = request.requestCode;
811 int callingPid = request.callingPid;
812 int callingUid = request.callingUid;
813 String callingPackage = request.callingPackage;
814 final int realCallingPid = request.realCallingPid;
815 final int realCallingUid = request.realCallingUid;
816 final int startFlags = request.startFlags;
817 final SafeActivityOptions options = request.activityOptions;
Louis Changcdec0802019-11-11 11:45:07 +0800818 Task inTask = request.inTask;
Louis Chang54fbb052019-10-16 17:10:17 +0800819
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800820 int err = ActivityManager.START_SUCCESS;
Chad Brubaker06068612017-04-06 09:43:47 -0700821 // Pull the optional Ephemeral Installer-only bundle out of the options early.
Louis Chang54fbb052019-10-16 17:10:17 +0800822 final Bundle verificationBundle =
823 options != null ? options.popAppVerificationBundle() : null;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800824
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700825 WindowProcessController callerApp = null;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800826 if (caller != null) {
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700827 callerApp = mService.getProcessController(caller);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800828 if (callerApp != null) {
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700829 callingPid = callerApp.getPid();
830 callingUid = callerApp.mInfo.uid;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800831 } else {
Louis Chang54fbb052019-10-16 17:10:17 +0800832 Slog.w(TAG, "Unable to find app for caller " + caller + " (pid=" + callingPid
833 + ") when starting: " + intent.toString());
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800834 err = ActivityManager.START_PERMISSION_DENIED;
835 }
836 }
837
Bryce Lee93e7f792017-10-25 15:54:55 -0700838 final int userId = aInfo != null && aInfo.applicationInfo != null
839 ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800840 if (err == ActivityManager.START_SUCCESS) {
841 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Andrii Kulian03c403d2016-11-11 11:14:12 -0800842 + "} from uid " + callingUid);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800843 }
844
845 ActivityRecord sourceRecord = null;
846 ActivityRecord resultRecord = null;
847 if (resultTo != null) {
Wale Ogunwaled32da472018-11-16 07:19:28 -0800848 sourceRecord = mRootActivityContainer.isInAnyStack(resultTo);
Louis Chang54fbb052019-10-16 17:10:17 +0800849 if (DEBUG_RESULTS) {
850 Slog.v(TAG_RESULTS, "Will send result to " + resultTo + " " + sourceRecord);
851 }
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800852 if (sourceRecord != null) {
853 if (requestCode >= 0 && !sourceRecord.finishing) {
854 resultRecord = sourceRecord;
855 }
856 }
857 }
858
859 final int launchFlags = intent.getFlags();
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800860 if ((launchFlags & Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Louis Chang54fbb052019-10-16 17:10:17 +0800861 // Transfer the result target from the source activity to the new one being started,
862 // including any failures.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800863 if (requestCode >= 0) {
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +0100864 SafeActivityOptions.abort(options);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800865 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
866 }
867 resultRecord = sourceRecord.resultTo;
868 if (resultRecord != null && !resultRecord.isInStackLocked()) {
869 resultRecord = null;
870 }
871 resultWho = sourceRecord.resultWho;
872 requestCode = sourceRecord.requestCode;
873 sourceRecord.resultTo = null;
874 if (resultRecord != null) {
875 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
876 }
877 if (sourceRecord.launchedFromUid == callingUid) {
Louis Chang54fbb052019-10-16 17:10:17 +0800878 // The new activity is being launched from the same uid as the previous activity
879 // in the flow, and asking to forward its result back to the previous. In this
880 // case the activity is serving as a trampoline between the two, so we also want
881 // to update its launchedFromPackage to be the same as the previous activity.
882 // Note that this is safe, since we know these two packages come from the same
883 // uid; the caller could just as well have supplied that same package name itself
884 // . This specifially deals with the case of an intent picker/chooser being
885 // launched in the app flow to redirect to an activity picked by the user, where
886 // we want the final activity to consider it to have been launched by the
887 // previous app activity.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800888 callingPackage = sourceRecord.launchedFromPackage;
889 }
890 }
891
892 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
893 // We couldn't find a class that can handle the given Intent.
894 // That's the end of that!
895 err = ActivityManager.START_INTENT_NOT_RESOLVED;
896 }
897
898 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
899 // We couldn't find the specific class specified in the Intent.
900 // Also the end of the line.
901 err = ActivityManager.START_CLASS_NOT_FOUND;
902 }
903
904 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
Louis Changcdec0802019-11-11 11:45:07 +0800905 && sourceRecord.getTask().voiceSession != null) {
Louis Chang54fbb052019-10-16 17:10:17 +0800906 // If this activity is being launched as part of a voice session, we need to ensure
907 // that it is safe to do so. If the upcoming activity will also be part of the voice
908 // session, we can only launch it if it has explicitly said it supports the VOICE
909 // category, or it is a part of the calling app.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800910 if ((launchFlags & FLAG_ACTIVITY_NEW_TASK) == 0
911 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
912 try {
913 intent.addCategory(Intent.CATEGORY_VOICE);
Wale Ogunwale906f9c62018-07-23 11:23:44 -0700914 if (!mService.getPackageManager().activitySupportsIntent(
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800915 intent.getComponent(), intent, resolvedType)) {
Louis Chang54fbb052019-10-16 17:10:17 +0800916 Slog.w(TAG, "Activity being started in current voice task does not support "
917 + "voice: " + intent);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800918 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
919 }
920 } catch (RemoteException e) {
921 Slog.w(TAG, "Failure checking voice capabilities", e);
922 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
923 }
924 }
925 }
926
927 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
928 // If the caller is starting a new voice session, just make sure the target
929 // is actually allowing it to run this way.
930 try {
Wale Ogunwale906f9c62018-07-23 11:23:44 -0700931 if (!mService.getPackageManager().activitySupportsIntent(intent.getComponent(),
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800932 intent, resolvedType)) {
933 Slog.w(TAG,
Louis Chang54fbb052019-10-16 17:10:17 +0800934 "Activity being started in new voice task does not support: " + intent);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800935 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
936 }
937 } catch (RemoteException e) {
938 Slog.w(TAG, "Failure checking voice capabilities", e);
939 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
940 }
941 }
942
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800943 final ActivityStack resultStack = resultRecord == null
944 ? null : resultRecord.getActivityStack();
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800945
Wale Ogunwale01d66562015-12-29 08:19:19 -0800946 if (err != START_SUCCESS) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800947 if (resultRecord != null) {
Andrii Kulian79d67982019-08-19 11:56:16 -0700948 resultRecord.sendResult(INVALID_UID, resultWho, requestCode, RESULT_CANCELED,
949 null /* data */);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800950 }
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +0100951 SafeActivityOptions.abort(options);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800952 return err;
953 }
954
955 boolean abort = !mSupervisor.checkStartAnyActivityPermission(intent, aInfo, resultWho,
Louis Chang54fbb052019-10-16 17:10:17 +0800956 requestCode, callingPid, callingUid, callingPackage, request.ignoreTargetSecurity,
Winson Chungc9804e72018-05-15 11:01:44 -0700957 inTask != null, callerApp, resultRecord, resultStack);
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700958 abort |= !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800959 callingPid, resolvedType, aInfo.applicationInfo);
Hai Zhangf4da9be2019-05-01 13:46:06 +0800960 abort |= !mService.getPermissionPolicyInternal().checkStartActivity(intent, callingUid,
961 callingPackage);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800962
Ricky Waib147fa12019-04-25 16:08:30 +0100963 boolean restrictedBgActivity = false;
Michal Karpinski8596ded2018-11-14 14:43:48 +0000964 if (!abort) {
Michal Karpinski4fd5b842019-01-28 15:13:32 +0000965 try {
Riddle Hsu2ca561b2019-10-08 21:58:58 +0800966 Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER,
Michal Karpinski4fd5b842019-01-28 15:13:32 +0000967 "shouldAbortBackgroundActivityStart");
Ricky Waib147fa12019-04-25 16:08:30 +0100968 restrictedBgActivity = shouldAbortBackgroundActivityStart(callingUid,
Ricky Waiaca8a772019-04-04 16:01:06 +0100969 callingPid, callingPackage, realCallingUid, realCallingPid, callerApp,
Louis Chang54fbb052019-10-16 17:10:17 +0800970 request.originatingPendingIntent, request.allowBackgroundActivityStart,
971 intent);
Michal Karpinski4fd5b842019-01-28 15:13:32 +0000972 } finally {
Riddle Hsu2ca561b2019-10-08 21:58:58 +0800973 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
Michal Karpinski4fd5b842019-01-28 15:13:32 +0000974 }
Michal Karpinski8596ded2018-11-14 14:43:48 +0000975 }
976
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +0100977 // Merge the two options bundles, while realCallerOptions takes precedence.
978 ActivityOptions checkedOptions = options != null
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700979 ? options.getOptions(intent, aInfo, callerApp, mSupervisor) : null;
Louis Chang54fbb052019-10-16 17:10:17 +0800980 if (request.allowPendingRemoteAnimationRegistryLookup) {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -0700981 checkedOptions = mService.getActivityStartController()
Jorim Jaggi6fa41c32018-04-23 18:35:00 +0200982 .getPendingRemoteAnimationRegistry()
983 .overrideOptionsIfNeeded(callingPackage, checkedOptions);
984 }
Wale Ogunwalea6191b42018-05-09 07:41:32 -0700985 if (mService.mController != null) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800986 try {
Louis Chang54fbb052019-10-16 17:10:17 +0800987 // The Intent we give to the watcher has the extra data stripped off, since it
988 // can contain private information.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800989 Intent watchIntent = intent.cloneFilter();
Wale Ogunwalea6191b42018-05-09 07:41:32 -0700990 abort |= !mService.mController.activityStarting(watchIntent,
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800991 aInfo.applicationInfo.packageName);
992 } catch (RemoteException e) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -0700993 mService.mController = null;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800994 }
995 }
996
Rubin Xu58d25992016-01-21 17:47:13 +0000997 mInterceptor.setStates(userId, realCallingPid, realCallingUid, startFlags, callingPackage);
Benjamin Franz563707b2017-06-29 15:06:13 +0100998 if (mInterceptor.intercept(intent, rInfo, aInfo, resolvedType, inTask, callingPid,
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +0100999 callingUid, checkedOptions)) {
Benjamin Franz563707b2017-06-29 15:06:13 +01001000 // activity start was intercepted, e.g. because the target user is currently in quiet
1001 // mode (turn off work) or the target application is suspended
1002 intent = mInterceptor.mIntent;
1003 rInfo = mInterceptor.mRInfo;
1004 aInfo = mInterceptor.mAInfo;
1005 resolvedType = mInterceptor.mResolvedType;
1006 inTask = mInterceptor.mInTask;
1007 callingPid = mInterceptor.mCallingPid;
1008 callingUid = mInterceptor.mCallingUid;
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01001009 checkedOptions = mInterceptor.mActivityOptions;
Benjamin Franz563707b2017-06-29 15:06:13 +01001010 }
1011
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001012 if (abort) {
1013 if (resultRecord != null) {
Andrii Kulian79d67982019-08-19 11:56:16 -07001014 resultRecord.sendResult(INVALID_UID, resultWho, requestCode, RESULT_CANCELED,
1015 null /* data */);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001016 }
Louis Chang54fbb052019-10-16 17:10:17 +08001017 // We pretend to the caller that it was really started, but they will just get a
1018 // cancel result.
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01001019 ActivityOptions.abort(checkedOptions);
Bryce Leef9d49542017-06-26 16:27:32 -07001020 return START_ABORTED;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001021 }
1022
1023 // If permissions need a review before any of the app components can run, we
1024 // launch the review activity and pass a pending intent to start the activity
1025 // we are to launching now after the review is completed.
Philip P. Moltmann6c644e62018-07-18 15:41:24 -07001026 if (aInfo != null) {
Wale Ogunwale906f9c62018-07-23 11:23:44 -07001027 if (mService.getPackageManagerInternalLocked().isPermissionsReviewRequired(
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001028 aInfo.packageName, userId)) {
Louis Chang54fbb052019-10-16 17:10:17 +08001029 final IIntentSender target = mService.getIntentSenderLocked(
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001030 ActivityManager.INTENT_SENDER_ACTIVITY, callingPackage,
1031 callingUid, userId, null, null, 0, new Intent[]{intent},
1032 new String[]{resolvedType}, PendingIntent.FLAG_CANCEL_CURRENT
1033 | PendingIntent.FLAG_ONE_SHOT, null);
1034
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001035 Intent newIntent = new Intent(Intent.ACTION_REVIEW_PERMISSIONS);
Philip P. Moltmannc3e66d02019-01-31 15:56:18 -08001036
1037 int flags = intent.getFlags();
1038 flags |= Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS;
1039
1040 /*
1041 * Prevent reuse of review activity: Each app needs their own review activity. By
1042 * default activities launched with NEW_TASK or NEW_DOCUMENT try to reuse activities
1043 * with the same launch parameters (extras are ignored). Hence to avoid possible
1044 * reuse force a new activity via the MULTIPLE_TASK flag.
1045 *
1046 * Activities that are not launched with NEW_TASK or NEW_DOCUMENT are not re-used,
1047 * hence no need to add the flag in this case.
1048 */
1049 if ((flags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_NEW_DOCUMENT)) != 0) {
1050 flags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1051 }
1052 newIntent.setFlags(flags);
1053
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001054 newIntent.putExtra(Intent.EXTRA_PACKAGE_NAME, aInfo.packageName);
1055 newIntent.putExtra(Intent.EXTRA_INTENT, new IntentSender(target));
1056 if (resultRecord != null) {
1057 newIntent.putExtra(Intent.EXTRA_RESULT_NEEDED, true);
1058 }
1059 intent = newIntent;
1060
1061 resolvedType = null;
1062 callingUid = realCallingUid;
1063 callingPid = realCallingPid;
1064
Svet Ganovcbcbf662018-05-10 17:25:29 -07001065 rInfo = mSupervisor.resolveIntent(intent, resolvedType, userId, 0,
Patrick Baumann31426b22018-05-21 13:46:40 -07001066 computeResolveFilterUid(
Louis Chang54fbb052019-10-16 17:10:17 +08001067 callingUid, realCallingUid, request.filterCallingUid));
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001068 aInfo = mSupervisor.resolveActivity(intent, rInfo, startFlags,
1069 null /*profilerInfo*/);
1070
1071 if (DEBUG_PERMISSIONS_REVIEW) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08001072 final ActivityStack focusedStack =
1073 mRootActivityContainer.getTopDisplayFocusedStack();
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001074 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true,
1075 true, false) + "} from uid " + callingUid + " on display "
Andrii Kulian52d255c2018-07-13 11:32:19 -07001076 + (focusedStack == null ? DEFAULT_DISPLAY : focusedStack.mDisplayId));
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001077 }
1078 }
1079 }
1080
1081 // If we have an ephemeral app, abort the process of launching the resolved intent.
1082 // Instead, launch the ephemeral installer. Once the installer is finished, it
1083 // starts either the intent we resolved here [on install error] or the ephemeral
1084 // app [on install success].
Patrick Baumanna89a1722018-02-07 15:26:52 -08001085 if (rInfo != null && rInfo.auxiliaryInfo != null) {
Louis Chang54fbb052019-10-16 17:10:17 +08001086 intent = createLaunchIntent(rInfo.auxiliaryInfo, request.ephemeralIntent,
Chad Brubaker06068612017-04-06 09:43:47 -07001087 callingPackage, verificationBundle, resolvedType, userId);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001088 resolvedType = null;
1089 callingUid = realCallingUid;
1090 callingPid = realCallingPid;
1091
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001092 aInfo = mSupervisor.resolveActivity(intent, rInfo, startFlags, null /*profilerInfo*/);
1093 }
1094
Louis Chang54fbb052019-10-16 17:10:17 +08001095 final ActivityRecord r = new ActivityRecord(mService, callerApp, callingPid, callingUid,
Wale Ogunwalef6733932018-06-27 05:14:34 -07001096 callingPackage, intent, resolvedType, aInfo, mService.getGlobalConfiguration(),
Louis Chang54fbb052019-10-16 17:10:17 +08001097 resultRecord, resultWho, requestCode, request.componentSpecified,
1098 voiceSession != null, mSupervisor, checkedOptions, sourceRecord);
1099 mLastStartActivityRecord = r;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001100
1101 if (r.appTimeTracker == null && sourceRecord != null) {
1102 // If the caller didn't specify an explicit time tracker, we want to continue
1103 // tracking under any it has.
1104 r.appTimeTracker = sourceRecord.appTimeTracker;
1105 }
1106
Wale Ogunwaled32da472018-11-16 07:19:28 -08001107 final ActivityStack stack = mRootActivityContainer.getTopDisplayFocusedStack();
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01001108
1109 // If we are starting an activity that is not from the same uid as the currently resumed
1110 // one, check whether app switches are allowed.
Bryce Leec4ab62a2018-03-05 14:19:26 -08001111 if (voiceSession == null && (stack.getResumedActivity() == null
1112 || stack.getResumedActivity().info.applicationInfo.uid != realCallingUid)) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001113 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001114 realCallingPid, realCallingUid, "Activity start")) {
Alan Stokes07389b62019-05-20 15:22:54 +01001115 if (!(restrictedBgActivity && handleBackgroundActivityAbort(r))) {
Ricky Waib147fa12019-04-25 16:08:30 +01001116 mController.addPendingActivityLaunch(new PendingActivityLaunch(r,
Wale Ogunwale586a8ee2019-06-04 13:44:14 +00001117 sourceRecord, startFlags, stack, callerApp));
Ricky Waib147fa12019-04-25 16:08:30 +01001118 }
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01001119 ActivityOptions.abort(checkedOptions);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001120 return ActivityManager.START_SWITCHES_CANCELED;
1121 }
1122 }
1123
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001124 mService.onStartActivitySetDidAppSwitch();
Bryce Leed3624e12017-11-30 08:51:45 -08001125 mController.doPendingActivityLaunches(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001126
Louis Chang54fbb052019-10-16 17:10:17 +08001127 mLastStartActivityResult = startActivityUnchecked(r, sourceRecord, voiceSession,
1128 request.voiceInteractor, startFlags, true /* doResume */, checkedOptions, inTask,
1129 restrictedBgActivity);
1130
1131 if (request.outActivity != null) {
1132 request.outActivity[0] = mLastStartActivityRecord;
1133 }
1134
Riddle Hsu7f8643a2019-12-05 14:37:30 +08001135 return mLastStartActivityResult;
Louis Chang54fbb052019-10-16 17:10:17 +08001136 }
1137
1138 /**
1139 * Return true if background activity is really aborted.
1140 *
1141 * TODO(b/131748165): Refactor the logic so we don't need to call this method everywhere.
1142 */
1143 private boolean handleBackgroundActivityAbort(ActivityRecord r) {
1144 // TODO(b/131747138): Remove toast and refactor related code in R release.
1145 final boolean abort = !mService.isBackgroundActivityStartsEnabled();
1146 if (!abort) {
1147 return false;
1148 }
1149 final ActivityRecord resultRecord = r.resultTo;
1150 final String resultWho = r.resultWho;
1151 int requestCode = r.requestCode;
1152 if (resultRecord != null) {
1153 resultRecord.sendResult(INVALID_UID, resultWho, requestCode, RESULT_CANCELED,
1154 null /* data */);
1155 }
1156 // We pretend to the caller that it was really started to make it backward compatible, but
1157 // they will just get a cancel result.
1158 ActivityOptions.abort(r.pendingOptions);
1159 return true;
1160 }
1161
1162 static int getExternalResult(int result) {
1163 // Aborted results are treated as successes externally, but we must track them internally.
1164 return result != START_ABORTED ? result : START_SUCCESS;
1165 }
1166
1167 /**
1168 * Called when execution is complete. Sets state indicating completion and proceeds with
1169 * recycling if appropriate.
1170 */
1171 private void onExecutionComplete() {
1172 mController.onExecutionComplete(this);
Wale Ogunwalecc25a8a2016-01-23 14:31:37 -08001173 }
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001174
Ricky Waiaca8a772019-04-04 16:01:06 +01001175 boolean shouldAbortBackgroundActivityStart(int callingUid, int callingPid,
Michal Karpinski9cbb20b2019-02-05 17:31:50 +00001176 final String callingPackage, int realCallingUid, int realCallingPid,
1177 WindowProcessController callerApp, PendingIntentRecord originatingPendingIntent,
1178 boolean allowBackgroundActivityStart, Intent intent) {
Michal Karpinski8596ded2018-11-14 14:43:48 +00001179 // don't abort for the most important UIDs
Alan Stokes2f4a4ed2019-05-08 16:56:45 +01001180 final int callingAppId = UserHandle.getAppId(callingUid);
1181 if (callingUid == Process.ROOT_UID || callingAppId == Process.SYSTEM_UID
1182 || callingAppId == Process.NFC_UID) {
Michal Karpinski8596ded2018-11-14 14:43:48 +00001183 return false;
1184 }
Alan Stokeseea8d3e2019-04-10 17:37:25 +01001185 // don't abort if the callingUid has a visible window or is a persistent system process
Riddle Hsua0536432019-02-16 00:38:59 +08001186 final int callingUidProcState = mService.getUidState(callingUid);
Michal Karpinski4fd5b842019-01-28 15:13:32 +00001187 final boolean callingUidHasAnyVisibleWindow =
1188 mService.mWindowManager.mRoot.isAnyNonToastWindowVisibleForUid(callingUid);
1189 final boolean isCallingUidForeground = callingUidHasAnyVisibleWindow
Amith Yamasanif235d0b2019-03-20 22:49:43 -07001190 || callingUidProcState == ActivityManager.PROCESS_STATE_TOP
1191 || callingUidProcState == ActivityManager.PROCESS_STATE_BOUND_TOP;
Alan Stokes2f4a4ed2019-05-08 16:56:45 +01001192 final boolean isCallingUidPersistentSystemProcess =
1193 callingUidProcState <= ActivityManager.PROCESS_STATE_PERSISTENT_UI;
Alan Stokeseea8d3e2019-04-10 17:37:25 +01001194 if (callingUidHasAnyVisibleWindow || isCallingUidPersistentSystemProcess) {
Michal Karpinski8596ded2018-11-14 14:43:48 +00001195 return false;
1196 }
Michal Karpinskiac116df2018-12-10 17:51:42 +00001197 // take realCallingUid into consideration
Michal Karpinski4fd5b842019-01-28 15:13:32 +00001198 final int realCallingUidProcState = (callingUid == realCallingUid)
1199 ? callingUidProcState
Riddle Hsua0536432019-02-16 00:38:59 +08001200 : mService.getUidState(realCallingUid);
Michal Karpinski4fd5b842019-01-28 15:13:32 +00001201 final boolean realCallingUidHasAnyVisibleWindow = (callingUid == realCallingUid)
1202 ? callingUidHasAnyVisibleWindow
1203 : mService.mWindowManager.mRoot.isAnyNonToastWindowVisibleForUid(realCallingUid);
1204 final boolean isRealCallingUidForeground = (callingUid == realCallingUid)
1205 ? isCallingUidForeground
1206 : realCallingUidHasAnyVisibleWindow
1207 || realCallingUidProcState == ActivityManager.PROCESS_STATE_TOP;
Alan Stokes2f4a4ed2019-05-08 16:56:45 +01001208 final int realCallingAppId = UserHandle.getAppId(realCallingUid);
Michal Karpinski4fd5b842019-01-28 15:13:32 +00001209 final boolean isRealCallingUidPersistentSystemProcess = (callingUid == realCallingUid)
1210 ? isCallingUidPersistentSystemProcess
Alan Stokes2f4a4ed2019-05-08 16:56:45 +01001211 : (realCallingAppId == Process.SYSTEM_UID)
Michal Karpinski4fd5b842019-01-28 15:13:32 +00001212 || realCallingUidProcState <= ActivityManager.PROCESS_STATE_PERSISTENT_UI;
Michal Karpinskiac116df2018-12-10 17:51:42 +00001213 if (realCallingUid != callingUid) {
Alan Stokes6ac9efd2019-05-09 12:50:37 +00001214 // don't abort if the realCallingUid has a visible window
1215 if (realCallingUidHasAnyVisibleWindow) {
Michal Karpinskiac116df2018-12-10 17:51:42 +00001216 return false;
1217 }
1218 // if the realCallingUid is a persistent system process, abort if the IntentSender
1219 // wasn't whitelisted to start an activity
Michal Karpinskid0162852019-01-15 16:05:25 +00001220 if (isRealCallingUidPersistentSystemProcess && allowBackgroundActivityStart) {
Michal Karpinskiac116df2018-12-10 17:51:42 +00001221 return false;
1222 }
Michal Karpinskida34cd42019-04-02 19:46:52 +01001223 // don't abort if the realCallingUid is an associated companion app
1224 if (mService.isAssociatedCompanionApp(UserHandle.getUserId(realCallingUid),
1225 realCallingUid)) {
1226 return false;
1227 }
Michal Karpinskiac116df2018-12-10 17:51:42 +00001228 }
Michal Karpinski7b97a022018-12-14 15:17:29 +00001229 // don't abort if the callingUid has START_ACTIVITIES_FROM_BACKGROUND permission
1230 if (mService.checkPermission(START_ACTIVITIES_FROM_BACKGROUND, callingPid, callingUid)
1231 == PERMISSION_GRANTED) {
1232 return false;
1233 }
Michal Karpinski82bb5902018-11-28 15:52:52 +00001234 // don't abort if the caller has the same uid as the recents component
1235 if (mSupervisor.mRecentTasks.isCallerRecents(callingUid)) {
1236 return false;
1237 }
Ricky Wai96f5c352019-04-10 18:40:17 +01001238 // don't abort if the callingUid is the device owner
1239 if (mService.isDeviceOwner(callingUid)) {
Michal Karpinski302dcec2019-02-01 11:48:25 +00001240 return false;
1241 }
Ricky Wai96f5c352019-04-10 18:40:17 +01001242 // don't abort if the callingUid has companion device
Ricky Wai2452e2d2019-03-18 19:19:08 +00001243 final int callingUserId = UserHandle.getUserId(callingUid);
Michal Karpinskida34cd42019-04-02 19:46:52 +01001244 if (mService.isAssociatedCompanionApp(callingUserId, callingUid)) {
Ricky Wai2452e2d2019-03-18 19:19:08 +00001245 return false;
1246 }
Michal Karpinski2e0aad22019-04-12 16:22:55 +01001247 // If we don't have callerApp at this point, no caller was provided to startActivity().
1248 // That's the case for PendingIntent-based starts, since the creator's process might not be
1249 // up and alive. If that's the case, we retrieve the WindowProcessController for the send()
1250 // caller, so that we can make the decision based on its foreground/whitelisted state.
1251 int callerAppUid = callingUid;
1252 if (callerApp == null) {
1253 callerApp = mService.getProcessController(realCallingPid, realCallingUid);
1254 callerAppUid = realCallingUid;
1255 }
1256 // don't abort if the callerApp or other processes of that uid are whitelisted in any way
1257 if (callerApp != null) {
1258 // first check the original calling process
1259 if (callerApp.areBackgroundActivityStartsAllowed()) {
1260 return false;
1261 }
1262 // only if that one wasn't whitelisted, check the other ones
1263 final ArraySet<WindowProcessController> uidProcesses =
1264 mService.mProcessMap.getProcesses(callerAppUid);
1265 if (uidProcesses != null) {
1266 for (int i = uidProcesses.size() - 1; i >= 0; i--) {
1267 final WindowProcessController proc = uidProcesses.valueAt(i);
1268 if (proc != callerApp && proc.areBackgroundActivityStartsAllowed()) {
1269 return false;
1270 }
1271 }
1272 }
1273 }
Michal Karpinski15486842019-04-25 17:33:42 +01001274 // don't abort if the callingUid has SYSTEM_ALERT_WINDOW permission
1275 if (mService.hasSystemAlertWindowPermission(callingUid, callingPid, callingPackage)) {
1276 Slog.w(TAG, "Background activity start for " + callingPackage
1277 + " allowed because SYSTEM_ALERT_WINDOW permission is granted.");
1278 return false;
1279 }
Michal Karpinskic02364c2019-01-22 13:00:04 +00001280 // anything that has fallen through would currently be aborted
1281 Slog.w(TAG, "Background activity start [callingPackage: " + callingPackage
Michal Karpinskid0162852019-01-15 16:05:25 +00001282 + "; callingUid: " + callingUid
1283 + "; isCallingUidForeground: " + isCallingUidForeground
1284 + "; isCallingUidPersistentSystemProcess: " + isCallingUidPersistentSystemProcess
1285 + "; realCallingUid: " + realCallingUid
1286 + "; isRealCallingUidForeground: " + isRealCallingUidForeground
1287 + "; isRealCallingUidPersistentSystemProcess: "
Ricky Waiaca8a772019-04-04 16:01:06 +01001288 + isRealCallingUidPersistentSystemProcess
Michal Karpinskid0162852019-01-15 16:05:25 +00001289 + "; originatingPendingIntent: " + originatingPendingIntent
1290 + "; isBgStartWhitelisted: " + allowBackgroundActivityStart
1291 + "; intent: " + intent
Michal Karpinski9cbb20b2019-02-05 17:31:50 +00001292 + "; callerApp: " + callerApp
Michal Karpinskid0162852019-01-15 16:05:25 +00001293 + "]");
Michal Karpinski4fd5b842019-01-28 15:13:32 +00001294 // log aborted activity start to TRON
1295 if (mService.isActivityStartsLoggingEnabled()) {
1296 mSupervisor.getActivityMetricsLogger().logAbortedBgActivityStart(intent, callerApp,
1297 callingUid, callingPackage, callingUidProcState, callingUidHasAnyVisibleWindow,
1298 realCallingUid, realCallingUidProcState, realCallingUidHasAnyVisibleWindow,
Michal Karpinski201bc0c2018-07-20 15:32:00 +01001299 (originatingPendingIntent != null));
Michal Karpinski201bc0c2018-07-20 15:32:00 +01001300 }
Michal Karpinski4fd5b842019-01-28 15:13:32 +00001301 return true;
Michal Karpinski201bc0c2018-07-20 15:32:00 +01001302 }
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01001303
Bryce Leeaa5e8c32017-03-01 16:01:06 -08001304 /**
1305 * Creates a launch intent for the given auxiliary resolution data.
1306 */
Patrick Baumann577d4022018-01-31 16:55:10 +00001307 private @NonNull Intent createLaunchIntent(@Nullable AuxiliaryResolveInfo auxiliaryResponse,
Chad Brubaker06068612017-04-06 09:43:47 -07001308 Intent originalIntent, String callingPackage, Bundle verificationBundle,
1309 String resolvedType, int userId) {
Patrick Baumann577d4022018-01-31 16:55:10 +00001310 if (auxiliaryResponse != null && auxiliaryResponse.needsPhaseTwo) {
Todd Kennedye9910222017-02-21 16:00:11 -08001311 // request phase two resolution
Wale Ogunwale906f9c62018-07-23 11:23:44 -07001312 mService.getPackageManagerInternalLocked().requestInstantAppResolutionPhaseTwo(
Chad Brubaker06068612017-04-06 09:43:47 -07001313 auxiliaryResponse, originalIntent, resolvedType, callingPackage,
1314 verificationBundle, userId);
Todd Kennedye9910222017-02-21 16:00:11 -08001315 }
Todd Kennedydfc27c62017-05-17 15:32:10 -07001316 return InstantAppResolver.buildEphemeralInstallerIntent(
Patrick Baumann577d4022018-01-31 16:55:10 +00001317 originalIntent,
1318 InstantAppResolver.sanitizeIntent(originalIntent),
1319 auxiliaryResponse == null ? null : auxiliaryResponse.failureIntent,
1320 callingPackage,
1321 verificationBundle,
1322 resolvedType,
1323 userId,
1324 auxiliaryResponse == null ? null : auxiliaryResponse.installFailureActivity,
1325 auxiliaryResponse == null ? null : auxiliaryResponse.token,
1326 auxiliaryResponse != null && auxiliaryResponse.needsPhaseTwo,
1327 auxiliaryResponse == null ? null : auxiliaryResponse.filters);
Todd Kennedye9910222017-02-21 16:00:11 -08001328 }
1329
Riddle Hsu16567132018-08-16 21:37:47 +08001330 void postStartActivityProcessing(ActivityRecord r, int result,
1331 ActivityStack startedActivityStack) {
Winson Chunge219ae12019-07-18 13:43:23 -07001332 if (!ActivityManager.isStartResultSuccessful(result)) {
1333 if (mFrozeTaskList) {
1334 // If we specifically froze the task list as part of starting an activity, then
1335 // reset the frozen list state if it failed to start. This is normally otherwise
1336 // called when the freeze-timeout has elapsed.
1337 mSupervisor.mRecentTasks.resetFreezeTaskListReorderingOnTimeout();
1338 }
1339 }
Bryce Lee7f936862017-05-09 15:33:18 -07001340 if (ActivityManager.isStartResultFatalError(result)) {
Wale Ogunwalecc25a8a2016-01-23 14:31:37 -08001341 return;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001342 }
Filip Gruszczynski303210b2016-01-08 16:28:08 -08001343
Chong Zhang5022da32016-06-21 16:31:37 -07001344 // We're waiting for an activity launch to finish, but that activity simply
Bryce Lee5f0e28f2018-01-30 16:00:03 -08001345 // brought another activity to front. We must also handle the case where the task is already
1346 // 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 +08001347 // considered focused as the trampoline will be finished). Let them know about this, so
1348 // it waits for the new activity to become visible instead, {@link #waitResultIfNeeded}.
Bryce Lee5f0e28f2018-01-30 16:00:03 -08001349 mSupervisor.reportWaitingActivityLaunchedIfNeeded(r, result);
Chong Zhang5022da32016-06-21 16:31:37 -07001350
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001351 if (startedActivityStack == null) {
1352 return;
1353 }
1354
Wale Ogunwaleac36e4d2017-11-29 13:30:26 -08001355 final int clearTaskFlags = FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK;
1356 boolean clearedTask = (mLaunchFlags & clearTaskFlags) == clearTaskFlags
1357 && mReuseTask != null;
Wale Ogunwale7d7973a2018-04-05 10:25:59 -07001358 if (result == START_TASK_TO_FRONT || result == START_DELIVERED_TO_TOP || clearedTask) {
1359 // The activity was already running so it wasn't started, but either brought to the
1360 // front or the new intent was delivered to it since it was already in front. Notify
1361 // anyone interested in this piece of information.
1362 switch (startedActivityStack.getWindowingMode()) {
1363 case WINDOWING_MODE_PINNED:
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07001364 mService.getTaskChangeNotificationController().notifyPinnedActivityRestartAttempt(
Wale Ogunwale7d7973a2018-04-05 10:25:59 -07001365 clearedTask);
1366 break;
1367 case WINDOWING_MODE_SPLIT_SCREEN_PRIMARY:
Louis Changbd48dca2018-08-29 17:44:34 +08001368 final ActivityStack homeStack =
1369 startedActivityStack.getDisplay().getHomeStack();
Wale Ogunwale7d7973a2018-04-05 10:25:59 -07001370 if (homeStack != null && homeStack.shouldBeVisible(null /* starting */)) {
1371 mService.mWindowManager.showRecentApps();
1372 }
1373 break;
1374 }
Wale Ogunwalecc25a8a2016-01-23 14:31:37 -08001375 }
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001376 }
1377
Svet Ganovcbcbf662018-05-10 17:25:29 -07001378 /**
1379 * Compute the logical UID based on which the package manager would filter
1380 * app components i.e. based on which the instant app policy would be applied
1381 * because it is the logical calling UID.
1382 *
1383 * @param customCallingUid The UID on whose behalf to make the call.
1384 * @param actualCallingUid The UID actually making the call.
Patrick Baumann31426b22018-05-21 13:46:40 -07001385 * @param filterCallingUid The UID to be used to filter for instant apps.
Svet Ganovcbcbf662018-05-10 17:25:29 -07001386 * @return The logical UID making the call.
1387 */
Patrick Baumann31426b22018-05-21 13:46:40 -07001388 static int computeResolveFilterUid(int customCallingUid, int actualCallingUid,
1389 int filterCallingUid) {
1390 return filterCallingUid != UserHandle.USER_NULL
1391 ? filterCallingUid
1392 : (customCallingUid >= 0 ? customCallingUid : actualCallingUid);
Svet Ganovcbcbf662018-05-10 17:25:29 -07001393 }
1394
Louis Chang54fbb052019-10-16 17:10:17 +08001395 /**
1396 * Start an activity while most of preliminary checks has been done and caller has been
1397 * confirmed that holds necessary permissions to do so.
1398 * Here also ensures that the starting activity is removed if the start wasn't successful.
1399 */
1400 private int startActivityUnchecked(final ActivityRecord r, ActivityRecord sourceRecord,
Bryce Leedaa91e42017-12-06 14:13:01 -08001401 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Louis Changcdec0802019-11-11 11:45:07 +08001402 int startFlags, boolean doResume, ActivityOptions options, Task inTask,
Louis Chang54fbb052019-10-16 17:10:17 +08001403 boolean restrictedBgActivity) {
Bryce Leeaa5e8c32017-03-01 16:01:06 -08001404 int result = START_CANCELED;
Riddle Hsu16567132018-08-16 21:37:47 +08001405 final ActivityStack startedActivityStack;
Bryce Leeaa5e8c32017-03-01 16:01:06 -08001406 try {
Riddle Hsua0022cd2019-09-09 21:12:41 +08001407 mService.deferWindowLayout();
Riddle Hsu2ca561b2019-10-08 21:58:58 +08001408 Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "startActivityInner");
1409 result = startActivityInner(r, sourceRecord, voiceSession, voiceInteractor,
Louis Chang54fbb052019-10-16 17:10:17 +08001410 startFlags, doResume, options, inTask, restrictedBgActivity);
Bryce Leeaa5e8c32017-03-01 16:01:06 -08001411 } finally {
Riddle Hsu2ca561b2019-10-08 21:58:58 +08001412 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
1413 startedActivityStack = handleStartResult(r, result);
Riddle Hsua0022cd2019-09-09 21:12:41 +08001414 mService.continueWindowLayout();
Bryce Leeaa5e8c32017-03-01 16:01:06 -08001415 }
1416
Riddle Hsu16567132018-08-16 21:37:47 +08001417 postStartActivityProcessing(r, result, startedActivityStack);
Bryce Leeaa5e8c32017-03-01 16:01:06 -08001418
1419 return result;
1420 }
1421
Ricky Waib147fa12019-04-25 16:08:30 +01001422 /**
Riddle Hsu2ca561b2019-10-08 21:58:58 +08001423 * If the start result is success, ensure that the configuration of the started activity matches
1424 * the current display. Otherwise clean up unassociated containers to avoid leakage.
1425 *
1426 * @return the stack where the successful started activity resides.
1427 */
1428 private @Nullable ActivityStack handleStartResult(@NonNull ActivityRecord started, int result) {
1429 final ActivityStack currentStack = started.getActivityStack();
1430 ActivityStack startedActivityStack = currentStack != null ? currentStack : mTargetStack;
1431
1432 if (ActivityManager.isStartResultSuccessful(result)) {
1433 if (startedActivityStack != null) {
1434 // If there is no state change (e.g. a resumed activity is reparented to top of
1435 // another display) to trigger a visibility/configuration checking, we have to
1436 // update the configuration for changing to different display.
1437 final ActivityRecord currentTop = startedActivityStack.topRunningActivityLocked();
1438 if (currentTop != null && currentTop.shouldUpdateConfigForDisplayChanged()) {
1439 mRootActivityContainer.ensureVisibilityAndConfig(
1440 currentTop, currentTop.getDisplayId(),
1441 true /* markFrozenIfConfigChanged */, false /* deferResume */);
1442 }
1443 }
1444 return startedActivityStack;
1445 }
1446
1447 // If we are not able to proceed, disassociate the activity from the task. Leaving an
1448 // activity in an incomplete state can lead to issues, such as performing operations
1449 // without a window container.
1450 final ActivityStack stack = mStartActivity.getActivityStack();
1451 if (stack != null) {
1452 mStartActivity.finishIfPossible("startActivity", true /* oomAdj */);
1453 }
1454
1455 // Stack should also be detached from display and be removed if it's empty.
1456 if (startedActivityStack != null && startedActivityStack.isAttached()
Wale Ogunwalea38654f2019-11-17 20:37:15 -08001457 && !startedActivityStack.hasActivity()
Riddle Hsu2ca561b2019-10-08 21:58:58 +08001458 && !startedActivityStack.isActivityTypeHome()) {
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -07001459 startedActivityStack.removeIfPossible();
Riddle Hsu2ca561b2019-10-08 21:58:58 +08001460 startedActivityStack = null;
1461 }
1462 return startedActivityStack;
1463 }
1464
1465 /**
Louis Chang54fbb052019-10-16 17:10:17 +08001466 * Start an activity and determine if the activity should be adding to the top of an existing
1467 * task or delivered new intent to an existing activity. Also manipulating the activity task
1468 * onto requested or valid stack/display.
Ricky Waib147fa12019-04-25 16:08:30 +01001469 *
Louis Chang54fbb052019-10-16 17:10:17 +08001470 * Note: This method should only be called from {@link #startActivityUnchecked}.
Ricky Waib147fa12019-04-25 16:08:30 +01001471 */
Riddle Hsu2ca561b2019-10-08 21:58:58 +08001472 private int startActivityInner(final ActivityRecord r, ActivityRecord sourceRecord,
Wale Ogunwale01d66562015-12-29 08:19:19 -08001473 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Louis Changcdec0802019-11-11 11:45:07 +08001474 int startFlags, boolean doResume, ActivityOptions options, Task inTask,
Louis Chang54fbb052019-10-16 17:10:17 +08001475 boolean restrictedBgActivity) {
Wale Ogunwale01d66562015-12-29 08:19:19 -08001476 setInitialState(r, options, inTask, doResume, startFlags, sourceRecord, voiceSession,
Ricky Waib147fa12019-04-25 16:08:30 +01001477 voiceInteractor, restrictedBgActivity);
1478
Louis Chang39ba54b2018-10-18 11:28:57 +08001479 final int preferredWindowingMode = mLaunchParams.mWindowingMode;
Wale Ogunwale01d66562015-12-29 08:19:19 -08001480
Louis Chang6fb1e842018-12-03 16:07:50 +08001481 computeLaunchingTaskFlags();
1482
1483 computeSourceStack();
1484
1485 mIntent.setFlags(mLaunchFlags);
1486
Louis Changcdec0802019-11-11 11:45:07 +08001487 final Task reusedTask = getReusableTask();
Louis Changf7dd7f22019-11-05 11:59:56 +08001488 mSupervisor.getLaunchParamsController().calculate(reusedTask != null ? reusedTask : mInTask,
Louis Chang6fb1e842018-12-03 16:07:50 +08001489 r.info.windowLayout, r, sourceRecord, options, PHASE_BOUNDS, mLaunchParams);
1490 mPreferredDisplayId =
1491 mLaunchParams.hasPreferredDisplay() ? mLaunchParams.mPreferredDisplayId
1492 : DEFAULT_DISPLAY;
1493
Winson Chunge219ae12019-07-18 13:43:23 -07001494 // If requested, freeze the task list
1495 if (mOptions != null && mOptions.freezeRecentTasksReordering()
1496 && mSupervisor.mRecentTasks.isCallerRecents(r.launchedFromUid)
1497 && !mSupervisor.mRecentTasks.isFreezeTaskListReorderingSet()) {
1498 mFrozeTaskList = true;
1499 mSupervisor.mRecentTasks.setFreezeTaskListReordering();
1500 }
1501
Louis Changbde91e92019-08-16 17:19:47 +08001502 // Compute if there is an existing task that should be used for.
Louis Changcdec0802019-11-11 11:45:07 +08001503 final Task targetTask = reusedTask != null ? reusedTask : computeTargetTask();
Louis Changbde91e92019-08-16 17:19:47 +08001504 final boolean newTask = targetTask == null;
1505
1506 // Check if starting activity on given task or on a new task is allowed.
1507 int startResult = isAllowedToStart(r, newTask, targetTask);
1508 if (startResult != START_SUCCESS) {
1509 return startResult;
Louis Changbd48dca2018-08-29 17:44:34 +08001510 }
1511
Wale Ogunwale21e06482019-11-18 05:14:15 -08001512 final ActivityRecord targetTaskTop = newTask
1513 ? null : targetTask.getTopNonFinishingActivity();
Louis Changbde91e92019-08-16 17:19:47 +08001514 if (targetTaskTop != null) {
1515 // Recycle the target task for this launch.
Louis Changf7dd7f22019-11-05 11:59:56 +08001516 startResult = recycleTask(targetTask, targetTaskTop, reusedTask);
Louis Changbde91e92019-08-16 17:19:47 +08001517 if (startResult != START_SUCCESS) {
1518 return startResult;
Wale Ogunwale01d66562015-12-29 08:19:19 -08001519 }
Louis Changf7dd7f22019-11-05 11:59:56 +08001520 } else {
1521 mAddingToTask = true;
Wale Ogunwale01d66562015-12-29 08:19:19 -08001522 }
1523
1524 // If the activity being launched is the same as the one currently at the top, then
1525 // we need to check if it should only be launched once.
Wale Ogunwaled32da472018-11-16 07:19:28 -08001526 final ActivityStack topStack = mRootActivityContainer.getTopDisplayFocusedStack();
Louis Changbde91e92019-08-16 17:19:47 +08001527 startResult = deliverToCurrentTopIfNeeded(topStack);
1528 if (startResult != START_SUCCESS) {
1529 return startResult;
Wale Ogunwale01d66562015-12-29 08:19:19 -08001530 }
1531
Louis Changbde91e92019-08-16 17:19:47 +08001532 if (mTargetStack == null) {
1533 mTargetStack = computeStackFocus(mStartActivity, true, mLaunchFlags, mOptions);
Wale Ogunwale01d66562015-12-29 08:19:19 -08001534 }
Louis Changbde91e92019-08-16 17:19:47 +08001535 if (newTask) {
Louis Changcdec0802019-11-11 11:45:07 +08001536 final Task taskToAffiliate = (mLaunchTaskBehind && mSourceRecord != null)
1537 ? mSourceRecord.getTask() : null;
Louis Changbde91e92019-08-16 17:19:47 +08001538 setNewTask(taskToAffiliate);
Louis Changa3e6b892019-09-16 10:39:00 +08001539 if (mService.getLockTaskController().isLockTaskModeViolation(
Louis Changcdec0802019-11-11 11:45:07 +08001540 mStartActivity.getTask())) {
Louis Changa3e6b892019-09-16 10:39:00 +08001541 Slog.e(TAG, "Attempted Lock Task Mode violation mStartActivity=" + mStartActivity);
1542 return START_RETURN_LOCK_TASK_MODE_VIOLATION;
1543 }
Louis Changbde91e92019-08-16 17:19:47 +08001544 } else if (mAddingToTask) {
1545 addOrReparentStartingActivity(targetTask, "adding to task");
1546 }
1547
1548 if (!mAvoidMoveToFront && mDoResume) {
1549 mTargetStack.moveToFront("reuseOrNewTask");
Chong Zhang6cda19c2016-06-14 19:07:56 -07001550 }
Wale Ogunwale01d66562015-12-29 08:19:19 -08001551
Wale Ogunwale586a8ee2019-06-04 13:44:14 +00001552 mService.mUgmInternal.grantUriPermissionFromIntent(mCallingUid, mStartActivity.packageName,
1553 mIntent, mStartActivity.getUriPermissionsLocked(), mStartActivity.mUserId);
Patrick Baumannde37e432019-08-28 09:51:29 -07001554 mService.getPackageManagerInternalLocked().grantImplicitAccess(
Andrii Kulianeceebbf2019-06-26 17:36:51 -07001555 mStartActivity.mUserId, mIntent,
Patrick Baumannb6e72972019-09-20 07:54:47 -07001556 mCallingUid,
Patrick Baumannde37e432019-08-28 09:51:29 -07001557 UserHandle.getAppId(mStartActivity.info.applicationInfo.uid)
1558 );
Wale Ogunwale01d66562015-12-29 08:19:19 -08001559 if (newTask) {
Jeff Changd136e772019-11-05 20:33:52 +08001560 EventLogTags.writeWmCreateTask(mStartActivity.mUserId,
Louis Changcdec0802019-11-11 11:45:07 +08001561 mStartActivity.getTask().mTaskId);
Wale Ogunwale01d66562015-12-29 08:19:19 -08001562 }
Andrii Kulian79d67982019-08-19 11:56:16 -07001563 mStartActivity.logStartActivity(
Jeff Changd136e772019-11-05 20:33:52 +08001564 EventLogTags.WM_CREATE_ACTIVITY, mStartActivity.getTask());
1565
Wale Ogunwale01d66562015-12-29 08:19:19 -08001566 mTargetStack.mLastPausedActivity = null;
Wei Wang98f03f92016-05-18 11:32:52 -07001567
Wale Ogunwaled32da472018-11-16 07:19:28 -08001568 mRootActivityContainer.sendPowerHintForLaunchStartIfNeeded(
1569 false /* forceSend */, mStartActivity);
Wei Wang98f03f92016-05-18 11:32:52 -07001570
Wale Ogunwale21e06482019-11-18 05:14:15 -08001571 mTargetStack.startActivityLocked(mStartActivity, topStack.getTopNonFinishingActivity(),
1572 newTask, mKeepCurTransition, mOptions);
Wale Ogunwale01d66562015-12-29 08:19:19 -08001573 if (mDoResume) {
Bryce Leeaf691c02017-03-20 14:20:22 -07001574 final ActivityRecord topTaskActivity =
Louis Changcdec0802019-11-11 11:45:07 +08001575 mStartActivity.getTask().topRunningActivityLocked();
Wale Ogunwale3b232392016-05-13 15:37:13 -07001576 if (!mTargetStack.isFocusable()
Wale Ogunwale68741142016-05-17 09:40:02 -07001577 || (topTaskActivity != null && topTaskActivity.mTaskOverlay
1578 && mStartActivity != topTaskActivity)) {
Filip Gruszczynski3d7fdc12016-01-31 17:33:29 -08001579 // If the activity is not focusable, we can't resume it, but still would like to
1580 // make sure it becomes visible as it starts (this will also trigger entry
1581 // animation). An example of this are PIP activities.
Wale Ogunwale3b232392016-05-13 15:37:13 -07001582 // Also, we don't want to resume activities in a task that currently has an overlay
1583 // as the starting activity just needs to be in the visible paused state until the
1584 // over is removed.
Wale Ogunwale076c3b12019-11-20 12:17:22 -08001585 mTargetStack.ensureActivitiesVisible(mStartActivity, 0, !PRESERVE_WINDOWS);
Wale Ogunwaleae846f42016-02-22 14:00:56 -08001586 // Go ahead and tell window manager to execute app transition for this activity
1587 // since the app transition will not be triggered through the resume channel.
Wale Ogunwale3a256e62018-12-06 14:41:18 -08001588 mTargetStack.getDisplay().mDisplayContent.executeAppTransition();
Wale Ogunwale3b232392016-05-13 15:37:13 -07001589 } else {
Winson Chung32066032016-11-04 11:55:21 -07001590 // If the target stack was not previously focusable (previous top running activity
1591 // on that stack was not visible) then any prior calls to move the stack to the
1592 // will not update the focused stack. If starting the new activity now allows the
1593 // task stack to be focusable, then ensure that we now update the focused stack
1594 // accordingly.
Andrii Kulian5f750bc2018-07-17 08:57:23 -07001595 if (mTargetStack.isFocusable()
Wale Ogunwaled32da472018-11-16 07:19:28 -08001596 && !mRootActivityContainer.isTopDisplayFocusedStack(mTargetStack)) {
Riddle Hsu2ca561b2019-10-08 21:58:58 +08001597 mTargetStack.moveToFront("startActivityInner");
Winson Chung32066032016-11-04 11:55:21 -07001598 }
Wale Ogunwaled32da472018-11-16 07:19:28 -08001599 mRootActivityContainer.resumeFocusedStacksTopActivities(
1600 mTargetStack, mStartActivity, mOptions);
Filip Gruszczynski3d7fdc12016-01-31 17:33:29 -08001601 }
Winson Chung1dbc8112017-09-28 18:05:31 -07001602 } else if (mStartActivity != null) {
Louis Changcdec0802019-11-11 11:45:07 +08001603 mSupervisor.mRecentTasks.add(mStartActivity.getTask());
Wale Ogunwale01d66562015-12-29 08:19:19 -08001604 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001605 mRootActivityContainer.updateUserStack(mStartActivity.mUserId, mTargetStack);
Wale Ogunwale01d66562015-12-29 08:19:19 -08001606
Louis Changcdec0802019-11-11 11:45:07 +08001607 mSupervisor.handleNonResizableTaskIfNeeded(mStartActivity.getTask(),
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001608 preferredWindowingMode, mPreferredDisplayId, mTargetStack);
Wale Ogunwale01d66562015-12-29 08:19:19 -08001609
1610 return START_SUCCESS;
1611 }
1612
Louis Changcdec0802019-11-11 11:45:07 +08001613 private Task computeTargetTask() {
Louis Changf7dd7f22019-11-05 11:59:56 +08001614 if (mStartActivity.resultTo == null && mInTask == null && !mAddingToTask
Louis Changbde91e92019-08-16 17:19:47 +08001615 && (mLaunchFlags & FLAG_ACTIVITY_NEW_TASK) != 0) {
1616 // A new task should be created instead of using existing one.
1617 return null;
1618 } else if (mSourceRecord != null) {
Louis Changcdec0802019-11-11 11:45:07 +08001619 return mSourceRecord.getTask();
Louis Changbde91e92019-08-16 17:19:47 +08001620 } else if (mInTask != null) {
1621 return mInTask;
1622 } else {
1623 final ActivityRecord top = computeStackFocus(mStartActivity, false /* newTask */,
Wale Ogunwale21e06482019-11-18 05:14:15 -08001624 mLaunchFlags, mOptions).getTopNonFinishingActivity();
Louis Changbde91e92019-08-16 17:19:47 +08001625 if (top != null) {
Louis Changcdec0802019-11-11 11:45:07 +08001626 return top.getTask();
Louis Changbde91e92019-08-16 17:19:47 +08001627 }
1628 }
1629 return null;
1630 }
1631
Louis Changcdec0802019-11-11 11:45:07 +08001632 private int isAllowedToStart(ActivityRecord r, boolean newTask, Task targetTask) {
Louis Changbde91e92019-08-16 17:19:47 +08001633 if (mStartActivity.packageName == null) {
1634 if (mStartActivity.resultTo != null) {
1635 mStartActivity.resultTo.sendResult(INVALID_UID, mStartActivity.resultWho,
1636 mStartActivity.requestCode, RESULT_CANCELED, null /* data */);
1637 }
1638 ActivityOptions.abort(mOptions);
1639 return START_CLASS_NOT_FOUND;
1640 }
1641
1642 // Do not start home activity if it cannot be launched on preferred display. We are not
1643 // doing this in ActivityStackSupervisor#canPlaceEntityOnDisplay because it might
1644 // fallback to launch on other displays.
1645 if (r.isActivityTypeHome() && !mRootActivityContainer.canStartHomeOnDisplay(r.info,
1646 mPreferredDisplayId, true /* allowInstrumenting */)) {
1647 Slog.w(TAG, "Cannot launch home on display " + mPreferredDisplayId);
1648 return START_CANCELED;
1649 }
1650
Wale Ogunwalea38654f2019-11-17 20:37:15 -08001651 if (mRestrictedBgActivity && (newTask || !targetTask.isUidPresent(mCallingUid))
Louis Changbde91e92019-08-16 17:19:47 +08001652 && handleBackgroundActivityAbort(mStartActivity)) {
1653 Slog.e(TAG, "Abort background activity starts from " + mCallingUid);
1654 return START_ABORTED;
1655 }
1656
1657 // When the flags NEW_TASK and CLEAR_TASK are set, then the task gets reused but still
1658 // needs to be a lock task mode violation since the task gets cleared out and the device
1659 // would otherwise leave the locked task.
1660 final boolean isNewClearTask =
1661 (mLaunchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
1662 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK);
Louis Changa3e6b892019-09-16 10:39:00 +08001663 if (!newTask && mService.getLockTaskController().isLockTaskModeViolation(targetTask,
1664 isNewClearTask)) {
Louis Changbde91e92019-08-16 17:19:47 +08001665 Slog.e(TAG, "Attempted Lock Task Mode violation mStartActivity=" + mStartActivity);
1666 return START_RETURN_LOCK_TASK_MODE_VIOLATION;
1667 }
1668
1669 return START_SUCCESS;
1670 }
1671
1672 /**
1673 * Prepare the target task to be reused for this launch, which including:
1674 * - Position the target task on valid stack on preferred display.
1675 * - Comply to the specified activity launch flags
1676 * - Determine whether need to add a new activity on top or just brought the task to front.
1677 */
Louis Chang07b13002019-11-27 22:08:37 +08001678 @VisibleForTesting
1679 int recycleTask(Task targetTask, ActivityRecord targetTaskTop, Task reusedTask) {
1680 // Should not recycle task which is from a different user, just adding the starting
1681 // activity to the task.
1682 if (targetTask.mUserId != mStartActivity.mUserId) {
1683 mTargetStack = targetTask.getStack();
1684 mAddingToTask = true;
1685 return START_SUCCESS;
1686 }
1687
Louis Changbde91e92019-08-16 17:19:47 +08001688 // True if we are clearing top and resetting of a standard (default) launch mode
1689 // ({@code LAUNCH_MULTIPLE}) activity. The existing activity will be finished.
1690 final boolean clearTopAndResetStandardLaunchMode =
1691 (mLaunchFlags & (FLAG_ACTIVITY_CLEAR_TOP | FLAG_ACTIVITY_RESET_TASK_IF_NEEDED))
1692 == (FLAG_ACTIVITY_CLEAR_TOP | FLAG_ACTIVITY_RESET_TASK_IF_NEEDED)
1693 && mLaunchMode == LAUNCH_MULTIPLE;
1694
Wale Ogunwaleda8b8272018-11-29 19:37:37 -08001695 boolean clearTaskForReuse = false;
Louis Changf7dd7f22019-11-05 11:59:56 +08001696 if (reusedTask != null) {
Wale Ogunwaleda8b8272018-11-29 19:37:37 -08001697 // If mStartActivity does not have a task associated with it, associate it with the
1698 // reused activity's task. Do not do so if we're clearing top and resetting for a
1699 // standard launchMode activity.
Louis Changcdec0802019-11-11 11:45:07 +08001700 if (mStartActivity.getTask() == null && !clearTopAndResetStandardLaunchMode) {
Louis Changf7dd7f22019-11-05 11:59:56 +08001701 mStartActivity.setTaskForReuse(reusedTask);
Wale Ogunwaleda8b8272018-11-29 19:37:37 -08001702 clearTaskForReuse = true;
1703 }
1704
Louis Changbde91e92019-08-16 17:19:47 +08001705 if (targetTask.intent == null) {
1706 // This task was started because of movement of the activity based on
1707 // affinity...
1708 // Now that we are actually launching it, we can assign the base intent.
1709 targetTask.setIntent(mStartActivity);
1710 } else {
1711 final boolean taskOnHome =
1712 (mStartActivity.intent.getFlags() & FLAG_ACTIVITY_TASK_ON_HOME) != 0;
1713 if (taskOnHome) {
1714 targetTask.intent.addFlags(FLAG_ACTIVITY_TASK_ON_HOME);
1715 } else {
1716 targetTask.intent.removeFlags(FLAG_ACTIVITY_TASK_ON_HOME);
1717 }
1718 }
1719 }
1720
1721 mRootActivityContainer.sendPowerHintForLaunchStartIfNeeded(false /* forceSend */,
1722 targetTaskTop);
1723
1724 setTargetStackIfNeeded(targetTaskTop);
1725
Louis Changbde91e92019-08-16 17:19:47 +08001726 // When there is a reused activity and the current result is a trampoline activity,
1727 // set the reused activity as the result.
Louis Chang54fbb052019-10-16 17:10:17 +08001728 if (mLastStartActivityRecord != null
1729 && (mLastStartActivityRecord.finishing || mLastStartActivityRecord.noDisplay)) {
1730 mLastStartActivityRecord = targetTaskTop;
Louis Changbde91e92019-08-16 17:19:47 +08001731 }
1732
1733 if ((mStartFlags & START_FLAG_ONLY_IF_NEEDED) != 0) {
1734 // We don't need to start a new activity, and the client said not to do anything
1735 // if that is the case, so this is it! And for paranoia, make sure we have
1736 // correctly resumed the top activity.
1737 if (!mMovedToFront && mDoResume) {
1738 if (DEBUG_TASKS) {
1739 Slog.d(TAG_TASKS, "Bring to front target: " + mTargetStack
1740 + " from " + targetTaskTop);
1741 }
1742 mTargetStack.moveToFront("intentActivityFound");
1743 }
1744 resumeTargetStackIfNeeded();
1745 return START_RETURN_INTENT_TO_CALLER;
1746 }
1747
Wale Ogunwale21e06482019-11-18 05:14:15 -08001748 complyActivityFlags(targetTask,
1749 reusedTask != null ? reusedTask.getTopNonFinishingActivity() : null);
Louis Changbde91e92019-08-16 17:19:47 +08001750
Wale Ogunwaleda8b8272018-11-29 19:37:37 -08001751 if (clearTaskForReuse) {
1752 // Clear task for re-use so later code to methods
1753 // {@link #setTaskFromReuseOrCreateNewTask}, {@link #setTaskFromSourceRecord}, or
1754 // {@link #setTaskFromInTask} can parent it to the task.
1755 mStartActivity.setTaskForReuse(null);
1756 }
1757
Louis Changbde91e92019-08-16 17:19:47 +08001758 if (mAddingToTask) {
1759 return START_SUCCESS;
1760 }
1761
Louis Chang3ee5fc02019-09-23 11:32:10 +08001762 if (mMovedToFront) {
1763 // We moved the task to front, use starting window to hide initial drawn delay.
1764 targetTaskTop.showStartingWindow(null /* prev */, false /* newTask */,
1765 true /* taskSwitch */);
1766 } else if (mDoResume) {
1767 // Make sure the stack and its belonging display are moved to topmost.
Louis Changbde91e92019-08-16 17:19:47 +08001768 mTargetStack.moveToFront("intentActivityFound");
1769 }
1770 // We didn't do anything... but it was needed (a.k.a., client don't use that intent!)
1771 // And for paranoia, make sure we have correctly resumed the top activity.
1772 resumeTargetStackIfNeeded();
Louis Chang54fbb052019-10-16 17:10:17 +08001773 // The reusedActivity could be finishing, for example of starting an activity with
1774 // FLAG_ACTIVITY_CLEAR_TOP flag. In that case, return the top running activity in the
1775 // task instead.
1776 mLastStartActivityRecord =
Wale Ogunwale21e06482019-11-18 05:14:15 -08001777 targetTaskTop.finishing ? targetTask.getTopNonFinishingActivity() : targetTaskTop;
Louis Changbde91e92019-08-16 17:19:47 +08001778 return mMovedToFront ? START_TASK_TO_FRONT : START_DELIVERED_TO_TOP;
1779 }
1780
1781 /**
1782 * Check if the activity being launched is the same as the one currently at the top and it
1783 * should only be launched once.
1784 */
1785 private int deliverToCurrentTopIfNeeded(ActivityStack topStack) {
1786 final ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(mNotTop);
1787 final boolean dontStart = top != null && mStartActivity.resultTo == null
1788 && top.mActivityComponent.equals(mStartActivity.mActivityComponent)
1789 && top.mUserId == mStartActivity.mUserId
1790 && top.attachedToProcess()
1791 && ((mLaunchFlags & FLAG_ACTIVITY_SINGLE_TOP) != 0
1792 || isLaunchModeOneOf(LAUNCH_SINGLE_TOP, LAUNCH_SINGLE_TASK))
1793 // This allows home activity to automatically launch on secondary display when
1794 // display added, if home was the top activity on default display, instead of
1795 // sending new intent to the home activity on default display.
1796 && (!top.isActivityTypeHome() || top.getDisplayId() == mPreferredDisplayId);
1797 if (!dontStart) {
1798 return START_SUCCESS;
1799 }
1800
1801 // For paranoia, make sure we have correctly resumed the top activity.
1802 topStack.mLastPausedActivity = null;
1803 if (mDoResume) {
1804 mRootActivityContainer.resumeFocusedStacksTopActivities();
1805 }
1806 ActivityOptions.abort(mOptions);
1807 if ((mStartFlags & START_FLAG_ONLY_IF_NEEDED) != 0) {
1808 // We don't need to start a new activity, and the client said not to do anything if
1809 // that is the case, so this is it!
1810 return START_RETURN_INTENT_TO_CALLER;
1811 }
1812
1813 deliverNewIntent(top);
1814
1815 // Don't use mStartActivity.task to show the toast. We're not starting a new activity but
1816 // reusing 'top'. Fields in mStartActivity may not be fully initialized.
Louis Changcdec0802019-11-11 11:45:07 +08001817 mSupervisor.handleNonResizableTaskIfNeeded(top.getTask(),
Louis Changbde91e92019-08-16 17:19:47 +08001818 mLaunchParams.mWindowingMode, mPreferredDisplayId, topStack);
1819
1820 return START_DELIVERED_TO_TOP;
1821 }
1822
1823 /**
1824 * Applying the launching flags to the task, which might clear few or all the activities in the
1825 * task.
1826 */
Louis Changcdec0802019-11-11 11:45:07 +08001827 private void complyActivityFlags(Task targetTask, ActivityRecord reusedActivity) {
Wale Ogunwale21e06482019-11-18 05:14:15 -08001828 ActivityRecord targetTaskTop = targetTask.getTopNonFinishingActivity();
Louis Chang2dcb1272019-09-27 15:01:19 +08001829 final boolean resetTask =
1830 reusedActivity != null && (mLaunchFlags & FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0;
1831 if (resetTask) {
Wale Ogunwaledfbeed72019-11-20 08:57:39 -08001832 targetTaskTop = mTargetStack.resetTaskIfNeeded(targetTaskTop, mStartActivity);
Louis Changbde91e92019-08-16 17:19:47 +08001833 }
1834
1835 if ((mLaunchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
1836 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK)) {
1837 // The caller has requested to completely replace any existing task with its new
1838 // activity. Well that should not be too hard...
Louis Changcdec0802019-11-11 11:45:07 +08001839 // Note: we must persist the {@link Task} first as intentActivity could be
Louis Changbde91e92019-08-16 17:19:47 +08001840 // removed from calling performClearTaskLocked (For example, if it is being brought out
1841 // of history or if it is finished immediately), thus disassociating the task. Also note
Louis Changcdec0802019-11-11 11:45:07 +08001842 // that mReuseTask is reset as a result of {@link Task#performClearTaskLocked}
Louis Changbde91e92019-08-16 17:19:47 +08001843 // launching another activity.
1844 // TODO(b/36119896): We shouldn't trigger activity launches in this path since we are
1845 // already launching one.
1846 targetTask.performClearTaskLocked();
1847 targetTask.setIntent(mStartActivity);
1848 mAddingToTask = true;
1849 } else if ((mLaunchFlags & FLAG_ACTIVITY_CLEAR_TOP) != 0
1850 || isDocumentLaunchesIntoExisting(mLaunchFlags)
1851 || isLaunchModeOneOf(LAUNCH_SINGLE_INSTANCE, LAUNCH_SINGLE_TASK)) {
1852 // In this situation we want to remove all activities from the task up to the one
1853 // being started. In most cases this means we are resetting the task to its initial
1854 // state.
1855 final ActivityRecord top = targetTask.performClearTaskForReuseLocked(mStartActivity,
1856 mLaunchFlags);
1857
1858 // The above code can remove {@code reusedActivity} from the task, leading to the
Louis Changcdec0802019-11-11 11:45:07 +08001859 // {@code ActivityRecord} removing its reference to the {@code Task}. The task
Wale Ogunwalec17418e2019-10-13 23:00:40 +02001860 // reference is needed in the call below to {@link setTargetStackAndMoveToFrontIfNeeded}
Louis Changcdec0802019-11-11 11:45:07 +08001861 if (targetTaskTop.getTask() == null) {
Wale Ogunwale2322bed2019-10-10 17:24:19 +02001862 targetTask.addChild(targetTaskTop);
Louis Changbde91e92019-08-16 17:19:47 +08001863 }
1864
1865 if (top != null) {
1866 if (top.isRootOfTask()) {
1867 // Activity aliases may mean we use different intents for the top activity,
1868 // so make sure the task now has the identity of the new intent.
Louis Changcdec0802019-11-11 11:45:07 +08001869 top.getTask().setIntent(mStartActivity);
Louis Changbde91e92019-08-16 17:19:47 +08001870 }
1871 deliverNewIntent(top);
1872 } else {
1873 // A special case: we need to start the activity because it is not currently
1874 // running, and the caller has asked to clear the current task to have this
1875 // activity at the top.
1876 mAddingToTask = true;
1877 if (targetTask.getStack() == null) {
1878 // Target stack got cleared when we all activities were removed above.
1879 // Go ahead and reset it.
1880 mTargetStack = computeStackFocus(mSourceRecord, false /* newTask */,
1881 mLaunchFlags, mOptions);
Wale Ogunwale2322bed2019-10-10 17:24:19 +02001882 mTargetStack.addChild(targetTask, !mLaunchTaskBehind /* toTop */,
1883 (mStartActivity.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
Louis Changbde91e92019-08-16 17:19:47 +08001884 }
1885 }
1886 } else if ((mLaunchFlags & FLAG_ACTIVITY_CLEAR_TOP) == 0 && !mAddingToTask
1887 && (mLaunchFlags & FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
1888 // In this case, we are launching an activity in our own task that may
1889 // already be running somewhere in the history, and we want to shuffle it to
1890 // the front of the stack if so.
Wale Ogunwalea38654f2019-11-17 20:37:15 -08001891 final ActivityRecord act =
1892 targetTask.findActivityInHistory(mStartActivity.mActivityComponent);
Louis Changbde91e92019-08-16 17:19:47 +08001893 if (act != null) {
Louis Changcdec0802019-11-11 11:45:07 +08001894 final Task task = act.getTask();
Louis Changbde91e92019-08-16 17:19:47 +08001895 task.moveActivityToFrontLocked(act);
1896 act.updateOptionsLocked(mOptions);
1897 deliverNewIntent(act);
1898 mTargetStack.mLastPausedActivity = null;
1899 } else {
1900 mAddingToTask = true;
1901 }
1902 } else if (mStartActivity.mActivityComponent.equals(targetTask.realActivity)) {
Louis Chang382419b2019-10-03 21:43:38 +08001903 if (targetTask == mInTask) {
1904 // In this case we are bringing up an existing activity from a recent task. We
1905 // don't need to add a new activity instance on top.
1906 } else if (((mLaunchFlags & FLAG_ACTIVITY_SINGLE_TOP) != 0
1907 || LAUNCH_SINGLE_TOP == mLaunchMode)
1908 && targetTaskTop.mActivityComponent.equals(mStartActivity.mActivityComponent)
1909 && mStartActivity.resultTo == null) {
1910 // In this case the top activity on the task is the same as the one being launched,
1911 // so we take that as a request to bring the task to the foreground. If the top
1912 // activity in the task is the root activity, deliver this new intent to it if it
1913 // desires.
Louis Changbde91e92019-08-16 17:19:47 +08001914 if (targetTaskTop.isRootOfTask()) {
Louis Changcdec0802019-11-11 11:45:07 +08001915 targetTaskTop.getTask().setIntent(mStartActivity);
Louis Changbde91e92019-08-16 17:19:47 +08001916 }
1917 deliverNewIntent(targetTaskTop);
1918 } else if (!targetTask.isSameIntentFilter(mStartActivity)) {
1919 // In this case we are launching the root activity of the task, but with a
1920 // different intent. We should start a new instance on top.
1921 mAddingToTask = true;
1922 } else if (reusedActivity == null) {
1923 mAddingToTask = true;
1924 }
Louis Chang2dcb1272019-09-27 15:01:19 +08001925 } else if (!resetTask) {
Louis Changbde91e92019-08-16 17:19:47 +08001926 // In this case an activity is being launched in to an existing task, without
1927 // resetting that task. This is typically the situation of launching an activity
1928 // from a notification or shortcut. We want to place the new activity on top of the
1929 // current task.
1930 mAddingToTask = true;
1931 } else if (!targetTask.rootWasReset) {
1932 // In this case we are launching into an existing task that has not yet been started
1933 // from its front door. The current task has been brought to the front. Ideally,
1934 // we'd probably like to place this new task at the bottom of its stack, but that's
1935 // a little hard to do with the current organization of the code so for now we'll
1936 // just drop it.
1937 targetTask.setIntent(mStartActivity);
1938 }
1939 }
1940
Bryce Leedaa91e42017-12-06 14:13:01 -08001941 /**
1942 * Resets the {@link ActivityStarter} state.
1943 * @param clearRequest whether the request should be reset to default values.
1944 */
1945 void reset(boolean clearRequest) {
1946 mStartActivity = null;
1947 mIntent = null;
1948 mCallingUid = -1;
1949 mOptions = null;
Ricky Waib147fa12019-04-25 16:08:30 +01001950 mRestrictedBgActivity = false;
Bryce Leedaa91e42017-12-06 14:13:01 -08001951
1952 mLaunchTaskBehind = false;
1953 mLaunchFlags = 0;
1954 mLaunchMode = INVALID_LAUNCH_MODE;
1955
Bryce Leeec55eb02017-12-05 20:51:27 -08001956 mLaunchParams.reset();
Bryce Leedaa91e42017-12-06 14:13:01 -08001957
1958 mNotTop = null;
1959 mDoResume = false;
1960 mStartFlags = 0;
1961 mSourceRecord = null;
1962 mPreferredDisplayId = INVALID_DISPLAY;
1963
1964 mInTask = null;
1965 mAddingToTask = false;
1966 mReuseTask = null;
1967
1968 mNewTaskInfo = null;
1969 mNewTaskIntent = null;
1970 mSourceStack = null;
1971
1972 mTargetStack = null;
1973 mMovedToFront = false;
1974 mNoAnimation = false;
1975 mKeepCurTransition = false;
1976 mAvoidMoveToFront = false;
Winson Chunge219ae12019-07-18 13:43:23 -07001977 mFrozeTaskList = false;
Bryce Leedaa91e42017-12-06 14:13:01 -08001978
1979 mVoiceSession = null;
1980 mVoiceInteractor = null;
1981
1982 mIntentDelivered = false;
1983
1984 if (clearRequest) {
1985 mRequest.reset();
1986 }
1987 }
1988
Louis Changcdec0802019-11-11 11:45:07 +08001989 private void setInitialState(ActivityRecord r, ActivityOptions options, Task inTask,
Wale Ogunwale01d66562015-12-29 08:19:19 -08001990 boolean doResume, int startFlags, ActivityRecord sourceRecord,
Ricky Waib147fa12019-04-25 16:08:30 +01001991 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1992 boolean restrictedBgActivity) {
Bryce Leedaa91e42017-12-06 14:13:01 -08001993 reset(false /* clearRequest */);
1994
Wale Ogunwale01d66562015-12-29 08:19:19 -08001995 mStartActivity = r;
1996 mIntent = r.intent;
1997 mOptions = options;
1998 mCallingUid = r.launchedFromUid;
1999 mSourceRecord = sourceRecord;
2000 mVoiceSession = voiceSession;
2001 mVoiceInteractor = voiceInteractor;
Ricky Waib147fa12019-04-25 16:08:30 +01002002 mRestrictedBgActivity = restrictedBgActivity;
Wale Ogunwale01d66562015-12-29 08:19:19 -08002003
Bryce Leeec55eb02017-12-05 20:51:27 -08002004 mLaunchParams.reset();
Bryce Leedacefc42017-10-10 12:56:02 -07002005
Louis Chang6fb1e842018-12-03 16:07:50 +08002006 // Preferred display id is the only state we need for now and it could be updated again
2007 // after we located a reusable task (which might be resided in another display).
Garfield Tan706dbcb2018-10-15 11:33:02 -07002008 mSupervisor.getLaunchParamsController().calculate(inTask, r.info.windowLayout, r,
Louis Chang6fb1e842018-12-03 16:07:50 +08002009 sourceRecord, options, PHASE_DISPLAY, mLaunchParams);
2010 mPreferredDisplayId =
2011 mLaunchParams.hasPreferredDisplay() ? mLaunchParams.mPreferredDisplayId
2012 : DEFAULT_DISPLAY;
Garfield Tanb5cc09f2018-09-28 10:06:52 -07002013
Bryce Lee7daee392017-10-12 13:46:18 -07002014 mLaunchMode = r.launchMode;
2015
Wale Ogunwale01d66562015-12-29 08:19:19 -08002016 mLaunchFlags = adjustLaunchFlagsToDocumentMode(
Bryce Lee7daee392017-10-12 13:46:18 -07002017 r, LAUNCH_SINGLE_INSTANCE == mLaunchMode,
2018 LAUNCH_SINGLE_TASK == mLaunchMode, mIntent.getFlags());
Wale Ogunwale01d66562015-12-29 08:19:19 -08002019 mLaunchTaskBehind = r.mLaunchTaskBehind
Bryce Lee7daee392017-10-12 13:46:18 -07002020 && !isLaunchModeOneOf(LAUNCH_SINGLE_TASK, LAUNCH_SINGLE_INSTANCE)
Wale Ogunwale01d66562015-12-29 08:19:19 -08002021 && (mLaunchFlags & FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
2022
2023 sendNewTaskResultRequestIfNeeded();
2024
2025 if ((mLaunchFlags & FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
2026 mLaunchFlags |= FLAG_ACTIVITY_NEW_TASK;
2027 }
2028
2029 // If we are actually going to launch in to a new task, there are some cases where
2030 // we further want to do multiple task.
2031 if ((mLaunchFlags & FLAG_ACTIVITY_NEW_TASK) != 0) {
2032 if (mLaunchTaskBehind
2033 || r.info.documentLaunchMode == DOCUMENT_LAUNCH_ALWAYS) {
2034 mLaunchFlags |= FLAG_ACTIVITY_MULTIPLE_TASK;
2035 }
2036 }
2037
2038 // We'll invoke onUserLeaving before onPause only if the launching
2039 // activity did not explicitly state that this is an automated launch.
2040 mSupervisor.mUserLeaving = (mLaunchFlags & FLAG_ACTIVITY_NO_USER_ACTION) == 0;
2041 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
2042 "startActivity() => mUserLeaving=" + mSupervisor.mUserLeaving);
2043
2044 // If the caller has asked not to resume at this point, we make note
2045 // of this in the record so that we can skip it when trying to find
2046 // the top running activity.
2047 mDoResume = doResume;
Louis Chang37317152019-05-09 09:53:58 +08002048 if (!doResume || !r.okToShowLocked() || mLaunchTaskBehind) {
Wale Ogunwale01d66562015-12-29 08:19:19 -08002049 r.delayedResume = true;
2050 mDoResume = false;
2051 }
2052
Winson Chunge2d72172018-01-25 17:46:20 +00002053 if (mOptions != null) {
2054 if (mOptions.getLaunchTaskId() != -1 && mOptions.getTaskOverlay()) {
2055 r.mTaskOverlay = true;
2056 if (!mOptions.canTaskOverlayResume()) {
Louis Changcdec0802019-11-11 11:45:07 +08002057 final Task task = mRootActivityContainer.anyTaskForId(
Winson Chunge2d72172018-01-25 17:46:20 +00002058 mOptions.getLaunchTaskId());
Wale Ogunwale21e06482019-11-18 05:14:15 -08002059 final ActivityRecord top = task != null
2060 ? task.getTopNonFinishingActivity() : null;
Bryce Lee7ace3952018-02-16 14:34:32 -08002061 if (top != null && !top.isState(RESUMED)) {
Jorim Jaggic875ae72016-04-26 22:41:06 -07002062
Winson Chunge2d72172018-01-25 17:46:20 +00002063 // The caller specifies that we'd like to be avoided to be moved to the
2064 // front, so be it!
2065 mDoResume = false;
2066 mAvoidMoveToFront = true;
2067 }
Winson Chungcbcadc92017-01-12 15:54:12 -08002068 }
Winson Chunge2d72172018-01-25 17:46:20 +00002069 } else if (mOptions.getAvoidMoveToFront()) {
Winson Chungba40d3a2018-05-16 09:40:16 -07002070 mDoResume = false;
Winson Chunge2d72172018-01-25 17:46:20 +00002071 mAvoidMoveToFront = true;
Jorim Jaggic875ae72016-04-26 22:41:06 -07002072 }
2073 }
2074
Louis Chang2f4e9b462019-03-05 16:43:15 +08002075 mNotTop = (mLaunchFlags & FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? sourceRecord : null;
Wale Ogunwale01d66562015-12-29 08:19:19 -08002076
2077 mInTask = inTask;
2078 // In some flows in to this function, we retrieve the task record and hold on to it
2079 // without a lock before calling back in to here... so the task at this point may
2080 // not actually be in recents. Check for that, and if it isn't in recents just
2081 // consider it invalid.
2082 if (inTask != null && !inTask.inRecents) {
2083 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
2084 mInTask = null;
2085 }
2086
2087 mStartFlags = startFlags;
2088 // If the onlyIfNeeded flag is set, then we can do this if the activity being launched
2089 // is the same as the one making the call... or, as a special case, if we do not know
2090 // the caller then we count the current top activity as the caller.
2091 if ((startFlags & START_FLAG_ONLY_IF_NEEDED) != 0) {
2092 ActivityRecord checkedCaller = sourceRecord;
2093 if (checkedCaller == null) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08002094 checkedCaller = mRootActivityContainer.getTopDisplayFocusedStack()
Andrii Kulian5f750bc2018-07-17 08:57:23 -07002095 .topRunningNonDelayedActivityLocked(mNotTop);
Wale Ogunwale01d66562015-12-29 08:19:19 -08002096 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002097 if (!checkedCaller.mActivityComponent.equals(r.mActivityComponent)) {
Wale Ogunwale01d66562015-12-29 08:19:19 -08002098 // Caller is not the same as launcher, so always needed.
2099 mStartFlags &= ~START_FLAG_ONLY_IF_NEEDED;
2100 }
2101 }
2102
2103 mNoAnimation = (mLaunchFlags & FLAG_ACTIVITY_NO_ANIMATION) != 0;
Ricky Waib147fa12019-04-25 16:08:30 +01002104
Alan Stokes07389b62019-05-20 15:22:54 +01002105 if (mRestrictedBgActivity && !mService.isBackgroundActivityStartsEnabled()) {
Ricky Waib147fa12019-04-25 16:08:30 +01002106 mAvoidMoveToFront = true;
2107 mDoResume = false;
2108 }
Wale Ogunwale01d66562015-12-29 08:19:19 -08002109 }
2110
2111 private void sendNewTaskResultRequestIfNeeded() {
Andrii Kulian79d67982019-08-19 11:56:16 -07002112 if (mStartActivity.resultTo != null && (mLaunchFlags & FLAG_ACTIVITY_NEW_TASK) != 0) {
Wale Ogunwale01d66562015-12-29 08:19:19 -08002113 // For whatever reason this activity is being launched into a new task...
2114 // yet the caller has requested a result back. Well, that is pretty messed up,
2115 // so instead immediately send back a cancel and let the new task continue launched
2116 // as normal without a dependency on its originator.
2117 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
Andrii Kulian79d67982019-08-19 11:56:16 -07002118 mStartActivity.resultTo.sendResult(INVALID_UID, mStartActivity.resultWho,
2119 mStartActivity.requestCode, RESULT_CANCELED, null /* data */);
Wale Ogunwale01d66562015-12-29 08:19:19 -08002120 mStartActivity.resultTo = null;
2121 }
2122 }
2123
2124 private void computeLaunchingTaskFlags() {
2125 // If the caller is not coming from another activity, but has given us an explicit task into
2126 // which they would like us to launch the new activity, then let's see about doing that.
Andrii Kulian02b7a832016-10-06 23:11:56 -07002127 if (mSourceRecord == null && mInTask != null && mInTask.getStack() != null) {
Wale Ogunwale01d66562015-12-29 08:19:19 -08002128 final Intent baseIntent = mInTask.getBaseIntent();
2129 final ActivityRecord root = mInTask.getRootActivity();
2130 if (baseIntent == null) {
2131 ActivityOptions.abort(mOptions);
2132 throw new IllegalArgumentException("Launching into task without base intent: "
2133 + mInTask);
2134 }
2135
2136 // If this task is empty, then we are adding the first activity -- it
2137 // determines the root, and must be launching as a NEW_TASK.
Bryce Lee7daee392017-10-12 13:46:18 -07002138 if (isLaunchModeOneOf(LAUNCH_SINGLE_INSTANCE, LAUNCH_SINGLE_TASK)) {
Wale Ogunwale01d66562015-12-29 08:19:19 -08002139 if (!baseIntent.getComponent().equals(mStartActivity.intent.getComponent())) {
2140 ActivityOptions.abort(mOptions);
2141 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
2142 + mStartActivity + " into different task " + mInTask);
2143 }
2144 if (root != null) {
2145 ActivityOptions.abort(mOptions);
2146 throw new IllegalArgumentException("Caller with mInTask " + mInTask
2147 + " has root " + root + " but target is singleInstance/Task");
2148 }
2149 }
2150
2151 // If task is empty, then adopt the interesting intent launch flags in to the
2152 // activity being started.
2153 if (root == null) {
2154 final int flagsOfInterest = FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_MULTIPLE_TASK
2155 | FLAG_ACTIVITY_NEW_DOCUMENT | FLAG_ACTIVITY_RETAIN_IN_RECENTS;
2156 mLaunchFlags = (mLaunchFlags & ~flagsOfInterest)
2157 | (baseIntent.getFlags() & flagsOfInterest);
2158 mIntent.setFlags(mLaunchFlags);
2159 mInTask.setIntent(mStartActivity);
2160 mAddingToTask = true;
2161
2162 // If the task is not empty and the caller is asking to start it as the root of
2163 // a new task, then we don't actually want to start this on the task. We will
2164 // bring the task to the front, and possibly give it a new intent.
2165 } else if ((mLaunchFlags & FLAG_ACTIVITY_NEW_TASK) != 0) {
2166 mAddingToTask = false;
2167
2168 } else {
2169 mAddingToTask = true;
2170 }
2171
2172 mReuseTask = mInTask;
2173 } else {
2174 mInTask = null;
2175 // Launch ResolverActivity in the source task, so that it stays in the task bounds
2176 // when in freeform workspace.
2177 // Also put noDisplay activities in the source task. These by itself can be placed
2178 // in any task/stack, however it could launch other activities like ResolverActivity,
2179 // and we want those to stay in the original task.
Louis Chang6a9be162019-07-15 10:41:32 +08002180 if ((mStartActivity.isResolverOrDelegateActivity() || mStartActivity.noDisplay)
2181 && mSourceRecord != null && mSourceRecord.inFreeformWindowingMode()) {
Wale Ogunwale01d66562015-12-29 08:19:19 -08002182 mAddingToTask = true;
2183 }
2184 }
2185
2186 if (mInTask == null) {
2187 if (mSourceRecord == null) {
2188 // This activity is not being started from another... in this
2189 // case we -always- start a new task.
2190 if ((mLaunchFlags & FLAG_ACTIVITY_NEW_TASK) == 0 && mInTask == null) {
2191 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
2192 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + mIntent);
2193 mLaunchFlags |= FLAG_ACTIVITY_NEW_TASK;
2194 }
2195 } else if (mSourceRecord.launchMode == LAUNCH_SINGLE_INSTANCE) {
2196 // The original activity who is starting us is running as a single
2197 // instance... this new activity it is starting must go on its
2198 // own task.
2199 mLaunchFlags |= FLAG_ACTIVITY_NEW_TASK;
Bryce Lee7daee392017-10-12 13:46:18 -07002200 } else if (isLaunchModeOneOf(LAUNCH_SINGLE_INSTANCE, LAUNCH_SINGLE_TASK)) {
Wale Ogunwale01d66562015-12-29 08:19:19 -08002201 // The activity being started is a single instance... it always
2202 // gets launched into its own task.
2203 mLaunchFlags |= FLAG_ACTIVITY_NEW_TASK;
2204 }
2205 }
2206 }
2207
2208 private void computeSourceStack() {
2209 if (mSourceRecord == null) {
2210 mSourceStack = null;
2211 return;
2212 }
2213 if (!mSourceRecord.finishing) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002214 mSourceStack = mSourceRecord.getActivityStack();
Wale Ogunwale01d66562015-12-29 08:19:19 -08002215 return;
2216 }
2217
2218 // If the source is finishing, we can't further count it as our source. This is because the
2219 // task it is associated with may now be empty and on its way out, so we don't want to
2220 // blindly throw it in to that task. Instead we will take the NEW_TASK flow and try to find
2221 // a task for it. But save the task information so it can be used when creating the new task.
2222 if ((mLaunchFlags & FLAG_ACTIVITY_NEW_TASK) == 0) {
2223 Slog.w(TAG, "startActivity called from finishing " + mSourceRecord
2224 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + mIntent);
2225 mLaunchFlags |= FLAG_ACTIVITY_NEW_TASK;
2226 mNewTaskInfo = mSourceRecord.info;
Bryce Leed9ed45d2017-05-22 15:57:24 -07002227
2228 // It is not guaranteed that the source record will have a task associated with it. For,
2229 // example, if this method is being called for processing a pending activity launch, it
2230 // is possible that the activity has been removed from the task after the launch was
2231 // enqueued.
Louis Changcdec0802019-11-11 11:45:07 +08002232 final Task sourceTask = mSourceRecord.getTask();
Bryce Leed9ed45d2017-05-22 15:57:24 -07002233 mNewTaskIntent = sourceTask != null ? sourceTask.intent : null;
Wale Ogunwale01d66562015-12-29 08:19:19 -08002234 }
2235 mSourceRecord = null;
2236 mSourceStack = null;
2237 }
2238
2239 /**
2240 * Decide whether the new activity should be inserted into an existing task. Returns null
2241 * if not or an ActivityRecord with the task into which the new activity should be added.
2242 */
Louis Changcdec0802019-11-11 11:45:07 +08002243 private Task getReusableTask() {
Wale Ogunwale01d66562015-12-29 08:19:19 -08002244 // We may want to try to place the new activity in to an existing task. We always
2245 // do this if the target activity is singleTask or singleInstance; we will also do
2246 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
2247 // us to still place it in a new task: multi task, always doc mode, or being asked to
2248 // launch this as a new task behind the current one.
2249 boolean putIntoExistingTask = ((mLaunchFlags & FLAG_ACTIVITY_NEW_TASK) != 0 &&
2250 (mLaunchFlags & FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Bryce Lee7daee392017-10-12 13:46:18 -07002251 || isLaunchModeOneOf(LAUNCH_SINGLE_INSTANCE, LAUNCH_SINGLE_TASK);
Wale Ogunwale01d66562015-12-29 08:19:19 -08002252 // If bring to front is requested, and no result is requested and we have not been given
2253 // an explicit task to launch in to, and we can find a task that was started with this
2254 // same component, then instead of launching bring that one to the front.
2255 putIntoExistingTask &= mInTask == null && mStartActivity.resultTo == null;
2256 ActivityRecord intentActivity = null;
Jorim Jaggi2adba072016-03-03 13:43:39 +01002257 if (mOptions != null && mOptions.getLaunchTaskId() != -1) {
Louis Changcdec0802019-11-11 11:45:07 +08002258 Task launchTask = mRootActivityContainer.anyTaskForId(mOptions.getLaunchTaskId());
Louis Changf7dd7f22019-11-05 11:59:56 +08002259 if (launchTask != null) {
2260 return launchTask;
2261 }
Jorim Jaggi2adba072016-03-03 13:43:39 +01002262 } else if (putIntoExistingTask) {
Bryce Lee7daee392017-10-12 13:46:18 -07002263 if (LAUNCH_SINGLE_INSTANCE == mLaunchMode) {
Wale Ogunwale13dbfff2016-05-20 08:50:15 -07002264 // There can be one and only one instance of single instance activity in the
2265 // history, and it is always in its own unique task, so we do a special search.
Wale Ogunwaled32da472018-11-16 07:19:28 -08002266 intentActivity = mRootActivityContainer.findActivity(mIntent, mStartActivity.info,
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002267 mStartActivity.isActivityTypeHome());
Wale Ogunwale13dbfff2016-05-20 08:50:15 -07002268 } else if ((mLaunchFlags & FLAG_ACTIVITY_LAUNCH_ADJACENT) != 0) {
2269 // For the launch adjacent case we only want to put the activity in an existing
2270 // task if the activity already exists in the history.
Wale Ogunwaled32da472018-11-16 07:19:28 -08002271 intentActivity = mRootActivityContainer.findActivity(mIntent, mStartActivity.info,
Bryce Lee7daee392017-10-12 13:46:18 -07002272 !(LAUNCH_SINGLE_TASK == mLaunchMode));
Wale Ogunwale13dbfff2016-05-20 08:50:15 -07002273 } else {
2274 // Otherwise find the best task to put the activity in.
Wale Ogunwaled32da472018-11-16 07:19:28 -08002275 intentActivity =
2276 mRootActivityContainer.findTask(mStartActivity, mPreferredDisplayId);
Wale Ogunwale13dbfff2016-05-20 08:50:15 -07002277 }
Wale Ogunwale01d66562015-12-29 08:19:19 -08002278 }
Louis Changbd48dca2018-08-29 17:44:34 +08002279
Louis Chang54506cb2018-11-23 11:03:41 +08002280 if (intentActivity != null
2281 && (mStartActivity.isActivityTypeHome() || intentActivity.isActivityTypeHome())
Louis Changbd48dca2018-08-29 17:44:34 +08002282 && intentActivity.getDisplayId() != mPreferredDisplayId) {
2283 // Do not reuse home activity on other displays.
2284 intentActivity = null;
2285 }
2286
Louis Changcdec0802019-11-11 11:45:07 +08002287 return intentActivity != null ? intentActivity.getTask() : null;
Wale Ogunwale01d66562015-12-29 08:19:19 -08002288 }
2289
Andrii Kulianfab9cd82017-03-21 19:37:09 -07002290 /**
2291 * Figure out which task and activity to bring to front when we have found an existing matching
2292 * activity record in history. May also clear the task if needed.
2293 * @param intentActivity Existing matching activity.
2294 * @return {@link ActivityRecord} brought to front.
2295 */
Louis Changbde91e92019-08-16 17:19:47 +08002296 private void setTargetStackIfNeeded(ActivityRecord intentActivity) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002297 mTargetStack = intentActivity.getActivityStack();
Wale Ogunwale01d66562015-12-29 08:19:19 -08002298 mTargetStack.mLastPausedActivity = null;
2299 // If the target task is not in the front, then we need to bring it to the front...
2300 // except... well, with SINGLE_TASK_LAUNCH it's not entirely clear. We'd like to have
2301 // the same behavior as if a new instance was being started, which means not bringing it
2302 // to the front if the caller is not itself in the front.
Riddle Hsub70b36d2018-09-11 21:20:02 +08002303 final boolean differentTopTask;
2304 if (mPreferredDisplayId == mTargetStack.mDisplayId) {
2305 final ActivityStack focusStack = mTargetStack.getDisplay().getFocusedStack();
2306 final ActivityRecord curTop = (focusStack == null)
2307 ? null : focusStack.topRunningNonDelayedActivityLocked(mNotTop);
Louis Changcdec0802019-11-11 11:45:07 +08002308 final Task topTask = curTop != null ? curTop.getTask() : null;
2309 differentTopTask = topTask != intentActivity.getTask()
Riddle Hsu273e9992019-04-29 22:40:59 +08002310 || (focusStack != null && topTask != focusStack.topTask());
Riddle Hsub70b36d2018-09-11 21:20:02 +08002311 } else {
2312 // The existing task should always be different from those in other displays.
2313 differentTopTask = true;
2314 }
Wale Ogunwale01d66562015-12-29 08:19:19 -08002315
Riddle Hsub70b36d2018-09-11 21:20:02 +08002316 if (differentTopTask && !mAvoidMoveToFront) {
Wale Ogunwale01d66562015-12-29 08:19:19 -08002317 mStartActivity.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Wale Ogunwale21e06482019-11-18 05:14:15 -08002318 if (mSourceRecord == null || (mSourceStack.getTopNonFinishingActivity() != null &&
2319 mSourceStack.getTopNonFinishingActivity().getTask()
Louis Changcdec0802019-11-11 11:45:07 +08002320 == mSourceRecord.getTask())) {
Wale Ogunwale01d66562015-12-29 08:19:19 -08002321 // We really do want to push this one into the user's face, right now.
2322 if (mLaunchTaskBehind && mSourceRecord != null) {
Louis Changcdec0802019-11-11 11:45:07 +08002323 intentActivity.setTaskToAffiliateWith(mSourceRecord.getTask());
Wale Ogunwale01d66562015-12-29 08:19:19 -08002324 }
Chong Zhangdea4bd92016-03-15 12:50:03 -07002325
Louis Changcdec0802019-11-11 11:45:07 +08002326 final Task intentTask = intentActivity.getTask();
Louis Changf3070c52019-10-09 15:57:30 +08002327 final ActivityStack launchStack =
2328 getLaunchStack(mStartActivity, mLaunchFlags, intentTask, mOptions);
Louis Changbde91e92019-08-16 17:19:47 +08002329 if (launchStack == null || launchStack == mTargetStack) {
2330 // We only want to move to the front, if we aren't going to launch on a
2331 // different stack. If we launch on a different stack, we will put the
2332 // task on top there.
2333 mTargetStack.moveTaskToFrontLocked(intentTask, mNoAnimation, mOptions,
2334 mStartActivity.appTimeTracker, "bringingFoundTaskToFront");
2335 mMovedToFront = true;
2336 } else if (launchStack.inSplitScreenWindowingMode()) {
2337 if ((mLaunchFlags & FLAG_ACTIVITY_LAUNCH_ADJACENT) != 0) {
2338 // If we want to launch adjacent and mTargetStack is not the computed
2339 // launch stack - move task to top of computed stack.
2340 intentTask.reparent(launchStack, ON_TOP,
Andrii Kulianfab9cd82017-03-21 19:37:09 -07002341 REPARENT_MOVE_STACK_TO_FRONT, ANIMATE, DEFER_RESUME,
Louis Changbde91e92019-08-16 17:19:47 +08002342 "launchToSide");
2343 } else {
2344 // TODO: This should be reevaluated in MW v2.
2345 // We choose to move task to front instead of launching it adjacent
2346 // when specific stack was requested explicitly and it appeared to be
2347 // adjacent stack, but FLAG_ACTIVITY_LAUNCH_ADJACENT was not set.
2348 mTargetStack.moveTaskToFrontLocked(intentTask,
2349 mNoAnimation, mOptions, mStartActivity.appTimeTracker,
2350 "bringToFrontInsteadOfAdjacentLaunch");
Chong Zhangdea4bd92016-03-15 12:50:03 -07002351 }
Louis Changbde91e92019-08-16 17:19:47 +08002352 mMovedToFront = launchStack != launchStack.getDisplay()
2353 .getTopStackInWindowingMode(launchStack.getWindowingMode());
2354 } else if (launchStack.mDisplayId != mTargetStack.mDisplayId) {
2355 // Target and computed stacks are on different displays and we've
2356 // found a matching task - move the existing instance to that display and
2357 // move it to front.
Louis Changcdec0802019-11-11 11:45:07 +08002358 intentActivity.getTask().reparent(launchStack, ON_TOP,
Louis Changbde91e92019-08-16 17:19:47 +08002359 REPARENT_MOVE_STACK_TO_FRONT, ANIMATE, DEFER_RESUME,
2360 "reparentToDisplay");
2361 mMovedToFront = true;
2362 } else if (launchStack.isActivityTypeHome()
2363 && !mTargetStack.isActivityTypeHome()) {
2364 // It is possible for the home activity to be in another stack initially.
2365 // For example, the activity may have been initially started with an intent
2366 // which placed it in the fullscreen stack. To ensure the proper handling of
2367 // the activity based on home stack assumptions, we must move it over.
Louis Changcdec0802019-11-11 11:45:07 +08002368 intentActivity.getTask().reparent(launchStack, ON_TOP,
Louis Changbde91e92019-08-16 17:19:47 +08002369 REPARENT_MOVE_STACK_TO_FRONT, ANIMATE, DEFER_RESUME,
2370 "reparentingHome");
2371 mMovedToFront = true;
Louis Changf3070c52019-10-09 15:57:30 +08002372 }
2373
Alan Stokes80912002019-10-29 14:53:23 +00002374 if (launchStack != null && launchStack.topTask() == null) {
Louis Changfe0dfcb2019-09-02 15:59:38 +08002375 // The task does not need to be reparented to the launch stack. Remove the
2376 // launch stack if there is no activity in it.
Louis Changf3070c52019-10-09 15:57:30 +08002377 Slog.w(TAG, "Removing an empty stack: " + launchStack);
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -07002378 launchStack.removeIfPossible();
Wale Ogunwale01d66562015-12-29 08:19:19 -08002379 }
Louis Changfe0dfcb2019-09-02 15:59:38 +08002380
Louis Changbde91e92019-08-16 17:19:47 +08002381 mOptions = null;
Wale Ogunwale01d66562015-12-29 08:19:19 -08002382 }
2383 }
Andrii Kulianb850ea52017-12-12 23:49:10 -08002384 // Need to update mTargetStack because if task was moved out of it, the original stack may
2385 // be destroyed.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002386 mTargetStack = intentActivity.getActivityStack();
Louis Changcdec0802019-11-11 11:45:07 +08002387 mSupervisor.handleNonResizableTaskIfNeeded(intentActivity.getTask(),
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002388 WINDOWING_MODE_UNDEFINED, DEFAULT_DISPLAY, mTargetStack);
Wale Ogunwale01d66562015-12-29 08:19:19 -08002389 }
2390
2391 private void resumeTargetStackIfNeeded() {
2392 if (mDoResume) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08002393 mRootActivityContainer.resumeFocusedStacksTopActivities(mTargetStack, null, mOptions);
Wale Ogunwale01d66562015-12-29 08:19:19 -08002394 } else {
2395 ActivityOptions.abort(mOptions);
2396 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002397 mRootActivityContainer.updateUserStack(mStartActivity.mUserId, mTargetStack);
Wale Ogunwale01d66562015-12-29 08:19:19 -08002398 }
2399
Louis Changcdec0802019-11-11 11:45:07 +08002400 private void setNewTask(Task taskToAffiliate) {
Louis Changbde91e92019-08-16 17:19:47 +08002401 final boolean toTop = !mLaunchTaskBehind && !mAvoidMoveToFront;
Louis Changcdec0802019-11-11 11:45:07 +08002402 final Task task = mTargetStack.createTask(
Louis Changbde91e92019-08-16 17:19:47 +08002403 mSupervisor.getNextTaskIdForUserLocked(mStartActivity.mUserId),
2404 mNewTaskInfo != null ? mNewTaskInfo : mStartActivity.info,
2405 mNewTaskIntent != null ? mNewTaskIntent : mIntent, mVoiceSession,
2406 mVoiceInteractor, toTop, mStartActivity, mSourceRecord, mOptions);
2407 addOrReparentStartingActivity(task, "setTaskFromReuseOrCreateNewTask - mReuseTask");
Louis Changcdec0802019-11-11 11:45:07 +08002408 updateBounds(mStartActivity.getTask(), mLaunchParams.mBounds);
Louis Change8902452019-06-10 10:49:28 +08002409
Louis Changbde91e92019-08-16 17:19:47 +08002410 if (DEBUG_TASKS) {
2411 Slog.v(TAG_TASKS, "Starting new activity " + mStartActivity
Louis Changcdec0802019-11-11 11:45:07 +08002412 + " in new task " + mStartActivity.getTask());
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08002413 }
2414
2415 if (taskToAffiliate != null) {
2416 mStartActivity.setTaskToAffiliateWith(taskToAffiliate);
Wale Ogunwale01d66562015-12-29 08:19:19 -08002417 }
2418 }
2419
Bryce Lee325e09682017-10-05 17:20:25 -07002420 private void deliverNewIntent(ActivityRecord activity) {
2421 if (mIntentDelivered) {
2422 return;
2423 }
2424
Jeff Changd136e772019-11-05 20:33:52 +08002425 activity.logStartActivity(EventLogTags.WM_NEW_INTENT, activity.getTask());
Wale Ogunwale586a8ee2019-06-04 13:44:14 +00002426 activity.deliverNewIntentLocked(mCallingUid, mStartActivity.intent,
Bryce Lee325e09682017-10-05 17:20:25 -07002427 mStartActivity.launchedFromPackage);
2428 mIntentDelivered = true;
2429 }
2430
Bryce Leed3624e12017-11-30 08:51:45 -08002431 @VisibleForTesting
Louis Changcdec0802019-11-11 11:45:07 +08002432 void updateBounds(Task task, Rect bounds) {
Bryce Leedacefc42017-10-10 12:56:02 -07002433 if (bounds.isEmpty()) {
Bryce Lee4e4a3ec2017-09-27 08:25:03 -07002434 return;
2435 }
2436
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002437 final ActivityStack stack = task.getStack();
Evan Roskydbe2ce52019-07-18 11:13:17 -07002438 if (stack != null && stack.inPinnedWindowingMode()) {
2439 mService.animateResizePinnedStack(stack.mStackId, bounds, -1);
Bryce Lee4e4a3ec2017-09-27 08:25:03 -07002440 } else {
Evan Roskya4cc3a92019-06-28 13:25:01 -07002441 task.setBounds(bounds);
Bryce Lee4e4a3ec2017-09-27 08:25:03 -07002442 }
2443 }
2444
Louis Changcdec0802019-11-11 11:45:07 +08002445 private void addOrReparentStartingActivity(Task parent, String reason) {
2446 if (mStartActivity.getTask() == null || mStartActivity.getTask() == parent) {
Wale Ogunwalec17418e2019-10-13 23:00:40 +02002447 parent.addChild(mStartActivity);
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08002448 } else {
Wale Ogunwale1a06f152019-10-11 11:26:30 +02002449 mStartActivity.reparent(parent, parent.getChildCount() /* top */, reason);
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08002450 }
Wale Ogunwale01d66562015-12-29 08:19:19 -08002451 }
2452
2453 private int adjustLaunchFlagsToDocumentMode(ActivityRecord r, boolean launchSingleInstance,
2454 boolean launchSingleTask, int launchFlags) {
2455 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
2456 (launchSingleInstance || launchSingleTask)) {
2457 // We have a conflict between the Intent and the Activity manifest, manifest wins.
2458 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
2459 "\"singleInstance\" or \"singleTask\"");
2460 launchFlags &=
2461 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | FLAG_ACTIVITY_MULTIPLE_TASK);
2462 } else {
2463 switch (r.info.documentLaunchMode) {
2464 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
2465 break;
2466 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
2467 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
2468 break;
2469 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
2470 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
2471 break;
2472 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
2473 launchFlags &= ~FLAG_ACTIVITY_MULTIPLE_TASK;
2474 break;
2475 }
2476 }
2477 return launchFlags;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08002478 }
2479
Bryce Leedacefc42017-10-10 12:56:02 -07002480 private ActivityStack computeStackFocus(ActivityRecord r, boolean newTask, int launchFlags,
2481 ActivityOptions aOptions) {
Louis Changcdec0802019-11-11 11:45:07 +08002482 final Task task = r.getTask();
Jorim Jaggi4ad98562016-04-19 20:30:47 -07002483 ActivityStack stack = getLaunchStack(r, launchFlags, task, aOptions);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08002484 if (stack != null) {
2485 return stack;
2486 }
2487
Andrii Kulian02b7a832016-10-06 23:11:56 -07002488 final ActivityStack currentStack = task != null ? task.getStack() : null;
Wale Ogunwaled32da472018-11-16 07:19:28 -08002489 final ActivityStack focusedStack = mRootActivityContainer.getTopDisplayFocusedStack();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002490 if (currentStack != null) {
Andrii Kulian52d255c2018-07-13 11:32:19 -07002491 if (focusedStack != currentStack) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08002492 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
2493 "computeStackFocus: Setting " + "focused stack to r=" + r
2494 + " task=" + task);
2495 } else {
2496 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Andrii Kulian52d255c2018-07-13 11:32:19 -07002497 "computeStackFocus: Focused stack already=" + focusedStack);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08002498 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002499 return currentStack;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08002500 }
2501
Andrii Kulianfb1bf692017-01-17 11:17:34 -08002502 if (canLaunchIntoFocusedStack(r, newTask)) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08002503 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Andrii Kulian52d255c2018-07-13 11:32:19 -07002504 "computeStackFocus: Have a focused stack=" + focusedStack);
2505 return focusedStack;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08002506 }
2507
David Stevense5a7b642017-05-22 13:18:23 -07002508 if (mPreferredDisplayId != DEFAULT_DISPLAY) {
Andrii Kuliana8fe3df2017-06-16 15:29:26 -07002509 // Try to put the activity in a stack on a secondary display.
Wale Ogunwaled32da472018-11-16 07:19:28 -08002510 stack = mRootActivityContainer.getValidLaunchStackOnDisplay(
2511 mPreferredDisplayId, r, aOptions, mLaunchParams);
Andrii Kuliana8fe3df2017-06-16 15:29:26 -07002512 if (stack == null) {
2513 // If source display is not suitable - look for topmost valid stack in the system.
2514 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
David Stevense5a7b642017-05-22 13:18:23 -07002515 "computeStackFocus: Can't launch on mPreferredDisplayId="
2516 + mPreferredDisplayId + ", looking on all displays.");
Wale Ogunwaled32da472018-11-16 07:19:28 -08002517 stack = mRootActivityContainer.getNextValidLaunchStack(r, mPreferredDisplayId);
Andrii Kuliana8fe3df2017-06-16 15:29:26 -07002518 }
2519 }
2520 if (stack == null) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08002521 stack = mRootActivityContainer.getLaunchStack(r, aOptions, task, ON_TOP);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08002522 }
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08002523 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS, "computeStackFocus: New stack r="
2524 + r + " stackId=" + stack.mStackId);
2525 return stack;
2526 }
2527
Andrii Kulianfb1bf692017-01-17 11:17:34 -08002528 /** Check if provided activity record can launch in currently focused stack. */
Wale Ogunwale68278562017-09-23 17:13:55 -07002529 // TODO: This method can probably be consolidated into getLaunchStack() below.
Andrii Kulianfb1bf692017-01-17 11:17:34 -08002530 private boolean canLaunchIntoFocusedStack(ActivityRecord r, boolean newTask) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08002531 final ActivityStack focusedStack = mRootActivityContainer.getTopDisplayFocusedStack();
Andrii Kulianfb1bf692017-01-17 11:17:34 -08002532 final boolean canUseFocusedStack;
Wale Ogunwale68278562017-09-23 17:13:55 -07002533 if (focusedStack.isActivityTypeAssistant()) {
2534 canUseFocusedStack = r.isActivityTypeAssistant();
2535 } else {
2536 switch (focusedStack.getWindowingMode()) {
2537 case WINDOWING_MODE_FULLSCREEN:
2538 // The fullscreen stack can contain any task regardless of if the task is
2539 // resizeable or not. So, we let the task go in the fullscreen task if it is the
2540 // focus stack.
2541 canUseFocusedStack = true;
2542 break;
2543 case WINDOWING_MODE_SPLIT_SCREEN_PRIMARY:
2544 case WINDOWING_MODE_SPLIT_SCREEN_SECONDARY:
2545 // Any activity which supports split screen can go in the docked stack.
2546 canUseFocusedStack = r.supportsSplitScreenWindowingMode();
2547 break;
2548 case WINDOWING_MODE_FREEFORM:
2549 // Any activity which supports freeform can go in the freeform stack.
2550 canUseFocusedStack = r.supportsFreeform();
2551 break;
2552 default:
2553 // Dynamic stacks behave similarly to the fullscreen stack and can contain any
2554 // resizeable task.
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002555 canUseFocusedStack = !focusedStack.isOnHomeDisplay()
Wale Ogunwale68278562017-09-23 17:13:55 -07002556 && r.canBeLaunchedOnDisplay(focusedStack.mDisplayId);
2557 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08002558 }
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07002559 return canUseFocusedStack && !newTask
Wale Ogunwale68278562017-09-23 17:13:55 -07002560 // Using the focus stack isn't important enough to override the preferred display.
David Stevense5a7b642017-05-22 13:18:23 -07002561 && (mPreferredDisplayId == focusedStack.mDisplayId);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08002562 }
2563
Louis Changcdec0802019-11-11 11:45:07 +08002564 private ActivityStack getLaunchStack(ActivityRecord r, int launchFlags, Task task,
Jorim Jaggi4ad98562016-04-19 20:30:47 -07002565 ActivityOptions aOptions) {
Bryce Leea19b5ad2017-06-07 16:54:11 -07002566 // We are reusing a task, keep the stack!
2567 if (mReuseTask != null) {
2568 return mReuseTask.getStack();
2569 }
Jorim Jaggib8c58762016-04-20 17:58:29 -07002570
Karthik Ravi Shankar99493db2017-03-08 18:30:19 -08002571 if (((launchFlags & FLAG_ACTIVITY_LAUNCH_ADJACENT) == 0)
David Stevense5a7b642017-05-22 13:18:23 -07002572 || mPreferredDisplayId != DEFAULT_DISPLAY) {
Louis Chang37317152019-05-09 09:53:58 +08002573 final boolean onTop =
2574 (aOptions == null || !aOptions.getAvoidMoveToFront()) && !mLaunchTaskBehind;
Wale Ogunwaled32da472018-11-16 07:19:28 -08002575 final ActivityStack stack =
lumarkf65e02d2019-09-14 19:25:21 +08002576 mRootActivityContainer.getLaunchStack(r, aOptions, task, onTop, mLaunchParams,
2577 mRequest.realCallingPid, mRequest.realCallingUid);
Garfield Tan20d9e2f2018-11-16 15:42:29 -08002578 return stack;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08002579 }
Andrii Kulian4ac2a582016-03-25 00:07:38 -07002580 // Otherwise handle adjacent launch.
Wale Ogunwale854809c2015-12-27 16:18:19 -08002581
Wale Ogunwaled32da472018-11-16 07:19:28 -08002582 final ActivityStack focusedStack = mRootActivityContainer.getTopDisplayFocusedStack();
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08002583 // The parent activity doesn't want to launch the activity on top of itself, but
2584 // instead tries to put it onto other side in side-by-side mode.
Andrii Kulian52d255c2018-07-13 11:32:19 -07002585 final ActivityStack parentStack = task != null ? task.getStack(): focusedStack;
Andrii Kulian4ac2a582016-03-25 00:07:38 -07002586
Andrii Kulian52d255c2018-07-13 11:32:19 -07002587 if (parentStack != focusedStack) {
Andrii Kulian4ac2a582016-03-25 00:07:38 -07002588 // If task's parent stack is not focused - use it during adjacent launch.
2589 return parentStack;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08002590 } else {
Andrii Kulian52d255c2018-07-13 11:32:19 -07002591 if (focusedStack != null && task == focusedStack.topTask()) {
Andrii Kulian4ac2a582016-03-25 00:07:38 -07002592 // If task is already on top of focused stack - use it. We don't want to move the
2593 // existing focused task to adjacent stack, just deliver new intent in this case.
Andrii Kulian52d255c2018-07-13 11:32:19 -07002594 return focusedStack;
Andrii Kulian4ac2a582016-03-25 00:07:38 -07002595 }
2596
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002597 if (parentStack != null && parentStack.inSplitScreenPrimaryWindowingMode()) {
Andrii Kulian4ac2a582016-03-25 00:07:38 -07002598 // If parent was in docked stack, the natural place to launch another activity
2599 // will be fullscreen, so it can appear alongside the docked window.
Wale Ogunwaled32da472018-11-16 07:19:28 -08002600 final int activityType =
2601 mRootActivityContainer.resolveActivityType(r, mOptions, task);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002602 return parentStack.getDisplay().getOrCreateStack(
2603 WINDOWING_MODE_SPLIT_SCREEN_SECONDARY, activityType, ON_TOP);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08002604 } else {
Andrii Kulian4ac2a582016-03-25 00:07:38 -07002605 // If the parent is not in the docked stack, we check if there is docked window
2606 // and if yes, we will launch into that stack. If not, we just put the new
2607 // activity into parent's stack, because we can't find a better place.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002608 final ActivityStack dockedStack =
Wale Ogunwaled32da472018-11-16 07:19:28 -08002609 mRootActivityContainer.getDefaultDisplay().getSplitScreenPrimaryStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002610 if (dockedStack != null && !dockedStack.shouldBeVisible(r)) {
Andrii Kulian4ac2a582016-03-25 00:07:38 -07002611 // There is a docked stack, but it isn't visible, so we can't launch into that.
Wale Ogunwaled32da472018-11-16 07:19:28 -08002612 return mRootActivityContainer.getLaunchStack(r, aOptions, task, ON_TOP);
Andrii Kulian4ac2a582016-03-25 00:07:38 -07002613 } else {
2614 return dockedStack;
2615 }
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08002616 }
2617 }
2618 }
2619
Bryce Lee7daee392017-10-12 13:46:18 -07002620 private boolean isLaunchModeOneOf(int mode1, int mode2) {
2621 return mode1 == mLaunchMode || mode2 == mLaunchMode;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08002622 }
2623
Daichi Hirono15a02992016-04-27 18:47:01 +09002624 static boolean isDocumentLaunchesIntoExisting(int flags) {
2625 return (flags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
2626 (flags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0;
2627 }
liulvpingcfa825f2016-09-26 20:00:15 +08002628
Bryce Lee4c9a5972017-12-01 22:14:24 -08002629 ActivityStarter setIntent(Intent intent) {
2630 mRequest.intent = intent;
2631 return this;
2632 }
2633
Bryce Lee32e09ef2018-03-19 15:29:49 -07002634 @VisibleForTesting
2635 Intent getIntent() {
2636 return mRequest.intent;
2637 }
2638
Bryce Lee4c9a5972017-12-01 22:14:24 -08002639 ActivityStarter setReason(String reason) {
2640 mRequest.reason = reason;
2641 return this;
2642 }
2643
2644 ActivityStarter setCaller(IApplicationThread caller) {
2645 mRequest.caller = caller;
2646 return this;
2647 }
2648
Bryce Lee4c9a5972017-12-01 22:14:24 -08002649 ActivityStarter setResolvedType(String type) {
2650 mRequest.resolvedType = type;
2651 return this;
2652 }
2653
2654 ActivityStarter setActivityInfo(ActivityInfo info) {
2655 mRequest.activityInfo = info;
2656 return this;
2657 }
2658
2659 ActivityStarter setResolveInfo(ResolveInfo info) {
2660 mRequest.resolveInfo = info;
2661 return this;
2662 }
2663
2664 ActivityStarter setVoiceSession(IVoiceInteractionSession voiceSession) {
2665 mRequest.voiceSession = voiceSession;
2666 return this;
2667 }
2668
2669 ActivityStarter setVoiceInteractor(IVoiceInteractor voiceInteractor) {
2670 mRequest.voiceInteractor = voiceInteractor;
2671 return this;
2672 }
2673
2674 ActivityStarter setResultTo(IBinder resultTo) {
2675 mRequest.resultTo = resultTo;
2676 return this;
2677 }
2678
2679 ActivityStarter setResultWho(String resultWho) {
2680 mRequest.resultWho = resultWho;
2681 return this;
2682 }
2683
2684 ActivityStarter setRequestCode(int requestCode) {
2685 mRequest.requestCode = requestCode;
2686 return this;
2687 }
2688
lumarkf65e02d2019-09-14 19:25:21 +08002689 /**
2690 * Sets the pid of the caller who originally started the activity.
2691 *
2692 * Normally, the pid/uid would be the calling pid from the binder call.
2693 * However, in case of a {@link PendingIntent}, the pid/uid pair of the caller is considered
2694 * the original entity that created the pending intent, in contrast to setRealCallingPid/Uid,
2695 * which represents the entity who invoked pending intent via {@link PendingIntent#send}.
2696 */
Bryce Lee4c9a5972017-12-01 22:14:24 -08002697 ActivityStarter setCallingPid(int pid) {
2698 mRequest.callingPid = pid;
2699 return this;
2700 }
2701
lumarkf65e02d2019-09-14 19:25:21 +08002702 /**
2703 * Sets the uid of the caller who originally started the activity.
2704 *
2705 * @see #setCallingPid
2706 */
Bryce Lee4c9a5972017-12-01 22:14:24 -08002707 ActivityStarter setCallingUid(int uid) {
2708 mRequest.callingUid = uid;
2709 return this;
2710 }
2711
2712 ActivityStarter setCallingPackage(String callingPackage) {
2713 mRequest.callingPackage = callingPackage;
2714 return this;
2715 }
2716
lumarkf65e02d2019-09-14 19:25:21 +08002717 /**
2718 * Sets the pid of the caller who requested to launch the activity.
2719 *
2720 * The pid/uid represents the caller who launches the activity in this request.
2721 * It will almost same as setCallingPid/Uid except when processing {@link PendingIntent}:
2722 * the pid/uid will be the caller who called {@link PendingIntent#send()}.
2723 *
2724 * @see #setCallingPid
2725 */
Bryce Lee4c9a5972017-12-01 22:14:24 -08002726 ActivityStarter setRealCallingPid(int pid) {
2727 mRequest.realCallingPid = pid;
2728 return this;
2729 }
2730
lumarkf65e02d2019-09-14 19:25:21 +08002731 /**
2732 * Sets the uid of the caller who requested to launch the activity.
2733 *
2734 * @see #setRealCallingPid
2735 */
Bryce Lee4c9a5972017-12-01 22:14:24 -08002736 ActivityStarter setRealCallingUid(int uid) {
2737 mRequest.realCallingUid = uid;
2738 return this;
2739 }
2740
2741 ActivityStarter setStartFlags(int startFlags) {
2742 mRequest.startFlags = startFlags;
2743 return this;
2744 }
2745
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01002746 ActivityStarter setActivityOptions(SafeActivityOptions options) {
Bryce Lee4c9a5972017-12-01 22:14:24 -08002747 mRequest.activityOptions = options;
2748 return this;
2749 }
2750
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01002751 ActivityStarter setActivityOptions(Bundle bOptions) {
2752 return setActivityOptions(SafeActivityOptions.fromBundle(bOptions));
2753 }
2754
Bryce Lee4c9a5972017-12-01 22:14:24 -08002755 ActivityStarter setIgnoreTargetSecurity(boolean ignoreTargetSecurity) {
2756 mRequest.ignoreTargetSecurity = ignoreTargetSecurity;
2757 return this;
2758 }
2759
Patrick Baumann31426b22018-05-21 13:46:40 -07002760 ActivityStarter setFilterCallingUid(int filterCallingUid) {
2761 mRequest.filterCallingUid = filterCallingUid;
2762 return this;
2763 }
2764
Bryce Lee4c9a5972017-12-01 22:14:24 -08002765 ActivityStarter setComponentSpecified(boolean componentSpecified) {
2766 mRequest.componentSpecified = componentSpecified;
2767 return this;
2768 }
2769
2770 ActivityStarter setOutActivity(ActivityRecord[] outActivity) {
2771 mRequest.outActivity = outActivity;
2772 return this;
2773 }
2774
Louis Changcdec0802019-11-11 11:45:07 +08002775 ActivityStarter setInTask(Task inTask) {
Bryce Lee4c9a5972017-12-01 22:14:24 -08002776 mRequest.inTask = inTask;
2777 return this;
2778 }
2779
2780 ActivityStarter setWaitResult(WaitResult result) {
2781 mRequest.waitResult = result;
2782 return this;
2783 }
2784
2785 ActivityStarter setProfilerInfo(ProfilerInfo info) {
2786 mRequest.profilerInfo = info;
2787 return this;
2788 }
2789
2790 ActivityStarter setGlobalConfiguration(Configuration config) {
2791 mRequest.globalConfig = config;
2792 return this;
2793 }
2794
Bryce Lee4c9a5972017-12-01 22:14:24 -08002795 ActivityStarter setUserId(int userId) {
2796 mRequest.userId = userId;
2797 return this;
2798 }
2799
Jorim Jaggi6fa41c32018-04-23 18:35:00 +02002800 ActivityStarter setAllowPendingRemoteAnimationRegistryLookup(boolean allowLookup) {
2801 mRequest.allowPendingRemoteAnimationRegistryLookup = allowLookup;
2802 return this;
2803 }
2804
Michal Karpinski201bc0c2018-07-20 15:32:00 +01002805 ActivityStarter setOriginatingPendingIntent(PendingIntentRecord originatingPendingIntent) {
2806 mRequest.originatingPendingIntent = originatingPendingIntent;
2807 return this;
2808 }
2809
Michal Karpinskiac116df2018-12-10 17:51:42 +00002810 ActivityStarter setAllowBackgroundActivityStart(boolean allowBackgroundActivityStart) {
2811 mRequest.allowBackgroundActivityStart = allowBackgroundActivityStart;
2812 return this;
2813 }
2814
Bryce Leed3624e12017-11-30 08:51:45 -08002815 void dump(PrintWriter pw, String prefix) {
Wale Ogunwale692dcd62017-06-20 13:38:14 -07002816 prefix = prefix + " ";
Dianne Hackborne676ec72017-07-25 10:55:08 -07002817 pw.print(prefix);
2818 pw.print("mCurrentUser=");
Wale Ogunwaled32da472018-11-16 07:19:28 -08002819 pw.println(mRootActivityContainer.mCurrentUser);
Dianne Hackborne676ec72017-07-25 10:55:08 -07002820 pw.print(prefix);
2821 pw.print("mLastStartReason=");
2822 pw.println(mLastStartReason);
2823 pw.print(prefix);
2824 pw.print("mLastStartActivityTimeMs=");
2825 pw.println(DateFormat.getDateTimeInstance().format(new Date(mLastStartActivityTimeMs)));
2826 pw.print(prefix);
2827 pw.print("mLastStartActivityResult=");
2828 pw.println(mLastStartActivityResult);
Louis Chang54fbb052019-10-16 17:10:17 +08002829 if (mLastStartActivityRecord != null) {
Dianne Hackborne676ec72017-07-25 10:55:08 -07002830 pw.print(prefix);
2831 pw.println("mLastStartActivityRecord:");
Louis Chang54fbb052019-10-16 17:10:17 +08002832 mLastStartActivityRecord.dump(pw, prefix + " ", true /* dumpAll */);
Wale Ogunwale692dcd62017-06-20 13:38:14 -07002833 }
Wale Ogunwale692dcd62017-06-20 13:38:14 -07002834 if (mStartActivity != null) {
Dianne Hackborne676ec72017-07-25 10:55:08 -07002835 pw.print(prefix);
2836 pw.println("mStartActivity:");
Garfield Tane8d84ab2019-10-11 09:49:40 -07002837 mStartActivity.dump(pw, prefix + " ", true /* dumpAll */);
Wale Ogunwale692dcd62017-06-20 13:38:14 -07002838 }
2839 if (mIntent != null) {
Dianne Hackborne676ec72017-07-25 10:55:08 -07002840 pw.print(prefix);
2841 pw.print("mIntent=");
2842 pw.println(mIntent);
Wale Ogunwale692dcd62017-06-20 13:38:14 -07002843 }
2844 if (mOptions != null) {
Dianne Hackborne676ec72017-07-25 10:55:08 -07002845 pw.print(prefix);
2846 pw.print("mOptions=");
2847 pw.println(mOptions);
Wale Ogunwale692dcd62017-06-20 13:38:14 -07002848 }
Dianne Hackborne676ec72017-07-25 10:55:08 -07002849 pw.print(prefix);
2850 pw.print("mLaunchSingleTop=");
Bryce Lee7daee392017-10-12 13:46:18 -07002851 pw.print(LAUNCH_SINGLE_TOP == mLaunchMode);
Dianne Hackborne676ec72017-07-25 10:55:08 -07002852 pw.print(" mLaunchSingleInstance=");
Bryce Lee7daee392017-10-12 13:46:18 -07002853 pw.print(LAUNCH_SINGLE_INSTANCE == mLaunchMode);
Dianne Hackborne676ec72017-07-25 10:55:08 -07002854 pw.print(" mLaunchSingleTask=");
Bryce Lee7daee392017-10-12 13:46:18 -07002855 pw.println(LAUNCH_SINGLE_TASK == mLaunchMode);
Dianne Hackborne676ec72017-07-25 10:55:08 -07002856 pw.print(prefix);
2857 pw.print("mLaunchFlags=0x");
2858 pw.print(Integer.toHexString(mLaunchFlags));
2859 pw.print(" mDoResume=");
2860 pw.print(mDoResume);
2861 pw.print(" mAddingToTask=");
2862 pw.println(mAddingToTask);
Wale Ogunwale692dcd62017-06-20 13:38:14 -07002863 }
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08002864}