Merge pull request #5954 from k9mail/vibration_setting_refactoring

Extract vibration settings to separate class `NotificationVibration`
This commit is contained in:
cketti 2022-03-10 16:04:23 +01:00 committed by GitHub
commit e316fda852
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 104 additions and 86 deletions

View file

@ -142,9 +142,11 @@ class AccountPreferenceSerializer(
isRingEnabled = storage.getBoolean("$accountUuid.ring", true),
ringtone = storage.getString("$accountUuid.ringtone", DEFAULT_RINGTONE_URI),
light = getEnumStringPref(storage, "$accountUuid.notificationLight", NotificationLight.Disabled),
isVibrateEnabled = storage.getBoolean("$accountUuid.vibrate", false),
vibratePattern = VibratePattern.deserialize(storage.getInt("$accountUuid.vibratePattern", 0)),
vibrateTimes = storage.getInt("$accountUuid.vibrateTimes", 5)
vibration = NotificationVibration(
isEnabled = storage.getBoolean("$accountUuid.vibrate", false),
pattern = VibratePattern.deserialize(storage.getInt("$accountUuid.vibratePattern", 0)),
repeatCount = storage.getInt("$accountUuid.vibrateTimes", 5)
)
)
}
@ -322,9 +324,9 @@ class AccountPreferenceSerializer(
editor.putBoolean("$accountUuid.markMessageAsReadOnDelete", isMarkMessageAsReadOnDelete)
editor.putBoolean("$accountUuid.alwaysShowCcBcc", isAlwaysShowCcBcc)
editor.putBoolean("$accountUuid.vibrate", notificationSettings.isVibrateEnabled)
editor.putInt("$accountUuid.vibratePattern", notificationSettings.vibratePattern.serialize())
editor.putInt("$accountUuid.vibrateTimes", notificationSettings.vibrateTimes)
editor.putBoolean("$accountUuid.vibrate", notificationSettings.vibration.isEnabled)
editor.putInt("$accountUuid.vibratePattern", notificationSettings.vibration.pattern.serialize())
editor.putInt("$accountUuid.vibrateTimes", notificationSettings.vibration.repeatCount)
editor.putBoolean("$accountUuid.ring", notificationSettings.isRingEnabled)
editor.putString("$accountUuid.ringtone", notificationSettings.ringtone)
editor.putString("$accountUuid.notificationLight", notificationSettings.light.name)
@ -607,9 +609,7 @@ class AccountPreferenceSerializer(
isRingEnabled = true,
ringtone = DEFAULT_RINGTONE_URI,
light = NotificationLight.Disabled,
isVibrateEnabled = false,
vibratePattern = VibratePattern.Default,
vibrateTimes = 5
vibration = NotificationVibration.DEFAULT
)
}

View file

@ -7,60 +7,5 @@ data class NotificationSettings(
val isRingEnabled: Boolean = false,
val ringtone: String? = null,
val light: NotificationLight = NotificationLight.Disabled,
val isVibrateEnabled: Boolean = false,
val vibratePattern: VibratePattern = VibratePattern.Default,
val vibrateTimes: Int = 0
) {
val vibrationPattern: LongArray
get() = getVibrationPattern(vibratePattern, vibrateTimes)
companion object {
fun getVibrationPattern(vibratePattern: VibratePattern, times: Int): LongArray {
val selectedPattern = vibratePattern.vibrationPattern
val repeatedPattern = LongArray(selectedPattern.size * times)
for (n in 0 until times) {
System.arraycopy(selectedPattern, 0, repeatedPattern, n * selectedPattern.size, selectedPattern.size)
}
// Do not wait before starting the vibration pattern.
repeatedPattern[0] = 0
return repeatedPattern
}
}
}
enum class VibratePattern(
/**
* These are "off, on" patterns, specified in milliseconds.
*/
val vibrationPattern: LongArray
) {
Default(vibrationPattern = longArrayOf(300, 200)),
Pattern1(vibrationPattern = longArrayOf(100, 200)),
Pattern2(vibrationPattern = longArrayOf(100, 500)),
Pattern3(vibrationPattern = longArrayOf(200, 200)),
Pattern4(vibrationPattern = longArrayOf(200, 500)),
Pattern5(vibrationPattern = longArrayOf(500, 500));
fun serialize(): Int = when (this) {
Default -> 0
Pattern1 -> 1
Pattern2 -> 2
Pattern3 -> 3
Pattern4 -> 4
Pattern5 -> 5
}
companion object {
fun deserialize(value: Int): VibratePattern = when (value) {
0 -> Default
1 -> Pattern1
2 -> Pattern2
3 -> Pattern3
4 -> Pattern4
5 -> Pattern5
else -> error("Unknown VibratePattern value: $value")
}
}
}
val vibration: NotificationVibration = NotificationVibration.DEFAULT
)

