Notification settings: pick custom sounds for messages and call ringtones (#6897)

* Notification settings: pick custom sounds for messages and call ringtones (#6702)

* Feature: add notification sound settings for messages and calls

* Add sound not available banner, accessibility checks

* Fix build and Konsist convention violations

- libraries:preferences:api now uses io.element.android-compose-library
  so NotificationSound's @Immutable annotation can resolve, matching the
  pattern in libraries:matrix:api.
- Rename SoundUnavailableBannerStateProvider to
  NotificationSoundUnavailableStateProvider so the class name contains
  the parameter type as required by KonsistClassNameTest.

* Suppress LargeClass on RoomListPresenterTest

The three new sound-unavailable tests pushed the class past Detekt's
default LargeClass threshold. Matches the suppression pattern already
used on five presenter test classes under features/messages/impl/test.

* NotificationSettingsView: guard ringtone-picker launchers in inspection mode

rememberLauncherForActivityResult requires a LocalActivityResultRegistryOwner,
which Paparazzi-driven preview snapshot tests don't provide, causing
NotificationSettingsViewPreview to throw IllegalStateException for every
preview parameter. Extract the click handlers into a helper that returns a
no-op when LocalInspectionMode.current is true; the runtime path is unchanged.

* Handle unresolved notification sounds mid session

* Quality checks, refactor mid session error ui

* Suppress detekt LargeClass on NotificationSettingsPresenterTest

* Remove notification sound resilience layer, replace with file copy

* Reselect element sound option, code cleanup

* Add element fade notification sound option

* Fix new file headers

* Revert to flowOf(Unit) for announcement service

* Add ringtone picker dialog to match message selector

* Update strings in conjunction with ios

* Update test to reflect new string

* Sync strings.

* Update screenshots

* Fix test

* Remove not properly located translations.
This will be fixed in the next String sync.

* Fix test on CI

---------

Co-authored-by: Jenna Vassar <jenna.vassar@toptal.com>
Co-authored-by: ElementBot <android@element.io>
This commit is contained in:
Benoit Marty
2026-05-28 16:54:12 +02:00
committed by GitHub
parent bdedf41f99
commit c88edc9621
79 changed files with 3135 additions and 139 deletions
+1 -1
View File
@@ -9,7 +9,7 @@ import extension.testCommonDependencies
*/
plugins {
id("io.element.android-library")
id("io.element.android-compose-library")
}
android {
@@ -41,5 +41,27 @@ interface AppPreferencesStore {
suspend fun setTracingLogPacks(targets: Set<TraceLogPack>)
fun getTracingLogPacksFlow(): Flow<Set<TraceLogPack>>
fun getMessageSoundFlow(): Flow<NotificationSound>
/**
* Atomically persists [sound] (with copy-time [title] for Custom; cleared otherwise) and
* bumps the channel version. Single transaction so process death can't desync URI and version.
*/
suspend fun setMessageSoundAndIncrementVersion(sound: NotificationSound, title: String?): Int
/** Title captured at copy time. Null for SystemDefault / Silent or pre-title persisted data. */
fun getMessageSoundDisplayNameFlow(): Flow<String?>
fun getCallRingtoneFlow(): Flow<NotificationSound>
/** See [setMessageSoundAndIncrementVersion]. */
suspend fun setCallRingtoneAndIncrementVersion(sound: NotificationSound, title: String?): Int
/** See [getMessageSoundDisplayNameFlow]. */
fun getCallRingtoneDisplayNameFlow(): Flow<String?>
/** Single-snapshot read of all sound prefs; used at boot to seed channels without N reads. */
suspend fun getNotificationSoundChannelConfig(): NotificationSoundChannelConfig
suspend fun reset()
}
@@ -0,0 +1,77 @@
/*
* Copyright (c) 2026 Element Creations Ltd.
*
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
* Please see LICENSE files in the repository root for full details.
*/
package io.element.android.libraries.preferences.api.store
import androidx.compose.runtime.Immutable
/**
* The user's choice of notification sound for a notification channel.
*/
@Immutable
sealed interface NotificationSound {
/** Use the system's default tone for this channel (e.g. notification or ringtone). */
data object SystemDefault : NotificationSound
/**
* Use the bundled in-app sound. Semantically meaningful only for the noisy message channel,
* where it resolves to `R.raw.message`; the ringing-call channel has no bundled tone and
* treats this the same as [SystemDefault].
*/
data object ElementDefault : NotificationSound
/**
* Use the bundled in-app fade sound. Like [ElementDefault] this is meaningful only for the
* noisy message channel (`R.raw.element_fade`); the ringing-call channel has no bundled tone
* and treats this the same as [SystemDefault].
*/
data object ElementFade : NotificationSound
/** Produce no sound. */
data object Silent : NotificationSound
/** Use the ringtone at [uri]. */
data class Custom(val uri: String) : NotificationSound
companion object {
// String? round-trip used by [AppPreferencesStore]:
// null -> SystemDefault, "silent" -> Silent, "element_default" -> ElementDefault,
// "element_fade" -> ElementFade, else -> Custom(uri).
private const val STORED_SILENT = "silent"
private const val STORED_ELEMENT_DEFAULT = "element_default"
private const val STORED_ELEMENT_FADE = "element_fade"
fun fromStored(value: String?): NotificationSound = when (value) {
null -> SystemDefault
STORED_SILENT -> Silent
STORED_ELEMENT_DEFAULT -> ElementDefault
STORED_ELEMENT_FADE -> ElementFade
else -> Custom(value)
}
fun NotificationSound.toStored(): String? = when (this) {
SystemDefault -> null
Silent -> STORED_SILENT
ElementDefault -> STORED_ELEMENT_DEFAULT
ElementFade -> STORED_ELEMENT_FADE
is Custom -> uri
}
}
}
/**
* Snapshot of the persisted notification-sound state, returned by
* [AppPreferencesStore.getNotificationSoundChannelConfig] in a single read.
*/
data class NotificationSoundChannelConfig(
val messageSound: NotificationSound,
val messageSoundVersion: Int,
val messageSoundDisplayName: String?,
val callRingtone: NotificationSound,
val callRingtoneVersion: Int,
val callRingtoneDisplayName: String?,
)
@@ -20,8 +20,12 @@ import io.element.android.libraries.matrix.api.media.MediaPreviewValue
import io.element.android.libraries.matrix.api.tracing.LogLevel
import io.element.android.libraries.matrix.api.tracing.TraceLogPack
import io.element.android.libraries.preferences.api.store.AppPreferencesStore
import io.element.android.libraries.preferences.api.store.NotificationSound
import io.element.android.libraries.preferences.api.store.NotificationSound.Companion.toStored
import io.element.android.libraries.preferences.api.store.NotificationSoundChannelConfig
import io.element.android.libraries.preferences.api.store.PreferenceDataStoreFactory
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
private val developerModeKey = booleanPreferencesKey("developerMode")
@@ -32,6 +36,12 @@ private val timelineMediaPreviewValueKey = stringPreferencesKey("timelineMediaPr
private val liveLocationMinimumDistanceUpdateKey = intPreferencesKey("liveLocationMinimumDistanceUpdate")
private val logLevelKey = stringPreferencesKey("logLevel")
private val traceLogPacksKey = stringPreferencesKey("traceLogPacks")
private val messageSoundUriKey = stringPreferencesKey("notificationMessageSoundUri")
private val messageSoundChannelVersionKey = intPreferencesKey("notificationMessageSoundChannelVersion")
private val messageSoundDisplayNameKey = stringPreferencesKey("notificationMessageSoundDisplayName")
private val callRingtoneUriKey = stringPreferencesKey("notificationCallRingtoneUri")
private val callRingtoneChannelVersionKey = intPreferencesKey("notificationCallRingtoneChannelVersion")
private val callRingtoneDisplayNameKey = stringPreferencesKey("notificationCallRingtoneDisplayName")
@ContributesBinding(AppScope::class)
class DefaultAppPreferencesStore(
@@ -158,6 +168,75 @@ class DefaultAppPreferencesStore(
}
}
override fun getMessageSoundFlow(): Flow<NotificationSound> {
return store.data.map { prefs -> NotificationSound.fromStored(prefs[messageSoundUriKey]) }
}
override suspend fun setMessageSoundAndIncrementVersion(sound: NotificationSound, title: String?): Int {
var newVersion = 0
store.edit { prefs ->
val stored = sound.toStored()
if (stored != null) {
prefs[messageSoundUriKey] = stored
} else {
prefs.remove(messageSoundUriKey)
}
// Clear title on non-Custom so the picker doesn't show a stale label after a revert.
if (sound is NotificationSound.Custom && !title.isNullOrBlank()) {
prefs[messageSoundDisplayNameKey] = title
} else {
prefs.remove(messageSoundDisplayNameKey)
}
newVersion = (prefs[messageSoundChannelVersionKey] ?: 0) + 1
prefs[messageSoundChannelVersionKey] = newVersion
}
return newVersion
}
override fun getMessageSoundDisplayNameFlow(): Flow<String?> {
return store.data.map { prefs -> prefs[messageSoundDisplayNameKey] }
}
override fun getCallRingtoneFlow(): Flow<NotificationSound> {
return store.data.map { prefs -> NotificationSound.fromStored(prefs[callRingtoneUriKey]) }
}
override suspend fun setCallRingtoneAndIncrementVersion(sound: NotificationSound, title: String?): Int {
var newVersion = 0
store.edit { prefs ->
val stored = sound.toStored()
if (stored != null) {
prefs[callRingtoneUriKey] = stored
} else {
prefs.remove(callRingtoneUriKey)
}
if (sound is NotificationSound.Custom && !title.isNullOrBlank()) {
prefs[callRingtoneDisplayNameKey] = title
} else {
prefs.remove(callRingtoneDisplayNameKey)
}
newVersion = (prefs[callRingtoneChannelVersionKey] ?: 0) + 1
prefs[callRingtoneChannelVersionKey] = newVersion
}
return newVersion
}
override fun getCallRingtoneDisplayNameFlow(): Flow<String?> {
return store.data.map { prefs -> prefs[callRingtoneDisplayNameKey] }
}
override suspend fun getNotificationSoundChannelConfig(): NotificationSoundChannelConfig {
val prefs = store.data.first()
return NotificationSoundChannelConfig(
messageSound = NotificationSound.fromStored(prefs[messageSoundUriKey]),
messageSoundVersion = prefs[messageSoundChannelVersionKey] ?: 0,
messageSoundDisplayName = prefs[messageSoundDisplayNameKey],
callRingtone = NotificationSound.fromStored(prefs[callRingtoneUriKey]),
callRingtoneVersion = prefs[callRingtoneChannelVersionKey] ?: 0,
callRingtoneDisplayName = prefs[callRingtoneDisplayNameKey],
)
}
override suspend fun reset() {
store.edit { it.clear() }
}
@@ -12,8 +12,11 @@ import io.element.android.libraries.matrix.api.media.MediaPreviewValue
import io.element.android.libraries.matrix.api.tracing.LogLevel
import io.element.android.libraries.matrix.api.tracing.TraceLogPack
import io.element.android.libraries.preferences.api.store.AppPreferencesStore
import io.element.android.libraries.preferences.api.store.NotificationSound
import io.element.android.libraries.preferences.api.store.NotificationSoundChannelConfig
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.updateAndGet
class InMemoryAppPreferencesStore(
isDeveloperModeEnabled: Boolean = false,
@@ -23,16 +26,28 @@ class InMemoryAppPreferencesStore(
theme: String? = null,
liveLocationMinimumDistanceUpdate: Int = 10,
logLevel: LogLevel = LogLevel.INFO,
traceLockPacks: Set<TraceLogPack> = emptySet(),
traceLogPacks: Set<TraceLogPack> = emptySet(),
messageSound: NotificationSound = NotificationSound.SystemDefault,
messageSoundChannelVersion: Int = 0,
messageSoundDisplayName: String? = null,
callRingtone: NotificationSound = NotificationSound.SystemDefault,
callRingtoneChannelVersion: Int = 0,
callRingtoneDisplayName: String? = null,
) : AppPreferencesStore {
private val isDeveloperModeEnabled = MutableStateFlow(isDeveloperModeEnabled)
private val customElementCallBaseUrl = MutableStateFlow(customElementCallBaseUrl)
private val theme = MutableStateFlow(theme)
private val liveLocationMinimumDistanceUpdate = MutableStateFlow(liveLocationMinimumDistanceUpdate)
private val logLevel = MutableStateFlow(logLevel)
private val tracingLogPacks = MutableStateFlow(traceLockPacks)
private val tracingLogPacks = MutableStateFlow(traceLogPacks)
private val hideInviteAvatars = MutableStateFlow(hideInviteAvatars)
private val timelineMediaPreviewValue = MutableStateFlow(timelineMediaPreviewValue)
private val messageSound = MutableStateFlow(messageSound)
private val messageSoundChannelVersion = MutableStateFlow(messageSoundChannelVersion)
private val messageSoundDisplayName = MutableStateFlow(messageSoundDisplayName)
private val callRingtone = MutableStateFlow(callRingtone)
private val callRingtoneChannelVersion = MutableStateFlow(callRingtoneChannelVersion)
private val callRingtoneDisplayName = MutableStateFlow(callRingtoneDisplayName)
override suspend fun setDeveloperModeEnabled(enabled: Boolean) {
isDeveloperModeEnabled.value = enabled
@@ -102,6 +117,45 @@ class InMemoryAppPreferencesStore(
return tracingLogPacks
}
override fun getMessageSoundFlow(): Flow<NotificationSound> {
return messageSound
}
override suspend fun setMessageSoundAndIncrementVersion(sound: NotificationSound, title: String?): Int {
messageSound.value = sound
messageSoundDisplayName.value = if (sound is NotificationSound.Custom && !title.isNullOrBlank()) title else null
return messageSoundChannelVersion.updateAndGet { it + 1 }
}
override fun getMessageSoundDisplayNameFlow(): Flow<String?> {
return messageSoundDisplayName
}
override fun getCallRingtoneFlow(): Flow<NotificationSound> {
return callRingtone
}
override suspend fun setCallRingtoneAndIncrementVersion(sound: NotificationSound, title: String?): Int {
callRingtone.value = sound
callRingtoneDisplayName.value = if (sound is NotificationSound.Custom && !title.isNullOrBlank()) title else null
return callRingtoneChannelVersion.updateAndGet { it + 1 }
}
override fun getCallRingtoneDisplayNameFlow(): Flow<String?> {
return callRingtoneDisplayName
}
override suspend fun getNotificationSoundChannelConfig(): NotificationSoundChannelConfig {
return NotificationSoundChannelConfig(
messageSound = messageSound.value,
messageSoundVersion = messageSoundChannelVersion.value,
messageSoundDisplayName = messageSoundDisplayName.value,
callRingtone = callRingtone.value,
callRingtoneVersion = callRingtoneChannelVersion.value,
callRingtoneDisplayName = callRingtoneDisplayName.value,
)
}
override suspend fun reset() {
// No op
}