androidkotlinandroid-package-managersgoogle-tv

Error when trying to app jump to "preinstalled" "cloud" app on Google TV


I have a working setup, where it is possible to app jump (context.startActivty(intent)) to other apps. This will fail if the app is not install, so we use the code below to check it. However, on Google TV this code will fail when the apps can be installed with the "AMATI_EXPERIENCE"!

App ready to be downloaded on Google TV

Does anyone know how to detect in code if an app is not installed as here on Google TV?

    fun isPackageInstalled(context: Context, packageName: String): Boolean =
        packageInfo(context, packageName) != null

    fun packageInfo(context: Context, packageName: String): PackageInfo? {
        return try {
            context.packageManager.getPackageInfo(packageName, 0)
        } catch (e: PackageManager.NameNotFoundException) {
            null
        }
    }

Exception thrown when starting activity for Viaplay

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=https://viaplay.no/... flg=0x10000000 pkg=com.viaplay.android }
    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2239)
    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1878)
    at android.app.ContextImpl.startActivity(ContextImpl.java:1132)
    at android.app.ContextImpl.startActivity(ContextImpl.java:1103)
    at android.content.ContextWrapper.startActivity(ContextWrapper.java:436)
    ...

Solution

  • I found an alternative call on the PackageManager, which triggered app download, so it at least does not crash:

    context.packageManager.getLaunchIntentForPackage(packageName)
    

    Combined with the check, it will look like this

        fun isPackageInstalled(context: Context, packageName: String): Boolean =
            packageInfo(context, packageName) != null && 
            context.packageManager.getLaunchIntentForPackage(packageName) != null
    

    Note: testing the function on Amazon Prime it didn't work, but it did with getLeanbackLaunchIntentForPackage("com.amazon.amazonvideo.livingroom").