Skip to content

Instantly share code, notes, and snippets.

@nisaefendioglu
Last active June 6, 2023 11:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nisaefendioglu/0a6bf42bd996da53053e5e3df88c0123 to your computer and use it in GitHub Desktop.
Save nisaefendioglu/0a6bf42bd996da53053e5e3df88c0123 to your computer and use it in GitHub Desktop.
class InAppUpdate(private val activity: Activity) : InstallStateUpdatedListener {
private var appUpdateManager: AppUpdateManager = AppUpdateManagerFactory.create(activity)
private val REQUEST_CODE_UPDATE = 500
private var currentUpdateType = AppUpdateType.FLEXIBLE
init {
// AppUpdateManager örneği oluşturuluyor
appUpdateManager.appUpdateInfo.addOnSuccessListener { updateInfo ->
if (updateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE) {
when (updateInfo.updatePriority()) {
5 -> {
// Hızlı ve zorunlu güncelleme
if (updateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) {
startUpdateFlow(updateInfo, AppUpdateType.IMMEDIATE)
}
}
4 -> {
val clientVersionStalenessDays = updateInfo.clientVersionStalenessDays()
if (clientVersionStalenessDays != null && clientVersionStalenessDays >= 5 && updateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) {
// Zorunlu güncelleme (5 gün veya daha eski)
startUpdateFlow(updateInfo, AppUpdateType.IMMEDIATE)
} else if (clientVersionStalenessDays != null && clientVersionStalenessDays >= 3 && updateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)) {
// Esnek güncelleme (3 gün veya daha eski)
startUpdateFlow(updateInfo, AppUpdateType.FLEXIBLE)
}
}
3 -> {
val clientVersionStalenessDays = updateInfo.clientVersionStalenessDays()
if (clientVersionStalenessDays != null && clientVersionStalenessDays >= 30 && updateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) {
// Zorunlu güncelleme (30 gün veya daha eski)
startUpdateFlow(updateInfo, AppUpdateType.IMMEDIATE)
} else if (clientVersionStalenessDays != null && clientVersionStalenessDays >= 15 && updateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)) {
// Esnek güncelleme (15 gün veya daha eski)
startUpdateFlow(updateInfo, AppUpdateType.FLEXIBLE)
}
}
2 -> {
val clientVersionStalenessDays = updateInfo.clientVersionStalenessDays()
if (clientVersionStalenessDays != null && clientVersionStalenessDays >= 90 && updateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) {
// Zorunlu güncelleme (90 gün veya daha eski)
startUpdateFlow(updateInfo, AppUpdateType.IMMEDIATE)
} else if (clientVersionStalenessDays != null && clientVersionStalenessDays >= 30 && updateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)) {
// Esnek güncelleme (30 gün veya daha eski)
startUpdateFlow(updateInfo, AppUpdateType.FLEXIBLE)
}
}
1 -> {
// Esnek güncelleme
startUpdateFlow(updateInfo, AppUpdateType.FLEXIBLE)
}
else -> {
// Uygulama içi güncellemeyi gösterme
}
}
} else {
// Güncelleme olmadığı durum
}
}
// Listener kaydediliyor
appUpdateManager.registerListener(this)
}
private fun startUpdateFlow(updateInfo: AppUpdateInfo, updateType: Int) {
try {
// Güncelleme akışı başlatılıyor
appUpdateManager.startUpdateFlowForResult(updateInfo, updateType, activity, REQUEST_CODE_UPDATE)
currentUpdateType = updateType
} catch (e: IntentSender.SendIntentException) {
e.printStackTrace()
showSnackbar("Güncelleme başlatılamadı.")
}
}
fun onResume() {
appUpdateManager.appUpdateInfo.addOnSuccessListener { updateInfo ->
if (currentUpdateType == AppUpdateType.FLEXIBLE) {
if (updateInfo.installStatus() == InstallStatus.DOWNLOADED)
// Esnek güncelleme tamamlandığında
onFlexibleUpdateDownloadCompleted()
} else if (currentUpdateType == AppUpdateType.IMMEDIATE) {
if (updateInfo.updateAvailability() == UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS) {
// Zorunlu güncelleme süreci devam ederken
startUpdateFlow(updateInfo, AppUpdateType.IMMEDIATE)
}
}
}
}
private fun onFlexibleUpdateDownloadCompleted() {
Snackbar.make(
activity.findViewById(R.id.activity_main_layout),
"Bir güncelleme indirildi.",
Snackbar.LENGTH_INDEFINITE
).apply {
setAction("RESTART") { appUpdateManager.completeUpdate() }
setActionTextColor(Color.WHITE)
show()
}
}
private fun showSnackbar(message: String) {
Snackbar.make(
activity.findViewById(R.id.activity_main_layout),
message,
Snackbar.LENGTH_SHORT
).show()
}
override fun onStateUpdate(state: InstallState) {
if (state.installStatus() == InstallStatus.DOWNLOADED) {
// Esnek güncelleme tamamlandığında
onFlexibleUpdateDownloadCompleted()
}
}
}
class MainActivity : AppCompatActivity() {
private lateinit var inAppUpdate: InAppUpdate
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// InAppUpdate sınıfının örneği oluşturuluyor
inAppUpdate = InAppUpdate(this)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
// InAppUpdate'den gelen onActivityResult çağrısını yönlendiriyor
inAppUpdate.onActivityResult(requestCode, resultCode, data)
}
override fun onResume() {
super.onResume()
// InAppUpdate onResume çağrısını yönlendiriyor
inAppUpdate.onResume()
}
override fun onDestroy() {
super.onDestroy()
// InAppUpdate nesnesini yok etmek için onDestroy çağrısı yapılıyor
inAppUpdate.onDestroy()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment