Skip to content

Commit

Permalink
Provide two ways of listening for thread/message db updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
cody-signal committed Jun 9, 2020
1 parent e04f76b commit dc46d88
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 39 deletions.
Expand Up @@ -44,6 +44,11 @@ protected void notifyConversationListeners(Set<Long> threadIds) {

protected void notifyConversationListeners(long threadId) {
context.getContentResolver().notifyChange(DatabaseContentProviders.Conversation.getUriForThread(threadId), null);
notifyVerboseConversationListeners(threadId);
}

protected void notifyVerboseConversationListeners(long threadId) {
context.getContentResolver().notifyChange(DatabaseContentProviders.Conversation.getVerboseUriForThread(threadId), null);
}

protected void notifyConversationListListeners() {
Expand All @@ -58,11 +63,15 @@ protected void notifyStickerPackListeners() {
context.getContentResolver().notifyChange(DatabaseContentProviders.StickerPack.CONTENT_URI, null);
}

protected void setNotifyConverationListeners(Cursor cursor, long threadId) {
protected void setNotifyConversationListeners(Cursor cursor, long threadId) {
cursor.setNotificationUri(context.getContentResolver(), DatabaseContentProviders.Conversation.getUriForThread(threadId));
}

protected void setNotifyConverationListListeners(Cursor cursor) {
protected void setNotifyVerboseConversationListeners(Cursor cursor, long threadId) {
cursor.setNotificationUri(context.getContentResolver(), DatabaseContentProviders.Conversation.getVerboseUriForThread(threadId));
}

protected void setNotifyConversationListListeners(Cursor cursor) {
cursor.setNotificationUri(context.getContentResolver(), DatabaseContentProviders.ConversationList.CONTENT_URI);
}

Expand Down
Expand Up @@ -23,6 +23,10 @@ public static class Conversation extends NoopContentProvider {
public static Uri getUriForThread(long threadId) {
return Uri.parse(CONTENT_URI_STRING + threadId);
}

public static Uri getVerboseUriForThread(long threadId) {
return Uri.parse(CONTENT_URI_STRING + "verbose/" + threadId);
}
}

public static class Attachment extends NoopContentProvider {
Expand Down
Expand Up @@ -92,7 +92,7 @@ public class MediaDatabase extends Database {
String query = sorting.applyToQuery(applyEqualityOperator(threadId, GALLERY_MEDIA_QUERY));
String[] args = {threadId + ""};
Cursor cursor = database.rawQuery(query, args);
setNotifyConverationListeners(cursor, threadId);
setNotifyConversationListeners(cursor, threadId);
return cursor;
}

Expand All @@ -101,7 +101,7 @@ public class MediaDatabase extends Database {
String query = sorting.applyToQuery(applyEqualityOperator(threadId, DOCUMENT_MEDIA_QUERY));
String[] args = {threadId + ""};
Cursor cursor = database.rawQuery(query, args);
setNotifyConverationListeners(cursor, threadId);
setNotifyConversationListeners(cursor, threadId);
return cursor;
}

Expand All @@ -110,7 +110,7 @@ public class MediaDatabase extends Database {
String query = sorting.applyToQuery(applyEqualityOperator(threadId, AUDIO_MEDIA_QUERY));
String[] args = {threadId + ""};
Cursor cursor = database.rawQuery(query, args);
setNotifyConverationListeners(cursor, threadId);
setNotifyConversationListeners(cursor, threadId);
return cursor;
}

Expand All @@ -119,7 +119,7 @@ public class MediaDatabase extends Database {
String query = sorting.applyToQuery(applyEqualityOperator(threadId, ALL_MEDIA_QUERY));
String[] args = {threadId + ""};
Cursor cursor = database.rawQuery(query, args);
setNotifyConverationListeners(cursor, threadId);
setNotifyConversationListeners(cursor, threadId);
return cursor;
}

Expand Down
Expand Up @@ -338,22 +338,22 @@ public void removeFailure(long messageId, NetworkFailure failure) {

public boolean incrementReceiptCount(SyncMessageId messageId, long timestamp, boolean deliveryReceipt) {
SQLiteDatabase database = databaseHelper.getWritableDatabase();
Cursor cursor = null;
boolean found = false;

try {
cursor = database.query(TABLE_NAME, new String[] {ID, THREAD_ID, MESSAGE_BOX, RECIPIENT_ID}, DATE_SENT + " = ?", new String[] {String.valueOf(messageId.getTimetamp())}, null, null, null, null);

try (Cursor cursor = database.query(TABLE_NAME, new String[] {ID, THREAD_ID, MESSAGE_BOX, RECIPIENT_ID, DELIVERY_RECEIPT_COUNT, READ_RECEIPT_COUNT},
DATE_SENT + " = ?", new String[] {String.valueOf(messageId.getTimetamp())},
null, null, null, null)) {
while (cursor.moveToNext()) {
if (Types.isOutgoingMessageType(cursor.getLong(cursor.getColumnIndexOrThrow(MESSAGE_BOX)))) {
RecipientId theirRecipientId = RecipientId.from(cursor.getLong(cursor.getColumnIndexOrThrow(RECIPIENT_ID)));
RecipientId ourRecipientId = messageId.getRecipientId();
String columnName = deliveryReceipt ? DELIVERY_RECEIPT_COUNT : READ_RECEIPT_COUNT;

if (ourRecipientId.equals(theirRecipientId) || Recipient.resolved(theirRecipientId).isGroup()) {
long id = cursor.getLong(cursor.getColumnIndexOrThrow(ID));
long threadId = cursor.getLong(cursor.getColumnIndexOrThrow(THREAD_ID));
int status = deliveryReceipt ? GroupReceiptDatabase.STATUS_DELIVERED : GroupReceiptDatabase.STATUS_READ;
long id = cursor.getLong(cursor.getColumnIndexOrThrow(ID));
long threadId = cursor.getLong(cursor.getColumnIndexOrThrow(THREAD_ID));
int status = deliveryReceipt ? GroupReceiptDatabase.STATUS_DELIVERED : GroupReceiptDatabase.STATUS_READ;
boolean isFirstIncrement = cursor.getLong(cursor.getColumnIndexOrThrow(columnName)) == 0;

found = true;

Expand All @@ -363,7 +363,12 @@ public boolean incrementReceiptCount(SyncMessageId messageId, long timestamp, bo

DatabaseFactory.getGroupReceiptDatabase(context).update(ourRecipientId, id, status, timestamp);
DatabaseFactory.getThreadDatabase(context).update(threadId, false);
notifyConversationListeners(threadId);

if (isFirstIncrement) {
notifyConversationListeners(threadId);
} else {
notifyVerboseConversationListeners(threadId);
}
}
}
}
Expand All @@ -374,9 +379,6 @@ public boolean incrementReceiptCount(SyncMessageId messageId, long timestamp, bo
}

return found;
} finally {
if (cursor != null)
cursor.close();
}
}

Expand Down Expand Up @@ -427,11 +429,21 @@ private Cursor rawQuery(@NonNull String where, @Nullable String[] arguments) {
}

public Cursor getMessage(long messageId) {
Cursor cursor = rawQuery(RAW_ID_WHERE, new String[] {messageId + ""});
setNotifyConverationListeners(cursor, getThreadIdForMessage(messageId));
Cursor cursor = internalGetMessage(messageId);
setNotifyConversationListeners(cursor, getThreadIdForMessage(messageId));
return cursor;
}

public Cursor getVerboseMessage(long messageId) {
Cursor cursor = internalGetMessage(messageId);
setNotifyVerboseConversationListeners(cursor, getThreadIdForMessage(messageId));
return cursor;
}

private Cursor internalGetMessage(long messageId) {
return rawQuery(RAW_ID_WHERE, new String[] {messageId + ""});
}

public MessageRecord getMessageRecord(long messageId) throws NoSuchMessageException {
try (Cursor cursor = rawQuery(RAW_ID_WHERE, new String[] {messageId + ""})) {
MessageRecord record = new Reader(cursor).getNext();
Expand Down
Expand Up @@ -162,7 +162,7 @@ public Cursor getConversation(long threadId, long offset, long limit) {
String limitStr = limit > 0 || offset > 0 ? offset + ", " + limit : null;

Cursor cursor = queryTables(PROJECTION, selection, order, limitStr);
setNotifyConverationListeners(cursor, threadId);
setNotifyConversationListeners(cursor, threadId);

return cursor;
}
Expand All @@ -176,7 +176,7 @@ public Cursor getIdentityConflictMessagesForThread(long threadId) {
String selection = MmsSmsColumns.THREAD_ID + " = " + threadId + " AND " + MmsSmsColumns.MISMATCHED_IDENTITIES + " IS NOT NULL";

Cursor cursor = queryTables(PROJECTION, selection, order, null);
setNotifyConverationListeners(cursor, threadId);
setNotifyConversationListeners(cursor, threadId);

return cursor;
}
Expand Down
Expand Up @@ -121,7 +121,7 @@ public Cursor queryMessages(@NonNull String query) {
Cursor cursor = db.rawQuery(MESSAGES_QUERY, new String[] { fullTextSearchQuery,
fullTextSearchQuery });

setNotifyConverationListListeners(cursor);
setNotifyConversationListListeners(cursor);
return cursor;
}

Expand All @@ -138,7 +138,7 @@ public Cursor queryMessages(@NonNull String query, long threadId) {
fullTextSearchQuery,
String.valueOf(threadId) });

setNotifyConverationListListeners(cursor);
setNotifyConversationListListeners(cursor);
return cursor;
}

Expand Down
Expand Up @@ -414,19 +414,18 @@ public void markAsNotified(long id) {

public boolean incrementReceiptCount(SyncMessageId messageId, boolean deliveryReceipt) {
SQLiteDatabase database = databaseHelper.getWritableDatabase();
Cursor cursor = null;
boolean foundMessage = false;

try {
cursor = database.query(TABLE_NAME, new String[] {ID, THREAD_ID, RECIPIENT_ID, TYPE},
try (Cursor cursor = database.query(TABLE_NAME, new String[] {ID, THREAD_ID, RECIPIENT_ID, TYPE, DELIVERY_RECEIPT_COUNT, READ_RECEIPT_COUNT},
DATE_SENT + " = ?", new String[] {String.valueOf(messageId.getTimetamp())},
null, null, null, null);
null, null, null, null)) {

while (cursor.moveToNext()) {
if (Types.isOutgoingMessageType(cursor.getLong(cursor.getColumnIndexOrThrow(TYPE)))) {
RecipientId theirRecipientId = messageId.getRecipientId();
RecipientId outRecipientId = RecipientId.from(cursor.getLong(cursor.getColumnIndexOrThrow(RECIPIENT_ID)));
String columnName = deliveryReceipt ? DELIVERY_RECEIPT_COUNT : READ_RECEIPT_COUNT;
boolean isFirstIncrement = cursor.getLong(cursor.getColumnIndexOrThrow(columnName)) == 0;

if (outRecipientId.equals(theirRecipientId)) {
long threadId = cursor.getLong(cursor.getColumnIndexOrThrow(THREAD_ID));
Expand All @@ -437,7 +436,13 @@ public boolean incrementReceiptCount(SyncMessageId messageId, boolean deliveryRe
new String[] {String.valueOf(cursor.getLong(cursor.getColumnIndexOrThrow(ID)))});

DatabaseFactory.getThreadDatabase(context).update(threadId, false);
notifyConversationListeners(threadId);

if (isFirstIncrement) {
notifyConversationListeners(threadId);
} else {
notifyVerboseConversationListeners(threadId);
}

foundMessage = true;
}
}
Expand All @@ -449,9 +454,6 @@ public boolean incrementReceiptCount(SyncMessageId messageId, boolean deliveryRe
}

return foundMessage;
} finally {
if (cursor != null)
cursor.close();
}
}

Expand Down Expand Up @@ -810,9 +812,20 @@ public SmsMessageRecord getMessage(long messageId) throws NoSuchMessageException
}

public Cursor getMessageCursor(long messageId) {
Cursor cursor = internalGetMessageCursor(messageId);
setNotifyConversationListeners(cursor, getThreadIdForMessage(messageId));
return cursor;
}

public Cursor getVerboseMessageCursor(long messageId) {
Cursor cursor = internalGetMessageCursor(messageId);
setNotifyVerboseConversationListeners(cursor, getThreadIdForMessage(messageId));
return cursor;
}

private Cursor internalGetMessageCursor(long messageId) {
SQLiteDatabase db = databaseHelper.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, MESSAGE_PROJECTION, ID_WHERE, new String[] {messageId + ""}, null, null, null);
setNotifyConverationListeners(cursor, getThreadIdForMessage(messageId));
return cursor;
}

Expand Down
Expand Up @@ -32,8 +32,6 @@
import net.sqlcipher.database.SQLiteDatabase;

import org.signal.storageservice.protos.groups.local.DecryptedGroup;
import org.thoughtcrime.securesms.contactshare.Contact;
import org.thoughtcrime.securesms.contactshare.ContactUtil;
import org.thoughtcrime.securesms.database.MessagingDatabase.MarkedMessageInfo;
import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper;
import org.thoughtcrime.securesms.database.model.MediaMmsMessageRecord;
Expand Down Expand Up @@ -63,7 +61,6 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;

Expand Down Expand Up @@ -440,7 +437,7 @@ public Cursor getFilteredConversationList(@Nullable List<RecipientId> filter) {
}

Cursor cursor = cursors.size() > 1 ? new MergeCursor(cursors.toArray(new Cursor[cursors.size()])) : cursors.get(0);
setNotifyConverationListListeners(cursor);
setNotifyConversationListListeners(cursor);
return cursor;
}

Expand Down Expand Up @@ -549,7 +546,7 @@ private Cursor getConversationList(String archived) {
String query = createQuery(ARCHIVED + " = ? AND " + MESSAGE_COUNT + " != 0", 0);
Cursor cursor = db.rawQuery(query, new String[]{archived});

setNotifyConverationListListeners(cursor);
setNotifyConversationListListeners(cursor);

return cursor;
}
Expand Down
Expand Up @@ -37,9 +37,9 @@ public MessageDetailsLoader(Context context, String type, long messageId) {
public Cursor getCursor() {
switch (type) {
case MmsSmsDatabase.SMS_TRANSPORT:
return DatabaseFactory.getSmsDatabase(context).getMessageCursor(messageId);
return DatabaseFactory.getSmsDatabase(context).getVerboseMessageCursor(messageId);
case MmsSmsDatabase.MMS_TRANSPORT:
return DatabaseFactory.getMmsDatabase(context).getMessage(messageId);
return DatabaseFactory.getMmsDatabase(context).getVerboseMessage(messageId);
default:
throw new AssertionError("no valid message type specified");
}
Expand Down

0 comments on commit dc46d88

Please sign in to comment.