androidkotlingoogle-playin-app-update

How to simulate onFailureListener & resultCode != Activity.RESULT_OK In App Updates?


How can I test this onFailureListener & resultCode != Activity.RESULT_OK?

When I open the app, I create a force Update State depending on my versionName of both apps shared on Google Play Internal Sharing.

At first, it will show me a custom dialog. On Negative Btn -> The app close

On Positive Btn -> The checkForUpdate() fun will start.

I use AppUpdateType.IMMEDIATE. So it will show me only the Google Play dialog for UPDATE.

I just made a build for inAppUpdates and I'm trying to handle onFailureListener. Without putting just a printstacktrace, I want to Open Google Play, and let user to install the app from there.

fun checkForAppUpdate() {
        val appUpdateManager = AppUpdateManagerFactory.create(this)
        val appUpdatedListener: InstallStateUpdatedListener by lazy {
            object : InstallStateUpdatedListener {
                override fun onStateUpdate(installState: InstallState) {
                    if(installState.installStatus() == InstallStatus.INSTALLED) {
                        appUpdateManager.unregisterListener(this)
                    }
                    else {Log.d("inAPPtag","InstallStateUpdatedListener: state: %s" + installState.installStatus())
                    }
                }
            }
        }
        // Returns an intent object that you use to check for an update.
        val appUpdateInfoTask = appUpdateManager.appUpdateInfo
        // Checks that the platform will allow the specified type of update.
        appUpdateInfoTask.addOnSuccessListener { appUpdateInfo ->
            if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE || appUpdateInfo.updateAvailability() == UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS) {

                appUpdateManager.registerListener(appUpdatedListener)
                // Request the update.
                try {
                    appUpdateManager.startUpdateFlowForResult(
                        appUpdateInfo,
                        AppUpdateType.IMMEDIATE,
                        this,
                         APP_UPDATE_REQUEST_CODE
                    )
                } catch (e: IntentSender.SendIntentException) {
                    e.printStackTrace()
                }
            }
        }
        appUpdateInfoTask.addOnFailureListener {
            it.printStackTrace()
            val intent = Intent(Intent.ACTION_VIEW, Uri.parse(Constants.STORE_URL))
            if (intent.resolveActivity(this.packageManager) != null) {
                splashTrace.putAttribute("SPLASH", "appUpdate")
                startActivity(intent)
                finish()
            }
        }
    }

Also, if resultCode != Activity.RESULT_OK. The same thing, to let the user install the app from Google Play.

if (requestCode == APP_UPDATE_REQUEST_CODE) {
           if (resultCode != Activity.RESULT_OK) {
               val intent = Intent(Intent.ACTION_VIEW, Uri.parse(Constants.STORE_URL))
               if (intent.resolveActivity(this.packageManager) != null) {
                   splashTrace.putAttribute("SPLASH", "appUpdate")
                   startActivity(intent)                    
                   finish()
                }
            }
        }

Solution

  • You have two places where your trying to catch an error in your app.

    The first one is a SendIntentException, as per documentation here it says that:

    Exception thrown when trying to send through a PendingIntent that has been canceled or is otherwise no longer able to execute the request.

    I guess this would happen if you trigger the startUpdateFlowForResult from another part of your code then the first one will get canceled and trigger this exception.

    The second error is on the appUpdateInfoTask.addOnFailureListener {. I believe this one will get triggered if the phone doesn't have the Google Play Store or if the device has no internet connection.

    Regarding your second question, you can check here. For an onActivityResult you have three options:

    1. RESULT_OK: the update was a success
    2. RESULT_CANCELED: user hit the back button
    3. RESULT_FIRST_USER: a user defined flow