Backported chat crash fixes and code improvements from video branch.

This commit is contained in:
Juha Heinanen
2020-05-31 14:24:50 +03:00
parent 366bfbb120
commit 3558fc0e03
5 changed files with 34 additions and 46 deletions
@@ -785,8 +785,8 @@ class BaresipService: Service() {
} }
val timeStamp = System.currentTimeMillis().toString() val timeStamp = System.currentTimeMillis().toString()
Log.d(LOG_TAG, "Message event for $uap from $peer at $timeStamp") Log.d(LOG_TAG, "Message event for $uap from $peer at $timeStamp")
Message.add(Message(ua.account.aor, peer, text, timeStamp.toLong(), Message(ua.account.aor, peer, text, timeStamp.toLong(),
R.drawable.arrow_down_green, 0, "", true)) R.drawable.arrow_down_green, 0, "", true).add()
Message.save() Message.save()
ua.account.unreadMessages = true ua.account.unreadMessages = true
if (!Utils.isVisible()) { if (!Utils.isVisible()) {
@@ -137,7 +137,7 @@ class ChatActivity : AppCompatActivity() {
val time = System.currentTimeMillis() val time = System.currentTimeMillis()
val msg = Message(aor, peerUri, msgText, time, R.drawable.arrow_up_yellow, val msg = Message(aor, peerUri, msgText, time, R.drawable.arrow_up_yellow,
0, "", true) 0, "", true)
Message.add(msg) msg.add()
chatMessages.add(msg) chatMessages.add(msg)
if (Api.message_send(ua.uap, peerUri, msgText, time.toString()) != 0) { if (Api.message_send(ua.uap, peerUri, msgText, time.toString()) != 0) {
Toast.makeText(getApplicationContext(), "${getString(R.string.message_failed)}!", Toast.makeText(getApplicationContext(), "${getString(R.string.message_failed)}!",
@@ -159,6 +159,7 @@ class ChatActivity : AppCompatActivity() {
intent.getStringExtra("time")!!) intent.getStringExtra("time")!!)
} }
} }
LocalBroadcastManager.getInstance(this).registerReceiver(messageResponseReceiver, LocalBroadcastManager.getInstance(this).registerReceiver(messageResponseReceiver,
IntentFilter("message response")) IntentFilter("message response"))
@@ -177,12 +178,13 @@ class ChatActivity : AppCompatActivity() {
BaresipService.chatTexts.put("$aor::$peerUri", newMessage.text.toString()) BaresipService.chatTexts.put("$aor::$peerUri", newMessage.text.toString())
} }
MainActivity.activityAor = aor MainActivity.activityAor = aor
LocalBroadcastManager.getInstance(this).unregisterReceiver(messageResponseReceiver)
super.onPause() super.onPause()
} }
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
val chatText = BaresipService.chatTexts.get("$aor::$peerUri") val chatText = BaresipService.chatTexts["$aor::$peerUri"]
if (chatText != null) { if (chatText != null) {
Log.d("Baresip", "Restoring newMessage ${newMessage.text} for $aor::$peerUri") Log.d("Baresip", "Restoring newMessage ${newMessage.text} for $aor::$peerUri")
newMessage.setText(chatText) newMessage.setText(chatText)
@@ -195,11 +197,6 @@ class ChatActivity : AppCompatActivity() {
listView.adapter = mlAdapter listView.adapter = mlAdapter
} }
override fun onDestroy() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(messageResponseReceiver)
super.onDestroy()
}
override fun onOptionsItemSelected(item: MenuItem): Boolean { override fun onOptionsItemSelected(item: MenuItem): Boolean {
if ((BaresipService.activities.indexOf("chat,$aor,$peerUri,false") == -1) && if ((BaresipService.activities.indexOf("chat,$aor,$peerUri,false") == -1) &&
@@ -73,14 +73,5 @@ class Contact(var name: String, var uri: String, var color: Int, val id: Long) {
return true return true
} }
fun import(): Boolean {
val contacts = Utils.getFileContents(BaresipService.downloadsPath + "/contacts.bs")
if (contacts != null) {
Utils.putFileContents(BaresipService.filesPath + "/contacts", contacts)
return restore()
}
return false
}
} }
} }
@@ -614,8 +614,8 @@ class MainActivity : AppCompatActivity() {
} }
val ev = event.split(",") val ev = event.split(",")
Log.d("Baresip", "Handling service event '${ev[0]}' for $uap") Log.d("Baresip", "Handling service event '${ev[0]}' for $uap")
val aor = ua.account.aor
val acc = ua.account val acc = ua.account
val aor = ua.account.aor
for (account_index in UserAgent.uas().indices) { for (account_index in UserAgent.uas().indices) {
if (UserAgent.uas()[account_index].account.aor == aor) { if (UserAgent.uas()[account_index].account.aor == aor) {
when (ev[0]) { when (ev[0]) {
@@ -640,7 +640,7 @@ class MainActivity : AppCompatActivity() {
volumeControlStream = AudioManager.STREAM_VOICE_CALL volumeControlStream = AudioManager.STREAM_VOICE_CALL
} }
"call rejected" -> { "call rejected" -> {
if (ua.account.aor == aorSpinner.tag) { if (aor == aorSpinner.tag) {
callsButton.setImageResource(R.drawable.calls_missed) callsButton.setImageResource(R.drawable.calls_missed)
} }
} }
@@ -652,8 +652,8 @@ class MainActivity : AppCompatActivity() {
} }
volumeControlStream = AudioManager.STREAM_RING volumeControlStream = AudioManager.STREAM_RING
if (visible) { if (visible) {
if (ua.account.aor != aorSpinner.tag) if (aor != aorSpinner.tag)
spinToAor(ua.account.aor) spinToAor(aor)
showCall(ua) showCall(ua)
} else { } else {
Log.d("Baresip", "Reordering to front") Log.d("Baresip", "Reordering to front")
@@ -666,7 +666,7 @@ class MainActivity : AppCompatActivity() {
} }
"call established" -> { "call established" -> {
volumeControlStream = AudioManager.STREAM_VOICE_CALL volumeControlStream = AudioManager.STREAM_VOICE_CALL
if (ua.account.aor == aorSpinner.tag) { if (aor == aorSpinner.tag) {
dtmf.setText("") dtmf.setText("")
dtmf.hint = getString(R.string.dtmf) dtmf.hint = getString(R.string.dtmf)
showCall(ua) showCall(ua)
@@ -693,7 +693,7 @@ class MainActivity : AppCompatActivity() {
} }
call.security = security call.security = security
call.zid = ev[3] call.zid = ev[3]
if (ua.account.aor == aorSpinner.tag) { if (aor == aorSpinner.tag) {
securityButton.setImageResource(security) securityButton.setImageResource(security)
setSecurityButtonTag(securityButton, security) setSecurityButtonTag(securityButton, security)
securityButton.visibility = View.VISIBLE securityButton.visibility = View.VISIBLE
@@ -703,7 +703,7 @@ class MainActivity : AppCompatActivity() {
verifyDialog.setNegativeButton(getString(R.string.no)) { dialog, _ -> verifyDialog.setNegativeButton(getString(R.string.no)) { dialog, _ ->
call.security = R.drawable.box_yellow call.security = R.drawable.box_yellow
call.zid = ev[3] call.zid = ev[3]
if (ua.account.aor == aorSpinner.tag) { if (aor == aorSpinner.tag) {
securityButton.setImageResource(R.drawable.box_yellow) securityButton.setImageResource(R.drawable.box_yellow)
securityButton.tag = "yellow" securityButton.tag = "yellow"
securityButton.visibility = View.VISIBLE securityButton.visibility = View.VISIBLE
@@ -724,7 +724,7 @@ class MainActivity : AppCompatActivity() {
tag = "yellow" tag = "yellow"
else else
tag = "green" tag = "green"
if (ua.account.aor == aorSpinner.tag) { if (aor == aorSpinner.tag) {
securityButton.setImageResource(call.security) securityButton.setImageResource(call.security)
securityButton.tag = tag securityButton.tag = tag
} }
@@ -768,9 +768,9 @@ class MainActivity : AppCompatActivity() {
showCall(ua) showCall(ua)
} }
"call closed" -> { "call closed" -> {
if (ua.account.aor == aorSpinner.tag) { if (aor == aorSpinner.tag) {
showCall(ua) showCall(ua)
if (ua.account.missedCalls) if (acc.missedCalls)
callsButton.setImageResource(R.drawable.calls_missed) callsButton.setImageResource(R.drawable.calls_missed)
} }
speakerIcon.setIcon(R.drawable.speaker_off) speakerIcon.setIcon(R.drawable.speaker_off)
@@ -811,7 +811,7 @@ class MainActivity : AppCompatActivity() {
break break
} }
} }
if (ua.account.aor == aorSpinner.tag) { if (aor == aorSpinner.tag) {
if (acc.vmNew > 0) if (acc.vmNew > 0)
voicemailButton.setImageResource(R.drawable.voicemail_new) voicemailButton.setImageResource(R.drawable.voicemail_new)
else else
@@ -1433,7 +1433,7 @@ class MainActivity : AppCompatActivity() {
val b = Bundle() val b = Bundle()
b.putString("aor", activity[1]) b.putString("aor", activity[1])
b.putString("peer", activity[2]) b.putString("peer", activity[2])
b.putString("focus", activity[3]) b.putBoolean("focus", activity[3] == "true")
i.putExtras(b) i.putExtras(b)
startActivityForResult(i, CHAT_CODE) startActivityForResult(i, CHAT_CODE)
} }
@@ -7,6 +7,22 @@ class Message(val aor: String, val peerUri: String, val message: String, val tim
var direction: Int, var responseCode: Int, var responseReason: String, var direction: Int, var responseCode: Int, var responseReason: String,
var new: Boolean): Serializable { var new: Boolean): Serializable {
fun add() {
BaresipService.messages.add(this)
var count = 0
var firstIndex = -1
for (i in BaresipService.messages.indices)
if (BaresipService.messages[i].aor == this.aor) {
if (count == 0) firstIndex = i
count++
if (count > MESSAGE_HISTORY_SIZE) {
break
}
}
if (count > MESSAGE_HISTORY_SIZE)
BaresipService.messages.removeAt(firstIndex)
}
companion object { companion object {
val MESSAGE_HISTORY_SIZE = 100 val MESSAGE_HISTORY_SIZE = 100
@@ -15,22 +31,6 @@ class Message(val aor: String, val peerUri: String, val message: String, val tim
return BaresipService.messages return BaresipService.messages
} }
fun add(message: Message) {
BaresipService.messages.add(message)
var count = 0
var firstIndex = -1
for (i in BaresipService.messages.indices)
if (BaresipService.messages[i].aor == message.aor) {
if (count == 0) firstIndex = i
count++
if (count > MESSAGE_HISTORY_SIZE) {
break
}
}
if (count > MESSAGE_HISTORY_SIZE)
BaresipService.messages.removeAt(firstIndex)
}
fun clear(aor: String) { fun clear(aor: String) {
val it = BaresipService.messages.iterator() val it = BaresipService.messages.iterator()
while (it.hasNext()) if (it.next().aor == aor) it.remove() while (it.hasNext()) if (it.next().aor == aor) it.remove()