introduced support for sending and receiving messages

This commit is contained in:
Juha Heinanen
2018-07-22 20:27:57 +03:00
parent 2c7c8ec8b9
commit 679c6855e0
20 changed files with 792 additions and 51 deletions
@@ -21,6 +21,10 @@ import java.io.ObjectInputStream
import android.support.v4.content.LocalBroadcastManager
import android.os.Build
import android.support.v4.app.NotificationCompat.VISIBILITY_PRIVATE
import java.nio.charset.StandardCharsets
import kotlin.experimental.and
import android.R.attr.name
import java.nio.charset.Charset
class BaresipService: Service() {
@@ -120,6 +124,21 @@ class BaresipService: Service() {
}
}
file = File(path, "messages")
if (file.exists()) {
try {
val fis = FileInputStream(file)
val ois = ObjectInputStream(fis)
@SuppressWarnings("unchecked")
MainActivity.messages = ois.readObject() as ArrayList<Message>
Log.d(LOG_TAG, "Restored ${MainActivity.messages.size} messages")
ois.close()
fis.close()
} catch (e: Exception) {
Log.w(LOG_TAG, "InputStream exception: - " + e.toString())
}
}
ContactsActivity.generateContacts(path + "/contacts")
Thread(Runnable { baresipStart(path) }).start()
@@ -191,7 +210,7 @@ class BaresipService: Service() {
}
@Keep
fun addUA(uap: String) {
fun uaAdd(uap: String) {
Log.d(LOG_TAG, "addUA at BaresipService")
val ua = UserAgent(uap)
MainActivity.uas.add(ua)
@@ -205,14 +224,13 @@ class BaresipService: Service() {
}
val intent = Intent("service event")
intent.putExtra("event", "ua added")
intent.putExtra("uap", uap)
intent.putExtra("callp", "")
intent.putExtra("params", arrayListOf(uap))
LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
updateStatusNotification()
}
@Keep
fun updateStatus(event: String, uap: String, callp: String) {
fun uaEvent(event: String, uap: String, callp: String) {
Log.d(LOG_TAG, "updateStatus got event $event/$uap/$callp")
if (!IS_SERVICE_RUNNING) return
val ua = UserAgent.find(MainActivity.uas, uap)
@@ -295,11 +313,76 @@ class BaresipService: Service() {
}
val intent = Intent("service event")
intent.putExtra("event", event)
intent.putExtra("uap", uap)
intent.putExtra("callp", callp)
intent.putExtra("params", arrayListOf(uap, callp))
LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
}
@Keep
fun messageEvent(uap: String, peer: String, msg: ByteArray) {
Log.d(LOG_TAG, "message event $uap/$peer")
var s = "Decoding of message failed!"
try {
s = String(msg, StandardCharsets.UTF_8)
Log.d(LOG_TAG, "UTF-8 $s")
} catch (e: Exception) {
Log.e(LOG_TAG, "UTF-8 decode failed")
}
val timeStamp = System.currentTimeMillis().toString()
if (!Utils.isVisible()) {
val cnb = NotificationCompat.Builder(this, HIGH_CHANNEL_ID)
cnb.setSmallIcon(R.drawable.ic_stat)
.setColor(0x0ca1fd)
.setContentIntent(npi)
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true)
.setContentTitle("Message from ${ContactsActivity.contactName(peer)}")
.setContentText(s)
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
cnb.setVibrate(LongArray(0))
.setVisibility(VISIBILITY_PRIVATE)
.setPriority(Notification.PRIORITY_HIGH)
}
/* val view = RemoteViews(getPackageName(), R.layout.call_notification)
view.setTextViewText(R.id.callFrom, "Call from ${Api.call_peeruri(callp)}")
cnb.setContent(view) */
val replyIntent = Intent(this, MainActivity::class.java)
replyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK or
Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP)
replyIntent.putExtra("action", "reply")
replyIntent.putExtra("uap", uap)
replyIntent.putExtra("peer", peer)
val replyPendingIntent = PendingIntent.getActivity(this,
0, replyIntent, PendingIntent.FLAG_UPDATE_CURRENT)
val archiveIntent = Intent(this, MainActivity::class.java)
archiveIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK or
Intent.FLAG_ACTIVITY_SINGLE_TOP)
archiveIntent.putExtra("action", "archive")
archiveIntent.putExtra("uap", uap)
archiveIntent.putExtra("time", timeStamp)
val archivePendingIntent = PendingIntent.getActivity(this,
1, archiveIntent, PendingIntent.FLAG_UPDATE_CURRENT)
val deleteIntent = Intent(this, MainActivity::class.java)
deleteIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK or
Intent.FLAG_ACTIVITY_SINGLE_TOP)
deleteIntent.putExtra("action", "delete")
deleteIntent.putExtra("uap", uap)
deleteIntent.putExtra("time", timeStamp)
val deletePendingIntent = PendingIntent.getActivity(this,
2, deleteIntent, PendingIntent.FLAG_UPDATE_CURRENT)
/* view.setOnClickPendingIntent(R.id.answerButton, answerPendingIntent)
view.setOnClickPendingIntent(R.id.rejectButton, rejectPendingIntent)
cnb.setCustomBigContentView(view) */
cnb.addAction(R.drawable.ic_stat, "Reply", replyPendingIntent)
cnb.addAction(R.drawable.ic_stat, "Archive", archivePendingIntent)
cnb.addAction(R.drawable.ic_stat, "Delete", deletePendingIntent)
nm.notify(MESSAGE_NOTIFICATION_ID, cnb.build())
}
val intent = Intent("service event")
intent.putExtra("event", "message")
intent.putExtra("params", arrayListOf(uap, peer, s, timeStamp))
LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
}
private fun updateStatusNotification() {
val contentView = RemoteViews(getPackageName(), R.layout.status_notification)
for (i: Int in 0 .. 5) {
@@ -321,9 +404,11 @@ class BaresipService: Service() {
private fun cleanStop() {
HistoryActivity.saveHistory()
MessagesActivity.saveMessages()
MainActivity.uas.clear()
MainActivity.images.clear()
MainActivity.history.clear()
MainActivity.messages.clear()
baresipStop()
BaresipService.IS_SERVICE_RUNNING = false
unregisterReceiver(nr)
@@ -339,8 +424,9 @@ class BaresipService: Service() {
var IS_SERVICE_RUNNING = false
val STATUS_NOTIFICATION_ID = 101
val DEFAULT_CHANNEL_ID = "com.tutpro.baresip.default"
val CALL_NOTIFICATION_ID = 102
val MESSAGE_NOTIFICATION_ID = 103
val DEFAULT_CHANNEL_ID = "com.tutpro.baresip.default"
val HIGH_CHANNEL_ID = "com.tutpro.baresip.high"
var disconnected = false
val RUN_FOREGROUNG = false