Skip to content

Commit

Permalink
Connect "mark all read" to sync and read receipts
Browse files Browse the repository at this point in the history
Fixes signalapp#7069
// FREEBIE
  • Loading branch information
moxie0 committed Oct 9, 2017
1 parent 5be1a5e commit 445f3c2
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 58 deletions.
14 changes: 12 additions & 2 deletions src/org/thoughtcrime/securesms/ConversationListActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,13 +37,18 @@
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;
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
{
Expand Down Expand Up @@ -201,8 +207,12 @@ private void handleMarkAllRead() {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
DatabaseFactory.getThreadDatabase(ConversationListActivity.this).setAllThreadsRead();
MessageNotifier.updateNotification(ConversationListActivity.this, masterSecret);
Context context = ConversationListActivity.this;
List<MarkedMessageInfo> messageIds = DatabaseFactory.getThreadDatabase(context).setAllThreadsRead();

MessageNotifier.updateNotification(context, masterSecret);
MarkReadReceiver.process(context, messageIds);

return null;
}
}.execute();
Expand Down
29 changes: 14 additions & 15 deletions src/org/thoughtcrime/securesms/database/MmsDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -413,17 +413,24 @@ public void markAsNotified(long id) {
database.update(TABLE_NAME, contentValues, ID_WHERE, new String[] {String.valueOf(id)});
}


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> result = new LinkedList<>();
Cursor cursor = null;
return setMessagesRead(THREAD_ID + " = ? AND " + READ + " = 0", new String[] {String.valueOf(threadId)});
}

public List<MarkedMessageInfo> setAllMessagesRead() {
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();

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))) {
Expand All @@ -437,7 +444,7 @@ public List<MarkedMessageInfo> setMessagesRead(long threadId) {
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();
Expand Down Expand Up @@ -487,14 +494,6 @@ public List<Pair<Long, Long>> setTimestampRead(SyncMessageId messageId, long exp
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);

Expand Down
74 changes: 36 additions & 38 deletions src/org/thoughtcrime/securesms/database/SmsDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -329,39 +329,6 @@ public void incrementReceiptCount(SyncMessageId messageId, boolean deliveryRecei
}
}

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) {
SQLiteDatabase database = databaseHelper.getWritableDatabase();
List<Pair<Long, Long>> expiring = new LinkedList<>();
Expand Down Expand Up @@ -403,12 +370,43 @@ public List<Pair<Long, Long>> setTimestampRead(SyncMessageId messageId, long exp
return expiring;
}

public void setAllMessagesRead() {
SQLiteDatabase database = databaseHelper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(READ, 1);
public List<MarkedMessageInfo> setMessagesRead(long threadId) {
return setMessagesRead(THREAD_ID + " = ? AND " + READ + " = 0", new String[] {String.valueOf(threadId)});
}

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);

database.update(TABLE_NAME, contentValues, 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) {
Expand Down
12 changes: 9 additions & 3 deletions src/org/thoughtcrime/securesms/database/ThreadDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -244,16 +244,22 @@ public void trimThread(long threadId, int length) {
}
}

public void setAllThreadsRead() {
public List<MarkedMessageInfo> 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<MarkedMessageInfo> smsRecords = DatabaseFactory.getSmsDatabase(context).setAllMessagesRead();
final List<MarkedMessageInfo> mmsRecords = DatabaseFactory.getMmsDatabase(context).setAllMessagesRead();

notifyConversationListListeners();

return new LinkedList<MarkedMessageInfo>() {{
addAll(smsRecords);
addAll(mmsRecords);
}};
}

public List<MarkedMessageInfo> setRead(long threadId, boolean lastSeen) {
Expand Down

0 comments on commit 445f3c2

Please sign in to comment.