Group Loki logic together in ApplicationContext

This commit is contained in:
Niels Andriesse 2019-06-18 13:23:58 +10:00
parent 6928bfa2ae
commit ab68ae2e58
2 changed files with 37 additions and 48 deletions

View File

@ -24,7 +24,6 @@ import android.content.Context;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Build; import android.os.Build;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.multidex.MultiDexApplication; import android.support.multidex.MultiDexApplication;
import com.google.android.gms.security.ProviderInstaller; import com.google.android.gms.security.ProviderInstaller;
@ -40,11 +39,11 @@ import org.thoughtcrime.securesms.dependencies.InjectableType;
import org.thoughtcrime.securesms.dependencies.SignalCommunicationModule; import org.thoughtcrime.securesms.dependencies.SignalCommunicationModule;
import org.thoughtcrime.securesms.jobmanager.DependencyInjector; import org.thoughtcrime.securesms.jobmanager.DependencyInjector;
import org.thoughtcrime.securesms.jobmanager.JobManager; import org.thoughtcrime.securesms.jobmanager.JobManager;
import org.thoughtcrime.securesms.jobs.FastJobStorage;
import org.thoughtcrime.securesms.jobs.JobManagerFactories;
import org.thoughtcrime.securesms.jobmanager.impl.JsonDataSerializer; import org.thoughtcrime.securesms.jobmanager.impl.JsonDataSerializer;
import org.thoughtcrime.securesms.jobs.CreateSignedPreKeyJob; import org.thoughtcrime.securesms.jobs.CreateSignedPreKeyJob;
import org.thoughtcrime.securesms.jobs.FastJobStorage;
import org.thoughtcrime.securesms.jobs.FcmRefreshJob; import org.thoughtcrime.securesms.jobs.FcmRefreshJob;
import org.thoughtcrime.securesms.jobs.JobManagerFactories;
import org.thoughtcrime.securesms.jobs.MultiDeviceContactUpdateJob; import org.thoughtcrime.securesms.jobs.MultiDeviceContactUpdateJob;
import org.thoughtcrime.securesms.jobs.PushNotificationReceiveJob; import org.thoughtcrime.securesms.jobs.PushNotificationReceiveJob;
import org.thoughtcrime.securesms.jobs.RefreshUnidentifiedDeliveryAbilityJob; import org.thoughtcrime.securesms.jobs.RefreshUnidentifiedDeliveryAbilityJob;
@ -83,7 +82,6 @@ import java.util.Set;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import dagger.ObjectGraph; import dagger.ObjectGraph;
import kotlin.jvm.functions.Function2;
/** /**
* Will be called once when the TextSecure process is created. * Will be called once when the TextSecure process is created.
@ -104,8 +102,7 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc
private IncomingMessageObserver incomingMessageObserver; private IncomingMessageObserver incomingMessageObserver;
private ObjectGraph objectGraph; private ObjectGraph objectGraph;
private PersistentLogger persistentLogger; private PersistentLogger persistentLogger;
private LokiLongPoller lokiLongPoller = null; // Loki
private LokiLongPoller lokiLongPoller = null;
private volatile boolean isAppVisible; private volatile boolean isAppVisible;
@ -136,17 +133,8 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc
initializeBlobProvider(); initializeBlobProvider();
NotificationChannels.create(this); NotificationChannels.create(this);
ProcessLifecycleOwner.get().getLifecycle().addObserver(this); ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
// Loki - Set up P2P API if needed
String hexEncodedPublicKey = TextSecurePreferences.getLocalNumber(this); setUpP2PAPI();
if (hexEncodedPublicKey != null) {
LokiP2PAPI.Companion.configure(hexEncodedPublicKey, new Function2<Boolean, String, Void>() {
@Override
public Void invoke(Boolean aBoolean, String s) {
return null;
}
}, this);
}
} }
@Override @Override
@ -155,9 +143,8 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc
Log.i(TAG, "App is now visible."); Log.i(TAG, "App is now visible.");
executePendingContactSync(); executePendingContactSync();
KeyCachingService.onAppForegrounded(this); KeyCachingService.onAppForegrounded(this);
// Loki - Start long polling if needed
// Start message receiving if we have registered startLongPolling();
startLokiLongPolling();
} }
@Override @Override
@ -165,10 +152,8 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc
isAppVisible = false; isAppVisible = false;
Log.i(TAG, "App is no longer visible."); Log.i(TAG, "App is no longer visible.");
KeyCachingService.onAppBackgrounded(this); KeyCachingService.onAppBackgrounded(this);
// Loki - Stop long polling if needed
if (lokiLongPoller != null) { if (lokiLongPoller != null) { lokiLongPoller.stop(); }
lokiLongPoller.stop();
}
} }
@Override @Override
@ -202,24 +187,6 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc
return persistentLogger; 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() { private void initializeSecurityProvider() {
try { try {
Class.forName("org.signal.aesgcmprovider.AesGcmCipher"); Class.forName("org.signal.aesgcmprovider.AesGcmCipher");
@ -403,8 +370,33 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc
private static class ProviderInitializationException extends RuntimeException { private static class ProviderInitializationException extends RuntimeException {
} }
// region Loki
public void setUpP2PAPI() {
String hexEncodedPublicKey = TextSecurePreferences.getLocalNumber(this);
if (hexEncodedPublicKey == null) { return; }
LokiP2PAPI.Companion.configure(hexEncodedPublicKey, (isOnline, contactPublicKey) -> {
// TODO: Implement
return null;
}, this);
}
@Override @Override
public void ping(@NotNull String s) { public void ping(@NotNull String s) {
// TODO: Implement // TODO: Implement
} }
public void startLongPolling() {
setUpLongPollingIfNeeded();
if (lokiLongPoller != null) { lokiLongPoller.startIfNecessary(); }
}
private void setUpLongPollingIfNeeded() {
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);
}
// endregion
} }

View File

@ -94,12 +94,9 @@ class KeyPairActivity : BaseActionBarActivity() {
IdentityDatabase.VerifiedStatus.VERIFIED, true, System.currentTimeMillis(), true) IdentityDatabase.VerifiedStatus.VERIFIED, true, System.currentTimeMillis(), true)
TextSecurePreferences.setLocalNumber(this, hexEncodedPublicKey) TextSecurePreferences.setLocalNumber(this, hexEncodedPublicKey)
TextSecurePreferences.setPromptedPushRegistration(this, true) TextSecurePreferences.setPromptedPushRegistration(this, true)
val application = ApplicationContext.getInstance(this)
// TODO: Configure P2P API application.setUpP2PAPI()
application.startLongPolling()
// Loki - start the long polling
ApplicationContext.getInstance(this).startLokiLongPolling()
startActivity(Intent(this, ConversationListActivity::class.java)) startActivity(Intent(this, ConversationListActivity::class.java))
finish() finish()
} }