Remove BaseNotifications class

This commit is contained in:
cketti 2021-10-11 20:10:31 +02:00
parent b6c8edc70c
commit 909904fd73
4 changed files with 50 additions and 201 deletions

View file

@ -1,60 +0,0 @@
package com.fsck.k9.notification
import android.content.Context
import androidx.core.app.NotificationCompat
import com.fsck.k9.Account
import com.fsck.k9.K9
import com.fsck.k9.K9.NotificationQuickDelete
internal abstract class BaseNotifications(
protected val notificationHelper: NotificationHelper,
protected val actionCreator: NotificationActionCreator,
protected val resourceProvider: NotificationResourceProvider
) {
protected val context: Context = notificationHelper.getContext()
fun createBigTextStyleNotification(
account: Account,
holder: NotificationHolder,
notificationId: Int
): NotificationCompat.Builder {
val accountName = notificationHelper.getAccountName(account)
val content = holder.content
val groupKey = NotificationGroupKeys.getGroupKey(account)
val builder = createAndInitializeNotificationBuilder(account)
.setTicker(content.summary)
.setGroup(groupKey)
.setContentTitle(content.sender)
.setContentText(content.subject)
.setSubText(accountName)
val style = createBigTextStyle(builder)
style.bigText(content.preview)
builder.setStyle(style)
val contentIntent = actionCreator.createViewMessagePendingIntent(content.messageReference, notificationId)
builder.setContentIntent(contentIntent)
return builder
}
fun createAndInitializeNotificationBuilder(account: Account): NotificationCompat.Builder {
return notificationHelper.createNotificationBuilder(account, NotificationChannelManager.ChannelType.MESSAGES)
.setSmallIcon(resourceProvider.iconNewMail)
.setColor(account.chipColor)
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setCategory(NotificationCompat.CATEGORY_EMAIL)
}
fun isDeleteActionEnabled(): Boolean {
val deleteOption = K9.notificationQuickDeleteBehaviour
return deleteOption === NotificationQuickDelete.ALWAYS ||
deleteOption === NotificationQuickDelete.FOR_SINGLE_MSG
}
protected open fun createBigTextStyle(builder: NotificationCompat.Builder?): NotificationCompat.BigTextStyle {
return NotificationCompat.BigTextStyle(builder)
}
}

View file

