message related enhancements

This commit is contained in:
Juha Heinanen
2018-07-25 14:00:17 +03:00
parent 80edbbc1d7
commit b016811a16
8 changed files with 96 additions and 28 deletions
+2 -2
View File
@@ -8,8 +8,8 @@ android {
applicationId = 'com.tutpro.baresip' applicationId = 'com.tutpro.baresip'
minSdkVersion 21 minSdkVersion 21
targetSdkVersion 26 targetSdkVersion 26
versionCode = 8 versionCode = 9
versionName = '1.0' versionName = '2.0'
ndk { ndk {
abiFilters 'armeabi-v7a' abiFilters 'armeabi-v7a'
} }
+24 -9
View File
@@ -234,17 +234,30 @@ static void send_resp_handler(int err, const struct sip_msg *msg, void *arg)
{ {
(void)arg; (void)arg;
LOGD("received message response %u\n", msg->scode);
if (err) { if (err) {
(void)re_fprintf(stderr, " \x1b[31m%m\x1b[;m\n", err); LOGD("send_response_handler received error %d\n", err);
return; return;
} }
if (msg->scode >= 300) { LOGD("send_response_handler received response %u at %s\n", msg->scode, (char *)arg);
(void)re_fprintf(stderr, " \x1b[31m%u %r\x1b[;m\n",
msg->scode, &msg->reason); UpdateContext *pctx = (UpdateContext*)(&g_ctx);
JavaVM *javaVM = pctx->javaVM;
JNIEnv *env;
jint res = (*javaVM)->GetEnv(javaVM, (void**)&env, JNI_VERSION_1_6);
if (res != JNI_OK) {
res = (*javaVM)->AttachCurrentThread(javaVM, &env, NULL);
if (JNI_OK != res) {
LOGE("Failed to AttachCurrentThread, ErrorCode = %d", res);
return;
}
} }
jmethodID methodId = (*env)->GetMethodID(env, pctx->mainActivityClz,
"messageResponse", "(ILjava/lang/String;)V");
jstring javaTime = (*env)->NewStringUTF(env, (char *)arg);
(*env)->CallVoidMethod(env, pctx->mainActivityObj, methodId, msg->scode, javaTime);
(*env)->DeleteLocalRef(env, javaTime);
} }
#include <unistd.h> #include <unistd.h>
@@ -966,15 +979,17 @@ Java_com_tutpro_baresip_MainActivity_call_1send_1digit(JNIEnv *env, jobject thiz
JNIEXPORT jint JNICALL JNIEXPORT jint JNICALL
Java_com_tutpro_baresip_MessageActivity_message_1send(JNIEnv *env, jobject thiz, jstring javaUA, Java_com_tutpro_baresip_MessageActivity_message_1send(JNIEnv *env, jobject thiz, jstring javaUA,
jstring javaPeer, jstring javaMsg) { jstring javaPeer, jstring javaMsg,
jstring javaTime) {
struct ua *ua; struct ua *ua;
const char *native_ua = (*env)->GetStringUTFChars(env, javaUA, 0); const char *native_ua = (*env)->GetStringUTFChars(env, javaUA, 0);
const char *native_peer = (*env)->GetStringUTFChars(env, javaPeer, 0); const char *native_peer = (*env)->GetStringUTFChars(env, javaPeer, 0);
const char *native_msg = (*env)->GetStringUTFChars(env, javaMsg, 0); const char *native_msg = (*env)->GetStringUTFChars(env, javaMsg, 0);
const char *native_time = (*env)->GetStringUTFChars(env, javaTime, 0);
LOGD("sending message from ua %s to %s\n", native_ua, native_peer); LOGD("sending message from ua %s to %s at %s\n", native_ua, native_peer, native_time);
ua = (struct ua *)strtoul(native_ua, NULL, 10); ua = (struct ua *)strtoul(native_ua, NULL, 10);
int err = message_send(ua, native_peer, native_msg, send_resp_handler, NULL); int err = message_send(ua, native_peer, native_msg, send_resp_handler, (void *)native_time);
if (err) { if (err) {
LOGW("message_send failed with error %d\n", err); LOGW("message_send failed with error %d\n", err);
} }
@@ -319,14 +319,13 @@ class BaresipService: Service() {
@Keep @Keep
fun messageEvent(uap: String, peer: String, msg: ByteArray) { fun messageEvent(uap: String, peer: String, msg: ByteArray) {
Log.d(LOG_TAG, "message event $uap/$peer")
var s = "Decoding of message failed!" var s = "Decoding of message failed!"
try { try {
s = String(msg, StandardCharsets.UTF_8) s = String(msg, StandardCharsets.UTF_8)
Log.d(LOG_TAG, "UTF-8 $s")
} catch (e: Exception) { } catch (e: Exception) {
Log.e(LOG_TAG, "UTF-8 decode failed") Log.e(LOG_TAG, "UTF-8 decode failed")
} }
Log.d(LOG_TAG, "Message event $uap/$peer/$s")
val timeStamp = System.currentTimeMillis().toString() val timeStamp = System.currentTimeMillis().toString()
if (!Utils.isVisible()) { if (!Utils.isVisible()) {
val cnb = NotificationCompat.Builder(this, HIGH_CHANNEL_ID) val cnb = NotificationCompat.Builder(this, HIGH_CHANNEL_ID)
@@ -383,6 +382,15 @@ class BaresipService: Service() {
LocalBroadcastManager.getInstance(this).sendBroadcast(intent) LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
} }
@Keep
fun messageResponse(responseCode: Int, time: String) {
Log.d(LOG_TAG, "Message response $responseCode at $time")
val intent = Intent("message response")
intent.putExtra("response code", responseCode)
intent.putExtra("time", time)
LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
}
private fun updateStatusNotification() { private fun updateStatusNotification() {
val contentView = RemoteViews(getPackageName(), R.layout.status_notification) val contentView = RemoteViews(getPackageName(), R.layout.status_notification)
for (i: Int in 0 .. 5) { for (i: Int in 0 .. 5) {
@@ -589,12 +589,12 @@ class MainActivity : AppCompatActivity() {
val new_message = Message(aor, peer_uri, R.drawable.arrow_down_green, msg, val new_message = Message(aor, peer_uri, R.drawable.arrow_down_green, msg,
time.toLong(), true) time.toLong(), true)
Log.d("Baresip", "Incoming message $aor/$peer_uri/$msg") Log.d("Baresip", "Incoming message $aor/$peer_uri/$msg")
messages.add(new_message) MessagesActivity.addMessage(new_message)
if (Utils.isVisible()) { if (Utils.isVisible()) {
if (ua != uas[aorSpinner.selectedItemPosition]) if (ua != uas[aorSpinner.selectedItemPosition])
aorSpinner.setSelection(account_index) aorSpinner.setSelection(account_index)
val i = Intent(applicationContext, MessagesActivity::class.java) val i = Intent(applicationContext, MessagesActivity::class.java)
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
val b = Bundle() val b = Bundle()
b.putString("aor", ua.account.aor) b.putString("aor", ua.account.aor)
i.putExtras(b) i.putExtras(b)
@@ -642,6 +642,7 @@ class MainActivity : AppCompatActivity() {
} }
b.putString("aor", aor) b.putString("aor", aor)
b.putString("peer", intent.getStringExtra("peer")) b.putString("peer", intent.getStringExtra("peer"))
b.putBoolean("reply", true)
i.putExtras(b) i.putExtras(b)
startActivityForResult(i, MESSAGES_CODE) startActivityForResult(i, MESSAGES_CODE)
} }
@@ -663,13 +664,13 @@ class MainActivity : AppCompatActivity() {
override fun onPause() { override fun onPause() {
super.onPause() super.onPause()
Log.d("Baresip", "Paused") // Log.d("Baresip", "Paused")
visible = false visible = false
} }
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
Log.d("Baresip", "Resumed") // Log.d("Baresip", "Resumed")
imm.hideSoftInputFromWindow(callee.windowToken, 0) imm.hideSoftInputFromWindow(callee.windowToken, 0)
visible = true visible = true
if ((answerCall != "") && (layout.childCount > 8)) { if ((answerCall != "") && (layout.childCount > 8)) {
@@ -2,6 +2,6 @@ package com.tutpro.baresip
import java.io.Serializable import java.io.Serializable
class Message(val aor: String, val peerURI: String, val direction: Int, val message: String, class Message(val aor: String, val peerURI: String, var direction: Int, val message: String,
val timeStamp: Long, var new: Boolean): Serializable { val timeStamp: Long, var new: Boolean): Serializable {
} }
@@ -24,6 +24,7 @@ class MessageActivity : AppCompatActivity() {
val aor = intent.extras.getString("aor") val aor = intent.extras.getString("aor")
val peerURI = intent.extras.getString("peer") val peerURI = intent.extras.getString("peer")
val reply = intent.extras.getBoolean("reply")
val ua = Account.findUa(aor) val ua = Account.findUa(aor)
if (ua == null) { if (ua == null) {
@@ -38,15 +39,16 @@ class MessageActivity : AppCompatActivity() {
receiver = findViewById(R.id.receiver) as AutoCompleteTextView receiver = findViewById(R.id.receiver) as AutoCompleteTextView
imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
if (reply)
if (peerURI == "") { title.text = "Message reply to ..."
else
title.text = "Message to ..." title.text = "Message to ..."
if (peerURI == "") {
receiver.threshold = 2 receiver.threshold = 2
receiver.setAdapter(ArrayAdapter(this, android.R.layout.select_dialog_item, receiver.setAdapter(ArrayAdapter(this, android.R.layout.select_dialog_item,
ContactsActivity.contacts.map { Contact -> Contact.name })) ContactsActivity.contacts.map { Contact -> Contact.name }))
receiver.requestFocus() receiver.requestFocus()
} else { } else {
title.text = "Reply to ..."
receiver.setText(ContactsActivity.contactName(peerURI), false) receiver.setText(ContactsActivity.contactName(peerURI), false)
message.requestFocus() message.requestFocus()
} }
@@ -66,14 +68,15 @@ class MessageActivity : AppCompatActivity() {
if (msg.length > 0) { if (msg.length > 0) {
imm.hideSoftInputFromWindow(receiver.getWindowToken(), 0) imm.hideSoftInputFromWindow(receiver.getWindowToken(), 0)
imm.hideSoftInputFromWindow(message.getWindowToken(), 0) imm.hideSoftInputFromWindow(message.getWindowToken(), 0)
val res = message_send(ua!!.uap, uri, msg) val time = System.currentTimeMillis()
val res = message_send(ua!!.uap, uri, msg, time.toString())
if (res != 0) { if (res != 0) {
Toast.makeText(getApplicationContext(), "Sending of message failed!", Toast.makeText(getApplicationContext(), "Sending of message failed!",
Toast.LENGTH_SHORT).show() Toast.LENGTH_SHORT).show()
} else { } else {
val new_message = Message(aor, uri, R.drawable.arrow_up_green, msg, val new_message = Message(aor, uri, R.drawable.arrow_up_yellow, msg, time,
System.currentTimeMillis(), false) false)
MainActivity.messages.add(new_message) MessagesActivity.addMessage(new_message)
MessagesActivity.uaMessages.add(0, new_message) MessagesActivity.uaMessages.add(0, new_message)
val i = Intent() val i = Intent()
setResult(Activity.RESULT_OK, i) setResult(Activity.RESULT_OK, i)
@@ -97,6 +100,6 @@ class MessageActivity : AppCompatActivity() {
return true return true
} }
external fun message_send(uap: String, peer_uri: String, message: String) : Int external fun message_send(uap: String, peer_uri: String, message: String, time: String) : Int
} }
@@ -1,9 +1,9 @@
package com.tutpro.baresip package com.tutpro.baresip
import android.app.Activity import android.app.Activity
import android.content.DialogInterface import android.content.*
import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.support.v4.content.LocalBroadcastManager
import android.support.v7.app.AlertDialog import android.support.v7.app.AlertDialog
import android.support.v7.app.AppCompatActivity import android.support.v7.app.AppCompatActivity
import android.util.Log import android.util.Log
@@ -22,6 +22,7 @@ class MessagesActivity: AppCompatActivity() {
internal lateinit var aor: String internal lateinit var aor: String
internal lateinit var mlAdapter: MessageListAdapter internal lateinit var mlAdapter: MessageListAdapter
internal lateinit var plusButton: ImageButton internal lateinit var plusButton: ImageButton
internal lateinit var serviceEventReceiver: BroadcastReceiver
public override fun onCreate(savedInstanceState: Bundle?) { public override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@@ -42,6 +43,7 @@ class MessagesActivity: AppCompatActivity() {
val b = Bundle() val b = Bundle()
b.putString("aor", aor) b.putString("aor", aor)
b.putString("peer", uaMessages[pos].peerURI) b.putString("peer", uaMessages[pos].peerURI)
b.putBoolean("reply", uaMessages[pos].direction == R.drawable.arrow_down_green)
i.putExtras(b) i.putExtras(b)
startActivityForResult(i, MainActivity.MESSAGE_CODE) startActivityForResult(i, MainActivity.MESSAGE_CODE)
} }
@@ -89,16 +91,27 @@ class MessagesActivity: AppCompatActivity() {
val b = Bundle() val b = Bundle()
b.putString("aor", aor) b.putString("aor", aor)
b.putString("peer", peer) b.putString("peer", peer)
b.putBoolean("reply", true)
i.putExtras(b) i.putExtras(b)
startActivityForResult(i, MainActivity.MESSAGE_CODE) startActivityForResult(i, MainActivity.MESSAGE_CODE)
} }
serviceEventReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
handleMessageResponse(intent.getIntExtra("response code", 0),
intent.getStringExtra("time"))
}
}
LocalBroadcastManager.getInstance(this).registerReceiver(serviceEventReceiver,
IntentFilter("message response"))
} }
override fun onOptionsItemSelected(item: MenuItem): Boolean { override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) { when (item.itemId) {
android.R.id.home -> { android.R.id.home -> {
Log.d("Baresip", "Back array was pressed at Messages") Log.d("Baresip", "Back array was pressed at Messages")
saveMessages()
val i = Intent() val i = Intent()
setResult(Activity.RESULT_CANCELED, i) setResult(Activity.RESULT_CANCELED, i)
finish() finish()
@@ -108,6 +121,7 @@ class MessagesActivity: AppCompatActivity() {
} }
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
Log.d("Baresip", "onActivityResult at Messages")
when (requestCode) { when (requestCode) {
MainActivity.MESSAGE_CODE -> { MainActivity.MESSAGE_CODE -> {
if (resultCode == RESULT_OK) { if (resultCode == RESULT_OK) {
@@ -117,6 +131,22 @@ class MessagesActivity: AppCompatActivity() {
} }
} }
private fun handleMessageResponse(responseCode: Int, time: String) {
if (responseCode < 300)
updateMessageDirection(time.toLong(), R.drawable.arrow_up_green)
else
updateMessageDirection(time.toLong(), R.drawable.arrow_up_red)
mlAdapter.notifyDataSetChanged()
}
fun updateMessageDirection(timeStamp: Long, direction: Int) {
for (m in uaMessages.reversed())
if (m.timeStamp == timeStamp) {
m.direction = direction
return
}
}
private fun uaMessages(aor: String) : ArrayList<Message> { private fun uaMessages(aor: String) : ArrayList<Message> {
val res = ArrayList<Message>() val res = ArrayList<Message>()
for (m in MainActivity.messages.reversed()) { for (m in MainActivity.messages.reversed()) {
@@ -149,6 +179,17 @@ class MessagesActivity: AppCompatActivity() {
} }
} }
fun addMessage(message: Message) {
if ((MainActivity.messages.filter{it.aor == message.aor}).size >=
MainActivity.MESSAGE_HISTORY_SIZE)
for (i in MainActivity.messages.indices)
if (MainActivity.messages[i].aor == message.aor) {
MainActivity.messages.removeAt(i)
break
}
MainActivity.messages.add(message)
}
fun saveMessages() { fun saveMessages() {
val file = File(MainActivity.filesPath, "messages") val file = File(MainActivity.filesPath, "messages")
try { try {
Binary file not shown.

After

Width:  |  Height:  |  Size: 297 B