From 3d574be93177d9efcdd3aaac1245d778e36fd2a7 Mon Sep 17 00:00:00 2001 From: Mikunj Date: Thu, 5 Dec 2019 10:51:54 +1100 Subject: [PATCH] Clean. Removed recipient cache as signal already had that. Removed message cache as it messes with the ui (public chat conversations pop in with all the messages) --- .../securesms/database/ThreadDatabase.java | 37 ++++--------------- .../securesms/jobs/PushDecryptJob.java | 2 +- .../securesms/loki/LokiAPIDatabase.kt | 4 +- .../securesms/loki/LokiPublicChatPoller.kt | 3 -- .../securesms/util/TextSecurePreferences.java | 8 ---- 5 files changed, 11 insertions(+), 43 deletions(-) diff --git a/src/org/thoughtcrime/securesms/database/ThreadDatabase.java b/src/org/thoughtcrime/securesms/database/ThreadDatabase.java index 49a0cc8bfb..60a304d459 100644 --- a/src/org/thoughtcrime/securesms/database/ThreadDatabase.java +++ b/src/org/thoughtcrime/securesms/database/ThreadDatabase.java @@ -62,8 +62,6 @@ public class ThreadDatabase extends Database { private static final String TAG = ThreadDatabase.class.getSimpleName(); private Map addressCache = new HashMap<>(); - private Map> messageCache = new HashMap<>(); - private Map recipientCache = new HashMap<>(); public static final String TABLE_NAME = "thread"; public static final String ID = "_id"; @@ -157,31 +155,9 @@ public class ThreadDatabase extends Database { contentValues.put(ARCHIVED, 0); } - if (!messageCache.containsKey(threadId)) { - messageCache.put(threadId, new ArrayList<>()); - } - messageCache.get(threadId).add(contentValues); - - if (!recipientCache.containsKey(threadId)) { - recipientCache.put(threadId, getRecipientForThreadId(threadId).getName().replaceAll("\\s*", "")); - } - String key = recipientCache.get(threadId); - int newMessagesNumber = TextSecurePreferences.getNewMessagesNumber(this.context, key); - if (newMessagesNumber == 0 || newMessagesNumber == messageCache.get(threadId).size()) { - SQLiteDatabase db = databaseHelper.getWritableDatabase(); - db.beginTransactionNonExclusive(); - try { - for (ContentValues contentValue : messageCache.get(threadId)) { - db.update(TABLE_NAME, contentValue, ID + " = ?", new String[] {threadId + ""}); - } - TextSecurePreferences.setNewMessagesNumber(this.context, key, TextSecurePreferences.getNewMessagesNumber(this.context, key) - newMessagesNumber); - messageCache.get(threadId).clear(); - db.setTransactionSuccessful(); - notifyConversationListListeners(); - } finally { - db.endTransaction(); - } - } + SQLiteDatabase db = databaseHelper.getWritableDatabase(); + db.update(TABLE_NAME, contentValues, ID + " = ?", new String[] {threadId + ""}); + notifyConversationListListeners(); } @@ -207,6 +183,7 @@ public class ThreadDatabase extends Database { private void deleteThread(long threadId) { SQLiteDatabase db = databaseHelper.getWritableDatabase(); db.delete(TABLE_NAME, ID_WHERE, new String[] {threadId + ""}); + addressCache.remove(threadId); notifyConversationListListeners(); } @@ -221,12 +198,16 @@ public class ThreadDatabase extends Database { where = where.substring(0, where.length() - 4); db.delete(TABLE_NAME, where, null); + for (long threadId: threadIds) { + addressCache.remove(threadId); + } notifyConversationListListeners(); } private void deleteAllThreads() { SQLiteDatabase db = databaseHelper.getWritableDatabase(); db.delete(TABLE_NAME, null, null); + addressCache.clear(); notifyConversationListListeners(); } @@ -559,8 +540,6 @@ public class ThreadDatabase extends Database { public @Nullable Recipient getRecipientForThreadId(long threadId) { // Loki - Cache the address. - // Don't know if this will affect any other signal code - // Don't know if it is necessary to add some cache time if (addressCache.containsKey(threadId) && addressCache.get(threadId) != null) { return Recipient.from(context, addressCache.get(threadId), false); } diff --git a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java index fee75e62b5..2b29b7916b 100644 --- a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java +++ b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java @@ -1712,7 +1712,7 @@ public class PushDecryptJob extends BaseJob implements InjectableType { } return Recipient.from(context, Address.fromSerialized(publicKey), false); } catch (Exception e) { - Log.d("Loki", "Failed to get primary device public key for message. " + e.getMessage()); + Log.d("Loki", "Failed to get primary device public key for " + pubKey + ". " + e.getMessage()); return Recipient.from(context, Address.fromSerialized(pubKey), false); } } diff --git a/src/org/thoughtcrime/securesms/loki/LokiAPIDatabase.kt b/src/org/thoughtcrime/securesms/loki/LokiAPIDatabase.kt index 85d8f38408..91526991e5 100644 --- a/src/org/thoughtcrime/securesms/loki/LokiAPIDatabase.kt +++ b/src/org/thoughtcrime/securesms/loki/LokiAPIDatabase.kt @@ -186,12 +186,12 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database( } override fun removePairingAuthorisations(hexEncodedPublicKey: String) { - val database = databaseHelper.readableDatabase + val database = databaseHelper.writableDatabase database.delete(pairingAuthorisationCache, "$primaryDevicePublicKey = ? OR $secondaryDevicePublicKey = ?", arrayOf( hexEncodedPublicKey, hexEncodedPublicKey )) } fun removePairingAuthorisation(primaryDevicePublicKey: String, secondaryDevicePublicKey: String) { - val database = databaseHelper.readableDatabase + val database = databaseHelper.writableDatabase database.delete(pairingAuthorisationCache, "${Companion.primaryDevicePublicKey} = ? OR ${Companion.secondaryDevicePublicKey} = ?", arrayOf( primaryDevicePublicKey, secondaryDevicePublicKey )) } } diff --git a/src/org/thoughtcrime/securesms/loki/LokiPublicChatPoller.kt b/src/org/thoughtcrime/securesms/loki/LokiPublicChatPoller.kt index e5dc2a75f8..9c21983fbe 100644 --- a/src/org/thoughtcrime/securesms/loki/LokiPublicChatPoller.kt +++ b/src/org/thoughtcrime/securesms/loki/LokiPublicChatPoller.kt @@ -10,7 +10,6 @@ import org.thoughtcrime.securesms.ApplicationContext import org.thoughtcrime.securesms.crypto.IdentityKeyUtil import org.thoughtcrime.securesms.database.Address import org.thoughtcrime.securesms.database.DatabaseFactory -import org.thoughtcrime.securesms.database.RecipientDatabase import org.thoughtcrime.securesms.jobs.PushDecryptJob import org.thoughtcrime.securesms.jobs.RetrieveProfileAvatarJob import org.thoughtcrime.securesms.recipients.Recipient @@ -246,8 +245,6 @@ class LokiPublicChatPoller(private val context: Context, private val group: Loki displayNameUpdatees = displayNameUpdatees.union(newDisplayNameUpdatees) }.successBackground { messages -> // Process messages in the background - val key = group.displayName.replace(Regex("\\s*"), "") - TextSecurePreferences.setNewMessagesNumber(this.context, key, TextSecurePreferences.getNewMessagesNumber(this.context, key) + messages.size) messages.forEach { message -> if (userDevices.contains(message.hexEncodedPublicKey)) { processOutgoingMessage(message) diff --git a/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java b/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java index f9a3c174c7..00f334bc1a 100644 --- a/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java +++ b/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java @@ -185,14 +185,6 @@ public class TextSecurePreferences { private static final String MEDIA_KEYBOARD_MODE = "pref_media_keyboard_mode"; - public static void setNewMessagesNumber(@NonNull Context context, String key, int value) { - setIntegerPrefrence(context, key, value); - } - - public static int getNewMessagesNumber(@NonNull Context context, String key) { - return getIntegerPreference(context, key, 0); - } - public static boolean isScreenLockEnabled(@NonNull Context context) { return getBooleanPreference(context, SCREEN_LOCK, false); }