I am trying to implement custom chrome tabs. I am using Google\'s default library customtabs.
I referred this tutorial for implementing custom chrom
The Custom Tabs protocol is open, meaning that other browsers can support it. In fact, the Samsung Internet Browser already supports it (albeit with broken implementation) and Firefox has added an initial, very experimental support to it's nightly builds.
So, best practice would be querying the Android system on which of the installed browsers support the Custom Tabs protocol:
private static final String ACTION_CUSTOM_TABS_CONNECTION =
"android.support.customtabs.action.CustomTabsService";
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);
if (pm.resolveService(serviceIntent, 0) != null) {
packagesSupportingCustomTabs.add(info);
}
}
return packagesSupportingCustomTabs;
}
You may want to check ResolveInfo#preferredOrder to check if the user has preference on one of the apps over the others. Also, if there's no preferred app (or two apps have the same major preference level), you may want to ask the user to choose one, or go with reasonable defaults
It is also important to check if the give Url has a native application that handles it installed. If so, it may make sense for your app to start the native one instead of opening it in a Custom Tab:
public static List getNativeApp(Context context, Uri uri) {
PackageManager pm = context.getPackageManager();
//Get all Apps that resolve a random url
Intent browserActivityIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));
List resolvedBrowserList = pm.queryIntentActivities(browserActivityIntent, 0);
Intent specializedActivityIntent = new Intent(Intent.ACTION_VIEW, uri);
List resolvedSpecializedList = pm.queryIntentActivities(specializedActivityIntent, 0);
resolvedSpecializedList.removeAll(resolvedBrowserList);
return resolvedBrowserList;
}
Once decided on which handler to use to open the Custom Tab, use the mCustomTabsIntent.intent.setPackage(packageName); to tell Custom Tab which application to open it.