diff --git a/src/org/thoughtcrime/securesms/ApplicationContext.java b/src/org/thoughtcrime/securesms/ApplicationContext.java index 54ba2f67a2..a17bb52e67 100644 --- a/src/org/thoughtcrime/securesms/ApplicationContext.java +++ b/src/org/thoughtcrime/securesms/ApplicationContext.java @@ -56,6 +56,7 @@ import org.thoughtcrime.securesms.logging.PersistentLogger; import org.thoughtcrime.securesms.logging.UncaughtExceptionLogger; import org.thoughtcrime.securesms.loki.BackgroundPollWorker; import org.thoughtcrime.securesms.loki.LokiAPIDatabase; +import org.thoughtcrime.securesms.loki.LokiGroupChatPoller; import org.thoughtcrime.securesms.notifications.NotificationChannels; import org.thoughtcrime.securesms.providers.BlobProvider; import org.thoughtcrime.securesms.push.SignalServiceNetworkAccess; @@ -114,6 +115,7 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc // Loki private LokiLongPoller lokiLongPoller = null; + private LokiGroupChatPoller lokiGroupChatPoller = null; public SignalCommunicationModule communicationModule; private volatile boolean isAppVisible; @@ -421,12 +423,19 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc if (lokiLongPoller != null) { lokiLongPoller.startIfNeeded(); } } - public void setUpPublicChatIfNeeded() { + private void setUpPublicChatIfNeeded() { + if (lokiGroupChatPoller != null) return; + lokiGroupChatPoller = new LokiGroupChatPoller(this, LokiGroupChatAPI.getPublicChatID()); boolean isPublicChatSetUp = TextSecurePreferences.isPublicChatSetUp(this); if (isPublicChatSetUp) return; String id = "loki-group-chat-" + LokiGroupChatAPI.getPublicChatID(); GroupManager.createGroup(id, this, new HashSet<>(), null, "Loki Public Chat", false); TextSecurePreferences.markPublicChatSetUp(this); } + + public void startPublicChatPollingIfNeeded() { + setUpPublicChatIfNeeded(); + lokiGroupChatPoller.startIfNeeded(); + } // endregion } diff --git a/src/org/thoughtcrime/securesms/ConversationListActivity.java b/src/org/thoughtcrime/securesms/ConversationListActivity.java index fa6a4a3c40..dde8c681ae 100644 --- a/src/org/thoughtcrime/securesms/ConversationListActivity.java +++ b/src/org/thoughtcrime/securesms/ConversationListActivity.java @@ -46,7 +46,6 @@ import org.thoughtcrime.securesms.database.Address; import org.thoughtcrime.securesms.database.DatabaseFactory; import org.thoughtcrime.securesms.database.MessagingDatabase.MarkedMessageInfo; import org.thoughtcrime.securesms.lock.RegistrationLockDialog; -import org.thoughtcrime.securesms.loki.LokiGroupChatPoller; import org.thoughtcrime.securesms.notifications.MarkReadReceiver; import org.thoughtcrime.securesms.notifications.MessageNotifier; import org.thoughtcrime.securesms.permissions.Permissions; @@ -58,7 +57,6 @@ import org.thoughtcrime.securesms.util.DynamicNoActionBarTheme; import org.thoughtcrime.securesms.util.DynamicTheme; import org.thoughtcrime.securesms.util.TextSecurePreferences; import org.thoughtcrime.securesms.util.concurrent.SimpleTask; -import org.whispersystems.signalservice.loki.api.LokiGroupChatAPI; import java.util.List; @@ -84,7 +82,7 @@ public class ConversationListActivity extends PassphraseRequiredActionBarActivit dynamicTheme.onCreate(this); dynamicLanguage.onCreate(this); if (TextSecurePreferences.getLocalNumber(this) != null) { - ApplicationContext.getInstance(this).setUpPublicChatIfNeeded(); + ApplicationContext.getInstance(this).startPublicChatPollingIfNeeded(); } } @@ -106,8 +104,6 @@ public class ConversationListActivity extends PassphraseRequiredActionBarActivit RegistrationLockDialog.showReminderIfNecessary(this); TooltipCompat.setTooltipText(searchAction, getText(R.string.SearchToolbar_search_for_conversations_contacts_and_messages)); - - LokiGroupChatPoller.poll(this, LokiGroupChatAPI.getPublicChatID()); } @Override diff --git a/src/org/thoughtcrime/securesms/loki/LokiGroupChatPoller.kt b/src/org/thoughtcrime/securesms/loki/LokiGroupChatPoller.kt index a474e54b49..8f30a07873 100644 --- a/src/org/thoughtcrime/securesms/loki/LokiGroupChatPoller.kt +++ b/src/org/thoughtcrime/securesms/loki/LokiGroupChatPoller.kt @@ -1,6 +1,7 @@ package org.thoughtcrime.securesms.loki import android.content.Context +import android.os.Handler import android.util.Log import org.thoughtcrime.securesms.jobs.PushDecryptJob import org.whispersystems.libsignal.util.guava.Optional @@ -10,10 +11,33 @@ import org.whispersystems.signalservice.api.messages.SignalServiceGroup import org.whispersystems.signalservice.api.push.SignalServiceAddress import org.whispersystems.signalservice.loki.api.LokiGroupChatAPI -object LokiGroupChatPoller { +class LokiGroupChatPoller(private val context: Context, private val groupID: Long) { + private val handler = Handler() + private var hasStarted = false - @JvmStatic - fun poll(context: Context, groupID: Long) { + private val task = object : Runnable { + + override fun run() { + poll() + handler.postDelayed(this, pollInterval) + } + } + + companion object { + private val pollInterval: Long = 5 * 1000 + } + + fun startIfNeeded() { + if (hasStarted) return + task.run() + hasStarted = true + } + + fun stop() { + handler.removeCallbacks(task) + } + + private fun poll() { LokiGroupChatAPI.getMessages(groupID).success { messages -> messages.map { message -> val id = "loki-group-chat-$groupID".toByteArray()