Fix setLastVersion called onException

This commit is contained in:
bemusementpark 2024-07-31 15:52:38 +09:30
parent 7fa3d9f3ff
commit 40db23d341
2 changed files with 22 additions and 29 deletions

View File

@ -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();
}

View File

@ -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)
}
}