- 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
+2
View File
@@ -8,6 +8,8 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" /> <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 <uses-feature
android:name="android.hardware.telephony" android:name="android.hardware.telephony"
@@ -93,7 +93,7 @@ class ContactActivity : AppCompatActivity() {
} }
Contact.contacts().sortBy { Contact -> Contact.name } Contact.contacts().sortBy { Contact -> Contact.name }
ContactsActivity.saveContacts(applicationContext.filesDir.absolutePath) ContactsActivity.saveContacts(applicationContext.filesDir)
i.putExtra("name", newName) i.putExtra("name", newName)
setResult(Activity.RESULT_OK, i) setResult(Activity.RESULT_OK, i)
@@ -67,7 +67,7 @@ class ContactListAdapter(private val cxt: Context, private val rows: ArrayList<C
when (which) { when (which) {
DialogInterface.BUTTON_NEGATIVE -> { DialogInterface.BUTTON_NEGATIVE -> {
Contact.contacts().removeAt(pos) Contact.contacts().removeAt(pos)
ContactsActivity.saveContacts() ContactsActivity.saveContacts(cxt.applicationContext.filesDir)
this.notifyDataSetChanged() this.notifyDataSetChanged()
} }
DialogInterface.BUTTON_POSITIVE -> { DialogInterface.BUTTON_POSITIVE -> {
@@ -3,8 +3,10 @@ package com.tutpro.baresip
import android.app.Activity import android.app.Activity
import android.content.* import android.content.*
import android.os.Bundle import android.os.Bundle
import android.os.Environment
import android.support.v7.app.AppCompatActivity import android.support.v7.app.AppCompatActivity
import android.util.Log import android.util.Log
import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import android.widget.ImageButton import android.widget.ImageButton
import android.widget.ListView import android.widget.ListView
@@ -19,8 +21,6 @@ class ContactsActivity : AppCompatActivity() {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_contacts) setContentView(R.layout.activity_contacts)
filesPath = applicationContext.filesDir.absolutePath
val listView = findViewById(R.id.contacts) as ListView val listView = findViewById(R.id.contacts) as ListView
val aor = intent.extras.getString("aor") val aor = intent.extras.getString("aor")
@@ -49,8 +49,35 @@ class ContactsActivity : AppCompatActivity() {
if (resultCode == RESULT_OK) clAdapter.notifyDataSetChanged() 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 { override fun onOptionsItemSelected(item: MenuItem): Boolean {
val dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
when (item.itemId) { 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 -> { android.R.id.home -> {
Log.d("Baresip", "Back array was pressed at Contacts") Log.d("Baresip", "Back array was pressed at Contacts")
val i = Intent() val i = Intent()
@@ -70,17 +97,17 @@ class ContactsActivity : AppCompatActivity() {
companion object { companion object {
internal var filesPath = ""
fun saveContacts(path: String = filesPath) { fun saveContacts(path: File): Boolean {
var contents = "" var contents = ""
for (c in Contact.contacts()) for (c in Contact.contacts())
contents += "\"${c.name}\" ${c.uri}\n" contents += "\"${c.name}\" ${c.uri}\n"
Utils.putFileContents(File(path + "/contacts"), contents) return Utils.putFileContents(File(path, "contacts"), contents)
} }
fun restoreContacts(path: String = filesPath) { fun restoreContacts(path: File): Boolean {
val content = Utils.getFileContents(File(path + "/contacts")) val content = Utils.getFileContents(File(path, "contacts"))
if (content == "Failed") return false
Api.contacts_remove() Api.contacts_remove()
Contact.contacts().clear() Contact.contacts().clear()
content.lines().forEach { content.lines().forEach {
@@ -92,6 +119,7 @@ class ContactsActivity : AppCompatActivity() {
Contact.contacts().add(Contact(name, uri)) Contact.contacts().add(Contact(name, uri))
} }
} }
return true
} }
fun findContactURI(name: String): String { fun findContactURI(name: String): String {
@@ -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, callUri.setAdapter(ArrayAdapter(this, android.R.layout.select_dialog_item,
Contact.contacts().map{Contact -> Contact.name})) Contact.contacts().map{Contact -> Contact.name}))
callUri.threshold = 2 callUri.threshold = 2
@@ -657,8 +657,7 @@ class MainActivity : AppCompatActivity() {
super.onNewIntent(intent) super.onNewIntent(intent)
val action = intent.getStringExtra("action") val action = intent.getStringExtra("action")
Log.d("Baresip", "onNewIntent action '$action'") Log.d("Baresip", "onNewIntent action '$action'")
if (action != null) if (action != null) handleIntent(intent)
handleIntent(intent)
} }
private fun handleIntent(intent: Intent) { private fun handleIntent(intent: Intent) {
@@ -16,7 +16,7 @@ object Utils {
fun getFileContents(file: File): String { fun getFileContents(file: File): String {
if (!file.exists()) { if (!file.exists()) {
Log.e("Baresip", "Failed to find file: " + file.path) Log.e("Baresip", "Failed to find file: " + file.path)
return "" return "Failed"
} else { } else {
val length = file.length().toInt() val length = file.length().toInt()
val bytes = ByteArray(length) val bytes = ByteArray(length)
@@ -31,13 +31,13 @@ object Utils {
} catch (e: java.io.IOException) { } catch (e: java.io.IOException) {
Log.e("Baresip", "Failed to read file: " + file.path + ": " + Log.e("Baresip", "Failed to read file: " + file.path + ": " +
e.toString()) e.toString())
return "" return "Failed"
} }
} }
} }
fun putFileContents(file: File, contents: String) { fun putFileContents(file: File, contents: String): Boolean {
try { try {
val fOut = FileOutputStream(file.absoluteFile, false) val fOut = FileOutputStream(file.absoluteFile, false)
val fWriter = OutputStreamWriter(fOut) val fWriter = OutputStreamWriter(fOut)
@@ -47,12 +47,14 @@ object Utils {
fOut.close() fOut.close()
} catch (e: java.io.IOException) { } catch (e: java.io.IOException) {
Log.e("Baresip", "Failed to put contents to file: " + e.toString()) Log.e("Baresip", "Failed to put contents to file: " + e.toString())
return false
} }
} catch (e: java.io.FileNotFoundException) { } catch (e: java.io.FileNotFoundException) {
Log.e("Baresip", "Failed to find contents file: " + e.toString()) Log.e("Baresip", "Failed to find contents file: " + e.toString())
return false
} }
return true
} }
fun getNameValue(string: String, name: String): ArrayList<String> { fun getNameValue(string: String, name: String): ArrayList<String> {
+1 -1
View File
@@ -22,7 +22,7 @@
<EditText <EditText
android:id="@+id/Name" android:id="@+id/Name"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:inputType="textPersonName" android:inputType="textPersonName|textCapWords"
android:textSize="18sp" android:textSize="18sp"
android:layout_width="fill_parent"> android:layout_width="fill_parent">
</EditText> </EditText>