Fix and simplify opening in browser (openUrlInBrowser)#12605
Conversation
The code was not previously working in case no default browser is set[1] AND NewPipe is set as default handler for the link in question. We improve it by telling the system to choose the target app as if the URI was `http://`, which works even if the user has not set a default browser. [1]: also the case if platform refuses to tell an app what the user's default browser is, which I observed on CalyxOS.
65dd81f to
83a0abd
Compare
|
I'm merging this for now, so people can start testing it in nightlies. Since this is a very small PR, it can always be reverted if it creates issues. |
| // (Implementation note: if the URL is one for which NewPipe itself | ||
| // is set as handler on Android >= 12, we actually remove the only eligible app | ||
| // for this link, and browsers will not be offered to the user. For that, use | ||
| // `openUrlInBrowser`.) |
There was a problem hiding this comment.
We should (I tested this in the Android 14 Developer Preview 2 on March 2023) be able use Intent.EXTRA_ALTERNATE_INTENTS (Android API 23+) to show all apps that can open a given intent even with web restrictions on Android >= 12. However, we need to have access to queries of all intents we use (so http, https, market, as we target Kodi's package directly):
final List<ResolveInfo> resolveInfoList;
// Use PackageManager.MATCH_ALL, as we want the list of apps which can handle a web intent
// and not only the default app
// This flag doesn't include disabled or uninstalled apps with data kept (they would be not
// accessible anyway, as this requires the QUERY_ALL_PACKAGES permission)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
resolveInfoList = context.getPackageManager()
.queryIntentActivities(intent,
PackageManager.ResolveInfoFlags.of(PackageManager.MATCH_ALL));
} else {
resolveInfoList = context.getPackageManager()
.queryIntentActivities(intent, PackageManager.MATCH_ALL);
}
final Intent[] intents = resolveInfoList.stream()
.map(resolveInfo -> new Intent(Intent.ACTION_VIEW, urlUri)
.setPackage(resolveInfo.activityInfo.packageName))
.toArray(Intent[]::new);
// Passing an Intent which doesn't match any activity, as we don't want any other app in the
// system chooser than the ones we got earlier
// In this example an ACTION_VIEW intent with an empty URI is used
final Intent chooserIntent = createChooserIntent(
context, new Intent(Intent.ACTION_VIEW, Uri.EMPTY), true);
// Intent.EXTRA_ALTERNATE_INTENTS extra allows to pass an unlimited amount of intents
chooserIntent.putExtra(Intent.EXTRA_ALTERNATE_INTENTS, intents);There was a problem hiding this comment.
Oh yeah, I see how this could work, we're basically recreating the chooser dialog. You propose adding this just for the case where openUrlInBrowser fails, right? Not for the general openAppChooser implementation, right? Because openAppChooser is also used in shareText which is not an URL.
The code was not previously working in case no default browser is set[1] AND NewPipe is set as default handler for the link in question.
We improve it by telling the system to choose the target app as if the URI was
http://, which makes more sense than expecting a default browser to be set and NewPipe trying to query that.[1]: also the case if platform refuses to tell an app what the user's default browser is, which I observed on CalyxOS.
What is it?
Fixes the following issue(s)
For technical reasons we do not add the behavior suggested in #9403 as we still mirror the behavior as if
http://is opened – unfortunately Android does not offer us a proper way to selectively exclude a specific activity (i.e. ourselves) from the target list (see also myImplementation notecomment in theopenAppChoosercode). cc @SebiderSushiAPK testing
The APK can be found by going to the "Checks" tab below the title. On the left pane, click on "CI", scroll down to "artifacts" and click "app" to download the zip file which contains the debug APK of this PR. You can find more info and a video demonstration on this wiki page.
Direct download link: app(2).zip
Due diligence