Added createPerson helper function

This commit is contained in:
Juha Heinanen
2026-05-03 20:44:16 +03:00
parent d08d0d1ea8
commit c0cb620a93

View File

@ -1329,45 +1329,15 @@ class BaresipService: Service() {
ua.account.unreadMessages = true ua.account.unreadMessages = true
if (!Utils.isVisible()) { if (!Utils.isVisible()) {
// common flags
val piFlags = PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT val piFlags = PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
// message show
val intent = Intent(applicationContext, MainActivity::class.java) val intent = Intent(applicationContext, MainActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP or intent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP or
Intent.FLAG_ACTIVITY_NEW_TASK Intent.FLAG_ACTIVITY_NEW_TASK
intent.putExtra("action", "message show").putExtra("uap", uap).putExtra("peer", peerUri) intent.putExtra("action", "message show").putExtra("uap", uap).putExtra("peer", peerUri)
val pi = PendingIntent.getActivity(applicationContext, MESSAGE_REQ_CODE, intent, piFlags) val pi = PendingIntent.getActivity(applicationContext, MESSAGE_REQ_CODE, intent, piFlags)
// message notification builder val sender = createPerson(this, peerUri, ua.account)
val sender = Utils.friendlyUri(this, peerUri, ua.account)
val senderContact = Contact.findContact(peerUri)
val personBuilder = Person.Builder().setName(sender)
val contactColor = senderContact?.color() ?: "#B0B0B0"
val initial = if (sender.isNotEmpty()) sender.take(1) else "?"
val textAvatarBitmap = Utils.createTextAvatar(initial,contactColor)
var icon = IconCompat.createWithBitmap(textAvatarBitmap)
if (senderContact is Contact.BaresipContact) {
if (senderContact.avatarImage != null)
icon = IconCompat.createWithBitmap(senderContact.avatarImage!!.toCircle())
}
else if (senderContact is Contact.AndroidContact) {
if (senderContact.thumbnailUri != null) {
try {
val source = ImageDecoder.createSource(contentResolver,
senderContact.thumbnailUri!!)
val bitmap = ImageDecoder.decodeBitmap(source) { decoder, _, _ ->
decoder.allocator = ImageDecoder.ALLOCATOR_SOFTWARE
// decoder.setTargetSize(256, 256)
}
icon = IconCompat.createWithBitmap(bitmap.toCircle())
} catch (e: Exception) {
Log.e(TAG, "Failed to load Android contact avatar: $e")
}
}
}
val senderPerson = personBuilder.setName(sender).setIcon(icon).build()
val localUserPerson = Person.Builder() val localUserPerson = Person.Builder()
.setName(getString(R.string.you)) .setName(getString(R.string.you))
.setKey(ua.account.aor) .setKey(ua.account.aor)
@ -1375,7 +1345,8 @@ class BaresipService: Service() {
val messagingStyle = MessagingStyle(localUserPerson) val messagingStyle = MessagingStyle(localUserPerson)
.setConversationTitle(null) .setConversationTitle(null)
.setGroupConversation(false) .setGroupConversation(false)
.addMessage(text, timeStamp, senderPerson) .addMessage(text, timeStamp, sender)
val nb = NotificationCompat.Builder(this, HIGH_CHANNEL_ID) val nb = NotificationCompat.Builder(this, HIGH_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification_message) .setSmallIcon(R.drawable.ic_notification_message)
.setColor(ContextCompat.getColor(this, R.color.colorPrimary)) .setColor(ContextCompat.getColor(this, R.color.colorPrimary))
@ -1385,15 +1356,15 @@ class BaresipService: Service() {
.setStyle(messagingStyle) .setStyle(messagingStyle)
.setCategory(NotificationCompat.CATEGORY_MESSAGE) .setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setPriority(NotificationCompat.PRIORITY_HIGH) .setPriority(NotificationCompat.PRIORITY_HIGH)
.addPerson(senderPerson) .addPerson(sender)
// message inline reply
val remoteInput = RemoteInput.Builder(KEY_TEXT_REPLY) val remoteInput = RemoteInput.Builder(KEY_TEXT_REPLY)
.setLabel(getString(R.string.reply)) .setLabel(getString(R.string.reply))
.build() .build()
val directReplyIntent = Intent(this, BaresipService::class.java) val directReplyIntent = Intent(this, BaresipService::class.java)
directReplyIntent.action = "Message Inline Reply" directReplyIntent.action = "Message Inline Reply"
directReplyIntent.putExtra("uap", uap).putExtra("peer", peerUri).putExtra("time", timeStamp) directReplyIntent.putExtra("uap", uap)
.putExtra("peer", peerUri).putExtra("time", timeStamp)
val directReplyPendingIntent = PendingIntent.getService( val directReplyPendingIntent = PendingIntent.getService(
this, this,
DIRECT_REPLY_REQ_CODE, DIRECT_REPLY_REQ_CODE,
@ -1409,7 +1380,6 @@ class BaresipService: Service() {
.setSemanticAction(NotificationCompat.Action.SEMANTIC_ACTION_REPLY). .setSemanticAction(NotificationCompat.Action.SEMANTIC_ACTION_REPLY).
build() build()
// message save
val saveIntent = Intent(this, BaresipService::class.java) val saveIntent = Intent(this, BaresipService::class.java)
saveIntent.action = "Message Save" saveIntent.action = "Message Save"
saveIntent.putExtra("uap", uap).putExtra("time", timeStampString) saveIntent.putExtra("uap", uap).putExtra("time", timeStampString)
@ -1420,7 +1390,6 @@ class BaresipService: Service() {
savePendingIntent savePendingIntent
).build() ).build()
// message delete
val deleteIntent = Intent(this, BaresipService::class.java) val deleteIntent = Intent(this, BaresipService::class.java)
deleteIntent.action = "Message Delete" deleteIntent.action = "Message Delete"
deleteIntent.putExtra("uap", uap).putExtra("time", timeStampString) deleteIntent.putExtra("uap", uap).putExtra("time", timeStampString)
@ -1803,30 +1772,6 @@ class BaresipService: Service() {
intent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK intent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK
val pi = PendingIntent.getActivity(applicationContext, CALL_REQ_CODE, intent, piFlags) val pi = PendingIntent.getActivity(applicationContext, CALL_REQ_CODE, intent, piFlags)
val caller = Utils.friendlyUri(this, peerUri, ua.account)
val callerContact = Contact.findContact(peerUri)
val contactColor = callerContact?.color() ?: "#B0B0B0"
val initial = if (caller.isNotEmpty()) caller.take(1) else "?"
val textAvatarBitmap = Utils.createTextAvatar(initial, contactColor)
var icon = IconCompat.createWithBitmap(textAvatarBitmap)
if (callerContact is Contact.BaresipContact) {
if (callerContact.avatarImage != null)
icon = IconCompat.createWithBitmap(callerContact.avatarImage!!.toCircle())
}
else if (callerContact is Contact.AndroidContact)
if (callerContact.thumbnailUri != null)
try {
val source = ImageDecoder.createSource(contentResolver, callerContact.thumbnailUri!!)
val bitmap = ImageDecoder.decodeBitmap(source) { decoder, _, _ ->
decoder.allocator = ImageDecoder.ALLOCATOR_SOFTWARE
}
icon = IconCompat.createWithBitmap(bitmap.toCircle())
} catch (e: Exception) {
Log.e(TAG, "Failed to load Android contact avatar: $e")
}
val person = Person.Builder().setName(caller).setIcon(icon).build()
val diverterUri = call.diverterUri() val diverterUri = call.diverterUri()
val contentText = if (diverterUri != "") val contentText = if (diverterUri != "")
"${getString(R.string.is_calling)} " + "${getString(R.string.is_calling)} " +
@ -1834,7 +1779,7 @@ class BaresipService: Service() {
"${Utils.friendlyUri(this, diverterUri, ua.account)})" "${Utils.friendlyUri(this, diverterUri, ua.account)})"
else else
getString(R.string.is_calling) getString(R.string.is_calling)
val caller = createPerson(this, peerUri, ua.account)
val nb = NotificationCompat.Builder(this, HIGH_CHANNEL_ID) val nb = NotificationCompat.Builder(this, HIGH_CHANNEL_ID)
nb.setSmallIcon(R.drawable.ic_notification_call) nb.setSmallIcon(R.drawable.ic_notification_call)
.setColor(ContextCompat.getColor(this, R.color.colorPrimary)) .setColor(ContextCompat.getColor(this, R.color.colorPrimary))
@ -1847,24 +1792,62 @@ class BaresipService: Service() {
.setShowWhen(true) .setShowWhen(true)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC) .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setPriority(NotificationCompat.PRIORITY_MAX) .setPriority(NotificationCompat.PRIORITY_MAX)
.addPerson(person) .addPerson(caller)
.setFullScreenIntent(pi, true) .setFullScreenIntent(pi, true)
val answerIntent = Intent(applicationContext, MainActivity::class.java) val answerIntent = Intent(applicationContext, MainActivity::class.java)
.putExtra("action", "call answer") .putExtra("action", "call answer")
.putExtra("callp", callp) .putExtra("callp", callp)
val api = PendingIntent.getActivity(applicationContext, ANSWER_REQ_CODE, answerIntent, piFlags) val api = PendingIntent.getActivity(
applicationContext,
ANSWER_REQ_CODE,
answerIntent,
piFlags
)
val rejectIntent = Intent(this, BaresipService::class.java) val rejectIntent = Intent(this, BaresipService::class.java)
rejectIntent.action = "Call Reject" rejectIntent.action = "Call Reject"
rejectIntent.putExtra("callp", callp) rejectIntent.putExtra("callp", callp)
val rpi = PendingIntent.getService(this, REJECT_REQ_CODE, rejectIntent, piFlags) val rpi = PendingIntent.getService(this, REJECT_REQ_CODE, rejectIntent, piFlags)
nb.setStyle(NotificationCompat.CallStyle.forIncomingCall(person, rpi, api)) nb.setStyle(NotificationCompat.CallStyle.forIncomingCall(
caller,
rpi,
api
))
nm.notify(CALL_NOTIFICATION_ID, nb.build()) nm.notify(CALL_NOTIFICATION_ID, nb.build())
} }
postServiceEvent(ServiceEvent("call incoming", arrayListOf(ua.uap, callp), System.nanoTime())) postServiceEvent(ServiceEvent(
"call incoming",
arrayListOf(ua.uap, callp),
System.nanoTime()
))
}
private fun createPerson(ctx: Context, peerUri: String, account: Account): Person {
val peer = Utils.friendlyUri(ctx, peerUri, account)
val contact = Contact.findContact(peerUri)
val contactColor = contact?.color() ?: "#B0B0B0"
val initial = if (peer.isNotEmpty()) peer.take(1) else "?"
val textAvatarBitmap = Utils.createTextAvatar(initial, contactColor)
var icon = IconCompat.createWithBitmap(textAvatarBitmap)
if (contact is Contact.BaresipContact) {
if (contact.avatarImage != null)
icon = IconCompat.createWithBitmap(contact.avatarImage!!.toCircle())
}
else if (contact is Contact.AndroidContact)
if (contact.thumbnailUri != null)
try {
val source = ImageDecoder.createSource(contentResolver, contact.thumbnailUri!!)
val bitmap = ImageDecoder.decodeBitmap(source) { decoder, _, _ ->
decoder.allocator = ImageDecoder.ALLOCATOR_SOFTWARE
}
icon = IconCompat.createWithBitmap(bitmap.toCircle())
} catch (e: Exception) {
Log.e(TAG, "Failed to load Android contact avatar: $e")
}
return Person.Builder().setName(peer).setIcon(icon).build()
} }
private fun startRinging() { private fun startRinging() {