Unify background pollers and reduce polling interval

We were getting rate limited
pull/223/head
nielsandriesse 4 years ago
parent eded767e56
commit 07737262ca

@ -673,11 +673,6 @@
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver android:name="org.thoughtcrime.securesms.loki.api.BackgroundOpenGroupPollWorker">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<!-- Session -->
<service
android:name="org.thoughtcrime.securesms.jobmanager.JobSchedulerScheduler$SystemService"

@ -59,7 +59,6 @@ import org.thoughtcrime.securesms.logging.Log;
import org.thoughtcrime.securesms.logging.PersistentLogger;
import org.thoughtcrime.securesms.logging.UncaughtExceptionLogger;
import org.thoughtcrime.securesms.loki.activities.HomeActivity;
import org.thoughtcrime.securesms.loki.api.BackgroundOpenGroupPollWorker;
import org.thoughtcrime.securesms.loki.api.BackgroundPollWorker;
import org.thoughtcrime.securesms.loki.api.LokiPublicChatManager;
import org.thoughtcrime.securesms.loki.api.LokiPushNotificationManager;
@ -366,7 +365,6 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc
LocalBackupListener.schedule(this);
RotateSenderCertificateListener.schedule(this);
BackgroundPollWorker.schedule(this); // Loki
BackgroundOpenGroupPollWorker.schedule(this); // Loki
if (BuildConfig.PLAY_STORE_DISABLED) {
UpdateApkRefreshListener.schedule(this);

@ -1,38 +0,0 @@
package org.thoughtcrime.securesms.loki.api
import android.content.Context
import android.content.Intent
import org.thoughtcrime.securesms.database.DatabaseFactory
import org.thoughtcrime.securesms.service.PersistentAlarmManagerListener
import org.thoughtcrime.securesms.util.TextSecurePreferences
import java.util.concurrent.TimeUnit
class BackgroundOpenGroupPollWorker : PersistentAlarmManagerListener() {
companion object {
private val pollInterval = TimeUnit.MINUTES.toMillis(4)
@JvmStatic
fun schedule(context: Context) {
BackgroundOpenGroupPollWorker().onReceive(context, Intent())
}
}
override fun getNextScheduledExecutionTime(context: Context): Long {
return TextSecurePreferences.getOpenGroupBackgroundPollTime(context)
}
override fun onAlarm(context: Context, scheduledTime: Long): Long {
if (scheduledTime != 0L) {
val openGroups = DatabaseFactory.getLokiThreadDatabase(context).getAllPublicChats().map { it.value }
for (openGroup in openGroups) {
val poller = LokiPublicChatPoller(context, openGroup)
poller.stop()
poller.pollForNewMessages()
}
}
val nextTime = System.currentTimeMillis() + pollInterval
TextSecurePreferences.setOpenGroupBackgroundPollTime(context, nextTime)
return nextTime
}
}

@ -15,7 +15,7 @@ import java.util.concurrent.TimeUnit
class BackgroundPollWorker : PersistentAlarmManagerListener() {
companion object {
private val pollInterval = TimeUnit.MINUTES.toMillis(2)
private val pollInterval = TimeUnit.MINUTES.toMillis(10)
@JvmStatic
fun schedule(context: Context) {
@ -28,21 +28,28 @@ class BackgroundPollWorker : PersistentAlarmManagerListener() {
}
override fun onAlarm(context: Context, scheduledTime: Long): Long {
if (TextSecurePreferences.isUsingFCM(context)) { return 0L }
if (scheduledTime != 0L) {
val userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(context)
val lokiAPIDatabase = DatabaseFactory.getLokiAPIDatabase(context)
try {
val applicationContext = context.applicationContext as ApplicationContext
val broadcaster = applicationContext.broadcaster
LokiAPI.configureIfNeeded(userHexEncodedPublicKey, lokiAPIDatabase, broadcaster)
LokiAPI.shared.getMessages().map { messages ->
messages.forEach {
PushContentReceiveJob(context).processEnvelope(SignalServiceEnvelope(it), false)
if (TextSecurePreferences.isUsingFCM(context)) {
val userPublicKey = TextSecurePreferences.getLocalNumber(context)
val lokiAPIDatabase = DatabaseFactory.getLokiAPIDatabase(context)
try {
val applicationContext = context.applicationContext as ApplicationContext
val broadcaster = applicationContext.broadcaster
LokiAPI.configureIfNeeded(userPublicKey, lokiAPIDatabase, broadcaster)
LokiAPI.shared.getMessages().map { messages ->
messages.forEach {
PushContentReceiveJob(context).processEnvelope(SignalServiceEnvelope(it), false)
}
}
} catch (exception: Throwable) {
// Do nothing
}
} catch (exception: Throwable) {
// Do nothing
}
val openGroups = DatabaseFactory.getLokiThreadDatabase(context).getAllPublicChats().map { it.value }
for (openGroup in openGroups) {
val poller = LokiPublicChatPoller(context, openGroup)
poller.stop()
poller.pollForNewMessages()
}
}
val nextTime = System.currentTimeMillis() + pollInterval

Loading…
Cancel
Save