View file

@ -0,0 +1,62 @@
package com.fsck.k9
data class NotificationVibration(
val isEnabled: Boolean,
val pattern: VibratePattern,
val repeatCount: Int
) {
val systemPattern: LongArray
get() = getSystemPattern(pattern, repeatCount)
companion object {
val DEFAULT = NotificationVibration(isEnabled = false, pattern = VibratePattern.Default, repeatCount = 5)
fun getSystemPattern(vibratePattern: VibratePattern, repeatCount: Int): LongArray {
val selectedPattern = vibratePattern.vibrationPattern
val repeatedPattern = LongArray(selectedPattern.size * repeatCount)
for (n in 0 until repeatCount) {
System.arraycopy(selectedPattern, 0, repeatedPattern, n * selectedPattern.size, selectedPattern.size)
}
// Do not wait before starting the vibration pattern.
repeatedPattern[0] = 0
return repeatedPattern
}
}
}
enum class VibratePattern(
/**
* These are "off, on" patterns, specified in milliseconds.
*/
val vibrationPattern: LongArray
) {
Default(vibrationPattern = longArrayOf(300, 200)),
Pattern1(vibrationPattern = longArrayOf(100, 200)),
Pattern2(vibrationPattern = longArrayOf(100, 500)),
Pattern3(vibrationPattern = longArrayOf(200, 200)),
Pattern4(vibrationPattern = longArrayOf(200, 500)),
Pattern5(vibrationPattern = longArrayOf(500, 500));
fun serialize(): Int = when (this) {
Default -> 0
Pattern1 -> 1
Pattern2 -> 2
Pattern3 -> 3
Pattern4 -> 4
Pattern5 -> 5
}
companion object {
fun deserialize(value: Int): VibratePattern = when (value) {
0 -> Default
1 -> Pattern1
2 -> Pattern2
3 -> Pattern3
4 -> Pattern4
5 -> Pattern5
else -> error("Unknown VibratePattern value: $value")
}
}
}

View file

