From 42733c910d3510248698634860481326a859b7ee Mon Sep 17 00:00:00 2001 From: ThomasSession Date: Tue, 30 Jul 2024 13:30:58 +1000 Subject: [PATCH] Clean up logic Fixed randomly found timeunit error --- .../securesms/ApplicationContext.java | 5 ++- .../org/thoughtcrime/securesms/MuteDialog.kt | 2 +- .../securesms/util/VersionUtil.kt | 32 ++++++++----------- 3 files changed, 16 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java b/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java index 498060f59e..cfdc16e06c 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java +++ b/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java @@ -250,7 +250,7 @@ public class ApplicationContext extends Application implements DefaultLifecycleO resubmitProfilePictureIfNeeded(); loadEmojiSearchIndexIfNeeded(); EmojiSource.refresh(); - versionUtil = new VersionUtil(this, textSecurePreferences); + versionUtil = new VersionUtil(textSecurePreferences); NetworkConstraint networkConstraint = new NetworkConstraint.Factory(this).create(); HTTP.INSTANCE.setConnectedToNetwork(networkConstraint::isMet); @@ -278,8 +278,7 @@ public class ApplicationContext extends Application implements DefaultLifecycleO OpenGroupManager.INSTANCE.startPolling(); }); - // try to fetch last version now and start the version polling - versionUtil.fetchVersionData(); + // fetch last version data versionUtil.startTimedVersionCheck(); } diff --git a/app/src/main/java/org/thoughtcrime/securesms/MuteDialog.kt b/app/src/main/java/org/thoughtcrime/securesms/MuteDialog.kt index f294e387ff..071da43311 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/MuteDialog.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/MuteDialog.kt @@ -18,7 +18,7 @@ fun showMuteDialog( private enum class Option(@StringRes val stringRes: Int, val getTime: () -> Long) { ONE_HOUR(R.string.arrays__mute_for_one_hour, duration = TimeUnit.HOURS.toMillis(1)), - TWO_HOURS(R.string.arrays__mute_for_two_hours, duration = TimeUnit.DAYS.toMillis(2)), + TWO_HOURS(R.string.arrays__mute_for_two_hours, duration = TimeUnit.HOURS.toMillis(2)), ONE_DAY(R.string.arrays__mute_for_one_day, duration = TimeUnit.DAYS.toMillis(1)), SEVEN_DAYS(R.string.arrays__mute_for_seven_days, duration = TimeUnit.DAYS.toMillis(7)), FOREVER(R.string.arrays__mute_forever, getTime = { Long.MAX_VALUE }); diff --git a/app/src/main/java/org/thoughtcrime/securesms/util/VersionUtil.kt b/app/src/main/java/org/thoughtcrime/securesms/util/VersionUtil.kt index 68da185baf..9781c1105a 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/util/VersionUtil.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/util/VersionUtil.kt @@ -1,20 +1,19 @@ package org.thoughtcrime.securesms.util -import android.content.Context import android.os.Handler import android.os.Looper -import android.util.Log import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job import kotlinx.coroutines.launch import org.session.libsession.messaging.file_server.FileServerApi import org.session.libsession.utilities.TextSecurePreferences +import java.util.concurrent.TimeUnit class VersionUtil( - private val context: Context, private val prefs: TextSecurePreferences ) { + private val FOUR_HOURS: Long = TimeUnit.HOURS.toMillis(4) private val handler = Handler(Looper.getMainLooper()) private val runnable: Runnable @@ -24,12 +23,8 @@ class VersionUtil( init { runnable = Runnable { - // Task to be executed every 4 hours - fetchVersionData() + fetchAndScheduleNextVersionCheck() } - - // Re-schedule the task - handler.postDelayed(runnable, FOUR_HOURS) } fun startTimedVersionCheck() { @@ -45,26 +40,25 @@ class VersionUtil( stopTimedVersionCheck() } - fun fetchVersionData() { - Log.d("", "***** Trying to fetch version. Last check: ${prefs.getLastVersionCheck()}") - // only perform this if at least 4h has elapsed since th last successful check - if(prefs.getLastVersionCheck() < FOUR_HOURS) return + private fun fetchAndScheduleNextVersionCheck() { + fetchVersionData() + handler.postDelayed(runnable, FOUR_HOURS) + } + private fun fetchVersionData() { + // only perform this if at least 4h has elapsed since th last successful check + val lastCheck = System.currentTimeMillis() - prefs.getLastVersionCheck() + if(lastCheck < FOUR_HOURS) return + + job?.cancel() job = scope.launch { try { // perform the version check - Log.d("", "***** Fetching last version") val clientVersion = FileServerApi.getClientVersion() - Log.d("", "***** Got version: $clientVersion") prefs.setLastVersionCheck() } catch (e: Exception) { // we can silently ignore the error - Log.e("", "***** Error fetching version", e) } } } - - companion object { - private const val FOUR_HOURS = 4 * 60 * 60 * 1000L // 4 hours in milliseconds - } } \ No newline at end of file