improved contact related activities
This commit is contained in:
3
app/src/main/kotlin/com/tutpro/baresip/Contact.kt
Normal file
3
app/src/main/kotlin/com/tutpro/baresip/Contact.kt
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
package com.tutpro.baresip
|
||||||
|
|
||||||
|
class Contact(var name: String, var uri: String)
|
||||||
@ -17,13 +17,23 @@ class ContactActivity : AppCompatActivity() {
|
|||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
setContentView(R.layout.activity_contact)
|
setContentView(R.layout.activity_contact)
|
||||||
|
|
||||||
index = intent.extras.getInt("index")
|
new = intent.extras.getBoolean("new")
|
||||||
val name = ContactsActivity.contactNames[index]
|
val name: String
|
||||||
val uri = ContactsActivity.contactURIs[index]
|
val uri: String
|
||||||
|
if (new) {
|
||||||
|
name = intent.extras.getString("name")
|
||||||
|
uri = intent.extras.getString("uri")
|
||||||
|
} else {
|
||||||
|
index = intent.extras.getInt("index")
|
||||||
|
name = ContactsActivity.contacts[index].name
|
||||||
|
uri = ContactsActivity.contacts[index].uri
|
||||||
|
}
|
||||||
|
|
||||||
setTitle(name)
|
setTitle(name)
|
||||||
|
|
||||||
nameView = findViewById(R.id.Name) as EditText
|
nameView = findViewById(R.id.Name) as EditText
|
||||||
nameView.setText(name)
|
nameView.setText(name)
|
||||||
|
if (new) nameView.setSelection(nameView.text.length)
|
||||||
|
|
||||||
uriView = findViewById(R.id.Uri) as EditText
|
uriView = findViewById(R.id.Uri) as EditText
|
||||||
uriView.setText(uri)
|
uriView.setText(uri)
|
||||||
@ -42,25 +52,37 @@ class ContactActivity : AppCompatActivity() {
|
|||||||
|
|
||||||
if (item.itemId == R.id.checkIcon) {
|
if (item.itemId == R.id.checkIcon) {
|
||||||
|
|
||||||
val name = nameView.text.toString().trim()
|
val newName = nameView.text.toString().trim()
|
||||||
if (!Utils.checkName(name)) {
|
if (!Utils.checkName(newName)) {
|
||||||
Utils.alertView(this, "Notice",
|
Utils.alertView(this, "Notice",
|
||||||
"Invalid contact name: $name")
|
"Invalid contact name: $newName")
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if ((new || (ContactsActivity.contacts[index].name != newName)) &&
|
||||||
|
ContactsActivity.nameExists(newName)) {
|
||||||
|
Utils.alertView(this, "Notice",
|
||||||
|
"Contact $newName already exists")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
var uri = uriView.text.toString().trim()
|
var newUri = uriView.text.toString().trim()
|
||||||
if (!uri.startsWith("<")) {
|
if (!newUri.startsWith("<")) {
|
||||||
if (!uri.startsWith("sip:")) uri = "sip:$uri"
|
if (!newUri.startsWith("sip:")) newUri = "sip:$newUri"
|
||||||
if (!Utils.checkUri(uri)) {
|
if (!Utils.checkUri(newUri)) {
|
||||||
Utils.alertView(this, "Notice","Invalid contact URI: $uri")
|
Utils.alertView(this, "Notice","Invalid contact URI: $newUri")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ContactsActivity.contactNames[index] = name
|
if (new) {
|
||||||
ContactsActivity.contactURIs[index] = uri
|
ContactsActivity.contacts.add(Contact(newName, newUri))
|
||||||
|
} else {
|
||||||
|
ContactsActivity.contacts[index].uri = newUri
|
||||||
|
ContactsActivity.contacts[index].name = newName
|
||||||
|
}
|
||||||
|
ContactsActivity.contacts.sortBy { Contact -> Contact.name }
|
||||||
ContactsActivity.saveContacts()
|
ContactsActivity.saveContacts()
|
||||||
|
|
||||||
setResult(RESULT_OK, i)
|
setResult(RESULT_OK, i)
|
||||||
finish()
|
finish()
|
||||||
return true
|
return true
|
||||||
@ -77,6 +99,7 @@ class ContactActivity : AppCompatActivity() {
|
|||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
internal var index = 0
|
internal var index = 0
|
||||||
|
internal var new = false
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,20 +15,21 @@ import android.widget.TextView
|
|||||||
|
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
class ContactListAdapter(private val cxt: Context, private val rows: ArrayList<String>) :
|
class ContactListAdapter(private val cxt: Context, private val rows: ArrayList<Contact>) :
|
||||||
ArrayAdapter<String>(cxt, R.layout.contact_row, rows) {
|
ArrayAdapter<Contact>(cxt, R.layout.contact_row, rows) {
|
||||||
|
|
||||||
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
|
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
|
||||||
val row = rows[position]
|
val row = rows[position]
|
||||||
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
|
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
|
||||||
val rowView = inflater.inflate(R.layout.contact_row, parent, false)
|
val rowView = inflater.inflate(R.layout.contact_row, parent, false)
|
||||||
val nameView = rowView.findViewById(R.id.contactName) as TextView
|
val nameView = rowView.findViewById(R.id.contactName) as TextView
|
||||||
nameView.text = row
|
nameView.text = row.name
|
||||||
nameView.textSize = 20f
|
nameView.textSize = 20f
|
||||||
nameView.setPadding(6, 6, 0, 6)
|
nameView.setPadding(6, 6, 0, 6)
|
||||||
nameView.setOnClickListener { _ ->
|
nameView.setOnClickListener { _ ->
|
||||||
val i = Intent(cxt, ContactActivity::class.java)
|
val i = Intent(cxt, ContactActivity::class.java)
|
||||||
val b = Bundle()
|
val b = Bundle()
|
||||||
|
b.putBoolean("new", false)
|
||||||
b.putInt("index", position)
|
b.putInt("index", position)
|
||||||
i.putExtras(b)
|
i.putExtras(b)
|
||||||
(cxt as Activity).startActivityForResult(i, MainActivity.CONTACT_CODE)
|
(cxt as Activity).startActivityForResult(i, MainActivity.CONTACT_CODE)
|
||||||
@ -38,10 +39,9 @@ class ContactListAdapter(private val cxt: Context, private val rows: ArrayList<S
|
|||||||
actionView.setOnClickListener { _ ->
|
actionView.setOnClickListener { _ ->
|
||||||
Log.d("Baresip", "Delete button clicked")
|
Log.d("Baresip", "Delete button clicked")
|
||||||
val deleteDialog = AlertDialog.Builder(cxt)
|
val deleteDialog = AlertDialog.Builder(cxt)
|
||||||
deleteDialog.setMessage("Do you want to delete contact ${row}")
|
deleteDialog.setMessage("Do you want to delete contact ${row.name}")
|
||||||
deleteDialog.setPositiveButton("Delete") { dialog, _ ->
|
deleteDialog.setPositiveButton("Delete") { dialog, _ ->
|
||||||
ContactsActivity.contactNames.removeAt(position)
|
ContactsActivity.contacts.removeAt(position)
|
||||||
ContactsActivity.contactURIs.removeAt(position)
|
|
||||||
ContactsActivity.saveContacts()
|
ContactsActivity.saveContacts()
|
||||||
this.notifyDataSetChanged()
|
this.notifyDataSetChanged()
|
||||||
dialog.dismiss()
|
dialog.dismiss()
|
||||||
|
|||||||
@ -23,8 +23,8 @@ class ContactsActivity : AppCompatActivity() {
|
|||||||
setContentView(R.layout.activity_contacts)
|
setContentView(R.layout.activity_contacts)
|
||||||
|
|
||||||
val listview = findViewById(R.id.contacts) as ListView
|
val listview = findViewById(R.id.contacts) as ListView
|
||||||
Log.d("Baresip", "Got ${contactNames.size} contacts")
|
Log.d("Baresip", "Got ${contacts.size} contacts")
|
||||||
clAdapter = ContactListAdapter(this, contactNames)
|
clAdapter = ContactListAdapter(this, contacts)
|
||||||
listview.adapter = clAdapter
|
listview.adapter = clAdapter
|
||||||
|
|
||||||
val addContactButton = findViewById(R.id.addContact) as ImageButton
|
val addContactButton = findViewById(R.id.addContact) as ImageButton
|
||||||
@ -35,27 +35,31 @@ class ContactsActivity : AppCompatActivity() {
|
|||||||
Log.e("Baresip", "Invalid contact name $name")
|
Log.e("Baresip", "Invalid contact name $name")
|
||||||
Utils.alertView(this, "Notice",
|
Utils.alertView(this, "Notice",
|
||||||
"Invalid contact name: $name")
|
"Invalid contact name: $name")
|
||||||
} else if (contactNames.contains(name)) {
|
} else if (nameExists(name)) {
|
||||||
Log.e("Baresip", "Contact name $name already exists")
|
|
||||||
Utils.alertView(this, "Notice",
|
Utils.alertView(this, "Notice",
|
||||||
"Contact $name already exists")
|
"Contact $name already exists")
|
||||||
} else {
|
} else {
|
||||||
newNameView.setText("")
|
newNameView.setText("")
|
||||||
newNameView.hint = "Contact name"
|
newNameView.hint = "Contact name"
|
||||||
newNameView.clearFocus()
|
newNameView.clearFocus()
|
||||||
contactNames.add(name)
|
|
||||||
contactURIs.add("")
|
|
||||||
clAdapter.notifyDataSetChanged()
|
|
||||||
saveContacts()
|
|
||||||
val i = Intent(this, ContactActivity::class.java)
|
val i = Intent(this, ContactActivity::class.java)
|
||||||
val b = Bundle()
|
val b = Bundle()
|
||||||
b.putInt("index", contactNames.size - 1)
|
b.putBoolean("new", true)
|
||||||
|
b.putString("name", name)
|
||||||
|
b.putString("uri", "")
|
||||||
i.putExtras(b)
|
i.putExtras(b)
|
||||||
startActivityForResult(i, MainActivity.CONTACT_CODE)
|
startActivityForResult(i, MainActivity.CONTACT_CODE)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||||
|
if (resultCode == RESULT_OK) {
|
||||||
|
Log.d("Baresip", "Notifying clAdapter")
|
||||||
|
clAdapter.notifyDataSetChanged()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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 -> {
|
||||||
@ -68,52 +72,55 @@ class ContactsActivity : AppCompatActivity() {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
override fun onBackPressed() {
|
||||||
if (resultCode == RESULT_OK) {
|
Log.d("Baresip", "Back button was pressed at Contacts")
|
||||||
Log.d("Baresip", "Notifying clAdapter")
|
val i = Intent()
|
||||||
clAdapter.notifyDataSetChanged()
|
setResult(Activity.RESULT_OK, i)
|
||||||
}
|
finish()
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|
||||||
var contactNames = ArrayList<String>()
|
var contacts = ArrayList<Contact>()
|
||||||
var contactURIs = ArrayList<String>()
|
|
||||||
|
|
||||||
fun generateContacts(path: String) {
|
fun generateContacts(path: String) {
|
||||||
val content = Utils.getFileContents(File(path))
|
val content = Utils.getFileContents(File(path))
|
||||||
MainActivity.contacts_remove()
|
MainActivity.contacts_remove()
|
||||||
contactNames.clear()
|
contacts.clear()
|
||||||
contactURIs.clear()
|
|
||||||
content.lines().forEach {
|
content.lines().forEach {
|
||||||
val parts = it.split("\"")
|
val parts = it.split("\"")
|
||||||
if (parts.size == 3) {
|
if (parts.size == 3) {
|
||||||
val name = parts[1]
|
val name = parts[1]
|
||||||
var uri = parts[2].trim()
|
val uri = parts[2].trim()
|
||||||
MainActivity.contact_add("\"$name\" $uri")
|
MainActivity.contact_add("\"$name\" $uri")
|
||||||
contactNames.add(name)
|
contacts.add(Contact(name, uri))
|
||||||
contactURIs.add(uri)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun saveContacts() {
|
fun saveContacts() {
|
||||||
var contents = ""
|
var contents = ""
|
||||||
for (i in contactNames.indices)
|
for (c in contacts)
|
||||||
contents += "\"${contactNames[i]}\" ${contactURIs[i]}\n"
|
contents += "\"${c.name}\" ${c.uri}\n"
|
||||||
val path = MainActivity.filesPath + "/contacts"
|
val path = MainActivity.filesPath + "/contacts"
|
||||||
Utils.putFileContents(File(path), contents)
|
Utils.putFileContents(File(path), contents)
|
||||||
Log.d("Baresip", "Saved contacts '${contents}' to '$path")
|
Log.d("Baresip", "Saved contacts '${contents}' to '$path")
|
||||||
}
|
}
|
||||||
|
|
||||||
fun findContactURI(name: String): String {
|
fun findContactURI(name: String): String {
|
||||||
for (i in contactNames.indices)
|
for (c in contacts)
|
||||||
if (contactNames[i] == name)
|
if (c.name == name)
|
||||||
return contactURIs[i].removePrefix("<")
|
return c.uri.removePrefix("<")
|
||||||
.replaceAfter(">", "")
|
.replaceAfter(">", "")
|
||||||
.replace(">", "")
|
.replace(">", "")
|
||||||
return name
|
return name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun nameExists(name: String): Boolean {
|
||||||
|
for (c in contacts)
|
||||||
|
if (c.name.equals(name, ignoreCase = true)) return true
|
||||||
|
return false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -27,8 +27,7 @@ class HistoryActivity : AppCompatActivity() {
|
|||||||
|
|
||||||
val listview = findViewById(R.id.history) as ListView
|
val listview = findViewById(R.id.history) as ListView
|
||||||
|
|
||||||
val b = intent.extras
|
aor = intent.extras.getString("aor")
|
||||||
aor = b.getString("aor")
|
|
||||||
aorGenerateHistory(aor)
|
aorGenerateHistory(aor)
|
||||||
|
|
||||||
val adapter = HistoryListAdapter(this, uaHistory)
|
val adapter = HistoryListAdapter(this, uaHistory)
|
||||||
@ -44,16 +43,15 @@ class HistoryActivity : AppCompatActivity() {
|
|||||||
val dialogClickListener = DialogInterface.OnClickListener { _, which ->
|
val dialogClickListener = DialogInterface.OnClickListener { _, which ->
|
||||||
when (which) {
|
when (which) {
|
||||||
DialogInterface.BUTTON_NEUTRAL -> {
|
DialogInterface.BUTTON_NEUTRAL -> {
|
||||||
ContactsActivity.contactNames.add("New Name")
|
|
||||||
if (uaHistory[pos].peerDomain == "")
|
|
||||||
ContactsActivity.contactURIs.add(uaHistory[pos].peerURI)
|
|
||||||
else
|
|
||||||
ContactsActivity.contactURIs.add("sip:${uaHistory[pos].peerURI}" +
|
|
||||||
"@" + uaHistory[pos].peerDomain)
|
|
||||||
ContactsActivity.saveContacts()
|
|
||||||
val i = Intent(this, ContactActivity::class.java)
|
val i = Intent(this, ContactActivity::class.java)
|
||||||
val b = Bundle()
|
val b = Bundle()
|
||||||
b.putInt("index", ContactsActivity.contactNames.size - 1)
|
b.putBoolean("new", true)
|
||||||
|
b.putString("name", "New Name")
|
||||||
|
if (uaHistory[pos].peerDomain == "")
|
||||||
|
b.putString("uri", uaHistory[pos].peerURI)
|
||||||
|
else
|
||||||
|
b.putString("uri", "sip:${uaHistory[pos].peerURI}@" +
|
||||||
|
uaHistory[pos].peerDomain)
|
||||||
i.putExtras(b)
|
i.putExtras(b)
|
||||||
startActivityForResult(i, MainActivity.CONTACT_CODE)
|
startActivityForResult(i, MainActivity.CONTACT_CODE)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -44,7 +44,6 @@ class MainActivity : AppCompatActivity() {
|
|||||||
internal lateinit var dtmf: EditText
|
internal lateinit var dtmf: EditText
|
||||||
internal lateinit var uaAdapter: UaSpinnerAdapter
|
internal lateinit var uaAdapter: UaSpinnerAdapter
|
||||||
internal lateinit var aorSpinner: Spinner
|
internal lateinit var aorSpinner: Spinner
|
||||||
internal lateinit var calleeAdapter: ArrayAdapter<String>
|
|
||||||
internal lateinit var am: AudioManager
|
internal lateinit var am: AudioManager
|
||||||
internal lateinit var imm: InputMethodManager
|
internal lateinit var imm: InputMethodManager
|
||||||
internal lateinit var nm: NotificationManager
|
internal lateinit var nm: NotificationManager
|
||||||
@ -228,9 +227,8 @@ class MainActivity : AppCompatActivity() {
|
|||||||
"/contacts")
|
"/contacts")
|
||||||
|
|
||||||
callee.threshold = 2
|
callee.threshold = 2
|
||||||
calleeAdapter = ArrayAdapter(this,
|
callee.setAdapter(ArrayAdapter(this, android.R.layout.select_dialog_item,
|
||||||
android.R.layout.select_dialog_item, ContactsActivity.contactNames)
|
ContactsActivity.contacts.map{Contact -> Contact.name}))
|
||||||
callee.setAdapter(calleeAdapter)
|
|
||||||
|
|
||||||
securityButton.setOnClickListener {
|
securityButton.setOnClickListener {
|
||||||
when (securityButton.tag) {
|
when (securityButton.tag) {
|
||||||
@ -472,7 +470,8 @@ class MainActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
CONTACTS_CODE -> {
|
CONTACTS_CODE -> {
|
||||||
calleeAdapter.notifyDataSetChanged()
|
callee.setAdapter(ArrayAdapter(this, android.R.layout.select_dialog_item,
|
||||||
|
ContactsActivity.contacts.map{Contact -> Contact.name}))
|
||||||
}
|
}
|
||||||
|
|
||||||
EDIT_CONFIG_CODE -> {
|
EDIT_CONFIG_CODE -> {
|
||||||
|
|||||||
Reference in New Issue
Block a user