Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

stuartmorgan-g
Copy link
Collaborator

Sets FLAG_ACTIVITY_REQUIRE_NON_BROWSER on the launch intent when externalNonBrowserApplication is requested.

This only works on API 30+, but since that's already >80% of devices, and launch modes are already documented to be best-effort based on platform capabilities, that should be enough.

(A fallback is possible, as demonstrated in
flutter/plugins#5953, but it's not fully reliable, adds complexity, and relies on adding extra query permissions to the app bundle in order to work, so I am not including that approach.)

Fixes flutter/flutter#66721

Pre-Review Checklist

Footnotes

  1. Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling. 2 3

Sets `FLAG_ACTIVITY_REQUIRE_NON_BROWSER` on the launch intent when
`externalNonBrowserApplication` is requested.

This only works on API 30+, but since that's already >80% of devices,
and launch modes are already documented to be best-effort based on
platform capabilities, that should be enough.

(A fallback is possible, as demonstrated in
flutter/plugins#5953, but it's not fully
reliable, adds complexity, and relies on adding extra query
permissions to the app bundle in order to work, so I am not
including that approach.)

Fixes flutter/flutter#66721
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds support for launching URLs in an external, non-browser application on Android by using the FLAG_ACTIVITY_REQUIRE_NON_BROWSER intent flag. The changes include updating the Pigeon interface to pass a new boolean flag, implementing the logic in the Android native code, and adding corresponding tests and an example usage. My review identifies a bug in the test implementation, a potential improvement in the native Android code for API level compatibility, and a correction for the example app's UI logic.

Comment on lines +94 to +96
if (requireNonBrowser) {
launchIntent.addFlags(Intent.FLAG_ACTIVITY_REQUIRE_NON_BROWSER);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The FLAG_ACTIVITY_REQUIRE_NON_BROWSER flag was introduced in API level 30. While the system may ignore unknown flags on older versions, it's best practice to explicitly check the device's API level before adding a flag that is not available on all supported versions. This makes the code more robust and self-documenting about its API level dependency. You may need to add an import for android.os.Build.

    if (requireNonBrowser) {
      if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.R) {
        launchIntent.addFlags(Intent.FLAG_ACTIVITY_REQUIRE_NON_BROWSER);
      }
    }

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gmackall @reidbaker Does the Android team have a preference here? I had considered adding this but then didn't because presumably adding a flag that didn't exist in old versions should be harmless, so I figured it didn't add any value. I guess the self-documenting aspect is worth considering though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have a strong preference here, but slightly lean towards including it just for readability.

Comment on lines 202 to 206
onPressed: _hasCustomTabSupport
? () => setState(() {
_launched = _launchInNonBrowserExternalApp(toLaunch);
})
: null,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The onPressed callback for the 'Launch in non-browser app' button is conditioned on _hasCustomTabSupport. However, the ability to launch an external non-browser application is not dependent on Custom Tabs support. This condition seems to be incorrectly copied from another button and could prevent users from testing this feature on devices that don't support Custom Tabs but can handle the intent. The button should likely be enabled unconditionally.

                onPressed: () => setState(() {
                  _launched = _launchInNonBrowserExternalApp(toLaunch);
                }),

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha, this was wrong because the code I copied from also shouldn't have had it; it had incorrectly been set up to gate the "launch in browser" button rather than the "launch in custom tab" button.

Fixed my copypasta and the pre-existing bug.

Copy link
Member

@gmackall gmackall left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[url_launcher] Support requiring non-browser for launchUrl on Android
2 participants