diff options
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/activities/ConfigureActivity.kt')
-rw-r--r-- | app/src/main/java/org/pacien/tincapp/activities/ConfigureActivity.kt | 190 |
1 files changed, 95 insertions, 95 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/activities/ConfigureActivity.kt b/app/src/main/java/org/pacien/tincapp/activities/ConfigureActivity.kt index 6c29a53..ec17c11 100644 --- a/app/src/main/java/org/pacien/tincapp/activities/ConfigureActivity.kt +++ b/app/src/main/java/org/pacien/tincapp/activities/ConfigureActivity.kt | |||
@@ -26,102 +26,102 @@ import org.pacien.tincapp.extensions.Java.exceptionallyAccept | |||
26 | */ | 26 | */ |
27 | class ConfigureActivity : BaseActivity() { | 27 | class ConfigureActivity : BaseActivity() { |
28 | 28 | ||
29 | companion object { | 29 | companion object { |
30 | val REQUEST_SCAN = 0 | 30 | val REQUEST_SCAN = 0 |
31 | val SCAN_PROVIDER = "com.google.zxing.client.android" | 31 | val SCAN_PROVIDER = "com.google.zxing.client.android" |
32 | } | ||
33 | |||
34 | private var joinDialog: View? = null | ||
35 | |||
36 | override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { | ||
37 | super.onActivityResult(requestCode, resultCode, data) | ||
38 | |||
39 | if (requestCode == REQUEST_SCAN && resultCode == Activity.RESULT_OK) | ||
40 | joinDialog?.invitation_url?.setText(data!!.getStringExtra("SCAN_RESULT").trim()) | ||
41 | } | ||
42 | |||
43 | override fun onCreate(savedInstanceState: Bundle?) { | ||
44 | super.onCreate(savedInstanceState) | ||
45 | supportActionBar!!.setDisplayHomeAsUpEnabled(true) | ||
46 | layoutInflater.inflate(R.layout.page_configure, main_content) | ||
47 | writeContent() | ||
48 | } | ||
49 | |||
50 | fun scanCode(@Suppress("UNUSED_PARAMETER") v: View) { | ||
51 | try { | ||
52 | startActivityForResult(Intent("$SCAN_PROVIDER.SCAN"), REQUEST_SCAN) | ||
53 | } catch (e: ActivityNotFoundException) { | ||
54 | AlertDialog.Builder(this).setTitle(R.string.action_scan_qr_code) | ||
55 | .setMessage(R.string.message_no_qr_code_scanner) | ||
56 | .setPositiveButton(R.string.action_install) { _, _ -> | ||
57 | startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=$SCAN_PROVIDER"))) | ||
58 | }.setNegativeButton(R.string.action_cancel, App.dismissAction).show() | ||
32 | } | 59 | } |
33 | 60 | } | |
34 | private var joinDialog: View? = null | 61 | |
35 | 62 | fun openGenerateConfDialog(@Suppress("UNUSED_PARAMETER") v: View) { | |
36 | override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { | 63 | val genDialog = layoutInflater.inflate(R.layout.dialog_network_generate, main_content, false) |
37 | super.onActivityResult(requestCode, resultCode, data) | 64 | AlertDialog.Builder(this).setTitle(R.string.title_new_network).setView(genDialog) |
38 | 65 | .setPositiveButton(R.string.action_create) { _, _ -> | |
39 | if (requestCode == REQUEST_SCAN && resultCode == Activity.RESULT_OK) | 66 | generateConf( |
40 | joinDialog?.invitation_url?.setText(data!!.getStringExtra("SCAN_RESULT").trim()) | 67 | genDialog.new_net_name.text.toString(), |
41 | } | 68 | genDialog.new_node_name.text.toString(), |
42 | 69 | genDialog.new_passphrase.text.toString()) | |
43 | override fun onCreate(savedInstanceState: Bundle?) { | 70 | }.setNegativeButton(R.string.action_cancel, App.dismissAction).show() |
44 | super.onCreate(savedInstanceState) | 71 | } |
45 | supportActionBar!!.setDisplayHomeAsUpEnabled(true) | 72 | |
46 | layoutInflater.inflate(R.layout.page_configure, main_content) | 73 | fun openJoinNetworkDialog(@Suppress("UNUSED_PARAMETER") v: View) { |
47 | writeContent() | 74 | joinDialog = layoutInflater.inflate(R.layout.dialog_network_join, main_content, false) |
48 | } | 75 | AlertDialog.Builder(this).setTitle(R.string.title_join_network).setView(joinDialog) |
49 | 76 | .setPositiveButton(R.string.action_join) { _, _ -> | |
50 | fun scanCode(@Suppress("UNUSED_PARAMETER") v: View) { | 77 | joinNetwork( |
51 | try { | 78 | joinDialog!!.net_name.text.toString(), |
52 | startActivityForResult(Intent("$SCAN_PROVIDER.SCAN"), REQUEST_SCAN) | 79 | joinDialog!!.invitation_url.text.toString(), |
53 | } catch (e: ActivityNotFoundException) { | 80 | joinDialog!!.join_passphrase.text.toString()) |
54 | AlertDialog.Builder(this).setTitle(R.string.action_scan_qr_code) | 81 | }.setNegativeButton(R.string.action_cancel, App.dismissAction).show() |
55 | .setMessage(R.string.message_no_qr_code_scanner) | 82 | } |
56 | .setPositiveButton(R.string.action_install) { _, _ -> | 83 | |
57 | startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=$SCAN_PROVIDER"))) | 84 | fun openEncryptDecryptPrivateKeyDialog(@Suppress("UNUSED_PARAMETER") v: View) { |
58 | }.setNegativeButton(R.string.action_cancel, App.dismissAction).show() | 85 | val encryptDecryptDialog = layoutInflater.inflate(R.layout.dialog_encrypt_decrypt_keys, main_content, false) |
59 | } | 86 | AlertDialog.Builder(this).setTitle(R.string.title_private_keys_encryption).setView(encryptDecryptDialog) |
60 | } | 87 | .setPositiveButton(R.string.action_apply) { _, _ -> |
61 | 88 | encryptDecryptPrivateKeys( | |
62 | fun openGenerateConfDialog(@Suppress("UNUSED_PARAMETER") v: View) { | 89 | encryptDecryptDialog!!.enc_dec_net_name.text.toString(), |
63 | val genDialog = layoutInflater.inflate(R.layout.dialog_network_generate, main_content, false) | 90 | encryptDecryptDialog.enc_dec_current_passphrase.text.toString(), |
64 | AlertDialog.Builder(this).setTitle(R.string.title_new_network).setView(genDialog) | 91 | encryptDecryptDialog.enc_dec_new_passphrase.text.toString()) |
65 | .setPositiveButton(R.string.action_create) { _, _ -> | 92 | }.setNegativeButton(R.string.action_cancel, App.dismissAction).show() |
66 | generateConf( | 93 | } |
67 | genDialog.new_net_name.text.toString(), | 94 | |
68 | genDialog.new_node_name.text.toString(), | 95 | private fun writeContent() { |
69 | genDialog.new_passphrase.text.toString()) | 96 | text_configuration_directory.text = AppPaths.confDir().absolutePath |
70 | }.setNegativeButton(R.string.action_cancel, App.dismissAction).show() | 97 | text_log_directory.text = AppPaths.cacheDir().absolutePath |
71 | } | 98 | text_tinc_binary.text = AppPaths.tinc().absolutePath |
72 | 99 | } | |
73 | fun openJoinNetworkDialog(@Suppress("UNUSED_PARAMETER") v: View) { | 100 | |
74 | joinDialog = layoutInflater.inflate(R.layout.dialog_network_join, main_content, false) | 101 | private fun generateConf(netName: String, nodeName: String, passphrase: String? = null) = execAction( |
75 | AlertDialog.Builder(this).setTitle(R.string.title_join_network).setView(joinDialog) | 102 | R.string.message_generating_configuration, |
76 | .setPositiveButton(R.string.action_join) { _, _ -> | 103 | Tinc.init(netName, nodeName) |
77 | joinNetwork( | 104 | .thenCompose { TincApp.removeScripts(netName) } |
78 | joinDialog!!.net_name.text.toString(), | 105 | .thenCompose { TincApp.setPassphrase(netName, newPassphrase = passphrase) }) |
79 | joinDialog!!.invitation_url.text.toString(), | 106 | |
80 | joinDialog!!.join_passphrase.text.toString()) | 107 | private fun joinNetwork(netName: String, url: String, passphrase: String? = null) = execAction( |
81 | }.setNegativeButton(R.string.action_cancel, App.dismissAction).show() | 108 | R.string.message_joining_network, |
82 | } | 109 | Tinc.join(netName, url) |
83 | 110 | .thenCompose { TincApp.removeScripts(netName) } | |
84 | fun openEncryptDecryptPrivateKeyDialog(@Suppress("UNUSED_PARAMETER") v: View) { | 111 | .thenCompose { TincApp.generateIfaceCfg(netName) } |
85 | val encryptDecryptDialog = layoutInflater.inflate(R.layout.dialog_encrypt_decrypt_keys, main_content, false) | 112 | .thenCompose { TincApp.setPassphrase(netName, newPassphrase = passphrase) }) |
86 | AlertDialog.Builder(this).setTitle(R.string.title_private_keys_encryption).setView(encryptDecryptDialog) | 113 | |
87 | .setPositiveButton(R.string.action_apply) { _, _ -> | 114 | private fun encryptDecryptPrivateKeys(netName: String, currentPassphrase: String, newPassphrase: String) = execAction( |
88 | encryptDecryptPrivateKeys( | 115 | R.string.message_encrypting_decrypting_private_keys, |
89 | encryptDecryptDialog!!.enc_dec_net_name.text.toString(), | 116 | TincApp.setPassphrase(netName, currentPassphrase, newPassphrase)) |
90 | encryptDecryptDialog.enc_dec_current_passphrase.text.toString(), | 117 | |
91 | encryptDecryptDialog.enc_dec_new_passphrase.text.toString()) | 118 | private fun execAction(@StringRes label: Int, action: CompletableFuture<Void>) { |
92 | }.setNegativeButton(R.string.action_cancel, App.dismissAction).show() | 119 | showProgressDialog(label).let { progressDialog -> |
93 | } | 120 | action |
94 | 121 | .whenComplete { _, _ -> progressDialog.dismiss() } | |
95 | private fun writeContent() { | 122 | .thenAccept { notify(R.string.message_network_configuration_written) } |
96 | text_configuration_directory.text = AppPaths.confDir().absolutePath | 123 | .exceptionallyAccept { runOnUiThread { showErrorDialog(it.cause!!.localizedMessage) } } |
97 | text_log_directory.text = AppPaths.cacheDir().absolutePath | ||
98 | text_tinc_binary.text = AppPaths.tinc().absolutePath | ||
99 | } | ||
100 | |||
101 | private fun generateConf(netName: String, nodeName: String, passphrase: String? = null) = execAction( | ||
102 | R.string.message_generating_configuration, | ||
103 | Tinc.init(netName, nodeName) | ||
104 | .thenCompose { TincApp.removeScripts(netName) } | ||
105 | .thenCompose { TincApp.setPassphrase(netName, newPassphrase = passphrase) }) | ||
106 | |||
107 | private fun joinNetwork(netName: String, url: String, passphrase: String? = null) = execAction( | ||
108 | R.string.message_joining_network, | ||
109 | Tinc.join(netName, url) | ||
110 | .thenCompose { TincApp.removeScripts(netName) } | ||
111 | .thenCompose { TincApp.generateIfaceCfg(netName) } | ||
112 | .thenCompose { TincApp.setPassphrase(netName, newPassphrase = passphrase) }) | ||
113 | |||
114 | private fun encryptDecryptPrivateKeys(netName: String, currentPassphrase: String, newPassphrase: String) = execAction( | ||
115 | R.string.message_encrypting_decrypting_private_keys, | ||
116 | TincApp.setPassphrase(netName, currentPassphrase, newPassphrase)) | ||
117 | |||
118 | private fun execAction(@StringRes label: Int, action: CompletableFuture<Void>) { | ||
119 | showProgressDialog(label).let { progressDialog -> | ||
120 | action | ||
121 | .whenComplete { _, _ -> progressDialog.dismiss() } | ||
122 | .thenAccept { notify(R.string.message_network_configuration_written) } | ||
123 | .exceptionallyAccept { runOnUiThread { showErrorDialog(it.cause!!.localizedMessage) } } | ||
124 | } | ||
125 | } | 124 | } |
125 | } | ||
126 | 126 | ||
127 | } | 127 | } |