From 2bb7f8df6857c515b052992615abb3df9c754a6c Mon Sep 17 00:00:00 2001 From: Jorge Blacio <34110092+jorgeblacio@users.noreply.github.com> Date: Tue, 5 Feb 2019 11:31:25 -0500 Subject: [PATCH] Prep for release, also fixed a small error with notifiation sound on pull down. (#397) --- build.gradle | 4 +-- .../NotificationOpenMailbox.kt | 4 --- .../com/criptext/mail/push/PushController.kt | 12 ++++---- .../mail/push/data/UpdateMailboxWorker.kt | 11 +++----- .../mail/scenes/mailbox/MailboxScene.kt | 21 +++++--------- .../scenes/mailbox/MailboxSceneController.kt | 5 ++-- .../com/criptext/mail/utils/EventHelper.kt | 23 ++++++++++----- .../mail/utils/EventHelperResultData.kt | 8 ++++++ .../generaldatasource/data/GeneralResult.kt | 2 ++ .../workers/UpdateMailboxWorker.kt | 28 +++++++++---------- .../MailboxControllerDataSourceEventsTest.kt | 9 ++++-- 11 files changed, 68 insertions(+), 59 deletions(-) create mode 100644 src/main/kotlin/com/criptext/mail/utils/EventHelperResultData.kt diff --git a/build.gradle b/build.gradle index 0c64ad26c..ecaaa7413 100644 --- a/build.gradle +++ b/build.gradle @@ -45,8 +45,8 @@ android { defaultConfig { minSdkVersion 21 targetSdkVersion 28 - versionCode 46 - versionName "0.16.4" + versionCode 47 + versionName "0.16.5" applicationId "com.criptext.mail" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" multiDexEnabled true diff --git a/src/main/kotlin/com/criptext/mail/androidui/criptextnotification/NotificationOpenMailbox.kt b/src/main/kotlin/com/criptext/mail/androidui/criptextnotification/NotificationOpenMailbox.kt index c5fbd245d..a5f853760 100644 --- a/src/main/kotlin/com/criptext/mail/androidui/criptextnotification/NotificationOpenMailbox.kt +++ b/src/main/kotlin/com/criptext/mail/androidui/criptextnotification/NotificationOpenMailbox.kt @@ -54,10 +54,6 @@ class NotificationOpenMailbox(override val ctx: Context): CriptextNotification(c .setGroupSummary(false) .setSmallIcon(R.drawable.push_icon) .setColor(Color.CYAN) - .setLargeIcon(Utility.getBitmapFromText( - pushData.title, - 250, - 250)) .setStyle(NotificationCompat.BigTextStyle().bigText(pushData.body)) return buildNotification(builder) diff --git a/src/main/kotlin/com/criptext/mail/push/PushController.kt b/src/main/kotlin/com/criptext/mail/push/PushController.kt index 0d20feb0e..7e4855c82 100644 --- a/src/main/kotlin/com/criptext/mail/push/PushController.kt +++ b/src/main/kotlin/com/criptext/mail/push/PushController.kt @@ -54,8 +54,8 @@ class PushController(private val dataSource: PushDataSource, private val host: M private fun parseNewOpenMailbox(pushData: Map, shouldPostNotification: Boolean): PushData.OpenMailbox { - val body = pushData["subject"] ?: "" - val title = pushData["name"] ?: "" + val body = pushData["body"] ?: "" + val title = pushData["title"] ?: "" return PushData.OpenMailbox(title = title, body = body, shouldPostNotification = shouldPostNotification, @@ -64,8 +64,8 @@ class PushController(private val dataSource: PushDataSource, private val host: M private fun parseLinkDevicePush(pushData: Map, shouldPostNotification: Boolean): PushData.LinkDevice { - val body = pushData["subject"] ?: "" - val title = pushData["name"] ?: "" + val body = pushData["body"] ?: "" + val title = pushData["title"] ?: "" val deviceId = pushData["randomId"] ?: "" val deviceType = pushData["deviceType"] ?: "" val deviceName = pushData["deviceName"] ?: "" @@ -79,8 +79,8 @@ class PushController(private val dataSource: PushDataSource, private val host: M private fun parseSyncDevicePush(pushData: Map, shouldPostNotification: Boolean): PushData.SyncDevice { - val body = pushData["subject"] ?: "" - val title = pushData["name"] ?: "" + val body = pushData["body"] ?: "" + val title = pushData["title"] ?: "" val randomId = pushData["randomId"] ?: "" val deviceId = pushData["deviceId"] ?: "" val deviceType = pushData["deviceType"] ?: "" diff --git a/src/main/kotlin/com/criptext/mail/push/data/UpdateMailboxWorker.kt b/src/main/kotlin/com/criptext/mail/push/data/UpdateMailboxWorker.kt index 568c46602..56d33c191 100644 --- a/src/main/kotlin/com/criptext/mail/push/data/UpdateMailboxWorker.kt +++ b/src/main/kotlin/com/criptext/mail/push/data/UpdateMailboxWorker.kt @@ -14,9 +14,6 @@ import com.criptext.mail.email_preview.EmailPreview import com.criptext.mail.scenes.mailbox.data.MailboxAPIClient import com.criptext.mail.scenes.mailbox.data.UpdateBannerData import com.criptext.mail.signal.SignalClient -import com.criptext.mail.utils.EventHelper -import com.criptext.mail.utils.EventLoader -import com.criptext.mail.utils.UIMessage import com.github.kittinunf.result.Result import com.github.kittinunf.result.flatMap import org.whispersystems.libsignal.DuplicateMessageException @@ -24,7 +21,7 @@ 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 +import com.criptext.mail.utils.* /** @@ -57,7 +54,7 @@ class UpdateMailboxWorker( return PushResult.UpdateMailbox.Failure(label, message, ex, pushData, shouldPostNotification) } - private fun processFailure(failure: Result.Failure, UpdateBannerData?, List>, + private fun processFailure(failure: Result.Failure): PushResult.UpdateMailbox { return if (failure.error is EventHelper.NothingNewException) PushResult.UpdateMailbox.Success( @@ -109,7 +106,7 @@ class UpdateMailboxWorker( PushResult.UpdateMailbox.SuccessAndRepeat( mailboxLabel = label, isManual = true, - mailboxThreads = operationResult.value.first, + mailboxThreads = operationResult.value.emailPreviews, pushData = newData, shouldPostNotification = shouldPostNotification, senderImage = bm @@ -118,7 +115,7 @@ class UpdateMailboxWorker( PushResult.UpdateMailbox.Success( mailboxLabel = label, isManual = true, - mailboxThreads = operationResult.value.first, + mailboxThreads = operationResult.value.emailPreviews, pushData = newData, shouldPostNotification = shouldPostNotification, senderImage = bm diff --git a/src/main/kotlin/com/criptext/mail/scenes/mailbox/MailboxScene.kt b/src/main/kotlin/com/criptext/mail/scenes/mailbox/MailboxScene.kt index c05167f01..2a9941903 100644 --- a/src/main/kotlin/com/criptext/mail/scenes/mailbox/MailboxScene.kt +++ b/src/main/kotlin/com/criptext/mail/scenes/mailbox/MailboxScene.kt @@ -3,7 +3,9 @@ package com.criptext.mail.scenes.mailbox import android.content.Context import android.media.AudioManager import android.media.RingtoneManager +import android.os.Build import android.os.Handler +import android.os.VibrationEffect import android.os.Vibrator import android.view.Gravity import android.view.View @@ -514,22 +516,13 @@ interface MailboxScene{ } override fun showNotification() { - val am = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager? - - when (am!!.ringerMode) { - AudioManager.RINGER_MODE_VIBRATE -> setNotification(false) - AudioManager.RINGER_MODE_NORMAL -> setNotification(true) - } - } - - private fun setNotification(sound: Boolean){ val vibrate: Vibrator = context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator - vibrate.vibrate(400) - if(sound){ - val defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION) - val ringtone = RingtoneManager.getRingtone(context.applicationContext, defaultSound) - ringtone.play() + if (Build.VERSION.SDK_INT >= 26) { + vibrate.vibrate(VibrationEffect.createOneShot(400,10)) + } else { + vibrate.vibrate(400) } + } } } diff --git a/src/main/kotlin/com/criptext/mail/scenes/mailbox/MailboxSceneController.kt b/src/main/kotlin/com/criptext/mail/scenes/mailbox/MailboxSceneController.kt index 071b38a73..6eef4035f 100644 --- a/src/main/kotlin/com/criptext/mail/scenes/mailbox/MailboxSceneController.kt +++ b/src/main/kotlin/com/criptext/mail/scenes/mailbox/MailboxSceneController.kt @@ -915,7 +915,7 @@ class MailboxSceneController(private val scene: MailboxScene, feedController.reloadFeeds() dataSource.submitRequest(MailboxRequest.ResendEmails()) if(!model.threads.isEmpty()){ - if(model.selectedLabel.text== Label.LABEL_INBOX && model.threads[0].unread){ + if(resultData.shouldNotify){ scene.showNotification() } } @@ -1043,7 +1043,8 @@ class MailboxSceneController(private val scene: MailboxScene, syncEventsList = resultData.syncEventsList, updateBannerData = resultData.updateBannerData, isManual = resultData.isManual, - mailboxThreads = resultData.mailboxThreads + mailboxThreads = resultData.mailboxThreads, + shouldNotify = resultData.shouldNotify ) handleSuccessfulMailboxUpdate(success) dataSourceController.updateMailbox(model.selectedLabel) diff --git a/src/main/kotlin/com/criptext/mail/utils/EventHelper.kt b/src/main/kotlin/com/criptext/mail/utils/EventHelper.kt index 69226b06f..efac72fa9 100644 --- a/src/main/kotlin/com/criptext/mail/utils/EventHelper.kt +++ b/src/main/kotlin/com/criptext/mail/utils/EventHelper.kt @@ -47,13 +47,14 @@ class EventHelper(private val db: EventLocalDB, private var updateBannerData: UpdateBannerData? = null private val linkDevicesEvents: MutableList = mutableListOf() private var shouldCallAgain = false + private var shouldNotify = false fun setupForMailbox(label: Label, threadCount: Int?){ this.label = label loadedThreadsCount = threadCount } - val processEvents: (Pair, Boolean>) -> Result, UpdateBannerData?, List>, Exception> = { events -> + val processEvents: (Pair, Boolean>) -> Result = { events -> Result.of { val shouldReload = processLowPreKeys(events.first).or(processNewEmails(events.first)).or(processTrackingUpdates(events.first)) @@ -63,8 +64,8 @@ class EventHelper(private val db: EventLocalDB, .or(processThreadDeletedPermanently(events.first)).or(processLabelCreated(events.first)) .or(processOnError(events.first)).or(processEmailReadStatusChanged(events.first)).or(processUpdateBannerData(events.first)) .or(processLinkRequestEvents(events.first)).or(processSyncRequestEvents(events.first)).or(processProfilePicChangePeer(events.first)) - Triple(reloadMailbox(shouldReload.or(acknowledgeEventsIgnoringErrors(eventsToAcknowldege))), - updateBannerData, linkDevicesEvents) + EventHelperResultData(reloadMailbox(shouldReload.or(acknowledgeEventsIgnoringErrors(eventsToAcknowldege))), + updateBannerData, linkDevicesEvents, shouldNotify) } } @@ -85,6 +86,8 @@ class EventHelper(private val db: EventLocalDB, if (cache.exists() && cache.isDirectory) { FileUtils.deleteDir(cache) } + if (acknoledgeEvents) + eventsToAcknowldege.addAll(eventIdsToAcknowledge) } return eventIdsToAcknowledge.isNotEmpty() @@ -140,8 +143,10 @@ class EventHelper(private val db: EventLocalDB, val deviceInfo = eventIdsToAcknowledge.map { it.second } linkDevicesEvents.add(if(deviceInfo.isEmpty()) null - else - eventIdsToAcknowledge.map { it.second }.last() + else{ + shouldNotify = true + eventIdsToAcknowledge.map { it.second }.last() + } ) return eventIdsToAcknowledge.isNotEmpty() @@ -162,8 +167,10 @@ class EventHelper(private val db: EventLocalDB, val deviceInfo = eventIdsToAcknowledge.map { it.second } linkDevicesEvents.add(if(deviceInfo.isEmpty()) null - else - eventIdsToAcknowledge.map { it.second }.last() + else { + shouldNotify = true + eventIdsToAcknowledge.map { it.second }.last() + } ) return eventIdsToAcknowledge.isNotEmpty() @@ -180,6 +187,7 @@ class EventHelper(private val db: EventLocalDB, when(operation){ is Result.Success ->{ updateBannerDataList.add(operation.value) + shouldNotify = true true } else -> false @@ -210,6 +218,7 @@ class EventHelper(private val db: EventLocalDB, { (_, metadata) -> try { insertIncomingEmailTransaction(metadata) + shouldNotify = true // insertion success, try to acknowledge it true } catch (ex: DuplicateMessageException) { diff --git a/src/main/kotlin/com/criptext/mail/utils/EventHelperResultData.kt b/src/main/kotlin/com/criptext/mail/utils/EventHelperResultData.kt new file mode 100644 index 000000000..a542399f5 --- /dev/null +++ b/src/main/kotlin/com/criptext/mail/utils/EventHelperResultData.kt @@ -0,0 +1,8 @@ +package com.criptext.mail.utils + +import com.criptext.mail.api.models.DeviceInfo +import com.criptext.mail.email_preview.EmailPreview +import com.criptext.mail.scenes.mailbox.data.UpdateBannerData + +data class EventHelperResultData(val emailPreviews: List, val updateBannerData: UpdateBannerData?, + val deviceInfo: List, val shouldNotify: Boolean) \ No newline at end of file diff --git a/src/main/kotlin/com/criptext/mail/utils/generaldatasource/data/GeneralResult.kt b/src/main/kotlin/com/criptext/mail/utils/generaldatasource/data/GeneralResult.kt index 76194e839..ab788eca2 100644 --- a/src/main/kotlin/com/criptext/mail/utils/generaldatasource/data/GeneralResult.kt +++ b/src/main/kotlin/com/criptext/mail/utils/generaldatasource/data/GeneralResult.kt @@ -35,6 +35,7 @@ sealed class GeneralResult { sealed class UpdateMailbox : GeneralResult() { abstract fun getDestinationMailbox(): Label data class Success( + val shouldNotify: Boolean, val mailboxLabel: Label, val mailboxThreads: List?, val updateBannerData: UpdateBannerData?, @@ -47,6 +48,7 @@ sealed class GeneralResult { } data class SuccessAndRepeat( + val shouldNotify: Boolean, val mailboxLabel: Label, val mailboxThreads: List?, val updateBannerData: UpdateBannerData?, diff --git a/src/main/kotlin/com/criptext/mail/utils/generaldatasource/workers/UpdateMailboxWorker.kt b/src/main/kotlin/com/criptext/mail/utils/generaldatasource/workers/UpdateMailboxWorker.kt index ed1a75398..35be08b43 100644 --- a/src/main/kotlin/com/criptext/mail/utils/generaldatasource/workers/UpdateMailboxWorker.kt +++ b/src/main/kotlin/com/criptext/mail/utils/generaldatasource/workers/UpdateMailboxWorker.kt @@ -15,10 +15,7 @@ import com.criptext.mail.email_preview.EmailPreview import com.criptext.mail.scenes.mailbox.data.MailboxAPIClient import com.criptext.mail.scenes.mailbox.data.UpdateBannerData import com.criptext.mail.signal.SignalClient -import com.criptext.mail.utils.EventHelper -import com.criptext.mail.utils.EventLoader -import com.criptext.mail.utils.ServerCodes -import com.criptext.mail.utils.UIMessage +import com.criptext.mail.utils.* import com.criptext.mail.utils.file.FileUtils import com.criptext.mail.utils.generaldatasource.data.GeneralAPIClient import com.criptext.mail.utils.generaldatasource.data.GeneralResult @@ -68,14 +65,15 @@ class UpdateMailboxWorker( else GeneralResult.UpdateMailbox.Failure(label, createErrorMessage(ex), ex) - private fun processFailure(failure: Result.Failure, UpdateBannerData?, List>, Exception>): GeneralResult.UpdateMailbox { + private fun processFailure(failure: Result.Failure): GeneralResult.UpdateMailbox { return if (failure.error is EventHelper.NothingNewException) GeneralResult.UpdateMailbox.Success( mailboxLabel = label, isManual = true, mailboxThreads = null, updateBannerData = null, - syncEventsList = listOf()) + syncEventsList = listOf(), + shouldNotify = false) else catchException(failure.error) } @@ -103,17 +101,19 @@ class UpdateMailboxWorker( GeneralResult.UpdateMailbox.SuccessAndRepeat( mailboxLabel = label, isManual = true, - mailboxThreads = finalResult.value.first, - updateBannerData = finalResult.value.second, - syncEventsList = finalResult.value.third + mailboxThreads = finalResult.value.emailPreviews, + updateBannerData = finalResult.value.updateBannerData, + syncEventsList = finalResult.value.deviceInfo, + shouldNotify = finalResult.value.shouldNotify ) }else { GeneralResult.UpdateMailbox.Success( mailboxLabel = label, isManual = true, - mailboxThreads = finalResult.value.first, - updateBannerData = finalResult.value.second, - syncEventsList = finalResult.value.third + mailboxThreads = finalResult.value.emailPreviews, + updateBannerData = finalResult.value.updateBannerData, + syncEventsList = finalResult.value.deviceInfo, + shouldNotify = finalResult.value.shouldNotify ) } } @@ -126,7 +126,7 @@ class UpdateMailboxWorker( TODO("CANCEL IS NOT IMPLEMENTED") } - private fun workOperation() : Result, UpdateBannerData?, List>, Exception> { + private fun workOperation() : Result { val requestEvents = EventLoader.getEvents(mailboxApiClient) shouldCallAgain = (requestEvents as? Result.Success)?.value?.second ?: false return requestEvents @@ -134,7 +134,7 @@ class UpdateMailboxWorker( } private fun newRetryWithNewSessionOperation() - : Result, UpdateBannerData?, List>, Exception> { + : Result { val refreshOperation = HttpErrorHandlingHelper.newRefreshSessionOperation(apiClient, activeAccount, storage, accountDao) .mapError(HttpErrorHandlingHelper.httpExceptionsToNetworkExceptions) diff --git a/src/test/java/com/criptext/mail/scenes/mailbox/MailboxControllerDataSourceEventsTest.kt b/src/test/java/com/criptext/mail/scenes/mailbox/MailboxControllerDataSourceEventsTest.kt index fd740400e..c3d131a11 100644 --- a/src/test/java/com/criptext/mail/scenes/mailbox/MailboxControllerDataSourceEventsTest.kt +++ b/src/test/java/com/criptext/mail/scenes/mailbox/MailboxControllerDataSourceEventsTest.kt @@ -118,7 +118,8 @@ class MailboxControllerDataSourceEventsTest: MailboxControllerTest() { mailboxLabel = model.selectedLabel, isManual = false, updateBannerData = null, - syncEventsList = listOf())) + syncEventsList = listOf(), + shouldNotify = false)) model.threads.size `should be` 20 verify { virtualListView.notifyDataSetChanged() } @@ -137,7 +138,8 @@ class MailboxControllerDataSourceEventsTest: MailboxControllerTest() { generalListenerSlot.captured(GeneralResult.UpdateMailbox.Success(mailboxThreads = null, mailboxLabel = model.selectedLabel, isManual = false, updateBannerData = null, - syncEventsList = listOf())) + syncEventsList = listOf(), + shouldNotify = false)) model.threads.size `should be` 40 verify(inverse = true) { virtualListView.notifyDataSetChanged() } @@ -156,7 +158,8 @@ class MailboxControllerDataSourceEventsTest: MailboxControllerTest() { generalListenerSlot.captured(GeneralResult.UpdateMailbox.Success(mailboxThreads = null, mailboxLabel = model.selectedLabel, isManual = true, updateBannerData = null, - syncEventsList = listOf())) + syncEventsList = listOf(), + shouldNotify = false)) verify { scene.clearRefreshing() } }