Actively poll public chat group if the app is in the foreground

This commit is contained in:
Niels Andriesse 2019-08-06 16:18:24 +10:00
parent 149d4b0181
commit 9e4b70058c
3 changed files with 38 additions and 9 deletions

View File

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

View File

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

View File

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