diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/api/email/sync/tasks/ArchiveMsgsSyncTask.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/api/email/sync/tasks/ArchiveMsgsSyncTask.kt index e7dfc2beda..9a87b580cf 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/api/email/sync/tasks/ArchiveMsgsSyncTask.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/api/email/sync/tasks/ArchiveMsgsSyncTask.kt @@ -10,7 +10,6 @@ import com.flowcrypt.email.api.email.sync.SyncListener import com.flowcrypt.email.database.FlowCryptRoomDatabase import com.flowcrypt.email.database.MessageState import com.flowcrypt.email.database.dao.source.AccountDao -import com.flowcrypt.email.database.dao.source.imap.MessageDaoSource import com.sun.mail.imap.IMAPFolder import javax.mail.Folder import javax.mail.Message @@ -32,11 +31,11 @@ class ArchiveMsgsSyncTask(ownerKey: String, requestCode: Int) : BaseSyncTask(own val foldersManager = FoldersManager.fromDatabase(context, account.email) val inboxFolder = foldersManager.findInboxFolder() ?: return val allMailFolder = foldersManager.folderAll ?: return - val msgDaoSource = MessageDaoSource() + val roomDatabase = FlowCryptRoomDatabase.getDatabase(context) while (true) { - val candidatesForArchiving = msgDaoSource.getMsgsWithState(context, account.email, - MessageState.PENDING_ARCHIVING) + val candidatesForArchiving = roomDatabase.msgDao().getMsgsWithState(account.email, + MessageState.PENDING_ARCHIVING.value) if (candidatesForArchiving.isEmpty()) { break @@ -49,7 +48,7 @@ class ArchiveMsgsSyncTask(ownerKey: String, requestCode: Int) : BaseSyncTask(own if (msgs.isNotEmpty()) { remoteSrcFolder.moveMessages(msgs.toTypedArray(), remoteDestFolder) - FlowCryptRoomDatabase.getDatabase(context).msgDao().deleteByUIDs(account.email, inboxFolder.fullName, uidList) + roomDatabase.msgDao().deleteByUIDs(account.email, inboxFolder.fullName, uidList) } remoteSrcFolder.close() diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/api/email/sync/tasks/ChangeMsgsReadStateSyncTask.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/api/email/sync/tasks/ChangeMsgsReadStateSyncTask.kt index 7b898fa425..f94b3dfc57 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/api/email/sync/tasks/ChangeMsgsReadStateSyncTask.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/api/email/sync/tasks/ChangeMsgsReadStateSyncTask.kt @@ -7,6 +7,7 @@ package com.flowcrypt.email.api.email.sync.tasks import android.content.Context import com.flowcrypt.email.api.email.sync.SyncListener +import com.flowcrypt.email.database.FlowCryptRoomDatabase import com.flowcrypt.email.database.MessageState import com.flowcrypt.email.database.dao.source.AccountDao import com.flowcrypt.email.database.dao.source.imap.MessageDaoSource @@ -40,13 +41,14 @@ class ChangeMsgsReadStateSyncTask(ownerKey: String, requestCode: Int) : BaseSync private fun changeMsgsReadState(context: Context, account: AccountDao, msgDaoSource: MessageDaoSource, store: Store, state: MessageState) { - val candidatesForMark = msgDaoSource.getMsgsWithState(context, account.email, state) + val roomDatabase = FlowCryptRoomDatabase.getDatabase(context) + val candidatesForMark = roomDatabase.msgDao().getMsgsWithState(account.email, state.value) if (candidatesForMark.isNotEmpty()) { - val setOfFolders = candidatesForMark.map { it.label }.toSet() + val setOfFolders = candidatesForMark.map { it.folder }.toSet() for (folder in setOfFolders) { - val filteredMsgs = candidatesForMark.filter { it.label == folder } + val filteredMsgs = candidatesForMark.filter { it.folder == folder } if (filteredMsgs.isEmpty()) { continue diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/api/email/sync/tasks/DeleteMessagesSyncTask.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/api/email/sync/tasks/DeleteMessagesSyncTask.kt index 149738649b..5591b763e7 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/api/email/sync/tasks/DeleteMessagesSyncTask.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/api/email/sync/tasks/DeleteMessagesSyncTask.kt @@ -11,7 +11,6 @@ import com.flowcrypt.email.api.email.sync.SyncListener import com.flowcrypt.email.database.FlowCryptRoomDatabase import com.flowcrypt.email.database.MessageState import com.flowcrypt.email.database.dao.source.AccountDao -import com.flowcrypt.email.database.dao.source.imap.MessageDaoSource import com.sun.mail.imap.IMAPFolder import javax.mail.Folder import javax.mail.Message @@ -35,19 +34,19 @@ class DeleteMessagesSyncTask(ownerKey: String, val context = listener.context val foldersManager = FoldersManager.fromDatabase(context, account.email) val trash = foldersManager.folderTrash ?: return - val msgDaoSource = MessageDaoSource() + val roomDatabase = FlowCryptRoomDatabase.getDatabase(context) while (true) { - val candidatesForDeleting = msgDaoSource.getMsgsWithState( - context, account.email, MessageState.PENDING_DELETING) + val candidatesForDeleting = roomDatabase.msgDao().getMsgsWithState( + account.email, MessageState.PENDING_DELETING.value) if (candidatesForDeleting.isEmpty()) { break } else { - val setOfFolders = candidatesForDeleting.map { it.label }.toSet() + val setOfFolders = candidatesForDeleting.map { it.folder }.toSet() for (folder in setOfFolders) { - val filteredMsgs = candidatesForDeleting.filter { it.label == folder } + val filteredMsgs = candidatesForDeleting.filter { it.folder == folder } if (filteredMsgs.isEmpty() || JavaEmailConstants.FOLDER_OUTBOX.equals(folder, ignoreCase = true)) { continue @@ -62,7 +61,7 @@ class DeleteMessagesSyncTask(ownerKey: String, if (msgs.isNotEmpty()) { remoteSrcFolder.moveMessages(msgs.toTypedArray(), remoteDestFolder) - FlowCryptRoomDatabase.getDatabase(context).msgDao().deleteByUIDs(account.email, folder, uidList) + roomDatabase.msgDao().deleteByUIDs(account.email, folder, uidList) } remoteSrcFolder.close() diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/api/email/sync/tasks/MovingToInboxSyncTask.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/api/email/sync/tasks/MovingToInboxSyncTask.kt index 0ce990bba1..a672e424fe 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/api/email/sync/tasks/MovingToInboxSyncTask.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/api/email/sync/tasks/MovingToInboxSyncTask.kt @@ -11,7 +11,6 @@ import com.flowcrypt.email.api.email.sync.SyncListener import com.flowcrypt.email.database.FlowCryptRoomDatabase import com.flowcrypt.email.database.MessageState import com.flowcrypt.email.database.dao.source.AccountDao -import com.flowcrypt.email.database.dao.source.imap.MessageDaoSource import com.sun.mail.imap.IMAPFolder import javax.mail.Folder import javax.mail.Message @@ -32,19 +31,19 @@ class MovingToInboxSyncTask(ownerKey: String, requestCode: Int) : BaseSyncTask(o val context = listener.context val foldersManager = FoldersManager.fromDatabase(context, account.email) val inboxFolder = foldersManager.findInboxFolder() ?: return - val msgDaoSource = MessageDaoSource() + val roomDatabase = FlowCryptRoomDatabase.getDatabase(context) while (true) { - val candidatesForMovingToInbox = msgDaoSource.getMsgsWithState(context, account.email, - MessageState.PENDING_MOVE_TO_INBOX) + val candidatesForMovingToInbox = roomDatabase.msgDao().getMsgsWithState(account.email, + MessageState.PENDING_MOVE_TO_INBOX.value) if (candidatesForMovingToInbox.isEmpty()) { break } else { - val setOfFolders = candidatesForMovingToInbox.map { it.label }.toSet() + val setOfFolders = candidatesForMovingToInbox.map { it.folder }.toSet() for (folder in setOfFolders) { - val filteredMsgs = candidatesForMovingToInbox.filter { it.label == folder } + val filteredMsgs = candidatesForMovingToInbox.filter { it.folder == folder } if (filteredMsgs.isEmpty() || JavaEmailConstants.FOLDER_OUTBOX.equals(folder, ignoreCase = true)) { continue @@ -59,7 +58,7 @@ class MovingToInboxSyncTask(ownerKey: String, requestCode: Int) : BaseSyncTask(o if (msgs.isNotEmpty()) { remoteSrcFolder.moveMessages(msgs.toTypedArray(), remoteDestFolder) - FlowCryptRoomDatabase.getDatabase(context).msgDao().deleteByUIDs(account.email, inboxFolder.fullName, uidList) + roomDatabase.msgDao().deleteByUIDs(account.email, inboxFolder.fullName, uidList) } remoteSrcFolder.close(false) diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/database/dao/MessagesDao.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/database/dao/MessagesDao.kt index f81a246cbc..769b5522cc 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/database/dao/MessagesDao.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/database/dao/MessagesDao.kt @@ -117,6 +117,9 @@ abstract class MessagesDao : BaseDao { @Query("SELECT uid, flags FROM messages WHERE email = :account AND folder = :label") abstract fun getUIDAndFlagsPairs(account: String?, label: String): List + @Query("SELECT * FROM messages WHERE email = :account AND state =:stateValue") + abstract fun getMsgsWithState(account: String?, stateValue: Int): List + @Transaction open fun deleteByUIDs(email: String?, label: String?, msgsUID: Collection) { val step = 50 diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/database/dao/source/imap/MessageDaoSource.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/database/dao/source/imap/MessageDaoSource.kt index 6ea1e906d6..d4cc501821 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/database/dao/source/imap/MessageDaoSource.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/database/dao/source/imap/MessageDaoSource.kt @@ -199,33 +199,6 @@ class MessageDaoSource : BaseDaoSource() { return details } - /** - * Get messages of some folder by the given state. - * - * @param context Interface to global information about an application environment. - * @param email The email of the [LocalFolder]. - * @param state The message state. - * @return A list of [GeneralMessageDetails] objects. - */ - fun getMsgsWithState(context: Context, email: String, state: MessageState): - List { - val contentResolver = context.contentResolver - val selection = "$COL_EMAIL= ? AND $COL_STATE = ?" - val cursor = contentResolver.query(baseContentUri, - null, selection, arrayOf(email, state.value.toString()), null) - - val generalMsgDetailsList = mutableListOf() - - if (cursor != null) { - while (cursor.moveToNext()) { - generalMsgDetailsList.add(getMsgInfo(cursor)) - } - cursor.close() - } - - return generalMsgDetailsList - } - /** * Get new messages. *