Loading app/core/src/main/java/com/fsck/k9/notification/CoreKoinModule.kt +9 −0 Original line number Diff line number Diff line Loading @@ -114,4 +114,13 @@ val coreNotificationModule = module { ) } factory { NotificationLightDecoder() } factory { NotificationVibrationDecoder() } factory { NotificationConfigurationConverter(notificationLightDecoder = get(), notificationVibrationDecoder = get()) } factory { NotificationSettingsUpdater( preferences = get(), notificationChannelManager = get(), notificationConfigurationConverter = get() ) } } app/core/src/main/java/com/fsck/k9/notification/NotificationConfigurationConverter.kt 0 → 100644 +32 −0 Original line number Diff line number Diff line package com.fsck.k9.notification import com.fsck.k9.Account import com.fsck.k9.NotificationSettings /** * Converts the [NotificationConfiguration] read from a `NotificationChannel` into a [NotificationSettings] instance. */ class NotificationConfigurationConverter( private val notificationLightDecoder: NotificationLightDecoder, private val notificationVibrationDecoder: NotificationVibrationDecoder ) { fun convert(account: Account, notificationConfiguration: NotificationConfiguration): NotificationSettings { val light = notificationLightDecoder.decode( isBlinkLightsEnabled = notificationConfiguration.isBlinkLightsEnabled, lightColor = notificationConfiguration.lightColor, accountColor = account.chipColor ) val vibration = notificationVibrationDecoder.decode( isVibrationEnabled = notificationConfiguration.isVibrationEnabled, systemPattern = notificationConfiguration.vibrationPattern ) return NotificationSettings( isRingEnabled = notificationConfiguration.sound != null, ringtone = notificationConfiguration.sound?.toString(), light = light, vibration = vibration ) } } app/core/src/main/java/com/fsck/k9/notification/NotificationSettingsUpdater.kt 0 → 100644 +34 −0 Original line number Diff line number Diff line package com.fsck.k9.notification import android.os.Build import androidx.annotation.RequiresApi import com.fsck.k9.Account import com.fsck.k9.Preferences /** * Update accounts with notification settings read from their "Messages" `NotificationChannel`. */ class NotificationSettingsUpdater( private val preferences: Preferences, private val notificationChannelManager: NotificationChannelManager, private val notificationConfigurationConverter: NotificationConfigurationConverter ) { fun updateNotificationSettings(accountUuids: Collection<String>) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return accountUuids .mapNotNull { accountUuid -> preferences.getAccount(accountUuid) } .forEach { account -> updateNotificationSettings(account) } } @RequiresApi(Build.VERSION_CODES.O) private fun updateNotificationSettings(account: Account) { val notificationConfiguration = notificationChannelManager.getNotificationConfiguration(account) val notificationSettings = notificationConfigurationConverter.convert(account, notificationConfiguration) if (notificationSettings != account.notificationSettings) { account.updateNotificationSettings { notificationSettings } preferences.saveAccount(account) } } } app/core/src/main/java/com/fsck/k9/notification/NotificationVibrationDecoder.kt 0 → 100644 +26 −0 Original line number Diff line number Diff line package com.fsck.k9.notification import com.fsck.k9.NotificationVibration import com.fsck.k9.VibratePattern /** * Converts the vibration values read from a `NotificationChannel` into [NotificationVibration]. */ class NotificationVibrationDecoder { fun decode(isVibrationEnabled: Boolean, systemPattern: List<Long>?): NotificationVibration { if (systemPattern == null || systemPattern.size < 2 || systemPattern.size % 2 != 0) { return NotificationVibration.DEFAULT } val systemPatternArray = systemPattern.toLongArray() val repeatCount = systemPattern.size / 2 val pattern = VibratePattern.values() .firstOrNull { vibratePattern -> val testPattern = NotificationVibration.getSystemPattern(vibratePattern, repeatCount) testPattern.contentEquals(systemPatternArray) } ?: VibratePattern.Default return NotificationVibration(isVibrationEnabled, pattern, repeatCount) } } app/core/src/main/java/com/fsck/k9/preferences/KoinModule.kt +2 −1 Original line number Diff line number Diff line Loading @@ -11,7 +11,8 @@ val preferencesModule = module { contentResolver = get(), preferences = get(), folderSettingsProvider = get(), folderRepository = get() folderRepository = get(), notificationSettingsUpdater = get() ) } factory { FolderSettingsProvider(folderRepository = get()) } Loading Loading
app/core/src/main/java/com/fsck/k9/notification/CoreKoinModule.kt +9 −0 Original line number Diff line number Diff line Loading @@ -114,4 +114,13 @@ val coreNotificationModule = module { ) } factory { NotificationLightDecoder() } factory { NotificationVibrationDecoder() } factory { NotificationConfigurationConverter(notificationLightDecoder = get(), notificationVibrationDecoder = get()) } factory { NotificationSettingsUpdater( preferences = get(), notificationChannelManager = get(), notificationConfigurationConverter = get() ) } }
app/core/src/main/java/com/fsck/k9/notification/NotificationConfigurationConverter.kt 0 → 100644 +32 −0 Original line number Diff line number Diff line package com.fsck.k9.notification import com.fsck.k9.Account import com.fsck.k9.NotificationSettings /** * Converts the [NotificationConfiguration] read from a `NotificationChannel` into a [NotificationSettings] instance. */ class NotificationConfigurationConverter( private val notificationLightDecoder: NotificationLightDecoder, private val notificationVibrationDecoder: NotificationVibrationDecoder ) { fun convert(account: Account, notificationConfiguration: NotificationConfiguration): NotificationSettings { val light = notificationLightDecoder.decode( isBlinkLightsEnabled = notificationConfiguration.isBlinkLightsEnabled, lightColor = notificationConfiguration.lightColor, accountColor = account.chipColor ) val vibration = notificationVibrationDecoder.decode( isVibrationEnabled = notificationConfiguration.isVibrationEnabled, systemPattern = notificationConfiguration.vibrationPattern ) return NotificationSettings( isRingEnabled = notificationConfiguration.sound != null, ringtone = notificationConfiguration.sound?.toString(), light = light, vibration = vibration ) } }
app/core/src/main/java/com/fsck/k9/notification/NotificationSettingsUpdater.kt 0 → 100644 +34 −0 Original line number Diff line number Diff line package com.fsck.k9.notification import android.os.Build import androidx.annotation.RequiresApi import com.fsck.k9.Account import com.fsck.k9.Preferences /** * Update accounts with notification settings read from their "Messages" `NotificationChannel`. */ class NotificationSettingsUpdater( private val preferences: Preferences, private val notificationChannelManager: NotificationChannelManager, private val notificationConfigurationConverter: NotificationConfigurationConverter ) { fun updateNotificationSettings(accountUuids: Collection<String>) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return accountUuids .mapNotNull { accountUuid -> preferences.getAccount(accountUuid) } .forEach { account -> updateNotificationSettings(account) } } @RequiresApi(Build.VERSION_CODES.O) private fun updateNotificationSettings(account: Account) { val notificationConfiguration = notificationChannelManager.getNotificationConfiguration(account) val notificationSettings = notificationConfigurationConverter.convert(account, notificationConfiguration) if (notificationSettings != account.notificationSettings) { account.updateNotificationSettings { notificationSettings } preferences.saveAccount(account) } } }
app/core/src/main/java/com/fsck/k9/notification/NotificationVibrationDecoder.kt 0 → 100644 +26 −0 Original line number Diff line number Diff line package com.fsck.k9.notification import com.fsck.k9.NotificationVibration import com.fsck.k9.VibratePattern /** * Converts the vibration values read from a `NotificationChannel` into [NotificationVibration]. */ class NotificationVibrationDecoder { fun decode(isVibrationEnabled: Boolean, systemPattern: List<Long>?): NotificationVibration { if (systemPattern == null || systemPattern.size < 2 || systemPattern.size % 2 != 0) { return NotificationVibration.DEFAULT } val systemPatternArray = systemPattern.toLongArray() val repeatCount = systemPattern.size / 2 val pattern = VibratePattern.values() .firstOrNull { vibratePattern -> val testPattern = NotificationVibration.getSystemPattern(vibratePattern, repeatCount) testPattern.contentEquals(systemPatternArray) } ?: VibratePattern.Default return NotificationVibration(isVibrationEnabled, pattern, repeatCount) } }
app/core/src/main/java/com/fsck/k9/preferences/KoinModule.kt +2 −1 Original line number Diff line number Diff line Loading @@ -11,7 +11,8 @@ val preferencesModule = module { contentResolver = get(), preferences = get(), folderSettingsProvider = get(), folderRepository = get() folderRepository = get(), notificationSettingsUpdater = get() ) } factory { FolderSettingsProvider(folderRepository = get()) } Loading