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