Search code examples
androidapplinkschrome-custom-tabs

Open CustomTabIntent with AppLink enabled application


I am trying to add CustomTabIntent on my application to open url of my website. The problem is that I have implemented AppLinking into my application due to which Chrome Tab doesn't appear and it is redirected to my deeplink handler class which redirects my url to chrome.

I have used following code for CustomTabIntent,

CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder()
            .setToolbarColor(ContextCompat.getColor(this, R.color.colorPrimary))
            .setSecondaryToolbarColor(ContextCompat.getColor(this, R.color.colorPrimaryDark))
            .setShowTitle(true)
            .addDefaultShareMenuItem()
            .setStartAnimations(this, R.anim.slide_in_right, R.anim.slide_out_left)
            .setExitAnimations(this, android.R.anim.slide_in_left,
                    android.R.anim.slide_out_right);
    CustomTabsIntent customTabsIntent = builder.build();
    customTabsIntent.launchUrl(this, Uri.parse(url));

Is their a way I can bypass applink to open CustomTabIntent?


Solution

  • Yes, you can set the package name of the application you want to open by accessing the internal Intent inside the CustomTabsIntent and calling setPackageName.

    String packageName = "...";
    CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder()
            .setToolbarColor(ContextCompat.getColor(this, R.color.colorPrimary))
            .setSecondaryToolbarColor(ContextCompat.getColor(this, R.color.colorPrimaryDark))
            .setShowTitle(true)
            .addDefaultShareMenuItem()
            .setStartAnimations(this, R.anim.slide_in_right, R.anim.slide_out_left)
            .setExitAnimations(this, android.R.anim.slide_in_left,
                    android.R.anim.slide_out_right);
    CustomTabsIntent customTabsIntent = builder.build();
    customTabsIntent.intent.setPackage(packageName);
    customTabsIntent.launchUrl(this, Uri.parse(url));
    

    From this point on, you must be wondering how to find out which package name to set when opening the Custom Tab. Even though you could hard-code a package name for a specific browser eg:

    customTabsIntent.intent.setPackageName("com.android.chrome");
    

    But, since other browsers such as Firefox and Samsung also support CustomTabs, you will ideally want to find out which ones are installed and use one of those.

    The following code can help with that:

    public static ArrayList getCustomTabsPackages(Context context) {
        PackageManager pm = context.getPackageManager();
        // Get default VIEW intent handler.
        Intent activityIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));
    
        // Get all apps that can handle VIEW intents.
        List resolvedActivityList = pm.queryIntentActivities(activityIntent, 0);
        ArrayList packagesSupportingCustomTabs = new ArrayList<>();
        for (ResolveInfo info : resolvedActivityList) {
            Intent serviceIntent = new Intent();
            serviceIntent.setAction(ACTION_CUSTOM_TABS_CONNECTION);
            serviceIntent.setPackage(info.activityInfo.packageName);
            // Check if this package also resolves the Custom Tabs service.
            if (pm.resolveService(serviceIntent, 0) != null) {
                packagesSupportingCustomTabs.add(info);
            }
        }
        return packagesSupportingCustomTabs;
    }
    

    It's possible to use this in combination with CustomTabsClient.getPackageName to select a package.