diff --git a/mobile/src/freedom/java/be/mygod/vpnhotspot/util/UpdateChecker.kt b/mobile/src/freedom/java/be/mygod/vpnhotspot/util/UpdateChecker.kt index f95497cf..bfff372f 100644 --- a/mobile/src/freedom/java/be/mygod/vpnhotspot/util/UpdateChecker.kt +++ b/mobile/src/freedom/java/be/mygod/vpnhotspot/util/UpdateChecker.kt @@ -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() } diff --git a/mobile/src/google/java/be/mygod/vpnhotspot/util/UpdateChecker.kt b/mobile/src/google/java/be/mygod/vpnhotspot/util/UpdateChecker.kt index 1cd885c7..42186214 100644 --- a/mobile/src/google/java/be/mygod/vpnhotspot/util/UpdateChecker.kt +++ b/mobile/src/google/java/be/mygod/vpnhotspot/util/UpdateChecker.kt @@ -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,14 +44,19 @@ object UpdateChecker { private val manager by lazy { AppUpdateManagerFactory.create(app) } - fun check() = manager.requestUpdateFlow().map { result -> - when (result) { - is AppUpdateResult.NotAvailable -> null - is AppUpdateResult.Available -> UpdateAvailable(result) - is AppUpdateResult.InProgress -> { - if (result.installState.installStatus() == InstallStatus.CANCELED) null else UpdateDownloading(result) + fun check() = try { + manager.requestUpdateFlow().map { result -> + when (result) { + is AppUpdateResult.NotAvailable -> null + is AppUpdateResult.Available -> UpdateAvailable(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) } } diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/MainActivity.kt b/mobile/src/main/java/be/mygod/vpnhotspot/MainActivity.kt index c7a8cde6..00b57efd 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/MainActivity.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/MainActivity.kt @@ -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() diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/util/AppUpdate.kt b/mobile/src/main/java/be/mygod/vpnhotspot/util/AppUpdate.kt index 4193bf6e..aacb9fa8 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/util/AppUpdate.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/util/AppUpdate.kt @@ -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