@ -5,16 +5,17 @@ import androidx.core.app.NotificationCompat
import com.fsck.k9.Account
import com.fsck.k9.K9
import com.fsck.k9.K9.NotificationQuickDelete
import com.fsck.k9.notification.NotificationChannelManager.ChannelType
import com.fsck.k9.notification.NotificationGroupKeys.getGroupKey
import com.fsck.k9.notification.NotificationIds.getNewMailSummaryNotificationId
internal open class MessageSummaryNotifications(
notificationHelper: NotificationHelper,
actionCreator: NotificationActionCreator,
private val notificationHelper: NotificationHelper,
private val actionCreator: NotificationActionCreator,
private val lockScreenNotification: LockScreenNotification,
private val singleMessageNotifications: SingleMessageNotifications,
resourceProvider: NotificationResourceProvider
) : BaseNotifications(notificationHelper, actionCreator, resourceProvider) {
private val resourceProvider: NotificationResourceProvider
) {
fun buildSummaryNotification(account: Account, notificationData: NotificationData, silent: Boolean): Notification {
val builder = when {
@ -76,7 +77,12 @@ internal open class MessageSummaryNotifications(
}
val groupKey = getGroupKey(account)
val builder = createAndInitializeNotificationBuilder(account)
val builder = notificationHelper.createNotificationBuilder(account, ChannelType.MESSAGES)
.setSmallIcon(resourceProvider.iconNewMail)
.setColor(account.chipColor)
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setCategory(NotificationCompat.CATEGORY_EMAIL)
.setNumber(newMessagesCount)
.setTicker(latestNotification.content.summary)
.setGroup(groupKey)
@ -197,6 +203,10 @@ internal open class MessageSummaryNotifications(
return isDeleteActionEnabled() && !K9.isConfirmDeleteFromNotification
}
private fun isDeleteActionEnabled(): Boolean {
return K9.notificationQuickDeleteBehaviour != NotificationQuickDelete.NEVER
}
private fun isArchiveActionAvailableForWear(account: Account): Boolean {
return account.archiveFolderId != null
}

View file

@ -4,13 +4,14 @@ import android.app.Notification
import androidx.core.app.NotificationCompat
import com.fsck.k9.Account
import com.fsck.k9.K9
import com.fsck.k9.notification.NotificationChannelManager.ChannelType
internal open class SingleMessageNotifications(
notificationHelper: NotificationHelper,
actionCreator: NotificationActionCreator,
resourceProvider: NotificationResourceProvider,
private val notificationHelper: NotificationHelper,
private val actionCreator: NotificationActionCreator,
private val resourceProvider: NotificationResourceProvider,
private val lockScreenNotification: LockScreenNotification,
) : BaseNotifications(notificationHelper, actionCreator, resourceProvider) {
) {
fun buildSingleMessageNotification(account: Account, holder: NotificationHolder): Notification {
val notificationId = holder.notificationId
@ -38,11 +39,31 @@ internal open class SingleMessageNotifications(
holder: NotificationHolder,
notificationId: Int
): NotificationCompat.Builder {
val accountName = notificationHelper.getAccountName(account)
val content = holder.content
val builder = createBigTextStyleNotification(account, holder, notificationId)
val groupKey = NotificationGroupKeys.getGroupKey(account)
val builder = notificationHelper.createNotificationBuilder(account, ChannelType.MESSAGES)
.setSmallIcon(resourceProvider.iconNewMail)
.setColor(account.chipColor)
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setCategory(NotificationCompat.CATEGORY_EMAIL)
.setTicker(content.summary)
.setGroup(groupKey)
.setContentTitle(content.sender)
.setContentText(content.subject)
.setSubText(accountName)
val style = createBigTextStyle(builder)
style.bigText(content.preview)
builder.setStyle(style)
val contentIntent = actionCreator.createViewMessagePendingIntent(content.messageReference, notificationId)
builder.setContentIntent(contentIntent)
val deletePendingIntent = actionCreator.createDismissMessagePendingIntent(
context, content.messageReference, holder.notificationId
notificationHelper.getContext(), content.messageReference, holder.notificationId
)
builder.setDeleteIntent(deletePendingIntent)
@ -179,6 +200,10 @@ internal open class SingleMessageNotifications(
return isDeleteActionEnabled() && !K9.isConfirmDeleteFromNotification
}
private fun isDeleteActionEnabled(): Boolean {
return K9.notificationQuickDeleteBehaviour != K9.NotificationQuickDelete.NEVER
}
private fun isArchiveActionAvailableForWear(account: Account): Boolean {
return account.archiveFolderId != null
}
@ -186,4 +211,8 @@ internal open class SingleMessageNotifications(
private fun isSpamActionAvailableForWear(account: Account): Boolean {
return account.spamFolderId != null && !K9.isConfirmSpam
}
protected open fun createBigTextStyle(builder: NotificationCompat.Builder?): NotificationCompat.BigTextStyle {
return NotificationCompat.BigTextStyle(builder)
}
}

View file

@ -1,130 +0,0 @@
package com.fsck.k9.notification
import androidx.core.app.NotificationCompat
import com.fsck.k9.Account
import com.fsck.k9.K9
import com.fsck.k9.K9.NotificationQuickDelete
import com.fsck.k9.controller.MessageReference
import com.fsck.k9.testing.MockHelper.mockBuilder
import com.google.common.truth.Truth.assertThat
import org.junit.Test
import org.mockito.Mockito.verify
import org.mockito.kotlin.any
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.mock
private const val ACCOUNT_COLOR = 0xAABBCC
private const val ACCOUNT_NAME = "AccountName"
private const val ACCOUNT_NUMBER = 2
private const val NOTIFICATION_SUMMARY = "Summary"
private const val SENDER = "MessageSender"
private const val SUBJECT = "Subject"
private const val NOTIFICATION_PREVIEW = "Preview"
class BaseNotificationsTest {
private val resourceProvider = TestNotificationResourceProvider()
private val notifications = createTestNotifications()
@Test
fun testCreateAndInitializeNotificationBuilder() {
val account = createFakeAccount()
val builder = notifications.createAndInitializeNotificationBuilder(account)
verify(builder).setSmallIcon(resourceProvider.iconNewMail)
verify(builder).color = ACCOUNT_COLOR
verify(builder).setAutoCancel(true)
}
@Test
fun testIsDeleteActionEnabled_NotificationQuickDelete_ALWAYS() {
K9.notificationQuickDeleteBehaviour = NotificationQuickDelete.ALWAYS
val result = notifications.isDeleteActionEnabled()
assertThat(result).isTrue()
}
@Test
fun testIsDeleteActionEnabled_NotificationQuickDelete_FOR_SINGLE_MSG() {
K9.notificationQuickDeleteBehaviour = NotificationQuickDelete.FOR_SINGLE_MSG
val result = notifications.isDeleteActionEnabled()
assertThat(result).isTrue()
}
@Test
fun testIsDeleteActionEnabled_NotificationQuickDelete_NEVER() {
K9.notificationQuickDeleteBehaviour = NotificationQuickDelete.NEVER
val result = notifications.isDeleteActionEnabled()
assertThat(result).isFalse()
}
@Test
fun testCreateBigTextStyleNotification() {
val account = createFakeAccount()
val notificationId = 23
val holder = createNotificationHolder(notificationId)
val builder = notifications.createBigTextStyleNotification(account, holder, notificationId)
verify(builder).setTicker(NOTIFICATION_SUMMARY)
verify(builder).setGroup("newMailNotifications-$ACCOUNT_NUMBER")
verify(builder).setContentTitle(SENDER)
verify(builder).setContentText(SUBJECT)
verify(builder).setSubText(ACCOUNT_NAME)
verify(notifications.bigTextStyle).bigText(NOTIFICATION_PREVIEW)
verify(builder).setStyle(notifications.bigTextStyle)
}
private fun createNotificationHolder(notificationId: Int): NotificationHolder {
return NotificationHolder(
notificationId = notificationId,
content = NotificationContent(
messageReference = MessageReference("irrelevant", 1, "irrelevant"),
sender = SENDER,
subject = SUBJECT,
preview = NOTIFICATION_PREVIEW,
summary = NOTIFICATION_SUMMARY,
isStarred = false
)
)
}
private fun createTestNotifications(): TestNotifications {
return TestNotifications(
notificationHelper = createFakeNotificationHelper(),
actionCreator = mock(),
resourceProvider = resourceProvider
)
}
private fun createFakeNotificationHelper(): NotificationHelper {
return mock {
on { createNotificationBuilder(any(), any()) } doReturn mockBuilder()
on { getAccountName(any()) } doReturn ACCOUNT_NAME
}
}
private fun createFakeAccount(): Account {
return mock {
on { accountNumber } doReturn ACCOUNT_NUMBER
on { chipColor } doReturn ACCOUNT_COLOR
}
}
internal class TestNotifications(
notificationHelper: NotificationHelper,
actionCreator: NotificationActionCreator,
resourceProvider: NotificationResourceProvider
) : BaseNotifications(notificationHelper, actionCreator, resourceProvider) {
val bigTextStyle = mock<NotificationCompat.BigTextStyle>()
override fun createBigTextStyle(builder: NotificationCompat.Builder?): NotificationCompat.BigTextStyle {
return bigTextStyle
}
}
}