Created
September 21, 2023 08:38
-
-
Save ikhlaqmalik13/3f95971120c6e23524633a0ddaff635f to your computer and use it in GitHub Desktop.
App Update
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { | |
super.onActivityResult(requestCode, resultCode, data) | |
} | |
override fun onResume() { | |
super.onResume() | |
when (getUpdateType()) { | |
AppUpdateType.FLEXIBLE -> setupFlexibleUpdateSuccessListener() | |
AppUpdateType.IMMEDIATE -> setupImmediateUpdateSuccessListener() | |
} | |
} | |
override fun onDestroy() { | |
super.onDestroy() | |
if (getUpdateType() == AppUpdateType.FLEXIBLE) { | |
mAppUpdateManager.unregisterListener(installStateUpdateListener) | |
} | |
} | |
private fun setupImmediateUpdateSuccessListener() { | |
mAppUpdateManager.appUpdateInfo.addOnSuccessListener { | |
if (it.updateAvailability() == UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS) { | |
requestAppUpdate(it, AppUpdateType.IMMEDIATE) | |
} | |
} | |
} | |
private fun setupFlexibleUpdateSuccessListener() { | |
mAppUpdateManager.appUpdateInfo.addOnSuccessListener { appUpdateInfo -> | |
if (appUpdateInfo.installStatus() == InstallStatus.DOWNLOADED) { | |
showInstallSnackBar() | |
} | |
} | |
} | |
private fun showInstallSnackBar() { | |
Snackbar.make( | |
findViewById(android.R.id.content), | |
getString(R.string.str_download_complete), | |
Snackbar.LENGTH_INDEFINITE | |
).apply { | |
setAction(getString(R.string.str_restart)) { | |
mAppUpdateManager.completeUpdate() | |
} | |
show() | |
} | |
} | |
private fun getUpdateType(): Int { | |
return AppUpdateType.FLEXIBLE | |
} | |
private fun checkForAppUpdates() { | |
val appUpdateInfoTask: Task<AppUpdateInfo> = mAppUpdateManager.appUpdateInfo | |
appUpdateInfoTask.addOnSuccessListener { info -> | |
val isUpdateAvailable = info.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE | |
val isUpdateAllowed = when (getUpdateType()) { | |
AppUpdateType.FLEXIBLE -> info.isFlexibleUpdateAllowed | |
AppUpdateType.IMMEDIATE -> info.isImmediateUpdateAllowed | |
else -> false | |
} | |
if (isUpdateAvailable && isUpdateAllowed) { | |
requestAppUpdate(info, getUpdateType()) | |
} | |
if (getUpdateType() == AppUpdateType.FLEXIBLE) { | |
mAppUpdateManager.registerListener(installStateUpdateListener) | |
} | |
} | |
} | |
private fun requestAppUpdate(appUpdateInfo: AppUpdateInfo, updateType: Int) { | |
try { | |
mAppUpdateManager.startUpdateFlowForResult( | |
appUpdateInfo, | |
this, | |
AppUpdateOptions.newBuilder(updateType).build(), | |
APP_UPDATE_REQUEST_CODE | |
) | |
} catch (e: Exception) { | |
e.printStackTrace() | |
} | |
} | |
private val installStateUpdateListener = InstallStateUpdatedListener { | |
if (it.installStatus() == InstallStatus.DOWNLOADED) { | |
showInstallSnackBar() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment