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 | 61 |
1 files changed, 59 insertions, 2 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 3590f99..d11809c 100644 --- a/app/src/main/java/org/pacien/tincapp/activities/ConfigureActivity.kt +++ b/app/src/main/java/org/pacien/tincapp/activities/ConfigureActivity.kt | |||
@@ -1,11 +1,20 @@ | |||
1 | package org.pacien.tincapp.activities | 1 | package org.pacien.tincapp.activities |
2 | 2 | ||
3 | import android.os.Bundle | 3 | import android.os.Bundle |
4 | import android.support.annotation.StringRes | ||
5 | import android.support.v7.app.AlertDialog | ||
4 | import android.view.View | 6 | import android.view.View |
7 | import android.view.ViewGroup | ||
8 | import android.widget.EditText | ||
9 | import android.widget.FrameLayout | ||
10 | import java8.util.concurrent.CompletableFuture | ||
5 | import kotlinx.android.synthetic.main.base.* | 11 | import kotlinx.android.synthetic.main.base.* |
6 | import kotlinx.android.synthetic.main.page_configure.* | 12 | import kotlinx.android.synthetic.main.page_configure.* |
7 | import org.pacien.tincapp.R | 13 | import org.pacien.tincapp.R |
14 | import org.pacien.tincapp.commands.Tinc | ||
15 | import org.pacien.tincapp.commands.TincApp | ||
8 | import org.pacien.tincapp.context.AppPaths | 16 | import org.pacien.tincapp.context.AppPaths |
17 | import org.pacien.tincapp.extensions.Java.exceptionallyAccept | ||
9 | 18 | ||
10 | /** | 19 | /** |
11 | * @author pacien | 20 | * @author pacien |
@@ -19,13 +28,61 @@ class ConfigureActivity : BaseActivity() { | |||
19 | writeContent() | 28 | writeContent() |
20 | } | 29 | } |
21 | 30 | ||
31 | fun openGenerateConfDialog(@Suppress("UNUSED_PARAMETER") v: View) { | ||
32 | val netNameField = EditText(this) | ||
33 | netNameField.layoutParams = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT) | ||
34 | netNameField.setHint(R.string.field_net_name) | ||
35 | |||
36 | val dialogFrame = layoutInflater.inflate(R.layout.dialog_frame, main_content, false) as ViewGroup | ||
37 | dialogFrame.addView(netNameField) | ||
38 | |||
39 | AlertDialog.Builder(this).setTitle(R.string.title_new_network).setView(dialogFrame) | ||
40 | .setPositiveButton(R.string.action_create) { _, _ -> generateConf(netNameField.text.toString()) } | ||
41 | .setNegativeButton(R.string.action_cancel, dismiss).show() | ||
42 | } | ||
43 | |||
44 | fun openJoinNetworkDialog(@Suppress("UNUSED_PARAMETER") v: View) { | ||
45 | val netNameField = EditText(this) | ||
46 | netNameField.layoutParams = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT) | ||
47 | netNameField.setHint(R.string.field_net_name) | ||
48 | |||
49 | val joinUrlField = EditText(this) | ||
50 | joinUrlField.layoutParams = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT) | ||
51 | joinUrlField.setHint(R.string.field_invitation_url) | ||
52 | |||
53 | val dialogFrame = layoutInflater.inflate(R.layout.dialog_frame, main_content, false) as ViewGroup | ||
54 | dialogFrame.addView(netNameField) | ||
55 | dialogFrame.addView(joinUrlField) | ||
56 | |||
57 | AlertDialog.Builder(this).setTitle(R.string.title_join_network).setView(dialogFrame) | ||
58 | .setPositiveButton(R.string.action_join) { _, _ -> joinNetwork(netNameField.text.toString(), joinUrlField.text.toString()) } | ||
59 | .setNegativeButton(R.string.action_cancel, dismiss).show() | ||
60 | } | ||
61 | |||
22 | private fun writeContent() { | 62 | private fun writeContent() { |
23 | text_configuration_directory.text = AppPaths.confDir().absolutePath | 63 | text_configuration_directory.text = AppPaths.confDir().absolutePath |
24 | text_log_directory.text = AppPaths.cacheDir().absolutePath | 64 | text_log_directory.text = AppPaths.cacheDir().absolutePath |
25 | text_tinc_binary.text = AppPaths.tinc().absolutePath | 65 | text_tinc_binary.text = AppPaths.tinc().absolutePath |
26 | } | 66 | } |
27 | 67 | ||
28 | fun generateConf(@Suppress("UNUSED_PARAMETER") v: View) = notify("Not implemented yet") | 68 | private fun generateConf(netName: String) = execAction( |
29 | fun joinNetwork(@Suppress("UNUSED_PARAMETER") v: View) = notify("Not implemented yet") | 69 | R.string.message_generating_configuration, |
70 | Tinc.init(netName) | ||
71 | .thenCompose { TincApp.removeScripts(netName) }) | ||
72 | |||
73 | private fun joinNetwork(netName: String, url: String) = execAction( | ||
74 | R.string.message_joining_network, | ||
75 | Tinc.join(netName, url) | ||
76 | .thenCompose { TincApp.removeScripts(netName) } | ||
77 | .thenCompose { TincApp.generateIfaceCfg(netName) }) | ||
78 | |||
79 | private fun execAction(@StringRes label: Int, action: CompletableFuture<Void>) { | ||
80 | showProgressDialog(label).let { progressDialog -> | ||
81 | action | ||
82 | .whenComplete { _, _ -> progressDialog.dismiss() } | ||
83 | .thenAccept { notify(R.string.message_network_configuration_created) } | ||
84 | .exceptionallyAccept { runOnUiThread { showErrorDialog(it.cause!!.localizedMessage) } } | ||
85 | } | ||
86 | } | ||
30 | 87 | ||
31 | } | 88 | } |