Skip to content

Commit

Permalink
Merge pull request #393 from jorgeblacio/daniel_princess_wishes
Browse files Browse the repository at this point in the history
Fixed filter menu colors on dark theme and also the filtering now works as intended.
  • Loading branch information
danieltigse authored Jan 31, 2019
2 parents 4c4cde3 + 0d61adc commit 3b665fd
Show file tree
Hide file tree
Showing 13 changed files with 179 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -1,31 +1,24 @@
package com.criptext.mail.androidui.criptextnotification

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.graphics.Color
import android.media.RingtoneManager
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat
import android.text.Html
import com.criptext.mail.R
import com.criptext.mail.androidui.CriptextNotification
import com.criptext.mail.db.KeyValueStorage
import com.criptext.mail.push.PushData
import com.criptext.mail.push.services.LinkDeviceActionService
import com.criptext.mail.push.data.PushDataSource
import com.criptext.mail.push.services.NewMailActionService
import com.criptext.mail.scenes.mailbox.MailboxActivity
import com.criptext.mail.services.MessagingInstance
import com.criptext.mail.utils.DeviceUtils
import com.criptext.mail.utils.UIMessage
import com.criptext.mail.utils.Utility
import com.criptext.mail.utils.getLocalizedUIMessage
import com.criptext.mail.utils.*
import com.criptext.mail.api.Hosts


class NotificationNewMail(override val ctx: Context): CriptextNotification(ctx) {

Expand Down Expand Up @@ -82,23 +75,28 @@ class NotificationNewMail(override val ctx: Context): CriptextNotification(ctx)

val defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val preview = if(pushData.preview.length == 300) pushData.preview.plus("...") else pushData.preview
val pushHtmlBody = "<span style='color:black;'>${pushData.body}</span><br>$preview"
val pushHtmlBody = "<span style='color:black;'>${pushData.subject}</span><br>$preview"
val pushBody = if(showEmailPreview)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
Html.fromHtml(pushHtmlBody, Html.FROM_HTML_MODE_LEGACY)
else
Html.fromHtml(pushHtmlBody)
else
pushData.body
pushData.subject

val pushHtmlText = "<span style='color:black;'>${pushData.body}</span>"
val pushHtmlText = "<span style='color:black;'>${pushData.subject}</span>"
val pushText = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
Html.fromHtml(pushHtmlText, Html.FROM_HTML_MODE_LEGACY)
else
Html.fromHtml(pushHtmlText)

val largeIcon = Utility.getCroppedBitmap(pushData.senderImage) ?: Utility.getBitmapFromText(
pushData.name,
250,
250)

val builder = NotificationCompat.Builder(ctx, CHANNEL_ID_NEW_EMAIL)
.setContentTitle(pushData.title)
.setContentTitle(pushData.name)
.setContentText(pushText)
.setSubText(pushData.activeEmail)
.setAutoCancel(true)
Expand All @@ -111,10 +109,7 @@ class NotificationNewMail(override val ctx: Context): CriptextNotification(ctx)
.addAction(0, ctx.getString(R.string.push_trash), trashPendingIntent)
.addAction(0, ctx.getString(R.string.push_reply), replyPendingAction)
.setDeleteIntent(deletePendingIntent)
.setLargeIcon(Utility.getBitmapFromText(
pushData.title,
250,
250))
.setLargeIcon(largeIcon)
.setStyle(NotificationCompat.BigTextStyle().bigText(pushBody))


Expand Down
44 changes: 29 additions & 15 deletions src/main/kotlin/com/criptext/mail/db/MailboxLocalDB.kt
Original file line number Diff line number Diff line change
Expand Up @@ -248,22 +248,36 @@ interface MailboxLocalDB {
}.map {
it.id
}
var emails = if(startDate != null)
db.emailDao().getEmailThreadsFromMailboxLabel(
isTrashOrSpam = (selectedLabel in conditionalLabels),
startDate = startDate,
rejectedLabels = rejectedIdLabels,
selectedLabel = selectedLabel,
limit = limit )

else
db.emailDao().getInitialEmailThreadsFromMailboxLabel(
isTrashOrSpam = (selectedLabel in conditionalLabels),
rejectedLabels = rejectedIdLabels,
selectedLabel = selectedLabel,
limit = limit )
var emails = if(startDate != null) {
if(filterUnread)
db.emailDao().getEmailThreadsFromMailboxLabelFiltered(
isTrashOrSpam = (selectedLabel in conditionalLabels),
startDate = startDate,
rejectedLabels = rejectedIdLabels,
selectedLabel = selectedLabel,
limit = limit)
else
db.emailDao().getEmailThreadsFromMailboxLabel(
isTrashOrSpam = (selectedLabel in conditionalLabels),
startDate = startDate,
rejectedLabels = rejectedIdLabels,
selectedLabel = selectedLabel,
limit = limit)
} else {
if (filterUnread)
db.emailDao().getInitialEmailThreadsFromMailboxLabelFiltered(
isTrashOrSpam = (selectedLabel in conditionalLabels),
rejectedLabels = rejectedIdLabels,
selectedLabel = selectedLabel,
limit = limit)
else
db.emailDao().getInitialEmailThreadsFromMailboxLabel(
isTrashOrSpam = (selectedLabel in conditionalLabels),
rejectedLabels = rejectedIdLabels,
selectedLabel = selectedLabel,
limit = limit)
}

emails = if(filterUnread) emails.filter { it.unread } else emails
emails = emails.map { it.copy(content = EmailUtils.getEmailContentFromFileSystem(
filesDir, it.metadataKey, it.content,
db.accountDao().getLoggedInAccount()!!.recipientId).first) }
Expand Down
44 changes: 44 additions & 0 deletions src/main/kotlin/com/criptext/mail/db/dao/EmailDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,29 @@ import java.util.*
selectedLabel: String,
limit: Int ): List<Email>

