Skip to content

Commit 1a8aa8b

Browse files
committed
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.
1 parent d33229a commit 1a8aa8b

1 file changed

Lines changed: 14 additions & 5 deletions

File tree

  • app/src/main/java/org/schabi/newpipe/util/external_communication

app/src/main/java/org/schabi/newpipe/util/external_communication/ShareUtils.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,19 @@ public static void openUrlInBrowser(@NonNull final Context context, final String
8686
PackageManager.MATCH_DEFAULT_ONLY);
8787
}
8888

89+
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url))
90+
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
91+
8992
if (defaultBrowserInfo == null) {
90-
// No app installed to open a web url
91-
Toast.makeText(context, R.string.no_app_to_open_intent, Toast.LENGTH_LONG).show();
93+
// No app installed to open a web URL, but it may be handled by other apps so try
94+
// opening a system chooser for the link in this case (it could be bypassed by the
95+
// system if there is only one app which can open the link or a default app associated
96+
// with the link domain on Android 12 and higher)
97+
openAppChooser(context, intent, true);
9298
return;
9399
}
94100

95101
final String defaultBrowserPackage = defaultBrowserInfo.activityInfo.packageName;
96-
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url))
97-
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
98102

99103
if (defaultBrowserPackage.equals("android")) {
100104
// No browser set as default (doesn't work on some devices)
@@ -205,7 +209,12 @@ private static void openAppChooser(@NonNull final Context context,
205209
chooserIntent.addFlags(permFlags);
206210
}
207211
}
208-
context.startActivity(chooserIntent);
212+
213+
try {
214+
context.startActivity(chooserIntent);
215+
} catch (final ActivityNotFoundException e) {
216+
Toast.makeText(context, R.string.no_app_to_open_intent, Toast.LENGTH_LONG).show();
217+
}
209218
}
210219

211220
/**

0 commit comments

Comments
 (0)