- 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

View File

@ -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

View File

@ -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()

View File

@ -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) {

View File

@ -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)

View File

@ -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 -> {

View File

@ -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
}

View File

@ -1,9 +0,0 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/export_accounts"
android:title="@string/export_accounts" />
<item android:id="@+id/import_accounts"
android:title="@string/import_accounts" />
</menu>

View File

@ -1,9 +0,0 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/export_contacts"
android:title="@string/export_contacts" />
<item android:id="@+id/import_contacts"
android:title="@string/import_contacts" />
</menu>

View File

@ -6,11 +6,11 @@
<item android:id="@+id/accounts"
android:title="@string/accounts" />
<item android:id="@+id/export_all"
android:title="@string/export_all" />
<item android:id="@+id/backup"
android:title="@string/backup" />
<item android:id="@+id/import_all"
android:title="@string/import_all" />
<item android:id="@+id/restore"
android:title="@string/restore" />
<item android:id="@+id/about"
android:title="@string/about" />

View File

@ -107,22 +107,6 @@
</string>
<string name="encrypt_password">Salasana</string>
<string name="decrypt_password">Salasana</string>
<string name="export_accounts">Tallenna</string>
<string name="exported_accounts">Tilit tallennettiin
Download-kansion tiedostoon \'accounts.bs\'.
</string>
<string name="export_accounts_error">"Tilien tallennus
Download-kansioon epäonnistui. Tarkista Sovellukset → baresip → Käyttöluvat → Tallennustila.
</string>
<string name="import_accounts">Palauta</string>
<string name="imported_accounts">Tilit palautettiin
Download-kansiosta. Sinun pitää käynnistää baresip uudelleen.
</string>
<string name="import_accounts_error">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.
</string>
<string name="delete_account">Haluatko poistaa tilin
\'%1$s\'?
</string>
@ -226,6 +210,9 @@
</string>
<string name="read_cert_error">Tiedoston \'cert.pem\' luku Download-kansiosta epäonnistui.</string>
<string name="read_ca_certs_error">Tiedoston \'ca_certs.crt\' luku Download-kansiosta epäonnistui.</string>
<string name="config_restart">baresip täytyy käynnistää uudelleen, jotta saat uuden
konfiguraation käyttöön. Käynnistä nyt?
</string>
<!-- Contact Activity -->
<string name="contact">Kontakti</string>
<string name="new_contact">Uusi kontakti</string>
@ -240,19 +227,6 @@
<string name="send_message">Lähetä viesti</string>
<string name="contact_delete_question">Haluatko poistaa kontaktin \'%1$s\'?</string>
<string name="contacts_exceeded">Kontaktiesi enimmäismäärä %1$d on ylittynyt.</string>
<string name="export_contacts">Tallenna</string>
<string name="exported_contacts">Kontaktit tallennettiin Download-kansion tiedostoon
\'contacts.bs\'.
</string>
<string name="export_error">"Kontaktien tallennus Download kansioon epäonnistui.
Tarkista Sovellukset → baresip → Käyttöluvat → Tallennustila.
</string>
<string name="import_contacts">Palauta</string>
<string name="imported_contacts">Kontaktit palautettiin.</string>
<string name="import_error">Kontantien palauttaminen Download-kansiosta epäonnistui.
Tarkista Sovellukset → baresip → Käyttöluvat → Tallennustila ja että tiedosto \'contacts.bs\' on
Download-kansiossa.
</string>
<!-- Generic -->
<string name="alert">Varoitus</string>
<string name="info">Tieto</string>
@ -272,6 +246,8 @@
<string name="status">Tila</string>
<string name="error">Virhe</string>
<!-- Main Activity -->
<string name="backup">Tallenna</string>
<string name="restore">Palauta</string>
<string name="about">Tietoja</string>
<string name="restart">Käynnistä uudelleen</string>
<string name="quit">Lopeta</string>
@ -317,8 +293,6 @@
</string>
<string name="call_failed">Puhelu epäonnistui</string>
<string name="call_closed">Puhelu on päättynyt</string>
<string name="restart_needed">Sinun pitää käynnistää baresip
uudelleen, jotta saat uuden konfiguraation käyttöön.</string>
<string name="call_not_secure">Tämä puhelu EI ole turvallinen!</string>
<string name="peer_not_verified">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."
</string>
<string name="exported_all">Sovelluksen kaikki data talletettiin Download-kansion tiedostoon
<string name="backed_up">Sovelluksen tiedot talletettiin Download-kansion tiedostoon
\'baresip.bs\'.
</string>
<string name="imported_all">Sovelluksen kaikki data palautettiin. Sinun pitää käynnistää
baresip uudelleen.
<string name="backup_failed">Sovelluksen tietojen talletus Download-kansion tiedostoon
\'baresip.bs\' epäonnistui. Check Apps → baresip → Permissions → Storage</string>
<string name="restored">Sovelluksen tiedot palautettiin. baresip pitää käynnistää
uudelleen. Käynnistä uudelleen nyt?
</string>
<string name="import_all_failed">Sovelluksen data palauttaminen Download-kansiosta epäonnistui.
<string name="restore_failed">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.
</string>

View File

@ -97,17 +97,6 @@
<string name="account_allocation_failure">"Failed to allocate new account.</string>
<string name="encrypt_password">Encrypt Password</string>
<string name="decrypt_password">Decrypt Password</string>
<string name="export_accounts">Export</string>
<string name="exported_accounts">Exported accounts to Download folder file \'accounts.bs\'.</string>
<string name="export_accounts_error">"Failed to export accounts to Download folder.
Check Apps → baresip → Permissions → Storage."
</string>
<string name="import_accounts">Import</string>
<string name="imported_accounts">Accounts imported. You need to restart baresip.</string>
<string name="import_accounts_error">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."
</string>
<string name="delete_account">Do you want to delete account \'%1$s\'?</string>
<!-- Baresip Service -->
@ -210,6 +199,9 @@
to factory default values.</string>
<string name="read_cert_error">Failed to read file \'cert.pem\' from Download directory.</string>
<string name="read_ca_certs_error">Failed to read file \'ca_certs.crt\' from Download directory.</string>
<string name="config_restart">You need to restart baresip in order to activate the new
configuration. Restart now?
</string>
<!-- Contact Activity -->
<string name="contact">Contact</string>
@ -227,12 +219,6 @@
<string name="send_message">Send Message</string>
<string name="contact_delete_question">Do you want to delete contact \'%1$s\'?</string>
<string name="contacts_exceeded">Your maximum number of contacts %1%d has been exceeded.</string>
<string name="exported_contacts">Exported contacts to Download folder file \'contacts.bs\'.</string>
<string name="export_error">"Failed to export contacts to Download folder.
Check Apps → baresip → Permissions → Storage."</string>
<string name="imported_contacts">Contacs imported.</string>
<string name="import_error">Failed to import contacts from Download folder. Check Apps →
baresip → Permissions → Storage and that file \'contacts.bs\' exists in the folder.</string>
<!-- Generic -->
<string name="alert">Alert</string>
@ -256,8 +242,8 @@
<string name="dots" translatable="false"></string>
<!-- Main Activity -->
<string name="export_all">Export</string>
<string name="import_all">Import</string>
<string name="backup">Backup</string>
<string name="restore">Restore</string>
<string name="about">About</string>
<string name="restart">Restart</string>
<string name="quit">Quit</string>
@ -296,19 +282,20 @@
<string name="transfer_query">Do you accept to transfer call to \'%1$s\'?</string>
<string name="call_failed">Call failed</string>
<string name="call_closed">Call closed</string>
<string name="restart_needed">You need to restart baresip in order to activate the new config.</string>
<string name="call_not_secure">This call is NOT secure!</string>
<string name="peer_not_verified">This call is SECURE, but peer is NOT verified!</string>
<string name="call_is_secure">This call is SECURE and peer is VERIFIED!
Do you want to unverify the peer?
</string>
<string name="unverify">Unverify</string>
<string name="exported_all">Exported all application data to Download folder file \'baresip.bs\'.</string>
<string name="export_all_failed">Failed to export application data to Download folder file
<string name="backed_up">Application data backed up to Download folder file \'baresip.bs\'.</string>
<string name="backup_failed">Failed to back up application data to Download folder file
\'baresip.bs\'. Check Apps → baresip → Permissions → Storage</string>
<string name="imported_all">All application data imported. You need to restart baresip.</string>
<string name="import_all_failed">Failed to import application data from Download folder. Check Apps →
baresip → Permissions → Storage and that exported file \'baresip.bs\' exists in the folder
<string name="restored">Application data restored. baresip needs to be restarted.
Restart now?
</string>
<string name="restore_failed">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.
</string>