diff --git a/src/org/thoughtcrime/securesms/ApplicationContext.java b/src/org/thoughtcrime/securesms/ApplicationContext.java index 366c2a1b76..bd088d7426 100644 --- a/src/org/thoughtcrime/securesms/ApplicationContext.java +++ b/src/org/thoughtcrime/securesms/ApplicationContext.java @@ -24,6 +24,7 @@ import android.content.Context; import android.os.AsyncTask; import android.os.Build; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.support.multidex.MultiDexApplication; import com.google.android.gms.security.ProviderInstaller; @@ -52,6 +53,7 @@ import org.thoughtcrime.securesms.logging.CustomSignalProtocolLogger; import org.thoughtcrime.securesms.logging.Log; import org.thoughtcrime.securesms.logging.PersistentLogger; import org.thoughtcrime.securesms.logging.UncaughtExceptionLogger; +import org.thoughtcrime.securesms.loki.LokiAPIDatabase; import org.thoughtcrime.securesms.notifications.NotificationChannels; import org.thoughtcrime.securesms.providers.BlobProvider; import org.thoughtcrime.securesms.push.SignalServiceNetworkAccess; @@ -70,6 +72,8 @@ import org.webrtc.PeerConnectionFactory.InitializationOptions; import org.webrtc.voiceengine.WebRtcAudioManager; import org.webrtc.voiceengine.WebRtcAudioUtils; import org.whispersystems.libsignal.logging.SignalProtocolLoggerProvider; +import org.whispersystems.signalservice.loki.api.LokiAPI; +import org.whispersystems.signalservice.loki.api.LokiLongPoller; import org.whispersystems.signalservice.loki.api.LokiP2PAPI; import org.whispersystems.signalservice.loki.api.LokiP2PAPIDelegate; @@ -101,6 +105,8 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc private ObjectGraph objectGraph; private PersistentLogger persistentLogger; + private LokiLongPoller lokiLongPoller = null; + private volatile boolean isAppVisible; public static ApplicationContext getInstance(Context context) { @@ -149,6 +155,9 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc Log.i(TAG, "App is now visible."); executePendingContactSync(); KeyCachingService.onAppForegrounded(this); + + // Start message receiving if we have registered + startLokiLongPolling(); } @Override @@ -156,6 +165,10 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc isAppVisible = false; Log.i(TAG, "App is no longer visible."); KeyCachingService.onAppBackgrounded(this); + + if (lokiLongPoller != null) { + lokiLongPoller.stop(); + } } @Override @@ -189,6 +202,24 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc return persistentLogger; } + public void startLokiLongPolling() { + initializeLokiLongPoller(); + if (lokiLongPoller != null) { + lokiLongPoller.startIfNecessary(); + } + } + + private void initializeLokiLongPoller() { + if (lokiLongPoller != null) return; + + String hexEncodedPublicKey = TextSecurePreferences.getLocalNumber(this); + if (hexEncodedPublicKey == null) return; + + LokiAPIDatabase database = DatabaseFactory.getLokiAPIDatabase(this); + LokiAPI lokiAPI = new LokiAPI(hexEncodedPublicKey, database); + lokiLongPoller = new LokiLongPoller(hexEncodedPublicKey, lokiAPI); + } + private void initializeSecurityProvider() { try { Class.forName("org.signal.aesgcmprovider.AesGcmCipher"); diff --git a/src/org/thoughtcrime/securesms/loki/KeyPairActivity.kt b/src/org/thoughtcrime/securesms/loki/KeyPairActivity.kt index dbc56ec1c5..c302fdd45e 100644 --- a/src/org/thoughtcrime/securesms/loki/KeyPairActivity.kt +++ b/src/org/thoughtcrime/securesms/loki/KeyPairActivity.kt @@ -7,6 +7,7 @@ import android.content.Intent import android.os.Bundle import android.widget.Toast import kotlinx.android.synthetic.main.activity_key_pair.* +import org.thoughtcrime.securesms.ApplicationContext import org.thoughtcrime.securesms.BaseActionBarActivity import org.thoughtcrime.securesms.ConversationListActivity import org.thoughtcrime.securesms.R @@ -96,6 +97,9 @@ class KeyPairActivity : BaseActionBarActivity() { // TODO: Configure P2P API + // Loki - start the long polling + ApplicationContext.getInstance(this).startLokiLongPolling() + startActivity(Intent(this, ConversationListActivity::class.java)) finish() }