Merge pull request #9968 from AudricV/fix-open-in-browser-without-browser

Use a system chooser when opening links in browser in the case there is no browser available
This commit is contained in:
Audric V 2023-03-26 12:41:42 +02:00 committed by GitHub
commit fb1b1c5be1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 5 deletions

View File

@ -86,15 +86,19 @@ public final class ShareUtils {
PackageManager.MATCH_DEFAULT_ONLY); PackageManager.MATCH_DEFAULT_ONLY);
} }
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url))
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (defaultBrowserInfo == null) { if (defaultBrowserInfo == null) {
// No app installed to open a web url // No app installed to open a web URL, but it may be handled by other apps so try
Toast.makeText(context, R.string.no_app_to_open_intent, Toast.LENGTH_LONG).show(); // opening a system chooser for the link in this case (it could be bypassed by the
// system if there is only one app which can open the link or a default app associated
// with the link domain on Android 12 and higher)
openAppChooser(context, intent, true);
return; return;
} }
final String defaultBrowserPackage = defaultBrowserInfo.activityInfo.packageName; final String defaultBrowserPackage = defaultBrowserInfo.activityInfo.packageName;
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url))
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (defaultBrowserPackage.equals("android")) { if (defaultBrowserPackage.equals("android")) {
// No browser set as default (doesn't work on some devices) // No browser set as default (doesn't work on some devices)
@ -205,7 +209,12 @@ public final class ShareUtils {
chooserIntent.addFlags(permFlags); chooserIntent.addFlags(permFlags);
} }
} }
try {
context.startActivity(chooserIntent); context.startActivity(chooserIntent);
} catch (final ActivityNotFoundException e) {
Toast.makeText(context, R.string.no_app_to_open_intent, Toast.LENGTH_LONG).show();
}
} }
/** /**