Skip to content

Instantly share code, notes, and snippets.

@Divya0319
Created February 7, 2020 12:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Divya0319/dd5a2a47d92106a6ac1bedd7d7840482 to your computer and use it in GitHub Desktop.
Save Divya0319/dd5a2a47d92106a6ac1bedd7d7840482 to your computer and use it in GitHub Desktop.
class LoginActivity : BaseActivity() {
public override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
ButterKnife.bind(this)
checkForAppUpdate()
}
override fun onResume() {
super.onResume()
mAppUpdateManager = AppUpdateManagerFactory.create(this)
mAppUpdateManager.appUpdateInfo
.addOnSuccessListener { appUpdateInfo ->
// If the update is downloaded but not installed,
// notify the user to complete the update.
if (appUpdateInfo.installStatus() == InstallStatus.DOWNLOADED) {
popupSnackBarForCompleteStatus()
}
if (appUpdateInfo.updateAvailability() == UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS) {
// If an in-app update is already running, resume the update.
try {
mAppUpdateManager.startUpdateFlowForResult(
appUpdateInfo,
AppUpdateType.IMMEDIATE,
this,
APP_UPDATE_REQ_CODE
)
} catch (e: Exception) {
Utils.sendCustomCrashlyticsLog(e, null)
}
}
}
}
private fun checkForAppUpdate() {
// Returns an intent object that you use to check for an update.
mAppUpdateManager = AppUpdateManagerFactory.create(this)
val appUpdateInfoTask = mAppUpdateManager.appUpdateInfo
// Checks that the platform will allow the specified type of update.
appUpdateInfoTask.addOnSuccessListener { appUpdateInfo ->
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
&& appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) {
// Request the update.
Utils.logMessage("Update", "Available")
mAppUpdateManager.startUpdateFlowForResult(
appUpdateInfo,
AppUpdateType.IMMEDIATE,
this,
APP_UPDATE_REQ_CODE
)
}
}
}
private fun popupSnackBarForCompleteStatus() {
val snackBar = Snackbar.make(
findViewById(R.id.container),
"An update has just been downloaded.",
Snackbar.LENGTH_INDEFINITE)
snackBar.setAction("RESTART") { mAppUpdateManager.completeUpdate() }
snackBar.setActionTextColor(ContextCompat.getColor(this, R.color.blue_button_bg))
snackBar.show()
}
companion object {
private const val APP_UPDATE_REQ_CODE = 345
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment