From 4d48ac82f9dec0ac2011481f922961c9352ed1d3 Mon Sep 17 00:00:00 2001 From: Juha Heinanen Date: Wed, 9 Oct 2019 21:21:04 +0300 Subject: [PATCH] - Ranamed Main Activity Export/Import to Backup/Restore. - Removed export/import of accounts and contacts. --- .../com/tutpro/baresip/AccountsActivity.kt | 75 +----------------- .../com/tutpro/baresip/ConfigActivity.kt | 14 +++- .../main/kotlin/com/tutpro/baresip/Contact.kt | 5 -- .../com/tutpro/baresip/ContactsActivity.kt | 28 ------- .../kotlin/com/tutpro/baresip/MainActivity.kt | 76 +++++++++++-------- .../main/kotlin/com/tutpro/baresip/Utils.kt | 12 ++- app/src/main/res/menu/accounts_menu.xml | 9 --- app/src/main/res/menu/contacts_menu.xml | 9 --- app/src/main/res/menu/main_menu.xml | 8 +- app/src/main/res/values-fi/strings.xml | 46 +++-------- app/src/main/res/values/strings.xml | 37 +++------ 11 files changed, 96 insertions(+), 223 deletions(-) delete mode 100644 app/src/main/res/menu/accounts_menu.xml delete mode 100644 app/src/main/res/menu/contacts_menu.xml diff --git a/app/src/main/kotlin/com/tutpro/baresip/AccountsActivity.kt b/app/src/main/kotlin/com/tutpro/baresip/AccountsActivity.kt index 6060b73a..3d13121a 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/AccountsActivity.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/AccountsActivity.kt @@ -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() @@ -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 diff --git a/app/src/main/kotlin/com/tutpro/baresip/ConfigActivity.kt b/app/src/main/kotlin/com/tutpro/baresip/ConfigActivity.kt index 66d12b04..ecdb788c 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/ConfigActivity.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/ConfigActivity.kt @@ -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() diff --git a/app/src/main/kotlin/com/tutpro/baresip/Contact.kt b/app/src/main/kotlin/com/tutpro/baresip/Contact.kt index c412fce0..2b331fea 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/Contact.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/Contact.kt @@ -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) { diff --git a/app/src/main/kotlin/com/tutpro/baresip/ContactsActivity.kt b/app/src/main/kotlin/com/tutpro/baresip/ContactsActivity.kt index 5e9f5d0c..229aa66e 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/ContactsActivity.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/ContactsActivity.kt @@ -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) diff --git a/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt b/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt index 4790c8ee..4c922af9 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt @@ -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 -> { diff --git a/app/src/main/kotlin/com/tutpro/baresip/Utils.kt b/app/src/main/kotlin/com/tutpro/baresip/Utils.kt index 7c3f599d..f8208bcb 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/Utils.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/Utils.kt @@ -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() 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 } diff --git a/app/src/main/res/menu/accounts_menu.xml b/app/src/main/res/menu/accounts_menu.xml deleted file mode 100644 index eac60e9a..00000000 --- a/app/src/main/res/menu/accounts_menu.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - diff --git a/app/src/main/res/menu/contacts_menu.xml b/app/src/main/res/menu/contacts_menu.xml deleted file mode 100644 index e86cb3c2..00000000 --- a/app/src/main/res/menu/contacts_menu.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - diff --git a/app/src/main/res/menu/main_menu.xml b/app/src/main/res/menu/main_menu.xml index 62fcc10f..7b0bcc55 100644 --- a/app/src/main/res/menu/main_menu.xml +++ b/app/src/main/res/menu/main_menu.xml @@ -6,11 +6,11 @@ - + - + diff --git a/app/src/main/res/values-fi/strings.xml b/app/src/main/res/values-fi/strings.xml index 23583d1a..36c432f4 100644 --- a/app/src/main/res/values-fi/strings.xml +++ b/app/src/main/res/values-fi/strings.xml @@ -107,22 +107,6 @@ Salasana Salasana - Tallenna - Tilit tallennettiin - Download-kansion tiedostoon \'accounts.bs\'. - - "Tilien tallennus - Download-kansioon epäonnistui. Tarkista Sovellukset → baresip → Käyttöluvat → Tallennustila. - - Palauta - Tilit palautettiin - Download-kansiosta. Sinun pitää käynnistää baresip uudelleen. - - Tilien palauttaminen - Download-kansiosta epäonnistui. Tarkista Sovellukset → baresip → - Käyttöluvat → Tallennustila ja että tiedosto \'accounts.bs\' on - kansiossa ja (jos on) että annoit oikean salasanan. - Haluatko poistaa tilin \'%1$s\'? @@ -226,6 +210,9 @@ Tiedoston \'cert.pem\' luku Download-kansiosta epäonnistui. Tiedoston \'ca_certs.crt\' luku Download-kansiosta epäonnistui. + baresip täytyy käynnistää uudelleen, jotta saat uuden + konfiguraation käyttöön. Käynnistä nyt? + Kontakti Uusi kontakti @@ -240,19 +227,6 @@ Lähetä viesti Haluatko poistaa kontaktin \'%1$s\'? Kontaktiesi enimmäismäärä %1$d on ylittynyt. - Tallenna - Kontaktit tallennettiin Download-kansion tiedostoon - \'contacts.bs\'. - - "Kontaktien tallennus Download kansioon epäonnistui. - Tarkista Sovellukset → baresip → Käyttöluvat → Tallennustila. - - Palauta - Kontaktit palautettiin. - Kontantien palauttaminen Download-kansiosta epäonnistui. - Tarkista Sovellukset → baresip → Käyttöluvat → Tallennustila ja että tiedosto \'contacts.bs\' on - Download-kansiossa. - Varoitus Tieto @@ -272,6 +246,8 @@ Tila Virhe + Tallenna + Palauta Tietoja Käynnistä uudelleen Lopeta @@ -317,8 +293,6 @@ Puhelu epäonnistui Puhelu on päättynyt - Sinun pitää käynnistää baresip - uudelleen, jotta saat uuden konfiguraation käyttöön. Tämä puhelu EI ole turvallinen! Tämä puhelu on turvallinen, mutta kohdetta ei ole todennettu! @@ -335,13 +309,15 @@ tämän baresip-sovelluksen TLS-sertifikaatin ja yksityisen avaimen, on ladattu tai ladataan Download-kansiosta." - Sovelluksen kaikki data talletettiin Download-kansion tiedostoon + Sovelluksen tiedot talletettiin Download-kansion tiedostoon \'baresip.bs\'. - Sovelluksen kaikki data palautettiin. Sinun pitää käynnistää - baresip uudelleen. + Sovelluksen tietojen talletus Download-kansion tiedostoon + \'baresip.bs\' epäonnistui. Check Apps → baresip → Permissions → Storage + Sovelluksen tiedot palautettiin. baresip pitää käynnistää + uudelleen. Käynnistä uudelleen nyt? - Sovelluksen data palauttaminen Download-kansiosta epäonnistui. + Sovelluksen data palauttaminen Download-kansiosta epäonnistui. Tarkista Sovellukset → baresip → Käyttöluvat → Tallennustila ja että tallennettu tiedosto \'baresip.bs\' on kansiossa ja (jos on) että annoit oikean salasanan. diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3a53dd1c..ce5da7fb 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -97,17 +97,6 @@ "Failed to allocate new account. Encrypt Password Decrypt Password - Export - Exported accounts to Download folder file \'accounts.bs\'. - "Failed to export accounts to Download folder. - Check Apps → baresip → Permissions → Storage." - - Import - Accounts imported. You need to restart baresip. - Failed to import accounts from Download folder file. - Check Apps → baresip → Permissions → Storage and that exported file \'accounts.bs\' exists in - the folder and, if so, you gave correct Decrypt Password." - Do you want to delete account \'%1$s\'? @@ -210,6 +199,9 @@ to factory default values. Failed to read file \'cert.pem\' from Download directory. Failed to read file \'ca_certs.crt\' from Download directory. + You need to restart baresip in order to activate the new + configuration. Restart now? + Contact @@ -227,12 +219,6 @@ Send Message Do you want to delete contact \'%1$s\'? Your maximum number of contacts %1%d has been exceeded. - Exported contacts to Download folder file \'contacts.bs\'. - "Failed to export contacts to Download folder. - Check Apps → baresip → Permissions → Storage." - Contacs imported. - Failed to import contacts from Download folder. Check Apps → - baresip → Permissions → Storage and that file \'contacts.bs\' exists in the folder. Alert @@ -256,8 +242,8 @@ - Export - Import + Backup + Restore About Restart Quit @@ -296,19 +282,20 @@ Do you accept to transfer call to \'%1$s\'? Call failed Call closed - You need to restart baresip in order to activate the new config. This call is NOT secure! This call is SECURE, but peer is NOT verified! This call is SECURE and peer is VERIFIED! Do you want to unverify the peer? Unverify - Exported all application data to Download folder file \'baresip.bs\'. - Failed to export application data to Download folder file + Application data backed up to Download folder file \'baresip.bs\'. + Failed to back up application data to Download folder file \'baresip.bs\'. Check Apps → baresip → Permissions → Storage - All application data imported. You need to restart baresip. - Failed to import application data from Download folder. Check Apps → - baresip → Permissions → Storage and that exported file \'baresip.bs\' exists in the folder + Application data restored. baresip needs to be restarted. + Restart now? + + Failed to restore application data from Download folder. Check Apps → + baresip → Permissions → Storage and that backup file \'baresip.bs\' exists in the folder and, if so, you gave correct Decrypt Password.