-
Notifications
You must be signed in to change notification settings - Fork 3.8k
[url_launcher_android] Support intent:// URIs in launchUrl and canLaunchUrl #11913
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
sorou3h1
wants to merge
1
commit into
flutter:main
Choose a base branch
from
sorou3h1:fix/url-launcher-android-intent-scheme
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+92
−6
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ | |
| import androidx.annotation.VisibleForTesting; | ||
| import androidx.browser.customtabs.CustomTabsClient; | ||
| import androidx.browser.customtabs.CustomTabsIntent; | ||
| import java.net.URISyntaxException; | ||
| import java.util.Collections; | ||
| import java.util.Locale; | ||
| import java.util.Map; | ||
|
|
@@ -64,8 +65,17 @@ void setActivity(@Nullable Activity activity) { | |
|
|
||
| @Override | ||
| public boolean canLaunchUrl(@NonNull String url) { | ||
| Intent launchIntent = new Intent(Intent.ACTION_VIEW); | ||
| launchIntent.setData(Uri.parse(url)); | ||
| Intent launchIntent; | ||
| if (url.startsWith("intent://")) { | ||
| try { | ||
| launchIntent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME); | ||
| } catch (URISyntaxException e) { | ||
| return false; | ||
| } | ||
| } else { | ||
| launchIntent = new Intent(Intent.ACTION_VIEW); | ||
| launchIntent.setData(Uri.parse(url)); | ||
| } | ||
| String componentName = intentResolver.getHandlerComponentName(launchIntent); | ||
| if (BuildConfig.DEBUG) { | ||
| Log.i(TAG, "component name for " + url + " is " + componentName); | ||
|
|
@@ -84,10 +94,19 @@ public boolean launchUrl( | |
| ensureActivity(); | ||
| assert activity != null; | ||
|
|
||
| Intent launchIntent = | ||
| new Intent(Intent.ACTION_VIEW) | ||
| .setData(Uri.parse(url)) | ||
| .putExtra(Browser.EXTRA_HEADERS, extractBundle(headers)); | ||
| Intent launchIntent; | ||
| if (url.startsWith("intent://")) { | ||
| try { | ||
| launchIntent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME); | ||
| } catch (URISyntaxException e) { | ||
| return false; | ||
| } | ||
| } else { | ||
| launchIntent = | ||
| new Intent(Intent.ACTION_VIEW) | ||
| .setData(Uri.parse(url)) | ||
| .putExtra(Browser.EXTRA_HEADERS, extractBundle(headers)); | ||
| } | ||
|
Comment on lines
+97
to
+109
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This implementation has two issues:
We can fix both issues by using Intent launchIntent;
if (url.regionMatches(true, 0, "intent:", 0, 7)) {
try {
launchIntent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
} catch (URISyntaxException e) {
return false;
}
} else {
launchIntent = new Intent(Intent.ACTION_VIEW).setData(Uri.parse(url));
}
launchIntent.putExtra(Browser.EXTRA_HEADERS, extractBundle(headers)); |
||
| if (requireNonBrowser && Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { | ||
| launchIntent.addFlags(Intent.FLAG_ACTIVITY_REQUIRE_NON_BROWSER); | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
url.startsWith("intent://")only matches URIs starting with the exact lowercase prefix and containing//. However, Android intent URIs can start withintent:(without//) and are case-insensitive per RFC 3986. Usingurl.regionMatches(true, 0, "intent:", 0, 7)is more robust and correctly supports all valid intent URIs.