From 6c7baf9b4017c2c4d21df867680ccfb6ece6b860 Mon Sep 17 00:00:00 2001 From: Floens Date: Sat, 23 Jul 2016 20:20:49 +0200 Subject: [PATCH] Fix "open in browser" links opening a new Clover instance. --- .../org/floens/chan/utils/AndroidUtils.java | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/Clover/app/src/main/java/org/floens/chan/utils/AndroidUtils.java b/Clover/app/src/main/java/org/floens/chan/utils/AndroidUtils.java index f8ef0b6e..e1af1020 100644 --- a/Clover/app/src/main/java/org/floens/chan/utils/AndroidUtils.java +++ b/Clover/app/src/main/java/org/floens/chan/utils/AndroidUtils.java @@ -136,10 +136,26 @@ public class AndroidUtils { } public static void openLinkInBrowser(Activity activity, String link) { - CustomTabsIntent intent = new CustomTabsIntent.Builder() - .setToolbarColor(theme().primaryColor.color) - .build(); - intent.launchUrl(activity, Uri.parse(link)); + // Hack that's sort of the same as openLink + // The link won't be opened in a custom tab if this app is the default handler for that link. + // Manually check if this app opens it instead of a custom tab, and use the logic of + // openLink to avoid that and show a chooser instead. + boolean openWithCustomTabs = true; + Intent urlIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(link)); + PackageManager pm = getAppContext().getPackageManager(); + ComponentName resolvedActivity = urlIntent.resolveActivity(pm); + if (resolvedActivity != null) { + openWithCustomTabs = !resolvedActivity.getPackageName().equals(getAppContext().getPackageName()); + } + + if (openWithCustomTabs) { + CustomTabsIntent tabsIntent = new CustomTabsIntent.Builder() + .setToolbarColor(theme().primaryColor.color) + .build(); + tabsIntent.launchUrl(activity, Uri.parse(link)); + } else { + openLink(link); + } } public static void shareLink(String link) {