From 901d627d32d4aab1b6ab6f245243ba308b609303 Mon Sep 17 00:00:00 2001 From: Mikunj Date: Mon, 23 Sep 2019 11:28:17 +1000 Subject: [PATCH] Linked storage api and database. --- .../thoughtcrime/securesms/ApplicationContext.java | 12 ++++++------ .../securesms/database/DatabaseFactory.java | 13 +++++++------ .../securesms/loki/LokiMultiDeviceDatabase.kt | 14 ++++++++++---- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/org/thoughtcrime/securesms/ApplicationContext.java b/src/org/thoughtcrime/securesms/ApplicationContext.java index 53bd2568e8..9cdc9f07f5 100644 --- a/src/org/thoughtcrime/securesms/ApplicationContext.java +++ b/src/org/thoughtcrime/securesms/ApplicationContext.java @@ -83,12 +83,7 @@ import org.webrtc.voiceengine.WebRtcAudioUtils; import org.whispersystems.libsignal.logging.SignalProtocolLoggerProvider; import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope; import org.whispersystems.signalservice.internal.push.SignalServiceProtos; -import org.whispersystems.signalservice.loki.api.LokiGroupChat; -import org.whispersystems.signalservice.loki.api.LokiGroupChatAPI; -import org.whispersystems.signalservice.loki.api.LokiLongPoller; -import org.whispersystems.signalservice.loki.api.LokiP2PAPI; -import org.whispersystems.signalservice.loki.api.LokiP2PAPIDelegate; -import org.whispersystems.signalservice.loki.api.LokiRSSFeed; +import org.whispersystems.signalservice.loki.api.*; import org.whispersystems.signalservice.loki.utilities.Analytics; import java.security.Security; @@ -164,6 +159,7 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc ProcessLifecycleOwner.get().getLifecycle().addObserver(this); // Loki - Set up P2P API if needed setUpP2PAPI(); + setUpStorageAPI(); // Loki - Set up beta analytics if (!BuildConfig.DEBUG) { Fabric.with(this, new Crashlytics()); @@ -414,6 +410,10 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc } // region Loki + public void setUpStorageAPI() { + LokiStorageAPI.Companion.configure(DatabaseFactory.getLokiMultiDeviceDatabase(this)); + } + public void setUpP2PAPI() { String hexEncodedPublicKey = TextSecurePreferences.getLocalNumber(this); if (hexEncodedPublicKey == null) { return; } diff --git a/src/org/thoughtcrime/securesms/database/DatabaseFactory.java b/src/org/thoughtcrime/securesms/database/DatabaseFactory.java index 63ec6943f3..f5e376db73 100644 --- a/src/org/thoughtcrime/securesms/database/DatabaseFactory.java +++ b/src/org/thoughtcrime/securesms/database/DatabaseFactory.java @@ -31,12 +31,7 @@ import org.thoughtcrime.securesms.crypto.MasterSecret; import org.thoughtcrime.securesms.database.helpers.ClassicOpenHelper; import org.thoughtcrime.securesms.database.helpers.SQLCipherMigrationHelper; import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper; -import org.thoughtcrime.securesms.loki.LokiAPIDatabase; -import org.thoughtcrime.securesms.loki.LokiPreKeyRecordDatabase; -import org.thoughtcrime.securesms.loki.LokiPreKeyBundleDatabase; -import org.thoughtcrime.securesms.loki.LokiMessageDatabase; -import org.thoughtcrime.securesms.loki.LokiThreadDatabase; -import org.thoughtcrime.securesms.loki.LokiUserDatabase; +import org.thoughtcrime.securesms.loki.*; import org.thoughtcrime.securesms.util.TextSecurePreferences; public class DatabaseFactory { @@ -68,6 +63,7 @@ public class DatabaseFactory { // Loki private final LokiAPIDatabase lokiAPIDatabase; + private final LokiMultiDeviceDatabase lokiMultiDeviceDatabase; private final LokiPreKeyRecordDatabase lokiContactPreKeyDatabase; private final LokiPreKeyBundleDatabase lokiPreKeyBundleDatabase; private final LokiMessageDatabase lokiMessageDatabase; @@ -168,6 +164,10 @@ public class DatabaseFactory { return getInstance(context).lokiAPIDatabase; } + public static LokiMultiDeviceDatabase getLokiMultiDeviceDatabase(Context context) { + return getInstance(context).lokiMultiDeviceDatabase; + } + public static LokiPreKeyRecordDatabase getLokiPreKeyRecordDatabase(Context context) { return getInstance(context).lokiContactPreKeyDatabase; } @@ -221,6 +221,7 @@ public class DatabaseFactory { this.jobDatabase = new JobDatabase(context, databaseHelper); this.stickerDatabase = new StickerDatabase(context, databaseHelper, attachmentSecret); this.lokiAPIDatabase = new LokiAPIDatabase(context, databaseHelper); + this.lokiMultiDeviceDatabase = new LokiMultiDeviceDatabase(context, databaseHelper); this.lokiContactPreKeyDatabase = new LokiPreKeyRecordDatabase(context, databaseHelper); this.lokiPreKeyBundleDatabase = new LokiPreKeyBundleDatabase(context, databaseHelper); this.lokiMessageDatabase = new LokiMessageDatabase(context, databaseHelper); diff --git a/src/org/thoughtcrime/securesms/loki/LokiMultiDeviceDatabase.kt b/src/org/thoughtcrime/securesms/loki/LokiMultiDeviceDatabase.kt index 081a3f247a..7c11166308 100644 --- a/src/org/thoughtcrime/securesms/loki/LokiMultiDeviceDatabase.kt +++ b/src/org/thoughtcrime/securesms/loki/LokiMultiDeviceDatabase.kt @@ -7,9 +7,10 @@ import org.thoughtcrime.securesms.database.Database import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper import org.thoughtcrime.securesms.util.Base64 import org.whispersystems.signalservice.loki.api.LokiPairingAuthorisation +import org.whispersystems.signalservice.loki.api.LokiStorageAPIDatabaseProtocol import java.util.* -class LokiMultiDeviceDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(context, helper) { +class LokiMultiDeviceDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(context, helper), LokiStorageAPIDatabaseProtocol { companion object { // Authorisation @@ -27,7 +28,7 @@ class LokiMultiDeviceDatabase(context: Context, helper: SQLCipherOpenHelper) : D ");" } - fun insertOrUpdatePairingAuthorisation(authorisation: LokiPairingAuthorisation) { + override fun insertOrUpdatePairingAuthorisation(authorisation: LokiPairingAuthorisation) { val database = databaseHelper.writableDatabase val values = ContentValues() values.put(primaryDevice, authorisation.primaryDevicePubKey) @@ -37,6 +38,11 @@ class LokiMultiDeviceDatabase(context: Context, helper: SQLCipherOpenHelper) : D database.insertOrUpdate(authorisation_table, values, "$primaryDevice = ? AND $secondaryDevice = ?", arrayOf(authorisation.primaryDevicePubKey, authorisation.secondaryDevicePubKey)) } + override fun removePairingAuthorisations(pubKey: String) { + val database = databaseHelper.readableDatabase + database.delete(authorisation_table, "$primaryDevice = ? OR $secondaryDevice = ?", arrayOf(pubKey, pubKey)) + } + fun getAuthorisationForSecondaryDevice(pubKey: String): LokiPairingAuthorisation? { val database = databaseHelper.readableDatabase return database.get(authorisation_table, "$secondaryDevice = ?", arrayOf(pubKey)) { cursor -> @@ -48,7 +54,7 @@ class LokiMultiDeviceDatabase(context: Context, helper: SQLCipherOpenHelper) : D } } - fun getSecondaryDevices(primaryDevicePubKey: String): List { + override fun getSecondaryDevices(primaryDevicePubKey: String): List { val database = databaseHelper.readableDatabase var cursor: Cursor? = null @@ -68,7 +74,7 @@ class LokiMultiDeviceDatabase(context: Context, helper: SQLCipherOpenHelper) : D return results } - fun getPrimaryDevice(secondaryDevicePubKey: String): String? { + override fun getPrimaryDevice(secondaryDevicePubKey: String): String? { val database = databaseHelper.readableDatabase return database.get(authorisation_table, "$secondaryDevice = ?", arrayOf(secondaryDevicePubKey)) { cursor -> cursor.getString(primaryDevice)