From ebf0a5e4e23d94deb2b1b3eec1f647d67b6012d4 Mon Sep 17 00:00:00 2001 From: pacien Date: Sun, 26 Aug 2018 02:33:35 +0200 Subject: Separate configuration tools into fragments --- .../tincapp/activities/BaseDialogFragment.kt | 38 ++++++++++ .../tincapp/activities/configure/ToolsFragment.kt | 27 ++++--- .../configure/tools/ConfigurationTool.kt | 64 ----------------- .../tools/ConfigurationToolDialogFragment.kt | 63 ++++++++++++++++ .../tools/EncryptDecryptPrivateKeysTool.kt | 49 ------------- .../EncryptDecryptPrivateKeysToolDialogFragment.kt | 49 +++++++++++++ .../configure/tools/GenerateConfigTool.kt | 51 ------------- .../tools/GenerateConfigToolDialogFragment.kt | 51 +++++++++++++ .../activities/configure/tools/JoinNetworkTool.kt | 83 ---------------------- .../tools/JoinNetworkToolDialogFragment.kt | 82 +++++++++++++++++++++ 10 files changed, 295 insertions(+), 262 deletions(-) create mode 100644 app/src/main/java/org/pacien/tincapp/activities/BaseDialogFragment.kt delete mode 100644 app/src/main/java/org/pacien/tincapp/activities/configure/tools/ConfigurationTool.kt create mode 100644 app/src/main/java/org/pacien/tincapp/activities/configure/tools/ConfigurationToolDialogFragment.kt delete mode 100644 app/src/main/java/org/pacien/tincapp/activities/configure/tools/EncryptDecryptPrivateKeysTool.kt create mode 100644 app/src/main/java/org/pacien/tincapp/activities/configure/tools/EncryptDecryptPrivateKeysToolDialogFragment.kt delete mode 100644 app/src/main/java/org/pacien/tincapp/activities/configure/tools/GenerateConfigTool.kt create mode 100644 app/src/main/java/org/pacien/tincapp/activities/configure/tools/GenerateConfigToolDialogFragment.kt delete mode 100644 app/src/main/java/org/pacien/tincapp/activities/configure/tools/JoinNetworkTool.kt create mode 100644 app/src/main/java/org/pacien/tincapp/activities/configure/tools/JoinNetworkToolDialogFragment.kt (limited to 'app') 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 @@ +/* + * Tinc App, an Android binding and user interface for the tinc mesh VPN daemon + * Copyright (C) 2017-2018 Pacien TRAN-GIRARD + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.pacien.tincapp.activities + +import android.content.Context +import android.support.annotation.LayoutRes +import android.support.v4.app.DialogFragment +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup + +/** + * @author pacien + */ +abstract class BaseDialogFragment : DialogFragment() { + protected val parentActivity by lazy { activity as BaseActivity } + // getLayoutInflater() calls onCreateDialog. See https://stackoverflow.com/a/15152788 + private val inflater by lazy { activity!!.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater } + + fun inflate(@LayoutRes layout: Int) = inflater.inflate(layout, null)!! + fun inflate(inflateFunc: (LayoutInflater, ViewGroup?, Boolean) -> View) = inflateFunc(inflater, null, false) +} 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 @@ package org.pacien.tincapp.activities.configure -import android.content.Intent import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import org.pacien.tincapp.activities.BaseActivity import org.pacien.tincapp.activities.BaseFragment -import org.pacien.tincapp.activities.configure.tools.EncryptDecryptPrivateKeysTool -import org.pacien.tincapp.activities.configure.tools.GenerateConfigTool -import org.pacien.tincapp.activities.configure.tools.JoinNetworkTool +import org.pacien.tincapp.activities.configure.tools.ConfigurationToolDialogFragment +import org.pacien.tincapp.activities.configure.tools.EncryptDecryptPrivateKeysToolDialogFragment +import org.pacien.tincapp.activities.configure.tools.GenerateConfigToolDialogFragment +import org.pacien.tincapp.activities.configure.tools.JoinNetworkToolDialogFragment import org.pacien.tincapp.databinding.ConfigureToolsFragmentBinding /** * @author pacien */ class ToolsFragment : BaseFragment() { - private val parentActivity by lazy { activity as BaseActivity } - private val generateConfigTool by lazy { GenerateConfigTool(parentActivity) } - private val joinNetworkTool by lazy { JoinNetworkTool(this, parentActivity) } - private val encryptDecryptPrivateKeysTool by lazy { EncryptDecryptPrivateKeysTool(parentActivity) } + private val generateConfigTool by lazy { GenerateConfigToolDialogFragment() } + private val joinNetworkTool by lazy { JoinNetworkToolDialogFragment() } + private val encryptDecryptPrivateKeysTool by lazy { EncryptDecryptPrivateKeysToolDialogFragment() } override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { val binding = ConfigureToolsFragmentBinding.inflate(inflater, container, false) - binding.generateConfigAction = generateConfigTool::openGenerateConfDialog - binding.joinNetworkAction = joinNetworkTool::openJoinNetworkDialog - binding.encryptDecryptPrivateKeysAction = encryptDecryptPrivateKeysTool::openEncryptDecryptPrivateKeyDialog + binding.generateConfigAction = openDialog(generateConfigTool) + binding.joinNetworkAction = openDialog(joinNetworkTool) + binding.encryptDecryptPrivateKeysAction = openDialog(encryptDecryptPrivateKeysTool) return binding.root } - override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { - joinNetworkTool.onActivityResult(requestCode, resultCode, data) - } + private fun openDialog(tool: ConfigurationToolDialogFragment) = + { tool.show(fragmentManager, tool.javaClass.simpleName) } } 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/ConfigurationTool.kt deleted file mode 100644 index 4fa85f8..0000000 --- a/app/src/main/java/org/pacien/tincapp/activities/configure/tools/ConfigurationTool.kt +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Tinc App, an Android binding and user interface for the tinc mesh VPN daemon - * Copyright (C) 2017-2018 Pacien TRAN-GIRARD - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.pacien.tincapp.activities.configure.tools - -import android.app.AlertDialog -import android.support.annotation.LayoutRes -import android.support.annotation.StringRes -import android.view.View -import java8.util.concurrent.CompletableFuture -import org.pacien.tincapp.R -import org.pacien.tincapp.activities.BaseActivity -import org.pacien.tincapp.activities.common.ProgressModal -import org.pacien.tincapp.extensions.Java.exceptionallyAccept -import java.util.regex.Pattern - -/** - * @author pacien - */ -abstract class ConfigurationTool(private val parentActivity: BaseActivity) { - private val networkNamePattern by lazy { Pattern.compile("^[^\\x00/]*$")!! } - - protected fun showDialog(@LayoutRes layout: Int, @StringRes title: Int, @StringRes applyButton: Int, applyAction: (View) -> Unit) = - showDialog(parentActivity.inflate(layout), title, applyButton, applyAction) - - protected fun showDialog(view: View, @StringRes title: Int, @StringRes applyButton: Int, applyAction: (View) -> Unit) { - AlertDialog.Builder(parentActivity) - .setTitle(title) - .setView(view) - .setPositiveButton(applyButton) { _, _ -> applyAction(view) } - .setNegativeButton(R.string.generic_action_cancel) { _, _ -> Unit } - .show() - } - - protected fun execAction(@StringRes label: Int, action: CompletableFuture) { - ProgressModal.show(parentActivity, parentActivity.getString(label)).let { progressDialog -> - action - .whenComplete { _, _ -> progressDialog.dismiss() } - .thenAccept { parentActivity.notify(R.string.configure_tools_message_network_configuration_written) } - .exceptionallyAccept { parentActivity.runOnUiThread { parentActivity.showErrorDialog(it.cause!!.localizedMessage) } } - } - } - - protected fun validateNetName(netName: String): CompletableFuture = - if (networkNamePattern.matcher(netName).matches()) - CompletableFuture.completedFuture(Unit) - else - CompletableFuture.failedFuture(IllegalArgumentException(parentActivity.getString(R.string.configure_tools_message_invalid_network_name))) -} diff --git a/app/src/main/java/org/pacien/tincapp/activities/configure/tools/ConfigurationToolDialogFragment.kt b/app/src/main/java/org/pacien/tincapp/activities/configure/tools/ConfigurationToolDialogFragment.kt new file mode 100644 index 0000000..9c94bf1 --- /dev/null +++ b/app/src/main/java/org/pacien/tincapp/activities/configure/tools/ConfigurationToolDialogFragment.kt @@ -0,0 +1,63 @@ +/* + * Tinc App, an Android binding and user interface for the tinc mesh VPN daemon + * Copyright (C) 2017-2018 Pacien TRAN-GIRARD + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.pacien.tincapp.activities.configure.tools + +import android.support.annotation.LayoutRes +import android.support.annotation.StringRes +import android.support.v7.app.AlertDialog +import android.view.View +import java8.util.concurrent.CompletableFuture +import org.pacien.tincapp.R +import org.pacien.tincapp.activities.BaseDialogFragment +import org.pacien.tincapp.activities.common.ProgressModal +import org.pacien.tincapp.extensions.Java.exceptionallyAccept +import java.util.regex.Pattern + +/** + * @author pacien + */ +abstract class ConfigurationToolDialogFragment : BaseDialogFragment() { + private val networkNamePattern by lazy { Pattern.compile("^[^\\x00/]*$")!! } + + protected fun makeDialog(@LayoutRes layout: Int, @StringRes title: Int, @StringRes applyButton: Int, applyAction: (View) -> Unit) = + makeDialog(inflate(layout), title, applyButton, applyAction) + + protected fun makeDialog(view: View, @StringRes title: Int, @StringRes applyButton: Int, applyAction: (View) -> Unit) = + AlertDialog.Builder(parentActivity) + .setTitle(title) + .setView(view) + .setPositiveButton(applyButton) { _, _ -> applyAction(view) } + .setNegativeButton(R.string.generic_action_cancel) { _, _ -> Unit } + .create()!! + + protected fun execAction(@StringRes label: Int, action: CompletableFuture) { + ProgressModal.show(parentActivity, getString(label)).let { progressDialog -> + action + .whenComplete { _, _ -> progressDialog.dismiss() } + .thenAccept { parentActivity.notify(R.string.configure_tools_message_network_configuration_written) } + .exceptionallyAccept { parentActivity.runOnUiThread { parentActivity.showErrorDialog(it.cause!!.localizedMessage) } } + } + } + + protected fun validateNetName(netName: String): CompletableFuture = + if (networkNamePattern.matcher(netName).matches()) + CompletableFuture.completedFuture(Unit) + else + CompletableFuture.failedFuture(IllegalArgumentException(getString(R.string.configure_tools_message_invalid_network_name))) +} 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/EncryptDecryptPrivateKeysTool.kt deleted file mode 100644 index cd55111..0000000 --- a/app/src/main/java/org/pacien/tincapp/activities/configure/tools/EncryptDecryptPrivateKeysTool.kt +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Tinc App, an Android binding and user interface for the tinc mesh VPN daemon - * Copyright (C) 2017-2018 Pacien TRAN-GIRARD - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.pacien.tincapp.activities.configure.tools - -import kotlinx.android.synthetic.main.configure_tools_dialog_encrypt_decrypt_keys.view.* -import org.pacien.tincapp.R -import org.pacien.tincapp.activities.BaseActivity -import org.pacien.tincapp.commands.TincApp - -/** - * @author pacien - */ -class EncryptDecryptPrivateKeysTool(parentActivity: BaseActivity) : ConfigurationTool(parentActivity) { - fun openEncryptDecryptPrivateKeyDialog() = - showDialog( - R.layout.configure_tools_dialog_encrypt_decrypt_keys, - R.string.configure_tools_private_keys_encryption_title, - R.string.configure_tools_private_keys_encryption_action - ) { dialog -> - encryptDecryptPrivateKeys( - dialog.enc_dec_net_name.text.toString(), - dialog.enc_dec_current_passphrase.text.toString(), - dialog.enc_dec_new_passphrase.text.toString() - ) - } - - private fun encryptDecryptPrivateKeys(netName: String, currentPassphrase: String, newPassphrase: String) = - execAction( - R.string.configure_tools_private_keys_encryption_encrypting, - validateNetName(netName) - .thenCompose { TincApp.setPassphrase(netName, currentPassphrase, newPassphrase) } - ) -} diff --git a/app/src/main/java/org/pacien/tincapp/activities/configure/tools/EncryptDecryptPrivateKeysToolDialogFragment.kt b/app/src/main/java/org/pacien/tincapp/activities/configure/tools/EncryptDecryptPrivateKeysToolDialogFragment.kt new file mode 100644 index 0000000..20f0b88 --- /dev/null +++ b/app/src/main/java/org/pacien/tincapp/activities/configure/tools/EncryptDecryptPrivateKeysToolDialogFragment.kt @@ -0,0 +1,49 @@ +/* + * Tinc App, an Android binding and user interface for the tinc mesh VPN daemon + * Copyright (C) 2017-2018 Pacien TRAN-GIRARD + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.pacien.tincapp.activities.configure.tools + +import android.os.Bundle +import kotlinx.android.synthetic.main.configure_tools_dialog_encrypt_decrypt_keys.view.* +import org.pacien.tincapp.R +import org.pacien.tincapp.commands.TincApp + +/** + * @author pacien + */ +class EncryptDecryptPrivateKeysToolDialogFragment : ConfigurationToolDialogFragment() { + override fun onCreateDialog(savedInstanceState: Bundle?) = + makeDialog( + R.layout.configure_tools_dialog_encrypt_decrypt_keys, + R.string.configure_tools_private_keys_encryption_title, + R.string.configure_tools_private_keys_encryption_action + ) { dialog -> + encryptDecryptPrivateKeys( + dialog.enc_dec_net_name.text.toString(), + dialog.enc_dec_current_passphrase.text.toString(), + dialog.enc_dec_new_passphrase.text.toString() + ) + } + + private fun encryptDecryptPrivateKeys(netName: String, currentPassphrase: String, newPassphrase: String) = + execAction( + R.string.configure_tools_private_keys_encryption_encrypting, + validateNetName(netName) + .thenCompose { TincApp.setPassphrase(netName, currentPassphrase, newPassphrase) } + ) +} diff --git a/app/src/main/java/org/pacien/tincapp/activities/configure/tools/GenerateConfigTool.kt b/app/src/main/java/org/pacien/tincapp/activities/configure/tools/GenerateConfigTool.kt deleted file mode 100644 index 2f0ef49..0000000 --- a/app/src/main/java/org/pacien/tincapp/activities/configure/tools/GenerateConfigTool.kt +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Tinc App, an Android binding and user interface for the tinc mesh VPN daemon - * Copyright (C) 2017-2018 Pacien TRAN-GIRARD - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.pacien.tincapp.activities.configure.tools - -import kotlinx.android.synthetic.main.configure_tools_dialog_network_generate.view.* -import org.pacien.tincapp.R -import org.pacien.tincapp.activities.BaseActivity -import org.pacien.tincapp.commands.Tinc -import org.pacien.tincapp.commands.TincApp - -/** - * @author pacien - */ -class GenerateConfigTool(parentActivity: BaseActivity) : ConfigurationTool(parentActivity) { - fun openGenerateConfDialog() = - showDialog( - R.layout.configure_tools_dialog_network_generate, - R.string.configure_tools_generate_config_title, - R.string.configure_tools_generate_config_action - ) { dialog -> - generateConf( - dialog.new_net_name.text.toString(), - dialog.new_node_name.text.toString(), - dialog.new_passphrase.text.toString() - ) - } - - private fun generateConf(netName: String, nodeName: String, passphrase: String? = null) = execAction( - R.string.configure_tools_generate_config_generating, - validateNetName(netName) - .thenCompose { Tinc.init(netName, nodeName) } - .thenCompose { TincApp.removeScripts(netName) } - .thenCompose { TincApp.generateIfaceCfgTemplate(netName) } - .thenCompose { TincApp.setPassphrase(netName, newPassphrase = passphrase) }) -} diff --git a/app/src/main/java/org/pacien/tincapp/activities/configure/tools/GenerateConfigToolDialogFragment.kt b/app/src/main/java/org/pacien/tincapp/activities/configure/tools/GenerateConfigToolDialogFragment.kt new file mode 100644 index 0000000..96e39ba --- /dev/null +++ b/app/src/main/java/org/pacien/tincapp/activities/configure/tools/GenerateConfigToolDialogFragment.kt @@ -0,0 +1,51 @@ +/* + * Tinc App, an Android binding and user interface for the tinc mesh VPN daemon + * Copyright (C) 2017-2018 Pacien TRAN-GIRARD + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.pacien.tincapp.activities.configure.tools + +import android.os.Bundle +import kotlinx.android.synthetic.main.configure_tools_dialog_network_generate.view.* +import org.pacien.tincapp.R +import org.pacien.tincapp.commands.Tinc +import org.pacien.tincapp.commands.TincApp + +/** + * @author pacien + */ +class GenerateConfigToolDialogFragment : ConfigurationToolDialogFragment() { + override fun onCreateDialog(savedInstanceState: Bundle?) = + makeDialog( + R.layout.configure_tools_dialog_network_generate, + R.string.configure_tools_generate_config_title, + R.string.configure_tools_generate_config_action + ) { dialog -> + generateConf( + dialog.new_net_name.text.toString(), + dialog.new_node_name.text.toString(), + dialog.new_passphrase.text.toString() + ) + } + + private fun generateConf(netName: String, nodeName: String, passphrase: String? = null) = execAction( + R.string.configure_tools_generate_config_generating, + validateNetName(netName) + .thenCompose { Tinc.init(netName, nodeName) } + .thenCompose { TincApp.removeScripts(netName) } + .thenCompose { TincApp.generateIfaceCfgTemplate(netName) } + .thenCompose { TincApp.setPassphrase(netName, newPassphrase = passphrase) }) +} diff --git a/app/src/main/java/org/pacien/tincapp/activities/configure/tools/JoinNetworkTool.kt b/app/src/main/java/org/pacien/tincapp/activities/configure/tools/JoinNetworkTool.kt deleted file mode 100644 index 6656d86..0000000 --- a/app/src/main/java/org/pacien/tincapp/activities/configure/tools/JoinNetworkTool.kt +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Tinc App, an Android binding and user interface for the tinc mesh VPN daemon - * Copyright (C) 2017-2018 Pacien TRAN-GIRARD - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.pacien.tincapp.activities.configure.tools - -import android.content.Intent -import android.view.View -import com.google.zxing.integration.android.IntentIntegrator -import com.google.zxing.integration.android.IntentResult -import kotlinx.android.synthetic.main.configure_tools_dialog_network_join.view.* -import org.pacien.tincapp.R -import org.pacien.tincapp.activities.BaseActivity -import org.pacien.tincapp.activities.BaseFragment -import org.pacien.tincapp.commands.Tinc -import org.pacien.tincapp.commands.TincApp -import org.pacien.tincapp.databinding.ConfigureToolsDialogNetworkJoinBinding - -/** - * @author pacien - */ -class JoinNetworkTool(parentFragment: BaseFragment, private val parentActivity: BaseActivity) : ConfigurationTool(parentActivity) { - private val scanner by lazy { IntentIntegrator.forSupportFragment(parentFragment) } - private var joinDialog: View? = null - - fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { - IntentIntegrator.parseActivityResult(requestCode, resultCode, data) - ?.let(IntentResult::getContents) - ?.let(String::trim) - ?.let { joinDialog?.invitation_url?.setText(it) } - } - - fun openJoinNetworkDialog() = - makeJoinDialog().let { newDialog -> - joinDialog = newDialog - showDialog( - newDialog, - R.string.configure_tools_join_network_title, - R.string.configure_tools_join_network_action - ) { dialog -> - joinNetwork( - dialog.net_name.text.toString(), - dialog.invitation_url.text.toString(), - dialog.join_passphrase.text.toString() - ) - } - } - - private fun makeJoinDialog() = - parentActivity.inflate { inflater, parent, attachToRoot -> - ConfigureToolsDialogNetworkJoinBinding.inflate(inflater, parent, attachToRoot) - .apply { scanAction = this@JoinNetworkTool::scanCode } - .root - } - - private fun scanCode() { - scanner.initiateScan() - } - - private fun joinNetwork(netName: String, url: String, passphrase: String? = null) = - execAction( - R.string.configure_tools_join_network_joining, - validateNetName(netName) - .thenCompose { Tinc.join(netName, url) } - .thenCompose { TincApp.removeScripts(netName) } - .thenCompose { TincApp.generateIfaceCfg(netName) } - .thenCompose { TincApp.setPassphrase(netName, newPassphrase = passphrase) } - ) -} diff --git a/app/src/main/java/org/pacien/tincapp/activities/configure/tools/JoinNetworkToolDialogFragment.kt b/app/src/main/java/org/pacien/tincapp/activities/configure/tools/JoinNetworkToolDialogFragment.kt new file mode 100644 index 0000000..25bdb15 --- /dev/null +++ b/app/src/main/java/org/pacien/tincapp/activities/configure/tools/JoinNetworkToolDialogFragment.kt @@ -0,0 +1,82 @@ +/* + * Tinc App, an Android binding and user interface for the tinc mesh VPN daemon + * Copyright (C) 2017-2018 Pacien TRAN-GIRARD + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.pacien.tincapp.activities.configure.tools + +import android.content.Intent +import android.os.Bundle +import android.view.View +import com.google.zxing.integration.android.IntentIntegrator +import com.google.zxing.integration.android.IntentResult +import kotlinx.android.synthetic.main.configure_tools_dialog_network_join.view.* +import org.pacien.tincapp.R +import org.pacien.tincapp.commands.Tinc +import org.pacien.tincapp.commands.TincApp +import org.pacien.tincapp.databinding.ConfigureToolsDialogNetworkJoinBinding + +/** + * @author pacien + */ +class JoinNetworkToolDialogFragment : ConfigurationToolDialogFragment() { + private val scanner by lazy { IntentIntegrator.forSupportFragment(this) } + private var joinDialog: View? = null + + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { + IntentIntegrator.parseActivityResult(requestCode, resultCode, data) + ?.let(IntentResult::getContents) + ?.let(String::trim) + ?.let { joinDialog?.invitation_url?.setText(it) } + } + + override fun onCreateDialog(savedInstanceState: Bundle?) = + makeJoinDialog().let { newDialog -> + joinDialog = newDialog + makeDialog( + newDialog, + R.string.configure_tools_join_network_title, + R.string.configure_tools_join_network_action + ) { dialog -> + joinNetwork( + dialog.net_name.text.toString(), + dialog.invitation_url.text.toString(), + dialog.join_passphrase.text.toString() + ) + } + } + + private fun makeJoinDialog() = + parentActivity.inflate { inflater, parent, attachToRoot -> + ConfigureToolsDialogNetworkJoinBinding.inflate(inflater, parent, attachToRoot) + .apply { scanAction = this@JoinNetworkToolDialogFragment::scanCode } + .root + } + + private fun scanCode() { + scanner.initiateScan() + } + + private fun joinNetwork(netName: String, url: String, passphrase: String? = null) = + execAction( + R.string.configure_tools_join_network_joining, + validateNetName(netName) + .thenCompose { Tinc.join(netName, url) } + .thenCompose { TincApp.removeScripts(netName) } + .thenCompose { TincApp.generateIfaceCfg(netName) } + .thenCompose { TincApp.setPassphrase(netName, newPassphrase = passphrase) } + ) +} -- cgit v1.2.3