Misc fixes for update checking
This commit is contained in:
@@ -74,9 +74,11 @@ object UpdateChecker {
|
||||
currentCoroutineContext().ensureActive()
|
||||
val conn = URL("https://api.github.com/repos/Mygod/VPNHotspot/releases?per_page=100")
|
||||
.openConnection() as HttpURLConnection
|
||||
var reset: Long? = null
|
||||
app.pref.edit {
|
||||
try {
|
||||
conn.setRequestProperty("Accept", "application/vnd.github.v3+json")
|
||||
reset = conn.getHeaderField("X-RateLimit-Reset")?.toLongOrNull()
|
||||
val update = findUpdate(JSONArray(withContext(Dispatchers.IO) {
|
||||
conn.inputStream.bufferedReader().readText()
|
||||
}))
|
||||
@@ -94,7 +96,7 @@ object UpdateChecker {
|
||||
putLong(KEY_LAST_FETCHED, System.currentTimeMillis())
|
||||
}
|
||||
}
|
||||
delay(System.currentTimeMillis() - (conn.getHeaderField("X-RateLimit-Reset")?.toLongOrNull() ?: 0) * 1000)
|
||||
reset?.let { delay(System.currentTimeMillis() - it * 1000) }
|
||||
}
|
||||
}.cancellable()
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import android.app.Activity
|
||||
import android.net.Uri
|
||||
import be.mygod.vpnhotspot.App.Companion.app
|
||||
import com.google.android.play.core.appupdate.AppUpdateManagerFactory
|
||||
import com.google.android.play.core.install.InstallException
|
||||
import com.google.android.play.core.install.model.InstallErrorCode
|
||||
import com.google.android.play.core.install.model.InstallStatus
|
||||
import com.google.android.play.core.ktx.AppUpdateResult
|
||||
@@ -43,7 +44,8 @@ object UpdateChecker {
|
||||
|
||||
private val manager by lazy { AppUpdateManagerFactory.create(app) }
|
||||
|
||||
fun check() = manager.requestUpdateFlow().map { result ->
|
||||
fun check() = try {
|
||||
manager.requestUpdateFlow().map { result ->
|
||||
when (result) {
|
||||
is AppUpdateResult.NotAvailable -> null
|
||||
is AppUpdateResult.Available -> UpdateAvailable(result)
|
||||
@@ -53,4 +55,8 @@ object UpdateChecker {
|
||||
is AppUpdateResult.Downloaded -> UpdateDownloaded(result)
|
||||
}
|
||||
}
|
||||
} catch (e: InstallException) {
|
||||
app.logEvent("InstallErrorCode") { param("errorCode", e.errorCode.toLong()) }
|
||||
throw AppUpdate.IgnoredException(e)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,6 +68,8 @@ class MainActivity : AppCompatActivity(), NavigationBarView.OnItemSelectedListen
|
||||
try {
|
||||
UpdateChecker.check().collect(this@MainActivity::onAppUpdateAvailable)
|
||||
} catch (_: CancellationException) {
|
||||
} catch (e: AppUpdate.IgnoredException) {
|
||||
Timber.d(e)
|
||||
} catch (e: Exception) {
|
||||
Timber.w(e)
|
||||
SmartSnackbar.make(e).show()
|
||||
|
||||
@@ -3,6 +3,8 @@ package be.mygod.vpnhotspot.util
|
||||
import android.app.Activity
|
||||
|
||||
interface AppUpdate {
|
||||
class IgnoredException(cause: Throwable?) : RuntimeException(cause)
|
||||
|
||||
val downloaded: Boolean? get() = null
|
||||
val message: String? get() = null
|
||||
val stalenessDays: Int? get() = null
|
||||
|
||||
Reference in New Issue
Block a user