From e9dc7d1c6da03a7f79e1c2f97faaf82bf72967a4 Mon Sep 17 00:00:00 2001 From: Marcel Hibbe Date: Thu, 12 Nov 2026 15:19:19 +0100 Subject: [PATCH 1/2] fix(notifications): offer ringtones when choosing the call sound On some OEMs (e.g. Samsung) the system notification channel settings only offer notification sounds for the calls channel, even though it uses USAGE_NOTIFICATION_RINGTONE, so no call ringtone could be selected. Open an in-app ringtone picker with TYPE_RINGTONE instead (as done by Signal). As the sound of an existing channel can't be changed by the app, the calls channel is recreated under a new versioned id (NOTIFICATION_CHANNEL_CALLS_V5__) keeping its other settings. The system channel settings are still opened if the channel is disabled or no ringtone picker is available. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marcel Hibbe --- .../nextcloud/talk/jobs/NotificationWorker.kt | 2 +- .../talk/settings/SettingsActivity.kt | 53 +++++++++-- .../nextcloud/talk/utils/NotificationUtils.kt | 94 +++++++++++++++++-- 3 files changed, 130 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/jobs/NotificationWorker.kt b/app/src/main/java/com/nextcloud/talk/jobs/NotificationWorker.kt index b21b1ad4b92..89f4c03155e 100644 --- a/app/src/main/java/com/nextcloud/talk/jobs/NotificationWorker.kt +++ b/app/src/main/java/com/nextcloud/talk/jobs/NotificationWorker.kt @@ -327,7 +327,7 @@ class NotificationWorker(context: Context, workerParams: WorkerParameters) : Wor ) val soundUri = getCallRingtoneUri(applicationContext, appPreferences) - val notificationChannelId = NotificationUtils.NotificationChannels.NOTIFICATION_CHANNEL_CALLS_V5.name + val notificationChannelId = NotificationUtils.getCallsNotificationChannelId(applicationContext) val uri = user.baseUrl!!.toUri() val baseUrl = uri.host diff --git a/app/src/main/java/com/nextcloud/talk/settings/SettingsActivity.kt b/app/src/main/java/com/nextcloud/talk/settings/SettingsActivity.kt index a78db86faaa..d2e7122c314 100644 --- a/app/src/main/java/com/nextcloud/talk/settings/SettingsActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/settings/SettingsActivity.kt @@ -16,6 +16,7 @@ import android.animation.AnimatorListenerAdapter import android.annotation.SuppressLint import android.app.KeyguardManager import android.app.NotificationManager +import android.content.ActivityNotFoundException import android.content.Context import android.content.DialogInterface import android.content.Intent @@ -37,6 +38,7 @@ import androidx.appcompat.app.AlertDialog import androidx.compose.runtime.MutableState import androidx.compose.runtime.mutableStateOf import androidx.core.content.ContextCompat +import androidx.core.content.IntentCompat import androidx.core.content.res.ResourcesCompat import androidx.core.graphics.drawable.toDrawable import androidx.core.net.toUri @@ -152,6 +154,17 @@ class SettingsActivity : } } + private val callRingtonePickerLauncher = + registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> + if (result.resultCode == RESULT_OK) { + val pickedUri = result.data?.let { + IntentCompat.getParcelableExtra(it, RingtoneManager.EXTRA_RINGTONE_PICKED_URI, Uri::class.java) + } + NotificationUtils.updateCallRingtone(context, appPreferences, pickedUri) + setupNotificationSoundsSettings() + } + } + private var currentUser: User? = null private var credentials: String? = null private lateinit var proxyTypeFlow: Flow @@ -598,14 +611,11 @@ class SettingsActivity : } binding.settingsCallSound.setOnClickListener { - val intent = Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS) - intent.putExtra(Settings.EXTRA_APP_PACKAGE, BuildConfig.APPLICATION_ID) - intent.putExtra( - Settings.EXTRA_CHANNEL_ID, - NotificationUtils.NotificationChannels.NOTIFICATION_CHANNEL_CALLS_V5.name - ) - - startActivity(intent) + if (NotificationUtils.isCallsNotificationChannelEnabled(context)) { + openCallRingtonePicker() + } else { + openCallsChannelSettings() + } } binding.settingsMessageSound.setOnClickListener { val intent = Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS) @@ -618,6 +628,33 @@ class SettingsActivity : } } + /** + * Use an in-app ringtone picker instead of the system channel settings, because on some OEMs (e.g. Samsung) + * the channel settings only offer notification sounds, even for the calls channel. + */ + private fun openCallRingtonePicker() { + val intent = Intent(RingtoneManager.ACTION_RINGTONE_PICKER) + .putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_RINGTONE) + .putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, getString(R.string.nc_settings_call_ringtone)) + .putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true) + .putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI, NotificationUtils.DEFAULT_CALL_RINGTONE_URI.toUri()) + .putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, true) + .putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, getCallRingtoneUri(context, appPreferences)) + try { + callRingtonePickerLauncher.launch(intent) + } catch (e: ActivityNotFoundException) { + Log.w(TAG, "No ringtone picker available, falling back to channel settings", e) + openCallsChannelSettings() + } + } + + private fun openCallsChannelSettings() { + val intent = Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS) + intent.putExtra(Settings.EXTRA_APP_PACKAGE, BuildConfig.APPLICATION_ID) + intent.putExtra(Settings.EXTRA_CHANNEL_ID, NotificationUtils.getCallsNotificationChannelId(context)) + startActivity(intent) + } + private fun setTroubleshootingClickListenersIfNecessary() { fun click() { val dialogBuilder = MaterialAlertDialogBuilder(this) diff --git a/app/src/main/java/com/nextcloud/talk/utils/NotificationUtils.kt b/app/src/main/java/com/nextcloud/talk/utils/NotificationUtils.kt index 62332359bed..14d0a35448d 100644 --- a/app/src/main/java/com/nextcloud/talk/utils/NotificationUtils.kt +++ b/app/src/main/java/com/nextcloud/talk/utils/NotificationUtils.kt @@ -66,6 +66,22 @@ object NotificationUtils { private const val BITMAP_COMPRESSION_QUALITY = 100 + /** + * The sound of a notification channel can't be changed by the app once the channel exists, and a deleted + * channel is restored with its old settings when it is recreated with the same id. So whenever the call + * ringtone is changed in-app, the calls channel is recreated with the base id plus this separator plus a + * timestamp, e.g. "NOTIFICATION_CHANNEL_CALLS_V5__1762950000000". + * + * This suffix is independent of the "_V5" in the base id: + * - "_V5" is a code-level version. It is bumped by developers when a release must reset the channel for all + * installations (e.g. changed audio attributes). Bumping it drops every channel with the old base id, + * including the timestamped ones, via [removeOldNotificationChannels]. + * - The timestamp suffix is a runtime version. It differs per installation and only changes when the user + * picks a new call ringtone. It is not a counter on top of "_V5" (V5 -> V6 -> ...), because such runtime + * bumps would collide with future code-level bumps. + */ + private const val CHANNEL_ID_VERSION_SEPARATOR = "__" + private fun createNotificationChannel( context: Context, notificationChannel: Channel, @@ -99,27 +115,85 @@ object NotificationUtils { } } + private fun callsAudioAttributes(): AudioAttributes = + AudioAttributes.Builder() + .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) + .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE) + .build() + private fun createCallsNotificationChannel(context: Context, appPreferences: AppPreferences) { - val audioAttributes = - AudioAttributes.Builder() - .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) - .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE) - .build() val soundUri = getCallRingtoneUri(context, appPreferences) createNotificationChannel( context, Channel( - NotificationChannels.NOTIFICATION_CHANNEL_CALLS_V5.name, + getCallsNotificationChannelId(context), context.resources.getString(R.string.nc_notification_channel_calls), context.resources.getString(R.string.nc_notification_channel_calls_description), true ), soundUri, - audioAttributes + callsAudioAttributes() ) } + private fun isCallsNotificationChannelId(channelId: String): Boolean { + val baseId = NotificationChannels.NOTIFICATION_CHANNEL_CALLS_V5.name + return channelId == baseId || channelId.startsWith(baseId + CHANNEL_ID_VERSION_SEPARATOR) + } + + /** + * Returns the id of the current incoming calls channel. It is versioned because it is recreated whenever the + * call ringtone is changed, see [updateCallRingtone]. + */ + fun getCallsNotificationChannelId(context: Context): String { + val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager + return notificationManager.notificationChannels + .firstOrNull { isCallsNotificationChannelId(it.id) } + ?.id + ?: NotificationChannels.NOTIFICATION_CHANNEL_CALLS_V5.name + } + + /** + * Sets a new call ringtone (null = silent). The sound of an existing channel can't be changed by the app, so + * the calls channel is recreated under a new id, keeping all other settings of the existing channel. + * + * This is needed because the system channel settings of some OEMs (e.g. Samsung) only offer notification + * sounds, even for channels with [AudioAttributes.USAGE_NOTIFICATION_RINGTONE]. + */ + fun updateCallRingtone(context: Context, appPreferences: AppPreferences, ringtoneUri: Uri?) { + appPreferences.callRingtoneUri = LoganSquare.serialize(RingtoneSettings(ringtoneUri, null)) + + val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager + val existingChannel = notificationManager.getNotificationChannel(getCallsNotificationChannelId(context)) + if (existingChannel == null) { + createCallsNotificationChannel(context, appPreferences) + return + } + if (existingChannel.sound == ringtoneUri) { + return + } + + val newChannelId = NotificationChannels.NOTIFICATION_CHANNEL_CALLS_V5.name + + CHANNEL_ID_VERSION_SEPARATOR + System.currentTimeMillis() + val newChannel = NotificationChannel(newChannelId, existingChannel.name, existingChannel.importance).apply { + description = existingChannel.description + group = existingChannel.group + setSound(ringtoneUri, existingChannel.audioAttributes ?: callsAudioAttributes()) + enableLights(existingChannel.shouldShowLights()) + lightColor = existingChannel.lightColor + enableVibration(existingChannel.shouldVibrate()) + vibrationPattern = existingChannel.vibrationPattern + lockscreenVisibility = existingChannel.lockscreenVisibility + setShowBadge(existingChannel.canShowBadge()) + setBypassDnd(existingChannel.canBypassDnd()) + } + + Log.i(TAG, "Recreating calls channel ${existingChannel.id} as $newChannelId with new ringtone") + notificationManager.deleteNotificationChannel(existingChannel.id) + notificationManager.createNotificationChannel(newChannel) + } + /** * Separate from [NotificationChannels.NOTIFICATION_CHANNEL_CALLS_V5] (the incoming-ring channel, * which must stay IMPORTANCE_HIGH) so the persistent "call in progress" foreground-service @@ -193,7 +267,7 @@ object NotificationUtils { // Delete all notification channels created by previous versions for (channel in notificationManager.notificationChannels) { - if (!channelsToKeep.contains(channel.id)) { + if (!channelsToKeep.contains(channel.id) && !isCallsNotificationChannelId(channel.id)) { notificationManager.deleteNotificationChannel(channel.id) } } @@ -286,7 +360,7 @@ object NotificationUtils { } fun isCallsNotificationChannelEnabled(context: Context): Boolean { - val channel = getNotificationChannel(context, NotificationChannels.NOTIFICATION_CHANNEL_CALLS_V5.name) + val channel = getNotificationChannel(context, getCallsNotificationChannelId(context)) if (channel != null) { return isNotificationChannelEnabled(channel) } @@ -340,7 +414,7 @@ object NotificationUtils { context, appPreferences.callRingtoneUri, DEFAULT_CALL_RINGTONE_URI, - NotificationChannels.NOTIFICATION_CHANNEL_CALLS_V5.name + getCallsNotificationChannelId(context) ) fun getMessageRingtoneUri(context: Context, appPreferences: AppPreferences): Uri? = From c25621d01470d4f02202b3a57a06799ccb7e2a88 Mon Sep 17 00:00:00 2001 From: Marcel Hibbe Date: Thu, 12 Nov 2026 15:43:16 +0100 Subject: [PATCH 2/2] fix(notifications): allow returning to the bundled default sounds MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The app ships its own default sounds ("Librem by feandesign") for calls and messages. These are app resources, not system sounds, so neither the system channel settings nor the system ringtone picker offer them. Once a user picked another call ringtone or message sound, there was no way back to the bundled default. Tapping the call or message sound now shows a dialog offering the bundled default or "Other sound…", which opens the system picker. The picker's "Default" entry now refers to the phone's default ringtone/notification sound, so it means the same on all devices. Message sounds are chosen in-app like call ringtones. As the sound of an existing channel can't be changed by the app, the messages channel is recreated under a versioned id (NOTIFICATION_CHANNEL_MESSAGES_V4__) the same way as the calls channel, sharing the recreation logic. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marcel Hibbe --- .../nextcloud/talk/jobs/NotificationWorker.kt | 5 +- .../talk/settings/SettingsActivity.kt | 147 ++++++++++++++---- .../nextcloud/talk/utils/NotificationUtils.kt | 145 +++++++++++------ app/src/main/res/values/strings.xml | 1 + 4 files changed, 210 insertions(+), 88 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/jobs/NotificationWorker.kt b/app/src/main/java/com/nextcloud/talk/jobs/NotificationWorker.kt index 89f4c03155e..9b8aea2ac3b 100644 --- a/app/src/main/java/com/nextcloud/talk/jobs/NotificationWorker.kt +++ b/app/src/main/java/com/nextcloud/talk/jobs/NotificationWorker.kt @@ -770,7 +770,7 @@ class NotificationWorker(context: Context, workerParams: WorkerParameters) : Wor TYPE_ADMIN_NOTIFICATIONS, TYPE_REMOTE_TALK_SHARE -> { notificationBuilder.setChannelId( - NotificationUtils.NotificationChannels.NOTIFICATION_CHANNEL_MESSAGES_V4.name + NotificationUtils.getMessagesNotificationChannelId(applicationContext) ) } } @@ -1219,8 +1219,7 @@ class NotificationWorker(context: Context, workerParams: WorkerParameters) : Wor if (isOngoingCallNotificationVisible) { val notificationBuilder = NotificationCompat.Builder( context!!, - NotificationUtils.NotificationChannels - .NOTIFICATION_CHANNEL_MESSAGES_V4.name + NotificationUtils.getMessagesNotificationChannelId(context!!) ) val intent = createContentIntent() diff --git a/app/src/main/java/com/nextcloud/talk/settings/SettingsActivity.kt b/app/src/main/java/com/nextcloud/talk/settings/SettingsActivity.kt index d2e7122c314..85537e84323 100644 --- a/app/src/main/java/com/nextcloud/talk/settings/SettingsActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/settings/SettingsActivity.kt @@ -33,6 +33,7 @@ import android.util.Log import android.view.View import android.view.WindowManager import android.widget.Toast +import androidx.activity.result.ActivityResultLauncher import androidx.activity.result.contract.ActivityResultContracts import androidx.appcompat.app.AlertDialog import androidx.compose.runtime.MutableState @@ -154,16 +155,13 @@ class SettingsActivity : } } - private val callRingtonePickerLauncher = - registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> - if (result.resultCode == RESULT_OK) { - val pickedUri = result.data?.let { - IntentCompat.getParcelableExtra(it, RingtoneManager.EXTRA_RINGTONE_PICKED_URI, Uri::class.java) - } - NotificationUtils.updateCallRingtone(context, appPreferences, pickedUri) - setupNotificationSoundsSettings() - } - } + private val callRingtonePickerLauncher = registerSoundPickerLauncher { uri -> + NotificationUtils.updateCallRingtone(context, appPreferences, uri) + } + + private val messageRingtonePickerLauncher = registerSoundPickerLauncher { uri -> + NotificationUtils.updateMessageRingtone(context, appPreferences, uri) + } private var currentUser: User? = null private var credentials: String? = null @@ -610,48 +608,129 @@ class SettingsActivity : } } - binding.settingsCallSound.setOnClickListener { - if (NotificationUtils.isCallsNotificationChannelEnabled(context)) { - openCallRingtonePicker() - } else { - openCallsChannelSettings() - } + binding.settingsCallSound.setOnClickListener { onCallSoundClicked() } + binding.settingsMessageSound.setOnClickListener { onMessageSoundClicked() } + } + + private fun onCallSoundClicked() { + if (NotificationUtils.isCallsNotificationChannelEnabled(context)) { + showSoundDialog( + R.string.nc_settings_call_ringtone, + onDefaultSelected = { + NotificationUtils.updateCallRingtone( + context, + appPreferences, + NotificationUtils.DEFAULT_CALL_RINGTONE_URI.toUri() + ) + }, + onOtherSelected = { + openSoundPicker( + callRingtonePickerLauncher, + RingtoneManager.TYPE_RINGTONE, + R.string.nc_settings_call_ringtone, + Settings.System.DEFAULT_RINGTONE_URI, + getCallRingtoneUri(context, appPreferences), + NotificationUtils.getCallsNotificationChannelId(context) + ) + } + ) + } else { + openChannelSettings(NotificationUtils.getCallsNotificationChannelId(context)) } - binding.settingsMessageSound.setOnClickListener { - val intent = Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS) - intent.putExtra(Settings.EXTRA_APP_PACKAGE, BuildConfig.APPLICATION_ID) - intent.putExtra( - Settings.EXTRA_CHANNEL_ID, - NotificationUtils.NotificationChannels.NOTIFICATION_CHANNEL_MESSAGES_V4.name + } + + private fun onMessageSoundClicked() { + if (NotificationUtils.isMessagesNotificationChannelEnabled(context)) { + showSoundDialog( + R.string.nc_settings_other_notifications_ringtone, + onDefaultSelected = { + NotificationUtils.updateMessageRingtone( + context, + appPreferences, + NotificationUtils.DEFAULT_MESSAGE_RINGTONE_URI.toUri() + ) + }, + onOtherSelected = { + openSoundPicker( + messageRingtonePickerLauncher, + RingtoneManager.TYPE_NOTIFICATION, + R.string.nc_settings_other_notifications_ringtone, + Settings.System.DEFAULT_NOTIFICATION_URI, + getMessageRingtoneUri(context, appPreferences), + NotificationUtils.getMessagesNotificationChannelId(context) + ) + } ) - startActivity(intent) + } else { + openChannelSettings(NotificationUtils.getMessagesNotificationChannelId(context)) } } + private fun registerSoundPickerLauncher(onPicked: (Uri?) -> Unit) = + registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> + if (result.resultCode == RESULT_OK) { + val pickedUri = result.data?.let { + IntentCompat.getParcelableExtra(it, RingtoneManager.EXTRA_RINGTONE_PICKED_URI, Uri::class.java) + } + onPicked(pickedUri) + setupNotificationSoundsSettings() + } + } + /** - * Use an in-app ringtone picker instead of the system channel settings, because on some OEMs (e.g. Samsung) - * the channel settings only offer notification sounds, even for the calls channel. + * Sounds are chosen in-app instead of in the system channel settings, because the bundled default sounds are + * not part of the system sounds, and some OEMs (e.g. Samsung) only offer notification sounds for the calls + * channel. */ - private fun openCallRingtonePicker() { + private fun showSoundDialog(titleRes: Int, onDefaultSelected: () -> Unit, onOtherSelected: () -> Unit) { + val items = arrayOf( + getString(R.string.nc_settings_default_ringtone), + getString(R.string.nc_settings_other_sound) + ) + val dialogBuilder = MaterialAlertDialogBuilder(this) + .setTitle(titleRes) + .setItems(items) { _, which -> + if (which == 0) { + onDefaultSelected() + setupNotificationSoundsSettings() + } else { + onOtherSelected() + } + } + .setNegativeButton(R.string.nc_cancel, null) + viewThemeUtils.dialog.colorMaterialAlertDialogBackground(this, dialogBuilder) + val dialog = dialogBuilder.show() + viewThemeUtils.platform.colorTextButtons(dialog.getButton(AlertDialog.BUTTON_NEGATIVE)) + } + + @Suppress("LongParameterList") + private fun openSoundPicker( + launcher: ActivityResultLauncher, + ringtoneType: Int, + titleRes: Int, + systemDefaultUri: Uri, + existingUri: Uri?, + fallbackChannelId: String + ) { val intent = Intent(RingtoneManager.ACTION_RINGTONE_PICKER) - .putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_RINGTONE) - .putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, getString(R.string.nc_settings_call_ringtone)) + .putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, ringtoneType) + .putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, getString(titleRes)) .putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true) - .putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI, NotificationUtils.DEFAULT_CALL_RINGTONE_URI.toUri()) + .putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI, systemDefaultUri) .putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, true) - .putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, getCallRingtoneUri(context, appPreferences)) + .putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, existingUri) try { - callRingtonePickerLauncher.launch(intent) + launcher.launch(intent) } catch (e: ActivityNotFoundException) { Log.w(TAG, "No ringtone picker available, falling back to channel settings", e) - openCallsChannelSettings() + openChannelSettings(fallbackChannelId) } } - private fun openCallsChannelSettings() { + private fun openChannelSettings(channelId: String) { val intent = Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS) intent.putExtra(Settings.EXTRA_APP_PACKAGE, BuildConfig.APPLICATION_ID) - intent.putExtra(Settings.EXTRA_CHANNEL_ID, NotificationUtils.getCallsNotificationChannelId(context)) + intent.putExtra(Settings.EXTRA_CHANNEL_ID, channelId) startActivity(intent) } diff --git a/app/src/main/java/com/nextcloud/talk/utils/NotificationUtils.kt b/app/src/main/java/com/nextcloud/talk/utils/NotificationUtils.kt index 14d0a35448d..ecb23d21922 100644 --- a/app/src/main/java/com/nextcloud/talk/utils/NotificationUtils.kt +++ b/app/src/main/java/com/nextcloud/talk/utils/NotificationUtils.kt @@ -69,15 +69,15 @@ object NotificationUtils { /** * The sound of a notification channel can't be changed by the app once the channel exists, and a deleted * channel is restored with its old settings when it is recreated with the same id. So whenever the call - * ringtone is changed in-app, the calls channel is recreated with the base id plus this separator plus a - * timestamp, e.g. "NOTIFICATION_CHANNEL_CALLS_V5__1762950000000". + * ringtone or message sound is changed in-app, the channel is recreated with the base id plus this separator + * plus a timestamp, e.g. "NOTIFICATION_CHANNEL_CALLS_V5__1762950000000". * * This suffix is independent of the "_V5" in the base id: * - "_V5" is a code-level version. It is bumped by developers when a release must reset the channel for all * installations (e.g. changed audio attributes). Bumping it drops every channel with the old base id, * including the timestamped ones, via [removeOldNotificationChannels]. * - The timestamp suffix is a runtime version. It differs per installation and only changes when the user - * picks a new call ringtone. It is not a counter on top of "_V5" (V5 -> V6 -> ...), because such runtime + * picks a new sound. It is not a counter on top of "_V5" (V5 -> V6 -> ...), because such runtime * bumps would collide with future code-level bumps. */ private const val CHANNEL_ID_VERSION_SEPARATOR = "__" @@ -137,61 +137,103 @@ object NotificationUtils { ) } - private fun isCallsNotificationChannelId(channelId: String): Boolean { - val baseId = NotificationChannels.NOTIFICATION_CHANNEL_CALLS_V5.name - return channelId == baseId || channelId.startsWith(baseId + CHANNEL_ID_VERSION_SEPARATOR) + private fun isVersionedChannelId(channelId: String, baseId: String): Boolean = + channelId == baseId || channelId.startsWith(baseId + CHANNEL_ID_VERSION_SEPARATOR) + + private fun getVersionedChannelId(context: Context, baseId: String): String { + val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager + return notificationManager.notificationChannels + .firstOrNull { isVersionedChannelId(it.id, baseId) } + ?.id + ?: baseId } /** * Returns the id of the current incoming calls channel. It is versioned because it is recreated whenever the * call ringtone is changed, see [updateCallRingtone]. */ - fun getCallsNotificationChannelId(context: Context): String { - val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager - return notificationManager.notificationChannels - .firstOrNull { isCallsNotificationChannelId(it.id) } - ?.id - ?: NotificationChannels.NOTIFICATION_CHANNEL_CALLS_V5.name - } + fun getCallsNotificationChannelId(context: Context): String = + getVersionedChannelId(context, NotificationChannels.NOTIFICATION_CHANNEL_CALLS_V5.name) /** - * Sets a new call ringtone (null = silent). The sound of an existing channel can't be changed by the app, so - * the calls channel is recreated under a new id, keeping all other settings of the existing channel. + * Returns the id of the current messages channel. It is versioned because it is recreated whenever the + * message sound is changed, see [updateMessageRingtone]. + */ + fun getMessagesNotificationChannelId(context: Context): String = + getVersionedChannelId(context, NotificationChannels.NOTIFICATION_CHANNEL_MESSAGES_V4.name) + + /** + * Sets a new call ringtone (null = silent). * - * This is needed because the system channel settings of some OEMs (e.g. Samsung) only offer notification - * sounds, even for channels with [AudioAttributes.USAGE_NOTIFICATION_RINGTONE]. + * The ringtone is chosen in-app because the system channel settings of some OEMs (e.g. Samsung) only offer + * notification sounds, even for channels with [AudioAttributes.USAGE_NOTIFICATION_RINGTONE], and never offer + * the bundled default sound. */ fun updateCallRingtone(context: Context, appPreferences: AppPreferences, ringtoneUri: Uri?) { appPreferences.callRingtoneUri = LoganSquare.serialize(RingtoneSettings(ringtoneUri, null)) - - val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager - val existingChannel = notificationManager.getNotificationChannel(getCallsNotificationChannelId(context)) - if (existingChannel == null) { + val recreated = recreateChannelWithSound( + context, + NotificationChannels.NOTIFICATION_CHANNEL_CALLS_V5.name, + ringtoneUri, + callsAudioAttributes() + ) + if (!recreated) { createCallsNotificationChannel(context, appPreferences) - return - } - if (existingChannel.sound == ringtoneUri) { - return } + } - val newChannelId = NotificationChannels.NOTIFICATION_CHANNEL_CALLS_V5.name + - CHANNEL_ID_VERSION_SEPARATOR + System.currentTimeMillis() - val newChannel = NotificationChannel(newChannelId, existingChannel.name, existingChannel.importance).apply { - description = existingChannel.description - group = existingChannel.group - setSound(ringtoneUri, existingChannel.audioAttributes ?: callsAudioAttributes()) - enableLights(existingChannel.shouldShowLights()) - lightColor = existingChannel.lightColor - enableVibration(existingChannel.shouldVibrate()) - vibrationPattern = existingChannel.vibrationPattern - lockscreenVisibility = existingChannel.lockscreenVisibility - setShowBadge(existingChannel.canShowBadge()) - setBypassDnd(existingChannel.canBypassDnd()) + /** + * Sets a new message sound (null = silent). It is chosen in-app because the system channel settings never + * offer the bundled default sound. + */ + fun updateMessageRingtone(context: Context, appPreferences: AppPreferences, ringtoneUri: Uri?) { + appPreferences.messageRingtoneUri = LoganSquare.serialize(RingtoneSettings(ringtoneUri, null)) + val recreated = recreateChannelWithSound( + context, + NotificationChannels.NOTIFICATION_CHANNEL_MESSAGES_V4.name, + ringtoneUri, + messagesAudioAttributes() + ) + if (!recreated) { + createMessagesNotificationChannel(context, appPreferences) } + } - Log.i(TAG, "Recreating calls channel ${existingChannel.id} as $newChannelId with new ringtone") - notificationManager.deleteNotificationChannel(existingChannel.id) - notificationManager.createNotificationChannel(newChannel) + /** + * The sound of an existing channel can't be changed by the app, so the channel is recreated under a new id, + * keeping all other settings of the existing channel. + * + * @return false if there is no existing channel to recreate + */ + private fun recreateChannelWithSound( + context: Context, + baseId: String, + sound: Uri?, + fallbackAudioAttributes: AudioAttributes + ): Boolean { + val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager + val existingChannel = notificationManager.getNotificationChannel(getVersionedChannelId(context, baseId)) + ?: return false + if (existingChannel.sound != sound) { + val newChannelId = baseId + CHANNEL_ID_VERSION_SEPARATOR + System.currentTimeMillis() + val newChannel = NotificationChannel(newChannelId, existingChannel.name, existingChannel.importance).apply { + description = existingChannel.description + group = existingChannel.group + setSound(sound, existingChannel.audioAttributes ?: fallbackAudioAttributes) + enableLights(existingChannel.shouldShowLights()) + lightColor = existingChannel.lightColor + enableVibration(existingChannel.shouldVibrate()) + vibrationPattern = existingChannel.vibrationPattern + lockscreenVisibility = existingChannel.lockscreenVisibility + setShowBadge(existingChannel.canShowBadge()) + setBypassDnd(existingChannel.canBypassDnd()) + } + + Log.i(TAG, "Recreating channel ${existingChannel.id} as $newChannelId with new sound") + notificationManager.deleteNotificationChannel(existingChannel.id) + notificationManager.createNotificationChannel(newChannel) + } + return true } /** @@ -213,24 +255,25 @@ object NotificationUtils { ) } + private fun messagesAudioAttributes(): AudioAttributes = + AudioAttributes.Builder() + .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) + .setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT) + .build() + private fun createMessagesNotificationChannel(context: Context, appPreferences: AppPreferences) { - val audioAttributes = - AudioAttributes.Builder() - .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) - .setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT) - .build() val soundUri = getMessageRingtoneUri(context, appPreferences) createNotificationChannel( context, Channel( - NotificationChannels.NOTIFICATION_CHANNEL_MESSAGES_V4.name, + getMessagesNotificationChannelId(context), context.resources.getString(R.string.nc_notification_channel_messages), context.resources.getString(R.string.nc_notification_channel_messages_description), true ), soundUri, - audioAttributes + messagesAudioAttributes() ) } @@ -267,7 +310,7 @@ object NotificationUtils { // Delete all notification channels created by previous versions for (channel in notificationManager.notificationChannels) { - if (!channelsToKeep.contains(channel.id) && !isCallsNotificationChannelId(channel.id)) { + if (channelsToKeep.none { isVersionedChannelId(channel.id, it) }) { notificationManager.deleteNotificationChannel(channel.id) } } @@ -368,7 +411,7 @@ object NotificationUtils { } fun isMessagesNotificationChannelEnabled(context: Context): Boolean { - val channel = getNotificationChannel(context, NotificationChannels.NOTIFICATION_CHANNEL_MESSAGES_V4.name) + val channel = getNotificationChannel(context, getMessagesNotificationChannelId(context)) if (channel != null) { return isNotificationChannelEnabled(channel) } @@ -422,7 +465,7 @@ object NotificationUtils { context, appPreferences.messageRingtoneUri, DEFAULT_MESSAGE_RINGTONE_URI, - NotificationChannels.NOTIFICATION_CHANNEL_MESSAGES_V4.name + getMessagesNotificationChannelId(context) ) fun loadAvatarSync(avatarUrl: String, context: Context, credentials: String? = null): IconCompat? { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8a17dc00159..784f9de4f02 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -129,6 +129,7 @@ How to translate with transifex: Messages Librem by feandesign No sound + Other sound… Server notifications app not installed Please contact the administrator of Battery settings