From fd389a637a8f7e607b789695eb1e6a06bfb81ea8 Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Tue, 10 Sep 2019 13:50:59 +1000 Subject: [PATCH] Clean --- .../securesms/ConversationListActivity.java | 4 +--- .../securesms/components/AvatarImageView.java | 7 +++--- .../conversation/ConversationItem.java | 4 +++- .../loki/{identicon => }/IdenticonDrawable.kt | 15 +++--------- .../{identicon => }/JazzIdenticonDrawable.kt | 6 +++-- .../securesms/loki/LokiGroupChatPoller.kt | 2 -- .../securesms/loki/LokiUserDatabase.kt | 24 +++++++++++-------- .../securesms/loki/{identicon => }/RNG.kt | 9 ++++--- .../widgets/ProfilePreference.java | 4 +--- 9 files changed, 33 insertions(+), 42 deletions(-) rename src/org/thoughtcrime/securesms/loki/{identicon => }/IdenticonDrawable.kt (79%) rename src/org/thoughtcrime/securesms/loki/{identicon => }/JazzIdenticonDrawable.kt (96%) rename src/org/thoughtcrime/securesms/loki/{identicon => }/RNG.kt (72%) diff --git a/src/org/thoughtcrime/securesms/ConversationListActivity.java b/src/org/thoughtcrime/securesms/ConversationListActivity.java index 1b70956137..ddd3ad3dff 100644 --- a/src/org/thoughtcrime/securesms/ConversationListActivity.java +++ b/src/org/thoughtcrime/securesms/ConversationListActivity.java @@ -37,8 +37,6 @@ import android.view.ViewTreeObserver; import android.widget.ImageView; import android.widget.Toast; -import com.lelloman.identicon.drawable.ClassicIdenticonDrawable; - import org.thoughtcrime.securesms.components.RatingManager; import org.thoughtcrime.securesms.components.SearchToolbar; import org.thoughtcrime.securesms.conversation.ConversationActivity; @@ -46,7 +44,7 @@ import org.thoughtcrime.securesms.database.Address; import org.thoughtcrime.securesms.database.DatabaseFactory; import org.thoughtcrime.securesms.database.MessagingDatabase.MarkedMessageInfo; import org.thoughtcrime.securesms.lock.RegistrationLockDialog; -import org.thoughtcrime.securesms.loki.identicon.JazzIdenticonDrawable; +import org.thoughtcrime.securesms.loki.JazzIdenticonDrawable; import org.thoughtcrime.securesms.notifications.MarkReadReceiver; import org.thoughtcrime.securesms.notifications.MessageNotifier; import org.thoughtcrime.securesms.permissions.Permissions; diff --git a/src/org/thoughtcrime/securesms/components/AvatarImageView.java b/src/org/thoughtcrime/securesms/components/AvatarImageView.java index dfaf053a0a..5a3db4bbce 100644 --- a/src/org/thoughtcrime/securesms/components/AvatarImageView.java +++ b/src/org/thoughtcrime/securesms/components/AvatarImageView.java @@ -16,13 +16,10 @@ import android.util.AttributeSet; import android.view.View; import android.view.ViewOutlineProvider; -import com.lelloman.identicon.drawable.ClassicIdenticonDrawable; - -import network.loki.messenger.R; import org.thoughtcrime.securesms.color.MaterialColor; import org.thoughtcrime.securesms.contacts.avatars.ContactColors; import org.thoughtcrime.securesms.contacts.avatars.GeneratedContactPhoto; -import org.thoughtcrime.securesms.loki.identicon.JazzIdenticonDrawable; +import org.thoughtcrime.securesms.loki.JazzIdenticonDrawable; import org.thoughtcrime.securesms.mms.GlideRequests; import org.thoughtcrime.securesms.recipients.Recipient; import org.thoughtcrime.securesms.recipients.RecipientExporter; @@ -30,6 +27,8 @@ import org.thoughtcrime.securesms.util.TextSecurePreferences; import org.thoughtcrime.securesms.util.ThemeUtil; import org.whispersystems.libsignal.util.guava.Optional; +import network.loki.messenger.R; + public class AvatarImageView extends AppCompatImageView { private static final String TAG = AvatarImageView.class.getSimpleName(); diff --git a/src/org/thoughtcrime/securesms/conversation/ConversationItem.java b/src/org/thoughtcrime/securesms/conversation/ConversationItem.java index 35b1be25d1..6dc4cf67c5 100644 --- a/src/org/thoughtcrime/securesms/conversation/ConversationItem.java +++ b/src/org/thoughtcrime/securesms/conversation/ConversationItem.java @@ -881,7 +881,9 @@ public class ConversationItem extends LinearLayout String serverId = GroupUtil.getDecodedStringId(conversationRecipient.getAddress().serialize()); String senderDisplayName = DatabaseFactory.getLokiUserDatabase(context).getServerDisplayName(serverId, recipient.getAddress().serialize()); if (senderDisplayName != null) { displayName = senderDisplayName; } - } catch (Exception e) {} + } catch (Exception e) { + // Do nothing + } this.groupSender.setText(displayName); diff --git a/src/org/thoughtcrime/securesms/loki/identicon/IdenticonDrawable.kt b/src/org/thoughtcrime/securesms/loki/IdenticonDrawable.kt similarity index 79% rename from src/org/thoughtcrime/securesms/loki/identicon/IdenticonDrawable.kt rename to src/org/thoughtcrime/securesms/loki/IdenticonDrawable.kt index 257b4bf40c..86d69ce990 100644 --- a/src/org/thoughtcrime/securesms/loki/identicon/IdenticonDrawable.kt +++ b/src/org/thoughtcrime/securesms/loki/IdenticonDrawable.kt @@ -1,22 +1,13 @@ -package org.thoughtcrime.securesms.loki.identicon +package org.thoughtcrime.securesms.loki -import android.graphics.Bitmap -import android.graphics.Canvas -import android.graphics.ColorFilter -import android.graphics.Paint -import android.graphics.Rect +import android.graphics.* import android.graphics.drawable.Drawable /** * Basically a [Bitmap] wrapper, the [Bitmap] size must be known when instantiating it * but when drawing it will draw the [Bitmap] to fit the canvas. */ -abstract class IdenticonDrawable( - width: Int, - height: Int, - hash: Long -) : Drawable() { - +abstract class IdenticonDrawable(width: Int, height: Int, hash: Long) : Drawable() { private val bitmapRect: Rect = Rect(0, 0, width, height) private val destinationRect: Rect = Rect(0, 0, width, height) private val bitmap: Bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888) diff --git a/src/org/thoughtcrime/securesms/loki/identicon/JazzIdenticonDrawable.kt b/src/org/thoughtcrime/securesms/loki/JazzIdenticonDrawable.kt similarity index 96% rename from src/org/thoughtcrime/securesms/loki/identicon/JazzIdenticonDrawable.kt rename to src/org/thoughtcrime/securesms/loki/JazzIdenticonDrawable.kt index 06c5fc618d..ff678db54c 100644 --- a/src/org/thoughtcrime/securesms/loki/identicon/JazzIdenticonDrawable.kt +++ b/src/org/thoughtcrime/securesms/loki/JazzIdenticonDrawable.kt @@ -1,6 +1,8 @@ -package org.thoughtcrime.securesms.loki.identicon +package org.thoughtcrime.securesms.loki -import android.graphics.* +import android.graphics.Canvas +import android.graphics.Color +import android.graphics.Paint import kotlin.math.* class JazzIdenticonDrawable(width: Int, height: Int, hash: Long) : IdenticonDrawable(width, height, hash) { diff --git a/src/org/thoughtcrime/securesms/loki/LokiGroupChatPoller.kt b/src/org/thoughtcrime/securesms/loki/LokiGroupChatPoller.kt index 3a5c94f36b..bf8c86458a 100644 --- a/src/org/thoughtcrime/securesms/loki/LokiGroupChatPoller.kt +++ b/src/org/thoughtcrime/securesms/loki/LokiGroupChatPoller.kt @@ -94,10 +94,8 @@ class LokiGroupChatPoller(private val context: Context, private val group: LokiG val x1 = SignalServiceGroup(SignalServiceGroup.Type.UPDATE, id, null, null, null) val x2 = SignalServiceDataMessage(message.timestamp, x1, null, message.body) val x3 = SignalServiceContent(x2, message.hexEncodedPublicKey, SignalServiceAddress.DEFAULT_DEVICE_ID, message.timestamp, false) - val senderDisplayName = "${message.displayName} (...${message.hexEncodedPublicKey.takeLast(8)})" DatabaseFactory.getLokiUserDatabase(context).setServerDisplayName(group.id, message.hexEncodedPublicKey, senderDisplayName) - PushDecryptJob(context).handleTextMessage(x3, x2, Optional.absent(), Optional.of(message.serverID)) } fun processOutgoingMessage(message: LokiGroupMessage) { diff --git a/src/org/thoughtcrime/securesms/loki/LokiUserDatabase.kt b/src/org/thoughtcrime/securesms/loki/LokiUserDatabase.kt index 4b16d8432a..5b907f8be7 100644 --- a/src/org/thoughtcrime/securesms/loki/LokiUserDatabase.kt +++ b/src/org/thoughtcrime/securesms/loki/LokiUserDatabase.kt @@ -3,6 +3,7 @@ package org.thoughtcrime.securesms.loki import android.content.ContentValues import android.content.Context import android.database.sqlite.SQLiteDatabase +import android.util.Log import org.thoughtcrime.securesms.database.Address import org.thoughtcrime.securesms.database.Database import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper @@ -13,13 +14,16 @@ import org.whispersystems.signalservice.loki.messaging.LokiUserDatabaseProtocol class LokiUserDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(context, helper), LokiUserDatabaseProtocol { companion object { - private val displayNameTable = "loki_user_display_name_database" - private val serverDisplayNameTable = "loki_user_server_display_name_database" - private val hexEncodedPublicKey = "hex_encoded_public_key" + // Shared private val displayName = "display_name" - private val serverId = "server_id" + // Display name cache + private val displayNameTable = "loki_user_display_name_database" + private val hexEncodedPublicKey = "hex_encoded_public_key" @JvmStatic val createDisplayNameTableCommand = "CREATE TABLE $displayNameTable ($hexEncodedPublicKey TEXT PRIMARY KEY, $displayName TEXT);" - @JvmStatic val createServerDisplayNameTableCommand = "CREATE TABLE $serverDisplayNameTable ($hexEncodedPublicKey TEXT, $serverId TEXT, $displayName TEXT, PRIMARY KEY ($hexEncodedPublicKey, $serverId));" + // Server display name cache + private val serverDisplayNameTable = "loki_user_server_display_name_database" + private val serverID = "server_id" + @JvmStatic val createServerDisplayNameTableCommand = "CREATE TABLE $serverDisplayNameTable ($hexEncodedPublicKey TEXT, $serverID TEXT, $displayName TEXT, PRIMARY KEY ($hexEncodedPublicKey, $serverID));" } override fun getDisplayName(hexEncodedPublicKey: String): String? { @@ -42,24 +46,24 @@ class LokiUserDatabase(context: Context, helper: SQLCipherOpenHelper) : Database Recipient.from(context, Address.fromSerialized(hexEncodedPublicKey), false).notifyListeners() } - fun getServerDisplayName(serverId: String, hexEncodedPublicKey: String): String? { + fun getServerDisplayName(serverID: String, hexEncodedPublicKey: String): String? { val database = databaseHelper.readableDatabase - return database.get(serverDisplayNameTable, "${Companion.hexEncodedPublicKey} = ? AND ${Companion.serverId} = ?", arrayOf(hexEncodedPublicKey, serverId)) { cursor -> + return database.get(serverDisplayNameTable, "${Companion.hexEncodedPublicKey} = ? AND ${Companion.serverID} = ?", arrayOf(hexEncodedPublicKey, serverID)) { cursor -> cursor.getString(cursor.getColumnIndexOrThrow(displayName)) } } - fun setServerDisplayName(serverId: String, hexEncodedPublicKey: String, displayName: String) { + fun setServerDisplayName(serverID: String, hexEncodedPublicKey: String, displayName: String) { val database = databaseHelper.writableDatabase val values = ContentValues(3) - values.put(Companion.serverId, serverId) + values.put(Companion.serverID, serverID) values.put(Companion.hexEncodedPublicKey, hexEncodedPublicKey) values.put(Companion.displayName, displayName) try { database.insertWithOnConflict(serverDisplayNameTable, null, values, SQLiteDatabase.CONFLICT_REPLACE) Recipient.from(context, Address.fromSerialized(hexEncodedPublicKey), false).notifyListeners() } catch (e: Exception) { - print(e) + Log.d("Loki", "Couldn't save server display name due to exception: $e.") } } } \ No newline at end of file diff --git a/src/org/thoughtcrime/securesms/loki/identicon/RNG.kt b/src/org/thoughtcrime/securesms/loki/RNG.kt similarity index 72% rename from src/org/thoughtcrime/securesms/loki/identicon/RNG.kt rename to src/org/thoughtcrime/securesms/loki/RNG.kt index 6e43ecaeb5..208414785a 100644 --- a/src/org/thoughtcrime/securesms/loki/identicon/RNG.kt +++ b/src/org/thoughtcrime/securesms/loki/RNG.kt @@ -1,7 +1,6 @@ -package org.thoughtcrime.securesms.loki.identicon +package org.thoughtcrime.securesms.loki class RNG(hash: Long) { - private var seed: Long private val initial: Long @@ -13,17 +12,17 @@ class RNG(hash: Long) { initial = seed } - public fun next(): Long { + fun next(): Long { val newSeed = (seed * 16807) % 2147483647 seed = newSeed return seed } - public fun nextFloat(): Float { + fun nextFloat(): Float { return (next() - 1).toFloat() / 2147483646 } - public fun reset() { + fun reset() { seed = initial } } \ No newline at end of file diff --git a/src/org/thoughtcrime/securesms/preferences/widgets/ProfilePreference.java b/src/org/thoughtcrime/securesms/preferences/widgets/ProfilePreference.java index 2838bfc0b3..49e60e1f97 100644 --- a/src/org/thoughtcrime/securesms/preferences/widgets/ProfilePreference.java +++ b/src/org/thoughtcrime/securesms/preferences/widgets/ProfilePreference.java @@ -18,10 +18,8 @@ import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; -import com.lelloman.identicon.drawable.ClassicIdenticonDrawable; - import org.thoughtcrime.securesms.database.Address; -import org.thoughtcrime.securesms.loki.identicon.JazzIdenticonDrawable; +import org.thoughtcrime.securesms.loki.JazzIdenticonDrawable; import org.thoughtcrime.securesms.util.TextSecurePreferences; import network.loki.messenger.R;