Pass also content-type of MESSAGE request to baresip service

This commit is contained in:
Juha Heinanen
2023-05-05 12:34:43 +03:00
parent 12159d8b95
commit 35222b0a5d
2 changed files with 29 additions and 14 deletions
+8 -7
View File
@@ -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, static void message_handler(struct ua *ua, const struct pl *peer, const struct pl *ctype,
struct mbuf *body, void *arg) struct mbuf *body, void *arg)
{ {
(void)ctype;
(void)arg; (void)arg;
char ctype_buf[128];
char peer_buf[256]; char peer_buf[256];
size_t size; 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) { 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; return;
} }
@@ -277,8 +275,10 @@ static void message_handler(struct ua *ua, const struct pl *peer, const struct p
return; return;
} }
jmethodID methodId = (*env)->GetMethodID(env, g_ctx.mainActivityClz, "messageEvent", 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); jstring jPeer = (*env)->NewStringUTF(env, peer_buf);
pl_strcpy(ctype, ctype_buf, 256);
jstring jCtype = (*env)->NewStringUTF(env, ctype_buf);
jbyteArray jMsg; jbyteArray jMsg;
size = mbuf_get_left(body); size = mbuf_get_left(body);
jMsg = (*env)->NewByteArray(env, (jsize)size); 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); void *temp = (*env)->GetPrimitiveArrayCritical(env, (jarray)jMsg, 0);
memcpy(temp, mbuf_buf(body), size); memcpy(temp, mbuf_buf(body), size);
(*env)->ReleasePrimitiveArrayCritical(env, jMsg, temp, 0); (*env)->ReleasePrimitiveArrayCritical(env, jMsg, temp, 0);
LOGD("sending message %ld/%s/%.*s\n", (long)ua, peer_buf, (int)size, mbuf_buf(body)); 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, jMsg); (*env)->CallVoidMethod(env, g_ctx.mainActivityObj, methodId, (jlong)ua, jPeer, jCtype, jMsg);
(*env)->DeleteLocalRef(env, jCtype);
(*env)->DeleteLocalRef(env, jPeer); (*env)->DeleteLocalRef(env, jPeer);
(*env)->DeleteLocalRef(env, jMsg); (*env)->DeleteLocalRef(env, jMsg);
re_thread_enter(); re_thread_enter();
@@ -42,6 +42,7 @@ import androidx.media.AudioFocusRequestCompat
import androidx.media.AudioManagerCompat import androidx.media.AudioManagerCompat
import java.io.File import java.io.File
import java.net.InetAddress import java.net.InetAddress
import java.nio.charset.Charset
import java.nio.charset.StandardCharsets import java.nio.charset.StandardCharsets
import java.util.* import java.util.*
import kotlin.concurrent.schedule import kotlin.concurrent.schedule
@@ -967,24 +968,35 @@ class BaresipService: Service() {
@SuppressLint("UnspecifiedImmutableFlag") @SuppressLint("UnspecifiedImmutableFlag")
@Keep @Keep
fun messageEvent(uap: Long, peerUri: String, msg: ByteArray) { fun messageEvent(uap: Long, peerUri: String, cType: 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")
}
val ua = UserAgent.ofUap(uap) val ua = UserAgent.ofUap(uap)
if (ua == null) { if (ua == null) {
Log.w(TAG, "messageEvent did not find ua $uap") Log.w(TAG, "messageEvent did not find ua $uap")
return 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 timeStamp = System.currentTimeMillis()
val timeStampString = timeStamp.toString() val timeStampString = timeStamp.toString()
Log.d(TAG, "Message event for $uap from $peerUri at $timeStampString") Log.d(TAG, "Message event for $uap from $peerUri at $timeStampString")
Message(ua.account.aor, peerUri, text, timeStamp, MESSAGE_DOWN, 0, "", true).add() Message(ua.account.aor, peerUri, text, timeStamp, MESSAGE_DOWN, 0, "", true).add()
Message.save() Message.save()
ua.account.unreadMessages = true ua.account.unreadMessages = true
if (!Utils.isVisible()) { if (!Utils.isVisible()) {
val piFlags = PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT val piFlags = PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
val intent = Intent(applicationContext, MainActivity::class.java) val intent = Intent(applicationContext, MainActivity::class.java)
@@ -1033,8 +1045,10 @@ class BaresipService: Service() {
nm.notify(MESSAGE_NOTIFICATION_ID, nb.build()) nm.notify(MESSAGE_NOTIFICATION_ID, nb.build())
return return
} }
if (nm.currentInterruptionFilter <= NotificationManager.INTERRUPTION_FILTER_ALL) if (nm.currentInterruptionFilter <= NotificationManager.INTERRUPTION_FILTER_ALL)
nt.play() nt.play()
postServiceEvent(ServiceEvent("message show", arrayListOf(uap, peerUri), System.nanoTime())) postServiceEvent(ServiceEvent("message show", arrayListOf(uap, peerUri), System.nanoTime()))
} }