|
|
|
@ -50,14 +50,21 @@ import org.whispersystems.libsignal.util.Pair;
|
|
|
|
|
import org.whispersystems.libsignal.util.guava.Optional;
|
|
|
|
|
|
|
|
|
|
import java.io.Closeable;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.LinkedList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
|
|
public class ThreadDatabase extends Database {
|
|
|
|
|
|
|
|
|
|
private static final String TAG = ThreadDatabase.class.getSimpleName();
|
|
|
|
|
|
|
|
|
|
private Map<Long, Address> addressCache = new HashMap<>();
|
|
|
|
|
private Map<Long, List<ContentValues>> messageCache = new HashMap<>();
|
|
|
|
|
private Map<Long, String> recipientCache = new HashMap<>();
|
|
|
|
|
|
|
|
|
|
public static final String TABLE_NAME = "thread";
|
|
|
|
|
public static final String ID = "_id";
|
|
|
|
|
public static final String DATE = "date";
|
|
|
|
@ -150,11 +157,34 @@ public class ThreadDatabase extends Database {
|
|
|
|
|
contentValues.put(ARCHIVED, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!messageCache.containsKey(threadId)) {
|
|
|
|
|
messageCache.put(threadId, new ArrayList<>());
|
|
|
|
|
}
|
|
|
|
|
messageCache.get(threadId).add(contentValues);
|
|
|
|
|
|
|
|
|
|
if (!recipientCache.containsKey(threadId)) {
|
|
|
|
|
recipientCache.put(threadId, getRecipientForThreadId(threadId).getName().replaceAll("\\s*", ""));
|
|
|
|
|
}
|
|
|
|
|
String key = recipientCache.get(threadId);
|
|
|
|
|
int newMessagesNumber = TextSecurePreferences.getNewMessagesNumber(this.context, key);
|
|
|
|
|
if (newMessagesNumber == 0 || newMessagesNumber == messageCache.get(threadId).size()) {
|
|
|
|
|
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
|
|
|
|
db.update(TABLE_NAME, contentValues, ID + " = ?", new String[] {threadId + ""});
|
|
|
|
|
db.beginTransactionNonExclusive();
|
|
|
|
|
try {
|
|
|
|
|
for (ContentValues contentValue : messageCache.get(threadId)) {
|
|
|
|
|
db.update(TABLE_NAME, contentValue, ID + " = ?", new String[] {threadId + ""});
|
|
|
|
|
}
|
|
|
|
|
TextSecurePreferences.setNewMessagesNumber(this.context, key, TextSecurePreferences.getNewMessagesNumber(this.context, key) - newMessagesNumber);
|
|
|
|
|
messageCache.get(threadId).clear();
|
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
|
notifyConversationListListeners();
|
|
|
|
|
} finally {
|
|
|
|
|
db.endTransaction();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void updateSnippet(long threadId, String snippet, @Nullable Uri attachment, long date, long type, boolean unarchive) {
|
|
|
|
|
ContentValues contentValues = new ContentValues(4);
|
|
|
|
|
|
|
|
|
@ -528,6 +558,13 @@ public class ThreadDatabase extends Database {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public @Nullable Recipient getRecipientForThreadId(long threadId) {
|
|
|
|
|
// Loki - Cache the address.
|
|
|
|
|
// Don't know if this will affect any other signal code
|
|
|
|
|
// Don't know if it is necessary to add some cache time
|
|
|
|
|
if (addressCache.containsKey(threadId) && addressCache.get(threadId) != null) {
|
|
|
|
|
return Recipient.from(context, addressCache.get(threadId), false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SQLiteDatabase db = databaseHelper.getReadableDatabase();
|
|
|
|
|
Cursor cursor = null;
|
|
|
|
|
|
|
|
|
@ -536,6 +573,7 @@ public class ThreadDatabase extends Database {
|
|
|
|
|
|
|
|
|
|
if (cursor != null && cursor.moveToFirst()) {
|
|
|
|
|
Address address = Address.fromSerialized(cursor.getString(cursor.getColumnIndexOrThrow(ADDRESS)));
|
|
|
|
|
addressCache.put(threadId, address);
|
|
|
|
|
return Recipient.from(context, address, false);
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|