- new implementation of messaging
- fixed possible crash when messages arrive before accounts are created - other minor fixes and improvements
This commit is contained in:
@@ -0,0 +1,234 @@
|
||||
package com.tutpro.baresip
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.*
|
||||
import android.os.Bundle
|
||||
import android.support.v7.app.AlertDialog
|
||||
import android.support.v7.app.AppCompatActivity
|
||||
import android.util.Log
|
||||
import android.view.MenuItem
|
||||
import android.widget.*
|
||||
|
||||
import java.io.File
|
||||
import java.io.FileInputStream
|
||||
import java.io.FileOutputStream
|
||||
import java.io.IOException
|
||||
import java.io.ObjectInputStream
|
||||
import java.io.ObjectOutputStream
|
||||
import java.util.*
|
||||
|
||||
class ChatsActivity: AppCompatActivity() {
|
||||
|
||||
internal lateinit var uaMessages: ArrayList<Message>
|
||||
internal lateinit var aor: String
|
||||
internal lateinit var listView: ListView
|
||||
internal lateinit var clAdapter: ChatListAdapter
|
||||
internal lateinit var peerUri: AutoCompleteTextView
|
||||
internal lateinit var plusButton: ImageButton
|
||||
|
||||
public override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_chats)
|
||||
|
||||
listView = findViewById(R.id.chats) as ListView
|
||||
plusButton = findViewById(R.id.plusButton) as ImageButton
|
||||
|
||||
aor = intent.extras.getString("aor")
|
||||
uaMessages = uaMessages(aor)
|
||||
|
||||
clAdapter = ChatListAdapter(this, uaMessages)
|
||||
listView.adapter = clAdapter
|
||||
listView.isLongClickable = true
|
||||
|
||||
listView.onItemClickListener = AdapterView.OnItemClickListener { _, _, pos, _ ->
|
||||
val i = Intent(this, ChatActivity::class.java)
|
||||
val b = Bundle()
|
||||
b.putString("aor", aor)
|
||||
b.putString("peer", uaMessages[pos].peerUri)
|
||||
i.putExtras(b)
|
||||
startActivityForResult(i, MainActivity.MESSAGE_CODE)
|
||||
}
|
||||
|
||||
listView.onItemLongClickListener = AdapterView.OnItemLongClickListener { _, _, pos, _ ->
|
||||
val dialogClickListener = DialogInterface.OnClickListener { _, which ->
|
||||
when (which) {
|
||||
DialogInterface.BUTTON_NEUTRAL -> {
|
||||
val i = Intent(this, ContactActivity::class.java)
|
||||
val b = Bundle()
|
||||
b.putBoolean("new", true)
|
||||
b.putString("uri", uaMessages[pos].peerUri)
|
||||
i.putExtras(b)
|
||||
startActivityForResult(i, MainActivity.CONTACT_CODE)
|
||||
}
|
||||
DialogInterface.BUTTON_NEGATIVE -> {
|
||||
val peerUri = uaMessages[pos].peerUri
|
||||
val msgs = ArrayList<Message>()
|
||||
for (m in MainActivity.messages)
|
||||
if ((m.aor != aor) || (m.peerUri != peerUri))
|
||||
msgs.add(m)
|
||||
else
|
||||
clAdapter.remove(m)
|
||||
clAdapter.notifyDataSetChanged()
|
||||
MainActivity.messages = msgs
|
||||
uaMessages = uaMessages(aor)
|
||||
}
|
||||
DialogInterface.BUTTON_POSITIVE -> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val builder = AlertDialog.Builder(this@ChatsActivity, R.style.Theme_AppCompat)
|
||||
val peer = ContactsActivity.contactName(uaMessages[pos].peerUri)
|
||||
if (peer.startsWith("sip:"))
|
||||
builder.setMessage("Do you want to delete chat with $peer or add peer to contacts?")
|
||||
.setPositiveButton("Cancel", dialogClickListener)
|
||||
.setNegativeButton("Delete Chat", dialogClickListener)
|
||||
.setNeutralButton("Add Contact", dialogClickListener)
|
||||
.show()
|
||||
else
|
||||
builder.setMessage("Do you want to delete chat with $peer?")
|
||||
.setPositiveButton("Cancel", dialogClickListener)
|
||||
.setNegativeButton("Delete Chat", dialogClickListener)
|
||||
.show()
|
||||
true
|
||||
}
|
||||
|
||||
peerUri = findViewById(R.id.peerUri) as AutoCompleteTextView
|
||||
peerUri.threshold = 2
|
||||
peerUri.setAdapter(ArrayAdapter(this, android.R.layout.select_dialog_item,
|
||||
ContactsActivity.contacts.map{Contact -> Contact.name}))
|
||||
|
||||
plusButton.setOnClickListener {
|
||||
val uriText = peerUri.text.toString().trim()
|
||||
if (uriText.length > 0) {
|
||||
var uri = ContactsActivity.findContactURI(uriText)
|
||||
if (!uri.startsWith("sip:")) {
|
||||
uri = "sip:$uri"
|
||||
if (!uri.contains("@")) {
|
||||
val host = aor.substring(aor.indexOf("@") + 1)
|
||||
uri = "$uri@$host"
|
||||
}
|
||||
}
|
||||
if (!Utils.checkSipUri(uri)) {
|
||||
Utils.alertView(this, "Notice", "Invalid SIP URI '$uri'")
|
||||
} else {
|
||||
peerUri.text.clear()
|
||||
peerUri.isCursorVisible = false
|
||||
val i = Intent(this@ChatsActivity, ChatActivity::class.java)
|
||||
val b = Bundle()
|
||||
b.putString("aor", aor)
|
||||
b.putString("peer", uri)
|
||||
i.putExtras(b)
|
||||
startActivityForResult(i, MainActivity.MESSAGE_CODE)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val peer = intent.extras.getString("peer")
|
||||
if (peer != "") {
|
||||
val i = Intent(this, ChatActivity::class.java)
|
||||
val b = Bundle()
|
||||
b.putString("aor", aor)
|
||||
b.putString("peer", peer)
|
||||
i.putExtras(b)
|
||||
startActivityForResult(i, MainActivity.MESSAGE_CODE)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
saveMessages()
|
||||
super.onPause()
|
||||
}
|
||||
|
||||
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
||||
when (item.itemId) {
|
||||
android.R.id.home -> {
|
||||
Log.d("Baresip", "Back array was pressed at Chats")
|
||||
val i = Intent()
|
||||
setResult(Activity.RESULT_CANCELED, i)
|
||||
finish()
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||
Log.d("Baresip", "onActivityResult $requestCode $resultCode")
|
||||
if ((requestCode == MainActivity.MESSAGE_CODE) && (resultCode == Activity.RESULT_OK)) {
|
||||
clAdapter.clear()
|
||||
uaMessages = uaMessages(aor)
|
||||
clAdapter = ChatListAdapter(this, uaMessages)
|
||||
listView.adapter = clAdapter
|
||||
}
|
||||
}
|
||||
|
||||
private fun uaMessages(aor: String) : ArrayList<Message> {
|
||||
val res = ArrayList<Message>()
|
||||
for (m in MainActivity.messages.reversed()) {
|
||||
if (m.aor != aor) continue
|
||||
var found = false
|
||||
for (r in res)
|
||||
if (r.peerUri == m.peerUri) {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
if (!found) res.add(m)
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
fun archiveUaMessage(aor: String, time: Long) {
|
||||
for (i in MainActivity.messages.indices.reversed())
|
||||
if ((MainActivity.messages[i].aor == aor) &&
|
||||
(MainActivity.messages[i].timeStamp == time)) {
|
||||
MainActivity.messages[i].new = false
|
||||
saveMessages()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
fun deleteUaMessage(aor: String, time: Long) {
|
||||
for (i in MainActivity.messages.indices.reversed())
|
||||
if ((MainActivity.messages[i].aor == aor) &&
|
||||
(MainActivity.messages[i].timeStamp == time)) {
|
||||
MainActivity.messages.removeAt(i)
|
||||
saveMessages()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
fun saveMessages() {
|
||||
val file = File(MainActivity.filesPath, "messages")
|
||||
try {
|
||||
val fos = FileOutputStream(file)
|
||||
val oos = ObjectOutputStream(fos)
|
||||
oos.writeObject(MainActivity.messages)
|
||||
oos.close()
|
||||
fos.close()
|
||||
} catch (e: IOException) {
|
||||
Log.w("Baresip", "OutputStream exception: " + e.toString())
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
|
||||
fun restoreMessages(path: String) {
|
||||
val 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("Baresip", "Restored ${MainActivity.messages.size} messages")
|
||||
ois.close()
|
||||
fis.close()
|
||||
} catch (e: Exception) {
|
||||
Log.w("Baresip", "InputStream exception: - " + e.toString())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user