From 445f3c234cf122a061ad45cff083dc8627dcebe0 Mon Sep 17 00:00:00 2001 From: Moxie Marlinspike Date: Sun, 8 Oct 2017 18:09:46 -0700 Subject: [PATCH] Connect "mark all read" to sync and read receipts Fixes #7069 // FREEBIE --- .../securesms/ConversationListActivity.java | 14 +++- .../securesms/database/MmsDatabase.java | 29 ++++---- .../securesms/database/SmsDatabase.java | 74 +++++++++---------- .../securesms/database/ThreadDatabase.java | 12 ++- 4 files changed, 71 insertions(+), 58 deletions(-) diff --git a/src/org/thoughtcrime/securesms/ConversationListActivity.java b/src/org/thoughtcrime/securesms/ConversationListActivity.java index 7da7d0c521..161e89c624 100644 --- a/src/org/thoughtcrime/securesms/ConversationListActivity.java +++ b/src/org/thoughtcrime/securesms/ConversationListActivity.java @@ -17,6 +17,7 @@ package org.thoughtcrime.securesms; import android.content.ActivityNotFoundException; +import android.content.Context; import android.content.Intent; import android.database.ContentObserver; import android.net.Uri; @@ -36,6 +37,9 @@ import android.widget.Toast; import org.thoughtcrime.securesms.components.RatingManager; import org.thoughtcrime.securesms.crypto.MasterSecret; import org.thoughtcrime.securesms.database.DatabaseFactory; +import org.thoughtcrime.securesms.database.MessagingDatabase; +import org.thoughtcrime.securesms.database.MessagingDatabase.MarkedMessageInfo; +import org.thoughtcrime.securesms.notifications.MarkReadReceiver; import org.thoughtcrime.securesms.notifications.MessageNotifier; import org.thoughtcrime.securesms.recipients.Recipient; import org.thoughtcrime.securesms.service.KeyCachingService; @@ -43,6 +47,8 @@ import org.thoughtcrime.securesms.util.DynamicLanguage; import org.thoughtcrime.securesms.util.DynamicTheme; import org.thoughtcrime.securesms.util.TextSecurePreferences; +import java.util.List; + public class ConversationListActivity extends PassphraseRequiredActionBarActivity implements ConversationListFragment.ConversationSelectedListener { @@ -201,8 +207,12 @@ public class ConversationListActivity extends PassphraseRequiredActionBarActivit new AsyncTask() { @Override protected Void doInBackground(Void... params) { - DatabaseFactory.getThreadDatabase(ConversationListActivity.this).setAllThreadsRead(); - MessageNotifier.updateNotification(ConversationListActivity.this, masterSecret); + Context context = ConversationListActivity.this; + List messageIds = DatabaseFactory.getThreadDatabase(context).setAllThreadsRead(); + + MessageNotifier.updateNotification(context, masterSecret); + MarkReadReceiver.process(context, messageIds); + return null; } }.execute(); diff --git a/src/org/thoughtcrime/securesms/database/MmsDatabase.java b/src/org/thoughtcrime/securesms/database/MmsDatabase.java index 05848b676c..4a5ef4d68c 100644 --- a/src/org/thoughtcrime/securesms/database/MmsDatabase.java +++ b/src/org/thoughtcrime/securesms/database/MmsDatabase.java @@ -413,17 +413,24 @@ public class MmsDatabase extends MessagingDatabase { database.update(TABLE_NAME, contentValues, ID_WHERE, new String[] {String.valueOf(id)}); } + public List setMessagesRead(long threadId) { - SQLiteDatabase database = databaseHelper.getWritableDatabase(); - String where = THREAD_ID + " = ? AND " + READ + " = 0"; - String[] selection = new String[]{String.valueOf(threadId)}; - List result = new LinkedList<>(); - Cursor cursor = null; + return setMessagesRead(THREAD_ID + " = ? AND " + READ + " = 0", new String[] {String.valueOf(threadId)}); + } + + public List setAllMessagesRead() { + return setMessagesRead(READ + " = 0", null); + } + + private List setMessagesRead(String where, String[] arguments) { + SQLiteDatabase database = databaseHelper.getWritableDatabase(); + List result = new LinkedList<>(); + Cursor cursor = null; database.beginTransaction(); try { - cursor = database.query(TABLE_NAME, new String[] {ID, ADDRESS, DATE_SENT, MESSAGE_BOX, EXPIRES_IN, EXPIRE_STARTED}, where, selection, null, null, null); + cursor = database.query(TABLE_NAME, new String[] {ID, ADDRESS, DATE_SENT, MESSAGE_BOX, EXPIRES_IN, EXPIRE_STARTED}, where, arguments, null, null, null); while(cursor != null && cursor.moveToNext()) { if (Types.isSecureType(cursor.getLong(3))) { @@ -437,7 +444,7 @@ public class MmsDatabase extends MessagingDatabase { ContentValues contentValues = new ContentValues(); contentValues.put(READ, 1); - database.update(TABLE_NAME, contentValues, where, selection); + database.update(TABLE_NAME, contentValues, where, arguments); database.setTransactionSuccessful(); } finally { if (cursor != null) cursor.close(); @@ -487,14 +494,6 @@ public class MmsDatabase extends MessagingDatabase { return expiring; } - public void setAllMessagesRead() { - SQLiteDatabase database = databaseHelper.getWritableDatabase(); - ContentValues contentValues = new ContentValues(); - contentValues.put(READ, 1); - - database.update(TABLE_NAME, contentValues, null, null); - } - public void updateMessageBody(MasterSecretUnion masterSecret, long messageId, String body) { body = getEncryptedBody(masterSecret, body); diff --git a/src/org/thoughtcrime/securesms/database/SmsDatabase.java b/src/org/thoughtcrime/securesms/database/SmsDatabase.java index ee589add90..f8ba35c1bb 100644 --- a/src/org/thoughtcrime/securesms/database/SmsDatabase.java +++ b/src/org/thoughtcrime/securesms/database/SmsDatabase.java @@ -329,39 +329,6 @@ public class SmsDatabase extends MessagingDatabase { } } - public List setMessagesRead(long threadId) { - SQLiteDatabase database = databaseHelper.getWritableDatabase(); - String where = THREAD_ID + " = ? AND " + READ + " = 0"; - String[] selection = new String[]{String.valueOf(threadId)}; - List results = new LinkedList<>(); - Cursor cursor = null; - - database.beginTransaction(); - try { - cursor = database.query(TABLE_NAME, new String[] {ID, ADDRESS, DATE_SENT, TYPE, EXPIRES_IN, EXPIRE_STARTED}, where, selection, null, null, null); - - while (cursor != null && cursor.moveToNext()) { - if (Types.isSecureType(cursor.getLong(3))) { - SyncMessageId syncMessageId = new SyncMessageId(Address.fromSerialized(cursor.getString(1)), cursor.getLong(2)); - ExpirationInfo expirationInfo = new ExpirationInfo(cursor.getLong(0), cursor.getLong(4), cursor.getLong(5), false); - - results.add(new MarkedMessageInfo(syncMessageId, expirationInfo)); - } - } - - ContentValues contentValues = new ContentValues(); - contentValues.put(READ, 1); - - database.update(TABLE_NAME, contentValues, where, selection); - database.setTransactionSuccessful(); - } finally { - if (cursor != null) cursor.close(); - database.endTransaction(); - } - - return results; - } - public List> setTimestampRead(SyncMessageId messageId, long expireStarted) { SQLiteDatabase database = databaseHelper.getWritableDatabase(); List> expiring = new LinkedList<>(); @@ -403,12 +370,43 @@ public class SmsDatabase extends MessagingDatabase { return expiring; } - public void setAllMessagesRead() { - SQLiteDatabase database = databaseHelper.getWritableDatabase(); - ContentValues contentValues = new ContentValues(); - contentValues.put(READ, 1); + public List setMessagesRead(long threadId) { + return setMessagesRead(THREAD_ID + " = ? AND " + READ + " = 0", new String[] {String.valueOf(threadId)}); + } - database.update(TABLE_NAME, contentValues, null, null); + public List setAllMessagesRead() { + return setMessagesRead(READ + " = 0", null); + } + + private List setMessagesRead(String where, String[] arguments) { + SQLiteDatabase database = databaseHelper.getWritableDatabase(); + List results = new LinkedList<>(); + Cursor cursor = null; + + database.beginTransaction(); + try { + cursor = database.query(TABLE_NAME, new String[] {ID, ADDRESS, DATE_SENT, TYPE, EXPIRES_IN, EXPIRE_STARTED}, where, arguments, null, null, null); + + while (cursor != null && cursor.moveToNext()) { + if (Types.isSecureType(cursor.getLong(3))) { + SyncMessageId syncMessageId = new SyncMessageId(Address.fromSerialized(cursor.getString(1)), cursor.getLong(2)); + ExpirationInfo expirationInfo = new ExpirationInfo(cursor.getLong(0), cursor.getLong(4), cursor.getLong(5), false); + + results.add(new MarkedMessageInfo(syncMessageId, expirationInfo)); + } + } + + ContentValues contentValues = new ContentValues(); + contentValues.put(READ, 1); + + database.update(TABLE_NAME, contentValues, where, arguments); + database.setTransactionSuccessful(); + } finally { + if (cursor != null) cursor.close(); + database.endTransaction(); + } + + return results; } protected Pair updateMessageBodyAndType(long messageId, String body, long maskOff, long maskOn) { diff --git a/src/org/thoughtcrime/securesms/database/ThreadDatabase.java b/src/org/thoughtcrime/securesms/database/ThreadDatabase.java index 66a1ebac64..943d517d08 100644 --- a/src/org/thoughtcrime/securesms/database/ThreadDatabase.java +++ b/src/org/thoughtcrime/securesms/database/ThreadDatabase.java @@ -244,16 +244,22 @@ public class ThreadDatabase extends Database { } } - public void setAllThreadsRead() { + public List setAllThreadsRead() { SQLiteDatabase db = databaseHelper.getWritableDatabase(); ContentValues contentValues = new ContentValues(1); contentValues.put(READ, 1); db.update(TABLE_NAME, contentValues, null, null); - DatabaseFactory.getSmsDatabase(context).setAllMessagesRead(); - DatabaseFactory.getMmsDatabase(context).setAllMessagesRead(); + final List smsRecords = DatabaseFactory.getSmsDatabase(context).setAllMessagesRead(); + final List mmsRecords = DatabaseFactory.getMmsDatabase(context).setAllMessagesRead(); + notifyConversationListListeners(); + + return new LinkedList() {{ + addAll(smsRecords); + addAll(mmsRecords); + }}; } public List setRead(long threadId, boolean lastSeen) {