- added possibility to export/import contacts from Download folder

- requires WRITE_EXTERNAL_STORAGE permission
This commit is contained in:
Juha Heinanen
2018-12-03 08:55:29 +02:00
parent d535126d65
commit 3bc93e5dd2
7 changed files with 48 additions and 17 deletions

View File

@ -8,6 +8,8 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature
android:name="android.hardware.telephony"

View File

@ -93,7 +93,7 @@ class ContactActivity : AppCompatActivity() {
}
Contact.contacts().sortBy { Contact -> Contact.name }
ContactsActivity.saveContacts(applicationContext.filesDir.absolutePath)
ContactsActivity.saveContacts(applicationContext.filesDir)
i.putExtra("name", newName)
setResult(Activity.RESULT_OK, i)

View File

@ -67,7 +67,7 @@ class ContactListAdapter(private val cxt: Context, private val rows: ArrayList<C
when (which) {
DialogInterface.BUTTON_NEGATIVE -> {
Contact.contacts().removeAt(pos)
ContactsActivity.saveContacts()
ContactsActivity.saveContacts(cxt.applicationContext.filesDir)
this.notifyDataSetChanged()
}
DialogInterface.BUTTON_POSITIVE -> {

View File

@ -3,8 +3,10 @@ package com.tutpro.baresip
import android.app.Activity
import android.content.*
import android.os.Bundle
import android.os.Environment
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.widget.ImageButton
import android.widget.ListView
@ -19,8 +21,6 @@ class ContactsActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_contacts)
filesPath = applicationContext.filesDir.absolutePath
val listView = findViewById(R.id.contacts) as ListView
val aor = intent.extras.getString("aor")
@ -49,8 +49,35 @@ class ContactsActivity : AppCompatActivity() {
if (resultCode == RESULT_OK) clAdapter.notifyDataSetChanged()
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.contacts_menu, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
val dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
when (item.itemId) {
R.id.export_contacts -> {
if (saveContacts(dir))
Utils.alertView(this,"",
"Exported contacts to Download folder.")
else
Utils.alertView(this,"Error",
"Failed to export contacts to Download folder. " +
"Check Apps -> baresip -> Permissions -> Storage.")
}
R.id.import_contacts -> {
if (restoreContacts(dir)) {
Utils.alertView(this, "",
"Imported contacts from Download folder.")
clAdapter.notifyDataSetChanged()
saveContacts(applicationContext.filesDir)
} else
Utils.alertView(this,"Error",
"Failed to import contacts from Download folder. " +
"Check Apps -> baresip -> Permissions -> Storage and that " +
"the file exists in the folder.")
}
android.R.id.home -> {
Log.d("Baresip", "Back array was pressed at Contacts")
val i = Intent()
@ -70,17 +97,17 @@ class ContactsActivity : AppCompatActivity() {
companion object {
internal var filesPath = ""
fun saveContacts(path: String = filesPath) {
fun saveContacts(path: File): Boolean {
var contents = ""
for (c in Contact.contacts())
contents += "\"${c.name}\" ${c.uri}\n"
Utils.putFileContents(File(path + "/contacts"), contents)
return Utils.putFileContents(File(path, "contacts"), contents)
}
fun restoreContacts(path: String = filesPath) {
val content = Utils.getFileContents(File(path + "/contacts"))
fun restoreContacts(path: File): Boolean {
val content = Utils.getFileContents(File(path, "contacts"))
if (content == "Failed") return false
Api.contacts_remove()
Contact.contacts().clear()
content.lines().forEach {
@ -92,6 +119,7 @@ class ContactsActivity : AppCompatActivity() {
Contact.contacts().add(Contact(name, uri))
}
}
return true
}
fun findContactURI(name: String): String {

View File

@ -186,7 +186,7 @@ class MainActivity : AppCompatActivity() {
}
}
ContactsActivity.restoreContacts(applicationContext.filesDir.absolutePath)
ContactsActivity.restoreContacts(applicationContext.filesDir)
callUri.setAdapter(ArrayAdapter(this, android.R.layout.select_dialog_item,
Contact.contacts().map{Contact -> Contact.name}))
callUri.threshold = 2
@ -657,8 +657,7 @@ class MainActivity : AppCompatActivity() {
super.onNewIntent(intent)
val action = intent.getStringExtra("action")
Log.d("Baresip", "onNewIntent action '$action'")
if (action != null)
handleIntent(intent)
if (action != null) handleIntent(intent)
}
private fun handleIntent(intent: Intent) {

View File

@ -16,7 +16,7 @@ object Utils {
fun getFileContents(file: File): String {
if (!file.exists()) {
Log.e("Baresip", "Failed to find file: " + file.path)
return ""
return "Failed"
} else {
val length = file.length().toInt()
val bytes = ByteArray(length)
@ -31,13 +31,13 @@ object Utils {
} catch (e: java.io.IOException) {
Log.e("Baresip", "Failed to read file: " + file.path + ": " +
e.toString())
return ""
return "Failed"
}
}
}
fun putFileContents(file: File, contents: String) {
fun putFileContents(file: File, contents: String): Boolean {
try {
val fOut = FileOutputStream(file.absoluteFile, false)
val fWriter = OutputStreamWriter(fOut)
@ -47,12 +47,14 @@ object Utils {
fOut.close()
} catch (e: java.io.IOException) {
Log.e("Baresip", "Failed to put contents to file: " + e.toString())
return false
}
} catch (e: java.io.FileNotFoundException) {
Log.e("Baresip", "Failed to find contents file: " + e.toString())
return false
}
return true
}
fun getNameValue(string: String, name: String): ArrayList<String> {

View File

@ -22,7 +22,7 @@
<EditText
android:id="@+id/Name"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:inputType="textPersonName|textCapWords"
android:textSize="18sp"
android:layout_width="fill_parent">
</EditText>