Shin
Shin

Reputation: 127

How to set default Chrome custom tabs didn't have to show “open with”

I have built an Android app with Chrome Custom Tabs, but when I clicked the button to show the URL, a dialog appear say I need to choose "Open With" and list of all my browser apps that are available to be chosen. My problem is how to determine that the app only set Chrome as default and don't have to open dialog "open with" anymore.

fab.setOnClickListener {
    val url = "http://myurl.com/"
    val builder = CustomTabsIntent.Builder()
    builder.setToolbarColor(ContextCompat.getColor(this@MainActivity,R.color.colorAccent))
    builder.addDefaultShareMenuItem()

    val anotherCustomTab = CustomTabsIntent.Builder().build()

    val intent = anotherCustomTab.intent
    intent.data = Uri.parse("http://myurl.com/")

    builder.setShowTitle(true)

    val customTabsIntent = builder.build()
    customTabsIntent.launchUrl(this@MainActivity, Uri.parse(url))
}

Upvotes: 1

Views: 5547

Answers (2)

tranandh
tranandh

Reputation: 118

Following code is from here but please remember we should respect user preference. By user preference I mean,

  1. If user has multiple browser that support custom tab and doesn't have default browser and one of the browser is Chrome then we can open the link in Chrome.
  2. If user has multiple browser that supports custom tabs and has one default browser A set up and that browser A has custom tabs support we should open the link in that browser A.

  3. If user has multiple browser that support custom tab and doesn't have default browser and one of the browser is not Chrome then we can show chooser dialog with custom tabs supported browser to open the link

There is few other use case too but hope you got the crux of this

Browser supporting CCT (Atleast with latest version)

  1. Samsung browser
  2. Firefox
  3. Microsoft edge

Browser without CCT support:

  1. Old browser build in older Android version and samsung Android device

  2. Opera

  3. DuckDuckGo

    public static String getPackageNameToUse(Context context) {
    
       String packageNameToUse = null;
       final PackageManager packageManager = context.getPackageManager();
       final Intent activityIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
    
       // Get all apps that can handle VIEW intents and have custom tab service
       final List<ResolveInfo> resolvedActivityList = packageManager.queryIntentActivities(activityIntent, 0);
       final List<String> packagesSupportingCustomTabs = new ArrayList<>();
    
       for (final ResolveInfo info : resolvedActivityList)
       {
           final Intent serviceIntent = new Intent();
           serviceIntent.setAction(ACTION_CUSTOM_TABS_CONNECTION);
           serviceIntent.setPackage(info.activityInfo.packageName);
           if (packageManager.resolveService(serviceIntent, 0) != null)
       packagesSupportingCustomTabs.add(info.activityInfo.packageName);
       }
    
       // Now packagesSupportingCustomTabs contains all apps that can handle both VIEW intents
       // and service calls.
       if (packagesSupportingCustomTabs.size() == 1)
        packageNameToUse = packagesSupportingCustomTabs.get(0);
       else if (packagesSupportingCustomTabs.contains(STABLE_PACKAGE))
        packageNameToUse = STABLE_PACKAGE;
    
       return packageNameToUse;
      }
    

Upvotes: 1

Shin
Shin

Reputation: 127

I found how to verify it, referenced to this so i got this coded and work, didnt ask to set browser anymore.

fab.setOnClickListener {

        val PACKAGE_NAME = "com.android.chrome"

        val builder = CustomTabsIntent.Builder()

        builder.setToolbarColor(ContextCompat.getColor(this@MainActivity,R.color.colorAccent))
        builder.addDefaultShareMenuItem()
        builder.setShowTitle(true)

        val anotherCustomTab = builder.build()
        val intent = anotherCustomTab.intent
        intent.data = Uri.parse("http://www.myurl.com/")

        val packageManager = packageManager
        val resolveInfoList = packageManager.queryIntentActivities(anotherCustomTab.intent, PackageManager.MATCH_DEFAULT_ONLY)

        for (resolveInfo in resolveInfoList) {
            val packageName = resolveInfo.activityInfo.packageName
            if (TextUtils.equals(packageName, PACKAGE_NAME))
                anotherCustomTab.intent.setPackage(PACKAGE_NAME)
        }
        anotherCustomTab.launchUrl(this, anotherCustomTab.intent.data)
    }

Upvotes: 10

Related Questions