Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

custom chrome tabs asks for multiple browser to choose

I am trying to implement custom chrome tabs. I am using Google's default library customtabs.

I referred this tutorial for implementing custom chrome tabs. now as suggested in tutorial, I did my coding something like this.

mCustomTabsServiceConnection = new CustomTabsServiceConnection() {
    @Override
    public void onCustomTabsServiceConnected(
        ComponentName componentName,
        CustomTabsClient customTabsClient) {
        mCustomTabsClient = customTabsClient;
        mCustomTabsClient.warmup(0L);
        mCustomTabsSession = mCustomTabsClient.newSession(null);
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        mCustomTabsClient = null;
    }
};

CustomTabsClient.bindCustomTabsService(this, "com.android.chrome",
        mCustomTabsServiceConnection);

CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(
        mCustomTabsSession).setShowTitle(true);
builder.setToolbarColor(getResources().getColor(R.color.purple_main));
        builder.setStartAnimations(getApplicationContext(), R.anim.fadein,
                R.anim.fadeout);
builder.setExitAnimations(getApplicationContext(), R.anim.fadein,
                R.anim.fadeout);
mCustomTabsIntent = builder.build(); 

and launching custom tabs.

mCustomTabsIntent.launchUrl(TampleDetails.this,
       Uri.parse(temple_website));

Now as you see I have bind custom tabs with chrome's package name, still it asks for choosing between Chrome and UC Browser. and it is obvious UC browser doesn't supports Custom tabs.

So my Question is how to restrict Custom tabs to bind only with Chrome Browser?

any help will be appreciated.

Thank you.

like image 501
V-rund Puro-hit Avatar asked Jul 18 '16 06:07

V-rund Puro-hit


People also ask

What does Chrome Custom Tabs do?

Custom Tabs is a browser feature, introduced by Chrome, that is now supported by most major browsers on Android. It give apps more control over their web experience, and make transitions between native and web content more seamless without having to resort to a WebView.

How do I disable Chrome custom Tabs?

Open Google application or do a search, click the hamburger in the top left and go into Settings. Go into Accounts and privacy. Turn off 'open web pages in app'. This appears to turn off chrome custom tabs.

What is Customize tab?

Community posts can include polls, GIFs, text, images, and video. Community posts can allow you to connect with your audience outside of video uploads. They show on the Community tab, and may show on Home or the Subscriptions feed.


2 Answers

Check this stackoverflow answer: if you set the CustomTabIntent package with the package of your desired browser before launch the url, you can skip the Android dialog browser choice; for me it worked:

/**
 * Opens the URL on a Custom Tab
 *
 * @param activity         The host activity.
 * @param uri              the Uri to be opened.
 */
public static void openCustomTab(Activity activity,
                                 Uri uri) {
    // Here is a method that returns the chrome package name 
    String packageName = CustomTabsHelper.getPackageNameToUse(activity, mUrl);

    CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
    mCustomTabsIntent = builder
            .setShowTitle(true)
            .build();
    builder.setToolbarColor(ContextCompat.getColor(activity, R.color.colorPrimary));

    if ( packageName != null ) {
        mCustomTabsIntent.intent.setPackage(packageName);
    }
    mCustomTabsIntent.launchUrl(activity, uri);
}
like image 163
Valentino Avatar answered Oct 14 '22 17:10

Valentino


Supporting multiple browsers

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<ResolveInfo> 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<ResolveInfo> resolvedActivityList = pm.queryIntentActivities(activityIntent, 0);
    ArrayList<ResolveInfo> 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

Thinking about Native apps

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<ResolveInfo> 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<ResolveInfo> resolvedBrowserList = pm.queryIntentActivities(browserActivityIntent, 0);

    Intent specializedActivityIntent = new Intent(Intent.ACTION_VIEW, uri);
    List<ResolveInfo> resolvedSpecializedList = pm.queryIntentActivities(specializedActivityIntent, 0);
    resolvedSpecializedList.removeAll(resolvedBrowserList);

    return resolvedBrowserList;

}

Opening the Custom Tab with an specific package

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.

like image 20
andreban Avatar answered Oct 14 '22 16:10

andreban