From 8fb73a092d4de0694d9d238c840003c22e7bc387 Mon Sep 17 00:00:00 2001 From: Moxie Marlinspike Date: Thu, 3 Dec 2015 11:47:28 -0800 Subject: [PATCH] Slight optimization for message status updates // FREEBIE --- .../securesms/database/MmsDatabase.java | 49 +++++++++++-------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/src/org/thoughtcrime/securesms/database/MmsDatabase.java b/src/org/thoughtcrime/securesms/database/MmsDatabase.java index 293452ff31..6939dd47f5 100644 --- a/src/org/thoughtcrime/securesms/database/MmsDatabase.java +++ b/src/org/thoughtcrime/securesms/database/MmsDatabase.java @@ -333,43 +333,50 @@ public class MmsDatabase extends MessagingDatabase { return readerFor(masterSecret, rawQuery(where, null)); } - private void updateMailboxBitmask(long id, long maskOff, long maskOn) { + private void updateMailboxBitmask(long id, long maskOff, long maskOn, Optional threadId) { SQLiteDatabase db = databaseHelper.getWritableDatabase(); db.execSQL("UPDATE " + TABLE_NAME + " SET " + MESSAGE_BOX + " = (" + MESSAGE_BOX + " & " + (Types.TOTAL_MASK - maskOff) + " | " + maskOn + " )" + " WHERE " + ID + " = ?", new String[] {id + ""}); - DatabaseFactory.getThreadDatabase(context).update(getThreadIdForMessage(id), false); + if (threadId.isPresent()) { + DatabaseFactory.getThreadDatabase(context).update(threadId.get(), false); + } } public void markAsOutbox(long messageId) { - updateMailboxBitmask(messageId, Types.BASE_TYPE_MASK, Types.BASE_OUTBOX_TYPE); - notifyConversationListeners(getThreadIdForMessage(messageId)); + long threadId = getThreadIdForMessage(messageId); + updateMailboxBitmask(messageId, Types.BASE_TYPE_MASK, Types.BASE_OUTBOX_TYPE, Optional.of(threadId)); } public void markAsForcedSms(long messageId) { - updateMailboxBitmask(messageId, Types.PUSH_MESSAGE_BIT, Types.MESSAGE_FORCE_SMS_BIT); - notifyConversationListeners(getThreadIdForMessage(messageId)); + long threadId = getThreadIdForMessage(messageId); + updateMailboxBitmask(messageId, Types.PUSH_MESSAGE_BIT, Types.MESSAGE_FORCE_SMS_BIT, Optional.of(threadId)); + notifyConversationListeners(threadId); } public void markAsPendingInsecureSmsFallback(long messageId) { - updateMailboxBitmask(messageId, Types.BASE_TYPE_MASK, Types.BASE_PENDING_INSECURE_SMS_FALLBACK); - notifyConversationListeners(getThreadIdForMessage(messageId)); + long threadId = getThreadIdForMessage(messageId); + updateMailboxBitmask(messageId, Types.BASE_TYPE_MASK, Types.BASE_PENDING_INSECURE_SMS_FALLBACK, Optional.of(threadId)); + notifyConversationListeners(threadId); } public void markAsSending(long messageId) { - updateMailboxBitmask(messageId, Types.BASE_TYPE_MASK, Types.BASE_SENDING_TYPE); - notifyConversationListeners(getThreadIdForMessage(messageId)); + long threadId = getThreadIdForMessage(messageId); + updateMailboxBitmask(messageId, Types.BASE_TYPE_MASK, Types.BASE_SENDING_TYPE, Optional.of(threadId)); + notifyConversationListeners(threadId); } public void markAsSentFailed(long messageId) { - updateMailboxBitmask(messageId, Types.BASE_TYPE_MASK, Types.BASE_SENT_FAILED_TYPE); - notifyConversationListeners(getThreadIdForMessage(messageId)); + long threadId = getThreadIdForMessage(messageId); + updateMailboxBitmask(messageId, Types.BASE_TYPE_MASK, Types.BASE_SENT_FAILED_TYPE, Optional.of(threadId)); + notifyConversationListeners(threadId); } public void markAsSent(long messageId) { - updateMailboxBitmask(messageId, Types.BASE_TYPE_MASK, Types.BASE_SENT_TYPE); - notifyConversationListeners(getThreadIdForMessage(messageId)); + long threadId = getThreadIdForMessage(messageId); + updateMailboxBitmask(messageId, Types.BASE_TYPE_MASK, Types.BASE_SENT_TYPE, Optional.of(threadId)); + notifyConversationListeners(threadId); } public void markDownloadState(long messageId, long state) { @@ -382,34 +389,34 @@ public class MmsDatabase extends MessagingDatabase { } public void markAsNoSession(long messageId, long threadId) { - updateMailboxBitmask(messageId, Types.ENCRYPTION_MASK, Types.ENCRYPTION_REMOTE_NO_SESSION_BIT); + updateMailboxBitmask(messageId, Types.ENCRYPTION_MASK, Types.ENCRYPTION_REMOTE_NO_SESSION_BIT, Optional.of(threadId)); notifyConversationListeners(threadId); } public void markAsSecure(long messageId) { - updateMailboxBitmask(messageId, 0, Types.SECURE_MESSAGE_BIT); + updateMailboxBitmask(messageId, 0, Types.SECURE_MESSAGE_BIT, Optional.absent()); } public void markAsInsecure(long messageId) { - updateMailboxBitmask(messageId, Types.SECURE_MESSAGE_BIT, 0); + updateMailboxBitmask(messageId, Types.SECURE_MESSAGE_BIT, 0, Optional.absent()); } public void markAsPush(long messageId) { - updateMailboxBitmask(messageId, 0, Types.PUSH_MESSAGE_BIT); + updateMailboxBitmask(messageId, 0, Types.PUSH_MESSAGE_BIT, Optional.absent()); } public void markAsDecryptFailed(long messageId, long threadId) { - updateMailboxBitmask(messageId, Types.ENCRYPTION_MASK, Types.ENCRYPTION_REMOTE_FAILED_BIT); + updateMailboxBitmask(messageId, Types.ENCRYPTION_MASK, Types.ENCRYPTION_REMOTE_FAILED_BIT, Optional.of(threadId)); notifyConversationListeners(threadId); } public void markAsDecryptDuplicate(long messageId, long threadId) { - updateMailboxBitmask(messageId, Types.ENCRYPTION_MASK, Types.ENCRYPTION_REMOTE_DUPLICATE_BIT); + updateMailboxBitmask(messageId, Types.ENCRYPTION_MASK, Types.ENCRYPTION_REMOTE_DUPLICATE_BIT, Optional.of(threadId)); notifyConversationListeners(threadId); } public void markAsLegacyVersion(long messageId, long threadId) { - updateMailboxBitmask(messageId, Types.ENCRYPTION_MASK, Types.ENCRYPTION_REMOTE_LEGACY_BIT); + updateMailboxBitmask(messageId, Types.ENCRYPTION_MASK, Types.ENCRYPTION_REMOTE_LEGACY_BIT, Optional.of(threadId)); notifyConversationListeners(threadId); }