aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2017-07-11 14:49:08 +0200
committerPacien TRAN-GIRARD2017-07-11 14:49:08 +0200
commita2bfd75dee9b4952771a66c555ba1d920a072ae9 (patch)
tree3fcf598fe4e8fe858b296089f651a198b4e5f47c
parent336a8f9cc02b2f78796fd10ea6244e5d0bc725c7 (diff)
downloadtincapp-a2bfd75dee9b4952771a66c555ba1d920a072ae9.tar.gz
Implement create and join network dialogs + refactoring
-rw-r--r--app/src/main/java/org/pacien/tincapp/activities/BaseActivity.kt9
-rw-r--r--app/src/main/java/org/pacien/tincapp/activities/ConfigureActivity.kt61
-rw-r--r--app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt2
-rw-r--r--app/src/main/java/org/pacien/tincapp/activities/StatusActivity.kt12
-rw-r--r--app/src/main/java/org/pacien/tincapp/commands/Executor.kt7
-rw-r--r--app/src/main/java/org/pacien/tincapp/commands/TincApp.kt29
-rw-r--r--app/src/main/java/org/pacien/tincapp/context/AppPaths.kt4
-rw-r--r--app/src/main/java/org/pacien/tincapp/data/CidrAddress.kt18
-rw-r--r--app/src/main/java/org/pacien/tincapp/data/VpnInterfaceConfiguration.kt78
-rw-r--r--app/src/main/java/org/pacien/tincapp/extensions/Android.kt24
-rw-r--r--app/src/main/java/org/pacien/tincapp/extensions/ApacheConfiguration.kt16
-rw-r--r--app/src/main/java/org/pacien/tincapp/extensions/Java.kt18
-rw-r--r--app/src/main/java/org/pacien/tincapp/extensions/VpnServiceBuilder.kt51
-rw-r--r--app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt9
-rw-r--r--app/src/main/java/org/pacien/tincapp/service/VpnInterfaceConfiguraton.kt62
-rw-r--r--app/src/main/java/org/pacien/tincapp/service/VpnServiceBuilderExtensions.kt46
-rw-r--r--app/src/main/java/org/pacien/tincapp/utils/AndroidExtensions.kt23
-rw-r--r--app/src/main/java/org/pacien/tincapp/utils/Functions.kt11
-rw-r--r--app/src/main/res/layout/page_configure.xml4
-rw-r--r--app/src/main/res/values/strings.xml10
20 files changed, 335 insertions, 159 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/activities/BaseActivity.kt b/app/src/main/java/org/pacien/tincapp/activities/BaseActivity.kt
index 274e1ba..000320c 100644
--- a/app/src/main/java/org/pacien/tincapp/activities/BaseActivity.kt
+++ b/app/src/main/java/org/pacien/tincapp/activities/BaseActivity.kt
@@ -1,5 +1,6 @@
1package org.pacien.tincapp.activities 1package org.pacien.tincapp.activities
2 2
3import android.app.ProgressDialog
3import android.content.Intent 4import android.content.Intent
4import android.net.Uri 5import android.net.Uri
5import android.os.Bundle 6import android.os.Bundle
@@ -38,12 +39,18 @@ abstract class BaseActivity : AppCompatActivity() {
38 resources.getString(R.string.app_license) + "\n\n" + 39 resources.getString(R.string.app_license) + "\n\n" +
39 AppInfo.all()) 40 AppInfo.all())
40 .setNeutralButton(R.string.action_open_project_website) { _, _ -> openWebsite(R.string.app_website_url) } 41 .setNeutralButton(R.string.action_open_project_website) { _, _ -> openWebsite(R.string.app_website_url) }
41 .setPositiveButton(R.string.action_close) { _, _ -> /* nop */ } 42 .setPositiveButton(R.string.action_close, dismiss)
42 .show() 43 .show()
43 } 44 }
44 45
45 protected fun openWebsite(@StringRes url: Int) = startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(resources.getString(url)))) 46 protected fun openWebsite(@StringRes url: Int) = startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(resources.getString(url))))
46 protected fun notify(@StringRes msg: Int) = Snackbar.make(activity_base, msg, Snackbar.LENGTH_LONG).show() 47 protected fun notify(@StringRes msg: Int) = Snackbar.make(activity_base, msg, Snackbar.LENGTH_LONG).show()
47 protected fun notify(msg: String) = Snackbar.make(activity_base, msg, Snackbar.LENGTH_LONG).show() 48 protected fun notify(msg: String) = Snackbar.make(activity_base, msg, Snackbar.LENGTH_LONG).show()
49 protected fun showProgressDialog(@StringRes msg: Int): ProgressDialog = ProgressDialog.show(this, null, getString(msg), true, false)
50 protected fun showErrorDialog(msg: String): AlertDialog = AlertDialog.Builder(this)
51 .setTitle(R.string.title_error).setMessage(msg)
52 .setPositiveButton(R.string.action_close, dismiss).show()
53
54 protected val dismiss = { _: Any, _: Any -> /* nop */ }
48 55
49} 56}
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 @@
1package org.pacien.tincapp.activities 1package org.pacien.tincapp.activities
2 2
3import android.os.Bundle 3import android.os.Bundle
4import android.support.annotation.StringRes
5import android.support.v7.app.AlertDialog
4import android.view.View 6import android.view.View
7import android.view.ViewGroup
8import android.widget.EditText
9import android.widget.FrameLayout
10import java8.util.concurrent.CompletableFuture
5import kotlinx.android.synthetic.main.base.* 11import kotlinx.android.synthetic.main.base.*
6import kotlinx.android.synthetic.main.page_configure.* 12import kotlinx.android.synthetic.main.page_configure.*
7import org.pacien.tincapp.R 13import org.pacien.tincapp.R
14import org.pacien.tincapp.commands.Tinc
15import org.pacien.tincapp.commands.TincApp
8import org.pacien.tincapp.context.AppPaths 16import org.pacien.tincapp.context.AppPaths
17import 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}
diff --git a/app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt b/app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt
index cecd474..6bd845d 100644
--- a/app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt
+++ b/app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt
@@ -13,9 +13,9 @@ import kotlinx.android.synthetic.main.base.*
13import kotlinx.android.synthetic.main.page_start.* 13import kotlinx.android.synthetic.main.page_start.*
14import org.pacien.tincapp.R 14import org.pacien.tincapp.R
15import org.pacien.tincapp.context.AppPaths 15import org.pacien.tincapp.context.AppPaths
16import org.pacien.tincapp.extensions.Android.setElements
16import org.pacien.tincapp.service.TincVpnService 17import org.pacien.tincapp.service.TincVpnService
17import org.pacien.tincapp.utils.FileObserver 18import org.pacien.tincapp.utils.FileObserver
18import org.pacien.tincapp.utils.setElements
19 19
20/** 20/**
21 * @author pacien 21 * @author pacien
diff --git a/app/src/main/java/org/pacien/tincapp/activities/StatusActivity.kt b/app/src/main/java/org/pacien/tincapp/activities/StatusActivity.kt
index fb6ab73..a2c39dd 100644
--- a/app/src/main/java/org/pacien/tincapp/activities/StatusActivity.kt
+++ b/app/src/main/java/org/pacien/tincapp/activities/StatusActivity.kt
@@ -17,10 +17,10 @@ import kotlinx.android.synthetic.main.fragment_network_status_header.*
17import kotlinx.android.synthetic.main.page_status.* 17import kotlinx.android.synthetic.main.page_status.*
18import org.pacien.tincapp.R 18import org.pacien.tincapp.R
19import org.pacien.tincapp.commands.Tinc 19import org.pacien.tincapp.commands.Tinc
20import org.pacien.tincapp.data.VpnInterfaceConfiguration
21import org.pacien.tincapp.extensions.Android.setElements
22import org.pacien.tincapp.extensions.Android.setText
20import org.pacien.tincapp.service.TincVpnService 23import org.pacien.tincapp.service.TincVpnService
21import org.pacien.tincapp.service.VpnInterfaceConfiguration
22import org.pacien.tincapp.utils.setElements