@Query("""
select email.*,CASE WHEN email.threadId = "" THEN email.id ELSE email.threadId END as uniqueId,
group_concat(email_label.labelId) as allLabels,
max(email.unread) as unread, max(email.date)
from email
left join email_label on email.id = email_label.emailId
and date < :startDate
where unread = 1 AND case when :isTrashOrSpam
then email_label.labelId = (select id from label where label.id= cast(trim(:selectedLabel, '%') as integer))
else not exists
(select * from email_label where email_label.emailId = email.id and email_label.labelId in (:rejectedLabels))
end
group by uniqueId
having coalesce(allLabels, "") like :selectedLabel
order by date DESC limit :limit
""")
fun getEmailThreadsFromMailboxLabelFiltered(
isTrashOrSpam: Boolean,
startDate: Date,
rejectedLabels: List<Long>,
selectedLabel: String,
limit: Int ): List<Email>

@Query("""
select email.*,CASE WHEN email.threadId = "" THEN email.id ELSE email.threadId END as uniqueId,
group_concat(email_label.labelId) as allLabels,
Expand Down Expand Up @@ -316,6 +339,27 @@ import java.util.*
selectedLabel: String,
limit: Int): List<Email>

@Query("""
select email.*,CASE WHEN email.threadId = "" THEN email.id ELSE email.threadId END as uniqueId,
group_concat(email_label.labelId) as allLabels,
max(email.unread) as unread, max(email.date)
from email
left join email_label on email.id = email_label.emailId
where unread = 1 AND case when :isTrashOrSpam
then email_label.labelId = (select id from label where label.id= cast(trim(:selectedLabel, '%') as integer))
else not exists
(select * from email_label where email_label.emailId = email.id and email_label.labelId in (:rejectedLabels))
end
group by uniqueId
having coalesce(allLabels, "") like :selectedLabel
order by date DESC limit :limit
""")
fun getInitialEmailThreadsFromMailboxLabelFiltered(
isTrashOrSpam: Boolean,
rejectedLabels: List<Long>,
selectedLabel: String,
limit: Int): List<Email>

@Query("""
select email.*,CASE WHEN email.threadId = "" THEN email.id ELSE email.threadId END as uniqueId,
max(email.unread) as unread, max(email.date),
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/com/criptext/mail/push/NewMailNotifier.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ sealed class NewMailNotifier(val data: PushData.NewMail): Notifier {
val pendingIntent = ActivityIntentFactory.buildSceneActivityPendingIntent(ctx, PushTypes.openActivity,
data.threadId, data.isPostNougat)
val cn = NotificationNewMail(ctx)
cn.showHeaderNotification(data.title, R.drawable.push_icon,
cn.showHeaderNotification(data.name, R.drawable.push_icon,
CriptextNotification.ACTION_INBOX, pendingIntent)
}

Expand Down
33 changes: 18 additions & 15 deletions src/main/kotlin/com/criptext/mail/push/PushController.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.criptext.mail.push

import android.graphics.Bitmap
import com.criptext.mail.R
import com.criptext.mail.db.models.ActiveAccount
import com.criptext.mail.db.models.Label
Expand Down Expand Up @@ -37,22 +38,24 @@ class PushController(private val dataSource: PushDataSource, private val host: M


private fun parseNewMailPush(pushData: Map<String, String>,
shouldPostNotification: Boolean): PushData.NewMail {
val body = pushData["body"] ?: ""
val title = pushData["title"] ?: ""
shouldPostNotification: Boolean, senderImage: Bitmap?): PushData.NewMail {
val subject = pushData["subject"] ?: ""
val name = pushData["name"] ?: ""
val email = pushData["email"] ?: ""
val threadId = pushData["threadId"] ?: ""
val metadataKey = pushData["metadataKey"]?.toLong()
val preview = pushData["preview"] ?: ""

return PushData.NewMail(title = title, body = body, threadId = threadId,
return PushData.NewMail(name = name, email = email, subject = subject, threadId = threadId,
metadataKey = metadataKey ?: -1, shouldPostNotification = shouldPostNotification,
isPostNougat = isPostNougat, preview = preview, activeEmail = activeAccount.userEmail)
isPostNougat = isPostNougat, preview = preview, activeEmail = activeAccount.userEmail,
senderImage = senderImage)
}

private fun parseNewOpenMailbox(pushData: Map<String, String>,
shouldPostNotification: Boolean): PushData.OpenMailbox {
val body = pushData["body"] ?: ""
val title = pushData["title"] ?: ""
val body = pushData["subject"] ?: ""
val title = pushData["name"] ?: ""

return PushData.OpenMailbox(title = title, body = body,
shouldPostNotification = shouldPostNotification,
Expand All @@ -61,8 +64,8 @@ class PushController(private val dataSource: PushDataSource, private val host: M

private fun parseLinkDevicePush(pushData: Map<String, String>,
shouldPostNotification: Boolean): PushData.LinkDevice {
val body = pushData["body"] ?: ""
val title = pushData["title"] ?: ""
val body = pushData["subject"] ?: ""
val title = pushData["name"] ?: ""
val deviceId = pushData["randomId"] ?: ""
val deviceType = pushData["deviceType"] ?: ""
val deviceName = pushData["deviceName"] ?: ""
Expand All @@ -76,8 +79,8 @@ class PushController(private val dataSource: PushDataSource, private val host: M

private fun parseSyncDevicePush(pushData: Map<String, String>,
shouldPostNotification: Boolean): PushData.SyncDevice {
val body = pushData["body"] ?: ""
val title = pushData["title"] ?: ""
val body = pushData["subject"] ?: ""
val title = pushData["name"] ?: ""
val randomId = pushData["randomId"] ?: ""
val deviceId = pushData["deviceId"] ?: ""
val deviceType = pushData["deviceType"] ?: ""
Expand All @@ -98,14 +101,14 @@ class PushController(private val dataSource: PushDataSource, private val host: M
}

private fun createAndNotifyPush(pushData: Map<String, String>, shouldPostNotification: Boolean,
isSuccess: Boolean){
isSuccess: Boolean, senderImage: Bitmap?){
val action = pushData["action"]
if (action != null) {
val type = PushTypes.fromActionString(action)
val notifier = when (type) {
PushTypes.newMail -> {
if(isSuccess) {
val data = parseNewMailPush(pushData, shouldPostNotification)
val data = parseNewMailPush(pushData, shouldPostNotification, senderImage)
NewMailNotifier.Single(data)
}else{
val data = PushData.Error(UIMessage(R.string.push_email_update_mailbox_title),
Expand Down Expand Up @@ -134,13 +137,13 @@ class PushController(private val dataSource: PushDataSource, private val host: M
private fun onUpdateMailbox(result: PushResult.UpdateMailbox){
when(result){
is PushResult.UpdateMailbox.Success -> {
createAndNotifyPush(result.pushData, result.shouldPostNotification, true)
createAndNotifyPush(result.pushData, result.shouldPostNotification, true, result.senderImage)
}
is PushResult.UpdateMailbox.SuccessAndRepeat -> {
parsePushPayload(result.pushData, result.shouldPostNotification)
}
is PushResult.UpdateMailbox.Failure -> {
createAndNotifyPush(result.pushData, result.shouldPostNotification, false)
createAndNotifyPush(result.pushData, result.shouldPostNotification, false, null)
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/kotlin/com/criptext/mail/push/PushData.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.criptext.mail.push

import android.graphics.Bitmap
import com.criptext.mail.utils.DeviceUtils
import com.criptext.mail.utils.UIMessage

Expand All @@ -13,9 +14,9 @@ sealed class PushData {
/**
* POJO that holds all the data from the NewMail push notification
*/
data class NewMail(val title: String, val body: String, val threadId: String,
data class NewMail(val name: String, val email: String, val subject: String, val threadId: String,
val metadataKey: Long, val isPostNougat: Boolean, val preview: String,
val shouldPostNotification:Boolean, val activeEmail: String): PushData()
val shouldPostNotification:Boolean, val activeEmail: String, val senderImage: Bitmap?): PushData()
data class OpenMailbox(val title: String, val body: String,
val isPostNougat: Boolean, val shouldPostNotification:Boolean): PushData()

Expand Down
7 changes: 5 additions & 2 deletions src/main/kotlin/com/criptext/mail/push/data/PushResult.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.criptext.mail.push.data

import android.graphics.Bitmap
import com.criptext.mail.db.models.Label
import com.criptext.mail.email_preview.EmailPreview
import com.criptext.mail.utils.UIMessage
Expand All @@ -14,7 +15,8 @@ sealed class PushResult {
val mailboxThreads: List<EmailPreview>?,
val isManual: Boolean,
val pushData: Map<String, String>,
val shouldPostNotification: Boolean): UpdateMailbox() {
val shouldPostNotification: Boolean,
val senderImage: Bitmap?): UpdateMailbox() {

override fun getDestinationMailbox(): Label {
return mailboxLabel
Expand All @@ -26,7 +28,8 @@ sealed class PushResult {
val mailboxThreads: List<EmailPreview>?,
val isManual: Boolean,
val pushData: Map<String, String>,
val shouldPostNotification: Boolean): UpdateMailbox() {
val shouldPostNotification: Boolean,
val senderImage: Bitmap?): UpdateMailbox() {

override fun getDestinationMailbox(): Label {
return mailboxLabel
Expand Down
25 changes: 20 additions & 5 deletions src/main/kotlin/com/criptext/mail/push/data/UpdateMailboxWorker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ import com.github.kittinunf.result.Result
import com.github.kittinunf.result.flatMap
import org.whispersystems.libsignal.DuplicateMessageException
import java.io.File
import com.squareup.picasso.Picasso
import android.graphics.Bitmap
import com.criptext.mail.api.Hosts
import com.criptext.mail.utils.EmailAddressUtils


/**
* Created by sebas on 3/22/18.
Expand Down Expand Up @@ -60,7 +65,8 @@ class UpdateMailboxWorker(
isManual = true,
mailboxThreads = null,
shouldPostNotification = shouldPostNotification,
pushData = pushData)
pushData = pushData,
senderImage = null)
else
PushResult.UpdateMailbox.Failure(
mailboxLabel = label,
Expand Down Expand Up @@ -89,24 +95,33 @@ class UpdateMailboxWorker(
val email = dbEvents.getEmailByMetadataKey(metadataKey)
if(email != null){
newData["preview"] = email.preview
newData["body"] = email.subject
newData["title"] = dbEvents.getFromContactByEmailId(email.id)[0].name
newData["subject"] = email.subject
newData["name"] = dbEvents.getFromContactByEmailId(email.id)[0].name
newData["email"] = dbEvents.getFromContactByEmailId(email.id)[0].email
val emailAddress = newData["email"]
val bm = if(emailAddress != null && EmailAddressUtils.isFromCriptextDomain(emailAddress))
Picasso.get().load(Hosts.restApiBaseUrl
.plus("/user/avatar/${EmailAddressUtils.extractRecipientIdFromCriptextAddress(emailAddress)}")).get()
else
null

if(shouldCallAgain) {
PushResult.UpdateMailbox.SuccessAndRepeat(
mailboxLabel = label,
isManual = true,
mailboxThreads = operationResult.value.first,
pushData = newData,
shouldPostNotification = shouldPostNotification
shouldPostNotification = shouldPostNotification,
senderImage = bm
)
}else{
PushResult.UpdateMailbox.Success(
mailboxLabel = label,
isManual = true,
mailboxThreads = operationResult.value.first,
pushData = newData,
shouldPostNotification = shouldPostNotification
shouldPostNotification = shouldPostNotification,
senderImage = bm
)
}
}else{
Expand Down
Loading

0 comments on commit 3b665fd

Please sign in to comment.