@ -41,7 +41,7 @@ internal class BaseNotificationDataCreator {
private fun createNotificationAppearance(account: Account): NotificationAppearance {
return with(account.notificationSettings) {
val vibrationPattern = if (isVibrateEnabled) vibrationPattern else null
val vibrationPattern = vibration.systemPattern.takeIf { vibration.isEnabled }
NotificationAppearance(ringtone, vibrationPattern, account.notificationSettings.light.toColor(account))
}
}

View file

@ -213,8 +213,8 @@ class NotificationChannelManager(
val notificationSettings = account.notificationSettings
return sound == notificationSettings.ringtoneUri &&
systemLight == notificationSettings.light &&
shouldVibrate() == notificationSettings.isVibrateEnabled &&
vibrationPattern.contentEquals(notificationSettings.vibrationPattern)
shouldVibrate() == notificationSettings.vibration.isEnabled &&
vibrationPattern.contentEquals(notificationSettings.vibration.systemPattern)
}
@RequiresApi(Build.VERSION_CODES.O)
@ -244,8 +244,8 @@ class NotificationChannelManager(
val isLightEnabled = notificationSettings.light != NotificationLight.Disabled
enableLights(isLightEnabled)
vibrationPattern = notificationSettings.vibrationPattern
enableVibration(notificationSettings.isVibrateEnabled)
vibrationPattern = notificationSettings.vibration.systemPattern
enableVibration(notificationSettings.vibration.isEnabled)
}
private val Account.messagesNotificationChannelSuffix: String

View file

@ -5,7 +5,7 @@ import com.fsck.k9.Identity
import com.fsck.k9.K9
import com.fsck.k9.K9.LockScreenNotificationVisibility
import com.fsck.k9.NotificationLight
import com.fsck.k9.NotificationSettings
import com.fsck.k9.NotificationVibration
import com.fsck.k9.VibratePattern
import com.google.common.truth.Truth.assertThat
import org.junit.Test
@ -151,14 +151,20 @@ class BaseNotificationDataCreatorTest {
@Test
fun `vibration pattern`() {
account.updateNotificationSettings {
it.copy(isVibrateEnabled = true, vibratePattern = VibratePattern.Pattern3, vibrateTimes = 2)
it.copy(
vibration = NotificationVibration(
isEnabled = true,
pattern = VibratePattern.Pattern3,
repeatCount = 2
)
)
}
val notificationData = createNotificationData()
val result = notificationDataCreator.createBaseNotificationData(notificationData)
assertThat(result.appearance.vibrationPattern).isEqualTo(
NotificationSettings.getVibrationPattern(VibratePattern.Pattern3, 2)
NotificationVibration.getSystemPattern(VibratePattern.Pattern3, 2)
)
}

View file

@ -4,6 +4,7 @@ import androidx.preference.PreferenceDataStore
import com.fsck.k9.Account
import com.fsck.k9.Account.SpecialFolderSelection
import com.fsck.k9.NotificationLight
import com.fsck.k9.NotificationVibration
import com.fsck.k9.Preferences
import com.fsck.k9.job.K9JobManager
import com.fsck.k9.notification.NotificationChannelManager
@ -263,20 +264,24 @@ class AccountSettingsDataStore(
}
private fun getCombinedVibrationValue(): String {
return VibrationPreference.encode(
isVibrationEnabled = account.notificationSettings.isVibrateEnabled,
vibratePattern = account.notificationSettings.vibratePattern,
vibrationTimes = account.notificationSettings.vibrateTimes
)
return with(account.notificationSettings.vibration) {
VibrationPreference.encode(
isVibrationEnabled = isEnabled,
vibratePattern = pattern,
vibrationTimes = repeatCount
)
}
}
private fun setCombinedVibrationValue(value: String) {
val (isVibrationEnabled, vibrationPattern, vibrationTimes) = VibrationPreference.decode(value)
account.updateNotificationSettings { notificationSettings ->
notificationSettings.copy(
isVibrateEnabled = isVibrationEnabled,
vibratePattern = vibrationPattern,
vibrateTimes = vibrationTimes,
vibration = NotificationVibration(
isEnabled = isVibrationEnabled,
pattern = vibrationPattern,
repeatCount = vibrationTimes,
)
)
}
notificationSettingsChanged = true

View file

@ -16,7 +16,7 @@ import androidx.appcompat.app.AlertDialog
import androidx.appcompat.widget.SwitchCompat
import androidx.core.content.getSystemService
import androidx.preference.PreferenceDialogFragmentCompat
import com.fsck.k9.NotificationSettings
import com.fsck.k9.NotificationVibration
import com.fsck.k9.VibratePattern
import com.fsck.k9.ui.R
import com.fsck.k9.ui.getEnum
@ -81,7 +81,7 @@ class VibrationDialogFragment : PreferenceDialogFragmentCompat() {
private fun playVibration() {
val vibratePattern = adapter.vibratePattern
val vibrationTimes = adapter.vibrationTimes
val vibrationPattern = NotificationSettings.getVibrationPattern(vibratePattern, vibrationTimes)
val vibrationPattern = NotificationVibration.getSystemPattern(vibratePattern, vibrationTimes)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val vibrationEffect = VibrationEffect.createWaveform(vibrationPattern, -1)

View file

@ -5,7 +5,7 @@ import android.content.Context
import android.util.AttributeSet
import androidx.core.content.res.TypedArrayUtils
import androidx.preference.ListPreference
import com.fsck.k9.NotificationSettings
import com.fsck.k9.NotificationVibration
import com.fsck.k9.VibratePattern
import com.fsck.k9.ui.R
import com.takisoft.preferencex.PreferenceFragmentCompat
@ -75,7 +75,7 @@ constructor(
VibratePattern.deserialize(serializedVibratePattern)
}
.firstOrNull { vibratePattern ->
val testPattern = NotificationSettings.getVibrationPattern(vibratePattern, vibrationTimes)
val testPattern = NotificationVibration.getSystemPattern(vibratePattern, vibrationTimes)
testPattern.contentEquals(combinedPatternArray)
} ?: DEFAULT_VIBRATE_PATTERN