diff --git a/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java b/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java index 023ec6b660..498060f59e 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java +++ b/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java @@ -86,6 +86,7 @@ import org.thoughtcrime.securesms.sskenvironment.ProfileManager; import org.thoughtcrime.securesms.sskenvironment.ReadReceiptManager; import org.thoughtcrime.securesms.sskenvironment.TypingStatusRepository; import org.thoughtcrime.securesms.util.Broadcaster; +import org.thoughtcrime.securesms.util.VersionUtil; import org.thoughtcrime.securesms.util.dynamiclanguage.LocaleParseHelper; import org.thoughtcrime.securesms.webrtc.CallMessageProcessor; import org.webrtc.PeerConnectionFactory; @@ -142,6 +143,7 @@ public class ApplicationContext extends Application implements DefaultLifecycleO private HandlerThread conversationListHandlerThread; private Handler conversationListHandler; private PersistentLogger persistentLogger; + private VersionUtil versionUtil; @Inject LokiAPIDatabase lokiAPIDatabase; @Inject public Storage storage; @@ -248,6 +250,7 @@ public class ApplicationContext extends Application implements DefaultLifecycleO resubmitProfilePictureIfNeeded(); loadEmojiSearchIndexIfNeeded(); EmojiSource.refresh(); + versionUtil = new VersionUtil(this, textSecurePreferences); NetworkConstraint networkConstraint = new NetworkConstraint.Factory(this).create(); HTTP.INSTANCE.setConnectedToNetwork(networkConstraint::isMet); @@ -274,6 +277,10 @@ public class ApplicationContext extends Application implements DefaultLifecycleO OpenGroupManager.INSTANCE.startPolling(); }); + + // try to fetch last version now and start the version polling + versionUtil.fetchVersionData(); + versionUtil.startTimedVersionCheck(); } @Override @@ -286,12 +293,14 @@ public class ApplicationContext extends Application implements DefaultLifecycleO poller.stopIfNeeded(); } ClosedGroupPollerV2.getShared().stopAll(); + versionUtil.stopTimedVersionCheck(); } @Override public void onTerminate() { stopKovenant(); // Loki OpenGroupManager.INSTANCE.stopPolling(); + versionUtil.clear(); super.onTerminate(); } 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 f5cde8d445..68da185baf 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/util/VersionUtil.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/util/VersionUtil.kt @@ -3,6 +3,12 @@ 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 class VersionUtil( @@ -13,6 +19,9 @@ class VersionUtil( private val handler = Handler(Looper.getMainLooper()) private val runnable: Runnable + private val scope = CoroutineScope(Dispatchers.Default) + private var job: Job? = null + init { runnable = Runnable { // Task to be executed every 4 hours @@ -31,11 +40,28 @@ class VersionUtil( handler.removeCallbacks(runnable) } - private fun fetchVersionData() { + fun clear() { + job?.cancel() + 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 - + 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 {