androidchrome-custom-tabs

How can I check whether Chrome supports Chrome custom tabs?


I have an activity that loads an external url into a webview within my app. I'd like to use Chrome Custom tabs when it's available but I support devices that might not have a version of Chrome that supports them.

In the case of CustomTabs not being supported I'd like to use my old code but use the CustomTabsIntent.Builder() when they are. The old code loads the content in a WebView contained in an Activity where I can still manage the ActionBar.

I'd like to write a helper method that will tell me if it's supported but I'm not sure how. The info on the developer page is pretty slim: https://developer.chrome.com/multidevice/android/customtabs

It says if you bind succeeds the custom tabs can be safely used. Is there an easy way to bind to test this?

Like this I assume:

Intent serviceIntent = new Intent("android.support.customtabs.action.CustomTabsService");
serviceIntent.setPackage("com.android.chrome");
boolean customTabsSupported = bindService(serviceIntent, new CustomTabsServiceConnection() {
            @Override
            public void onCustomTabsServiceConnected(final ComponentName componentName, final CustomTabsClient customTabsClient) {}

            @Override
            public void onServiceDisconnected(final ComponentName name) {}
        },
        Context.BIND_AUTO_CREATE | Context.BIND_WAIVE_PRIORITY);

if (customTabsSupported) {
    // is supported
}

Solution

  • From developer site of Chrome, I found the followings -

    As of Chrome 45, Chrome Custom Tabs is now generally available to all users of Chrome, on all of Chrome's supported Android versions (Jellybean onwards).

    Link: https://developer.chrome.com/multidevice/android/customtabs#whencaniuseit

    So, I checked whether Chrome supports Chrome Custom Tab by version.

    Check my code:

    String chromePackageName = "com.android.chrome";
    int chromeTargetVersion  = 45;
    
    boolean isSupportCustomTab = false;
    try {
        String chromeVersion = getApplicationContext().getPackageManager().getPackageInfo(chromePackageName, 0).versionName;
        if(chromeVersion.contains(".")) {
            chromeVersion = chromeVersion.substring(0, chromeVersion.indexOf('.'));
        }
        isSupportCustomTab = (Integer.valueOf(chromeVersion) >= chromeTargetVersion);
    } catch (PackageManager.NameNotFoundException ex) {
    } catch (Exception ex) { }
    
    if (isSupportCustomTab) {
        //Use Chrome Custom Tab
    } else {
        //Use WebView or other Browser
    }
    

    I don't know how efficient it is, just want to share.