Provide two ways of listening for thread/message db updates.

master
Cody Henthorne 2020-06-09 11:52:58 -04:00 committed by GitHub
parent e04f76b558
commit dc46d88ddd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 74 additions and 39 deletions

View File

@ -44,6 +44,11 @@ public abstract class Database {
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() {
@ -58,11 +63,15 @@ public abstract class Database {
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);
}

View File

@ -23,6 +23,10 @@ public class DatabaseContentProviders {
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 {

View File

@ -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;
}
@ -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;
}
@ -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;
}
@ -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;
}

View File

@ -338,12 +338,11 @@ public class MmsDatabase extends MessagingDatabase {
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)));
@ -351,9 +350,10 @@ public class MmsDatabase extends MessagingDatabase {
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;
@ -363,7 +363,12 @@ public class MmsDatabase extends MessagingDatabase {
DatabaseFactory.getGroupReceiptDatabase(context).update(ourRecipientId, id, status, timestamp);
DatabaseFactory.getThreadDatabase(context).update(threadId, false);
notifyConversationListeners(threadId);
if (isFirstIncrement) {
notifyConversationListeners(threadId);
} else {
notifyVerboseConversationListeners(threadId);
}
}
}
}
@ -374,9 +379,6 @@ public class MmsDatabase extends MessagingDatabase {
}
return found;
} finally {
if (cursor != null)
cursor.close();
}
}
@ -427,11 +429,21 @@ public class MmsDatabase extends MessagingDatabase {
}
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();

View File

@ -162,7 +162,7 @@ public class MmsSmsDatabase extends Database {
String limitStr = limit > 0 || offset > 0 ? offset + ", " + limit : null;
Cursor cursor = queryTables(PROJECTION, selection, order, limitStr);
setNotifyConverationListeners(cursor, threadId);
setNotifyConversationListeners(cursor, threadId);
return cursor;
}
@ -176,7 +176,7 @@ public class MmsSmsDatabase extends Database {
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;
}

View File

@ -121,7 +121,7 @@ public class SearchDatabase extends Database {
Cursor cursor = db.rawQuery(MESSAGES_QUERY, new String[] { fullTextSearchQuery,
fullTextSearchQuery });
setNotifyConverationListListeners(cursor);
setNotifyConversationListListeners(cursor);
return cursor;
}
@ -138,7 +138,7 @@ public class SearchDatabase extends Database {
fullTextSearchQuery,
String.valueOf(threadId) });
setNotifyConverationListListeners(cursor);
setNotifyConversationListListeners(cursor);
return cursor;
}

View File

@ -414,19 +414,18 @@ public class SmsDatabase extends MessagingDatabase {
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));
@ -437,7 +436,13 @@ public class SmsDatabase extends MessagingDatabase {
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;
}
}
@ -449,9 +454,6 @@ public class SmsDatabase extends MessagingDatabase {
}
return foundMessage;
} finally {
if (cursor != null)
cursor.close();
}
}
@ -810,9 +812,20 @@ public class SmsDatabase extends MessagingDatabase {
}
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;
}

View File

@ -32,8 +32,6 @@ import com.fasterxml.jackson.annotation.JsonProperty;
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;
@ -63,7 +61,6 @@ import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
@ -440,7 +437,7 @@ public class ThreadDatabase extends Database {
}
Cursor cursor = cursors.size() > 1 ? new MergeCursor(cursors.toArray(new Cursor[cursors.size()])) : cursors.get(0);
setNotifyConverationListListeners(cursor);
setNotifyConversationListListeners(cursor);
return cursor;
}
@ -549,7 +546,7 @@ public class ThreadDatabase extends Database {
String query = createQuery(ARCHIVED + " = ? AND " + MESSAGE_COUNT + " != 0", 0);
Cursor cursor = db.rawQuery(query, new String[]{archived});
setNotifyConverationListListeners(cursor);
setNotifyConversationListListeners(cursor);
return cursor;
}

View File

@ -37,9 +37,9 @@ public class MessageDetailsLoader extends AbstractCursorLoader {
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");
}