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..22520f553 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,11 +5,15 @@ 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 @@ -81,6 +85,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 5cb859402..ef3924d96 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,26 +59,37 @@ public void open(PluginCall call) { Logger.error(getLogTag(), "Invalid color provided for toolbarColor. Using default", null); } - // open the browser and finish - - 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 + 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 -> { - try { - activity.open(implementation, url, finalToolbarColor); - browserControllerActivityInstance = activity; - call.resolve(); - } catch (ActivityNotFoundException ex) { - Logger.error(getLogTag(), ex.getLocalizedMessage(), null); - call.reject("Unable to display URL"); + Integer finalToolbarColor = toolbarColor; + setBrowserControllerListener( + activity -> { + try { + 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"); } - ); + } } @PluginMethod