Skip to content

Commit c089f25

Browse files
Opt in InstrumentationActivityInvoker to grant BAL
PiperOrigin-RevId: 587045326
1 parent 023b498 commit c089f25

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

core/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
`androidx.test:core:{version}` and `androidx.test:core-ktx:{version}` are released.
66

77
**Bug Fixes**
8+
* Activity starts are automatically opted in to allow background activity laucnhes when targetSdk >= 34
89

910
**New Features**
1011

core/java/androidx/test/core/app/InstrumentationActivityInvoker.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,9 @@ protected void onResume() {
168168
isTargetActivityStarted = true;
169169
PendingIntent startTargetActivityIntent =
170170
checkNotNull(getIntent().getParcelableExtra(TARGET_ACTIVITY_INTENT_KEY));
171-
Bundle options = getIntent().getBundleExtra(TARGET_ACTIVITY_OPTIONS_BUNDLE_KEY);
171+
Bundle options =
172+
optInToGrantBalPrivileges(
173+
getIntent().getBundleExtra(TARGET_ACTIVITY_OPTIONS_BUNDLE_KEY));
172174
try {
173175
if (options == null) {
174176
// Override and disable FLAG_ACTIVITY_NEW_TASK flag by flagsMask and flagsValue.
@@ -441,15 +443,7 @@ public void startActivityForResult(Intent intent, @Nullable Bundle activityOptio
441443

442444
activityResultWaiter = new ActivityResultWaiter(getApplicationContext());
443445

444-
if (VERSION.SDK_INT >= VERSION_CODES.UPSIDE_DOWN_CAKE) {
445-
ActivityOptions activityOptions =
446-
ActivityOptions.makeBasic()
447-
.setPendingIntentBackgroundActivityStartMode(MODE_BACKGROUND_ACTIVITY_START_ALLOWED);
448-
if (activityOptionsBundle == null) {
449-
activityOptionsBundle = new Bundle();
450-
}
451-
activityOptionsBundle.putAll(activityOptions.toBundle());
452-
}
446+
activityOptionsBundle = optInToGrantBalPrivileges(activityOptionsBundle);
453447

454448
// Note: Instrumentation.startActivitySync(Intent) cannot be used here because BootstrapActivity
455449
// may start in different process. Also, we use PendingIntent because the target activity may
@@ -470,6 +464,22 @@ public void startActivityForResult(Intent intent, @Nullable Bundle activityOptio
470464
getApplicationContext().startActivity(bootstrapIntent, activityOptionsBundle);
471465
}
472466

467+
private static Bundle optInToGrantBalPrivileges(Bundle activityOptionsBundle) {
468+
if (VERSION.SDK_INT < VERSION_CODES.UPSIDE_DOWN_CAKE) {
469+
return activityOptionsBundle;
470+
}
471+
// Initialize a bundle to grant this activities start privilege.
472+
Bundle updatedActivityOptions =
473+
ActivityOptions.makeBasic()
474+
.setPendingIntentBackgroundActivityStartMode(MODE_BACKGROUND_ACTIVITY_START_ALLOWED)
475+
.toBundle();
476+
// Merge the bundle with the one passed in. This allows overriding the start mode if desired.
477+
if (activityOptionsBundle != null) {
478+
updatedActivityOptions.putAll(activityOptionsBundle);
479+
}
480+
return updatedActivityOptions;
481+
}
482+
473483
@Override
474484
public void startActivityForResult(Intent intent) {
475485
startActivityForResult(intent, null);

0 commit comments

Comments
 (0)