From f21ec109af9639f40690ec6865a42162cec7f3f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Strau=C3=9F?= Date: Sun, 15 Dec 2024 20:42:22 +0100 Subject: [PATCH] feat(browser): add support check for custom tabs and fallback to default browser --- .../capacitorjs/plugins/browser/Browser.java | 24 ++++++++++ .../plugins/browser/BrowserPlugin.java | 47 ++++++++++++------- 2 files changed, 53 insertions(+), 18 deletions(-) diff --git a/browser/android/src/main/java/com/capacitorjs/plugins/browser/Browser.java b/browser/android/src/main/java/com/capacitorjs/plugins/browser/Browser.java index 060a4b956..02f910f04 100644 --- a/browser/android/src/main/java/com/capacitorjs/plugins/browser/Browser.java +++ b/browser/android/src/main/java/com/capacitorjs/plugins/browser/Browser.java @@ -5,12 +5,18 @@ import android.content.ComponentName; import android.content.Context; import android.content.Intent; +import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; import android.net.Uri; import android.os.Bundle; + import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.browser.customtabs.*; +import java.util.ArrayList; +import java.util.List; + /** * The Browser class implements Custom Chrome Tabs. See * https://developer.chrome.com/multidevice/android/customtabs for background @@ -81,6 +87,24 @@ public BrowserEventListener getBrowserEventListenerListener() { return browserEventListener; } + /** + * Check if custom tabs are supported. + * @return boolean + */ + public boolean areCustomTabsSupported() { + PackageManager packageManager = context.getPackageManager(); + Intent activityIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com")); + List resolveInfos = packageManager.queryIntentActivities(activityIntent, PackageManager.MATCH_ALL); + + List packageNames = new ArrayList<>(); + for (ResolveInfo info : resolveInfos) { + packageNames.add(info.activityInfo.packageName); + } + + String packageName = CustomTabsClient.getPackageName(context, packageNames, true); + return packageName != null; + } + /** * Open the browser to the specified URL. * @param url diff --git a/browser/android/src/main/java/com/capacitorjs/plugins/browser/BrowserPlugin.java b/browser/android/src/main/java/com/capacitorjs/plugins/browser/BrowserPlugin.java index 1fbc16852..d2a08e35b 100644 --- a/browser/android/src/main/java/com/capacitorjs/plugins/browser/BrowserPlugin.java +++ b/browser/android/src/main/java/com/capacitorjs/plugins/browser/BrowserPlugin.java @@ -59,25 +59,36 @@ public void open(PluginCall call) { Logger.error(getLogTag(), "Invalid color provided for toolbarColor. Using default", null); } - // open the browser and finish - try { - Intent intent = new Intent(getContext(), BrowserControllerActivity.class); - intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - getContext().startActivity(intent); + if (implementation.areCustomTabsSupported()) { + // open the browser and finish + try { + Intent intent = new Intent(getContext(), BrowserControllerActivity.class); + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + getContext().startActivity(intent); - Integer finalToolbarColor = toolbarColor; - setBrowserControllerListener( - activity -> { - activity.open(implementation, url, finalToolbarColor); - browserControllerActivityInstance = activity; - call.resolve(); - } - ); - } catch (ActivityNotFoundException ex) { - Logger.error(getLogTag(), ex.getLocalizedMessage(), null); - call.reject("Unable to display URL"); - return; + Integer finalToolbarColor = toolbarColor; + setBrowserControllerListener( + activity -> { + activity.open(implementation, url, finalToolbarColor); + browserControllerActivityInstance = activity; + call.resolve(); + } + ); + } catch (ActivityNotFoundException ex) { + Logger.error(getLogTag(), ex.getLocalizedMessage(), null); + call.reject("Unable to display URL"); + } + } else { + // fallback to opening the URL in the default browser + Intent browserIntent = new Intent(Intent.ACTION_VIEW, url); + browserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + if (browserIntent.resolveActivity(getContext().getPackageManager()) != null) { + getContext().startActivity(browserIntent); + call.resolve(); + } else { + call.reject("No application can handle the URL"); + } } }