Loading app/core/src/main/java/com/fsck/k9/notification/CoreKoinModule.kt +8 −0 Original line number Diff line number Diff line Loading @@ -115,4 +115,12 @@ 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/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 app/core/src/main/java/com/fsck/k9/preferences/SettingsExporter.kt +15 −1 Original line number Diff line number Diff line Loading @@ -10,6 +10,7 @@ import com.fsck.k9.AccountPreferenceSerializer.Companion.IDENTITY_EMAIL_KEY import com.fsck.k9.AccountPreferenceSerializer.Companion.IDENTITY_NAME_KEY import com.fsck.k9.Preferences import com.fsck.k9.mailstore.FolderRepository import com.fsck.k9.notification.NotificationSettingsUpdater import com.fsck.k9.preferences.ServerTypeConverter.fromServerSettingsType import com.fsck.k9.preferences.Settings.InvalidSettingValueException import com.fsck.k9.preferences.Settings.SettingsDescription Loading @@ -24,10 +25,13 @@ class SettingsExporter( private val contentResolver: ContentResolver, private val preferences: Preferences, private val folderSettingsProvider: FolderSettingsProvider, private val folderRepository: FolderRepository private val folderRepository: FolderRepository, private val notificationSettingsUpdater: NotificationSettingsUpdater ) { @Throws(SettingsImportExportException::class) fun exportToUri(includeGlobals: Boolean, accountUuids: Set<String>, uri: Uri) { updateNotificationSettings(accountUuids) try { contentResolver.openOutputStream(uri)!!.use { outputStream -> exportPreferences(outputStream, includeGlobals, accountUuids) Loading @@ -37,6 +41,16 @@ class SettingsExporter( } } private fun updateNotificationSettings(accountUuids: Set<String>) { try { notificationSettingsUpdater.updateNotificationSettings(accountUuids) } catch (e: Exception) { // An error here could mean we export notification settings that don't reflect the current configuration // of the notification channels. But we prefer stale data over failing the export. Timber.w(e, "Error while updating accounts with notification configuration from system") } } @Throws(SettingsImportExportException::class) fun exportPreferences(outputStream: OutputStream, includeGlobals: Boolean, accountUuids: Set<String>) { try { Loading Loading
app/core/src/main/java/com/fsck/k9/notification/CoreKoinModule.kt +8 −0 Original line number Diff line number Diff line Loading @@ -115,4 +115,12 @@ 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/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
app/core/src/main/java/com/fsck/k9/preferences/SettingsExporter.kt +15 −1 Original line number Diff line number Diff line Loading @@ -10,6 +10,7 @@ import com.fsck.k9.AccountPreferenceSerializer.Companion.IDENTITY_EMAIL_KEY import com.fsck.k9.AccountPreferenceSerializer.Companion.IDENTITY_NAME_KEY import com.fsck.k9.Preferences import com.fsck.k9.mailstore.FolderRepository import com.fsck.k9.notification.NotificationSettingsUpdater import com.fsck.k9.preferences.ServerTypeConverter.fromServerSettingsType import com.fsck.k9.preferences.Settings.InvalidSettingValueException import com.fsck.k9.preferences.Settings.SettingsDescription Loading @@ -24,10 +25,13 @@ class SettingsExporter( private val contentResolver: ContentResolver, private val preferences: Preferences, private val folderSettingsProvider: FolderSettingsProvider, private val folderRepository: FolderRepository private val folderRepository: FolderRepository, private val notificationSettingsUpdater: NotificationSettingsUpdater ) { @Throws(SettingsImportExportException::class) fun exportToUri(includeGlobals: Boolean, accountUuids: Set<String>, uri: Uri) { updateNotificationSettings(accountUuids) try { contentResolver.openOutputStream(uri)!!.use { outputStream -> exportPreferences(outputStream, includeGlobals, accountUuids) Loading @@ -37,6 +41,16 @@ class SettingsExporter( } } private fun updateNotificationSettings(accountUuids: Set<String>) { try { notificationSettingsUpdater.updateNotificationSettings(accountUuids) } catch (e: Exception) { // An error here could mean we export notification settings that don't reflect the current configuration // of the notification channels. But we prefer stale data over failing the export. Timber.w(e, "Error while updating accounts with notification configuration from system") } } @Throws(SettingsImportExportException::class) fun exportPreferences(outputStream: OutputStream, includeGlobals: Boolean, accountUuids: Set<String>) { try { Loading