- Ranamed Main Activity Export/Import to Backup/Restore.

- Removed export/import of accounts and contacts.
This commit is contained in:
Juha Heinanen
2019-10-09 21:21:04 +03:00
parent e9f37b5c26
commit 4d48ac82f9
11 changed files with 96 additions and 223 deletions
@@ -4,12 +4,8 @@ import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.Menu
import android.view.MenuItem
import android.widget.*
import android.app.AlertDialog
import android.view.ViewGroup
import android.view.LayoutInflater
import java.util.ArrayList
@@ -69,34 +65,15 @@ class AccountsActivity : AppCompatActivity() {
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.accounts_menu, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.export_accounts -> {
if (Utils.requestPermission(this,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE))
askPassword(getString(R.string.encrypt_password))
}
R.id.import_accounts -> {
if (Utils.requestPermission(this,
android.Manifest.permission.READ_EXTERNAL_STORAGE))
askPassword(getString(R.string.decrypt_password))
}
android.R.id.home -> {
Log.d("Baresip", "Back array was pressed at Accounts")
BaresipService.activities.removeAt(0)
val i = Intent()
setResult(Activity.RESULT_OK, i)
setResult(RESULT_OK, i)
finish()
}
@@ -116,42 +93,6 @@ class AccountsActivity : AppCompatActivity() {
}
private fun askPassword(title: String) {
val builder = AlertDialog.Builder(this)
builder.setTitle(title)
val viewInflated = LayoutInflater.from(this)
.inflate(R.layout.password_dialog, findViewById(android.R.id.content) as ViewGroup,
false)
val input = viewInflated.findViewById(R.id.password) as EditText
builder.setView(viewInflated)
builder.setPositiveButton(android.R.string.ok) { dialog, _ ->
dialog.dismiss()
val password = input.text.toString()
if (password != "") {
if (title == getString(R.string.encrypt_password)) {
if (exportAccounts(password))
Utils.alertView(this, getString(R.string.info),
getString(R.string.exported_accounts))
else
Utils.alertView(this, getString(R.string.error),
getString(R.string.export_accounts_error))
} else {
if (importAccounts(password))
Utils.alertView(this, getString(R.string.info),
getString(R.string.imported_accounts))
else
Utils.alertView(this, getString(R.string.error),
getString(R.string.import_accounts_error))
}
}
}
builder.setNegativeButton(android.R.string.cancel) { dialog, _ ->
dialog.cancel()
}
builder.show()
}
companion object {
var accounts = ArrayList<AccountRow>()
@@ -170,20 +111,6 @@ class AccountsActivity : AppCompatActivity() {
// Log.d("Baresip", "Saved accounts '${accounts}' to '${BaresipService.filesPath}/accounts'")
}
fun exportAccounts(password: String): Boolean {
val content = Utils.getFileContents("${BaresipService.filesPath}/accounts")
if (content == null) return false
return Utils.encryptToFile("${BaresipService.downloadsPath}/accounts.bs",
content, password)
}
fun importAccounts(password: String): Boolean {
val content = Utils.decryptFromFile("${BaresipService.downloadsPath}/accounts.bs",
password)
if (content == null) return false
return Utils.putFileContents("${BaresipService.filesPath}/accounts", content)
}
fun noAccounts(): Boolean {
val contents = Utils.getFileContents(BaresipService.filesPath + "/accounts")
return contents == null || contents.size == 0
@@ -335,7 +335,19 @@ class ConfigActivity : AppCompatActivity() {
if (save) Config.save()
intent.putExtra("restart", restart)
if (restart) {
val restartDialog = android.support.v7.app.AlertDialog.Builder(this)
restartDialog.setMessage(getString(R.string.config_restart))
restartDialog.setPositiveButton(getText(R.string.restart)) { dialog, _ ->
dialog.dismiss()
intent.putExtra("restart", true)
}
restartDialog.setNegativeButton(getText(R.string.cancel)) { dialog, _ ->
dialog.dismiss()
}
restartDialog.create().show()
}
setResult(RESULT_OK, intent)
finish()
@@ -41,11 +41,6 @@ class Contact(var name: String, var uri: String) {
return true
}
fun export(): Boolean {
return Utils.putFileContents(BaresipService.downloadsPath + "/contacts.bs",
Utils.getFileContents(BaresipService.filesPath + "/contacts")!!)
}
fun import(): Boolean {
val contacts = Utils.getFileContents(BaresipService.downloadsPath + "/contacts.bs")
if (contacts != null) {
@@ -4,7 +4,6 @@ import android.app.Activity
import android.content.*
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.Menu
import android.view.MenuItem
import android.widget.ImageButton
import android.widget.ListView
@@ -51,37 +50,10 @@ class ContactsActivity : AppCompatActivity() {
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.contacts_menu, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.export_contacts -> {
if (Contact.export())
Utils.alertView(this, "",
getString(R.string.exported_contacts))
else
Utils.alertView(this,getString(R.string.error),
getString(R.string.export_error))
}
R.id.import_contacts -> {
if (Contact.import()) {
Utils.alertView(this, "",
getString(R.string.imported_contacts))
clAdapter.notifyDataSetChanged()
Contact.save()
} else
Utils.alertView(this,getString(R.string.error),
getString(R.string.import_error))
}
android.R.id.home -> {
Log.d("Baresip", "Back array was pressed at Contacts")
BaresipService.activities.removeAt(0)
@@ -898,12 +898,12 @@ class MainActivity : AppCompatActivity() {
i = Intent(this, AccountsActivity::class.java)
startActivityForResult(i, ACCOUNTS_CODE)
}
R.id.export_all -> {
R.id.backup -> {
if (Utils.requestPermission(this,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE))
askPassword(getString(R.string.encrypt_password))
}
R.id.import_all -> {
R.id.restore -> {
if (Utils.requestPermission(this,
android.Manifest.permission.READ_EXTERNAL_STORAGE))
askPassword(getString(R.string.decrypt_password))
@@ -913,25 +913,29 @@ class MainActivity : AppCompatActivity() {
startActivityForResult(i, ABOUT_CODE)
}
R.id.restart, R.id.quit -> {
if (stopState == "initial") {
Log.d("Baresip", "Quiting")
if (BaresipService.isServiceRunning) {
if (item.itemId == R.id.restart) restart = true
baresipService.setAction("Stop");
startService(baresipService)
quitTimer.start()
} else {
finishAndRemoveTask()
System.exit(0)
}
}
quitRestart(item.itemId == R.id.restart)
}
}
return true
}
private fun quitRestart(reStart: Boolean) {
if (stopState == "initial") {
Log.d("Baresip", "quitRestart Restart = $restart")
if (BaresipService.isServiceRunning) {
restart = reStart
baresipService.setAction("Stop");
startService(baresipService)
quitTimer.start()
} else {
finishAndRemoveTask()
System.exit(0)
}
}
}
private fun askPassword(title: String) {
val builder = android.app.AlertDialog.Builder(this)
val builder = AlertDialog.Builder(this)
builder.setTitle(title)
val viewInflated = LayoutInflater.from(this)
.inflate(R.layout.password_dialog, findViewById(android.R.id.content) as ViewGroup,
@@ -943,9 +947,9 @@ class MainActivity : AppCompatActivity() {
val password = input.text.toString()
if (password != "") {
if (title == getString(R.string.encrypt_password))
exportAll(password)
backup(password)
else
importAll(password)
restore(password)
}
}
builder.setNegativeButton(android.R.string.cancel) { dialog, _ ->
@@ -954,10 +958,10 @@ class MainActivity : AppCompatActivity() {
builder.show()
}
private fun exportAll(password: String) {
private fun backup(password: String) {
val files = arrayOf("accounts", "config", "contacts", "history", "messages", "uuid",
"zrtp_cache.dat", "zrtp_zid", "cert.pem", "ca_cert", "ca_certs.crt")
val exportFilePath = BaresipService.downloadsPath + "/baresip.bs"
val backupFilePath = BaresipService.downloadsPath + "/baresip.bs"
val zipFilePath = BaresipService.filesPath + "/baresip.zip"
if (!Utils.zip(files, "baresip.zip")) {
Utils.alertView(this, getString(R.string.error), "Failed to write zip file 'baresip.zip")
@@ -968,20 +972,20 @@ class MainActivity : AppCompatActivity() {
Utils.alertView(this, getString(R.string.error), "Failed to read zip file 'baresip.zip")
return
}
if (!Utils.encryptToFile(exportFilePath, content, password)) {
Utils.alertView(this, getString(R.string.error), getString(R.string.export_all_failed))
if (!Utils.encryptToFile(backupFilePath, content, password)) {
Utils.alertView(this, getString(R.string.error), getString(R.string.backup_failed))
return
}
Utils.alertView(this, getString(R.string.info), getString(R.string.exported_all))
Utils.alertView(this, getString(R.string.info), getString(R.string.backed_up))
Utils.deleteFile(zipFilePath)
}
private fun importAll(password: String) {
val importFilePath = BaresipService.downloadsPath + "/baresip.bs"
private fun restore(password: String) {
val backupFilePath = BaresipService.downloadsPath + "/baresip.bs"
val zipFilePath = BaresipService.filesPath + "/baresip.zip"
val zipData = Utils.decryptFromFile(importFilePath, password)
val zipData = Utils.decryptFromFile(backupFilePath, password)
if (zipData == null) {
Utils.alertView(this, getString(R.string.error), getString(R.string.import_all_failed))
Utils.alertView(this, getString(R.string.error), getString(R.string.restore_failed))
return
}
if (!Utils.putFileContents(zipFilePath, zipData)) {
@@ -994,8 +998,18 @@ class MainActivity : AppCompatActivity() {
"Failed to unzip file 'baresip.zip'")
return
}
Utils.alertView(this, getString(R.string.info), getString(R.string.imported_all))
Utils.deleteFile(zipFilePath)
Log.d("Baresip", "Showing restart dialog")
val restartDialog = AlertDialog.Builder(this)
restartDialog.setMessage(getString(R.string.restored))
restartDialog.setPositiveButton(getText(R.string.restart)) { dialog, _ ->
quitRestart(true)
dialog.dismiss()
}
restartDialog.setNegativeButton(getText(R.string.cancel)) { dialog, _ ->
dialog.dismiss()
}
restartDialog.create().show()
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
@@ -1007,7 +1021,7 @@ class MainActivity : AppCompatActivity() {
if (UserAgent.uas().size > 0) {
if ((aorSpinner.selectedItemPosition == -1) ||
(aorSpinner.selectedItemPosition >= UserAgent.uas().size))
aorSpinner.setSelection(0)
aorSpinner.setSelection(0)
else
updateIcons(UserAgent.uas()[aorSpinner.selectedItemPosition].account)
} else {
@@ -1027,10 +1041,8 @@ class MainActivity : AppCompatActivity() {
}
CONFIG_CODE -> {
if ((resultCode == RESULT_OK) &&
(data!!.getBooleanExtra("restart", true)))
Utils.alertView(this, getString(R.string.notice),
getString(R.string.restart_needed))
if ((data != null) && data.hasExtra("restart"))
quitRestart(true)
}
CALLS_CODE -> {
@@ -280,7 +280,10 @@ object Utils {
fun deleteFile(filePath: String) {
val file = File(filePath)
if (file.exists()) file.delete()
if (file.exists()) {
Log.d("Baresip", "Deleting file '$filePath'")
file.delete()
}
}
fun getFileContents(filePath: String): ByteArray? {
@@ -411,9 +414,13 @@ object Utils {
}
fun unZip(zipFilePath: String): Boolean {
val allFiles = listOf("accounts", "config", "contacts", "history", "messages", "uuid",
"zrtp_cache.dat", "zrtp_zid", "cert.pem", "ca_cert", "ca_certs.crt")
val zipFiles = mutableListOf<String>()
try {
ZipFile(zipFilePath).use { zip ->
zip.entries().asSequence().forEach { entry ->
zipFiles.add(entry.name.substringAfterLast("/"))
zip.getInputStream(entry).use { input ->
File(entry.name).outputStream().use { output ->
input.copyTo(output)
@@ -425,6 +432,9 @@ object Utils {
Log.e("Baresip", "Failed to unzip file '$zipFilePath': $e")
return false
}
(allFiles - zipFiles).iterator().forEach {
deleteFile(BaresipService.filesPath + "/$it")
}
return true
}