From 87ec3620d2259064831356c2f4000ae591756fd2 Mon Sep 17 00:00:00 2001 From: pacien Date: Sat, 10 Feb 2018 14:16:15 +0100 Subject: Reformat code --- .../org/pacien/tincapp/activities/BaseActivity.kt | 52 ++--- .../pacien/tincapp/activities/ConfigureActivity.kt | 190 +++++++++--------- .../pacien/tincapp/activities/LaunchActivity.kt | 118 ++++++------ .../org/pacien/tincapp/activities/StartActivity.kt | 100 +++++----- .../pacien/tincapp/activities/StatusActivity.kt | 212 ++++++++++----------- .../java/org/pacien/tincapp/commands/Command.kt | 30 +-- .../java/org/pacien/tincapp/commands/Executor.kt | 48 ++--- .../main/java/org/pacien/tincapp/commands/Tinc.kt | 70 +++---- .../java/org/pacien/tincapp/commands/TincApp.kt | 62 +++--- .../main/java/org/pacien/tincapp/commands/Tincd.kt | 22 +-- .../main/java/org/pacien/tincapp/context/App.kt | 36 ++-- .../java/org/pacien/tincapp/context/AppInfo.kt | 30 +-- .../java/org/pacien/tincapp/context/AppPaths.kt | 54 +++--- .../java/org/pacien/tincapp/data/CidrAddress.kt | 18 +- .../org/pacien/tincapp/data/TincConfiguration.kt | 16 +- .../tincapp/data/VpnInterfaceConfiguration.kt | 82 ++++---- .../java/org/pacien/tincapp/extensions/Android.kt | 20 +- .../tincapp/extensions/ApacheConfiguration.kt | 8 +- .../java/org/pacien/tincapp/extensions/Java.kt | 12 +- .../pacien/tincapp/extensions/VpnServiceBuilder.kt | 74 +++---- .../org/pacien/tincapp/service/TincVpnService.kt | 210 ++++++++++---------- .../main/java/org/pacien/tincapp/utils/PemUtils.kt | 82 ++++---- 22 files changed, 773 insertions(+), 773 deletions(-) (limited to 'app/src/main/java/org') 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 4904a66..c092111 100644 --- a/app/src/main/java/org/pacien/tincapp/activities/BaseActivity.kt +++ b/app/src/main/java/org/pacien/tincapp/activities/BaseActivity.kt @@ -19,34 +19,34 @@ import org.pacien.tincapp.context.AppInfo */ abstract class BaseActivity : AppCompatActivity() { - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - setContentView(R.layout.base) - setSupportActionBar(toolbar) - } + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.base) + setSupportActionBar(toolbar) + } - override fun onCreateOptionsMenu(m: Menu): Boolean { - menuInflater.inflate(R.menu.menu_base, m) - return true - } + override fun onCreateOptionsMenu(m: Menu): Boolean { + menuInflater.inflate(R.menu.menu_base, m) + return true + } - fun aboutDialog(@Suppress("UNUSED_PARAMETER") i: MenuItem) { - AlertDialog.Builder(this) - .setTitle(BuildConfig.APPLICATION_ID) - .setMessage(resources.getString(R.string.app_short_desc) + "\n\n" + - resources.getString(R.string.app_copyright) + " " + - resources.getString(R.string.app_license) + "\n\n" + - AppInfo.all()) - .setNeutralButton(R.string.action_open_project_website) { _, _ -> App.openURL(resources.getString(R.string.app_website_url)) } - .setPositiveButton(R.string.action_close, App.dismissAction) - .show() - } + fun aboutDialog(@Suppress("UNUSED_PARAMETER") i: MenuItem) { + AlertDialog.Builder(this) + .setTitle(BuildConfig.APPLICATION_ID) + .setMessage(resources.getString(R.string.app_short_desc) + "\n\n" + + resources.getString(R.string.app_copyright) + " " + + resources.getString(R.string.app_license) + "\n\n" + + AppInfo.all()) + .setNeutralButton(R.string.action_open_project_website) { _, _ -> App.openURL(resources.getString(R.string.app_website_url)) } + .setPositiveButton(R.string.action_close, App.dismissAction) + .show() + } - protected fun notify(@StringRes msg: Int) = Snackbar.make(activity_base, msg, Snackbar.LENGTH_LONG).show() - protected fun notify(msg: String) = Snackbar.make(activity_base, msg, Snackbar.LENGTH_LONG).show() - protected fun showProgressDialog(@StringRes msg: Int): ProgressDialog = ProgressDialog.show(this, null, getString(msg), true, false) - protected fun showErrorDialog(msg: String): AlertDialog = AlertDialog.Builder(this) - .setTitle(R.string.title_error).setMessage(msg) - .setPositiveButton(R.string.action_close, App.dismissAction).show() + protected fun notify(@StringRes msg: Int) = Snackbar.make(activity_base, msg, Snackbar.LENGTH_LONG).show() + protected fun notify(msg: String) = Snackbar.make(activity_base, msg, Snackbar.LENGTH_LONG).show() + protected fun showProgressDialog(@StringRes msg: Int): ProgressDialog = ProgressDialog.show(this, null, getString(msg), true, false) + protected fun showErrorDialog(msg: String): AlertDialog = AlertDialog.Builder(this) + .setTitle(R.string.title_error).setMessage(msg) + .setPositiveButton(R.string.action_close, App.dismissAction).show() } 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 6c29a53..ec17c11 100644 --- a/app/src/main/java/org/pacien/tincapp/activities/ConfigureActivity.kt +++ b/app/src/main/java/org/pacien/tincapp/activities/ConfigureActivity.kt @@ -26,102 +26,102 @@ import org.pacien.tincapp.extensions.Java.exceptionallyAccept */ class ConfigureActivity : BaseActivity() { - companion object { - val REQUEST_SCAN = 0 - val SCAN_PROVIDER = "com.google.zxing.client.android" + companion object { + val REQUEST_SCAN = 0 + val SCAN_PROVIDER = "com.google.zxing.client.android" + } + + private var joinDialog: View? = null + + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { + super.onActivityResult(requestCode, resultCode, data) + + if (requestCode == REQUEST_SCAN && resultCode == Activity.RESULT_OK) + joinDialog?.invitation_url?.setText(data!!.getStringExtra("SCAN_RESULT").trim()) + } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + supportActionBar!!.setDisplayHomeAsUpEnabled(true) + layoutInflater.inflate(R.layout.page_configure, main_content) + writeContent() + } + + fun scanCode(@Suppress("UNUSED_PARAMETER") v: View) { + try { + startActivityForResult(Intent("$SCAN_PROVIDER.SCAN"), REQUEST_SCAN) + } catch (e: ActivityNotFoundException) { + AlertDialog.Builder(this).setTitle(R.string.action_scan_qr_code) + .setMessage(R.string.message_no_qr_code_scanner) + .setPositiveButton(R.string.action_install) { _, _ -> + startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=$SCAN_PROVIDER"))) + }.setNegativeButton(R.string.action_cancel, App.dismissAction).show() } - - private var joinDialog: View? = null - - override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { - super.onActivityResult(requestCode, resultCode, data) - - if (requestCode == REQUEST_SCAN && resultCode == Activity.RESULT_OK) - joinDialog?.invitation_url?.setText(data!!.getStringExtra("SCAN_RESULT").trim()) - } - - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - supportActionBar!!.setDisplayHomeAsUpEnabled(true) - layoutInflater.inflate(R.layout.page_configure, main_content) - writeContent() - } - - fun scanCode(@Suppress("UNUSED_PARAMETER") v: View) { - try { - startActivityForResult(Intent("$SCAN_PROVIDER.SCAN"), REQUEST_SCAN) - } catch (e: ActivityNotFoundException) { - AlertDialog.Builder(this).setTitle(R.string.action_scan_qr_code) - .setMessage(R.string.message_no_qr_code_scanner) - .setPositiveButton(R.string.action_install) { _, _ -> - startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=$SCAN_PROVIDER"))) - }.setNegativeButton(R.string.action_cancel, App.dismissAction).show() - } - } - - fun openGenerateConfDialog(@Suppress("UNUSED_PARAMETER") v: View) { - val genDialog = layoutInflater.inflate(R.layout.dialog_network_generate, main_content, false) - AlertDialog.Builder(this).setTitle(R.string.title_new_network).setView(genDialog) - .setPositiveButton(R.string.action_create) { _, _ -> - generateConf( - genDialog.new_net_name.text.toString(), - genDialog.new_node_name.text.toString(), - genDialog.new_passphrase.text.toString()) - }.setNegativeButton(R.string.action_cancel, App.dismissAction).show() - } - - fun openJoinNetworkDialog(@Suppress("UNUSED_PARAMETER") v: View) { - joinDialog = layoutInflater.inflate(R.layout.dialog_network_join, main_content, false) - AlertDialog.Builder(this).setTitle(R.string.title_join_network).setView(joinDialog) - .setPositiveButton(R.string.action_join) { _, _ -> - joinNetwork( - joinDialog!!.net_name.text.toString(), - joinDialog!!.invitation_url.text.toString(), - joinDialog!!.join_passphrase.text.toString()) - }.setNegativeButton(R.string.action_cancel, App.dismissAction).show() - } - - fun openEncryptDecryptPrivateKeyDialog(@Suppress("UNUSED_PARAMETER") v: View) { - val encryptDecryptDialog = layoutInflater.inflate(R.layout.dialog_encrypt_decrypt_keys, main_content, false) - AlertDialog.Builder(this).setTitle(R.string.title_private_keys_encryption).setView(encryptDecryptDialog) - .setPositiveButton(R.string.action_apply) { _, _ -> - encryptDecryptPrivateKeys( - encryptDecryptDialog!!.enc_dec_net_name.text.toString(), - encryptDecryptDialog.enc_dec_current_passphrase.text.toString(), - encryptDecryptDialog.enc_dec_new_passphrase.text.toString()) - }.setNegativeButton(R.string.action_cancel, App.dismissAction).show() - } - - private fun writeContent() { - text_configuration_directory.text = AppPaths.confDir().absolutePath - text_log_directory.text = AppPaths.cacheDir().absolutePath - text_tinc_binary.text = AppPaths.tinc().absolutePath - } - - private fun generateConf(netName: String, nodeName: String, passphrase: String? = null) = execAction( - R.string.message_generating_configuration, - Tinc.init(netName, nodeName) - .thenCompose { TincApp.removeScripts(netName) } - .thenCompose { TincApp.setPassphrase(netName, newPassphrase = passphrase) }) - - private fun joinNetwork(netName: String, url: String, passphrase: String? = null) = execAction( - R.string.message_joining_network, - Tinc.join(netName, url) - .thenCompose { TincApp.removeScripts(netName) } - .thenCompose { TincApp.generateIfaceCfg(netName) } - .thenCompose { TincApp.setPassphrase(netName, newPassphrase = passphrase) }) - - private fun encryptDecryptPrivateKeys(netName: String, currentPassphrase: String, newPassphrase: String) = execAction( - R.string.message_encrypting_decrypting_private_keys, - TincApp.setPassphrase(netName, currentPassphrase, newPassphrase)) - - private fun execAction(@StringRes label: Int, action: CompletableFuture) { - showProgressDialog(label).let { progressDialog -> - action - .whenComplete { _, _ -> progressDialog.dismiss() } - .thenAccept { notify(R.string.message_network_configuration_written) } - .exceptionallyAccept { runOnUiThread { showErrorDialog(it.cause!!.localizedMessage) } } - } + } + + fun openGenerateConfDialog(@Suppress("UNUSED_PARAMETER") v: View) { + val genDialog = layoutInflater.inflate(R.layout.dialog_network_generate, main_content, false) + AlertDialog.Builder(this).setTitle(R.string.title_new_network).setView(genDialog) + .setPositiveButton(R.string.action_create) { _, _ -> + generateConf( + genDialog.new_net_name.text.toString(), + genDialog.new_node_name.text.toString(), + genDialog.new_passphrase.text.toString()) + }.setNegativeButton(R.string.action_cancel, App.dismissAction).show() + } + + fun openJoinNetworkDialog(@Suppress("UNUSED_PARAMETER") v: View) { + joinDialog = layoutInflater.inflate(R.layout.dialog_network_join, main_content, false) + AlertDialog.Builder(this).setTitle(R.string.title_join_network).setView(joinDialog) + .setPositiveButton(R.string.action_join) { _, _ -> + joinNetwork( + joinDialog!!.net_name.text.toString(), + joinDialog!!.invitation_url.text.toString(), + joinDialog!!.join_passphrase.text.toString()) + }.setNegativeButton(R.string.action_cancel, App.dismissAction).show() + } + + fun openEncryptDecryptPrivateKeyDialog(@Suppress("UNUSED_PARAMETER") v: View) { + val encryptDecryptDialog = layoutInflater.inflate(R.layout.dialog_encrypt_decrypt_keys, main_content, false) + AlertDialog.Builder(this).setTitle(R.string.title_private_keys_encryption).setView(encryptDecryptDialog) + .setPositiveButton(R.string.action_apply) { _, _ -> + encryptDecryptPrivateKeys( + encryptDecryptDialog!!.enc_dec_net_name.text.toString(), + encryptDecryptDialog.enc_dec_current_passphrase.text.toString(), + encryptDecryptDialog.enc_dec_new_passphrase.text.toString()) + }.setNegativeButton(R.string.action_cancel, App.dismissAction).show() + } + + private fun writeContent() { + text_configuration_directory.text = AppPaths.confDir().absolutePath + text_log_directory.text = AppPaths.cacheDir().absolutePath + text_tinc_binary.text = AppPaths.tinc().absolutePath + } + + private fun generateConf(netName: String, nodeName: String, passphrase: String? = null) = execAction( + R.string.message_generating_configuration, + Tinc.init(netName, nodeName) + .thenCompose { TincApp.removeScripts(netName) } + .thenCompose { TincApp.setPassphrase(netName, newPassphrase = passphrase) }) + + private fun joinNetwork(netName: String, url: String, passphrase: String? = null) = execAction( + R.string.message_joining_network, + Tinc.join(netName, url) + .thenCompose { TincApp.removeScripts(netName) } + .thenCompose { TincApp.generateIfaceCfg(netName) } + .thenCompose { TincApp.setPassphrase(netName, newPassphrase = passphrase) }) + + private fun encryptDecryptPrivateKeys(netName: String, currentPassphrase: String, newPassphrase: String) = execAction( + R.string.message_encrypting_decrypting_private_keys, + TincApp.setPassphrase(netName, currentPassphrase, newPassphrase)) + + private fun execAction(@StringRes label: Int, action: CompletableFuture) { + showProgressDialog(label).let { progressDialog -> + action + .whenComplete { _, _ -> progressDialog.dismiss() } + .thenAccept { notify(R.string.message_network_configuration_written) } + .exceptionallyAccept { runOnUiThread { showErrorDialog(it.cause!!.localizedMessage) } } } + } } diff --git a/app/src/main/java/org/pacien/tincapp/activities/LaunchActivity.kt b/app/src/main/java/org/pacien/tincapp/activities/LaunchActivity.kt index 74a059b..0179040 100644 --- a/app/src/main/java/org/pacien/tincapp/activities/LaunchActivity.kt +++ b/app/src/main/java/org/pacien/tincapp/activities/LaunchActivity.kt @@ -24,75 +24,75 @@ import java.io.FileNotFoundException */ class LaunchActivity : AppCompatActivity() { - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) - when (intent.action) { - ACTION_CONNECT -> requestPerm() - ACTION_DISCONNECT -> disconnect() - } + when (intent.action) { + ACTION_CONNECT -> requestPerm() + ACTION_DISCONNECT -> disconnect() } - - override fun onActivityResult(request: Int, result: Int, data: Intent?) { - if (request == PERMISSION_REQUEST_CODE && result == Activity.RESULT_OK) askPassphrase() + } + + override fun onActivityResult(request: Int, result: Int, data: Intent?) { + if (request == PERMISSION_REQUEST_CODE && result == Activity.RESULT_OK) askPassphrase() + } + + private fun requestPerm() = VpnService.prepare(this).let { + if (it != null) + startActivityForResult(it, PERMISSION_REQUEST_CODE) + else + onActivityResult(PERMISSION_REQUEST_CODE, Activity.RESULT_OK, null) + } + + @SuppressLint("InflateParams") + private fun askPassphrase() { + val netName = intent.data.schemeSpecificPart + + if (needPassphrase(netName) && intent.data.fragment == null) { + val dialog = layoutInflater.inflate(R.layout.dialog_decrypt_keys, null, false) + AlertDialog.Builder(this) + .setTitle(R.string.title_unlock_private_keys).setView(dialog) + .setPositiveButton(R.string.action_unlock) { _, _ -> connect(netName, dialog.passphrase.text.toString()) } + .setNegativeButton(R.string.action_cancel, { _, _ -> finish() }) + .show() + } else { + connect(netName, intent.data.fragment) } + } - private fun requestPerm() = VpnService.prepare(this).let { - if (it != null) - startActivityForResult(it, PERMISSION_REQUEST_CODE) - else - onActivityResult(PERMISSION_REQUEST_CODE, Activity.RESULT_OK, null) - } + private fun needPassphrase(netName: String) = try { + TincApp.listPrivateKeys(netName).filter { it.exists() }.any { PemUtils.isEncrypted(PemUtils.read(it)) } + } catch (e: FileNotFoundException) { + false + } - @SuppressLint("InflateParams") - private fun askPassphrase() { - val netName = intent.data.schemeSpecificPart - - if (needPassphrase(netName) && intent.data.fragment == null) { - val dialog = layoutInflater.inflate(R.layout.dialog_decrypt_keys, null, false) - AlertDialog.Builder(this) - .setTitle(R.string.title_unlock_private_keys).setView(dialog) - .setPositiveButton(R.string.action_unlock) { _, _ -> connect(netName, dialog.passphrase.text.toString()) } - .setNegativeButton(R.string.action_cancel, { _, _ -> finish() }) - .show() - } else { - connect(netName, intent.data.fragment) - } - } + private fun connect(netName: String, passphrase: String? = null) { + TincVpnService.startVpn(netName, passphrase) + finish() + } - private fun needPassphrase(netName: String) = try { - TincApp.listPrivateKeys(netName).filter { it.exists() }.any { PemUtils.isEncrypted(PemUtils.read(it)) } - } catch (e: FileNotFoundException) { - false - } + private fun disconnect() { + TincVpnService.stopVpn() + finish() + } - private fun connect(netName: String, passphrase: String? = null) { - TincVpnService.startVpn(netName, passphrase) - finish() - } + companion object { - private fun disconnect() { - TincVpnService.stopVpn() - finish() - } + private val PERMISSION_REQUEST_CODE = 0 - companion object { - - private val PERMISSION_REQUEST_CODE = 0 - - fun connect(netName: String, passphrase: String? = null) { - App.getContext().startActivity(Intent(App.getContext(), LaunchActivity::class.java) - .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) - .setAction(ACTION_CONNECT) - .setData(Uri.Builder().scheme(TINC_SCHEME).opaquePart(netName).fragment(passphrase).build())) - } - - fun disconnect() { - App.getContext().startActivity(Intent(App.getContext(), LaunchActivity::class.java) - .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) - .setAction(ACTION_DISCONNECT)) - } + fun connect(netName: String, passphrase: String? = null) { + App.getContext().startActivity(Intent(App.getContext(), LaunchActivity::class.java) + .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + .setAction(ACTION_CONNECT) + .setData(Uri.Builder().scheme(TINC_SCHEME).opaquePart(netName).fragment(passphrase).build())) + } + fun disconnect() { + App.getContext().startActivity(Intent(App.getContext(), LaunchActivity::class.java) + .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + .setAction(ACTION_DISCONNECT)) } + } + } 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 44d6e95..719bbc1 100644 --- a/app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt +++ b/app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt @@ -22,67 +22,67 @@ import org.pacien.tincapp.service.TincVpnService */ class StartActivity : BaseActivity(), AdapterView.OnItemClickListener, SwipeRefreshLayout.OnRefreshListener { - private var networkListAdapter: ArrayAdapter? = null + private var networkListAdapter: ArrayAdapter? = null - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - networkListAdapter = ArrayAdapter(this, R.layout.fragment_list_item) - layoutInflater.inflate(R.layout.fragment_list_view, main_content) - list_wrapper.setOnRefreshListener(this) - list.addHeaderView(layoutInflater.inflate(R.layout.fragment_network_list_header, list, false), null, false) - list.addFooterView(View(this), null, false) - list.adapter = networkListAdapter - list.onItemClickListener = this - } + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + networkListAdapter = ArrayAdapter(this, R.layout.fragment_list_item) + layoutInflater.inflate(R.layout.fragment_list_view, main_content) + list_wrapper.setOnRefreshListener(this) + list.addHeaderView(layoutInflater.inflate(R.layout.fragment_network_list_header, list, false), null, false) + list.addFooterView(View(this), null, false) + list.adapter = networkListAdapter + list.onItemClickListener = this + } - override fun onCreateOptionsMenu(m: Menu): Boolean { - menuInflater.inflate(R.menu.menu_start, m) - return super.onCreateOptionsMenu(m) - } + override fun onCreateOptionsMenu(m: Menu): Boolean { + menuInflater.inflate(R.menu.menu_start, m) + return super.onCreateOptionsMenu(m) + } - override fun onDestroy() { - networkListAdapter = null - super.onDestroy() - } + override fun onDestroy() { + networkListAdapter = null + super.onDestroy() + } - override fun onStart() { - super.onStart() - onRefresh() - } + override fun onStart() { + super.onStart() + onRefresh() + } - override fun onResume() { - super.onResume() - if (TincVpnService.isConnected()) openStatusActivity() - } + override fun onResume() { + super.onResume() + if (TincVpnService.isConnected()) openStatusActivity() + } - override fun onRefresh() { - val networks = AppPaths.confDir()?.list()?.toList() ?: emptyList() - runOnUiThread { - networkListAdapter?.setElements(networks) - setPlaceholderVisibility() - list_wrapper.isRefreshing = false - } + override fun onRefresh() { + val networks = AppPaths.confDir()?.list()?.toList() ?: emptyList() + runOnUiThread { + networkListAdapter?.setElements(networks) + setPlaceholderVisibility() + list_wrapper.isRefreshing = false } + } - override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) = - LaunchActivity.connect((view as TextView).text.toString()) + override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) = + LaunchActivity.connect((view as TextView).text.toString()) - fun openConfigureActivity(@Suppress("UNUSED_PARAMETER") i: MenuItem) = - startActivity(Intent(this, ConfigureActivity::class.java)) + fun openConfigureActivity(@Suppress("UNUSED_PARAMETER") i: MenuItem) = + startActivity(Intent(this, ConfigureActivity::class.java)) - fun openStatusActivity() = - startActivity(Intent(this, StatusActivity::class.java) - .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)) + fun openStatusActivity() = + startActivity(Intent(this, StatusActivity::class.java) + .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)) - private fun setPlaceholderVisibility() = if (networkListAdapter?.isEmpty != false) { - network_list_placeholder.text = getListPlaceholderText() - network_list_placeholder.visibility = View.VISIBLE - } else { - network_list_placeholder.visibility = View.GONE - } + private fun setPlaceholderVisibility() = if (networkListAdapter?.isEmpty != false) { + network_list_placeholder.text = getListPlaceholderText() + network_list_placeholder.visibility = View.VISIBLE + } else { + network_list_placeholder.visibility = View.GONE + } - private fun getListPlaceholderText() = - if (!AppPaths.storageAvailable()) getText(R.string.message_storage_unavailable) - else getText(R.string.message_no_network_configuration_found) + private fun getListPlaceholderText() = + if (!AppPaths.storageAvailable()) getText(R.string.message_storage_unavailable) + else getText(R.string.message_no_network_configuration_found) } 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 356ff6e..4b5384c 100644 --- a/app/src/main/java/org/pacien/tincapp/activities/StatusActivity.kt +++ b/app/src/main/java/org/pacien/tincapp/activities/StatusActivity.kt @@ -29,115 +29,115 @@ import kotlin.concurrent.timerTask */ class StatusActivity : BaseActivity(), AdapterView.OnItemClickListener, SwipeRefreshLayout.OnRefreshListener { - private var nodeListAdapter: ArrayAdapter? = null - private var refreshTimer: Timer? = null - private var updateView: Boolean = false - - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - - nodeListAdapter = ArrayAdapter(this, R.layout.fragment_list_item) - refreshTimer = Timer(true) - - layoutInflater.inflate(R.layout.fragment_list_view, main_content) - list_wrapper.setOnRefreshListener(this) - list.addHeaderView(layoutInflater.inflate(R.layout.fragment_network_status_header, list, false), null, false) - list.addFooterView(View(this), null, false) - list.onItemClickListener = this - list.adapter = nodeListAdapter - } - - override fun onCreateOptionsMenu(m: Menu): Boolean { - menuInflater.inflate(R.menu.menu_status, m) - return super.onCreateOptionsMenu(m) - } - - override fun onDestroy() { - super.onDestroy() - refreshTimer?.cancel() - nodeListAdapter = null - refreshTimer = null - } - - override fun onStart() { - super.onStart() - writeNetworkInfo(TincVpnService.getCurrentInterfaceCfg() ?: VpnInterfaceConfiguration()) - updateView = true - onRefresh() - updateNodeList() - } - - override fun onStop() { - super.onStop() - updateView = false - } - - override fun onResume() { - super.onResume() + private var nodeListAdapter: ArrayAdapter? = null + private var refreshTimer: Timer? = null + private var updateView: Boolean = false + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + + nodeListAdapter = ArrayAdapter(this, R.layout.fragment_list_item) + refreshTimer = Timer(true) + + layoutInflater.inflate(R.layout.fragment_list_view, main_content) + list_wrapper.setOnRefreshListener(this) + list.addHeaderView(layoutInflater.inflate(R.layout.fragment_network_status_header, list, false), null, false) + list.addFooterView(View(this), null, false) + list.onItemClickListener = this + list.adapter = nodeListAdapter + } + + override fun onCreateOptionsMenu(m: Menu): Boolean { + menuInflater.inflate(R.menu.menu_status, m) + return super.onCreateOptionsMenu(m) + } + + override fun onDestroy() { + super.onDestroy() + refreshTimer?.cancel() + nodeListAdapter = null + refreshTimer = null + } + + override fun onStart() { + super.onStart() + writeNetworkInfo(TincVpnService.getCurrentInterfaceCfg() ?: VpnInterfaceConfiguration()) + updateView = true + onRefresh() + updateNodeList() + } + + override fun onStop() { + super.onStop() + updateView = false + } + + override fun onResume() { + super.onResume() + if (!TincVpnService.isConnected()) openStartActivity() + } + + override fun onRefresh() { + getNodeNames().thenAccept { + runOnUiThread { + nodeListAdapter?.setElements(it) + node_list_placeholder.visibility = if (nodeListAdapter?.isEmpty != false) View.VISIBLE else View.GONE + list_wrapper.isRefreshing = false if (!TincVpnService.isConnected()) openStartActivity() + } } - - override fun onRefresh() { - getNodeNames().thenAccept { - runOnUiThread { - nodeListAdapter?.setElements(it) - node_list_placeholder.visibility = if (nodeListAdapter?.isEmpty != false) View.VISIBLE else View.GONE - list_wrapper.isRefreshing = false - if (!TincVpnService.isConnected()) openStartActivity() - } - } - } - - override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) { - val nodeName = (view as TextView).text.toString() - val dialogTextView = layoutInflater.inflate(R.layout.dialog_node_details, main_content, false) - Tinc.info(TincVpnService.getCurrentNetName()!!, nodeName).thenAccept { - runOnUiThread { - dialogTextView.dialog_node_details.text = it - AlertDialog.Builder(this) - .setTitle(R.string.title_node_info) - .setView(dialogTextView) - .setPositiveButton(R.string.action_close) { _, _ -> /* nop */ } - .show() - } - } - } - - fun writeNetworkInfo(cfg: VpnInterfaceConfiguration) { - text_network_name.text = TincVpnService.getCurrentNetName() ?: getString(R.string.value_none) - text_network_ip_addresses.setText(cfg.addresses.map { it.toSlashSeparated() }) - text_network_routes.setText(cfg.routes.map { it.toSlashSeparated() }) - text_network_dns_servers.setText(cfg.dnsServers) - text_network_search_domains.setText(cfg.searchDomains) - text_network_allow_bypass.text = getString(if (cfg.allowBypass) R.string.value_yes else R.string.value_no) - block_network_allowed_applications.visibility = if (cfg.allowedApplications.isNotEmpty()) View.VISIBLE else View.GONE - text_network_allowed_applications.setText(cfg.allowedApplications) - block_network_disallowed_applications.visibility = if (cfg.disallowedApplications.isNotEmpty()) View.VISIBLE else View.GONE - text_network_disallowed_applications.setText(cfg.disallowedApplications) - } - - fun updateNodeList() { - refreshTimer?.schedule(timerTask { - onRefresh() - if (updateView) updateNodeList() - }, REFRESH_RATE) + } + + override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) { + val nodeName = (view as TextView).text.toString() + val dialogTextView = layoutInflater.inflate(R.layout.dialog_node_details, main_content, false) + Tinc.info(TincVpnService.getCurrentNetName()!!, nodeName).thenAccept { + runOnUiThread { + dialogTextView.dialog_node_details.text = it + AlertDialog.Builder(this) + .setTitle(R.string.title_node_info) + .setView(dialogTextView) + .setPositiveButton(R.string.action_close) { _, _ -> /* nop */ } + .show() + } } - - fun stopVpn(@Suppress("UNUSED_PARAMETER") i: MenuItem) { - TincVpnService.stopVpn() - openStartActivity() - finish() - } - - fun openStartActivity() = startActivity(Intent(this, StartActivity::class.java).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) - - companion object { - private val REFRESH_RATE = 5000L - - fun getNodeNames(): CompletableFuture> = when (TincVpnService.isConnected()) { - true -> Tinc.dumpNodes(TincVpnService.getCurrentNetName()!!).thenApply> { it.map { it.substringBefore(' ') } } - false -> CompletableFuture.supplyAsync> { emptyList() } - } + } + + fun writeNetworkInfo(cfg: VpnInterfaceConfiguration) { + text_network_name.text = TincVpnService.getCurrentNetName() ?: getString(R.string.value_none) + text_network_ip_addresses.setText(cfg.addresses.map { it.toSlashSeparated() }) + text_network_routes.setText(cfg.routes.map { it.toSlashSeparated() }) + text_network_dns_servers.setText(cfg.dnsServers) + text_network_search_domains.setText(cfg.searchDomains) + text_network_allow_bypass.text = getString(if (cfg.allowBypass) R.string.value_yes else R.string.value_no) + block_network_allowed_applications.visibility = if (cfg.allowedApplications.isNotEmpty()) View.VISIBLE else View.GONE + text_network_allowed_applications.setText(cfg.allowedApplications) + block_network_disallowed_applications.visibility = if (cfg.disallowedApplications.isNotEmpty()) View.VISIBLE else View.GONE + text_network_disallowed_applications.setText(cfg.disallowedApplications) + } + + fun updateNodeList() { + refreshTimer?.schedule(timerTask { + onRefresh() + if (updateView) updateNodeList() + }, REFRESH_RATE) + } + + fun stopVpn(@Suppress("UNUSED_PARAMETER") i: MenuItem) { + TincVpnService.stopVpn() + openStartActivity() + finish() + } + + fun openStartActivity() = startActivity(Intent(this, StartActivity::class.java).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) + + companion object { + private val REFRESH_RATE = 5000L + + fun getNodeNames(): CompletableFuture> = when (TincVpnService.isConnected()) { + true -> Tinc.dumpNodes(TincVpnService.getCurrentNetName()!!).thenApply> { it.map { it.substringBefore(' ') } } + false -> CompletableFuture.supplyAsync> { emptyList() } } + } } diff --git a/app/src/main/java/org/pacien/tincapp/commands/Command.kt b/app/src/main/java/org/pacien/tincapp/commands/Command.kt index 6eab66b..cb95619 100644 --- a/app/src/main/java/org/pacien/tincapp/commands/Command.kt +++ b/app/src/main/java/org/pacien/tincapp/commands/Command.kt @@ -7,25 +7,25 @@ import java.util.* */ internal class Command(private val cmd: String) { - private data class Option(val key: String, val value: String?) { - fun toCommandLineOption(): String = if (value != null) "--$key=$value" else "--$key" - } + private data class Option(val key: String, val value: String?) { + fun toCommandLineOption(): String = if (value != null) "--$key=$value" else "--$key" + } - private val opts: MutableList