From 956f20fc0e87ce000556f4ab4deba4539f6e020b Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Fri, 19 Jul 2019 15:02:47 +1000 Subject: [PATCH] Use display name --- .../securesms/database/DatabaseFactory.java | 7 +++++ .../database/helpers/SQLCipherOpenHelper.java | 2 ++ .../securesms/jobs/PushDecryptJob.java | 2 +- .../loki/LokiUserDisplayNameDatabase.kt | 31 +++++++++++++++++++ .../securesms/recipients/Recipient.java | 13 ++++++-- .../recipients/RecipientProvider.java | 4 +-- 6 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 src/org/thoughtcrime/securesms/loki/LokiUserDisplayNameDatabase.kt diff --git a/src/org/thoughtcrime/securesms/database/DatabaseFactory.java b/src/org/thoughtcrime/securesms/database/DatabaseFactory.java index 1b89e35611..53e1471f53 100644 --- a/src/org/thoughtcrime/securesms/database/DatabaseFactory.java +++ b/src/org/thoughtcrime/securesms/database/DatabaseFactory.java @@ -36,6 +36,7 @@ import org.thoughtcrime.securesms.loki.LokiPreKeyRecordDatabase; import org.thoughtcrime.securesms.loki.LokiPreKeyBundleDatabase; import org.thoughtcrime.securesms.loki.LokiMessageFriendRequestDatabase; import org.thoughtcrime.securesms.loki.LokiThreadFriendRequestDatabase; +import org.thoughtcrime.securesms.loki.LokiUserDisplayNameDatabase; import org.thoughtcrime.securesms.util.TextSecurePreferences; public class DatabaseFactory { @@ -70,6 +71,7 @@ public class DatabaseFactory { private final LokiPreKeyBundleDatabase lokiPreKeyBundleDatabase; private final LokiMessageFriendRequestDatabase lokiMessageFriendRequestDatabase; private final LokiThreadFriendRequestDatabase lokiThreadFriendRequestDatabase; + private final LokiUserDisplayNameDatabase lokiUserDisplayNameDatabase; public static DatabaseFactory getInstance(Context context) { synchronized (lock) { @@ -176,6 +178,10 @@ public class DatabaseFactory { public static LokiThreadFriendRequestDatabase getLokiThreadFriendRequestDatabase(Context context) { return getInstance(context).lokiThreadFriendRequestDatabase; } + + public static LokiUserDisplayNameDatabase getLokiUserDisplayNameDatabase(Context context) { + return getInstance(context).lokiUserDisplayNameDatabase; + } // endregion public static void upgradeRestored(Context context, SQLiteDatabase database){ @@ -214,6 +220,7 @@ public class DatabaseFactory { this.lokiPreKeyBundleDatabase = new LokiPreKeyBundleDatabase(context, databaseHelper); this.lokiMessageFriendRequestDatabase = new LokiMessageFriendRequestDatabase(context, databaseHelper); this.lokiThreadFriendRequestDatabase = new LokiThreadFriendRequestDatabase(context, databaseHelper); + this.lokiUserDisplayNameDatabase = new LokiUserDisplayNameDatabase(context, databaseHelper); } public void onApplicationLevelUpgrade(@NonNull Context context, @NonNull MasterSecret masterSecret, diff --git a/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java b/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java index 4800b67a81..78f9b67b2a 100644 --- a/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java +++ b/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java @@ -39,6 +39,7 @@ import org.thoughtcrime.securesms.loki.LokiPreKeyRecordDatabase; import org.thoughtcrime.securesms.loki.LokiMessageFriendRequestDatabase; import org.thoughtcrime.securesms.loki.LokiPreKeyBundleDatabase; import org.thoughtcrime.securesms.loki.LokiThreadFriendRequestDatabase; +import org.thoughtcrime.securesms.loki.LokiUserDisplayNameDatabase; import org.thoughtcrime.securesms.notifications.NotificationChannels; import org.thoughtcrime.securesms.service.KeyCachingService; import org.thoughtcrime.securesms.util.TextSecurePreferences; @@ -123,6 +124,7 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper { db.execSQL(LokiPreKeyRecordDatabase.getCreateTableCommand()); db.execSQL(LokiMessageFriendRequestDatabase.getCreateTableCommand()); db.execSQL(LokiThreadFriendRequestDatabase.getCreateTableCommand()); + db.execSQL(LokiUserDisplayNameDatabase.getCreateTableCommand()); executeStatements(db, SmsDatabase.CREATE_INDEXS); executeStatements(db, MmsDatabase.CREATE_INDEXS); diff --git a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java index dc43049887..85b01dbf6b 100644 --- a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java +++ b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java @@ -271,7 +271,7 @@ public class PushDecryptJob extends BaseJob implements InjectableType { Optional senderDisplayName = content.senderDisplayName; if (senderDisplayName.isPresent()) { - // TODO: Use display name + DatabaseFactory.getLokiUserDisplayNameDatabase(context).setDisplayName(envelope.getSource(), senderDisplayName.get()); } if (content.getDataMessage().isPresent()) { diff --git a/src/org/thoughtcrime/securesms/loki/LokiUserDisplayNameDatabase.kt b/src/org/thoughtcrime/securesms/loki/LokiUserDisplayNameDatabase.kt new file mode 100644 index 0000000000..2c3b16ac61 --- /dev/null +++ b/src/org/thoughtcrime/securesms/loki/LokiUserDisplayNameDatabase.kt @@ -0,0 +1,31 @@ +package org.thoughtcrime.securesms.loki + +import android.content.ContentValues +import android.content.Context +import org.thoughtcrime.securesms.database.Database +import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper + +class LokiUserDisplayNameDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(context, helper) { + + companion object { + private val tableName = "loki_user_display_name_database" + private val hexEncodedPublicKey = "hex_encoded_public_key" + private val displayName = "display_name" + @JvmStatic val createTableCommand = "CREATE TABLE $tableName ($hexEncodedPublicKey TEXT PRIMARY KEY, $displayName TEXT);" + } + + fun getDisplayName(hexEncodedPublicKey: String): String? { + val database = databaseHelper.readableDatabase + return database.get(tableName, "${Companion.hexEncodedPublicKey} = ?", arrayOf( hexEncodedPublicKey )) { cursor -> + cursor.getString(cursor.getColumnIndexOrThrow(displayName)) + } + } + + fun setDisplayName(hexEncodedPublicKey: String, displayName: String) { + val database = databaseHelper.writableDatabase + val row = ContentValues(2) + row.put(Companion.hexEncodedPublicKey, hexEncodedPublicKey) + row.put(Companion.displayName, displayName) + database.insertOrUpdate(tableName, row, "${Companion.hexEncodedPublicKey} = ?", arrayOf( hexEncodedPublicKey )) + } +} \ No newline at end of file diff --git a/src/org/thoughtcrime/securesms/recipients/Recipient.java b/src/org/thoughtcrime/securesms/recipients/Recipient.java index 222e8bf99d..52d51ed775 100644 --- a/src/org/thoughtcrime/securesms/recipients/Recipient.java +++ b/src/org/thoughtcrime/securesms/recipients/Recipient.java @@ -38,6 +38,7 @@ import org.thoughtcrime.securesms.contacts.avatars.ResourceContactPhoto; import org.thoughtcrime.securesms.contacts.avatars.SystemContactPhoto; import org.thoughtcrime.securesms.contacts.avatars.TransparentContactPhoto; import org.thoughtcrime.securesms.database.Address; +import org.thoughtcrime.securesms.database.DatabaseFactory; import org.thoughtcrime.securesms.database.GroupDatabase; import org.thoughtcrime.securesms.database.RecipientDatabase.RecipientSettings; import org.thoughtcrime.securesms.database.RecipientDatabase.RegisteredState; @@ -48,7 +49,6 @@ import org.thoughtcrime.securesms.notifications.NotificationChannels; import org.thoughtcrime.securesms.recipients.RecipientProvider.RecipientDetails; import org.thoughtcrime.securesms.util.FutureTaskListener; import org.thoughtcrime.securesms.util.ListenableFutureTask; -import org.thoughtcrime.securesms.util.TextSecurePreferences; import org.thoughtcrime.securesms.util.Util; import org.whispersystems.libsignal.util.guava.Optional; @@ -70,6 +70,7 @@ public class Recipient implements RecipientModifiedListener { private final @NonNull Address address; private final @NonNull List participants = new LinkedList<>(); + private Context context; private @Nullable String name; private @Nullable String customLabel; private boolean resolving; @@ -116,11 +117,13 @@ public class Recipient implements RecipientModifiedListener { if (recipient.isPresent()) consumer.accept(recipient.get()); } - Recipient(@NonNull Address address, + Recipient(@NonNull Context context, + @NonNull Address address, @Nullable Recipient stale, @NonNull Optional details, @NonNull ListenableFutureTask future) { + this.context = context; this.address = address; this.color = null; this.resolving = true; @@ -235,7 +238,8 @@ public class Recipient implements RecipientModifiedListener { }); } - Recipient(@NonNull Address address, @NonNull RecipientDetails details) { + Recipient(@NonNull Context context, @NonNull Address address, @NonNull RecipientDetails details) { + this.context = context; this.address = address; this.contactUri = details.contactUri; this.name = details.name; @@ -288,6 +292,9 @@ public class Recipient implements RecipientModifiedListener { } public synchronized @Nullable String getName() { + String displayName = DatabaseFactory.getLokiUserDisplayNameDatabase(context).getDisplayName(this.address.toString()); + if (displayName != null) { return displayName; } + if (this.name == null && isMmsGroupRecipient()) { List names = new LinkedList<>(); diff --git a/src/org/thoughtcrime/securesms/recipients/RecipientProvider.java b/src/org/thoughtcrime/securesms/recipients/RecipientProvider.java index 9c4d72f0dd..efa94d6489 100644 --- a/src/org/thoughtcrime/securesms/recipients/RecipientProvider.java +++ b/src/org/thoughtcrime/securesms/recipients/RecipientProvider.java @@ -66,9 +66,9 @@ class RecipientProvider { Optional prefetchedRecipientDetails = createPrefetchedRecipientDetails(context, address, settings, groupRecord); if (asynchronous) { - cachedRecipient = new Recipient(address, cachedRecipient, prefetchedRecipientDetails, getRecipientDetailsAsync(context, address, settings, groupRecord)); + cachedRecipient = new Recipient(context, address, cachedRecipient, prefetchedRecipientDetails, getRecipientDetailsAsync(context, address, settings, groupRecord)); } else { - cachedRecipient = new Recipient(address, getRecipientDetailsSync(context, address, settings, groupRecord, false)); + cachedRecipient = new Recipient(context, address, getRecipientDetailsSync(context, address, settings, groupRecord, false)); } recipientCache.set(address, cachedRecipient);