From 13902607b2d4393f672731a888847fa6ee734975 Mon Sep 17 00:00:00 2001 From: Al Lansley Date: Thu, 18 Apr 2024 08:51:04 +1000 Subject: [PATCH] Commit before converting SmsDatabase from Java to Kotlin --- .../securesms/database/SmsDatabase.java | 22 +++--- .../securesms/database/ThreadDatabase.java | 76 ++++++++++--------- .../database/helpers/SQLCipherOpenHelper.java | 2 +- 3 files changed, 53 insertions(+), 47 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/SmsDatabase.java b/app/src/main/java/org/thoughtcrime/securesms/database/SmsDatabase.java index 7f92f366a8..5166b4fdf3 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/SmsDatabase.java +++ b/app/src/main/java/org/thoughtcrime/securesms/database/SmsDatabase.java @@ -701,12 +701,7 @@ public class SmsDatabase extends MessagingDatabase { } } - /*package */void deleteThread(long threadId) { - SQLiteDatabase db = databaseHelper.getWritableDatabase(); - db.delete(TABLE_NAME, THREAD_ID + " = ?", new String[] {threadId+""}); - } - - /*package*/void deleteMessagesInThreadBeforeDate(long threadId, long date) { + void deleteMessagesInThreadBeforeDate(long threadId, long date) { SQLiteDatabase db = databaseHelper.getWritableDatabase(); String where = THREAD_ID + " = ? AND (CASE " + TYPE; @@ -719,7 +714,12 @@ public class SmsDatabase extends MessagingDatabase { db.delete(TABLE_NAME, where, new String[] {threadId + ""}); } - /*package*/ void deleteThreads(Set threadIds) { + void deleteThread(long threadId) { + SQLiteDatabase db = databaseHelper.getWritableDatabase(); + db.delete(TABLE_NAME, THREAD_ID + " = ?", new String[] {threadId+""}); + } + + void deleteThreads(Set threadIds) { SQLiteDatabase db = databaseHelper.getWritableDatabase(); String where = ""; @@ -727,23 +727,23 @@ public class SmsDatabase extends MessagingDatabase { where += THREAD_ID + " = '" + threadId + "' OR "; } - where = where.substring(0, where.length() - 4); + where = where.substring(0, where.length() - 4); // Remove the final: "' OR " db.delete(TABLE_NAME, where, null); } - /*package */ void deleteAllThreads() { + void deleteAllThreads() { SQLiteDatabase db = databaseHelper.getWritableDatabase(); db.delete(TABLE_NAME, null, null); } - /*package*/ SQLiteDatabase beginTransaction() { + SQLiteDatabase beginTransaction() { SQLiteDatabase database = databaseHelper.getWritableDatabase(); database.beginTransaction(); return database; } - /*package*/ void endTransaction(SQLiteDatabase database) { + void endTransaction(SQLiteDatabase database) { database.setTransactionSuccessful(); database.endTransaction(); } diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/ThreadDatabase.java b/app/src/main/java/org/thoughtcrime/securesms/database/ThreadDatabase.java index 507088a0ad..c8605b1a59 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/ThreadDatabase.java +++ b/app/src/main/java/org/thoughtcrime/securesms/database/ThreadDatabase.java @@ -26,14 +26,10 @@ import android.content.Context; import android.database.Cursor; import android.database.MergeCursor; import android.net.Uri; - import androidx.annotation.NonNull; import androidx.annotation.Nullable; - import com.annimon.stream.Stream; - import net.zetetic.database.sqlcipher.SQLiteDatabase; - import org.jetbrains.annotations.NotNull; import org.session.libsession.snode.SnodeAPI; import org.session.libsession.utilities.Address; @@ -61,14 +57,12 @@ import org.thoughtcrime.securesms.mms.Slide; import org.thoughtcrime.securesms.mms.SlideDeck; import org.thoughtcrime.securesms.notifications.MarkReadReceiver; import org.thoughtcrime.securesms.util.SessionMetaProtocol; - import java.io.Closeable; import java.util.Collections; 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 { @@ -83,7 +77,7 @@ public class ThreadDatabase extends Database { public static final String TABLE_NAME = "thread"; public static final String ID = "_id"; - public static final String DATE = "date"; + public static final String THREAD_CREATION_DATE = "date"; public static final String MESSAGE_COUNT = "message_count"; public static final String ADDRESS = "recipient_ids"; public static final String SNIPPET = "snippet"; @@ -91,7 +85,7 @@ public class ThreadDatabase extends Database { public static final String READ = "read"; public static final String UNREAD_COUNT = "unread_count"; public static final String UNREAD_MENTION_COUNT = "unread_mention_count"; - public static final String TYPE = "type"; + public static final String DISTRIBUTION_TYPE = "type"; // See: DistributionTypes.kt private static final String ERROR = "error"; public static final String SNIPPET_TYPE = "snippet_type"; public static final String SNIPPET_URI = "snippet_uri"; @@ -101,27 +95,27 @@ public class ThreadDatabase extends Database { public static final String READ_RECEIPT_COUNT = "read_receipt_count"; public static final String EXPIRES_IN = "expires_in"; public static final String LAST_SEEN = "last_seen"; - public static final String HAS_SENT = "has_sent"; + public static final String HAS_SENT = "has_sent"; public static final String IS_PINNED = "is_pinned"; public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" + - ID + " INTEGER PRIMARY KEY, " + DATE + " INTEGER DEFAULT 0, " + + ID + " INTEGER PRIMARY KEY, " + THREAD_CREATION_DATE + " INTEGER DEFAULT 0, " + MESSAGE_COUNT + " INTEGER DEFAULT 0, " + ADDRESS + " TEXT, " + SNIPPET + " TEXT, " + SNIPPET_CHARSET + " INTEGER DEFAULT 0, " + READ + " INTEGER DEFAULT 1, " + - TYPE + " INTEGER DEFAULT 0, " + ERROR + " INTEGER DEFAULT 0, " + + DISTRIBUTION_TYPE + " INTEGER DEFAULT 0, " + ERROR + " INTEGER DEFAULT 0, " + SNIPPET_TYPE + " INTEGER DEFAULT 0, " + SNIPPET_URI + " TEXT DEFAULT NULL, " + ARCHIVED + " INTEGER DEFAULT 0, " + STATUS + " INTEGER DEFAULT 0, " + DELIVERY_RECEIPT_COUNT + " INTEGER DEFAULT 0, " + EXPIRES_IN + " INTEGER DEFAULT 0, " + LAST_SEEN + " INTEGER DEFAULT 0, " + HAS_SENT + " INTEGER DEFAULT 0, " + READ_RECEIPT_COUNT + " INTEGER DEFAULT 0, " + UNREAD_COUNT + " INTEGER DEFAULT 0);"; - public static final String[] CREATE_INDEXS = { + public static final String[] CREATE_INDEXES = { "CREATE INDEX IF NOT EXISTS thread_recipient_ids_index ON " + TABLE_NAME + " (" + ADDRESS + ");", "CREATE INDEX IF NOT EXISTS archived_count_index ON " + TABLE_NAME + " (" + ARCHIVED + ", " + MESSAGE_COUNT + ");", }; private static final String[] THREAD_PROJECTION = { - ID, DATE, MESSAGE_COUNT, ADDRESS, SNIPPET, SNIPPET_CHARSET, READ, UNREAD_COUNT, UNREAD_MENTION_COUNT, TYPE, ERROR, SNIPPET_TYPE, + ID, THREAD_CREATION_DATE, MESSAGE_COUNT, ADDRESS, SNIPPET, SNIPPET_CHARSET, READ, UNREAD_COUNT, UNREAD_MENTION_COUNT, DISTRIBUTION_TYPE, ERROR, SNIPPET_TYPE, SNIPPET_URI, ARCHIVED, STATUS, DELIVERY_RECEIPT_COUNT, EXPIRES_IN, LAST_SEEN, READ_RECEIPT_COUNT, IS_PINNED }; @@ -131,8 +125,8 @@ public class ThreadDatabase extends Database { private static final List COMBINED_THREAD_RECIPIENT_GROUP_PROJECTION = Stream.concat(Stream.concat(Stream.of(TYPED_THREAD_PROJECTION), Stream.of(RecipientDatabase.TYPED_RECIPIENT_PROJECTION)), - Stream.of(GroupDatabase.TYPED_GROUP_PROJECTION)) - .toList(); + Stream.of(GroupDatabase.TYPED_GROUP_PROJECTION)) + .toList(); public static String getCreatePinnedCommand() { return "ALTER TABLE "+ TABLE_NAME + " " + @@ -158,11 +152,10 @@ public class ThreadDatabase extends Database { ContentValues contentValues = new ContentValues(4); long date = SnodeAPI.getNowWithOffset(); - contentValues.put(DATE, date - date % 1000); + contentValues.put(THREAD_CREATION_DATE, date - date % 1000); contentValues.put(ADDRESS, address.serialize()); - if (group) - contentValues.put(TYPE, distributionType); + if (group) contentValues.put(DISTRIBUTION_TYPE, distributionType); contentValues.put(MESSAGE_COUNT, 0); @@ -175,7 +168,7 @@ public class ThreadDatabase extends Database { long expiresIn, int readReceiptCount) { ContentValues contentValues = new ContentValues(7); - contentValues.put(DATE, date - date % 1000); + contentValues.put(THREAD_CREATION_DATE, date - date % 1000); contentValues.put(MESSAGE_COUNT, count); if (!body.isEmpty()) { contentValues.put(SNIPPET, body); @@ -187,19 +180,19 @@ public class ThreadDatabase extends Database { contentValues.put(READ_RECEIPT_COUNT, readReceiptCount); contentValues.put(EXPIRES_IN, expiresIn); - if (unarchive) { - contentValues.put(ARCHIVED, 0); - } + if (unarchive) { contentValues.put(ARCHIVED, 0); } SQLiteDatabase db = databaseHelper.getWritableDatabase(); db.update(TABLE_NAME, contentValues, ID + " = ?", new String[] {threadId + ""}); notifyConversationListListeners(); } + // ACL remove unused method + /* public void updateSnippet(long threadId, String snippet, @Nullable Uri attachment, long date, long type, boolean unarchive) { ContentValues contentValues = new ContentValues(4); - contentValues.put(DATE, date - date % 1000); + contentValues.put(THREAD_CREATION_DATE, date - date % 1000); if (!snippet.isEmpty()) { contentValues.put(SNIPPET, snippet); } @@ -214,6 +207,7 @@ public class ThreadDatabase extends Database { db.update(TABLE_NAME, contentValues, ID + " = ?", new String[] {threadId + ""}); notifyConversationListListeners(); } + */ private void deleteThread(long threadId) { Recipient recipient = getRecipientForThreadId(threadId); @@ -226,6 +220,8 @@ public class ThreadDatabase extends Database { } } + // ACL remove unused methods + /* private void deleteThreads(Set threadIds) { SQLiteDatabase db = databaseHelper.getWritableDatabase(); String where = ""; @@ -249,6 +245,7 @@ public class ThreadDatabase extends Database { addressCache.clear(); notifyConversationListListeners(); } + */ public void trimAllThreads(int length, ProgressListener listener) { Cursor cursor = null; @@ -356,18 +353,21 @@ public class ThreadDatabase extends Database { }}; } + // ACL remove unused method + /* public void setDistributionType(long threadId, int distributionType) { ContentValues contentValues = new ContentValues(1); - contentValues.put(TYPE, distributionType); + contentValues.put(CONVERSATION_TYPE, distributionType); SQLiteDatabase db = databaseHelper.getWritableDatabase(); db.update(TABLE_NAME, contentValues, ID_WHERE, new String[] {threadId + ""}); notifyConversationListListeners(); } + */ public void setDate(long threadId, long date) { ContentValues contentValues = new ContentValues(1); - contentValues.put(DATE, date); + contentValues.put(THREAD_CREATION_DATE, date); SQLiteDatabase db = databaseHelper.getWritableDatabase(); int updated = db.update(TABLE_NAME, contentValues, ID_WHERE, new String[] {threadId+""}); if (updated > 0) notifyConversationListListeners(); @@ -375,11 +375,11 @@ public class ThreadDatabase extends Database { public int getDistributionType(long threadId) { SQLiteDatabase db = databaseHelper.getReadableDatabase(); - Cursor cursor = db.query(TABLE_NAME, new String[]{TYPE}, ID_WHERE, new String[]{String.valueOf(threadId)}, null, null, null); + Cursor cursor = db.query(TABLE_NAME, new String[]{DISTRIBUTION_TYPE}, ID_WHERE, new String[]{String.valueOf(threadId)}, null, null, null); try { if (cursor != null && cursor.moveToNext()) { - return cursor.getInt(cursor.getColumnIndexOrThrow(TYPE)); + return cursor.getInt(cursor.getColumnIndexOrThrow(DISTRIBUTION_TYPE)); } return DistributionTypes.DEFAULT; @@ -469,7 +469,7 @@ public class ThreadDatabase extends Database { Cursor cursor = null; try { - String where = "SELECT " + DATE + " FROM " + TABLE_NAME + + String where = "SELECT " + THREAD_CREATION_DATE + " FROM " + TABLE_NAME + " LEFT OUTER JOIN " + RecipientDatabase.TABLE_NAME + " ON " + TABLE_NAME + "." + ADDRESS + " = " + RecipientDatabase.TABLE_NAME + "." + RecipientDatabase.ADDRESS + " LEFT OUTER JOIN " + GroupDatabase.TABLE_NAME + @@ -477,7 +477,7 @@ public class ThreadDatabase extends Database { " WHERE " + MESSAGE_COUNT + " != 0 AND " + ARCHIVED + " = 0 AND " + HAS_SENT + " = 0 AND " + RecipientDatabase.TABLE_NAME + "." + RecipientDatabase.BLOCK + " = 0 AND " + RecipientDatabase.TABLE_NAME + "." + RecipientDatabase.APPROVED + " = 0 AND " + - GroupDatabase.TABLE_NAME + "." + GROUP_ID + " IS NULL ORDER BY " + DATE + " DESC LIMIT 1"; + GroupDatabase.TABLE_NAME + "." + GROUP_ID + " IS NULL ORDER BY " + THREAD_CREATION_DATE + " DESC LIMIT 1"; cursor = db.rawQuery(where, null); if (cursor != null && cursor.moveToFirst()) @@ -595,7 +595,7 @@ public class ThreadDatabase extends Database { public Long getLastUpdated(long threadId) { SQLiteDatabase db = databaseHelper.getReadableDatabase(); - Cursor cursor = db.query(TABLE_NAME, new String[]{DATE}, ID_WHERE, new String[]{String.valueOf(threadId)}, null, null, null); + Cursor cursor = db.query(TABLE_NAME, new String[]{THREAD_CREATION_DATE}, ID_WHERE, new String[]{String.valueOf(threadId)}, null, null, null); try { if (cursor != null && cursor.moveToFirst()) { @@ -622,6 +622,9 @@ public class ThreadDatabase extends Database { } public void deleteConversation(long threadId) { + + Log.w("[ACL]", "Hit ThreadDatabase.deleteConversation"); + DatabaseComponent.get(context).smsDatabase().deleteThread(threadId); DatabaseComponent.get(context).mmsDatabase().deleteThread(threadId); DatabaseComponent.get(context).draftDatabase().clearDrafts(threadId); @@ -733,10 +736,13 @@ public class ThreadDatabase extends Database { } public boolean update(long threadId, boolean unarchive, boolean shouldDeleteOnEmpty) { + + Log.w("[ACL]", "Hit ThreadDatabase.update!"); + MmsSmsDatabase mmsSmsDatabase = DatabaseComponent.get(context).mmsSmsDatabase(); long count = mmsSmsDatabase.getConversationCount(threadId); - boolean shouldDeleteEmptyThread = shouldDeleteOnEmpty && deleteThreadOnEmpty(threadId); + boolean shouldDeleteEmptyThread = shouldDeleteOnEmpty && possibleToDeleteThreadOnEmpty(threadId); if (count == 0 && shouldDeleteEmptyThread) { deleteThread(threadId); @@ -810,7 +816,7 @@ public class ThreadDatabase extends Database { return setLastSeen(threadId, lastSeenTime); } - private boolean deleteThreadOnEmpty(long threadId) { + private boolean possibleToDeleteThreadOnEmpty(long threadId) { Recipient threadRecipient = getRecipientForThreadId(threadId); return threadRecipient != null && !threadRecipient.isCommunityRecipient(); } @@ -855,7 +861,7 @@ public class ThreadDatabase extends Database { " LEFT OUTER JOIN " + GroupDatabase.TABLE_NAME + " ON " + TABLE_NAME + "." + ADDRESS + " = " + GroupDatabase.TABLE_NAME + "." + GROUP_ID + " WHERE " + where + - " ORDER BY " + TABLE_NAME + "." + IS_PINNED + " DESC, " + TABLE_NAME + "." + DATE + " DESC"; + " ORDER BY " + TABLE_NAME + "." + IS_PINNED + " DESC, " + TABLE_NAME + "." + THREAD_CREATION_DATE + " DESC"; if (limit > 0) { query += " LIMIT " + limit; @@ -900,7 +906,7 @@ public class ThreadDatabase extends Database { public ThreadRecord getCurrent() { long threadId = cursor.getLong(cursor.getColumnIndexOrThrow(ThreadDatabase.ID)); - int distributionType = cursor.getInt(cursor.getColumnIndexOrThrow(ThreadDatabase.TYPE)); + int distributionType = cursor.getInt(cursor.getColumnIndexOrThrow(ThreadDatabase.DISTRIBUTION_TYPE)); Address address = Address.fromSerialized(cursor.getString(cursor.getColumnIndexOrThrow(ThreadDatabase.ADDRESS))); Optional settings; @@ -916,7 +922,7 @@ public class ThreadDatabase extends Database { Recipient recipient = Recipient.from(context, address, settings, groupRecord, true); String body = cursor.getString(cursor.getColumnIndexOrThrow(ThreadDatabase.SNIPPET)); - long date = cursor.getLong(cursor.getColumnIndexOrThrow(ThreadDatabase.DATE)); + long date = cursor.getLong(cursor.getColumnIndexOrThrow(ThreadDatabase.THREAD_CREATION_DATE)); long count = cursor.getLong(cursor.getColumnIndexOrThrow(ThreadDatabase.MESSAGE_COUNT)); int unreadCount = cursor.getInt(cursor.getColumnIndexOrThrow(ThreadDatabase.UNREAD_COUNT)); int unreadMentionCount = cursor.getInt(cursor.getColumnIndexOrThrow(ThreadDatabase.UNREAD_MENTION_COUNT)); diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java b/app/src/main/java/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java index 7713043c2c..cd1988e83f 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java +++ b/app/src/main/java/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java @@ -357,7 +357,7 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper { executeStatements(db, SmsDatabase.CREATE_INDEXS); executeStatements(db, MmsDatabase.CREATE_INDEXS); executeStatements(db, AttachmentDatabase.CREATE_INDEXS); - executeStatements(db, ThreadDatabase.CREATE_INDEXS); + executeStatements(db, ThreadDatabase.CREATE_INDEXES); executeStatements(db, DraftDatabase.CREATE_INDEXS); executeStatements(db, GroupDatabase.CREATE_INDEXS); executeStatements(db, GroupReceiptDatabase.CREATE_INDEXES);