Connect "mark all read" to sync and read receipts

Fixes #7069
// FREEBIE
This commit is contained in:
Moxie Marlinspike 2017-10-08 18:09:46 -07:00
parent 5be1a5e3d5
commit 445f3c234c
4 changed files with 71 additions and 58 deletions

View File

@ -17,6 +17,7 @@
package org.thoughtcrime.securesms; package org.thoughtcrime.securesms;
import android.content.ActivityNotFoundException; import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.database.ContentObserver; import android.database.ContentObserver;
import android.net.Uri; import android.net.Uri;
@ -36,6 +37,9 @@ import android.widget.Toast;
import org.thoughtcrime.securesms.components.RatingManager; import org.thoughtcrime.securesms.components.RatingManager;
import org.thoughtcrime.securesms.crypto.MasterSecret; import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.database.DatabaseFactory; 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.notifications.MessageNotifier;
import org.thoughtcrime.securesms.recipients.Recipient; import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.service.KeyCachingService; 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.DynamicTheme;
import org.thoughtcrime.securesms.util.TextSecurePreferences; import org.thoughtcrime.securesms.util.TextSecurePreferences;
import java.util.List;
public class ConversationListActivity extends PassphraseRequiredActionBarActivity public class ConversationListActivity extends PassphraseRequiredActionBarActivity
implements ConversationListFragment.ConversationSelectedListener implements ConversationListFragment.ConversationSelectedListener
{ {
@ -201,8 +207,12 @@ public class ConversationListActivity extends PassphraseRequiredActionBarActivit
new AsyncTask<Void, Void, Void>() { new AsyncTask<Void, Void, Void>() {
@Override @Override
protected Void doInBackground(Void... params) { protected Void doInBackground(Void... params) {
DatabaseFactory.getThreadDatabase(ConversationListActivity.this).setAllThreadsRead(); Context context = ConversationListActivity.this;
MessageNotifier.updateNotification(ConversationListActivity.this, masterSecret); List<MarkedMessageInfo> messageIds = DatabaseFactory.getThreadDatabase(context).setAllThreadsRead();
MessageNotifier.updateNotification(context, masterSecret);
MarkReadReceiver.process(context, messageIds);
return null; return null;
} }
}.execute(); }.execute();

View File

@ -413,17 +413,24 @@ public class MmsDatabase extends MessagingDatabase {
database.update(TABLE_NAME, contentValues, ID_WHERE, new String[] {String.valueOf(id)}); database.update(TABLE_NAME, contentValues, ID_WHERE, new String[] {String.valueOf(id)});
} }
public List<MarkedMessageInfo> setMessagesRead(long threadId) { public List<MarkedMessageInfo> setMessagesRead(long threadId) {
SQLiteDatabase database = databaseHelper.getWritableDatabase(); return setMessagesRead(THREAD_ID + " = ? AND " + READ + " = 0", new String[] {String.valueOf(threadId)});
String where = THREAD_ID + " = ? AND " + READ + " = 0"; }
String[] selection = new String[]{String.valueOf(threadId)};
List<MarkedMessageInfo> result = new LinkedList<>(); public List<MarkedMessageInfo> setAllMessagesRead() {
Cursor cursor = null; return setMessagesRead(READ + " = 0", null);
}
private List<MarkedMessageInfo> setMessagesRead(String where, String[] arguments) {
SQLiteDatabase database = databaseHelper.getWritableDatabase();
List<MarkedMessageInfo> result = new LinkedList<>();
Cursor cursor = null;
database.beginTransaction(); database.beginTransaction();
try { 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()) { while(cursor != null && cursor.moveToNext()) {
if (Types.isSecureType(cursor.getLong(3))) { if (Types.isSecureType(cursor.getLong(3))) {
@ -437,7 +444,7 @@ public class MmsDatabase extends MessagingDatabase {
ContentValues contentValues = new ContentValues(); ContentValues contentValues = new ContentValues();
contentValues.put(READ, 1); contentValues.put(READ, 1);
database.update(TABLE_NAME, contentValues, where, selection); database.update(TABLE_NAME, contentValues, where, arguments);
database.setTransactionSuccessful(); database.setTransactionSuccessful();
} finally { } finally {
if (cursor != null) cursor.close(); if (cursor != null) cursor.close();
@ -487,14 +494,6 @@ public class MmsDatabase extends MessagingDatabase {
return expiring; 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) { public void updateMessageBody(MasterSecretUnion masterSecret, long messageId, String body) {
body = getEncryptedBody(masterSecret, body); body = getEncryptedBody(masterSecret, body);

View File

@ -329,39 +329,6 @@ public class SmsDatabase extends MessagingDatabase {
} }
} }
public List<MarkedMessageInfo> setMessagesRead(long threadId) {
SQLiteDatabase database = databaseHelper.getWritableDatabase();
String where = THREAD_ID + " = ? AND " + READ + " = 0";
String[] selection = new String[]{String.valueOf(threadId)};
List<MarkedMessageInfo> 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<Pair<Long, Long>> setTimestampRead(SyncMessageId messageId, long expireStarted) { public List<Pair<Long, Long>> setTimestampRead(SyncMessageId messageId, long expireStarted) {
SQLiteDatabase database = databaseHelper.getWritableDatabase(); SQLiteDatabase database = databaseHelper.getWritableDatabase();
List<Pair<Long, Long>> expiring = new LinkedList<>(); List<Pair<Long, Long>> expiring = new LinkedList<>();
@ -403,12 +370,43 @@ public class SmsDatabase extends MessagingDatabase {
return expiring; return expiring;
} }
public void setAllMessagesRead() { public List<MarkedMessageInfo> setMessagesRead(long threadId) {
SQLiteDatabase database = databaseHelper.getWritableDatabase(); return setMessagesRead(THREAD_ID + " = ? AND " + READ + " = 0", new String[] {String.valueOf(threadId)});
ContentValues contentValues = new ContentValues(); }
contentValues.put(READ, 1);
database.update(TABLE_NAME, contentValues, null, null); public List<MarkedMessageInfo> setAllMessagesRead() {
return setMessagesRead(READ + " = 0", null);
}
private List<MarkedMessageInfo> setMessagesRead(String where, String[] arguments) {
SQLiteDatabase database = databaseHelper.getWritableDatabase();
List<MarkedMessageInfo> 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<Long, Long> updateMessageBodyAndType(long messageId, String body, long maskOff, long maskOn) { protected Pair<Long, Long> updateMessageBodyAndType(long messageId, String body, long maskOff, long maskOn) {

View File

@ -244,16 +244,22 @@ public class ThreadDatabase extends Database {
} }
} }
public void setAllThreadsRead() { public List<MarkedMessageInfo> setAllThreadsRead() {
SQLiteDatabase db = databaseHelper.getWritableDatabase(); SQLiteDatabase db = databaseHelper.getWritableDatabase();
ContentValues contentValues = new ContentValues(1); ContentValues contentValues = new ContentValues(1);
contentValues.put(READ, 1); contentValues.put(READ, 1);
db.update(TABLE_NAME, contentValues, null, null); db.update(TABLE_NAME, contentValues, null, null);
DatabaseFactory.getSmsDatabase(context).setAllMessagesRead(); final List<MarkedMessageInfo> smsRecords = DatabaseFactory.getSmsDatabase(context).setAllMessagesRead();
DatabaseFactory.getMmsDatabase(context).setAllMessagesRead(); final List<MarkedMessageInfo> mmsRecords = DatabaseFactory.getMmsDatabase(context).setAllMessagesRead();
notifyConversationListListeners(); notifyConversationListListeners();
return new LinkedList<MarkedMessageInfo>() {{
addAll(smsRecords);
addAll(mmsRecords);
}};
} }
public List<MarkedMessageInfo> setRead(long threadId, boolean lastSeen) { public List<MarkedMessageInfo> setRead(long threadId, boolean lastSeen) {