From 1a8aa8b17eeaee626fe97551270142ade7e0c5d6 Mon Sep 17 00:00:00 2001 From: AudricV <74829229+AudricV@users.noreply.github.com> Date: Fri, 24 Mar 2023 23:05:15 +0100 Subject: [PATCH] Use a system chooser when opening links in browser in the case there is no browser available This change makes the app using the behavior when there is no default browser on Android 11 and lower, by opening a system chooser when there is no browser available (on all Android versions). Also catch any exception when the system chooser cannot be opened and show the "No app on your device can open this" toast in this case, as an `ActivityNotFoundException` could be thrown if no app is available to open a given web link. --- .../external_communication/ShareUtils.java | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/util/external_communication/ShareUtils.java b/app/src/main/java/org/schabi/newpipe/util/external_communication/ShareUtils.java index 2f50a94ae..118b77026 100644 --- a/app/src/main/java/org/schabi/newpipe/util/external_communication/ShareUtils.java +++ b/app/src/main/java/org/schabi/newpipe/util/external_communication/ShareUtils.java @@ -86,15 +86,19 @@ public final class ShareUtils { PackageManager.MATCH_DEFAULT_ONLY); } + final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)) + .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + if (defaultBrowserInfo == null) { - // No app installed to open a web url - Toast.makeText(context, R.string.no_app_to_open_intent, Toast.LENGTH_LONG).show(); + // No app installed to open a web URL, but it may be handled by other apps so try + // 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; } 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")) { // No browser set as default (doesn't work on some devices) @@ -205,7 +209,12 @@ public final class ShareUtils { chooserIntent.addFlags(permFlags); } } - context.startActivity(chooserIntent); + + try { + context.startActivity(chooserIntent); + } catch (final ActivityNotFoundException e) { + Toast.makeText(context, R.string.no_app_to_open_intent, Toast.LENGTH_LONG).show(); + } } /**