Skip to content

Commit

Permalink
Removed getMsgInfo(), getNewMsgs() in MessageDaoSource. Refactored co…
Browse files Browse the repository at this point in the history
…de.| #793
  • Loading branch information
DenBond7 committed Dec 26, 2019
1 parent 3f17d66 commit 1b77a49
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 185 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package com.flowcrypt.email.database.dao

import android.os.Build
import androidx.lifecycle.LiveData
import androidx.paging.DataSource
import androidx.room.Dao
Expand Down Expand Up @@ -32,7 +33,15 @@ abstract class MessagesDao : BaseDao<MessageEntity> {
abstract fun getMsg(account: String?, folder: String?, uid: Long): MessageEntity?

@Query("SELECT * FROM messages WHERE email = :account AND folder = :folder")
abstract fun getMessages(account: String, folder: String): LiveData<MessageEntity>
abstract fun getMsgs(account: String, folder: String): LiveData<MessageEntity>

@Query("SELECT * FROM messages WHERE email = :account AND folder = :folder AND is_new = 1 ORDER BY :orderBy")
abstract fun getNewMsgs(account: String, folder: String,
orderBy: String = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
"received_date ASC"
} else {
"received_date DESC"
}): List<MessageEntity>

@Query("SELECT * FROM messages WHERE email = :account AND folder = :folder AND uid IN (:msgsUID)")
abstract fun getMsgsByUids(account: String?, folder: String?, msgsUID: Collection<Long>?): List<MessageEntity>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,11 @@ package com.flowcrypt.email.database.dao.source.imap

import android.content.ContentValues
import android.content.Context
import android.database.Cursor
import android.os.Build
import android.provider.BaseColumns
import android.text.TextUtils
import com.flowcrypt.email.api.email.model.GeneralMessageDetails
import com.flowcrypt.email.api.email.model.LocalFolder
import com.flowcrypt.email.api.email.model.MessageFlag
import com.flowcrypt.email.database.MessageState
import com.flowcrypt.email.database.dao.source.BaseDaoSource
import java.util.*
import javax.mail.internet.AddressException
import javax.mail.internet.InternetAddress

/**
* This class describes the dao source for [GeneralMessageDetails] class.
Expand Down Expand Up @@ -107,100 +100,6 @@ class MessageDaoSource : BaseDaoSource() {
-1
}

/**
* Generate a [LocalFolder] object from the current cursor position.
*
* @param cursor The [Cursor] which contains information about [LocalFolder].
* @return A generated [LocalFolder].
*/
fun getMsgInfo(cursor: Cursor): GeneralMessageDetails {
val details = GeneralMessageDetails(
cursor.getString(cursor.getColumnIndex(COL_EMAIL)),
cursor.getString(cursor.getColumnIndex(COL_FOLDER)),
cursor.getInt(cursor.getColumnIndex(COL_UID)),
cursor.getInt(cursor.getColumnIndex(BaseColumns._ID)),
cursor.getLong(cursor.getColumnIndex(COL_RECEIVED_DATE)),
cursor.getLong(cursor.getColumnIndex(COL_SENT_DATE)), null, null, null, null,
cursor.getString(cursor.getColumnIndex(COL_SUBJECT)),
listOf(*parseFlags(cursor.getString(cursor.getColumnIndex(COL_FLAGS)))),
!cursor.isNull(cursor.getColumnIndex(COL_RAW_MESSAGE_WITHOUT_ATTACHMENTS)),
cursor.getInt(cursor.getColumnIndex(COL_IS_MESSAGE_HAS_ATTACHMENTS)) == 1,
cursor.getInt(cursor.getColumnIndex(COL_IS_ENCRYPTED)) == 1,
MessageState.generate(cursor.getInt(cursor.getColumnIndex(COL_STATE))),
cursor.getString(cursor.getColumnIndex(COL_ATTACHMENTS_DIRECTORY)),
cursor.getString(cursor.getColumnIndex(COL_ERROR_MSG))
)

try {
val fromAddresses = cursor.getString(cursor.getColumnIndex(COL_FROM_ADDRESSES))
details.from = if (TextUtils.isEmpty(fromAddresses)) null else listOf(*InternetAddress.parse(fromAddresses))
} catch (e: AddressException) {
e.printStackTrace()
}

try {
val replyToAddresses = cursor.getString(cursor.getColumnIndex(COL_REPLY_TO))
details.replyTo = if (TextUtils.isEmpty(replyToAddresses)) null else listOf(*InternetAddress
.parse(replyToAddresses))
} catch (e: AddressException) {
e.printStackTrace()
}

try {
val toAddresses = cursor.getString(cursor.getColumnIndex(COL_TO_ADDRESSES))
details.to = if (TextUtils.isEmpty(toAddresses)) null else listOf(*InternetAddress.parse(toAddresses))
} catch (e: AddressException) {
e.printStackTrace()
}

try {
val ccAddresses = cursor.getString(cursor.getColumnIndex(COL_CC_ADDRESSES))
details.cc = if (TextUtils.isEmpty(ccAddresses)) null else listOf(*InternetAddress.parse(ccAddresses))
} catch (e: AddressException) {
e.printStackTrace()
}

return details
}

