From c0cb620a936f07ecfeb12f603c7063b411a63cd0 Mon Sep 17 00:00:00 2001 From: Juha Heinanen Date: Sun, 3 May 2026 20:44:16 +0300 Subject: [PATCH] Added createPerson helper function --- .../com/tutpro/baresip/BaresipService.kt | 115 ++++++++---------- 1 file changed, 49 insertions(+), 66 deletions(-) diff --git a/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt b/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt index adb2b37f..10610182 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt @@ -1329,45 +1329,15 @@ class BaresipService: Service() { ua.account.unreadMessages = true if (!Utils.isVisible()) { - - // common flags val piFlags = PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT - // message show val intent = Intent(applicationContext, MainActivity::class.java) intent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK intent.putExtra("action", "message show").putExtra("uap", uap).putExtra("peer", peerUri) val pi = PendingIntent.getActivity(applicationContext, MESSAGE_REQ_CODE, intent, piFlags) - // message notification builder - 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 sender = createPerson(this, peerUri, ua.account) val localUserPerson = Person.Builder() .setName(getString(R.string.you)) .setKey(ua.account.aor) @@ -1375,7 +1345,8 @@ class BaresipService: Service() { val messagingStyle = MessagingStyle(localUserPerson) .setConversationTitle(null) .setGroupConversation(false) - .addMessage(text, timeStamp, senderPerson) + .addMessage(text, timeStamp, sender) + val nb = NotificationCompat.Builder(this, HIGH_CHANNEL_ID) .setSmallIcon(R.drawable.ic_notification_message) .setColor(ContextCompat.getColor(this, R.color.colorPrimary)) @@ -1385,15 +1356,15 @@ class BaresipService: Service() { .setStyle(messagingStyle) .setCategory(NotificationCompat.CATEGORY_MESSAGE) .setPriority(NotificationCompat.PRIORITY_HIGH) - .addPerson(senderPerson) + .addPerson(sender) - // message inline reply val remoteInput = RemoteInput.Builder(KEY_TEXT_REPLY) .setLabel(getString(R.string.reply)) .build() val directReplyIntent = Intent(this, BaresipService::class.java) 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( this, DIRECT_REPLY_REQ_CODE, @@ -1409,7 +1380,6 @@ class BaresipService: Service() { .setSemanticAction(NotificationCompat.Action.SEMANTIC_ACTION_REPLY). build() - // message save val saveIntent = Intent(this, BaresipService::class.java) saveIntent.action = "Message Save" saveIntent.putExtra("uap", uap).putExtra("time", timeStampString) @@ -1420,7 +1390,6 @@ class BaresipService: Service() { savePendingIntent ).build() - // message delete val deleteIntent = Intent(this, BaresipService::class.java) deleteIntent.action = "Message Delete" 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 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 contentText = if (diverterUri != "") "${getString(R.string.is_calling)} " + @@ -1834,7 +1779,7 @@ class BaresipService: Service() { "${Utils.friendlyUri(this, diverterUri, ua.account)})" else getString(R.string.is_calling) - + val caller = createPerson(this, peerUri, ua.account) val nb = NotificationCompat.Builder(this, HIGH_CHANNEL_ID) nb.setSmallIcon(R.drawable.ic_notification_call) .setColor(ContextCompat.getColor(this, R.color.colorPrimary)) @@ -1847,24 +1792,62 @@ class BaresipService: Service() { .setShowWhen(true) .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) .setPriority(NotificationCompat.PRIORITY_MAX) - .addPerson(person) + .addPerson(caller) .setFullScreenIntent(pi, true) val answerIntent = Intent(applicationContext, MainActivity::class.java) .putExtra("action", "call answer") .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) rejectIntent.action = "Call Reject" rejectIntent.putExtra("callp", callp) 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()) } - 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() {