improved contact related activities

This commit is contained in:
Juha Heinanen
2018-06-13 10:06:29 +03:00
parent 607c27a159
commit b7cb2b7c47
6 changed files with 90 additions and 60 deletions
@@ -23,8 +23,8 @@ class ContactsActivity : AppCompatActivity() {
setContentView(R.layout.activity_contacts)
val listview = findViewById(R.id.contacts) as ListView
Log.d("Baresip", "Got ${contactNames.size} contacts")
clAdapter = ContactListAdapter(this, contactNames)
Log.d("Baresip", "Got ${contacts.size} contacts")
clAdapter = ContactListAdapter(this, contacts)
listview.adapter = clAdapter
val addContactButton = findViewById(R.id.addContact) as ImageButton
@@ -35,27 +35,31 @@ class ContactsActivity : AppCompatActivity() {
Log.e("Baresip", "Invalid contact name $name")
Utils.alertView(this, "Notice",
"Invalid contact name: $name")
} else if (contactNames.contains(name)) {
Log.e("Baresip", "Contact name $name already exists")
} else if (nameExists(name)) {
Utils.alertView(this, "Notice",
"Contact $name already exists")
} else {
newNameView.setText("")
newNameView.hint = "Contact name"
newNameView.clearFocus()
contactNames.add(name)
contactURIs.add("")
clAdapter.notifyDataSetChanged()
saveContacts()
val i = Intent(this, ContactActivity::class.java)
val b = Bundle()
b.putInt("index", contactNames.size - 1)
b.putBoolean("new", true)
b.putString("name", name)
b.putString("uri", "")
i.putExtras(b)
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 {
when (item.itemId) {
android.R.id.home -> {
@@ -68,52 +72,55 @@ class ContactsActivity : AppCompatActivity() {
return true
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (resultCode == RESULT_OK) {
Log.d("Baresip", "Notifying clAdapter")
clAdapter.notifyDataSetChanged()
}
override fun onBackPressed() {
Log.d("Baresip", "Back button was pressed at Contacts")
val i = Intent()
setResult(Activity.RESULT_OK, i)
finish()
}
companion object {
var contactNames = ArrayList<String>()
var contactURIs = ArrayList<String>()
var contacts = ArrayList<Contact>()
fun generateContacts(path: String) {
val content = Utils.getFileContents(File(path))
MainActivity.contacts_remove()
contactNames.clear()
contactURIs.clear()
contacts.clear()
content.lines().forEach {
val parts = it.split("\"")
if (parts.size == 3) {
val name = parts[1]
var uri = parts[2].trim()
val uri = parts[2].trim()
MainActivity.contact_add("\"$name\" $uri")
contactNames.add(name)
contactURIs.add(uri)
contacts.add(Contact(name, uri))
}
}
}
fun saveContacts() {
var contents = ""
for (i in contactNames.indices)
contents += "\"${contactNames[i]}\" ${contactURIs[i]}\n"
for (c in contacts)
contents += "\"${c.name}\" ${c.uri}\n"
val path = MainActivity.filesPath + "/contacts"
Utils.putFileContents(File(path), contents)
Log.d("Baresip", "Saved contacts '${contents}' to '$path")
}
fun findContactURI(name: String): String {
for (i in contactNames.indices)
if (contactNames[i] == name)
return contactURIs[i].removePrefix("<")
for (c in contacts)
if (c.name == name)
return c.uri.removePrefix("<")
.replaceAfter(">", "")
.replace(">", "")
return name
}
fun nameExists(name: String): Boolean {
for (c in contacts)
if (c.name.equals(name, ignoreCase = true)) return true
return false
}
}
}