Fixed notifications if app stops/restarts in between

This commit is contained in:
Sylvain Berfini 2021-09-01 10:59:59 +02:00
parent 3679c29cfd
commit efc84d4054

View file

@ -36,6 +36,8 @@ import androidx.core.content.ContextCompat
import androidx.core.content.LocusIdCompat import androidx.core.content.LocusIdCompat
import androidx.core.graphics.drawable.IconCompat import androidx.core.graphics.drawable.IconCompat
import androidx.navigation.NavDeepLinkBuilder import androidx.navigation.NavDeepLinkBuilder
import kotlin.collections.ArrayList
import kotlin.collections.HashMap
import org.linphone.LinphoneApplication.Companion.coreContext import org.linphone.LinphoneApplication.Companion.coreContext
import org.linphone.LinphoneApplication.Companion.corePreferences import org.linphone.LinphoneApplication.Companion.corePreferences
import org.linphone.R import org.linphone.R
@ -96,7 +98,6 @@ class NotificationsManager(private val context: Context) {
private val chatNotificationsMap: HashMap<String, Notifiable> = HashMap() private val chatNotificationsMap: HashMap<String, Notifiable> = HashMap()
private val callNotificationsMap: HashMap<String, Notifiable> = HashMap() private val callNotificationsMap: HashMap<String, Notifiable> = HashMap()
private var lastNotificationId: Int = 5
private var currentForegroundServiceNotificationId: Int = 0 private var currentForegroundServiceNotificationId: Int = 0
private var serviceNotification: Notification? = null private var serviceNotification: Notification? = null
@ -349,14 +350,17 @@ class NotificationsManager(private val context: Context) {
/* Call related */ /* Call related */
private fun getNotificationIdForCall(call: Call): Int {
return call.callLog.startDate.toInt()
}
private fun getNotifiableForCall(call: Call): Notifiable { private fun getNotifiableForCall(call: Call): Notifiable {
val address = call.remoteAddress.asStringUriOnly() val address = call.remoteAddress.asStringUriOnly()
var notifiable: Notifiable? = callNotificationsMap[address] var notifiable: Notifiable? = callNotificationsMap[address]
if (notifiable == null) { if (notifiable == null) {
notifiable = Notifiable(lastNotificationId) notifiable = Notifiable(getNotificationIdForCall(call))
notifiable.remoteAddress = call.remoteAddress.asStringUriOnly() notifiable.remoteAddress = call.remoteAddress.asStringUriOnly()
lastNotificationId += 1
callNotificationsMap[address] = notifiable callNotificationsMap[address] = notifiable
} }
return notifiable return notifiable
@ -586,6 +590,10 @@ class NotificationsManager(private val context: Context) {
/* Chat related */ /* Chat related */
private fun getNotificationIdForChat(chatRoom: ChatRoom): Int {
return chatRoom.creationTime.toInt()
}
private fun displayChatNotifiable(room: ChatRoom, notifiable: Notifiable) { private fun displayChatNotifiable(room: ChatRoom, notifiable: Notifiable) {
val localAddress = room.localAddress.asStringUriOnly() val localAddress = room.localAddress.asStringUriOnly()
val peerAddress = room.peerAddress.asStringUriOnly() val peerAddress = room.peerAddress.asStringUriOnly()
@ -612,37 +620,15 @@ class NotificationsManager(private val context: Context) {
private fun displayIncomingChatNotification(room: ChatRoom, message: ChatMessage) { private fun displayIncomingChatNotification(room: ChatRoom, message: ChatMessage) {
val contact: Contact? = coreContext.contactsManager.findContactByAddress(message.fromAddress) val contact: Contact? = coreContext.contactsManager.findContactByAddress(message.fromAddress)
val pictureUri = contact?.getContactThumbnailPictureUri()
val roundPicture = ImageUtils.getRoundBitmapFromUri(context, pictureUri)
val displayName = contact?.fullName ?: LinphoneUtils.getDisplayName(message.fromAddress)
var text: String = message.contents.find { content -> content.isText }?.utf8Text ?: ""
if (text.isEmpty()) {
for (content in message.contents) {
text += content.name
}
}
val notifiable = getNotifiableForRoom(room) val notifiable = getNotifiableForRoom(room)
val notifiableMessage = NotifiableMessage(text, contact, displayName, message.time, senderAvatar = roundPicture, isOutgoing = message.isOutgoing) if (notifiable.messages.isNotEmpty() || room.unreadMessagesCount == 1) {
val notifiableMessage = getNotifiableMessage(message, contact)
notifiable.messages.add(notifiableMessage) notifiable.messages.add(notifiableMessage)
for (content in message.contents) {
if (content.isFile) {
val path = content.filePath
if (path != null) {
val contentUri: Uri = FileUtils.getPublicFilePath(context, path)
val filePath: String = contentUri.toString()
val extension = FileUtils.getExtensionFromFileName(filePath)
if (extension.isNotEmpty()) {
val mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension)
notifiableMessage.filePath = contentUri
notifiableMessage.fileMime = mime
Log.i("[Notifications Manager] Added file $contentUri with MIME $mime to notification")
} else { } else {
Log.e("[Notifications Manager] Couldn't find extension for incoming message with file $path") for (chatMessage in room.getUnreadHistory()) {
} val notifiableMessage = getNotifiableMessage(chatMessage, contact)
} notifiable.messages.add(notifiableMessage)
} }
} }
@ -660,17 +646,60 @@ class NotificationsManager(private val context: Context) {
val address = room.peerAddress.asStringUriOnly() val address = room.peerAddress.asStringUriOnly()
var notifiable: Notifiable? = chatNotificationsMap[address] var notifiable: Notifiable? = chatNotificationsMap[address]
if (notifiable == null) { if (notifiable == null) {
notifiable = Notifiable(lastNotificationId) notifiable = Notifiable(getNotificationIdForChat(room))
notifiable.myself = LinphoneUtils.getDisplayName(room.localAddress) notifiable.myself = LinphoneUtils.getDisplayName(room.localAddress)
notifiable.localIdentity = room.localAddress.asStringUriOnly() notifiable.localIdentity = room.localAddress.asStringUriOnly()
notifiable.remoteAddress = room.peerAddress.asStringUriOnly() notifiable.remoteAddress = room.peerAddress.asStringUriOnly()
lastNotificationId += 1
chatNotificationsMap[address] = notifiable chatNotificationsMap[address] = notifiable
} }
return notifiable return notifiable
} }
private fun getNotifiableMessage(message: ChatMessage, contact: Contact?): NotifiableMessage {
val pictureUri = contact?.getContactThumbnailPictureUri()
val roundPicture = ImageUtils.getRoundBitmapFromUri(context, pictureUri)
val displayName = contact?.fullName ?: LinphoneUtils.getDisplayName(message.fromAddress)
var text: String = message.contents.find { content -> content.isText }?.utf8Text ?: ""
if (text.isEmpty()) {
for (content in message.contents) {
text += content.name
}
}
val notifiableMessage = NotifiableMessage(
text,
contact,
displayName,
message.time,
senderAvatar = roundPicture,
isOutgoing = message.isOutgoing
)
for (content in message.contents) {
if (content.isFile) {
val path = content.filePath
if (path != null) {
val contentUri: Uri = FileUtils.getPublicFilePath(context, path)
val filePath: String = contentUri.toString()
val extension = FileUtils.getExtensionFromFileName(filePath)
if (extension.isNotEmpty()) {
val mime =
MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension)
notifiableMessage.filePath = contentUri
notifiableMessage.fileMime = mime
Log.i("[Notifications Manager] Added file $contentUri with MIME $mime to notification")
} else {
Log.e("[Notifications Manager] Couldn't find extension for incoming message with file $path")
}
}
}
}
return notifiableMessage
}
private fun displayReplyMessageNotification(message: ChatMessage, notifiable: Notifiable) { private fun displayReplyMessageNotification(message: ChatMessage, notifiable: Notifiable) {
Log.i("[Notifications Manager] Updating message notification with reply for notification ${notifiable.notificationId}") Log.i("[Notifications Manager] Updating message notification with reply for notification ${notifiable.notificationId}")