diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 678d284d4f..36c9f0787e 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -435,6 +435,7 @@
android:exported="true"
android:configChanges="touchscreen|keyboard|keyboardHidden|orientation|screenLayout|screenSize"/>
+
+
@@ -669,6 +671,14 @@
+
+
+
+
+
+
+
+
, Unit>() {
+ lokiLongPoller = new LokiLongPoller(userHexEncodedPublicKey, database, new Function1, Unit>() {
@Override
- public Unit invoke(List envelopes) {
- for (SignalServiceProtos.Envelope proto : envelopes) {
- SignalServiceEnvelope envelope;
- if (proto.getSource() != null && proto.getSourceDevice() > 0) {
- envelope = new SignalServiceEnvelope(proto.getType().getNumber(), proto.getSource(), proto.getSourceDevice(), proto.getTimestamp(),
- proto.getLegacyMessage().toByteArray(), proto.getContent().toByteArray(), proto.getServerTimestamp(), proto.getServerGuid());
- } else {
- envelope = new SignalServiceEnvelope(proto.getType().getNumber(), proto.getTimestamp(), proto.getLegacyMessage().toByteArray(),
- proto.getContent().toByteArray(), proto.getServerTimestamp(), proto.getServerGuid());
- }
- new PushContentReceiveJob(context).processEnvelope(envelope);
+ public Unit invoke(List protos) {
+ for (SignalServiceProtos.Envelope proto : protos) {
+ new PushContentReceiveJob(context).processEnvelope(new SignalServiceEnvelope(proto));
}
return Unit.INSTANCE;
}
diff --git a/src/org/thoughtcrime/securesms/loki/BackgroundPollWorker.kt b/src/org/thoughtcrime/securesms/loki/BackgroundPollWorker.kt
index 93f28e5301..d0ceb91125 100644
--- a/src/org/thoughtcrime/securesms/loki/BackgroundPollWorker.kt
+++ b/src/org/thoughtcrime/securesms/loki/BackgroundPollWorker.kt
@@ -1,20 +1,44 @@
package org.thoughtcrime.securesms.loki
import android.content.Context
-import androidx.work.Worker
-import androidx.work.WorkerParameters
+import android.content.Intent
+import org.thoughtcrime.securesms.database.DatabaseFactory
+import org.thoughtcrime.securesms.jobs.PushContentReceiveJob
+import org.thoughtcrime.securesms.service.PersistentAlarmManagerListener
+import org.thoughtcrime.securesms.util.TextSecurePreferences
+import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope
import org.whispersystems.signalservice.loki.api.LokiAPI
-import org.whispersystems.signalservice.loki.api.LokiAPIDatabaseProtocol
+import java.util.concurrent.TimeUnit
-class BackgroundPollWorker(private val userHexEncodedPublicKey: String, private val apiDatabase: LokiAPIDatabaseProtocol, context: Context, parameters: WorkerParameters) : Worker(context, parameters) {
+class BackgroundPollWorker : PersistentAlarmManagerListener() {
- override fun doWork(): Result {
- return try {
- LokiAPI(userHexEncodedPublicKey, apiDatabase).getMessages().get()
- // TODO: Process envelopes
- Result.success()
- } catch (exception: Exception) {
- Result.failure()
+ companion object {
+ private val pollInterval = TimeUnit.MINUTES.toMillis(5)
+
+ @JvmStatic
+ fun schedule(context: Context) {
+ BackgroundPollWorker().onReceive(context, Intent())
}
}
-}
\ No newline at end of file
+
+ override fun getNextScheduledExecutionTime(context: Context): Long {
+ return TextSecurePreferences.getBackgroundPollTime(context)
+ }
+
+ override fun onAlarm(context: Context, scheduledTime: Long): Long {
+ if (scheduledTime != 0L) {
+ val userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(context)
+ val apiDatabase = DatabaseFactory.getLokiAPIDatabase(context)
+ try {
+ LokiAPI(userHexEncodedPublicKey, apiDatabase).getMessages().get().forEach {
+ PushContentReceiveJob(context).processEnvelope(SignalServiceEnvelope(it))
+ }
+ } catch (exception: Throwable) {
+ // Do nothing
+ }
+ }
+ val nextTime = System.currentTimeMillis() + pollInterval
+ TextSecurePreferences.setBackgroundPollTime(context, nextTime)
+ return nextTime
+ }
+}
diff --git a/src/org/thoughtcrime/securesms/loki/KeyPairActivity.kt b/src/org/thoughtcrime/securesms/loki/KeyPairActivity.kt
index 180ec53719..cfd62ed845 100644
--- a/src/org/thoughtcrime/securesms/loki/KeyPairActivity.kt
+++ b/src/org/thoughtcrime/securesms/loki/KeyPairActivity.kt
@@ -144,7 +144,7 @@ class KeyPairActivity : BaseActionBarActivity() {
TextSecurePreferences.setPromptedPushRegistration(this, true)
val application = ApplicationContext.getInstance(this)
application.setUpP2PAPI()
- application.startLongPolling()
+ application.startLongPollingIfNeeded()
startActivity(Intent(this, ConversationListActivity::class.java))
finish()
}
diff --git a/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java b/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java
index cd769178e4..effcc3f620 100644
--- a/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java
+++ b/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java
@@ -1126,4 +1126,12 @@ public class TextSecurePreferences {
return defaultValues;
}
}
+
+ public static long getBackgroundPollTime(Context context) {
+ return getLongPreference(context, "background_poll_time", 0L);
+ }
+
+ public static void setBackgroundPollTime(Context context, long backgroundPollTime) {
+ setLongPreference(context, "background_poll_time", backgroundPollTime);
+ }
}