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()
}
}
}