GoogleChrome / android-browser-helper

The Android Browser Helper library helps developers use Custom Tabs and Trusted Web Activities on top of the AndroidX browser support library.
Apache License 2.0
695 stars 289 forks source link

Setting up TWA provider package name still opens default browser. #390

Open prashant-angelbroking opened 2 years ago

prashant-angelbroking commented 2 years ago

Hi guys,

As part of our app requirement, we are opening a webpage in a TWA fashion. We have been using chrome as our TWA provider passing non-whitelisted CORS headers.

Only recently, we started testing on other browsers that support TWA as mentioned here https://github.com/GoogleChrome/android-browser-helper/blob/main/docs/trusted-web-activity-browser-support.md and as per our testing

  1. Firefox (org.mozilla.firefo) doesn't support TWA. onRelationshipValidationResult is never called.
  2. Edge (com.microsoft.emmx) supports TWA, but doesn't support passing header all the time (the way shown in the demo project)
  3. Samsung (com.sec.android.app.sbrowser) doesn't support TWA. onRelationshipValidationResult is never called.

Is this an issue with this library? How do we solve this?

Consequently, we decided to force the TWA provider to chrome (com.android.chrome).

The bug is, with edge installed and set to the default browser, even after setting the package name to that of chrome, the .launch() method still opens edge browser.

Attached below is the code

private val chromePackageName = "com.android.chrome"

val packageName =
            CustomTabsClient.getPackageName(context, listOf(chromePackageName))

CustomTabsClient.bindCustomTabsService(
            context,
            packageName,
            customTabsServiceConnection!!
        )

val intentBuilder = TrustedWebActivityIntentBuilder(uri)
val intent = intentBuilder.build(customTabsSession!!)
// intent.intent.getPackage() always returns com.android.chrome, even if chrome is NOT set default

TwaLauncher(context).launch(intentBuilder, null, null, null) // this still opens edge

Steps to reproduce the behavior:

  1. Have Edge browser installed and set to default app
  2. On passing the chrome package name in the code snippet above still launched edge.

The expected behavior is that it should launch the chrome app in TWA and not Edge. What is the correct way to force open the TWA to the chrome browser?