Skip to content

Commit

Permalink
Use FLAG_IMMUTABLE instead of FLAG_MUTABLE for ActivityScenario pendi…
Browse files Browse the repository at this point in the history
…ng implicit intents.

This fixes using ActivityScenario#launchActivityForResult with an implicit Intent
when targetSdk = 34.

Fixes #2193

PiperOrigin-RevId: 628194870
  • Loading branch information
brettchabot authored and copybara-androidxtest committed Apr 26, 2024
1 parent 10abebe commit ef740a2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
3 changes: 2 additions & 1 deletion core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

**Bug Fixes**

* make ViewCapture use ControlledLooper API instead of hardcoding is Robolectric check
* Make ViewCapture use ControlledLooper API instead of hardcoding is Robolectric check
* Fix using ActivityScenario#launchActivityForResult with an implicit Intent

**New Features**

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,12 @@ public void startActivityForResult(Intent intent, @Nullable Bundle activityOptio

activityOptionsBundle = optInToGrantBalPrivileges(activityOptionsBundle);

// make an immutable intent if its implicit
int intentMutability =
intent.getPackage() == null && intent.getComponent() == null
? PendingIntent.FLAG_IMMUTABLE
: PendingIntent.FLAG_MUTABLE;

// Note: Instrumentation.startActivitySync(Intent) cannot be used here because BootstrapActivity
// may start in different process. Also, we use PendingIntent because the target activity may
// set "exported" attribute to false so that it prohibits starting the activity outside of their
Expand All @@ -458,7 +464,7 @@ public void startActivityForResult(Intent intent, @Nullable Bundle activityOptio
getApplicationContext(),
/* requestCode= */ 0,
intent,
/* flags= */ PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE))
/* flags= */ PendingIntent.FLAG_UPDATE_CURRENT | intentMutability))
.putExtra(TARGET_ACTIVITY_OPTIONS_BUNDLE_KEY, activityOptionsBundle);

getApplicationContext().startActivity(bootstrapIntent, activityOptionsBundle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,15 @@ public void launch_intentWithPackageName() {
assertThat(activityScenario).isNotNull();
}

@Test
public void launchActivityForResult_intentWithAction() {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("scenario://test"));

ActivityScenario<IntentActivity> activityScenario =
ActivityScenario.launchActivityForResult(intent);
assertThat(activityScenario).isNotNull();
}

private static Stage lastLifeCycleTransition(Activity activity) {
return ActivityLifecycleMonitorRegistry.getInstance().getLifecycleStageOf(activity);
}
Expand Down

0 comments on commit ef740a2

Please sign in to comment.