Play incoming chat sound file when a message is being received in currently opened conversation

This commit is contained in:
Sylvain Berfini 2025-02-25 11:22:26 +01:00
parent f1b23337e0
commit fcd365ad81
2 changed files with 33 additions and 1 deletions

View file

@ -345,6 +345,10 @@ class CorePreferences
val ssoCacheFile: String
get() = context.filesDir.absolutePath + "/auth_state.json"
@get:AnyThread
val messageReceivedInVisibleConversationNotificationSound: String
get() = context.filesDir.absolutePath + "/share/sounds/linphone/incoming_chat.wav"
@UiThread
fun copyAssetsFromPackage() {
copy("linphonerc_default", configPath)

View file

@ -31,6 +31,7 @@ import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.media.AudioAttributes
import android.media.AudioManager
import android.media.MediaPlayer
import android.media.RingtoneManager
import android.net.Uri
import android.os.Bundle
@ -124,6 +125,8 @@ class NotificationsManager
private var currentlyDisplayedChatRoomId: String = ""
private var currentlyDisplayedIncomingCallFragment: Boolean = false
private val mediaPlayer: MediaPlayer
private val contactsListener = object : ContactsListener {
@WorkerThread
override fun onContactsLoaded() { }
@ -267,8 +270,9 @@ class NotificationsManager
val id = LinphoneUtils.getConversationId(chatRoom)
if (currentlyDisplayedChatRoomId.isNotEmpty() && id == currentlyDisplayedChatRoomId) {
Log.i(
"$TAG Do not notify received messages for currently displayed conversation [$id]"
"$TAG Do not notify received messages for currently displayed conversation [$id] but play sound"
)
playMessageReceivedSound()
return
}
@ -442,6 +446,21 @@ class NotificationsManager
previousChatNotifications.add(notification.id)
}
}
val soundPath = corePreferences.messageReceivedInVisibleConversationNotificationSound
mediaPlayer = MediaPlayer().apply {
setAudioAttributes(
AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build()
)
setDataSource(soundPath)
try {
prepare()
} catch (e: Exception) {
Log.e("$TAG Failed to prepare message received sound file [$soundPath]: $e")
}
}
}
@AnyThread
@ -1606,6 +1625,15 @@ class NotificationsManager
}
}
@WorkerThread
private fun playMessageReceivedSound() {
try {
mediaPlayer.start()
} catch (e: Exception) {
Log.e("$TAG Failed to play message received sound file: $e")
}
}
class Notifiable(val notificationId: Int) {
var myself: String? = null