From 40db23d341ff7a1e4a40a6199966df05b17b0b74 Mon Sep 17 00:00:00 2001 From: bemusementpark Date: Wed, 31 Jul 2024 15:52:38 +0930 Subject: [PATCH] Fix setLastVersion called onException --- .../securesms/ApplicationContext.java | 2 +- .../securesms/util/VersionDataFetcher.kt | 49 ++++++++----------- 2 files changed, 22 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java b/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java index a50e003c65..daa159501b 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java +++ b/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java @@ -297,7 +297,7 @@ public class ApplicationContext extends Application implements DefaultLifecycleO public void onTerminate() { stopKovenant(); // Loki OpenGroupManager.INSTANCE.stopPolling(); - versionDataFetcher.clear(); + versionDataFetcher.stopTimedVersionCheck(); super.onTerminate(); } diff --git a/app/src/main/java/org/thoughtcrime/securesms/util/VersionDataFetcher.kt b/app/src/main/java/org/thoughtcrime/securesms/util/VersionDataFetcher.kt index 48be478c95..2868e4e8db 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/util/VersionDataFetcher.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/util/VersionDataFetcher.kt @@ -20,43 +20,36 @@ class VersionDataFetcher @Inject constructor( private val prefs: TextSecurePreferences ) { private val handler = Handler(Looper.getMainLooper()) - private val runnable = Runnable { - fetchVersionData() - } - - private val scope = CoroutineScope(Dispatchers.Default) - - fun startTimedVersionCheck() { - stopTimedVersionCheck() - - // Call immediately if 4h or more has elapsed since the last successful check else schedule. - handler.postDelayed( - runnable, - REFRESH_TIME_MS + prefs.getLastVersionCheck() - System.currentTimeMillis() - ) - } - - fun stopTimedVersionCheck() { - handler.removeCallbacks(runnable) - } - - fun clear() { - stopTimedVersionCheck() - } - - private fun fetchVersionData() { + private val fetchVersionData = Runnable { scope.launch { try { // Perform the version check val clientVersion = FileServerApi.getClientVersion() Log.i(TAG, "Fetched version data: $clientVersion") + prefs.setLastVersionCheck() + startTimedVersionCheck() } catch (e: Exception) { // We can silently ignore the error Log.e(TAG, "Error fetching version data", e) + // Schedule the next check for 4 hours from now, but do not setLastVersionCheck + // so the app will retry when the app is next foregrounded. + startTimedVersionCheck(REFRESH_TIME_MS) } - - prefs.setLastVersionCheck() - startTimedVersionCheck() } } + + private val scope = CoroutineScope(Dispatchers.Default) + + fun startTimedVersionCheck( + delayMillis: Long = REFRESH_TIME_MS + prefs.getLastVersionCheck() - System.currentTimeMillis() + ) { + stopTimedVersionCheck() + + // Call immediately if 4h or more has elapsed since the last successful check else schedule. + handler.postDelayed(fetchVersionData, delayMillis) + } + + fun stopTimedVersionCheck() { + handler.removeCallbacks(fetchVersionData) + } } \ No newline at end of file