blob: c7270f25792348f0e43350e570e482971af11ad9 [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 Ogunwale7d7973a2018-04-05 10:25:59 -070032import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -070033import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
34import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_SECONDARY;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070035import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -080036import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK;
37import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
Wale Ogunwale2a25a622016-01-30 11:27:21 -080038import static android.content.Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -080039import static android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
Wale Ogunwale01d66562015-12-29 08:19:19 -080040import static android.content.Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -080041import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
Wale Ogunwale01d66562015-12-29 08:19:19 -080042import static android.content.Intent.FLAG_ACTIVITY_NO_ANIMATION;
43import static android.content.Intent.FLAG_ACTIVITY_NO_USER_ACTION;
Wale Ogunwale01d66562015-12-29 08:19:19 -080044import static android.content.Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP;
45import static android.content.Intent.FLAG_ACTIVITY_REORDER_TO_FRONT;
46import static android.content.Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED;
47import static android.content.Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
48import static android.content.Intent.FLAG_ACTIVITY_SINGLE_TOP;
Louis Changb45ee7e2019-01-17 10:36:56 +080049import static android.content.Intent.FLAG_ACTIVITY_TASK_ON_HOME;
Wale Ogunwale01d66562015-12-29 08:19:19 -080050import static android.content.pm.ActivityInfo.DOCUMENT_LAUNCH_ALWAYS;
Wale Ogunwale2322bed2019-10-10 17:24:19 +020051import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Wale Ogunwale01d66562015-12-29 08:19:19 -080052import static android.content.pm.ActivityInfo.LAUNCH_SINGLE_INSTANCE;
53import static android.content.pm.ActivityInfo.LAUNCH_SINGLE_TASK;
54import static android.content.pm.ActivityInfo.LAUNCH_SINGLE_TOP;
Michal Karpinski7b97a022018-12-14 15:17:29 +000055import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Andrii Kulian79d67982019-08-19 11:56:16 -070056import static android.os.Process.INVALID_UID;
David Stevensc6b91c62017-02-08 14:23:58 -080057import static android.view.Display.DEFAULT_DISPLAY;
Andrii Kulian16802aa2016-11-02 12:21:33 -070058import static android.view.Display.INVALID_DISPLAY;
Riddle Hsub70b36d2018-09-11 21:20:02 +080059
Wale Ogunwale59507092018-10-29 09:00:30 -070060import static com.android.server.wm.ActivityStack.ActivityState.RESUMED;
61import static com.android.server.wm.ActivityStackSupervisor.DEFER_RESUME;
62import static com.android.server.wm.ActivityStackSupervisor.ON_TOP;
63import static com.android.server.wm.ActivityStackSupervisor.PRESERVE_WINDOWS;
64import static com.android.server.wm.ActivityStackSupervisor.TAG_TASKS;
65import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_CONFIGURATION;
Wale Ogunwale59507092018-10-29 09:00:30 -070066import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_PERMISSIONS_REVIEW;
67import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_RESULTS;
Wale Ogunwale59507092018-10-29 09:00:30 -070068import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_TASKS;
69import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_USER_LEAVING;
70import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_CONFIGURATION;
71import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_FOCUS;
72import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_RESULTS;
73import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_USER_LEAVING;
74import static com.android.server.wm.ActivityTaskManagerDebugConfig.TAG_ATM;
75import static com.android.server.wm.ActivityTaskManagerDebugConfig.TAG_WITH_CLASS_NAME;
76import static com.android.server.wm.ActivityTaskManagerService.ANIMATE;
Louis Chang6fb1e842018-12-03 16:07:50 +080077import static com.android.server.wm.LaunchParamsController.LaunchParamsModifier.PHASE_BOUNDS;
78import static com.android.server.wm.LaunchParamsController.LaunchParamsModifier.PHASE_DISPLAY;
Louis Changcdec0802019-11-11 11:45:07 +080079import static com.android.server.wm.Task.REPARENT_MOVE_STACK_TO_FRONT;
Winson Chung74666102017-02-22 17:49:24 -080080
Todd Kennedye9910222017-02-21 16:00:11 -080081import android.annotation.NonNull;
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +010082import android.annotation.Nullable;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -080083import android.app.ActivityManager;
84import android.app.ActivityOptions;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -080085import android.app.IApplicationThread;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -080086import android.app.PendingIntent;
87import android.app.ProfilerInfo;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -070088import android.app.WaitResult;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -080089import android.content.IIntentSender;
90import android.content.Intent;
91import android.content.IntentSender;
92import android.content.pm.ActivityInfo;
93import android.content.pm.ApplicationInfo;
Todd Kennedye9910222017-02-21 16:00:11 -080094import android.content.pm.AuxiliaryResolveInfo;
Kenny Guyb1b30262016-02-09 16:02:35 +000095import android.content.pm.PackageManager;
Winson81fef842019-08-28 12:19:08 -070096import android.content.pm.PackageManagerInternal;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -080097import android.content.pm.ResolveInfo;
Kenny Guyb1b30262016-02-09 16:02:35 +000098import android.content.pm.UserInfo;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -080099import android.content.res.Configuration;
100import android.graphics.Rect;
101import android.os.Binder;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800102import android.os.Bundle;
103import android.os.IBinder;
Michal Karpinski8596ded2018-11-14 14:43:48 +0000104import android.os.Process;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800105import android.os.RemoteException;
Michal Karpinski201bc0c2018-07-20 15:32:00 +0100106import android.os.Trace;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800107import android.os.UserHandle;
Kenny Guyb1b30262016-02-09 16:02:35 +0000108import android.os.UserManager;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800109import android.service.voice.IVoiceInteractionSession;
Wale Ogunwale692dcd62017-06-20 13:38:14 -0700110import android.text.TextUtils;
Michal Karpinskib7daac22019-03-25 10:12:41 +0000111import android.util.ArraySet;
Bryce Leedaa91e42017-12-06 14:13:01 -0800112import android.util.Pools.SynchronizedPool;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800113import android.util.Slog;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800114
Bryce Leed3624e12017-11-30 08:51:45 -0800115import com.android.internal.annotations.VisibleForTesting;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800116import com.android.internal.app.HeavyWeightSwitcherActivity;
117import com.android.internal.app.IVoiceInteractor;
Wale Ogunwale59507092018-10-29 09:00:30 -0700118import com.android.server.am.PendingIntentRecord;
Louis Changdd3592a2018-11-05 11:04:14 +0800119import com.android.server.pm.InstantAppResolver;
Riddle Hsufd66d4d2019-11-14 10:35:55 +0800120import com.android.server.wm.ActivityMetricsLogger.LaunchingState;
Wale Ogunwale59507092018-10-29 09:00:30 -0700121import com.android.server.wm.ActivityStackSupervisor.PendingActivityLaunch;
122import com.android.server.wm.LaunchParamsController.LaunchParams;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800123
Wale Ogunwale692dcd62017-06-20 13:38:14 -0700124import java.io.PrintWriter;
125import java.text.DateFormat;
Wale Ogunwale692dcd62017-06-20 13:38:14 -0700126import java.util.Date;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800127
128/**
Bryce Leed3624e12017-11-30 08:51:45 -0800129 * Controller for interpreting how and then launching an activity.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800130 *
131 * This class collects all the logic for determining how an intent and flags should be turned into
132 * an activity and associated task and stack.
133 */
Wale Ogunwale01d66562015-12-29 08:19:19 -0800134class ActivityStarter {
Wale Ogunwale98875612018-10-12 07:53:02 -0700135 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStarter" : TAG_ATM;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800136 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
137 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
138 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
139 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
Bryce Lee7daee392017-10-12 13:46:18 -0700140 private static final int INVALID_LAUNCH_MODE = -1;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800141
Wale Ogunwalec9e57de2018-05-08 14:28:07 -0700142 private final ActivityTaskManagerService mService;
Louis Chang149d5c82019-12-30 09:47:39 +0800143 private final RootWindowContainer mRootWindowContainer;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800144 private final ActivityStackSupervisor mSupervisor;
Benjamin Franz563707b2017-06-29 15:06:13 +0100145 private final ActivityStartInterceptor mInterceptor;
Bryce Leed3624e12017-11-30 08:51:45 -0800146 private final ActivityStartController mController;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800147
Wale Ogunwale01d66562015-12-29 08:19:19 -0800148 // Share state variable among methods when starting an activity.
Louis Chang07b13002019-11-27 22:08:37 +0800149 @VisibleForTesting
150 ActivityRecord mStartActivity;
Wale Ogunwale01d66562015-12-29 08:19:19 -0800151 private Intent mIntent;
152 private int mCallingUid;
153 private ActivityOptions mOptions;
154
Ricky Waib147fa12019-04-25 16:08:30 +0100155 // If it is true, background activity can only be started in an existing task that contains
Alan Stokes07389b62019-05-20 15:22:54 +0100156 // an activity with same uid, or if activity starts are enabled in developer options.
Ricky Waib147fa12019-04-25 16:08:30 +0100157 private boolean mRestrictedBgActivity;
158
Bryce Lee7daee392017-10-12 13:46:18 -0700159 private int mLaunchMode;
Wale Ogunwale01d66562015-12-29 08:19:19 -0800160 private boolean mLaunchTaskBehind;
161 private int mLaunchFlags;
162
Bryce Leeec55eb02017-12-05 20:51:27 -0800163 private LaunchParams mLaunchParams = new LaunchParams();
Wale Ogunwale01d66562015-12-29 08:19:19 -0800164
165 private ActivityRecord mNotTop;
166 private boolean mDoResume;
167 private int mStartFlags;
168 private ActivityRecord mSourceRecord;
Bryce Lee7daee392017-10-12 13:46:18 -0700169
David Stevense5a7b642017-05-22 13:18:23 -0700170 // The display to launch the activity onto, barring any strong reason to do otherwise.
171 private int mPreferredDisplayId;
Wale Ogunwale01d66562015-12-29 08:19:19 -0800172
Louis Changcdec0802019-11-11 11:45:07 +0800173 private Task mInTask;
Louis Chang07b13002019-11-27 22:08:37 +0800174 @VisibleForTesting
175 boolean mAddingToTask;
Louis Changcdec0802019-11-11 11:45:07 +0800176 private Task mReuseTask;
Wale Ogunwale01d66562015-12-29 08:19:19 -0800177
178 private ActivityInfo mNewTaskInfo;
179 private Intent mNewTaskIntent;
180 private ActivityStack mSourceStack;
181 private ActivityStack mTargetStack;
Wale Ogunwale01d66562015-12-29 08:19:19 -0800182 private boolean mMovedToFront;
183 private boolean mNoAnimation;
184 private boolean mKeepCurTransition;
Jorim Jaggic875ae72016-04-26 22:41:06 -0700185 private boolean mAvoidMoveToFront;
Winson Chunge219ae12019-07-18 13:43:23 -0700186 private boolean mFrozeTaskList;
Wale Ogunwale01d66562015-12-29 08:19:19 -0800187
Bryce Lee325e09682017-10-05 17:20:25 -0700188 // We must track when we deliver the new intent since multiple code paths invoke
189 // {@link #deliverNewIntent}. This is due to early returns in the code path. This flag is used
190 // inside {@link #deliverNewIntent} to suppress duplicate requests and ensure the intent is
191 // delivered at most once.
192 private boolean mIntentDelivered;
193
Wale Ogunwale01d66562015-12-29 08:19:19 -0800194 private IVoiceInteractionSession mVoiceSession;
195 private IVoiceInteractor mVoiceInteractor;
196
Wale Ogunwale692dcd62017-06-20 13:38:14 -0700197 // Last activity record we attempted to start
Louis Chang54fbb052019-10-16 17:10:17 +0800198 private ActivityRecord mLastStartActivityRecord;
Wale Ogunwale692dcd62017-06-20 13:38:14 -0700199 // The result of the last activity we attempted to start.
200 private int mLastStartActivityResult;
201 // Time in milli seconds we attempted to start the last activity.
202 private long mLastStartActivityTimeMs;
203 // The reason we were trying to start the last activity
204 private String mLastStartReason;
Wale Ogunwale59bcba62017-06-16 12:42:51 -0700205
Bryce Lee4c9a5972017-12-01 22:14:24 -0800206 /*
207 * Request details provided through setter methods. Should be reset after {@link #execute()}
208 * to avoid unnecessarily retaining parameters. Note that the request is ignored when
209 * {@link #startResolvedActivity} is invoked directly.
210 */
Louis Chang54fbb052019-10-16 17:10:17 +0800211 @VisibleForTesting
212 Request mRequest = new Request();
Bryce Lee4c9a5972017-12-01 22:14:24 -0800213
Bryce Leed3624e12017-11-30 08:51:45 -0800214 /**
215 * An interface that to provide {@link ActivityStarter} instances to the controller. This is
216 * used by tests to inject their own starter implementations for verification purposes.
217 */
218 @VisibleForTesting
219 interface Factory {
220 /**
Bryce Lee4c9a5972017-12-01 22:14:24 -0800221 * Sets the {@link ActivityStartController} to be passed to {@link ActivityStarter}.
222 */
223 void setController(ActivityStartController controller);
224
225 /**
Bryce Leed3624e12017-11-30 08:51:45 -0800226 * Generates an {@link ActivityStarter} that is ready to handle a new start request.
227 * @param controller The {@link ActivityStartController} which the starter who will own
228 * this instance.
229 * @return an {@link ActivityStarter}
230 */
Bryce Leedaa91e42017-12-06 14:13:01 -0800231 ActivityStarter obtain();
232
233 /**
234 * Recycles a starter for reuse.
235 */
236 void recycle(ActivityStarter starter);
Wale Ogunwale01d66562015-12-29 08:19:19 -0800237 }
238
Bryce Leed3624e12017-11-30 08:51:45 -0800239 /**
240 * Default implementation of {@link StarterFactory}.
241 */
242 static class DefaultFactory implements Factory {
Bryce Leedaa91e42017-12-06 14:13:01 -0800243 /**
244 * The maximum count of starters that should be active at one time:
245 * 1. last ran starter (for logging and post activity processing)
246 * 2. current running starter
247 * 3. starter from re-entry in (2)
248 */
249 private final int MAX_STARTER_COUNT = 3;
250
Bryce Lee4c9a5972017-12-01 22:14:24 -0800251 private ActivityStartController mController;
Wale Ogunwalec9e57de2018-05-08 14:28:07 -0700252 private ActivityTaskManagerService mService;
Bryce Lee4c9a5972017-12-01 22:14:24 -0800253 private ActivityStackSupervisor mSupervisor;
254 private ActivityStartInterceptor mInterceptor;
255
Bryce Leedaa91e42017-12-06 14:13:01 -0800256 private SynchronizedPool<ActivityStarter> mStarterPool =
257 new SynchronizedPool<>(MAX_STARTER_COUNT);
258
Wale Ogunwalec9e57de2018-05-08 14:28:07 -0700259 DefaultFactory(ActivityTaskManagerService service,
Bryce Lee4c9a5972017-12-01 22:14:24 -0800260 ActivityStackSupervisor supervisor, ActivityStartInterceptor interceptor) {
261 mService = service;
262 mSupervisor = supervisor;
263 mInterceptor = interceptor;
Bryce Leed3624e12017-11-30 08:51:45 -0800264 }
Bryce Lee4c9a5972017-12-01 22:14:24 -0800265
266 @Override
267 public void setController(ActivityStartController controller) {
268 mController = controller;
269 }
270
271 @Override
Bryce Leedaa91e42017-12-06 14:13:01 -0800272 public ActivityStarter obtain() {
273 ActivityStarter starter = mStarterPool.acquire();
274
275 if (starter == null) {
276 starter = new ActivityStarter(mController, mService, mSupervisor, mInterceptor);
277 }
278
279 return starter;
280 }
281
282 @Override
283 public void recycle(ActivityStarter starter) {
284 starter.reset(true /* clearRequest*/);
285 mStarterPool.release(starter);
Bryce Lee4c9a5972017-12-01 22:14:24 -0800286 }
287 }
288
289 /**
290 * Container for capturing initial start request details. This information is NOT reset until
291 * the {@link ActivityStarter} is recycled, allowing for multiple invocations with the same
292 * parameters.
293 *
294 * TODO(b/64750076): Investigate consolidating member variables of {@link ActivityStarter} with
295 * the request object. Note that some member variables are referenced in
296 * {@link #dump(PrintWriter, String)} and therefore cannot be cleared immediately after
297 * execution.
298 */
Louis Chang54fbb052019-10-16 17:10:17 +0800299 @VisibleForTesting
300 static class Request {
Bryce Lee4c9a5972017-12-01 22:14:24 -0800301 private static final int DEFAULT_CALLING_UID = -1;
302 private static final int DEFAULT_CALLING_PID = 0;
Michal Karpinski84d9ebd2019-01-17 18:28:59 +0000303 static final int DEFAULT_REAL_CALLING_UID = -1;
304 static final int DEFAULT_REAL_CALLING_PID = 0;
Bryce Lee4c9a5972017-12-01 22:14:24 -0800305
306 IApplicationThread caller;
307 Intent intent;
Louis Chang54fbb052019-10-16 17:10:17 +0800308 // A copy of the original requested intent, in case for ephemeral app launch.
Bryce Lee4c9a5972017-12-01 22:14:24 -0800309 Intent ephemeralIntent;
310 String resolvedType;
311 ActivityInfo activityInfo;
312 ResolveInfo resolveInfo;
313 IVoiceInteractionSession voiceSession;
314 IVoiceInteractor voiceInteractor;
315 IBinder resultTo;
316 String resultWho;
317 int requestCode;
Michal Karpinski84d9ebd2019-01-17 18:28:59 +0000318 int callingPid = DEFAULT_CALLING_PID;
319 int callingUid = DEFAULT_CALLING_UID;
Bryce Lee4c9a5972017-12-01 22:14:24 -0800320 String callingPackage;
Michal Karpinski84d9ebd2019-01-17 18:28:59 +0000321 int realCallingPid = DEFAULT_REAL_CALLING_PID;
322 int realCallingUid = DEFAULT_REAL_CALLING_UID;
Bryce Lee4c9a5972017-12-01 22:14:24 -0800323 int startFlags;
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +0100324 SafeActivityOptions activityOptions;
Bryce Lee4c9a5972017-12-01 22:14:24 -0800325 boolean ignoreTargetSecurity;
326 boolean componentSpecified;
Winson Chunge2d72172018-01-25 17:46:20 +0000327 boolean avoidMoveToFront;
Bryce Lee4c9a5972017-12-01 22:14:24 -0800328 ActivityRecord[] outActivity;
Louis Changcdec0802019-11-11 11:45:07 +0800329 Task inTask;
Bryce Lee4c9a5972017-12-01 22:14:24 -0800330 String reason;
331 ProfilerInfo profilerInfo;
332 Configuration globalConfig;
Bryce Lee4c9a5972017-12-01 22:14:24 -0800333 int userId;
334 WaitResult waitResult;
Patrick Baumann31426b22018-05-21 13:46:40 -0700335 int filterCallingUid;
Michal Karpinski201bc0c2018-07-20 15:32:00 +0100336 PendingIntentRecord originatingPendingIntent;
Michal Karpinskiac116df2018-12-10 17:51:42 +0000337 boolean allowBackgroundActivityStart;
Bryce Lee4c9a5972017-12-01 22:14:24 -0800338
339 /**
Jorim Jaggi6fa41c32018-04-23 18:35:00 +0200340 * If set to {@code true}, allows this activity start to look into
341 * {@link PendingRemoteAnimationRegistry}
342 */
343 boolean allowPendingRemoteAnimationRegistryLookup;
344
345 /**
Bryce Leea3cd8e02018-01-09 15:44:24 -0800346 * Ensure constructed request matches reset instance.
347 */
348 Request() {
349 reset();
350 }
351
352 /**
Bryce Leedaa91e42017-12-06 14:13:01 -0800353 * Sets values back to the initial state, clearing any held references.
354 */
355 void reset() {
356 caller = null;
357 intent = null;
358 ephemeralIntent = null;
359 resolvedType = null;
360 activityInfo = null;
361 resolveInfo = null;
362 voiceSession = null;
363 voiceInteractor = null;
364 resultTo = null;
365 resultWho = null;
366 requestCode = 0;
Bryce Leea3cd8e02018-01-09 15:44:24 -0800367 callingPid = DEFAULT_CALLING_PID;
368 callingUid = DEFAULT_CALLING_UID;
Bryce Leedaa91e42017-12-06 14:13:01 -0800369 callingPackage = null;
Michal Karpinski84d9ebd2019-01-17 18:28:59 +0000370 realCallingPid = DEFAULT_REAL_CALLING_PID;
371 realCallingUid = DEFAULT_REAL_CALLING_UID;
Bryce Leedaa91e42017-12-06 14:13:01 -0800372 startFlags = 0;
373 activityOptions = null;
374 ignoreTargetSecurity = false;
375 componentSpecified = false;
376 outActivity = null;
377 inTask = null;
378 reason = null;
379 profilerInfo = null;
380 globalConfig = null;
Bryce Leedaa91e42017-12-06 14:13:01 -0800381 userId = 0;
382 waitResult = null;
Winson Chunge2d72172018-01-25 17:46:20 +0000383 avoidMoveToFront = false;
Jorim Jaggi6fa41c32018-04-23 18:35:00 +0200384 allowPendingRemoteAnimationRegistryLookup = true;
Patrick Baumann31426b22018-05-21 13:46:40 -0700385 filterCallingUid = UserHandle.USER_NULL;
Michal Karpinski201bc0c2018-07-20 15:32:00 +0100386 originatingPendingIntent = null;
Michal Karpinskiac116df2018-12-10 17:51:42 +0000387 allowBackgroundActivityStart = false;
Bryce Leedaa91e42017-12-06 14:13:01 -0800388 }
389
390 /**
391 * Adopts all values from passed in request.
392 */
393 void set(Request request) {
394 caller = request.caller;
395 intent = request.intent;
396 ephemeralIntent = request.ephemeralIntent;
397 resolvedType = request.resolvedType;
398 activityInfo = request.activityInfo;
399 resolveInfo = request.resolveInfo;
400 voiceSession = request.voiceSession;
401 voiceInteractor = request.voiceInteractor;
402 resultTo = request.resultTo;
403 resultWho = request.resultWho;
404 requestCode = request.requestCode;
405 callingPid = request.callingPid;
406 callingUid = request.callingUid;
407 callingPackage = request.callingPackage;
408 realCallingPid = request.realCallingPid;
409 realCallingUid = request.realCallingUid;
410 startFlags = request.startFlags;
411 activityOptions = request.activityOptions;
412 ignoreTargetSecurity = request.ignoreTargetSecurity;
413 componentSpecified = request.componentSpecified;
414 outActivity = request.outActivity;
415 inTask = request.inTask;
416 reason = request.reason;
417 profilerInfo = request.profilerInfo;
418 globalConfig = request.globalConfig;
Bryce Leedaa91e42017-12-06 14:13:01 -0800419 userId = request.userId;
420 waitResult = request.waitResult;
Winson Chunge2d72172018-01-25 17:46:20 +0000421 avoidMoveToFront = request.avoidMoveToFront;
Jorim Jaggi6fa41c32018-04-23 18:35:00 +0200422 allowPendingRemoteAnimationRegistryLookup
423 = request.allowPendingRemoteAnimationRegistryLookup;
Patrick Baumann31426b22018-05-21 13:46:40 -0700424 filterCallingUid = request.filterCallingUid;
Michal Karpinski201bc0c2018-07-20 15:32:00 +0100425 originatingPendingIntent = request.originatingPendingIntent;
Michal Karpinskiac116df2018-12-10 17:51:42 +0000426 allowBackgroundActivityStart = request.allowBackgroundActivityStart;
Bryce Leedaa91e42017-12-06 14:13:01 -0800427 }
Louis Chang54fbb052019-10-16 17:10:17 +0800428
429 /**
430 * Resolve activity from the given intent for this launch.
431 */
432 void resolveActivity(ActivityStackSupervisor supervisor) {
433 if (realCallingPid == Request.DEFAULT_REAL_CALLING_PID) {
434 realCallingPid = Binder.getCallingPid();
435 }
436 if (realCallingUid == Request.DEFAULT_REAL_CALLING_UID) {
437 realCallingUid = Binder.getCallingUid();
438 }
439
440 if (callingUid >= 0) {
441 callingPid = -1;
442 } else if (caller == null) {
443 callingPid = realCallingPid;
444 callingUid = realCallingUid;
445 } else {
446 callingPid = callingUid = -1;
447 }
448
449 // Save a copy in case ephemeral needs it
450 ephemeralIntent = new Intent(intent);
451 // Don't modify the client's object!
452 intent = new Intent(intent);
453 if (intent.getComponent() != null
454 && !(Intent.ACTION_VIEW.equals(intent.getAction()) && intent.getData() == null)
455 && !Intent.ACTION_INSTALL_INSTANT_APP_PACKAGE.equals(intent.getAction())
456 && !Intent.ACTION_RESOLVE_INSTANT_APP_PACKAGE.equals(intent.getAction())
457 && supervisor.mService.getPackageManagerInternalLocked()
458 .isInstantAppInstallerComponent(intent.getComponent())) {
459 // Intercept intents targeted directly to the ephemeral installer the ephemeral
460 // installer should never be started with a raw Intent; instead adjust the intent
461 // so it looks like a "normal" instant app launch.
462 intent.setComponent(null /* component */);
463 }
464
465 resolveInfo = supervisor.resolveIntent(intent, resolvedType, userId,
466 0 /* matchFlags */,
467 computeResolveFilterUid(callingUid, realCallingUid, filterCallingUid));
468 if (resolveInfo == null) {
469 final UserInfo userInfo = supervisor.getUserInfo(userId);
470 if (userInfo != null && userInfo.isManagedProfile()) {
471 // Special case for managed profiles, if attempting to launch non-cryto aware
472 // app in a locked managed profile from an unlocked parent allow it to resolve
473 // as user will be sent via confirm credentials to unlock the profile.
474 final UserManager userManager = UserManager.get(supervisor.mService.mContext);
475 boolean profileLockedAndParentUnlockingOrUnlocked = false;
476 final long token = Binder.clearCallingIdentity();
477 try {
478 final UserInfo parent = userManager.getProfileParent(userId);
479 profileLockedAndParentUnlockingOrUnlocked = (parent != null)
480 && userManager.isUserUnlockingOrUnlocked(parent.id)
481 && !userManager.isUserUnlockingOrUnlocked(userId);
482 } finally {
483 Binder.restoreCallingIdentity(token);
484 }
485 if (profileLockedAndParentUnlockingOrUnlocked) {
486 resolveInfo = supervisor.resolveIntent(intent, resolvedType, userId,
487 PackageManager.MATCH_DIRECT_BOOT_AWARE
488 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE,
489 computeResolveFilterUid(callingUid, realCallingUid,
490 filterCallingUid));
491 }
492 }
493 }
494
495 // Collect information about the target of the Intent.
496 activityInfo = supervisor.resolveActivity(intent, resolveInfo, startFlags,
497 profilerInfo);
498 }
Bryce Leed3624e12017-11-30 08:51:45 -0800499 }
500
Wale Ogunwalec9e57de2018-05-08 14:28:07 -0700501 ActivityStarter(ActivityStartController controller, ActivityTaskManagerService service,
Bryce Leed3624e12017-11-30 08:51:45 -0800502 ActivityStackSupervisor supervisor, ActivityStartInterceptor interceptor) {
503 mController = controller;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800504 mService = service;
Louis Chang149d5c82019-12-30 09:47:39 +0800505 mRootWindowContainer = service.mRootWindowContainer;
Bryce Leed3624e12017-11-30 08:51:45 -0800506 mSupervisor = supervisor;
507 mInterceptor = interceptor;
Bryce Leedaa91e42017-12-06 14:13:01 -0800508 reset(true);
509 }
510
511 /**
512 * Effectively duplicates the starter passed in. All state and request values will be
513 * mirrored.
514 * @param starter
515 */
516 void set(ActivityStarter starter) {
517 mStartActivity = starter.mStartActivity;
518 mIntent = starter.mIntent;
519 mCallingUid = starter.mCallingUid;
520 mOptions = starter.mOptions;
Ricky Waib147fa12019-04-25 16:08:30 +0100521 mRestrictedBgActivity = starter.mRestrictedBgActivity;
Bryce Leedaa91e42017-12-06 14:13:01 -0800522
523 mLaunchTaskBehind = starter.mLaunchTaskBehind;
524 mLaunchFlags = starter.mLaunchFlags;
525 mLaunchMode = starter.mLaunchMode;
526
Bryce Leeec55eb02017-12-05 20:51:27 -0800527 mLaunchParams.set(starter.mLaunchParams);
Bryce Leedaa91e42017-12-06 14:13:01 -0800528
529 mNotTop = starter.mNotTop;
530 mDoResume = starter.mDoResume;
531 mStartFlags = starter.mStartFlags;
532 mSourceRecord = starter.mSourceRecord;
533 mPreferredDisplayId = starter.mPreferredDisplayId;
534
535 mInTask = starter.mInTask;
536 mAddingToTask = starter.mAddingToTask;
537 mReuseTask = starter.mReuseTask;
538
539 mNewTaskInfo = starter.mNewTaskInfo;
540 mNewTaskIntent = starter.mNewTaskIntent;
541 mSourceStack = starter.mSourceStack;
542
543 mTargetStack = starter.mTargetStack;
544 mMovedToFront = starter.mMovedToFront;
545 mNoAnimation = starter.mNoAnimation;
546 mKeepCurTransition = starter.mKeepCurTransition;
547 mAvoidMoveToFront = starter.mAvoidMoveToFront;
Winson Chunge219ae12019-07-18 13:43:23 -0700548 mFrozeTaskList = starter.mFrozeTaskList;
Bryce Leedaa91e42017-12-06 14:13:01 -0800549
550 mVoiceSession = starter.mVoiceSession;
551 mVoiceInteractor = starter.mVoiceInteractor;
552
553 mIntentDelivered = starter.mIntentDelivered;
554
555 mRequest.set(starter.mRequest);
Bryce Leed3624e12017-11-30 08:51:45 -0800556 }
557
Bryce Lee4c9a5972017-12-01 22:14:24 -0800558 boolean relatedToPackage(String packageName) {
Louis Chang54fbb052019-10-16 17:10:17 +0800559 return (mLastStartActivityRecord != null
560 && packageName.equals(mLastStartActivityRecord.packageName))
Bryce Lee4c9a5972017-12-01 22:14:24 -0800561 || (mStartActivity != null && packageName.equals(mStartActivity.packageName));
562 }
563
564 /**
Louis Chang54fbb052019-10-16 17:10:17 +0800565 * Starts an activity based on the provided {@link ActivityRecord} and environment parameters.
566 * Note that this method is called internally as well as part of {@link #executeRequest}.
567 */
568 void startResolvedActivity(final ActivityRecord r, ActivityRecord sourceRecord,
569 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Louis Changcdec0802019-11-11 11:45:07 +0800570 int startFlags, boolean doResume, ActivityOptions options, Task inTask) {
Louis Chang54fbb052019-10-16 17:10:17 +0800571 try {
Riddle Hsufd66d4d2019-11-14 10:35:55 +0800572 final LaunchingState launchingState = mSupervisor.getActivityMetricsLogger()
573 .notifyActivityLaunching(r.intent, r.resultTo);
Louis Chang54fbb052019-10-16 17:10:17 +0800574 mLastStartReason = "startResolvedActivity";
575 mLastStartActivityTimeMs = System.currentTimeMillis();
576 mLastStartActivityRecord = r;
577 mLastStartActivityResult = startActivityUnchecked(r, sourceRecord, voiceSession,
578 voiceInteractor, startFlags, doResume, options, inTask,
579 false /* restrictedBgActivity */);
Riddle Hsufd66d4d2019-11-14 10:35:55 +0800580 mSupervisor.getActivityMetricsLogger().notifyActivityLaunched(launchingState,
581 mLastStartActivityResult, mLastStartActivityRecord);
Louis Chang54fbb052019-10-16 17:10:17 +0800582 } finally {
583 onExecutionComplete();
584 }
585 }
586
587 /**
588 * Resolve necessary information according the request parameters provided earlier, and execute
589 * the request which begin the journey of starting an activity.
Bryce Lee4c9a5972017-12-01 22:14:24 -0800590 * @return The starter result.
591 */
592 int execute() {
Bryce Leedaa91e42017-12-06 14:13:01 -0800593 try {
Louis Chang54fbb052019-10-16 17:10:17 +0800594 // Refuse possible leaked file descriptors
595 if (mRequest.intent != null && mRequest.intent.hasFileDescriptors()) {
596 throw new IllegalArgumentException("File descriptors passed in Intent");
597 }
598
Riddle Hsufd66d4d2019-11-14 10:35:55 +0800599 final LaunchingState launchingState;
600 synchronized (mService.mGlobalLock) {
601 final ActivityRecord caller = ActivityRecord.forTokenLocked(mRequest.resultTo);
602 launchingState = mSupervisor.getActivityMetricsLogger().notifyActivityLaunching(
603 mRequest.intent, caller);
604 }
Louis Chang54fbb052019-10-16 17:10:17 +0800605
Riddle Hsufd66d4d2019-11-14 10:35:55 +0800606 // Do not lock the resolving to avoid potential deadlock.
Louis Chang54fbb052019-10-16 17:10:17 +0800607 if (mRequest.activityInfo == null) {
608 mRequest.resolveActivity(mSupervisor);
609 }
610
611 int res;
612 synchronized (mService.mGlobalLock) {
Louis Chang149d5c82019-12-30 09:47:39 +0800613 final ActivityStack stack = mRootWindowContainer.getTopDisplayFocusedStack();
Louis Chang54fbb052019-10-16 17:10:17 +0800614 stack.mConfigWillChange = mRequest.globalConfig != null
615 && mService.getGlobalConfiguration().diff(mRequest.globalConfig) != 0;
616 if (DEBUG_CONFIGURATION) {
617 Slog.v(TAG_CONFIGURATION, "Starting activity when config will change = "
618 + stack.mConfigWillChange);
619 }
620
621 final long origId = Binder.clearCallingIdentity();
622
623 res = resolveToHeavyWeightSwitcherIfNeeded();
624 if (res != START_SUCCESS) {
625 return res;
626 }
627 res = executeRequest(mRequest);
628
629 Binder.restoreCallingIdentity(origId);
630
631 if (stack.mConfigWillChange) {
632 // If the caller also wants to switch to a new configuration, do so now.
633 // This allows a clean switch, as we are waiting for the current activity
634 // to pause (so we will not destroy it), and have not yet started the
635 // next activity.
636 mService.mAmInternal.enforceCallingPermission(
637 android.Manifest.permission.CHANGE_CONFIGURATION,
638 "updateConfiguration()");
639 stack.mConfigWillChange = false;
640 if (DEBUG_CONFIGURATION) {
641 Slog.v(TAG_CONFIGURATION,
642 "Updating to new configuration after starting activity.");
643 }
644 mService.updateConfigurationLocked(mRequest.globalConfig, null, false);
645 }
646
647 // Notify ActivityMetricsLogger that the activity has launched.
648 // ActivityMetricsLogger will then wait for the windows to be drawn and populate
649 // WaitResult.
Riddle Hsufd66d4d2019-11-14 10:35:55 +0800650 mSupervisor.getActivityMetricsLogger().notifyActivityLaunched(launchingState, res,
Louis Chang54fbb052019-10-16 17:10:17 +0800651 mLastStartActivityRecord);
652 return getExternalResult(mRequest.waitResult == null ? res
653 : waitForResult(res, mLastStartActivityRecord));
Bryce Leedaa91e42017-12-06 14:13:01 -0800654 }
655 } finally {
656 onExecutionComplete();
657 }
658 }
659
660 /**
Louis Chang54fbb052019-10-16 17:10:17 +0800661 * Updates the request to heavy-weight switch if this is a heavy-weight process while there
662 * already have another, different heavy-weight process running.
Bryce Leedaa91e42017-12-06 14:13:01 -0800663 */
Louis Chang54fbb052019-10-16 17:10:17 +0800664 private int resolveToHeavyWeightSwitcherIfNeeded() {
665 if (mRequest.activityInfo == null || !mService.mHasHeavyWeightFeature
666 || (mRequest.activityInfo.applicationInfo.privateFlags
667 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) == 0) {
668 return START_SUCCESS;
Wale Ogunwale692dcd62017-06-20 13:38:14 -0700669 }
Bryce Leef9d49542017-06-26 16:27:32 -0700670
Louis Chang54fbb052019-10-16 17:10:17 +0800671 if (!mRequest.activityInfo.processName.equals(
672 mRequest.activityInfo.applicationInfo.packageName)) {
673 return START_SUCCESS;
674 }
Bryce Lee93e7f792017-10-25 15:54:55 -0700675
Louis Chang54fbb052019-10-16 17:10:17 +0800676 final WindowProcessController heavy = mService.mHeavyWeightProcess;
677 if (heavy == null || (heavy.mInfo.uid == mRequest.activityInfo.applicationInfo.uid
678 && heavy.mName.equals(mRequest.activityInfo.processName))) {
679 return START_SUCCESS;
680 }
681
682 int appCallingUid = mRequest.callingUid;
683 if (mRequest.caller != null) {
684 WindowProcessController callerApp = mService.getProcessController(mRequest.caller);
685 if (callerApp != null) {
686 appCallingUid = callerApp.mInfo.uid;
687 } else {
688 Slog.w(TAG, "Unable to find app for caller " + mRequest.caller + " (pid="
689 + mRequest.callingPid + ") when starting: " + mRequest.intent.toString());
690 SafeActivityOptions.abort(mRequest.activityOptions);
691 return ActivityManager.START_PERMISSION_DENIED;
692 }
693 }
694
695 final IIntentSender target = mService.getIntentSenderLocked(
Philip P. Moltmanne7421e92020-02-10 16:14:12 +0000696 ActivityManager.INTENT_SENDER_ACTIVITY, "android" /* packageName */, appCallingUid,
697 mRequest.userId, null /* token */, null /* resultWho*/, 0 /* requestCode*/,
698 new Intent[] { mRequest.intent }, new String[] { mRequest.resolvedType },
Louis Chang54fbb052019-10-16 17:10:17 +0800699 PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT,
700 null /* bOptions */);
701
702 final Intent newIntent = new Intent();
703 if (mRequest.requestCode >= 0) {
704 // Caller is requesting a result.
705 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
706 }
707 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT, new IntentSender(target));
708 heavy.updateIntentForHeavyWeightActivity(newIntent);
709 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
710 mRequest.activityInfo.packageName);
711 newIntent.setFlags(mRequest.intent.getFlags());
712 newIntent.setClassName("android" /* packageName */,
713 HeavyWeightSwitcherActivity.class.getName());
714 mRequest.intent = newIntent;
715 mRequest.resolvedType = null;
716 mRequest.caller = null;
717 mRequest.callingUid = Binder.getCallingUid();
718 mRequest.callingPid = Binder.getCallingPid();
719 mRequest.componentSpecified = true;
720 mRequest.resolveInfo = mSupervisor.resolveIntent(mRequest.intent, null /* resolvedType */,
721 mRequest.userId, 0 /* matchFlags */,
722 computeResolveFilterUid(mRequest.callingUid, mRequest.realCallingUid,
723 mRequest.filterCallingUid));
724 mRequest.activityInfo =
725 mRequest.resolveInfo != null ? mRequest.resolveInfo.activityInfo : null;
726 if (mRequest.activityInfo != null) {
727 mRequest.activityInfo = mService.mAmInternal.getActivityInfoForUser(
728 mRequest.activityInfo, mRequest.userId);
729 }
730
731 return START_SUCCESS;
Wale Ogunwale692dcd62017-06-20 13:38:14 -0700732 }
733
Bryce Leedaa91e42017-12-06 14:13:01 -0800734 /**
Louis Chang54fbb052019-10-16 17:10:17 +0800735 * Wait for activity launch completes.
Bryce Leedaa91e42017-12-06 14:13:01 -0800736 */
Louis Chang54fbb052019-10-16 17:10:17 +0800737 private int waitForResult(int res, ActivityRecord r) {
738 mRequest.waitResult.result = res;
739 switch(res) {
740 case START_SUCCESS: {
741 mSupervisor.mWaitingActivityLaunched.add(mRequest.waitResult);
742 do {
743 try {
744 mService.mGlobalLock.wait();
745 } catch (InterruptedException e) {
746 }
747 } while (mRequest.waitResult.result != START_TASK_TO_FRONT
748 && !mRequest.waitResult.timeout && mRequest.waitResult.who == null);
749 if (mRequest.waitResult.result == START_TASK_TO_FRONT) {
750 res = START_TASK_TO_FRONT;
751 }
752 break;
753 }
754 case START_DELIVERED_TO_TOP: {
755 mRequest.waitResult.timeout = false;
756 mRequest.waitResult.who = r.mActivityComponent;
757 mRequest.waitResult.totalTime = 0;
758 break;
759 }
760 case START_TASK_TO_FRONT: {
761 mRequest.waitResult.launchState =
762 r.attachedToProcess() ? LAUNCH_STATE_HOT : LAUNCH_STATE_COLD;
763 // ActivityRecord may represent a different activity, but it should not be
764 // in the resumed state.
765 if (r.nowVisible && r.isState(RESUMED)) {
766 mRequest.waitResult.timeout = false;
767 mRequest.waitResult.who = r.mActivityComponent;
768 mRequest.waitResult.totalTime = 0;
769 } else {
Riddle Hsuc48c8912019-10-31 13:34:27 +0800770 mSupervisor.waitActivityVisible(r.mActivityComponent, mRequest.waitResult);
Louis Chang54fbb052019-10-16 17:10:17 +0800771 // Note: the timeout variable is not currently not ever set.
772 do {
773 try {
774 mService.mGlobalLock.wait();
775 } catch (InterruptedException e) {
776 }
777 } while (!mRequest.waitResult.timeout && mRequest.waitResult.who == null);
778 }
779 break;
780 }
781 }
782 return res;
Bryce Leedaa91e42017-12-06 14:13:01 -0800783 }
784
Louis Chang54fbb052019-10-16 17:10:17 +0800785 /**
786 * Executing activity start request and starts the journey of starting an activity. Here
787 * begins with performing several preliminary checks. The normally activity launch flow will
788 * go through {@link #startActivityUnchecked} to {@link #startActivityInner}.
789 */
790 private int executeRequest(Request request) {
791 if (TextUtils.isEmpty(request.reason)) {
792 throw new IllegalArgumentException("Need to specify a reason.");
793 }
794 mLastStartReason = request.reason;
795 mLastStartActivityTimeMs = System.currentTimeMillis();
796 mLastStartActivityRecord = null;
797
798 final IApplicationThread caller = request.caller;
799 Intent intent = request.intent;
800 String resolvedType = request.resolvedType;
801 ActivityInfo aInfo = request.activityInfo;
802 ResolveInfo rInfo = request.resolveInfo;
803 final IVoiceInteractionSession voiceSession = request.voiceSession;
804 final IBinder resultTo = request.resultTo;
805 String resultWho = request.resultWho;
806 int requestCode = request.requestCode;
807 int callingPid = request.callingPid;
808 int callingUid = request.callingUid;
809 String callingPackage = request.callingPackage;
810 final int realCallingPid = request.realCallingPid;
811 final int realCallingUid = request.realCallingUid;
812 final int startFlags = request.startFlags;
813 final SafeActivityOptions options = request.activityOptions;
Louis Changcdec0802019-11-11 11:45:07 +0800814 Task inTask = request.inTask;
Louis Chang54fbb052019-10-16 17:10:17 +0800815
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800816 int err = ActivityManager.START_SUCCESS;
Chad Brubaker06068612017-04-06 09:43:47 -0700817 // Pull the optional Ephemeral Installer-only bundle out of the options early.
Louis Chang54fbb052019-10-16 17:10:17 +0800818 final Bundle verificationBundle =
819 options != null ? options.popAppVerificationBundle() : null;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800820
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700821 WindowProcessController callerApp = null;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800822 if (caller != null) {
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700823 callerApp = mService.getProcessController(caller);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800824 if (callerApp != null) {
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700825 callingPid = callerApp.getPid();
826 callingUid = callerApp.mInfo.uid;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800827 } else {
Louis Chang54fbb052019-10-16 17:10:17 +0800828 Slog.w(TAG, "Unable to find app for caller " + caller + " (pid=" + callingPid
829 + ") when starting: " + intent.toString());
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800830 err = ActivityManager.START_PERMISSION_DENIED;
831 }
832 }
833
Bryce Lee93e7f792017-10-25 15:54:55 -0700834 final int userId = aInfo != null && aInfo.applicationInfo != null
835 ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800836 if (err == ActivityManager.START_SUCCESS) {
837 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Andrii Kulian03c403d2016-11-11 11:14:12 -0800838 + "} from uid " + callingUid);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800839 }
840
841 ActivityRecord sourceRecord = null;
842 ActivityRecord resultRecord = null;
843 if (resultTo != null) {
Louis Chang149d5c82019-12-30 09:47:39 +0800844 sourceRecord = mRootWindowContainer.isInAnyStack(resultTo);
Louis Chang54fbb052019-10-16 17:10:17 +0800845 if (DEBUG_RESULTS) {
846 Slog.v(TAG_RESULTS, "Will send result to " + resultTo + " " + sourceRecord);
847 }
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800848 if (sourceRecord != null) {
849 if (requestCode >= 0 && !sourceRecord.finishing) {
850 resultRecord = sourceRecord;
851 }
852 }
853 }
854
855 final int launchFlags = intent.getFlags();
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800856 if ((launchFlags & Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Louis Chang54fbb052019-10-16 17:10:17 +0800857 // Transfer the result target from the source activity to the new one being started,
858 // including any failures.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800859 if (requestCode >= 0) {
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +0100860 SafeActivityOptions.abort(options);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800861 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
862 }
863 resultRecord = sourceRecord.resultTo;
864 if (resultRecord != null && !resultRecord.isInStackLocked()) {
865 resultRecord = null;
866 }
867 resultWho = sourceRecord.resultWho;
868 requestCode = sourceRecord.requestCode;
869 sourceRecord.resultTo = null;
870 if (resultRecord != null) {
871 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
872 }
873 if (sourceRecord.launchedFromUid == callingUid) {
Louis Chang54fbb052019-10-16 17:10:17 +0800874 // The new activity is being launched from the same uid as the previous activity
875 // in the flow, and asking to forward its result back to the previous. In this
876 // case the activity is serving as a trampoline between the two, so we also want
877 // to update its launchedFromPackage to be the same as the previous activity.
878 // Note that this is safe, since we know these two packages come from the same
879 // uid; the caller could just as well have supplied that same package name itself
880 // . This specifially deals with the case of an intent picker/chooser being
881 // launched in the app flow to redirect to an activity picked by the user, where
882 // we want the final activity to consider it to have been launched by the
883 // previous app activity.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800884 callingPackage = sourceRecord.launchedFromPackage;
885 }
886 }
887
888 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
889 // We couldn't find a class that can handle the given Intent.
890 // That's the end of that!
891 err = ActivityManager.START_INTENT_NOT_RESOLVED;
892 }
893
894 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
895 // We couldn't find the specific class specified in the Intent.
896 // Also the end of the line.
897 err = ActivityManager.START_CLASS_NOT_FOUND;
898 }
899
900 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
Louis Changcdec0802019-11-11 11:45:07 +0800901 && sourceRecord.getTask().voiceSession != null) {
Louis Chang54fbb052019-10-16 17:10:17 +0800902 // If this activity is being launched as part of a voice session, we need to ensure
903 // that it is safe to do so. If the upcoming activity will also be part of the voice
904 // session, we can only launch it if it has explicitly said it supports the VOICE
905 // category, or it is a part of the calling app.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800906 if ((launchFlags & FLAG_ACTIVITY_NEW_TASK) == 0
907 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
908 try {
909 intent.addCategory(Intent.CATEGORY_VOICE);
Wale Ogunwale906f9c62018-07-23 11:23:44 -0700910 if (!mService.getPackageManager().activitySupportsIntent(
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800911 intent.getComponent(), intent, resolvedType)) {
Louis Chang54fbb052019-10-16 17:10:17 +0800912 Slog.w(TAG, "Activity being started in current voice task does not support "
913 + "voice: " + intent);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800914 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
915 }
916 } catch (RemoteException e) {
917 Slog.w(TAG, "Failure checking voice capabilities", e);
918 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
919 }
920 }
921 }
922
923 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
924 // If the caller is starting a new voice session, just make sure the target
925 // is actually allowing it to run this way.
926 try {
Wale Ogunwale906f9c62018-07-23 11:23:44 -0700927 if (!mService.getPackageManager().activitySupportsIntent(intent.getComponent(),
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800928 intent, resolvedType)) {
929 Slog.w(TAG,
Louis Chang54fbb052019-10-16 17:10:17 +0800930 "Activity being started in new voice task does not support: " + intent);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800931 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
932 }
933 } catch (RemoteException e) {
934 Slog.w(TAG, "Failure checking voice capabilities", e);
935 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
936 }
937 }
938
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800939 final ActivityStack resultStack = resultRecord == null
Wale Ogunwale1ebcd8e2020-01-21 11:27:03 -0800940 ? null : resultRecord.getRootTask();
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800941
Wale Ogunwale01d66562015-12-29 08:19:19 -0800942 if (err != START_SUCCESS) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800943 if (resultRecord != null) {
Andrii Kulian79d67982019-08-19 11:56:16 -0700944 resultRecord.sendResult(INVALID_UID, resultWho, requestCode, RESULT_CANCELED,
945 null /* data */);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800946 }
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +0100947 SafeActivityOptions.abort(options);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800948 return err;
949 }
950
951 boolean abort = !mSupervisor.checkStartAnyActivityPermission(intent, aInfo, resultWho,
Philip P. Moltmanne7421e92020-02-10 16:14:12 +0000952 requestCode, callingPid, callingUid, callingPackage, request.ignoreTargetSecurity,
953 inTask != null, callerApp, resultRecord, resultStack);
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700954 abort |= !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800955 callingPid, resolvedType, aInfo.applicationInfo);
Hai Zhangf4da9be2019-05-01 13:46:06 +0800956 abort |= !mService.getPermissionPolicyInternal().checkStartActivity(intent, callingUid,
957 callingPackage);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800958
Ricky Waib147fa12019-04-25 16:08:30 +0100959 boolean restrictedBgActivity = false;
Michal Karpinski8596ded2018-11-14 14:43:48 +0000960 if (!abort) {
Michal Karpinski4fd5b842019-01-28 15:13:32 +0000961 try {
Riddle Hsu2ca561b2019-10-08 21:58:58 +0800962 Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER,
Michal Karpinski4fd5b842019-01-28 15:13:32 +0000963 "shouldAbortBackgroundActivityStart");
Ricky Waib147fa12019-04-25 16:08:30 +0100964 restrictedBgActivity = shouldAbortBackgroundActivityStart(callingUid,
Ricky Waiaca8a772019-04-04 16:01:06 +0100965 callingPid, callingPackage, realCallingUid, realCallingPid, callerApp,
Louis Chang54fbb052019-10-16 17:10:17 +0800966 request.originatingPendingIntent, request.allowBackgroundActivityStart,
967 intent);
Michal Karpinski4fd5b842019-01-28 15:13:32 +0000968 } finally {
Riddle Hsu2ca561b2019-10-08 21:58:58 +0800969 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
Michal Karpinski4fd5b842019-01-28 15:13:32 +0000970 }
Michal Karpinski8596ded2018-11-14 14:43:48 +0000971 }
972
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +0100973 // Merge the two options bundles, while realCallerOptions takes precedence.
974 ActivityOptions checkedOptions = options != null
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700975 ? options.getOptions(intent, aInfo, callerApp, mSupervisor) : null;
Louis Chang54fbb052019-10-16 17:10:17 +0800976 if (request.allowPendingRemoteAnimationRegistryLookup) {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -0700977 checkedOptions = mService.getActivityStartController()
Jorim Jaggi6fa41c32018-04-23 18:35:00 +0200978 .getPendingRemoteAnimationRegistry()
979 .overrideOptionsIfNeeded(callingPackage, checkedOptions);
980 }
Wale Ogunwalea6191b42018-05-09 07:41:32 -0700981 if (mService.mController != null) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800982 try {
Louis Chang54fbb052019-10-16 17:10:17 +0800983 // The Intent we give to the watcher has the extra data stripped off, since it
984 // can contain private information.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800985 Intent watchIntent = intent.cloneFilter();
Wale Ogunwalea6191b42018-05-09 07:41:32 -0700986 abort |= !mService.mController.activityStarting(watchIntent,
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800987 aInfo.applicationInfo.packageName);
988 } catch (RemoteException e) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -0700989 mService.mController = null;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800990 }
991 }
992
Philip P. Moltmanne7421e92020-02-10 16:14:12 +0000993 mInterceptor.setStates(userId, realCallingPid, realCallingUid, startFlags, callingPackage);
Benjamin Franz563707b2017-06-29 15:06:13 +0100994 if (mInterceptor.intercept(intent, rInfo, aInfo, resolvedType, inTask, callingPid,
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +0100995 callingUid, checkedOptions)) {
Benjamin Franz563707b2017-06-29 15:06:13 +0100996 // activity start was intercepted, e.g. because the target user is currently in quiet
997 // mode (turn off work) or the target application is suspended
998 intent = mInterceptor.mIntent;
999 rInfo = mInterceptor.mRInfo;
1000 aInfo = mInterceptor.mAInfo;
1001 resolvedType = mInterceptor.mResolvedType;
1002 inTask = mInterceptor.mInTask;
1003 callingPid = mInterceptor.mCallingPid;
1004 callingUid = mInterceptor.mCallingUid;
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01001005 checkedOptions = mInterceptor.mActivityOptions;
Benjamin Franz563707b2017-06-29 15:06:13 +01001006 }
1007
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001008 if (abort) {
1009 if (resultRecord != null) {
Andrii Kulian79d67982019-08-19 11:56:16 -07001010 resultRecord.sendResult(INVALID_UID, resultWho, requestCode, RESULT_CANCELED,
1011 null /* data */);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001012 }
Louis Chang54fbb052019-10-16 17:10:17 +08001013 // We pretend to the caller that it was really started, but they will just get a
1014 // cancel result.
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01001015 ActivityOptions.abort(checkedOptions);
Bryce Leef9d49542017-06-26 16:27:32 -07001016 return START_ABORTED;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001017 }
1018
1019 // If permissions need a review before any of the app components can run, we
1020 // launch the review activity and pass a pending intent to start the activity
1021 // we are to launching now after the review is completed.
Philip P. Moltmann6c644e62018-07-18 15:41:24 -07001022 if (aInfo != null) {
Wale Ogunwale906f9c62018-07-23 11:23:44 -07001023 if (mService.getPackageManagerInternalLocked().isPermissionsReviewRequired(
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001024 aInfo.packageName, userId)) {
Louis Chang54fbb052019-10-16 17:10:17 +08001025 final IIntentSender target = mService.getIntentSenderLocked(
Philip P. Moltmanne7421e92020-02-10 16:14:12 +00001026 ActivityManager.INTENT_SENDER_ACTIVITY, callingPackage,
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001027 callingUid, userId, null, null, 0, new Intent[]{intent},
1028 new String[]{resolvedType}, PendingIntent.FLAG_CANCEL_CURRENT
1029 | PendingIntent.FLAG_ONE_SHOT, null);
1030
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001031 Intent newIntent = new Intent(Intent.ACTION_REVIEW_PERMISSIONS);
Philip P. Moltmannc3e66d02019-01-31 15:56:18 -08001032
1033 int flags = intent.getFlags();
1034 flags |= Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS;
1035
1036 /*
1037 * Prevent reuse of review activity: Each app needs their own review activity. By
1038 * default activities launched with NEW_TASK or NEW_DOCUMENT try to reuse activities
1039 * with the same launch parameters (extras are ignored). Hence to avoid possible
1040 * reuse force a new activity via the MULTIPLE_TASK flag.
1041 *
1042 * Activities that are not launched with NEW_TASK or NEW_DOCUMENT are not re-used,
1043 * hence no need to add the flag in this case.
1044 */
1045 if ((flags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_NEW_DOCUMENT)) != 0) {
1046 flags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1047 }
1048 newIntent.setFlags(flags);
1049
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001050 newIntent.putExtra(Intent.EXTRA_PACKAGE_NAME, aInfo.packageName);
1051 newIntent.putExtra(Intent.EXTRA_INTENT, new IntentSender(target));
1052 if (resultRecord != null) {
1053 newIntent.putExtra(Intent.EXTRA_RESULT_NEEDED, true);
1054 }
1055 intent = newIntent;
1056
1057 resolvedType = null;
1058 callingUid = realCallingUid;
1059 callingPid = realCallingPid;
1060
Svet Ganovcbcbf662018-05-10 17:25:29 -07001061 rInfo = mSupervisor.resolveIntent(intent, resolvedType, userId, 0,
Patrick Baumann31426b22018-05-21 13:46:40 -07001062 computeResolveFilterUid(
Louis Chang54fbb052019-10-16 17:10:17 +08001063 callingUid, realCallingUid, request.filterCallingUid));
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001064 aInfo = mSupervisor.resolveActivity(intent, rInfo, startFlags,
1065 null /*profilerInfo*/);
1066
1067 if (DEBUG_PERMISSIONS_REVIEW) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08001068 final ActivityStack focusedStack =
Louis Chang149d5c82019-12-30 09:47:39 +08001069 mRootWindowContainer.getTopDisplayFocusedStack();
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001070 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true,
1071 true, false) + "} from uid " + callingUid + " on display "
Wale Ogunwale0b3d2922019-12-30 08:55:07 -08001072 + (focusedStack == null ? DEFAULT_DISPLAY
1073 : focusedStack.getDisplayId()));
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001074 }
1075 }
1076 }
1077
1078 // If we have an ephemeral app, abort the process of launching the resolved intent.
1079 // Instead, launch the ephemeral installer. Once the installer is finished, it
1080 // starts either the intent we resolved here [on install error] or the ephemeral
1081 // app [on install success].
Patrick Baumanna89a1722018-02-07 15:26:52 -08001082 if (rInfo != null && rInfo.auxiliaryInfo != null) {
Louis Chang54fbb052019-10-16 17:10:17 +08001083 intent = createLaunchIntent(rInfo.auxiliaryInfo, request.ephemeralIntent,
Philip P. Moltmanne7421e92020-02-10 16:14:12 +00001084 callingPackage, verificationBundle, resolvedType, userId);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001085 resolvedType = null;
1086 callingUid = realCallingUid;
1087 callingPid = realCallingPid;
1088
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001089 aInfo = mSupervisor.resolveActivity(intent, rInfo, startFlags, null /*profilerInfo*/);
1090 }
1091
Louis Chang54fbb052019-10-16 17:10:17 +08001092 final ActivityRecord r = new ActivityRecord(mService, callerApp, callingPid, callingUid,
Philip P. Moltmanne7421e92020-02-10 16:14:12 +00001093 callingPackage, intent, resolvedType, aInfo, mService.getGlobalConfiguration(),
1094 resultRecord, resultWho, requestCode, request.componentSpecified,
1095 voiceSession != null, mSupervisor, checkedOptions, sourceRecord);
Louis Chang54fbb052019-10-16 17:10:17 +08001096 mLastStartActivityRecord = r;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001097
1098 if (r.appTimeTracker == null && sourceRecord != null) {
1099 // If the caller didn't specify an explicit time tracker, we want to continue
1100 // tracking under any it has.
1101 r.appTimeTracker = sourceRecord.appTimeTracker;
1102 }
1103
Louis Chang149d5c82019-12-30 09:47:39 +08001104 final ActivityStack stack = mRootWindowContainer.getTopDisplayFocusedStack();
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01001105
1106 // If we are starting an activity that is not from the same uid as the currently resumed
1107 // one, check whether app switches are allowed.
Bryce Leec4ab62a2018-03-05 14:19:26 -08001108 if (voiceSession == null && (stack.getResumedActivity() == null
1109 || stack.getResumedActivity().info.applicationInfo.uid != realCallingUid)) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001110 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001111 realCallingPid, realCallingUid, "Activity start")) {
Alan Stokes07389b62019-05-20 15:22:54 +01001112 if (!(restrictedBgActivity && handleBackgroundActivityAbort(r))) {
Ricky Waib147fa12019-04-25 16:08:30 +01001113 mController.addPendingActivityLaunch(new PendingActivityLaunch(r,
Wale Ogunwale586a8ee2019-06-04 13:44:14 +00001114 sourceRecord, startFlags, stack, callerApp));
Ricky Waib147fa12019-04-25 16:08:30 +01001115 }
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01001116 ActivityOptions.abort(checkedOptions);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001117 return ActivityManager.START_SWITCHES_CANCELED;
1118 }
1119 }
1120
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001121 mService.onStartActivitySetDidAppSwitch();
Bryce Leed3624e12017-11-30 08:51:45 -08001122 mController.doPendingActivityLaunches(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001123
Louis Chang54fbb052019-10-16 17:10:17 +08001124 mLastStartActivityResult = startActivityUnchecked(r, sourceRecord, voiceSession,
1125 request.voiceInteractor, startFlags, true /* doResume */, checkedOptions, inTask,
1126 restrictedBgActivity);
1127
1128 if (request.outActivity != null) {
1129 request.outActivity[0] = mLastStartActivityRecord;
1130 }
1131
Riddle Hsu7f8643a2019-12-05 14:37:30 +08001132 return mLastStartActivityResult;
Louis Chang54fbb052019-10-16 17:10:17 +08001133 }
1134
1135 /**
1136 * Return true if background activity is really aborted.
1137 *
1138 * TODO(b/131748165): Refactor the logic so we don't need to call this method everywhere.
1139 */
1140 private boolean handleBackgroundActivityAbort(ActivityRecord r) {
1141 // TODO(b/131747138): Remove toast and refactor related code in R release.
1142 final boolean abort = !mService.isBackgroundActivityStartsEnabled();
1143 if (!abort) {
1144 return false;
1145 }
1146 final ActivityRecord resultRecord = r.resultTo;
1147 final String resultWho = r.resultWho;
1148 int requestCode = r.requestCode;
1149 if (resultRecord != null) {
1150 resultRecord.sendResult(INVALID_UID, resultWho, requestCode, RESULT_CANCELED,
1151 null /* data */);
1152 }
1153 // We pretend to the caller that it was really started to make it backward compatible, but
1154 // they will just get a cancel result.
1155 ActivityOptions.abort(r.pendingOptions);
1156 return true;
1157 }
1158
1159 static int getExternalResult(int result) {
1160 // Aborted results are treated as successes externally, but we must track them internally.
1161 return result != START_ABORTED ? result : START_SUCCESS;
1162 }
1163
1164 /**
1165 * Called when execution is complete. Sets state indicating completion and proceeds with
1166 * recycling if appropriate.
1167 */
1168 private void onExecutionComplete() {
1169 mController.onExecutionComplete(this);
Wale Ogunwalecc25a8a2016-01-23 14:31:37 -08001170 }
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001171
Ricky Waiaca8a772019-04-04 16:01:06 +01001172 boolean shouldAbortBackgroundActivityStart(int callingUid, int callingPid,
Michal Karpinski9cbb20b2019-02-05 17:31:50 +00001173 final String callingPackage, int realCallingUid, int realCallingPid,
1174 WindowProcessController callerApp, PendingIntentRecord originatingPendingIntent,
1175 boolean allowBackgroundActivityStart, Intent intent) {
Michal Karpinski8596ded2018-11-14 14:43:48 +00001176 // don't abort for the most important UIDs
Alan Stokes2f4a4ed2019-05-08 16:56:45 +01001177 final int callingAppId = UserHandle.getAppId(callingUid);
1178 if (callingUid == Process.ROOT_UID || callingAppId == Process.SYSTEM_UID
1179 || callingAppId == Process.NFC_UID) {
Michal Karpinski8596ded2018-11-14 14:43:48 +00001180 return false;
1181 }
Alan Stokeseea8d3e2019-04-10 17:37:25 +01001182 // don't abort if the callingUid has a visible window or is a persistent system process
Riddle Hsua0536432019-02-16 00:38:59 +08001183 final int callingUidProcState = mService.getUidState(callingUid);
Michal Karpinski4fd5b842019-01-28 15:13:32 +00001184 final boolean callingUidHasAnyVisibleWindow =
1185 mService.mWindowManager.mRoot.isAnyNonToastWindowVisibleForUid(callingUid);
1186 final boolean isCallingUidForeground = callingUidHasAnyVisibleWindow
Amith Yamasanif235d0b2019-03-20 22:49:43 -07001187 || callingUidProcState == ActivityManager.PROCESS_STATE_TOP
1188 || callingUidProcState == ActivityManager.PROCESS_STATE_BOUND_TOP;
Alan Stokes2f4a4ed2019-05-08 16:56:45 +01001189 final boolean isCallingUidPersistentSystemProcess =
1190 callingUidProcState <= ActivityManager.PROCESS_STATE_PERSISTENT_UI;
Alan Stokeseea8d3e2019-04-10 17:37:25 +01001191 if (callingUidHasAnyVisibleWindow || isCallingUidPersistentSystemProcess) {
Michal Karpinski8596ded2018-11-14 14:43:48 +00001192 return false;
1193 }
Michal Karpinskiac116df2018-12-10 17:51:42 +00001194 // take realCallingUid into consideration
Michal Karpinski4fd5b842019-01-28 15:13:32 +00001195 final int realCallingUidProcState = (callingUid == realCallingUid)
1196 ? callingUidProcState
Riddle Hsua0536432019-02-16 00:38:59 +08001197 : mService.getUidState(realCallingUid);
Michal Karpinski4fd5b842019-01-28 15:13:32 +00001198 final boolean realCallingUidHasAnyVisibleWindow = (callingUid == realCallingUid)
1199 ? callingUidHasAnyVisibleWindow
1200 : mService.mWindowManager.mRoot.isAnyNonToastWindowVisibleForUid(realCallingUid);
1201 final boolean isRealCallingUidForeground = (callingUid == realCallingUid)
1202 ? isCallingUidForeground
1203 : realCallingUidHasAnyVisibleWindow
1204 || realCallingUidProcState == ActivityManager.PROCESS_STATE_TOP;
Alan Stokes2f4a4ed2019-05-08 16:56:45 +01001205 final int realCallingAppId = UserHandle.getAppId(realCallingUid);
Michal Karpinski4fd5b842019-01-28 15:13:32 +00001206 final boolean isRealCallingUidPersistentSystemProcess = (callingUid == realCallingUid)
1207 ? isCallingUidPersistentSystemProcess
Alan Stokes2f4a4ed2019-05-08 16:56:45 +01001208 : (realCallingAppId == Process.SYSTEM_UID)
Michal Karpinski4fd5b842019-01-28 15:13:32 +00001209 || realCallingUidProcState <= ActivityManager.PROCESS_STATE_PERSISTENT_UI;
Michal Karpinskiac116df2018-12-10 17:51:42 +00001210 if (realCallingUid != callingUid) {
Alan Stokes6ac9efd2019-05-09 12:50:37 +00001211 // don't abort if the realCallingUid has a visible window
1212 if (realCallingUidHasAnyVisibleWindow) {
Michal Karpinskiac116df2018-12-10 17:51:42 +00001213 return false;
1214 }
1215 // if the realCallingUid is a persistent system process, abort if the IntentSender
1216 // wasn't whitelisted to start an activity
Michal Karpinskid0162852019-01-15 16:05:25 +00001217 if (isRealCallingUidPersistentSystemProcess && allowBackgroundActivityStart) {
Michal Karpinskiac116df2018-12-10 17:51:42 +00001218 return false;
1219 }
Michal Karpinskida34cd42019-04-02 19:46:52 +01001220 // don't abort if the realCallingUid is an associated companion app
1221 if (mService.isAssociatedCompanionApp(UserHandle.getUserId(realCallingUid),
1222 realCallingUid)) {
1223 return false;
1224 }
Michal Karpinskiac116df2018-12-10 17:51:42 +00001225 }
Michal Karpinski7b97a022018-12-14 15:17:29 +00001226 // don't abort if the callingUid has START_ACTIVITIES_FROM_BACKGROUND permission
1227 if (mService.checkPermission(START_ACTIVITIES_FROM_BACKGROUND, callingPid, callingUid)
1228 == PERMISSION_GRANTED) {
1229 return false;
1230 }
Michal Karpinski82bb5902018-11-28 15:52:52 +00001231 // don't abort if the caller has the same uid as the recents component
1232 if (mSupervisor.mRecentTasks.isCallerRecents(callingUid)) {
1233 return false;
1234 }
Ricky Wai96f5c352019-04-10 18:40:17 +01001235 // don't abort if the callingUid is the device owner
1236 if (mService.isDeviceOwner(callingUid)) {
Michal Karpinski302dcec2019-02-01 11:48:25 +00001237 return false;
1238 }
Ricky Wai96f5c352019-04-10 18:40:17 +01001239 // don't abort if the callingUid has companion device
Ricky Wai2452e2d2019-03-18 19:19:08 +00001240 final int callingUserId = UserHandle.getUserId(callingUid);
Michal Karpinskida34cd42019-04-02 19:46:52 +01001241 if (mService.isAssociatedCompanionApp(callingUserId, callingUid)) {
Ricky Wai2452e2d2019-03-18 19:19:08 +00001242 return false;
1243 }
Michal Karpinski2e0aad22019-04-12 16:22:55 +01001244 // If we don't have callerApp at this point, no caller was provided to startActivity().
1245 // That's the case for PendingIntent-based starts, since the creator's process might not be
1246 // up and alive. If that's the case, we retrieve the WindowProcessController for the send()
1247 // caller, so that we can make the decision based on its foreground/whitelisted state.
1248 int callerAppUid = callingUid;
1249 if (callerApp == null) {
1250 callerApp = mService.getProcessController(realCallingPid, realCallingUid);
1251 callerAppUid = realCallingUid;
1252 }
1253 // don't abort if the callerApp or other processes of that uid are whitelisted in any way
1254 if (callerApp != null) {
1255 // first check the original calling process
1256 if (callerApp.areBackgroundActivityStartsAllowed()) {
1257 return false;
1258 }
1259 // only if that one wasn't whitelisted, check the other ones
1260 final ArraySet<WindowProcessController> uidProcesses =
1261 mService.mProcessMap.getProcesses(callerAppUid);
1262 if (uidProcesses != null) {
1263 for (int i = uidProcesses.size() - 1; i >= 0; i--) {
1264 final WindowProcessController proc = uidProcesses.valueAt(i);
1265 if (proc != callerApp && proc.areBackgroundActivityStartsAllowed()) {
1266 return false;
1267 }
1268 }
1269 }
1270 }
Michal Karpinski15486842019-04-25 17:33:42 +01001271 // don't abort if the callingUid has SYSTEM_ALERT_WINDOW permission
1272 if (mService.hasSystemAlertWindowPermission(callingUid, callingPid, callingPackage)) {
1273 Slog.w(TAG, "Background activity start for " + callingPackage
1274 + " allowed because SYSTEM_ALERT_WINDOW permission is granted.");
1275 return false;
1276 }
Michal Karpinskic02364c2019-01-22 13:00:04 +00001277 // anything that has fallen through would currently be aborted
1278 Slog.w(TAG, "Background activity start [callingPackage: " + callingPackage
Michal Karpinskid0162852019-01-15 16:05:25 +00001279 + "; callingUid: " + callingUid
1280 + "; isCallingUidForeground: " + isCallingUidForeground
1281 + "; isCallingUidPersistentSystemProcess: " + isCallingUidPersistentSystemProcess
1282 + "; realCallingUid: " + realCallingUid
1283 + "; isRealCallingUidForeground: " + isRealCallingUidForeground
1284 + "; isRealCallingUidPersistentSystemProcess: "
Ricky Waiaca8a772019-04-04 16:01:06 +01001285 + isRealCallingUidPersistentSystemProcess
Michal Karpinskid0162852019-01-15 16:05:25 +00001286 + "; originatingPendingIntent: " + originatingPendingIntent
1287 + "; isBgStartWhitelisted: " + allowBackgroundActivityStart
1288 + "; intent: " + intent
Michal Karpinski9cbb20b2019-02-05 17:31:50 +00001289 + "; callerApp: " + callerApp
Michal Karpinskid0162852019-01-15 16:05:25 +00001290 + "]");
Michal Karpinski4fd5b842019-01-28 15:13:32 +00001291 // log aborted activity start to TRON
1292 if (mService.isActivityStartsLoggingEnabled()) {
1293 mSupervisor.getActivityMetricsLogger().logAbortedBgActivityStart(intent, callerApp,
1294 callingUid, callingPackage, callingUidProcState, callingUidHasAnyVisibleWindow,
1295 realCallingUid, realCallingUidProcState, realCallingUidHasAnyVisibleWindow,
Michal Karpinski201bc0c2018-07-20 15:32:00 +01001296 (originatingPendingIntent != null));
Michal Karpinski201bc0c2018-07-20 15:32:00 +01001297 }
Michal Karpinski4fd5b842019-01-28 15:13:32 +00001298 return true;
Michal Karpinski201bc0c2018-07-20 15:32:00 +01001299 }
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01001300
Bryce Leeaa5e8c32017-03-01 16:01:06 -08001301 /**
1302 * Creates a launch intent for the given auxiliary resolution data.
1303 */
Patrick Baumann577d4022018-01-31 16:55:10 +00001304 private @NonNull Intent createLaunchIntent(@Nullable AuxiliaryResolveInfo auxiliaryResponse,
Philip P. Moltmanne7421e92020-02-10 16:14:12 +00001305 Intent originalIntent, String callingPackage, Bundle verificationBundle,
1306 String resolvedType, int userId) {
Patrick Baumann577d4022018-01-31 16:55:10 +00001307 if (auxiliaryResponse != null && auxiliaryResponse.needsPhaseTwo) {
Todd Kennedye9910222017-02-21 16:00:11 -08001308 // request phase two resolution
Winson81fef842019-08-28 12:19:08 -07001309 PackageManagerInternal packageManager = mService.getPackageManagerInternalLocked();
1310 boolean isRequesterInstantApp = packageManager.isInstantApp(callingPackage, userId);
1311 packageManager.requestInstantAppResolutionPhaseTwo(
Chad Brubaker06068612017-04-06 09:43:47 -07001312 auxiliaryResponse, originalIntent, resolvedType, callingPackage,
Philip P. Moltmanne7421e92020-02-10 16:14:12 +00001313 isRequesterInstantApp, verificationBundle, userId);
Todd Kennedye9910222017-02-21 16:00:11 -08001314 }
Todd Kennedydfc27c62017-05-17 15:32:10 -07001315 return InstantAppResolver.buildEphemeralInstallerIntent(
Patrick Baumann577d4022018-01-31 16:55:10 +00001316 originalIntent,
1317 InstantAppResolver.sanitizeIntent(originalIntent),
1318 auxiliaryResponse == null ? null : auxiliaryResponse.failureIntent,
1319 callingPackage,
1320 verificationBundle,
1321 resolvedType,
1322 userId,
1323 auxiliaryResponse == null ? null : auxiliaryResponse.installFailureActivity,
1324 auxiliaryResponse == null ? null : auxiliaryResponse.token,
1325 auxiliaryResponse != null && auxiliaryResponse.needsPhaseTwo,
1326 auxiliaryResponse == null ? null : auxiliaryResponse.filters);
Todd Kennedye9910222017-02-21 16:00:11 -08001327 }
1328
Riddle Hsu16567132018-08-16 21:37:47 +08001329 void postStartActivityProcessing(ActivityRecord r, int result,
1330 ActivityStack startedActivityStack) {
Winson Chunge219ae12019-07-18 13:43:23 -07001331 if (!ActivityManager.isStartResultSuccessful(result)) {
1332 if (mFrozeTaskList) {
1333 // If we specifically froze the task list as part of starting an activity, then
1334 // reset the frozen list state if it failed to start. This is normally otherwise
1335 // called when the freeze-timeout has elapsed.
1336 mSupervisor.mRecentTasks.resetFreezeTaskListReorderingOnTimeout();
1337 }
1338 }
Bryce Lee7f936862017-05-09 15:33:18 -07001339 if (ActivityManager.isStartResultFatalError(result)) {
Wale Ogunwalecc25a8a2016-01-23 14:31:37 -08001340 return;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001341 }
Filip Gruszczynski303210b2016-01-08 16:28:08 -08001342
Chong Zhang5022da32016-06-21 16:31:37 -07001343 // We're waiting for an activity launch to finish, but that activity simply
Bryce Lee5f0e28f2018-01-30 16:00:03 -08001344 // brought another activity to front. We must also handle the case where the task is already
1345 // 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 +08001346 // considered focused as the trampoline will be finished). Let them know about this, so
1347 // it waits for the new activity to become visible instead, {@link #waitResultIfNeeded}.
Bryce Lee5f0e28f2018-01-30 16:00:03 -08001348 mSupervisor.reportWaitingActivityLaunchedIfNeeded(r, result);
Chong Zhang5022da32016-06-21 16:31:37 -07001349
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001350 if (startedActivityStack == null) {
1351 return;
1352 }
1353
Wale Ogunwaleac36e4d2017-11-29 13:30:26 -08001354 final int clearTaskFlags = FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK;
1355 boolean clearedTask = (mLaunchFlags & clearTaskFlags) == clearTaskFlags
1356 && mReuseTask != null;
Wale Ogunwale7d7973a2018-04-05 10:25:59 -07001357 if (result == START_TASK_TO_FRONT || result == START_DELIVERED_TO_TOP || clearedTask) {
1358 // The activity was already running so it wasn't started, but either brought to the
1359 // front or the new intent was delivered to it since it was already in front. Notify
1360 // anyone interested in this piece of information.
1361 switch (startedActivityStack.getWindowingMode()) {
1362 case WINDOWING_MODE_PINNED:
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07001363 mService.getTaskChangeNotificationController().notifyPinnedActivityRestartAttempt(
Wale Ogunwale7d7973a2018-04-05 10:25:59 -07001364 clearedTask);
1365 break;
1366 case WINDOWING_MODE_SPLIT_SCREEN_PRIMARY:
Louis Changbd48dca2018-08-29 17:44:34 +08001367 final ActivityStack homeStack =
Wale Ogunwale734b8962020-01-21 12:17:42 -08001368 startedActivityStack.getDisplay().getRootHomeTask();
Wale Ogunwale7d7973a2018-04-05 10:25:59 -07001369 if (homeStack != null && homeStack.shouldBeVisible(null /* starting */)) {
1370 mService.mWindowManager.showRecentApps();
1371 }
1372 break;
1373 }
Wale Ogunwalecc25a8a2016-01-23 14:31:37 -08001374 }
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001375 }
1376
Svet Ganovcbcbf662018-05-10 17:25:29 -07001377 /**
1378 * Compute the logical UID based on which the package manager would filter
1379 * app components i.e. based on which the instant app policy would be applied
1380 * because it is the logical calling UID.
1381 *
1382 * @param customCallingUid The UID on whose behalf to make the call.
1383 * @param actualCallingUid The UID actually making the call.
Patrick Baumann31426b22018-05-21 13:46:40 -07001384 * @param filterCallingUid The UID to be used to filter for instant apps.
Svet Ganovcbcbf662018-05-10 17:25:29 -07001385 * @return The logical UID making the call.
1386 */
Patrick Baumann31426b22018-05-21 13:46:40 -07001387 static int computeResolveFilterUid(int customCallingUid, int actualCallingUid,
1388 int filterCallingUid) {
1389 return filterCallingUid != UserHandle.USER_NULL
1390 ? filterCallingUid
1391 : (customCallingUid >= 0 ? customCallingUid : actualCallingUid);
Svet Ganovcbcbf662018-05-10 17:25:29 -07001392 }
1393
Louis Chang54fbb052019-10-16 17:10:17 +08001394 /**
1395 * Start an activity while most of preliminary checks has been done and caller has been
1396 * confirmed that holds necessary permissions to do so.
1397 * Here also ensures that the starting activity is removed if the start wasn't successful.
1398 */
1399 private int startActivityUnchecked(final ActivityRecord r, ActivityRecord sourceRecord,
Bryce Leedaa91e42017-12-06 14:13:01 -08001400 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Louis Changcdec0802019-11-11 11:45:07 +08001401 int startFlags, boolean doResume, ActivityOptions options, Task inTask,
Louis Chang54fbb052019-10-16 17:10:17 +08001402 boolean restrictedBgActivity) {
Bryce Leeaa5e8c32017-03-01 16:01:06 -08001403 int result = START_CANCELED;
Riddle Hsu16567132018-08-16 21:37:47 +08001404 final ActivityStack startedActivityStack;
Bryce Leeaa5e8c32017-03-01 16:01:06 -08001405 try {
Riddle Hsua0022cd2019-09-09 21:12:41 +08001406 mService.deferWindowLayout();
Riddle Hsu2ca561b2019-10-08 21:58:58 +08001407 Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "startActivityInner");
1408 result = startActivityInner(r, sourceRecord, voiceSession, voiceInteractor,
Louis Chang54fbb052019-10-16 17:10:17 +08001409 startFlags, doResume, options, inTask, restrictedBgActivity);
Bryce Leeaa5e8c32017-03-01 16:01:06 -08001410 } finally {
Riddle Hsu2ca561b2019-10-08 21:58:58 +08001411 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
1412 startedActivityStack = handleStartResult(r, result);
Riddle Hsua0022cd2019-09-09 21:12:41 +08001413 mService.continueWindowLayout();
Bryce Leeaa5e8c32017-03-01 16:01:06 -08001414 }
1415
Riddle Hsu16567132018-08-16 21:37:47 +08001416 postStartActivityProcessing(r, result, startedActivityStack);
Bryce Leeaa5e8c32017-03-01 16:01:06 -08001417
1418 return result;
1419 }
1420
Ricky Waib147fa12019-04-25 16:08:30 +01001421 /**
Riddle Hsu2ca561b2019-10-08 21:58:58 +08001422 * If the start result is success, ensure that the configuration of the started activity matches
1423 * the current display. Otherwise clean up unassociated containers to avoid leakage.
1424 *
1425 * @return the stack where the successful started activity resides.
1426 */
1427 private @Nullable ActivityStack handleStartResult(@NonNull ActivityRecord started, int result) {
Wale Ogunwale1ebcd8e2020-01-21 11:27:03 -08001428 final ActivityStack currentStack = started.getRootTask();
Riddle Hsu2ca561b2019-10-08 21:58:58 +08001429 ActivityStack startedActivityStack = currentStack != null ? currentStack : mTargetStack;
1430
1431 if (ActivityManager.isStartResultSuccessful(result)) {
1432 if (startedActivityStack != null) {
1433 // If there is no state change (e.g. a resumed activity is reparented to top of
1434 // another display) to trigger a visibility/configuration checking, we have to
1435 // update the configuration for changing to different display.
Wale Ogunwale85fb19a2019-12-05 10:41:05 +09001436 final ActivityRecord currentTop = startedActivityStack.topRunningActivity();
Riddle Hsu2ca561b2019-10-08 21:58:58 +08001437 if (currentTop != null && currentTop.shouldUpdateConfigForDisplayChanged()) {
Louis Chang149d5c82019-12-30 09:47:39 +08001438 mRootWindowContainer.ensureVisibilityAndConfig(
Riddle Hsu2ca561b2019-10-08 21:58:58 +08001439 currentTop, currentTop.getDisplayId(),
1440 true /* markFrozenIfConfigChanged */, false /* deferResume */);
1441 }
1442 }
1443 return startedActivityStack;
1444 }
1445
1446 // If we are not able to proceed, disassociate the activity from the task. Leaving an
1447 // activity in an incomplete state can lead to issues, such as performing operations
1448 // without a window container.
Wale Ogunwale1ebcd8e2020-01-21 11:27:03 -08001449 final ActivityStack stack = mStartActivity.getRootTask();
Riddle Hsu2ca561b2019-10-08 21:58:58 +08001450 if (stack != null) {
1451 mStartActivity.finishIfPossible("startActivity", true /* oomAdj */);
1452 }
1453
1454 // Stack should also be detached from display and be removed if it's empty.
1455 if (startedActivityStack != null && startedActivityStack.isAttached()
Wale Ogunwalea38654f2019-11-17 20:37:15 -08001456 && !startedActivityStack.hasActivity()
Riddle Hsu2ca561b2019-10-08 21:58:58 +08001457 && !startedActivityStack.isActivityTypeHome()) {
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -07001458 startedActivityStack.removeIfPossible();
Riddle Hsu2ca561b2019-10-08 21:58:58 +08001459 startedActivityStack = null;
1460 }
1461 return startedActivityStack;
1462 }
1463
1464 /**
Louis Chang54fbb052019-10-16 17:10:17 +08001465 * Start an activity and determine if the activity should be adding to the top of an existing
1466 * task or delivered new intent to an existing activity. Also manipulating the activity task
1467 * onto requested or valid stack/display.
Ricky Waib147fa12019-04-25 16:08:30 +01001468 *
Louis Chang54fbb052019-10-16 17:10:17 +08001469 * Note: This method should only be called from {@link #startActivityUnchecked}.
Ricky Waib147fa12019-04-25 16:08:30 +01001470 */
Riddle Hsu2ca561b2019-10-08 21:58:58 +08001471 private int startActivityInner(final ActivityRecord r, ActivityRecord sourceRecord,
Wale Ogunwale01d66562015-12-29 08:19:19 -08001472 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Louis Changcdec0802019-11-11 11:45:07 +08001473 int startFlags, boolean doResume, ActivityOptions options, Task inTask,
Louis Chang54fbb052019-10-16 17:10:17 +08001474 boolean restrictedBgActivity) {
Wale Ogunwale01d66562015-12-29 08:19:19 -08001475 setInitialState(r, options, inTask, doResume, startFlags, sourceRecord, voiceSession,
Ricky Waib147fa12019-04-25 16:08:30 +01001476 voiceInteractor, restrictedBgActivity);
1477
Louis Chang39ba54b2018-10-18 11:28:57 +08001478 final int preferredWindowingMode = mLaunchParams.mWindowingMode;
Wale Ogunwale01d66562015-12-29 08:19:19 -08001479
Louis Chang6fb1e842018-12-03 16:07:50 +08001480 computeLaunchingTaskFlags();
1481
1482 computeSourceStack();
1483
1484 mIntent.setFlags(mLaunchFlags);
1485
Louis Changcdec0802019-11-11 11:45:07 +08001486 final Task reusedTask = getReusableTask();
Louis Chang6fb1e842018-12-03 16:07:50 +08001487
Winson Chunge219ae12019-07-18 13:43:23 -07001488 // If requested, freeze the task list
1489 if (mOptions != null && mOptions.freezeRecentTasksReordering()
1490 && mSupervisor.mRecentTasks.isCallerRecents(r.launchedFromUid)
1491 && !mSupervisor.mRecentTasks.isFreezeTaskListReorderingSet()) {
1492 mFrozeTaskList = true;
1493 mSupervisor.mRecentTasks.setFreezeTaskListReordering();
1494 }
1495
Louis Changbde91e92019-08-16 17:19:47 +08001496 // Compute if there is an existing task that should be used for.
Louis Changcdec0802019-11-11 11:45:07 +08001497 final Task targetTask = reusedTask != null ? reusedTask : computeTargetTask();
Louis Changbde91e92019-08-16 17:19:47 +08001498 final boolean newTask = targetTask == null;
1499
Louis Chang38430df2020-01-02 17:14:59 +08001500 computeLaunchParams(r, sourceRecord, targetTask);
1501
Louis Changbde91e92019-08-16 17:19:47 +08001502 // Check if starting activity on given task or on a new task is allowed.
1503 int startResult = isAllowedToStart(r, newTask, targetTask);
1504 if (startResult != START_SUCCESS) {
1505 return startResult;
Louis Changbd48dca2018-08-29 17:44:34 +08001506 }
1507
Wale Ogunwale21e06482019-11-18 05:14:15 -08001508 final ActivityRecord targetTaskTop = newTask
1509 ? null : targetTask.getTopNonFinishingActivity();
Louis Changbde91e92019-08-16 17:19:47 +08001510 if (targetTaskTop != null) {
1511 // Recycle the target task for this launch.
Louis Changf7dd7f22019-11-05 11:59:56 +08001512 startResult = recycleTask(targetTask, targetTaskTop, reusedTask);
Louis Changbde91e92019-08-16 17:19:47 +08001513 if (startResult != START_SUCCESS) {
1514 return startResult;
Wale Ogunwale01d66562015-12-29 08:19:19 -08001515 }
Louis Changf7dd7f22019-11-05 11:59:56 +08001516 } else {
1517 mAddingToTask = true;
Wale Ogunwale01d66562015-12-29 08:19:19 -08001518 }
1519
1520 // If the activity being launched is the same as the one currently at the top, then
1521 // we need to check if it should only be launched once.
Louis Chang149d5c82019-12-30 09:47:39 +08001522 final ActivityStack topStack = mRootWindowContainer.getTopDisplayFocusedStack();
Louis Changbde91e92019-08-16 17:19:47 +08001523 startResult = deliverToCurrentTopIfNeeded(topStack);
1524 if (startResult != START_SUCCESS) {
1525 return startResult;
Wale Ogunwale01d66562015-12-29 08:19:19 -08001526 }
1527
Louis Changbde91e92019-08-16 17:19:47 +08001528 if (mTargetStack == null) {
Louis Chang38430df2020-01-02 17:14:59 +08001529 mTargetStack = getLaunchStack(mStartActivity, mLaunchFlags, targetTask, mOptions);
Wale Ogunwale01d66562015-12-29 08:19:19 -08001530 }
Louis Changbde91e92019-08-16 17:19:47 +08001531 if (newTask) {
Louis Changcdec0802019-11-11 11:45:07 +08001532 final Task taskToAffiliate = (mLaunchTaskBehind && mSourceRecord != null)
1533 ? mSourceRecord.getTask() : null;
Louis Changbde91e92019-08-16 17:19:47 +08001534 setNewTask(taskToAffiliate);
Louis Changa3e6b892019-09-16 10:39:00 +08001535 if (mService.getLockTaskController().isLockTaskModeViolation(
Louis Changcdec0802019-11-11 11:45:07 +08001536 mStartActivity.getTask())) {
Louis Changa3e6b892019-09-16 10:39:00 +08001537 Slog.e(TAG, "Attempted Lock Task Mode violation mStartActivity=" + mStartActivity);
1538 return START_RETURN_LOCK_TASK_MODE_VIOLATION;
1539 }
Louis Changbde91e92019-08-16 17:19:47 +08001540 } else if (mAddingToTask) {
1541 addOrReparentStartingActivity(targetTask, "adding to task");
1542 }
1543
1544 if (!mAvoidMoveToFront && mDoResume) {
1545 mTargetStack.moveToFront("reuseOrNewTask");
Chong Zhang6cda19c2016-06-14 19:07:56 -07001546 }
Wale Ogunwale01d66562015-12-29 08:19:19 -08001547
Wale Ogunwale586a8ee2019-06-04 13:44:14 +00001548 mService.mUgmInternal.grantUriPermissionFromIntent(mCallingUid, mStartActivity.packageName,
1549 mIntent, mStartActivity.getUriPermissionsLocked(), mStartActivity.mUserId);
Patrick Baumannde37e432019-08-28 09:51:29 -07001550 mService.getPackageManagerInternalLocked().grantImplicitAccess(
Andrii Kulianeceebbf2019-06-26 17:36:51 -07001551 mStartActivity.mUserId, mIntent,
Patrick Baumannb6e72972019-09-20 07:54:47 -07001552 mCallingUid,
Patrick Baumannde37e432019-08-28 09:51:29 -07001553 UserHandle.getAppId(mStartActivity.info.applicationInfo.uid)
1554 );
Wale Ogunwale01d66562015-12-29 08:19:19 -08001555 if (newTask) {
Jeff Changd136e772019-11-05 20:33:52 +08001556 EventLogTags.writeWmCreateTask(mStartActivity.mUserId,
Louis Changcdec0802019-11-11 11:45:07 +08001557 mStartActivity.getTask().mTaskId);
Wale Ogunwale01d66562015-12-29 08:19:19 -08001558 }
Andrii Kulian79d67982019-08-19 11:56:16 -07001559 mStartActivity.logStartActivity(
Jeff Changd136e772019-11-05 20:33:52 +08001560 EventLogTags.WM_CREATE_ACTIVITY, mStartActivity.getTask());
1561
Wale Ogunwale01d66562015-12-29 08:19:19 -08001562 mTargetStack.mLastPausedActivity = null;
Wei Wang98f03f92016-05-18 11:32:52 -07001563
Louis Chang149d5c82019-12-30 09:47:39 +08001564 mRootWindowContainer.sendPowerHintForLaunchStartIfNeeded(
Wale Ogunwaled32da472018-11-16 07:19:28 -08001565 false /* forceSend */, mStartActivity);
Wei Wang98f03f92016-05-18 11:32:52 -07001566
Wale Ogunwale21e06482019-11-18 05:14:15 -08001567 mTargetStack.startActivityLocked(mStartActivity, topStack.getTopNonFinishingActivity(),
1568 newTask, mKeepCurTransition, mOptions);
Wale Ogunwale01d66562015-12-29 08:19:19 -08001569 if (mDoResume) {
Bryce Leeaf691c02017-03-20 14:20:22 -07001570 final ActivityRecord topTaskActivity =
Louis Changcdec0802019-11-11 11:45:07 +08001571 mStartActivity.getTask().topRunningActivityLocked();
Evan Rosky226de132020-01-03 18:00:29 -08001572 if (!mTargetStack.isTopActivityFocusable()
Wale Ogunwale0b3d2922019-12-30 08:55:07 -08001573 || (topTaskActivity != null && topTaskActivity.isTaskOverlay()
Wale Ogunwale68741142016-05-17 09:40:02 -07001574 && mStartActivity != topTaskActivity)) {
Filip Gruszczynski3d7fdc12016-01-31 17:33:29 -08001575 // If the activity is not focusable, we can't resume it, but still would like to
1576 // make sure it becomes visible as it starts (this will also trigger entry
1577 // animation). An example of this are PIP activities.
Wale Ogunwale3b232392016-05-13 15:37:13 -07001578 // Also, we don't want to resume activities in a task that currently has an overlay
1579 // as the starting activity just needs to be in the visible paused state until the
1580 // over is removed.
Wale Ogunwale076c3b12019-11-20 12:17:22 -08001581 mTargetStack.ensureActivitiesVisible(mStartActivity, 0, !PRESERVE_WINDOWS);
Wale Ogunwaleae846f42016-02-22 14:00:56 -08001582 // Go ahead and tell window manager to execute app transition for this activity
1583 // since the app transition will not be triggered through the resume channel.
Wale Ogunwale3a256e62018-12-06 14:41:18 -08001584 mTargetStack.getDisplay().mDisplayContent.executeAppTransition();
Wale Ogunwale3b232392016-05-13 15:37:13 -07001585 } else {
Winson Chung32066032016-11-04 11:55:21 -07001586 // If the target stack was not previously focusable (previous top running activity
1587 // on that stack was not visible) then any prior calls to move the stack to the
1588 // will not update the focused stack. If starting the new activity now allows the
1589 // task stack to be focusable, then ensure that we now update the focused stack
1590 // accordingly.
Evan Rosky226de132020-01-03 18:00:29 -08001591 if (mTargetStack.isTopActivityFocusable()
Louis Chang149d5c82019-12-30 09:47:39 +08001592 && !mRootWindowContainer.isTopDisplayFocusedStack(mTargetStack)) {
Riddle Hsu2ca561b2019-10-08 21:58:58 +08001593 mTargetStack.moveToFront("startActivityInner");
Winson Chung32066032016-11-04 11:55:21 -07001594 }
Louis Chang149d5c82019-12-30 09:47:39 +08001595 mRootWindowContainer.resumeFocusedStacksTopActivities(
Wale Ogunwaled32da472018-11-16 07:19:28 -08001596 mTargetStack, mStartActivity, mOptions);
Filip Gruszczynski3d7fdc12016-01-31 17:33:29 -08001597 }
Wale Ogunwale01d66562015-12-29 08:19:19 -08001598 }
Louis Chang149d5c82019-12-30 09:47:39 +08001599 mRootWindowContainer.updateUserStack(mStartActivity.mUserId, mTargetStack);
Wale Ogunwale01d66562015-12-29 08:19:19 -08001600
Winson Chungf45dd9f2019-10-11 15:07:47 -07001601 // Update the recent tasks list immediately when the activity starts
1602 mSupervisor.mRecentTasks.add(mStartActivity.getTask());
Louis Changcdec0802019-11-11 11:45:07 +08001603 mSupervisor.handleNonResizableTaskIfNeeded(mStartActivity.getTask(),
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001604 preferredWindowingMode, mPreferredDisplayId, mTargetStack);
Wale Ogunwale01d66562015-12-29 08:19:19 -08001605
1606 return START_SUCCESS;
1607 }
1608
Louis Changcdec0802019-11-11 11:45:07 +08001609 private Task computeTargetTask() {
Louis Changf7dd7f22019-11-05 11:59:56 +08001610 if (mStartActivity.resultTo == null && mInTask == null && !mAddingToTask
Louis Changbde91e92019-08-16 17:19:47 +08001611 && (mLaunchFlags & FLAG_ACTIVITY_NEW_TASK) != 0) {
1612 // A new task should be created instead of using existing one.
1613 return null;
1614 } else if (mSourceRecord != null) {
Louis Changcdec0802019-11-11 11:45:07 +08001615 return mSourceRecord.getTask();
Louis Changbde91e92019-08-16 17:19:47 +08001616 } else if (mInTask != null) {
1617 return mInTask;
1618 } else {
Louis Chang38430df2020-01-02 17:14:59 +08001619 final ActivityStack stack = getLaunchStack(mStartActivity, mLaunchFlags,
1620 null /* task */, mOptions);
1621 final ActivityRecord top = stack.getTopNonFinishingActivity();
Louis Changbde91e92019-08-16 17:19:47 +08001622 if (top != null) {
Louis Changcdec0802019-11-11 11:45:07 +08001623 return top.getTask();
Louis Chang38430df2020-01-02 17:14:59 +08001624 } else {
1625 // Remove the stack if no activity in the stack.
1626 stack.removeIfPossible();
Louis Changbde91e92019-08-16 17:19:47 +08001627 }
1628 }
1629 return null;
1630 }
1631
Louis Chang38430df2020-01-02 17:14:59 +08001632 private void computeLaunchParams(ActivityRecord r, ActivityRecord sourceRecord,
1633 Task targetTask) {
1634 final ActivityStack sourceStack = mSourceStack != null ? mSourceStack
1635 : mRootWindowContainer.getTopDisplayFocusedStack();
1636 if (sourceStack != null && sourceStack.inSplitScreenWindowingMode()
1637 && (mOptions == null
1638 || mOptions.getLaunchWindowingMode() == WINDOWING_MODE_UNDEFINED)) {
1639 int windowingMode =
1640 targetTask != null ? targetTask.getWindowingMode() : WINDOWING_MODE_UNDEFINED;
1641 if ((mLaunchFlags & FLAG_ACTIVITY_LAUNCH_ADJACENT) != 0) {
1642 if (sourceStack.inSplitScreenPrimaryWindowingMode()) {
1643 windowingMode = WINDOWING_MODE_SPLIT_SCREEN_SECONDARY;
1644 } else if (sourceStack.inSplitScreenSecondaryWindowingMode()) {
1645 windowingMode = WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
1646 }
1647 }
1648
1649 if (mOptions == null) {
1650 mOptions = ActivityOptions.makeBasic();
1651 }
1652 mOptions.setLaunchWindowingMode(windowingMode);
1653 }
1654
1655 mSupervisor.getLaunchParamsController().calculate(targetTask, r.info.windowLayout, r,
1656 sourceRecord, mOptions, PHASE_BOUNDS, mLaunchParams);
1657 mPreferredDisplayId =
1658 mLaunchParams.hasPreferredDisplay() ? mLaunchParams.mPreferredDisplayId
1659 : DEFAULT_DISPLAY;
1660 }
1661
Louis Changcdec0802019-11-11 11:45:07 +08001662 private int isAllowedToStart(ActivityRecord r, boolean newTask, Task targetTask) {
Louis Changbde91e92019-08-16 17:19:47 +08001663 if (mStartActivity.packageName == null) {
1664 if (mStartActivity.resultTo != null) {
1665 mStartActivity.resultTo.sendResult(INVALID_UID, mStartActivity.resultWho,
1666 mStartActivity.requestCode, RESULT_CANCELED, null /* data */);
1667 }
1668 ActivityOptions.abort(mOptions);
1669 return START_CLASS_NOT_FOUND;
1670 }
1671
1672 // Do not start home activity if it cannot be launched on preferred display. We are not
1673 // doing this in ActivityStackSupervisor#canPlaceEntityOnDisplay because it might
1674 // fallback to launch on other displays.
Louis Chang149d5c82019-12-30 09:47:39 +08001675 if (r.isActivityTypeHome() && !mRootWindowContainer.canStartHomeOnDisplay(r.info,
Louis Changbde91e92019-08-16 17:19:47 +08001676 mPreferredDisplayId, true /* allowInstrumenting */)) {
1677 Slog.w(TAG, "Cannot launch home on display " + mPreferredDisplayId);
1678 return START_CANCELED;
1679 }
1680
Wale Ogunwalea38654f2019-11-17 20:37:15 -08001681 if (mRestrictedBgActivity && (newTask || !targetTask.isUidPresent(mCallingUid))
Louis Changbde91e92019-08-16 17:19:47 +08001682 && handleBackgroundActivityAbort(mStartActivity)) {
1683 Slog.e(TAG, "Abort background activity starts from " + mCallingUid);
1684 return START_ABORTED;
1685 }
1686
1687 // When the flags NEW_TASK and CLEAR_TASK are set, then the task gets reused but still
1688 // needs to be a lock task mode violation since the task gets cleared out and the device
1689 // would otherwise leave the locked task.
1690 final boolean isNewClearTask =
1691 (mLaunchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
1692 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK);
Louis Changa3e6b892019-09-16 10:39:00 +08001693 if (!newTask && mService.getLockTaskController().isLockTaskModeViolation(targetTask,
1694 isNewClearTask)) {
Louis Changbde91e92019-08-16 17:19:47 +08001695 Slog.e(TAG, "Attempted Lock Task Mode violation mStartActivity=" + mStartActivity);
1696 return START_RETURN_LOCK_TASK_MODE_VIOLATION;
1697 }
1698
1699 return START_SUCCESS;
1700 }
1701
1702 /**
1703 * Prepare the target task to be reused for this launch, which including:
1704 * - Position the target task on valid stack on preferred display.
1705 * - Comply to the specified activity launch flags
1706 * - Determine whether need to add a new activity on top or just brought the task to front.
1707 */
Louis Chang07b13002019-11-27 22:08:37 +08001708 @VisibleForTesting
1709 int recycleTask(Task targetTask, ActivityRecord targetTaskTop, Task reusedTask) {
1710 // Should not recycle task which is from a different user, just adding the starting
1711 // activity to the task.
1712 if (targetTask.mUserId != mStartActivity.mUserId) {
1713 mTargetStack = targetTask.getStack();
1714 mAddingToTask = true;
1715 return START_SUCCESS;
1716 }
1717
Wale Ogunwaleda8b8272018-11-29 19:37:37 -08001718 boolean clearTaskForReuse = false;
Louis Changf7dd7f22019-11-05 11:59:56 +08001719 if (reusedTask != null) {
Louis Chang2787a9a2019-12-17 15:15:11 +08001720 if (mStartActivity.getTask() == null) {
Louis Changf7dd7f22019-11-05 11:59:56 +08001721 mStartActivity.setTaskForReuse(reusedTask);
Wale Ogunwaleda8b8272018-11-29 19:37:37 -08001722 clearTaskForReuse = true;
1723 }
1724
Louis Changbde91e92019-08-16 17:19:47 +08001725 if (targetTask.intent == null) {
1726 // This task was started because of movement of the activity based on
1727 // affinity...
1728 // Now that we are actually launching it, we can assign the base intent.
1729 targetTask.setIntent(mStartActivity);
1730 } else {
1731 final boolean taskOnHome =
1732 (mStartActivity.intent.getFlags() & FLAG_ACTIVITY_TASK_ON_HOME) != 0;
1733 if (taskOnHome) {
1734 targetTask.intent.addFlags(FLAG_ACTIVITY_TASK_ON_HOME);
1735 } else {
1736 targetTask.intent.removeFlags(FLAG_ACTIVITY_TASK_ON_HOME);
1737 }
1738 }
1739 }
1740
Louis Chang149d5c82019-12-30 09:47:39 +08001741 mRootWindowContainer.sendPowerHintForLaunchStartIfNeeded(false /* forceSend */,
Louis Changbde91e92019-08-16 17:19:47 +08001742 targetTaskTop);
1743
1744 setTargetStackIfNeeded(targetTaskTop);
1745
Louis Changbde91e92019-08-16 17:19:47 +08001746 // When there is a reused activity and the current result is a trampoline activity,
1747 // set the reused activity as the result.
Louis Chang54fbb052019-10-16 17:10:17 +08001748 if (mLastStartActivityRecord != null
1749 && (mLastStartActivityRecord.finishing || mLastStartActivityRecord.noDisplay)) {
1750 mLastStartActivityRecord = targetTaskTop;
Louis Changbde91e92019-08-16 17:19:47 +08001751 }
1752
1753 if ((mStartFlags & START_FLAG_ONLY_IF_NEEDED) != 0) {
1754 // We don't need to start a new activity, and the client said not to do anything
1755 // if that is the case, so this is it! And for paranoia, make sure we have
1756 // correctly resumed the top activity.
1757 if (!mMovedToFront && mDoResume) {
1758 if (DEBUG_TASKS) {
1759 Slog.d(TAG_TASKS, "Bring to front target: " + mTargetStack
1760 + " from " + targetTaskTop);
1761 }
1762 mTargetStack.moveToFront("intentActivityFound");
1763 }
1764 resumeTargetStackIfNeeded();
1765 return START_RETURN_INTENT_TO_CALLER;
1766 }
1767
Wale Ogunwale21e06482019-11-18 05:14:15 -08001768 complyActivityFlags(targetTask,
1769 reusedTask != null ? reusedTask.getTopNonFinishingActivity() : null);
Louis Changbde91e92019-08-16 17:19:47 +08001770
Wale Ogunwaleda8b8272018-11-29 19:37:37 -08001771 if (clearTaskForReuse) {
1772 // Clear task for re-use so later code to methods
1773 // {@link #setTaskFromReuseOrCreateNewTask}, {@link #setTaskFromSourceRecord}, or
1774 // {@link #setTaskFromInTask} can parent it to the task.
1775 mStartActivity.setTaskForReuse(null);
1776 }
1777
Louis Changbde91e92019-08-16 17:19:47 +08001778 if (mAddingToTask) {
1779 return START_SUCCESS;
1780 }
1781
Louis Chang3ee5fc02019-09-23 11:32:10 +08001782 if (mMovedToFront) {
1783 // We moved the task to front, use starting window to hide initial drawn delay.
1784 targetTaskTop.showStartingWindow(null /* prev */, false /* newTask */,
1785 true /* taskSwitch */);
1786 } else if (mDoResume) {
1787 // Make sure the stack and its belonging display are moved to topmost.
Louis Changbde91e92019-08-16 17:19:47 +08001788 mTargetStack.moveToFront("intentActivityFound");
1789 }
1790 // We didn't do anything... but it was needed (a.k.a., client don't use that intent!)
1791 // And for paranoia, make sure we have correctly resumed the top activity.
1792 resumeTargetStackIfNeeded();
Louis Chang54fbb052019-10-16 17:10:17 +08001793 // The reusedActivity could be finishing, for example of starting an activity with
1794 // FLAG_ACTIVITY_CLEAR_TOP flag. In that case, return the top running activity in the
1795 // task instead.
1796 mLastStartActivityRecord =
Wale Ogunwale21e06482019-11-18 05:14:15 -08001797 targetTaskTop.finishing ? targetTask.getTopNonFinishingActivity() : targetTaskTop;
Louis Changbde91e92019-08-16 17:19:47 +08001798 return mMovedToFront ? START_TASK_TO_FRONT : START_DELIVERED_TO_TOP;
1799 }
1800
1801 /**
1802 * Check if the activity being launched is the same as the one currently at the top and it
1803 * should only be launched once.
1804 */
1805 private int deliverToCurrentTopIfNeeded(ActivityStack topStack) {
1806 final ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(mNotTop);
1807 final boolean dontStart = top != null && mStartActivity.resultTo == null
1808 && top.mActivityComponent.equals(mStartActivity.mActivityComponent)
1809 && top.mUserId == mStartActivity.mUserId
1810 && top.attachedToProcess()
1811 && ((mLaunchFlags & FLAG_ACTIVITY_SINGLE_TOP) != 0
1812 || isLaunchModeOneOf(LAUNCH_SINGLE_TOP, LAUNCH_SINGLE_TASK))
1813 // This allows home activity to automatically launch on secondary display when
1814 // display added, if home was the top activity on default display, instead of
1815 // sending new intent to the home activity on default display.
1816 && (!top.isActivityTypeHome() || top.getDisplayId() == mPreferredDisplayId);
1817 if (!dontStart) {
1818 return START_SUCCESS;
1819 }
1820
1821 // For paranoia, make sure we have correctly resumed the top activity.
1822 topStack.mLastPausedActivity = null;
1823 if (mDoResume) {
Louis Chang149d5c82019-12-30 09:47:39 +08001824 mRootWindowContainer.resumeFocusedStacksTopActivities();
Louis Changbde91e92019-08-16 17:19:47 +08001825 }
1826 ActivityOptions.abort(mOptions);
1827 if ((mStartFlags & START_FLAG_ONLY_IF_NEEDED) != 0) {
1828 // We don't need to start a new activity, and the client said not to do anything if
1829 // that is the case, so this is it!
1830 return START_RETURN_INTENT_TO_CALLER;
1831 }
1832
1833 deliverNewIntent(top);
1834
1835 // Don't use mStartActivity.task to show the toast. We're not starting a new activity but
1836 // reusing 'top'. Fields in mStartActivity may not be fully initialized.
Louis Changcdec0802019-11-11 11:45:07 +08001837 mSupervisor.handleNonResizableTaskIfNeeded(top.getTask(),
Louis Changbde91e92019-08-16 17:19:47 +08001838 mLaunchParams.mWindowingMode, mPreferredDisplayId, topStack);
1839
1840 return START_DELIVERED_TO_TOP;
1841 }
1842
1843 /**
1844 * Applying the launching flags to the task, which might clear few or all the activities in the
1845 * task.
1846 */
Louis Changcdec0802019-11-11 11:45:07 +08001847 private void complyActivityFlags(Task targetTask, ActivityRecord reusedActivity) {
Wale Ogunwale21e06482019-11-18 05:14:15 -08001848 ActivityRecord targetTaskTop = targetTask.getTopNonFinishingActivity();
Louis Chang2dcb1272019-09-27 15:01:19 +08001849 final boolean resetTask =
1850 reusedActivity != null && (mLaunchFlags & FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0;
1851 if (resetTask) {
Wale Ogunwaledfbeed72019-11-20 08:57:39 -08001852 targetTaskTop = mTargetStack.resetTaskIfNeeded(targetTaskTop, mStartActivity);
Louis Changbde91e92019-08-16 17:19:47 +08001853 }
1854
1855 if ((mLaunchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
1856 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK)) {
1857 // The caller has requested to completely replace any existing task with its new
1858 // activity. Well that should not be too hard...
Louis Changcdec0802019-11-11 11:45:07 +08001859 // Note: we must persist the {@link Task} first as intentActivity could be
Louis Changbde91e92019-08-16 17:19:47 +08001860 // removed from calling performClearTaskLocked (For example, if it is being brought out
1861 // of history or if it is finished immediately), thus disassociating the task. Also note
Louis Changcdec0802019-11-11 11:45:07 +08001862 // that mReuseTask is reset as a result of {@link Task#performClearTaskLocked}
Louis Changbde91e92019-08-16 17:19:47 +08001863 // launching another activity.
1864 // TODO(b/36119896): We shouldn't trigger activity launches in this path since we are
1865 // already launching one.
1866 targetTask.performClearTaskLocked();
1867 targetTask.setIntent(mStartActivity);
1868 mAddingToTask = true;
1869 } else if ((mLaunchFlags & FLAG_ACTIVITY_CLEAR_TOP) != 0
1870 || isDocumentLaunchesIntoExisting(mLaunchFlags)
1871 || isLaunchModeOneOf(LAUNCH_SINGLE_INSTANCE, LAUNCH_SINGLE_TASK)) {
1872 // In this situation we want to remove all activities from the task up to the one
1873 // being started. In most cases this means we are resetting the task to its initial
1874 // state.
1875 final ActivityRecord top = targetTask.performClearTaskForReuseLocked(mStartActivity,
1876 mLaunchFlags);
1877
1878 // The above code can remove {@code reusedActivity} from the task, leading to the
Louis Changcdec0802019-11-11 11:45:07 +08001879 // {@code ActivityRecord} removing its reference to the {@code Task}. The task
Wale Ogunwalec17418e2019-10-13 23:00:40 +02001880 // reference is needed in the call below to {@link setTargetStackAndMoveToFrontIfNeeded}
Louis Changcdec0802019-11-11 11:45:07 +08001881 if (targetTaskTop.getTask() == null) {
Wale Ogunwale2322bed2019-10-10 17:24:19 +02001882 targetTask.addChild(targetTaskTop);
Louis Changbde91e92019-08-16 17:19:47 +08001883 }
1884
1885 if (top != null) {
1886 if (top.isRootOfTask()) {
1887 // Activity aliases may mean we use different intents for the top activity,
1888 // so make sure the task now has the identity of the new intent.
Louis Changcdec0802019-11-11 11:45:07 +08001889 top.getTask().setIntent(mStartActivity);
Louis Changbde91e92019-08-16 17:19:47 +08001890 }
1891 deliverNewIntent(top);
1892 } else {
1893 // A special case: we need to start the activity because it is not currently
1894 // running, and the caller has asked to clear the current task to have this
1895 // activity at the top.
1896 mAddingToTask = true;
1897 if (targetTask.getStack() == null) {
1898 // Target stack got cleared when we all activities were removed above.
1899 // Go ahead and reset it.
Louis Chang38430df2020-01-02 17:14:59 +08001900 mTargetStack =
1901 getLaunchStack(mStartActivity, mLaunchFlags, null /* task */, mOptions);
Wale Ogunwale2322bed2019-10-10 17:24:19 +02001902 mTargetStack.addChild(targetTask, !mLaunchTaskBehind /* toTop */,
1903 (mStartActivity.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
Louis Changbde91e92019-08-16 17:19:47 +08001904 }
1905 }
1906 } else if ((mLaunchFlags & FLAG_ACTIVITY_CLEAR_TOP) == 0 && !mAddingToTask
1907 && (mLaunchFlags & FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
1908 // In this case, we are launching an activity in our own task that may
1909 // already be running somewhere in the history, and we want to shuffle it to
1910 // the front of the stack if so.
Wale Ogunwalea38654f2019-11-17 20:37:15 -08001911 final ActivityRecord act =
1912 targetTask.findActivityInHistory(mStartActivity.mActivityComponent);
Louis Changbde91e92019-08-16 17:19:47 +08001913 if (act != null) {
Louis Changcdec0802019-11-11 11:45:07 +08001914 final Task task = act.getTask();
Louis Changbde91e92019-08-16 17:19:47 +08001915 task.moveActivityToFrontLocked(act);
1916 act.updateOptionsLocked(mOptions);
1917 deliverNewIntent(act);
1918 mTargetStack.mLastPausedActivity = null;
1919 } else {
1920 mAddingToTask = true;
1921 }
1922 } else if (mStartActivity.mActivityComponent.equals(targetTask.realActivity)) {
Louis Chang382419b2019-10-03 21:43:38 +08001923 if (targetTask == mInTask) {
1924 // In this case we are bringing up an existing activity from a recent task. We
1925 // don't need to add a new activity instance on top.
1926 } else if (((mLaunchFlags & FLAG_ACTIVITY_SINGLE_TOP) != 0
1927 || LAUNCH_SINGLE_TOP == mLaunchMode)
1928 && targetTaskTop.mActivityComponent.equals(mStartActivity.mActivityComponent)
1929 && mStartActivity.resultTo == null) {
1930 // In this case the top activity on the task is the same as the one being launched,
1931 // so we take that as a request to bring the task to the foreground. If the top
1932 // activity in the task is the root activity, deliver this new intent to it if it
1933 // desires.
Louis Changbde91e92019-08-16 17:19:47 +08001934 if (targetTaskTop.isRootOfTask()) {
Louis Changcdec0802019-11-11 11:45:07 +08001935 targetTaskTop.getTask().setIntent(mStartActivity);
Louis Changbde91e92019-08-16 17:19:47 +08001936 }
1937 deliverNewIntent(targetTaskTop);
1938 } else if (!targetTask.isSameIntentFilter(mStartActivity)) {
1939 // In this case we are launching the root activity of the task, but with a
1940 // different intent. We should start a new instance on top.
1941 mAddingToTask = true;
1942 } else if (reusedActivity == null) {
1943 mAddingToTask = true;
1944 }
Louis Chang2dcb1272019-09-27 15:01:19 +08001945 } else if (!resetTask) {
Louis Changbde91e92019-08-16 17:19:47 +08001946 // In this case an activity is being launched in to an existing task, without
1947 // resetting that task. This is typically the situation of launching an activity
1948 // from a notification or shortcut. We want to place the new activity on top of the
1949 // current task.
1950 mAddingToTask = true;
1951 } else if (!targetTask.rootWasReset) {
1952 // In this case we are launching into an existing task that has not yet been started
1953 // from its front door. The current task has been brought to the front. Ideally,
1954 // we'd probably like to place this new task at the bottom of its stack, but that's
1955 // a little hard to do with the current organization of the code so for now we'll
1956 // just drop it.
1957 targetTask.setIntent(mStartActivity);
1958 }
1959 }
1960
Bryce Leedaa91e42017-12-06 14:13:01 -08001961 /**
1962 * Resets the {@link ActivityStarter} state.
1963 * @param clearRequest whether the request should be reset to default values.
1964 */
1965 void reset(boolean clearRequest) {
1966 mStartActivity = null;
1967 mIntent = null;
1968 mCallingUid = -1;
1969 mOptions = null;
Ricky Waib147fa12019-04-25 16:08:30 +01001970 mRestrictedBgActivity = false;
Bryce Leedaa91e42017-12-06 14:13:01 -08001971
1972 mLaunchTaskBehind = false;
1973 mLaunchFlags = 0;
1974 mLaunchMode = INVALID_LAUNCH_MODE;
1975
Bryce Leeec55eb02017-12-05 20:51:27 -08001976 mLaunchParams.reset();
Bryce Leedaa91e42017-12-06 14:13:01 -08001977
1978 mNotTop = null;
1979 mDoResume = false;
1980 mStartFlags = 0;
1981 mSourceRecord = null;
1982 mPreferredDisplayId = INVALID_DISPLAY;
1983
1984 mInTask = null;
1985 mAddingToTask = false;
1986 mReuseTask = null;
1987
1988 mNewTaskInfo = null;
1989 mNewTaskIntent = null;
1990 mSourceStack = null;
1991
1992 mTargetStack = null;
1993 mMovedToFront = false;
1994 mNoAnimation = false;
1995 mKeepCurTransition = false;
1996 mAvoidMoveToFront = false;
Winson Chunge219ae12019-07-18 13:43:23 -07001997 mFrozeTaskList = false;
Bryce Leedaa91e42017-12-06 14:13:01 -08001998
1999 mVoiceSession = null;
2000 mVoiceInteractor = null;
2001
2002 mIntentDelivered = false;
2003
2004 if (clearRequest) {
2005 mRequest.reset();
2006 }
2007 }
2008
Louis Changcdec0802019-11-11 11:45:07 +08002009 private void setInitialState(ActivityRecord r, ActivityOptions options, Task inTask,
Wale Ogunwale01d66562015-12-29 08:19:19 -08002010 boolean doResume, int startFlags, ActivityRecord sourceRecord,
Ricky Waib147fa12019-04-25 16:08:30 +01002011 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
2012 boolean restrictedBgActivity) {
Bryce Leedaa91e42017-12-06 14:13:01 -08002013 reset(false /* clearRequest */);
2014
Wale Ogunwale01d66562015-12-29 08:19:19 -08002015 mStartActivity = r;
2016 mIntent = r.intent;
2017 mOptions = options;
2018 mCallingUid = r.launchedFromUid;
2019 mSourceRecord = sourceRecord;
2020 mVoiceSession = voiceSession;
2021 mVoiceInteractor = voiceInteractor;
Ricky Waib147fa12019-04-25 16:08:30 +01002022 mRestrictedBgActivity = restrictedBgActivity;
Wale Ogunwale01d66562015-12-29 08:19:19 -08002023
Bryce Leeec55eb02017-12-05 20:51:27 -08002024 mLaunchParams.reset();
Bryce Leedacefc42017-10-10 12:56:02 -07002025
Louis Chang6fb1e842018-12-03 16:07:50 +08002026 // Preferred display id is the only state we need for now and it could be updated again
2027 // after we located a reusable task (which might be resided in another display).
Garfield Tan706dbcb2018-10-15 11:33:02 -07002028 mSupervisor.getLaunchParamsController().calculate(inTask, r.info.windowLayout, r,
Louis Chang6fb1e842018-12-03 16:07:50 +08002029 sourceRecord, options, PHASE_DISPLAY, mLaunchParams);
2030 mPreferredDisplayId =
2031 mLaunchParams.hasPreferredDisplay() ? mLaunchParams.mPreferredDisplayId
2032 : DEFAULT_DISPLAY;
Garfield Tanb5cc09f2018-09-28 10:06:52 -07002033
Bryce Lee7daee392017-10-12 13:46:18 -07002034 mLaunchMode = r.launchMode;
2035
Wale Ogunwale01d66562015-12-29 08:19:19 -08002036 mLaunchFlags = adjustLaunchFlagsToDocumentMode(
Bryce Lee7daee392017-10-12 13:46:18 -07002037 r, LAUNCH_SINGLE_INSTANCE == mLaunchMode,
2038 LAUNCH_SINGLE_TASK == mLaunchMode, mIntent.getFlags());
Wale Ogunwale01d66562015-12-29 08:19:19 -08002039 mLaunchTaskBehind = r.mLaunchTaskBehind
Bryce Lee7daee392017-10-12 13:46:18 -07002040 && !isLaunchModeOneOf(LAUNCH_SINGLE_TASK, LAUNCH_SINGLE_INSTANCE)
Wale Ogunwale01d66562015-12-29 08:19:19 -08002041 && (mLaunchFlags & FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
2042
2043 sendNewTaskResultRequestIfNeeded();
2044
2045 if ((mLaunchFlags & FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
2046 mLaunchFlags |= FLAG_ACTIVITY_NEW_TASK;
2047 }
2048
2049 // If we are actually going to launch in to a new task, there are some cases where
2050 // we further want to do multiple task.
2051 if ((mLaunchFlags & FLAG_ACTIVITY_NEW_TASK) != 0) {
2052 if (mLaunchTaskBehind
2053 || r.info.documentLaunchMode == DOCUMENT_LAUNCH_ALWAYS) {
2054 mLaunchFlags |= FLAG_ACTIVITY_MULTIPLE_TASK;
2055 }
2056 }
2057
2058 // We'll invoke onUserLeaving before onPause only if the launching
2059 // activity did not explicitly state that this is an automated launch.
2060 mSupervisor.mUserLeaving = (mLaunchFlags & FLAG_ACTIVITY_NO_USER_ACTION) == 0;
2061 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
2062 "startActivity() => mUserLeaving=" + mSupervisor.mUserLeaving);
2063
2064 // If the caller has asked not to resume at this point, we make note
2065 // of this in the record so that we can skip it when trying to find
2066 // the top running activity.
2067 mDoResume = doResume;
Louis Chang37317152019-05-09 09:53:58 +08002068 if (!doResume || !r.okToShowLocked() || mLaunchTaskBehind) {
Wale Ogunwale01d66562015-12-29 08:19:19 -08002069 r.delayedResume = true;
2070 mDoResume = false;
2071 }
2072
Winson Chunge2d72172018-01-25 17:46:20 +00002073 if (mOptions != null) {
2074 if (mOptions.getLaunchTaskId() != -1 && mOptions.getTaskOverlay()) {
Wale Ogunwale0b3d2922019-12-30 08:55:07 -08002075 r.setTaskOverlay(true);
Winson Chunge2d72172018-01-25 17:46:20 +00002076 if (!mOptions.canTaskOverlayResume()) {
Louis Chang149d5c82019-12-30 09:47:39 +08002077 final Task task = mRootWindowContainer.anyTaskForId(
Winson Chunge2d72172018-01-25 17:46:20 +00002078 mOptions.getLaunchTaskId());
Wale Ogunwale21e06482019-11-18 05:14:15 -08002079 final ActivityRecord top = task != null
2080 ? task.getTopNonFinishingActivity() : null;
Bryce Lee7ace3952018-02-16 14:34:32 -08002081 if (top != null && !top.isState(RESUMED)) {
Jorim Jaggic875ae72016-04-26 22:41:06 -07002082
Winson Chunge2d72172018-01-25 17:46:20 +00002083 // The caller specifies that we'd like to be avoided to be moved to the
2084 // front, so be it!
2085 mDoResume = false;
2086 mAvoidMoveToFront = true;
2087 }
Winson Chungcbcadc92017-01-12 15:54:12 -08002088 }
Winson Chunge2d72172018-01-25 17:46:20 +00002089 } else if (mOptions.getAvoidMoveToFront()) {
Winson Chungba40d3a2018-05-16 09:40:16 -07002090 mDoResume = false;
Winson Chunge2d72172018-01-25 17:46:20 +00002091 mAvoidMoveToFront = true;
Jorim Jaggic875ae72016-04-26 22:41:06 -07002092 }
2093 }
2094
Louis Chang2f4e9b462019-03-05 16:43:15 +08002095 mNotTop = (mLaunchFlags & FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? sourceRecord : null;
Wale Ogunwale01d66562015-12-29 08:19:19 -08002096
2097 mInTask = inTask;
2098 // In some flows in to this function, we retrieve the task record and hold on to it
2099 // without a lock before calling back in to here... so the task at this point may
2100 // not actually be in recents. Check for that, and if it isn't in recents just
2101 // consider it invalid.
2102 if (inTask != null && !inTask.inRecents) {
2103 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
2104 mInTask = null;
2105 }
2106
2107 mStartFlags = startFlags;
2108 // If the onlyIfNeeded flag is set, then we can do this if the activity being launched
2109 // is the same as the one making the call... or, as a special case, if we do not know
2110 // the caller then we count the current top activity as the caller.
2111 if ((startFlags & START_FLAG_ONLY_IF_NEEDED) != 0) {
2112 ActivityRecord checkedCaller = sourceRecord;
2113 if (checkedCaller == null) {
Louis Chang149d5c82019-12-30 09:47:39 +08002114 checkedCaller = mRootWindowContainer.getTopDisplayFocusedStack()
Andrii Kulian5f750bc2018-07-17 08:57:23 -07002115 .topRunningNonDelayedActivityLocked(mNotTop);
Wale Ogunwale01d66562015-12-29 08:19:19 -08002116 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002117 if (!checkedCaller.mActivityComponent.equals(r.mActivityComponent)) {
Wale Ogunwale01d66562015-12-29 08:19:19 -08002118 // Caller is not the same as launcher, so always needed.
2119 mStartFlags &= ~START_FLAG_ONLY_IF_NEEDED;
2120 }
2121 }
2122
2123 mNoAnimation = (mLaunchFlags & FLAG_ACTIVITY_NO_ANIMATION) != 0;
Ricky Waib147fa12019-04-25 16:08:30 +01002124
Alan Stokes07389b62019-05-20 15:22:54 +01002125 if (mRestrictedBgActivity && !mService.isBackgroundActivityStartsEnabled()) {
Ricky Waib147fa12019-04-25 16:08:30 +01002126 mAvoidMoveToFront = true;
2127 mDoResume = false;
2128 }
Wale Ogunwale01d66562015-12-29 08:19:19 -08002129 }
2130
2131 private void sendNewTaskResultRequestIfNeeded() {
Andrii Kulian79d67982019-08-19 11:56:16 -07002132 if (mStartActivity.resultTo != null && (mLaunchFlags & FLAG_ACTIVITY_NEW_TASK) != 0) {
Wale Ogunwale01d66562015-12-29 08:19:19 -08002133 // For whatever reason this activity is being launched into a new task...
2134 // yet the caller has requested a result back. Well, that is pretty messed up,
2135 // so instead immediately send back a cancel and let the new task continue launched
2136 // as normal without a dependency on its originator.
2137 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
Andrii Kulian79d67982019-08-19 11:56:16 -07002138 mStartActivity.resultTo.sendResult(INVALID_UID, mStartActivity.resultWho,
2139 mStartActivity.requestCode, RESULT_CANCELED, null /* data */);
Wale Ogunwale01d66562015-12-29 08:19:19 -08002140 mStartActivity.resultTo = null;
2141 }
2142 }
2143
2144 private void computeLaunchingTaskFlags() {
2145 // If the caller is not coming from another activity, but has given us an explicit task into
2146 // which they would like us to launch the new activity, then let's see about doing that.
Andrii Kulian02b7a832016-10-06 23:11:56 -07002147 if (mSourceRecord == null && mInTask != null && mInTask.getStack() != null) {
Wale Ogunwale01d66562015-12-29 08:19:19 -08002148 final Intent baseIntent = mInTask.getBaseIntent();
2149 final ActivityRecord root = mInTask.getRootActivity();
2150 if (baseIntent == null) {
2151 ActivityOptions.abort(mOptions);
2152 throw new IllegalArgumentException("Launching into task without base intent: "
2153 + mInTask);
2154 }
2155
2156 // If this task is empty, then we are adding the first activity -- it
2157 // determines the root, and must be launching as a NEW_TASK.
Bryce Lee7daee392017-10-12 13:46:18 -07002158 if (isLaunchModeOneOf(LAUNCH_SINGLE_INSTANCE, LAUNCH_SINGLE_TASK)) {
Wale Ogunwale01d66562015-12-29 08:19:19 -08002159 if (!baseIntent.getComponent().equals(mStartActivity.intent.getComponent())) {
2160 ActivityOptions.abort(mOptions);
2161 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
2162 + mStartActivity + " into different task " + mInTask);
2163 }
2164 if (root != null) {
2165 ActivityOptions.abort(mOptions);
2166 throw new IllegalArgumentException("Caller with mInTask " + mInTask
2167 + " has root " + root + " but target is singleInstance/Task");
2168 }
2169 }
2170
2171 // If task is empty, then adopt the interesting intent launch flags in to the
2172 // activity being started.
2173 if (root == null) {
2174 final int flagsOfInterest = FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_MULTIPLE_TASK
2175 | FLAG_ACTIVITY_NEW_DOCUMENT | FLAG_ACTIVITY_RETAIN_IN_RECENTS;
2176 mLaunchFlags = (mLaunchFlags & ~flagsOfInterest)
2177 | (baseIntent.getFlags() & flagsOfInterest);
2178 mIntent.setFlags(mLaunchFlags);
2179 mInTask.setIntent(mStartActivity);
2180 mAddingToTask = true;
2181
2182 // If the task is not empty and the caller is asking to start it as the root of
2183 // a new task, then we don't actually want to start this on the task. We will
2184 // bring the task to the front, and possibly give it a new intent.
2185 } else if ((mLaunchFlags & FLAG_ACTIVITY_NEW_TASK) != 0) {
2186 mAddingToTask = false;
2187
2188 } else {
2189 mAddingToTask = true;
2190 }
2191
2192 mReuseTask = mInTask;
2193 } else {
2194 mInTask = null;
2195 // Launch ResolverActivity in the source task, so that it stays in the task bounds
2196 // when in freeform workspace.
2197 // Also put noDisplay activities in the source task. These by itself can be placed
2198 // in any task/stack, however it could launch other activities like ResolverActivity,
2199 // and we want those to stay in the original task.
Louis Chang6a9be162019-07-15 10:41:32 +08002200 if ((mStartActivity.isResolverOrDelegateActivity() || mStartActivity.noDisplay)
2201 && mSourceRecord != null && mSourceRecord.inFreeformWindowingMode()) {
Wale Ogunwale01d66562015-12-29 08:19:19 -08002202 mAddingToTask = true;
2203 }
2204 }
2205
2206 if (mInTask == null) {
2207 if (mSourceRecord == null) {
2208 // This activity is not being started from another... in this
2209 // case we -always- start a new task.
2210 if ((mLaunchFlags & FLAG_ACTIVITY_NEW_TASK) == 0 && mInTask == null) {
2211 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
2212 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + mIntent);
2213 mLaunchFlags |= FLAG_ACTIVITY_NEW_TASK;
2214 }
2215 } else if (mSourceRecord.launchMode == LAUNCH_SINGLE_INSTANCE) {
2216 // The original activity who is starting us is running as a single
2217 // instance... this new activity it is starting must go on its
2218 // own task.
2219 mLaunchFlags |= FLAG_ACTIVITY_NEW_TASK;
Bryce Lee7daee392017-10-12 13:46:18 -07002220 } else if (isLaunchModeOneOf(LAUNCH_SINGLE_INSTANCE, LAUNCH_SINGLE_TASK)) {
Wale Ogunwale01d66562015-12-29 08:19:19 -08002221 // The activity being started is a single instance... it always
2222 // gets launched into its own task.
2223 mLaunchFlags |= FLAG_ACTIVITY_NEW_TASK;
2224 }
2225 }
2226 }
2227
2228 private void computeSourceStack() {
2229 if (mSourceRecord == null) {
2230 mSourceStack = null;
2231 return;
2232 }
2233 if (!mSourceRecord.finishing) {
Wale Ogunwale1ebcd8e2020-01-21 11:27:03 -08002234 mSourceStack = mSourceRecord.getRootTask();
Wale Ogunwale01d66562015-12-29 08:19:19 -08002235 return;
2236 }
2237
2238 // If the source is finishing, we can't further count it as our source. This is because the
2239 // task it is associated with may now be empty and on its way out, so we don't want to
2240 // blindly throw it in to that task. Instead we will take the NEW_TASK flow and try to find
2241 // a task for it. But save the task information so it can be used when creating the new task.
2242 if ((mLaunchFlags & FLAG_ACTIVITY_NEW_TASK) == 0) {
2243 Slog.w(TAG, "startActivity called from finishing " + mSourceRecord
2244 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + mIntent);
2245 mLaunchFlags |= FLAG_ACTIVITY_NEW_TASK;
2246 mNewTaskInfo = mSourceRecord.info;
Bryce Leed9ed45d2017-05-22 15:57:24 -07002247
2248 // It is not guaranteed that the source record will have a task associated with it. For,
2249 // example, if this method is being called for processing a pending activity launch, it
2250 // is possible that the activity has been removed from the task after the launch was
2251 // enqueued.
Louis Changcdec0802019-11-11 11:45:07 +08002252 final Task sourceTask = mSourceRecord.getTask();
Bryce Leed9ed45d2017-05-22 15:57:24 -07002253 mNewTaskIntent = sourceTask != null ? sourceTask.intent : null;
Wale Ogunwale01d66562015-12-29 08:19:19 -08002254 }
2255 mSourceRecord = null;
2256 mSourceStack = null;
2257 }
2258
2259 /**
2260 * Decide whether the new activity should be inserted into an existing task. Returns null
2261 * if not or an ActivityRecord with the task into which the new activity should be added.
2262 */
Louis Changcdec0802019-11-11 11:45:07 +08002263 private Task getReusableTask() {
Wale Ogunwale01d66562015-12-29 08:19:19 -08002264 // We may want to try to place the new activity in to an existing task. We always
2265 // do this if the target activity is singleTask or singleInstance; we will also do
2266 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
2267 // us to still place it in a new task: multi task, always doc mode, or being asked to
2268 // launch this as a new task behind the current one.
2269 boolean putIntoExistingTask = ((mLaunchFlags & FLAG_ACTIVITY_NEW_TASK) != 0 &&
2270 (mLaunchFlags & FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Bryce Lee7daee392017-10-12 13:46:18 -07002271 || isLaunchModeOneOf(LAUNCH_SINGLE_INSTANCE, LAUNCH_SINGLE_TASK);
Wale Ogunwale01d66562015-12-29 08:19:19 -08002272 // If bring to front is requested, and no result is requested and we have not been given
2273 // an explicit task to launch in to, and we can find a task that was started with this
2274 // same component, then instead of launching bring that one to the front.
2275 putIntoExistingTask &= mInTask == null && mStartActivity.resultTo == null;
2276 ActivityRecord intentActivity = null;
Jorim Jaggi2adba072016-03-03 13:43:39 +01002277 if (mOptions != null && mOptions.getLaunchTaskId() != -1) {
Louis Chang149d5c82019-12-30 09:47:39 +08002278 Task launchTask = mRootWindowContainer.anyTaskForId(mOptions.getLaunchTaskId());
Louis Changf7dd7f22019-11-05 11:59:56 +08002279 if (launchTask != null) {
2280 return launchTask;
2281 }
Jorim Jaggi2adba072016-03-03 13:43:39 +01002282 } else if (putIntoExistingTask) {
Bryce Lee7daee392017-10-12 13:46:18 -07002283 if (LAUNCH_SINGLE_INSTANCE == mLaunchMode) {
Wale Ogunwale13dbfff2016-05-20 08:50:15 -07002284 // There can be one and only one instance of single instance activity in the
2285 // history, and it is always in its own unique task, so we do a special search.
Louis Chang149d5c82019-12-30 09:47:39 +08002286 intentActivity = mRootWindowContainer.findActivity(mIntent, mStartActivity.info,
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002287 mStartActivity.isActivityTypeHome());
Wale Ogunwale13dbfff2016-05-20 08:50:15 -07002288 } else if ((mLaunchFlags & FLAG_ACTIVITY_LAUNCH_ADJACENT) != 0) {
2289 // For the launch adjacent case we only want to put the activity in an existing
2290 // task if the activity already exists in the history.
Louis Chang149d5c82019-12-30 09:47:39 +08002291 intentActivity = mRootWindowContainer.findActivity(mIntent, mStartActivity.info,
Bryce Lee7daee392017-10-12 13:46:18 -07002292 !(LAUNCH_SINGLE_TASK == mLaunchMode));
Wale Ogunwale13dbfff2016-05-20 08:50:15 -07002293 } else {
2294 // Otherwise find the best task to put the activity in.
Wale Ogunwaled32da472018-11-16 07:19:28 -08002295 intentActivity =
Louis Chang149d5c82019-12-30 09:47:39 +08002296 mRootWindowContainer.findTask(mStartActivity, mPreferredDisplayId);
Wale Ogunwale13dbfff2016-05-20 08:50:15 -07002297 }
Wale Ogunwale01d66562015-12-29 08:19:19 -08002298 }
Louis Changbd48dca2018-08-29 17:44:34 +08002299
Louis Chang54506cb2018-11-23 11:03:41 +08002300 if (intentActivity != null
2301 && (mStartActivity.isActivityTypeHome() || intentActivity.isActivityTypeHome())
Louis Changbd48dca2018-08-29 17:44:34 +08002302 && intentActivity.getDisplayId() != mPreferredDisplayId) {
2303 // Do not reuse home activity on other displays.
2304 intentActivity = null;
2305 }
2306
Louis Changcdec0802019-11-11 11:45:07 +08002307 return intentActivity != null ? intentActivity.getTask() : null;
Wale Ogunwale01d66562015-12-29 08:19:19 -08002308 }
2309
Andrii Kulianfab9cd82017-03-21 19:37:09 -07002310 /**
2311 * Figure out which task and activity to bring to front when we have found an existing matching
2312 * activity record in history. May also clear the task if needed.
2313 * @param intentActivity Existing matching activity.
2314 * @return {@link ActivityRecord} brought to front.
2315 */
Louis Changbde91e92019-08-16 17:19:47 +08002316 private void setTargetStackIfNeeded(ActivityRecord intentActivity) {
Wale Ogunwale1ebcd8e2020-01-21 11:27:03 -08002317 mTargetStack = intentActivity.getRootTask();
Wale Ogunwale01d66562015-12-29 08:19:19 -08002318 mTargetStack.mLastPausedActivity = null;
2319 // If the target task is not in the front, then we need to bring it to the front...
2320 // except... well, with SINGLE_TASK_LAUNCH it's not entirely clear. We'd like to have
2321 // the same behavior as if a new instance was being started, which means not bringing it
2322 // to the front if the caller is not itself in the front.
Riddle Hsub70b36d2018-09-11 21:20:02 +08002323 final boolean differentTopTask;
Wale Ogunwale0b3d2922019-12-30 08:55:07 -08002324 if (mPreferredDisplayId == mTargetStack.getDisplayId()) {
Riddle Hsub70b36d2018-09-11 21:20:02 +08002325 final ActivityStack focusStack = mTargetStack.getDisplay().getFocusedStack();
2326 final ActivityRecord curTop = (focusStack == null)
2327 ? null : focusStack.topRunningNonDelayedActivityLocked(mNotTop);
Louis Changcdec0802019-11-11 11:45:07 +08002328 final Task topTask = curTop != null ? curTop.getTask() : null;
2329 differentTopTask = topTask != intentActivity.getTask()
Wale Ogunwale85fb19a2019-12-05 10:41:05 +09002330 || (focusStack != null && topTask != focusStack.getTopMostTask());
Riddle Hsub70b36d2018-09-11 21:20:02 +08002331 } else {
2332 // The existing task should always be different from those in other displays.
2333 differentTopTask = true;
2334 }
Wale Ogunwale01d66562015-12-29 08:19:19 -08002335
Riddle Hsub70b36d2018-09-11 21:20:02 +08002336 if (differentTopTask && !mAvoidMoveToFront) {
Wale Ogunwale01d66562015-12-29 08:19:19 -08002337 mStartActivity.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Wale Ogunwale21e06482019-11-18 05:14:15 -08002338 if (mSourceRecord == null || (mSourceStack.getTopNonFinishingActivity() != null &&
2339 mSourceStack.getTopNonFinishingActivity().getTask()
Louis Changcdec0802019-11-11 11:45:07 +08002340 == mSourceRecord.getTask())) {
Wale Ogunwale01d66562015-12-29 08:19:19 -08002341 // We really do want to push this one into the user's face, right now.
2342 if (mLaunchTaskBehind && mSourceRecord != null) {
Louis Changcdec0802019-11-11 11:45:07 +08002343 intentActivity.setTaskToAffiliateWith(mSourceRecord.getTask());
Wale Ogunwale01d66562015-12-29 08:19:19 -08002344 }
Chong Zhangdea4bd92016-03-15 12:50:03 -07002345
Louis Changcdec0802019-11-11 11:45:07 +08002346 final Task intentTask = intentActivity.getTask();
Louis Changf3070c52019-10-09 15:57:30 +08002347 final ActivityStack launchStack =
2348 getLaunchStack(mStartActivity, mLaunchFlags, intentTask, mOptions);
Louis Changbde91e92019-08-16 17:19:47 +08002349 if (launchStack == null || launchStack == mTargetStack) {
Louis Chang38430df2020-01-02 17:14:59 +08002350 // Do not set mMovedToFront to true below for split-screen-top stack, or
2351 // START_TASK_TO_FRONT will be returned and trigger unexpected animations when a
2352 // new intent has delivered.
2353 final boolean isSplitScreenTopStack = mTargetStack.isTopSplitScreenStack();
2354
Louis Changbde91e92019-08-16 17:19:47 +08002355 // We only want to move to the front, if we aren't going to launch on a
2356 // different stack. If we launch on a different stack, we will put the
2357 // task on top there.
Louis Chang38430df2020-01-02 17:14:59 +08002358 // Defer resuming the top activity while moving task to top, since the
2359 // current task-top activity may not be the activity that should be resumed.
Louis Changbde91e92019-08-16 17:19:47 +08002360 mTargetStack.moveTaskToFrontLocked(intentTask, mNoAnimation, mOptions,
Louis Chang38430df2020-01-02 17:14:59 +08002361 mStartActivity.appTimeTracker, DEFER_RESUME,
2362 "bringingFoundTaskToFront");
2363 mMovedToFront = !isSplitScreenTopStack;
2364 } else {
2365 intentTask.reparent(launchStack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, ANIMATE,
2366 DEFER_RESUME, "reparentToTargetStack");
Louis Changbde91e92019-08-16 17:19:47 +08002367 mMovedToFront = true;
Louis Changf3070c52019-10-09 15:57:30 +08002368 }
Louis Changbde91e92019-08-16 17:19:47 +08002369 mOptions = null;
Wale Ogunwale01d66562015-12-29 08:19:19 -08002370 }
2371 }
Andrii Kulianb850ea52017-12-12 23:49:10 -08002372 // Need to update mTargetStack because if task was moved out of it, the original stack may
2373 // be destroyed.
Wale Ogunwale1ebcd8e2020-01-21 11:27:03 -08002374 mTargetStack = intentActivity.getRootTask();
Louis Changcdec0802019-11-11 11:45:07 +08002375 mSupervisor.handleNonResizableTaskIfNeeded(intentActivity.getTask(),
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002376 WINDOWING_MODE_UNDEFINED, DEFAULT_DISPLAY, mTargetStack);
Wale Ogunwale01d66562015-12-29 08:19:19 -08002377 }
2378
2379 private void resumeTargetStackIfNeeded() {
2380 if (mDoResume) {
Louis Chang149d5c82019-12-30 09:47:39 +08002381 mRootWindowContainer.resumeFocusedStacksTopActivities(mTargetStack, null, mOptions);
Wale Ogunwale01d66562015-12-29 08:19:19 -08002382 } else {
2383 ActivityOptions.abort(mOptions);
2384 }
Louis Chang149d5c82019-12-30 09:47:39 +08002385 mRootWindowContainer.updateUserStack(mStartActivity.mUserId, mTargetStack);
Wale Ogunwale01d66562015-12-29 08:19:19 -08002386 }
2387
Louis Changcdec0802019-11-11 11:45:07 +08002388 private void setNewTask(Task taskToAffiliate) {
Louis Changbde91e92019-08-16 17:19:47 +08002389 final boolean toTop = !mLaunchTaskBehind && !mAvoidMoveToFront;
Louis Changcdec0802019-11-11 11:45:07 +08002390 final Task task = mTargetStack.createTask(
Wale Ogunwale0b3d2922019-12-30 08:55:07 -08002391 mSupervisor.getNextTaskIdForUser(mStartActivity.mUserId),
Louis Changbde91e92019-08-16 17:19:47 +08002392 mNewTaskInfo != null ? mNewTaskInfo : mStartActivity.info,
2393 mNewTaskIntent != null ? mNewTaskIntent : mIntent, mVoiceSession,
2394 mVoiceInteractor, toTop, mStartActivity, mSourceRecord, mOptions);
2395 addOrReparentStartingActivity(task, "setTaskFromReuseOrCreateNewTask - mReuseTask");
Louis Changcdec0802019-11-11 11:45:07 +08002396 updateBounds(mStartActivity.getTask(), mLaunchParams.mBounds);
Louis Change8902452019-06-10 10:49:28 +08002397
Louis Changbde91e92019-08-16 17:19:47 +08002398 if (DEBUG_TASKS) {
2399 Slog.v(TAG_TASKS, "Starting new activity " + mStartActivity
Louis Changcdec0802019-11-11 11:45:07 +08002400 + " in new task " + mStartActivity.getTask());
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08002401 }
2402
2403 if (taskToAffiliate != null) {
2404 mStartActivity.setTaskToAffiliateWith(taskToAffiliate);
Wale Ogunwale01d66562015-12-29 08:19:19 -08002405 }
2406 }
2407
Bryce Lee325e09682017-10-05 17:20:25 -07002408 private void deliverNewIntent(ActivityRecord activity) {
2409 if (mIntentDelivered) {
2410 return;
2411 }
2412
Jeff Changd136e772019-11-05 20:33:52 +08002413 activity.logStartActivity(EventLogTags.WM_NEW_INTENT, activity.getTask());
Wale Ogunwale586a8ee2019-06-04 13:44:14 +00002414 activity.deliverNewIntentLocked(mCallingUid, mStartActivity.intent,
Bryce Lee325e09682017-10-05 17:20:25 -07002415 mStartActivity.launchedFromPackage);
2416 mIntentDelivered = true;
2417 }
2418
Bryce Leed3624e12017-11-30 08:51:45 -08002419 @VisibleForTesting
Louis Changcdec0802019-11-11 11:45:07 +08002420 void updateBounds(Task task, Rect bounds) {
Bryce Leedacefc42017-10-10 12:56:02 -07002421 if (bounds.isEmpty()) {
Bryce Lee4e4a3ec2017-09-27 08:25:03 -07002422 return;
2423 }
2424
Wale Ogunwale1ebcd8e2020-01-21 11:27:03 -08002425 final Task rootTask = task.getRootTask();
2426 if (rootTask != null && rootTask.inPinnedWindowingMode()) {
2427 mService.animateResizePinnedStack(rootTask.mTaskId, bounds, -1);
Bryce Lee4e4a3ec2017-09-27 08:25:03 -07002428 } else {
Wale Ogunwale1ebcd8e2020-01-21 11:27:03 -08002429 // TODO: I don't believe it is possible to reach this else condition anymore...
Evan Roskya4cc3a92019-06-28 13:25:01 -07002430 task.setBounds(bounds);
Bryce Lee4e4a3ec2017-09-27 08:25:03 -07002431 }
2432 }
2433
Louis Changcdec0802019-11-11 11:45:07 +08002434 private void addOrReparentStartingActivity(Task parent, String reason) {
2435 if (mStartActivity.getTask() == null || mStartActivity.getTask() == parent) {
Wale Ogunwalec17418e2019-10-13 23:00:40 +02002436 parent.addChild(mStartActivity);
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08002437 } else {
Wale Ogunwale1a06f152019-10-11 11:26:30 +02002438 mStartActivity.reparent(parent, parent.getChildCount() /* top */, reason);
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08002439 }
Wale Ogunwale01d66562015-12-29 08:19:19 -08002440 }
2441
2442 private int adjustLaunchFlagsToDocumentMode(ActivityRecord r, boolean launchSingleInstance,
2443 boolean launchSingleTask, int launchFlags) {
2444 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
2445 (launchSingleInstance || launchSingleTask)) {
2446 // We have a conflict between the Intent and the Activity manifest, manifest wins.
2447 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
2448 "\"singleInstance\" or \"singleTask\"");
2449 launchFlags &=
2450 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | FLAG_ACTIVITY_MULTIPLE_TASK);
2451 } else {
2452 switch (r.info.documentLaunchMode) {
2453 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
2454 break;
2455 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
2456 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
2457 break;
2458 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
2459 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
2460 break;
2461 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
2462 launchFlags &= ~FLAG_ACTIVITY_MULTIPLE_TASK;
2463 break;
2464 }
2465 }
2466 return launchFlags;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08002467 }
2468
Louis Changcdec0802019-11-11 11:45:07 +08002469 private ActivityStack getLaunchStack(ActivityRecord r, int launchFlags, Task task,
Jorim Jaggi4ad98562016-04-19 20:30:47 -07002470 ActivityOptions aOptions) {
Bryce Leea19b5ad2017-06-07 16:54:11 -07002471 // We are reusing a task, keep the stack!
2472 if (mReuseTask != null) {
2473 return mReuseTask.getStack();
2474 }
Jorim Jaggib8c58762016-04-20 17:58:29 -07002475
Louis Chang38430df2020-01-02 17:14:59 +08002476 final boolean onTop =
2477 (aOptions == null || !aOptions.getAvoidMoveToFront()) && !mLaunchTaskBehind;
2478 return mRootWindowContainer.getLaunchStack(r, aOptions, task, onTop, mLaunchParams,
2479 mRequest.realCallingPid, mRequest.realCallingUid);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08002480 }
2481
Bryce Lee7daee392017-10-12 13:46:18 -07002482 private boolean isLaunchModeOneOf(int mode1, int mode2) {
2483 return mode1 == mLaunchMode || mode2 == mLaunchMode;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08002484 }
2485
Daichi Hirono15a02992016-04-27 18:47:01 +09002486 static boolean isDocumentLaunchesIntoExisting(int flags) {
2487 return (flags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
2488 (flags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0;
2489 }
liulvpingcfa825f2016-09-26 20:00:15 +08002490
Bryce Lee4c9a5972017-12-01 22:14:24 -08002491 ActivityStarter setIntent(Intent intent) {
2492 mRequest.intent = intent;
2493 return this;
2494 }
2495
Bryce Lee32e09ef2018-03-19 15:29:49 -07002496 Intent getIntent() {
2497 return mRequest.intent;
2498 }
2499
Bryce Lee4c9a5972017-12-01 22:14:24 -08002500 ActivityStarter setReason(String reason) {
2501 mRequest.reason = reason;
2502 return this;
2503 }
2504
2505 ActivityStarter setCaller(IApplicationThread caller) {
2506 mRequest.caller = caller;
2507 return this;
2508 }
2509
Bryce Lee4c9a5972017-12-01 22:14:24 -08002510 ActivityStarter setResolvedType(String type) {
2511 mRequest.resolvedType = type;
2512 return this;
2513 }
2514
2515 ActivityStarter setActivityInfo(ActivityInfo info) {
2516 mRequest.activityInfo = info;
2517 return this;
2518 }
2519
2520 ActivityStarter setResolveInfo(ResolveInfo info) {
2521 mRequest.resolveInfo = info;
2522 return this;
2523 }
2524
2525 ActivityStarter setVoiceSession(IVoiceInteractionSession voiceSession) {
2526 mRequest.voiceSession = voiceSession;
2527 return this;
2528 }
2529
2530 ActivityStarter setVoiceInteractor(IVoiceInteractor voiceInteractor) {
2531 mRequest.voiceInteractor = voiceInteractor;
2532 return this;
2533 }
2534
2535 ActivityStarter setResultTo(IBinder resultTo) {
2536 mRequest.resultTo = resultTo;
2537 return this;
2538 }
2539
2540 ActivityStarter setResultWho(String resultWho) {
2541 mRequest.resultWho = resultWho;
2542 return this;
2543 }
2544
2545 ActivityStarter setRequestCode(int requestCode) {
2546 mRequest.requestCode = requestCode;
2547 return this;
2548 }
2549
lumarkf65e02d2019-09-14 19:25:21 +08002550 /**
2551 * Sets the pid of the caller who originally started the activity.
2552 *
2553 * Normally, the pid/uid would be the calling pid from the binder call.
2554 * However, in case of a {@link PendingIntent}, the pid/uid pair of the caller is considered
2555 * the original entity that created the pending intent, in contrast to setRealCallingPid/Uid,
2556 * which represents the entity who invoked pending intent via {@link PendingIntent#send}.
2557 */
Bryce Lee4c9a5972017-12-01 22:14:24 -08002558 ActivityStarter setCallingPid(int pid) {
2559 mRequest.callingPid = pid;
2560 return this;
2561 }
2562
lumarkf65e02d2019-09-14 19:25:21 +08002563 /**
2564 * Sets the uid of the caller who originally started the activity.
2565 *
2566 * @see #setCallingPid
2567 */
Bryce Lee4c9a5972017-12-01 22:14:24 -08002568 ActivityStarter setCallingUid(int uid) {
2569 mRequest.callingUid = uid;
2570 return this;
2571 }
2572
2573 ActivityStarter setCallingPackage(String callingPackage) {
2574 mRequest.callingPackage = callingPackage;
2575 return this;
2576 }
2577
lumarkf65e02d2019-09-14 19:25:21 +08002578 /**
2579 * Sets the pid of the caller who requested to launch the activity.
2580 *
2581 * The pid/uid represents the caller who launches the activity in this request.
2582 * It will almost same as setCallingPid/Uid except when processing {@link PendingIntent}:
2583 * the pid/uid will be the caller who called {@link PendingIntent#send()}.
2584 *
2585 * @see #setCallingPid
2586 */
Bryce Lee4c9a5972017-12-01 22:14:24 -08002587 ActivityStarter setRealCallingPid(int pid) {
2588 mRequest.realCallingPid = pid;
2589 return this;
2590 }
2591
lumarkf65e02d2019-09-14 19:25:21 +08002592 /**
2593 * Sets the uid of the caller who requested to launch the activity.
2594 *
2595 * @see #setRealCallingPid
2596 */
Bryce Lee4c9a5972017-12-01 22:14:24 -08002597 ActivityStarter setRealCallingUid(int uid) {
2598 mRequest.realCallingUid = uid;
2599 return this;
2600 }
2601
2602 ActivityStarter setStartFlags(int startFlags) {
2603 mRequest.startFlags = startFlags;
2604 return this;
2605 }
2606
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01002607 ActivityStarter setActivityOptions(SafeActivityOptions options) {
Bryce Lee4c9a5972017-12-01 22:14:24 -08002608 mRequest.activityOptions = options;
2609 return this;
2610 }
2611
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01002612 ActivityStarter setActivityOptions(Bundle bOptions) {
2613 return setActivityOptions(SafeActivityOptions.fromBundle(bOptions));
2614 }
2615
Bryce Lee4c9a5972017-12-01 22:14:24 -08002616 ActivityStarter setIgnoreTargetSecurity(boolean ignoreTargetSecurity) {
2617 mRequest.ignoreTargetSecurity = ignoreTargetSecurity;
2618 return this;
2619 }
2620
Patrick Baumann31426b22018-05-21 13:46:40 -07002621 ActivityStarter setFilterCallingUid(int filterCallingUid) {
2622 mRequest.filterCallingUid = filterCallingUid;
2623 return this;
2624 }
2625
Bryce Lee4c9a5972017-12-01 22:14:24 -08002626 ActivityStarter setComponentSpecified(boolean componentSpecified) {
2627 mRequest.componentSpecified = componentSpecified;
2628 return this;
2629 }
2630
2631 ActivityStarter setOutActivity(ActivityRecord[] outActivity) {
2632 mRequest.outActivity = outActivity;
2633 return this;
2634 }
2635
Louis Changcdec0802019-11-11 11:45:07 +08002636 ActivityStarter setInTask(Task inTask) {
Bryce Lee4c9a5972017-12-01 22:14:24 -08002637 mRequest.inTask = inTask;
2638 return this;
2639 }
2640
2641 ActivityStarter setWaitResult(WaitResult result) {
2642 mRequest.waitResult = result;
2643 return this;
2644 }
2645
2646 ActivityStarter setProfilerInfo(ProfilerInfo info) {
2647 mRequest.profilerInfo = info;
2648 return this;
2649 }
2650
2651 ActivityStarter setGlobalConfiguration(Configuration config) {
2652 mRequest.globalConfig = config;
2653 return this;
2654 }
2655
Bryce Lee4c9a5972017-12-01 22:14:24 -08002656 ActivityStarter setUserId(int userId) {
2657 mRequest.userId = userId;
2658 return this;
2659 }
2660
Jorim Jaggi6fa41c32018-04-23 18:35:00 +02002661 ActivityStarter setAllowPendingRemoteAnimationRegistryLookup(boolean allowLookup) {
2662 mRequest.allowPendingRemoteAnimationRegistryLookup = allowLookup;
2663 return this;
2664 }
2665
Michal Karpinski201bc0c2018-07-20 15:32:00 +01002666 ActivityStarter setOriginatingPendingIntent(PendingIntentRecord originatingPendingIntent) {
2667 mRequest.originatingPendingIntent = originatingPendingIntent;
2668 return this;
2669 }
2670
Michal Karpinskiac116df2018-12-10 17:51:42 +00002671 ActivityStarter setAllowBackgroundActivityStart(boolean allowBackgroundActivityStart) {
2672 mRequest.allowBackgroundActivityStart = allowBackgroundActivityStart;
2673 return this;
2674 }
2675
Bryce Leed3624e12017-11-30 08:51:45 -08002676 void dump(PrintWriter pw, String prefix) {
Wale Ogunwale692dcd62017-06-20 13:38:14 -07002677 prefix = prefix + " ";
Dianne Hackborne676ec72017-07-25 10:55:08 -07002678 pw.print(prefix);
2679 pw.print("mCurrentUser=");
Louis Chang149d5c82019-12-30 09:47:39 +08002680 pw.println(mRootWindowContainer.mCurrentUser);
Dianne Hackborne676ec72017-07-25 10:55:08 -07002681 pw.print(prefix);
2682 pw.print("mLastStartReason=");
2683 pw.println(mLastStartReason);
2684 pw.print(prefix);
2685 pw.print("mLastStartActivityTimeMs=");
2686 pw.println(DateFormat.getDateTimeInstance().format(new Date(mLastStartActivityTimeMs)));
2687 pw.print(prefix);
2688 pw.print("mLastStartActivityResult=");
2689 pw.println(mLastStartActivityResult);
Louis Chang54fbb052019-10-16 17:10:17 +08002690 if (mLastStartActivityRecord != null) {
Dianne Hackborne676ec72017-07-25 10:55:08 -07002691 pw.print(prefix);
2692 pw.println("mLastStartActivityRecord:");
Louis Chang54fbb052019-10-16 17:10:17 +08002693 mLastStartActivityRecord.dump(pw, prefix + " ", true /* dumpAll */);
Wale Ogunwale692dcd62017-06-20 13:38:14 -07002694 }
Wale Ogunwale692dcd62017-06-20 13:38:14 -07002695 if (mStartActivity != null) {
Dianne Hackborne676ec72017-07-25 10:55:08 -07002696 pw.print(prefix);
2697 pw.println("mStartActivity:");
Garfield Tane8d84ab2019-10-11 09:49:40 -07002698 mStartActivity.dump(pw, prefix + " ", true /* dumpAll */);
Wale Ogunwale692dcd62017-06-20 13:38:14 -07002699 }
2700 if (mIntent != null) {
Dianne Hackborne676ec72017-07-25 10:55:08 -07002701 pw.print(prefix);
2702 pw.print("mIntent=");
2703 pw.println(mIntent);
Wale Ogunwale692dcd62017-06-20 13:38:14 -07002704 }
2705 if (mOptions != null) {
Dianne Hackborne676ec72017-07-25 10:55:08 -07002706 pw.print(prefix);
2707 pw.print("mOptions=");
2708 pw.println(mOptions);
Wale Ogunwale692dcd62017-06-20 13:38:14 -07002709 }
Dianne Hackborne676ec72017-07-25 10:55:08 -07002710 pw.print(prefix);
2711 pw.print("mLaunchSingleTop=");
Bryce Lee7daee392017-10-12 13:46:18 -07002712 pw.print(LAUNCH_SINGLE_TOP == mLaunchMode);
Dianne Hackborne676ec72017-07-25 10:55:08 -07002713 pw.print(" mLaunchSingleInstance=");
Bryce Lee7daee392017-10-12 13:46:18 -07002714 pw.print(LAUNCH_SINGLE_INSTANCE == mLaunchMode);
Dianne Hackborne676ec72017-07-25 10:55:08 -07002715 pw.print(" mLaunchSingleTask=");
Bryce Lee7daee392017-10-12 13:46:18 -07002716 pw.println(LAUNCH_SINGLE_TASK == mLaunchMode);
Dianne Hackborne676ec72017-07-25 10:55:08 -07002717 pw.print(prefix);
2718 pw.print("mLaunchFlags=0x");
2719 pw.print(Integer.toHexString(mLaunchFlags));
2720 pw.print(" mDoResume=");
2721 pw.print(mDoResume);
2722 pw.print(" mAddingToTask=");
2723 pw.println(mAddingToTask);
Wale Ogunwale692dcd62017-06-20 13:38:14 -07002724 }
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08002725}