diff options
author | pacien | 2018-08-26 02:33:35 +0200 |
---|---|---|
committer | pacien | 2018-08-26 02:33:35 +0200 |
commit | ebf0a5e4e23d94deb2b1b3eec1f647d67b6012d4 (patch) | |
tree | c9db3ef69ce7ff6c4a9bbb3f2fe9464edd1d99f3 /app/src | |
parent | ead3b3958744b5bea273f9562170d9c0ab7dbc10 (diff) | |
download | tincapp-ebf0a5e4e23d94deb2b1b3eec1f647d67b6012d4.tar.gz |
Separate configuration tools into fragments
Diffstat (limited to 'app/src')
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 | |||
19 | package org.pacien.tincapp.activities | ||
20 | |||
21 | import android.content.Context | ||
22 | import android.support.annotation.LayoutRes | ||
23 | import android.support.v4.app.DialogFragment | ||
24 | import android.view.LayoutInflater | ||
25 | import android.view.View | ||
26 | import android.view.ViewGroup | ||
27 | |||
28 | /** | ||
29 | * @author pacien | ||
30 | */ | ||
31 | abstract 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 | ||
19 | package org.pacien.tincapp.activities.configure | 19 | package org.pacien.tincapp.activities.configure |
20 | 20 | ||
21 | import android.content.Intent | ||
22 | import android.os.Bundle | 21 | import android.os.Bundle |
23 | import android.view.LayoutInflater | 22 | import android.view.LayoutInflater |
24 | import android.view.View | 23 | import android.view.View |
25 | import android.view.ViewGroup | 24 | import android.view.ViewGroup |
26 | import org.pacien.tincapp.activities.BaseActivity | ||
27 | import org.pacien.tincapp.activities.BaseFragment | 25 | import org.pacien.tincapp.activities.BaseFragment |
28 | import org.pacien.tincapp.activities.configure.tools.EncryptDecryptPrivateKeysTool | 26 | import org.pacien.tincapp.activities.configure.tools.ConfigurationToolDialogFragment |
29 | import org.pacien.tincapp.activities.configure.tools.GenerateConfigTool | 27 | import org.pacien.tincapp.activities.configure.tools.EncryptDecryptPrivateKeysToolDialogFragment |
30 | import org.pacien.tincapp.activities.configure.tools.JoinNetworkTool | 28 | import org.pacien.tincapp.activities.configure.tools.GenerateConfigToolDialogFragment |
29 | import org.pacien.tincapp.activities.configure.tools.JoinNetworkToolDialogFragment | ||
31 | import org.pacien.tincapp.databinding.ConfigureToolsFragmentBinding | 30 | import org.pacien.tincapp.databinding.ConfigureToolsFragmentBinding |
32 | 31 | ||
33 | /** | 32 | /** |
34 | * @author pacien | 33 | * @author pacien |
35 | */ | 34 | */ |
36 | class ToolsFragment : BaseFragment() { | 35 | class 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 | ||
19 | package org.pacien.tincapp.activities.configure.tools | 19 | package org.pacien.tincapp.activities.configure.tools |
20 | 20 | ||
21 | import android.app.AlertDialog | ||
22 | import android.support.annotation.LayoutRes | 21 | import android.support.annotation.LayoutRes |
23 | import android.support.annotation.StringRes | 22 | import android.support.annotation.StringRes |
23 | import android.support.v7.app.AlertDialog | ||
24 | import android.view.View | 24 | import android.view.View |
25 | import java8.util.concurrent.CompletableFuture | 25 | import java8.util.concurrent.CompletableFuture |
26 | import org.pacien.tincapp.R | 26 | import org.pacien.tincapp.R |
27 | import org.pacien.tincapp.activities.BaseActivity | 27 | import org.pacien.tincapp.activities.BaseDialogFragment |
28 | import org.pacien.tincapp.activities.common.ProgressModal | 28 | import org.pacien.tincapp.activities.common.ProgressModal |
29 | import org.pacien.tincapp.extensions.Java.exceptionallyAccept | 29 | import org.pacien.tincapp.extensions.Java.exceptionallyAccept |
30 | import java.util.regex.Pattern | 30 | import java.util.regex.Pattern |
@@ -32,23 +32,22 @@ import java.util.regex.Pattern | |||
32 | /** | 32 | /** |
33 | * @author pacien | 33 | * @author pacien |
34 | */ | 34 | */ |
35 | abstract class ConfigurationTool(private val parentActivity: BaseActivity) { | 35 | abstract 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/main/java/org/pacien/tincapp/activities/configure/tools/EncryptDecryptPrivateKeysToolDialogFragment.kt | |||
@@ -18,17 +18,17 @@ | |||
18 |