More fixes for update checker
This commit is contained in:
@@ -16,6 +16,7 @@ import java.time.Instant
|
|||||||
import java.util.concurrent.CancellationException
|
import java.util.concurrent.CancellationException
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
import kotlin.math.max
|
import kotlin.math.max
|
||||||
|
import kotlin.math.min
|
||||||
|
|
||||||
object UpdateChecker {
|
object UpdateChecker {
|
||||||
private const val KEY_LAST_FETCHED = "update.lastFetched"
|
private const val KEY_LAST_FETCHED = "update.lastFetched"
|
||||||
@@ -49,18 +50,26 @@ object UpdateChecker {
|
|||||||
private val myVer = BuildConfig.VERSION_NAME.toSemVer()
|
private val myVer = BuildConfig.VERSION_NAME.toSemVer()
|
||||||
|
|
||||||
private fun findUpdate(response: JSONArray): GitHubUpdate? {
|
private fun findUpdate(response: JSONArray): GitHubUpdate? {
|
||||||
|
var latest: String? = null
|
||||||
|
var latestVer = myVer
|
||||||
|
var earliest = Long.MAX_VALUE
|
||||||
for (i in 0 until response.length()) {
|
for (i in 0 until response.length()) {
|
||||||
val obj = response.getJSONObject(i)
|
val obj = response.getJSONObject(i)
|
||||||
val name = obj.getString("name")
|
val name = obj.getString("name")
|
||||||
try {
|
val semver = try {
|
||||||
if (name.toSemVer() <= myVer) continue
|
name.toSemVer()
|
||||||
} catch (e: IllegalArgumentException) {
|
} catch (e: IllegalArgumentException) {
|
||||||
Timber.w(e)
|
Timber.w(e)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
return GitHubUpdate(name, Instant.parse(obj.getString("published_at")).toEpochMilli())
|
if (semver <= myVer) continue
|
||||||
|
if (semver > latestVer) {
|
||||||
|
latest = name
|
||||||
|
latestVer = semver
|
||||||
|
}
|
||||||
|
earliest = min(earliest, Instant.parse(obj.getString("published_at")).toEpochMilli())
|
||||||
}
|
}
|
||||||
return null
|
return latest?.let { GitHubUpdate(it, earliest) }
|
||||||
}
|
}
|
||||||
fun check() = flow<AppUpdate?> {
|
fun check() = flow<AppUpdate?> {
|
||||||
emit(app.pref.getString(KEY_VERSION, null)?.let {
|
emit(app.pref.getString(KEY_VERSION, null)?.let {
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import com.google.android.play.core.install.model.InstallStatus
|
|||||||
import com.google.android.play.core.ktx.AppUpdateResult
|
import com.google.android.play.core.ktx.AppUpdateResult
|
||||||
import com.google.android.play.core.ktx.requestUpdateFlow
|
import com.google.android.play.core.ktx.requestUpdateFlow
|
||||||
import kotlinx.coroutines.GlobalScope
|
import kotlinx.coroutines.GlobalScope
|
||||||
|
import kotlinx.coroutines.flow.catch
|
||||||
import kotlinx.coroutines.flow.map
|
import kotlinx.coroutines.flow.map
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
@@ -44,23 +45,26 @@ object UpdateChecker {
|
|||||||
|
|
||||||
private val manager by lazy { AppUpdateManagerFactory.create(app) }
|
private val manager by lazy { AppUpdateManagerFactory.create(app) }
|
||||||
|
|
||||||
fun check() = try {
|
fun check() = manager.requestUpdateFlow().catch { e ->
|
||||||
manager.requestUpdateFlow().map { result ->
|
when (e) {
|
||||||
when (result) {
|
is InstallException -> {
|
||||||
is AppUpdateResult.NotAvailable -> null
|
app.logEvent("InstallErrorCode") { param("errorCode", e.errorCode.toLong()) }
|
||||||
is AppUpdateResult.Available -> UpdateAvailable(result)
|
throw AppUpdate.IgnoredException(e)
|
||||||
is AppUpdateResult.InProgress -> {
|
}
|
||||||
if (result.installState.installStatus() == InstallStatus.CANCELED) null else UpdateDownloading(result)
|
is RuntimeException -> if (e.message == "Failed to bind to the service.") {
|
||||||
}
|
app.logEvent("UpdateBindFailure")
|
||||||
is AppUpdateResult.Downloaded -> UpdateDownloaded(result)
|
throw AppUpdate.IgnoredException(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (e: InstallException) {
|
throw e
|
||||||
app.logEvent("InstallErrorCode") { param("errorCode", e.errorCode.toLong()) }
|
}.map { result ->
|
||||||
throw AppUpdate.IgnoredException(e)
|
when (result) {
|
||||||
} catch (e: RuntimeException) {
|
is AppUpdateResult.NotAvailable -> null
|
||||||
if (e.message != "Failed to bind to the service.") throw e
|
is AppUpdateResult.Available -> UpdateAvailable(result)
|
||||||
app.logEvent("UpdateBindFailure")
|
is AppUpdateResult.InProgress -> {
|
||||||
throw AppUpdate.IgnoredException(e)
|
if (result.installState.installStatus() == InstallStatus.CANCELED) null else UpdateDownloading(result)
|
||||||
|
}
|
||||||
|
is AppUpdateResult.Downloaded -> UpdateDownloaded(result)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user