Last active
May 7, 2023 16:59
-
-
Save NonCoderF/3a0f1dc216d35b49ab3d4a7ec6782afb to your computer and use it in GitHub Desktop.
This file contains 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
interface AppUpdater { | |
fun checkUpdate() | |
} | |
class AppUpdaterImpl(val activity: AppCompatActivity) : AppUpdater { | |
companion object { | |
private const val APP_UPDATE_REQUEST_CODE = 1991 | |
} | |
private val appUpdateManager: AppUpdateManager by lazy { | |
if (BuildConfig.DEBUG) { | |
FakeAppUpdateManager(activity).apply { | |
setUpdateAvailable(308, AppUpdateType.FLEXIBLE) | |
setUpdatePriority(5) | |
} | |
} else { | |
AppUpdateManagerFactory.create(activity) | |
} | |
} | |
private val appUpdatedListener: InstallStateUpdatedListener by lazy { | |
object : InstallStateUpdatedListener { | |
override fun onStateUpdate(installState: InstallState) { | |
when (installState.installStatus()) { | |
InstallStatus.FAILED -> updateFailed() | |
InstallStatus.DOWNLOADED -> updateSuccess() | |
InstallStatus.INSTALLED -> appUpdateManager.unregisterListener(this) | |
else -> Log.e("TAG", "Install status listener : ${installState.installStatus()}") | |
} | |
} | |
} | |
} | |
private val updateFlowResultLauncher = activity.registerForActivityResult( | |
ActivityResultContracts.StartIntentSenderForResult(), | |
) { result -> | |
Log.e("TAG", "Flow result") | |
if (result.resultCode != Activity.RESULT_OK) updateFailed() | |
} | |
private val starter = | |
IntentSenderForResultStarter { intent, _, fillInIntent, flagsMask, flagsValues, _, _ -> | |
Log.e("TAG", "Flow launch") | |
updateFlowResultLauncher.launch( | |
IntentSenderRequest.Builder(intent).setFillInIntent(fillInIntent) | |
.setFlags(flagsValues, flagsMask).build() | |
) | |
} | |
private fun updateSuccess() { | |
activity.alertDialog { | |
title = "Update success" | |
message = "Please restart the app to complete the update" | |
positiveButton("Restart"){ | |
it.dismiss() | |
appUpdateManager.completeUpdate() | |
} | |
cancelable = false | |
}.safeShow(activity) | |
} | |
private fun updateFailed() { | |
activity.alertDialog { | |
title = "Update failed" | |
message = "Please retry to download and install" | |
positiveButton("Retry"){ | |
it.dismiss() | |
checkUpdate() | |
} | |
cancelable = false | |
}.safeShow(activity) | |
} | |
override fun checkUpdate() { | |
Log.e("TAG", "Checking update") | |
val appUpdateInfoTask = appUpdateManager.appUpdateInfo | |
appUpdateInfoTask.addOnSuccessListener { appUpdateInfo -> | |
when (appUpdateInfo.updateAvailability()) { | |
UpdateAvailability.UPDATE_NOT_AVAILABLE -> { | |
Log.e("TAG", "Update not available") | |
} | |
UpdateAvailability.UPDATE_AVAILABLE -> { | |
Log.e("TAG", "Update available") | |
try { | |
val installType = when { | |
appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE) -> AppUpdateType.FLEXIBLE | |
appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE) -> AppUpdateType.IMMEDIATE | |
else -> null | |
} | |
if (installType == AppUpdateType.FLEXIBLE) | |
appUpdateManager.registerListener(appUpdatedListener) | |
installType?.let { | |
appUpdateManager.startUpdateFlowForResult( | |
appUpdateInfo, | |
it, | |
starter, | |
APP_UPDATE_REQUEST_CODE | |
) | |
Log.e("TAG", "Launching app update") | |
} | |
} catch (e: IntentSender.SendIntentException) { | |
e.printStackTrace() | |
} | |
} | |
UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS -> { | |
Log.e("TAG", "Update running") | |
} | |
else -> { | |
Log.e("TAG", "Update unknown") | |
} | |
} | |
} | |
} | |
} | |
//Usage | |
//Lazy Initialize inside Activity | |
private val appUpdater by lazy { AppUpdaterImpl(this) } | |
//Check anywhere | |
appUpdater.checkUpdate() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment