private static String getPackageNameSupportingCustomTabs()

in auth-lib/src/auth/java/com/spotify/sdk/android/auth/browser/CustomTabsSupportChecker.java [41:77]


    private static String getPackageNameSupportingCustomTabs(PackageManager pm, Uri uri) {
        Intent activityIntent = new Intent(Intent.ACTION_VIEW, uri).addCategory(Intent.CATEGORY_BROWSABLE);
        // Check for default handler
        ResolveInfo defaultViewHandlerInfo = pm.resolveActivity(activityIntent, 0);
        String defaultViewHandlerPackageName = null;
        if (defaultViewHandlerInfo != null) {
            defaultViewHandlerPackageName = defaultViewHandlerInfo.activityInfo.packageName;
        }
        Log.d(TAG, "Found default package name for handling VIEW intents: " + defaultViewHandlerPackageName);

        // Get all apps that can handle the intent
        List<ResolveInfo> resolvedActivityList = pm.queryIntentActivities(activityIntent, 0);
        ArrayList<String> 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) {
                Log.d(TAG, "Adding " + info.activityInfo.packageName + " to supported packages");
                packagesSupportingCustomTabs.add(info.activityInfo.packageName);
            }
        }

        String packageNameToUse = null;
        if (packagesSupportingCustomTabs.size() == 1) {
            packageNameToUse = packagesSupportingCustomTabs.get(0);
        } else if (packagesSupportingCustomTabs.size() > 1) {
            if (!TextUtils.isEmpty(defaultViewHandlerPackageName)
                    && packagesSupportingCustomTabs.contains(defaultViewHandlerPackageName)) {
                packageNameToUse = defaultViewHandlerPackageName;
            } else {
                packageNameToUse = packagesSupportingCustomTabs.get(0);
            }
        }
        return packageNameToUse;
    }