/**
* Get new messages.
*
* @param context Interface to global information about an application environment.
* @param email The user email.
* @param label The label name.
* @return A list of [GeneralMessageDetails] objects.
*/
fun getNewMsgs(context: Context, email: String, label: String): List<GeneralMessageDetails> {
val contentResolver = context.contentResolver

val orderType: String = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
"ASC"
} else {
"DESC"
}

val selection = "$COL_EMAIL= ? AND $COL_FOLDER = ? AND $COL_IS_NEW = 1 "
val selectionArgs = arrayOf(email, label)
val cursor = contentResolver.query(baseContentUri, null, selection, selectionArgs,
"$COL_RECEIVED_DATE $orderType")

val detailsList = ArrayList<GeneralMessageDetails>()

if (cursor != null) {
while (cursor.moveToNext()) {
detailsList.add(getMsgInfo(cursor))
}
cursor.close()
}

return detailsList
}

private fun parseFlags(string: String): Array<String> {
return parseArray(string, "\\s")
}

companion object {
const val TABLE_NAME_MESSAGES = "messages"

Expand All @@ -222,15 +121,5 @@ class MessageDaoSource : BaseDaoSource() {
const val COL_ATTACHMENTS_DIRECTORY = "attachments_directory"
const val COL_ERROR_MSG = "error_msg"
const val COL_REPLY_TO = "reply_to"

const val ENCRYPTED_STATE_UNDEFINED = -1

private fun parseArray(attributesAsString: String?, regex: String): Array<String> {
return if (attributesAsString != null && attributesAsString.isNotEmpty()) {
attributesAsString.split(regex.toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
} else {
arrayOf()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import com.flowcrypt.email.api.retrofit.response.model.node.NodeKeyDetails
import com.flowcrypt.email.database.FlowCryptRoomDatabase
import com.flowcrypt.email.database.dao.source.AccountDao
import com.flowcrypt.email.database.dao.source.AccountDaoSource
import com.flowcrypt.email.database.dao.source.imap.MessageDaoSource
import com.flowcrypt.email.database.entity.MessageEntity
import com.flowcrypt.email.service.MessagesNotificationManager
import com.flowcrypt.email.util.GeneralUtil
Expand Down Expand Up @@ -119,35 +118,33 @@ class SyncJobService : JobService(), SyncListener {
remoteFolder: IMAPFolder, newMsgs: Array<Message>,
updateMsgs: Array<Message>, ownerKey: String, requestCode: Int) {
try {
val msgDaoSource = MessageDaoSource()
val roomDatabase = FlowCryptRoomDatabase.getDatabase(applicationContext)

val folderName = localFolder.fullName
val mapOfUIDsAndMsgsFlags = roomDatabase.msgDao().getMapOfUIDAndMsgFlags(account.email, folderName)
val uidSet = HashSet(mapOfUIDsAndMsgsFlags.keys)
val deleteCandidatesUIDs = EmailUtil.genDeleteCandidates(uidSet, remoteFolder, updateMsgs)

val generalMsgDetailsBeforeUpdate = msgDaoSource.getNewMsgs(applicationContext, account.email, folderName)
val msgsBeforeUpdate = roomDatabase.msgDao().getNewMsgs(account.email, folderName)
roomDatabase.msgDao().deleteByUIDs(account.email, folderName, deleteCandidatesUIDs)

val updateCandidates = EmailUtil.genUpdateCandidates(mapOfUIDsAndMsgsFlags, remoteFolder,
updateMsgs).map { remoteFolder.getUID(it) to it.flags }.toMap()
roomDatabase.msgDao().updateFlags(account.email, folderName, updateCandidates)

val detailsAfterUpdate = msgDaoSource.getNewMsgs(applicationContext,
account.email, folderName)
val detailsAfterUpdate = roomDatabase.msgDao().getNewMsgs(account.email, folderName)

val detailsDeleteCandidates = LinkedList(generalMsgDetailsBeforeUpdate)
detailsDeleteCandidates.removeAll(detailsAfterUpdate)
val msgsDeleteCandidates = LinkedList(msgsBeforeUpdate)
msgsDeleteCandidates.removeAll(detailsAfterUpdate)

val isInbox = FoldersManager.getFolderType(localFolder) === FoldersManager.FolderType.INBOX
if (!GeneralUtil.isAppForegrounded() && isInbox) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
for ((_, _, uid) in detailsDeleteCandidates) {
messagesNotificationManager!!.cancel(uid)
for (msgEntity in msgsDeleteCandidates) {
messagesNotificationManager?.cancel(msgEntity.uid.toInt())
}
} else {
if (!detailsDeleteCandidates.isEmpty()) {
if (!msgsDeleteCandidates.isEmpty()) {
val unseenMsgs = roomDatabase.msgDao().getUIDOfUnseenMsgs(account.email, folderName)
messagesNotificationManager?.notify(this, account, localFolder, detailsAfterUpdate,
unseenMsgs, true)
Expand Down Expand Up @@ -198,7 +195,6 @@ class SyncJobService : JobService(), SyncListener {
val context = applicationContext
val isEncryptedModeEnabled = AccountDaoSource().isEncryptedModeEnabled(context, account.email)

val msgDaoSource = MessageDaoSource()
val roomDatabase = FlowCryptRoomDatabase.getDatabase(applicationContext)

val folderName = localFolder.fullName
Expand Down Expand Up @@ -226,8 +222,7 @@ class SyncJobService : JobService(), SyncListener {
return
}

val newMsgsList = msgDaoSource.getNewMsgs(applicationContext,
account.email, folderName)
val newMsgsList = roomDatabase.msgDao().getNewMsgs(account.email, folderName)
val unseenUIDs = roomDatabase.msgDao().getUIDOfUnseenMsgs(account.email, folderName)

messagesNotificationManager?.notify(this, account, localFolder, newMsgsList, unseenUIDs,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import com.flowcrypt.email.database.dao.source.AccountDao
import com.flowcrypt.email.database.dao.source.AccountDaoSource
import com.flowcrypt.email.database.dao.source.imap.AttachmentDaoSource
import com.flowcrypt.email.database.dao.source.imap.ImapLabelsDaoSource
import com.flowcrypt.email.database.dao.source.imap.MessageDaoSource
import com.flowcrypt.email.database.entity.MessageEntity
import com.flowcrypt.email.model.EmailAndNamePair
import com.flowcrypt.email.ui.activity.SearchMessagesActivity
Expand Down Expand Up @@ -288,7 +287,6 @@ class EmailSyncService : BaseService(), SyncListener {
val email = account.email
val folderName = localFolder.fullName

val msgDaoSource = MessageDaoSource()
val folderType = FoldersManager.getFolderType(localFolder)
val isNew = !GeneralUtil.isAppForegrounded() && folderType === FoldersManager.FolderType.INBOX

Expand All @@ -313,7 +311,7 @@ class EmailSyncService : BaseService(), SyncListener {
}

if (!GeneralUtil.isAppForegrounded()) {
val detailsList = msgDaoSource.getNewMsgs(this, email, folderName)
val detailsList = roomDatabase.msgDao().getNewMsgs(email, folderName)
val uidListOfUnseenMsgs = roomDatabase.msgDao().getUIDOfUnseenMsgs(email, folderName)
notificationManager.notify(this, account, localFolder, detailsList, uidListOfUnseenMsgs, false)
}
Expand Down Expand Up @@ -379,7 +377,6 @@ class EmailSyncService : BaseService(), SyncListener {
val folderName = localFolder.fullName

try {
val msgsDaoSource = MessageDaoSource()
val roomDatabase = FlowCryptRoomDatabase.getDatabase(applicationContext)

val mapOfUIDAndMsgFlags = roomDatabase.msgDao().getMapOfUIDAndMsgFlags(email, folderName)
Expand All @@ -395,7 +392,7 @@ class EmailSyncService : BaseService(), SyncListener {
notificationManager.cancel(uid.toInt())
}
} else {
val detailsList = msgsDaoSource.getNewMsgs(this, email, folderName)
val detailsList = roomDatabase.msgDao().getNewMsgs(email, folderName)
val uidListOfUnseenMsgs = roomDatabase.msgDao().getUIDOfUnseenMsgs(email, folderName)
notificationManager.notify(this, account, localFolder, detailsList, uidListOfUnseenMsgs, false)
}
Expand Down Expand Up @@ -455,7 +452,7 @@ class EmailSyncService : BaseService(), SyncListener {
}

override fun onFoldersInfoReceived(account: AccountDao, folders: Array<Folder>, ownerKey: String, requestCode: Int) {
LogsUtil.d(TAG, "onFoldersInfoReceived:" + Arrays.toString(folders))
LogsUtil.d(TAG, "onFoldersInfoReceived:" + folders.contentToString())
val email = account.email

val foldersManager = FoldersManager()
Expand Down Expand Up @@ -541,11 +538,10 @@ class EmailSyncService : BaseService(), SyncListener {
}

} else {
val msgDaoSource = MessageDaoSource()
val roomDatabase = FlowCryptRoomDatabase.getDatabase(applicationContext)
val detailsList = msgDaoSource.getNewMsgs(this, email, folderName)
val newMsgs = roomDatabase.msgDao().getNewMsgs(email, folderName)
val uidListOfUnseenMsgs = roomDatabase.msgDao().getUIDOfUnseenMsgs(email, folderName)
notificationManager.notify(this, account, localFolder, detailsList, uidListOfUnseenMsgs, true)
notificationManager.notify(this, account, localFolder, newMsgs, uidListOfUnseenMsgs, true)
}
}
}
Expand All @@ -557,10 +553,9 @@ class EmailSyncService : BaseService(), SyncListener {
val folderType = FoldersManager.getFolderType(localFolder)

if (folderType === FoldersManager.FolderType.INBOX && !GeneralUtil.isAppForegrounded()) {
val msgDaoSource = MessageDaoSource()
val roomDatabase = FlowCryptRoomDatabase.getDatabase(applicationContext)

val detailsList = msgDaoSource.getNewMsgs(this, email, folderName)
val detailsList = roomDatabase.msgDao().getNewMsgs(email, folderName)
val uidListOfUnseenMsgs = roomDatabase.msgDao().getUIDOfUnseenMsgs(email, folderName)

notificationManager.notify(this, account, localFolder, detailsList, uidListOfUnseenMsgs, false)
Expand Down Expand Up @@ -623,7 +618,7 @@ class EmailSyncService : BaseService(), SyncListener {
*/
private fun updateLocalContactsIfNeeded(imapFolder: IMAPFolder, messages: Array<javax.mail.Message>) {
try {
val isSentFolder = Arrays.asList(*imapFolder.attributes).contains("\\Sent")
val isSentFolder = listOf(*imapFolder.attributes).contains("\\Sent")

if (isSentFolder) {
val emailAndNamePairs = ArrayList<EmailAndNamePair>()
Expand Down
Loading

0 comments on commit 1b77a49

Please sign in to comment.