From 35222b0a5d01201b7132b92b2976f3125a620371 Mon Sep 17 00:00:00 2001 From: Juha Heinanen Date: Fri, 5 May 2023 12:34:43 +0300 Subject: [PATCH] Pass also content-type of MESSAGE request to baresip service --- app/src/main/cpp/baresip.c | 15 +++++----- .../com/tutpro/baresip/BaresipService.kt | 28 ++++++++++++++----- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/app/src/main/cpp/baresip.c b/app/src/main/cpp/baresip.c index b8db32ac..e34c34f0 100644 --- a/app/src/main/cpp/baresip.c +++ b/app/src/main/cpp/baresip.c @@ -257,15 +257,13 @@ static void ua_event_handler(struct ua *ua, enum ua_event ev, static void message_handler(struct ua *ua, const struct pl *peer, const struct pl *ctype, struct mbuf *body, void *arg) { - (void)ctype; (void)arg; + char ctype_buf[128]; char peer_buf[256]; size_t size; - LOGD("got message '%.*s' from peer '%.*s'", (int)mbuf_get_left(body), mbuf_buf(body), - (int)peer->l, peer->p); if (snprintf(peer_buf, 256, "%.*s", (int)peer->l, peer->p) >= 256) { - LOGE("message peer is too long (max 255 charcaters)\n"); + LOGE("message peer is too long (max 255 characters)\n"); return; } @@ -277,8 +275,10 @@ static void message_handler(struct ua *ua, const struct pl *peer, const struct p return; } jmethodID methodId = (*env)->GetMethodID(env, g_ctx.mainActivityClz, "messageEvent", - "(JLjava/lang/String;[B)V"); + "(JLjava/lang/String;Ljava/lang/String;[B)V"); jstring jPeer = (*env)->NewStringUTF(env, peer_buf); + pl_strcpy(ctype, ctype_buf, 256); + jstring jCtype = (*env)->NewStringUTF(env, ctype_buf); jbyteArray jMsg; size = mbuf_get_left(body); jMsg = (*env)->NewByteArray(env, (jsize)size); @@ -289,8 +289,9 @@ static void message_handler(struct ua *ua, const struct pl *peer, const struct p void *temp = (*env)->GetPrimitiveArrayCritical(env, (jarray)jMsg, 0); memcpy(temp, mbuf_buf(body), size); (*env)->ReleasePrimitiveArrayCritical(env, jMsg, temp, 0); - LOGD("sending message %ld/%s/%.*s\n", (long)ua, peer_buf, (int)size, mbuf_buf(body)); - (*env)->CallVoidMethod(env, g_ctx.mainActivityObj, methodId, (jlong)ua, jPeer, jMsg); + LOGD("sending message %ld/%s/%s/%.*s\n", (long)ua, peer_buf, ctype_buf, (int)size, mbuf_buf(body)); + (*env)->CallVoidMethod(env, g_ctx.mainActivityObj, methodId, (jlong)ua, jPeer, jCtype, jMsg); + (*env)->DeleteLocalRef(env, jCtype); (*env)->DeleteLocalRef(env, jPeer); (*env)->DeleteLocalRef(env, jMsg); re_thread_enter(); diff --git a/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt b/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt index 890a8cbf..1ecd8e84 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt @@ -42,6 +42,7 @@ import androidx.media.AudioFocusRequestCompat import androidx.media.AudioManagerCompat import java.io.File import java.net.InetAddress +import java.nio.charset.Charset import java.nio.charset.StandardCharsets import java.util.* import kotlin.concurrent.schedule @@ -967,24 +968,35 @@ class BaresipService: Service() { @SuppressLint("UnspecifiedImmutableFlag") @Keep - fun messageEvent(uap: Long, peerUri: String, msg: ByteArray) { - var text = "Decoding of message failed!" - try { - text = String(msg, StandardCharsets.UTF_8) - } catch (e: Exception) { - Log.w(TAG, "UTF-8 decode failed") - } + fun messageEvent(uap: Long, peerUri: String, cType: String, msg: ByteArray) { + val ua = UserAgent.ofUap(uap) if (ua == null) { Log.w(TAG, "messageEvent did not find ua $uap") return } + + val charsetString = Utils.paramValue(cType.replace(" ", ""), "charset") + val charset = try { + Charset.forName(charsetString) + } catch (e: Exception) { + StandardCharsets.UTF_8 + } + val text = try { + String(msg, charset) + } catch (e: Exception) { + val error = "Decoding of message failed using charset $charset from $cType!" + Log.w(TAG, error) + error + } + val timeStamp = System.currentTimeMillis() val timeStampString = timeStamp.toString() Log.d(TAG, "Message event for $uap from $peerUri at $timeStampString") Message(ua.account.aor, peerUri, text, timeStamp, MESSAGE_DOWN, 0, "", true).add() Message.save() ua.account.unreadMessages = true + if (!Utils.isVisible()) { val piFlags = PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT val intent = Intent(applicationContext, MainActivity::class.java) @@ -1033,8 +1045,10 @@ class BaresipService: Service() { nm.notify(MESSAGE_NOTIFICATION_ID, nb.build()) return } + if (nm.currentInterruptionFilter <= NotificationManager.INTERRUPTION_FILTER_ALL) nt.play() + postServiceEvent(ServiceEvent("message show", arrayListOf(uap, peerUri), System.nanoTime())) }