aboutsummaryrefslogtreecommitdiff
path: root/app/src/main
diff options
context:
space:
mode:
authorpacien2018-08-26 02:33:35 +0200
committerpacien2018-08-26 02:33:35 +0200
commitebf0a5e4e23d94deb2b1b3eec1f647d67b6012d4 (patch)
treec9db3ef69ce7ff6c4a9bbb3f2fe9464edd1d99f3 /app/src/main
parentead3b3958744b5bea273f9562170d9c0ab7dbc10 (diff)
downloadtincapp-ebf0a5e4e23d94deb2b1b3eec1f647d67b6012d4.tar.gz
Separate configuration tools into fragments
Diffstat (limited to 'app/src/main')
-rw-r--r--app/src/main/java/org/pacien/tincapp/activities/BaseDialogFragment.kt38
-rw-r--r--app/src/main/java/org/pacien/tincapp/activities/configure/ToolsFragment.kt27
-rw-r--r--app/src/main/java/org/pacien/tincapp/activities/configure/tools/ConfigurationToolDialogFragment.kt (renamed from app/src/main/java/org/pacien/tincapp/activities/configure/tools/ConfigurationTool.kt)19
-rw-r--r--app/src/main/java/org/pacien/tincapp/activities/configure/tools/EncryptDecryptPrivateKeysToolDialogFragment.kt (renamed from app/src/main/java/org/pacien/tincapp/activities/configure/tools/EncryptDecryptPrivateKeysTool.kt)8
-rw-r--r--app/src/main/java/org/pacien/tincapp/activities/configure/tools/GenerateConfigToolDialogFragment.kt (renamed from app/src/main/java/org/pacien/tincapp/activities/configure/tools/GenerateConfigTool.kt)8
-rw-r--r--app/src/main/java/org/pacien/tincapp/activities/configure/tools/JoinNetworkToolDialogFragment.kt (renamed from app/src/main/java/org/pacien/tincapp/activities/configure/tools/JoinNetworkTool.kt)15
6 files changed, 74 insertions, 41 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/activities/BaseDialogFragment.kt b/app/src/main/java/org/pacien/tincapp/activities/BaseDialogFragment.kt
new file mode 100644
index 0000000..ae82fe7
--- /dev/null
+++ b/app/src/main/java/org/pacien/tincapp/activities/BaseDialogFragment.kt
@@ -0,0 +1,38 @@
1/*
2 * Tinc App, an Android binding and user interface for the tinc mesh VPN daemon
3 * Copyright (C) 2017-2018 Pacien TRAN-GIRARD
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19package org.pacien.tincapp.activities
20
21import android.content.Context
22import android.support.annotation.LayoutRes
23import android.support.v4.app.DialogFragment
24import android.view.LayoutInflater
25import android.view.View
26import android.view.ViewGroup
27
28/**
29 * @author pacien
30 */
31abstract class BaseDialogFragment : DialogFragment() {
32 protected val parentActivity by lazy { activity as BaseActivity }
33 // getLayoutInflater() calls onCreateDialog. See https://stackoverflow.com/a/15152788
34 private val inflater by lazy { activity!!.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater }
35
36 fun inflate(@LayoutRes layout: Int) = inflater.inflate(layout, null)!!
37 fun inflate(inflateFunc: (LayoutInflater, ViewGroup?, Boolean) -> View) = inflateFunc(inflater, null, false)
38}
diff --git a/app/src/main/java/org/pacien/tincapp/activities/configure/ToolsFragment.kt b/app/src/main/java/org/pacien/tincapp/activities/configure/ToolsFragment.kt
index 714a7ae..2135f59 100644
--- a/app/src/main/java/org/pacien/tincapp/activities/configure/ToolsFragment.kt
+++ b/app/src/main/java/org/pacien/tincapp/activities/configure/ToolsFragment.kt
@@ -18,36 +18,33 @@
18 18
19package org.pacien.tincapp.activities.configure 19package org.pacien.tincapp.activities.configure
20 20
21import android.content.Intent
22import android.os.Bundle 21import android.os.Bundle
23import android.view.LayoutInflater 22import android.view.LayoutInflater
24import android.view.View 23import android.view.View
25import android.view.ViewGroup 24import android.view.ViewGroup
26import org.pacien.tincapp.activities.BaseActivity
27import org.pacien.tincapp.activities.BaseFragment 25import org.pacien.tincapp.activities.BaseFragment
28import org.pacien.tincapp.activities.configure.tools.EncryptDecryptPrivateKeysTool 26import org.pacien.tincapp.activities.configure.tools.ConfigurationToolDialogFragment
29import org.pacien.tincapp.activities.configure.tools.GenerateConfigTool 27import org.pacien.tincapp.activities.configure.tools.EncryptDecryptPrivateKeysToolDialogFragment
30import org.pacien.tincapp.activities.configure.tools.JoinNetworkTool 28import org.pacien.tincapp.activities.configure.tools.GenerateConfigToolDialogFragment
29import org.pacien.tincapp.activities.configure.tools.JoinNetworkToolDialogFragment
31import org.pacien.tincapp.databinding.ConfigureToolsFragmentBinding 30import org.pacien.tincapp.databinding.ConfigureToolsFragmentBinding
32 31
33/** 32/**
34 * @author pacien 33 * @author pacien
35 */ 34 */
36class ToolsFragment : BaseFragment() { 35class ToolsFragment : BaseFragment() {
37 private val parentActivity by lazy { activity as BaseActivity } 36 private val generateConfigTool by lazy { GenerateConfigToolDialogFragment() }
38 private val generateConfigTool by lazy { GenerateConfigTool(parentActivity) } 37 private val joinNetworkTool by lazy { JoinNetworkToolDialogFragment() }
39 private val joinNetworkTool by lazy { JoinNetworkTool(this, parentActivity) } 38 private val encryptDecryptPrivateKeysTool by lazy { EncryptDecryptPrivateKeysToolDialogFragment() }
40 private val encryptDecryptPrivateKeysTool by lazy { EncryptDecryptPrivateKeysTool(parentActivity) }
41 39
42 override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { 40 override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
43 val binding = ConfigureToolsFragmentBinding.inflate(inflater, container, false) 41 val binding = ConfigureToolsFragmentBinding.inflate(inflater, container, false)
44 binding.generateConfigAction = generateConfigTool::openGenerateConfDialog 42 binding.generateConfigAction = openDialog(generateConfigTool)
45 binding.joinNetworkAction = joinNetworkTool::openJoinNetworkDialog 43 binding.joinNetworkAction = openDialog(joinNetworkTool)
46 binding.encryptDecryptPrivateKeysAction = encryptDecryptPrivateKeysTool::openEncryptDecryptPrivateKeyDialog 44 binding.encryptDecryptPrivateKeysAction = openDialog(encryptDecryptPrivateKeysTool)
47 return binding.root 45 return binding.root
48 } 46 }
49 47
50 override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { 48 private fun openDialog(tool: ConfigurationToolDialogFragment) =
51 joinNetworkTool.onActivityResult(requestCode, resultCode, data) 49 { tool.show(fragmentManager, tool.javaClass.simpleName) }
52 }
53} 50}
diff --git a/app/src/main/java/org/pacien/tincapp/activities/configure/tools/ConfigurationTool.kt b/app/src/main/java/org/pacien/tincapp/activities/configure/tools/ConfigurationToolDialogFragment.kt
index 4fa85f8..9c94bf1 100644
--- a/app/src/main/java/org/pacien/tincapp/activities/configure/tools/ConfigurationTool.kt
+++ b/app/src/main/java/org/pacien/tincapp/activities/configure/tools/ConfigurationToolDialogFragment.kt
@@ -18,13 +18,13 @@
18 18
19package org.pacien.tincapp.activities.configure.tools 19package org.pacien.tincapp.activities.configure.tools
20 20
21import android.app.AlertDialog
22import android.support.annotation.LayoutRes 21import android.support.annotation.LayoutRes
23import android.support.annotation.StringRes 22import android.support.annotation.StringRes
23import android.support.v7.app.AlertDialog
24import android.view.View 24import android.view.View
25import java8.util.concurrent.CompletableFuture 25import java8.util.concurrent.CompletableFuture
26import org.pacien.tincapp.R 26import org.pacien.tincapp.R
27import org.pacien.tincapp.activities.BaseActivity 27import org.pacien.tincapp.activities.BaseDialogFragment
28import org.pacien.tincapp.activities.common.ProgressModal 28import org.pacien.tincapp.activities.common.ProgressModal
29import org.pacien.tincapp.extensions.Java.exceptionallyAccept 29import org.pacien.tincapp.extensions.Java.exceptionallyAccept
30import java.util.regex.Pattern 30import java.util.regex.Pattern
@@ -32,23 +32,22 @@ import java.util.regex.Pattern
32/** 32/**
33 * @author pacien 33 * @author pacien
34 */ 34 */
35abstract class ConfigurationTool(private val parentActivity: BaseActivity) { 35abstract class ConfigurationToolDialogFragment : BaseDialogFragment() {
36 private val networkNamePattern by lazy { Pattern.compile("^[^\\x00/]*$")!! } 36 private val networkNamePattern by lazy { Pattern.compile("^[^\\x00/]*$")!! }
37 37
38 protected fun showDialog(@LayoutRes layout: Int, @StringRes title: Int, @StringRes applyButton: Int, applyAction: (View) -> Unit) = 38 protected fun makeDialog(@LayoutRes layout: Int, @StringRes title: Int, @StringRes applyButton: Int, applyAction: (View) -> Unit) =
39 showDialog(parentActivity.inflate(layout), title, applyButton, applyAction) 39 makeDialog(inflate(layout), title, applyButton, applyAction)
40 40
41 protected fun showDialog(view: View, @StringRes title: Int, @StringRes applyButton: Int, applyAction: (View) -> Unit) { 41 protected fun makeDialog(view: View, @StringRes title: Int, @StringRes applyButton: Int, applyAction: (View) -> Unit) =
42 AlertDialog.Builder(parentActivity) 42 AlertDialog.Builder(parentActivity)
43 .setTitle(title) 43 .setTitle(title)
44 .setView(view) 44 .setView(view)
45 .setPositiveButton(applyButton) { _, _ -> applyAction(view) } 45 .setPositiveButton(applyButton) { _, _ -> applyAction(view) }
46 .setNegativeButton(R.string.generic_action_cancel) { _, _ -> Unit } 46 .setNegativeButton(R.string.generic_action_cancel) { _, _ -> Unit }
47 .show() 47 .create()!!
48 }
49 48
50 protected fun execAction(@StringRes label: Int, action: CompletableFuture<Unit>) { 49 protected fun execAction(@StringRes label: Int, action: CompletableFuture<Unit>) {
51 ProgressModal.show(parentActivity, parentActivity.getString(label)).let { progressDialog -> 50 ProgressModal.show(parentActivity, getString(label)).let { progressDialog ->
52 action 51 action
53 .whenComplete { _, _ -> progressDialog.dismiss() } 52 .whenComplete { _, _ -> progressDialog.dismiss() }
54 .thenAccept { parentActivity.notify(R.string.configure_tools_message_network_configuration_written) } 53 .thenAccept { parentActivity.notify(R.string.configure_tools_message_network_configuration_written) }
@@ -60,5 +59,5 @@ abstract class ConfigurationTool(private val parentActivity: BaseActivity) {
60 if (networkNamePattern.matcher(netName).matches()) 59 if (networkNamePattern.matcher(netName).matches())
61 CompletableFuture.completedFuture(Unit) 60 CompletableFuture.completedFuture(Unit)
62 else 61 else
63 CompletableFuture.failedFuture(IllegalArgumentException(parentActivity.getString(R.string.configure_tools_message_invalid_network_name))) 62 CompletableFuture.failedFuture(IllegalArgumentException(getString(R.string.configure_tools_message_invalid_network_name)))
64} 63}
diff --git a/app/src/main/java/org/pacien/tincapp/activities/configure/tools/EncryptDecryptPrivateKeysTool.kt b/app/src/main/java/org/pacien/tincapp/activities/configure/tools/EncryptDecryptPrivateKeysToolDialogFragment.kt
index cd55111..20f0b88 100644
--- a/app/src/main/java/org/pacien/tincapp/activities/configure/tools/EncryptDecryptPrivateKeysTool.kt
+++ b/app/src/mai