From 4629e909c9f8dd6fdc7ec9ed1dcc4668311e134f Mon Sep 17 00:00:00 2001
From: pacien
Date: Tue, 21 Aug 2018 02:09:57 +0200
Subject: Add subnet list tab to status activity
---
.../tincapp/activities/status/StatusActivity.kt | 4 +-
.../activities/status/nodes/NodeListFragment.kt | 4 +-
.../activities/status/subnets/SubnetInfo.kt | 36 ++++++++++++++
.../status/subnets/SubnetInfoArrayAdapter.kt | 44 +++++++++++++++++
.../status/subnets/SubnetListFragment.kt | 57 ++++++++++++++++++++++
.../status/subnets/SubnetListLiveData.kt | 38 +++++++++++++++
.../status/subnets/SubnetListViewModel.kt | 30 ++++++++++++
.../main/java/org/pacien/tincapp/commands/Tinc.kt | 4 ++
app/src/main/res/layout/status_node_list.xml | 39 ---------------
.../main/res/layout/status_node_list_fragment.xml | 38 +++++++++++++++
.../res/layout/status_subnet_list_fragment.xml | 38 +++++++++++++++
.../main/res/layout/status_subnet_list_item.xml | 50 +++++++++++++++++++
app/src/main/res/values/strings.xml | 4 +-
app/src/main/res/values/styles.xml | 1 +
14 files changed, 344 insertions(+), 43 deletions(-)
create mode 100644 app/src/main/java/org/pacien/tincapp/activities/status/subnets/SubnetInfo.kt
create mode 100644 app/src/main/java/org/pacien/tincapp/activities/status/subnets/SubnetInfoArrayAdapter.kt
create mode 100644 app/src/main/java/org/pacien/tincapp/activities/status/subnets/SubnetListFragment.kt
create mode 100644 app/src/main/java/org/pacien/tincapp/activities/status/subnets/SubnetListLiveData.kt
create mode 100644 app/src/main/java/org/pacien/tincapp/activities/status/subnets/SubnetListViewModel.kt
delete mode 100644 app/src/main/res/layout/status_node_list.xml
create mode 100644 app/src/main/res/layout/status_node_list_fragment.xml
create mode 100644 app/src/main/res/layout/status_subnet_list_fragment.xml
create mode 100644 app/src/main/res/layout/status_subnet_list_item.xml
(limited to 'app')
diff --git a/app/src/main/java/org/pacien/tincapp/activities/status/StatusActivity.kt b/app/src/main/java/org/pacien/tincapp/activities/status/StatusActivity.kt
index f47df6c..3815bc9 100644
--- a/app/src/main/java/org/pacien/tincapp/activities/status/StatusActivity.kt
+++ b/app/src/main/java/org/pacien/tincapp/activities/status/StatusActivity.kt
@@ -31,6 +31,7 @@ import org.pacien.tincapp.activities.common.ProgressModal
import org.pacien.tincapp.activities.start.StartActivity
import org.pacien.tincapp.activities.status.networkinfo.NetworkInfoFragment
import org.pacien.tincapp.activities.status.nodes.NodeListFragment
+import org.pacien.tincapp.activities.status.subnets.SubnetListFragment
import org.pacien.tincapp.activities.viewlog.ViewLogActivity
import org.pacien.tincapp.intent.Actions
import org.pacien.tincapp.intent.BroadcastMapper
@@ -46,7 +47,8 @@ class StatusActivity : BaseActivity() {
private val broadcastMapper = BroadcastMapper(mapOf(Actions.EVENT_DISCONNECTED to this::onVpnShutdown))
private val pages = listOf(
R.string.status_activity_title_network_info to NetworkInfoFragment(),
- R.string.status_activity_title_node_list to NodeListFragment()
+ R.string.status_activity_title_node_list to NodeListFragment(),
+ R.string.status_activity_title_subnet_list to SubnetListFragment()
)
private var shutdownDialog: AlertDialog? = null
diff --git a/app/src/main/java/org/pacien/tincapp/activities/status/nodes/NodeListFragment.kt b/app/src/main/java/org/pacien/tincapp/activities/status/nodes/NodeListFragment.kt
index 1f762c0..1111de7 100644
--- a/app/src/main/java/org/pacien/tincapp/activities/status/nodes/NodeListFragment.kt
+++ b/app/src/main/java/org/pacien/tincapp/activities/status/nodes/NodeListFragment.kt
@@ -28,7 +28,7 @@ import android.view.View
import android.view.ViewGroup
import kotlinx.android.synthetic.main.base.*
import kotlinx.android.synthetic.main.status_node_info_dialog.view.*
-import kotlinx.android.synthetic.main.status_node_list.*
+import kotlinx.android.synthetic.main.status_node_list_fragment.*
import org.pacien.tincapp.R
import org.pacien.tincapp.commands.Tinc
import org.pacien.tincapp.extensions.hideBottomSeparator
@@ -53,7 +53,7 @@ class NodeListFragment : Fragment() {
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
- return inflater.inflate(R.layout.status_node_list, container, false)
+ return inflater.inflate(R.layout.status_node_list_fragment, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
diff --git a/app/src/main/java/org/pacien/tincapp/activities/status/subnets/SubnetInfo.kt b/app/src/main/java/org/pacien/tincapp/activities/status/subnets/SubnetInfo.kt
new file mode 100644
index 0000000..57c7ca1
--- /dev/null
+++ b/app/src/main/java/org/pacien/tincapp/activities/status/subnets/SubnetInfo.kt
@@ -0,0 +1,36 @@
+/*
+ * 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.status.subnets
+
+import java.util.regex.Matcher
+import java.util.regex.Pattern
+
+/**
+ * @author pacien
+ */
+data class SubnetInfo(val ipRange: String, val owner: String) {
+ companion object {
+ private const val SUBNET_DUMP_PATTERN_STRING = "(\\S+) owner (\\S+)"
+ private val SUBNET_DUMP_PATTERN by lazy { Pattern.compile(SUBNET_DUMP_PATTERN_STRING) }
+
+ fun ofSubnetDump(line: String) = ofSubnetDump(SUBNET_DUMP_PATTERN.matcher(line).apply { find() })
+ private fun ofSubnetDump(matcher: Matcher) = SubnetInfo(ipRange = matcher[1], owner = matcher[2])
+ private operator fun Matcher.get(index: Int) = group(index)
+ }
+}
diff --git a/app/src/main/java/org/pacien/tincapp/activities/status/subnets/SubnetInfoArrayAdapter.kt b/app/src/main/java/org/pacien/tincapp/activities/status/subnets/SubnetInfoArrayAdapter.kt
new file mode 100644
index 0000000..a5e4a1d
--- /dev/null
+++ b/app/src/main/java/org/pacien/tincapp/activities/status/subnets/SubnetInfoArrayAdapter.kt
@@ -0,0 +1,44 @@
+/*
+ * 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.status.subnets
+
+import android.content.Context
+import android.databinding.DataBindingUtil
+import android.view.LayoutInflater
+import android.view.View
+import android.view.ViewGroup
+import android.widget.ArrayAdapter
+import org.pacien.tincapp.databinding.StatusSubnetListItemBinding
+
+/**
+ * @author pacien
+ */
+class SubnetInfoArrayAdapter(context: Context?) : ArrayAdapter(context, -1) {
+ private val layoutInflater = LayoutInflater.from(context)!!
+
+ override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
+ val binding = when (convertView) {
+ null -> StatusSubnetListItemBinding.inflate(layoutInflater, parent, false)
+ else -> DataBindingUtil.getBinding(convertView)!!
+ }
+
+ binding.subnetInfo = getItem(position)
+ return binding.root
+ }
+}
diff --git a/app/src/main/java/org/pacien/tincapp/activities/status/subnets/SubnetListFragment.kt b/app/src/main/java/org/pacien/tincapp/activities/status/subnets/SubnetListFragment.kt
new file mode 100644
index 0000000..084ec68
--- /dev/null
+++ b/app/src/main/java/org/pacien/tincapp/activities/status/subnets/SubnetListFragment.kt
@@ -0,0 +1,57 @@
+/*
+ * 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.status.subnets
+
+import android.arch.lifecycle.Observer
+import android.arch.lifecycle.ViewModelProviders
+import android.os.Bundle
+import android.support.v4.app.Fragment
+import android.view.LayoutInflater
+import android.view.View
+import android.view.ViewGroup
+import kotlinx.android.synthetic.main.status_subnet_list_fragment.*
+import org.pacien.tincapp.R
+import org.pacien.tincapp.extensions.hideBottomSeparator
+import org.pacien.tincapp.extensions.hideTopSeparator
+import org.pacien.tincapp.extensions.setElements
+
+/**
+ * @author pacien
+ */
+class SubnetListFragment : Fragment() {
+ private val subnetListViewModel by lazy { ViewModelProviders.of(this).get(SubnetListViewModel::class.java) }
+ private val subnetListAdapter by lazy { SubnetInfoArrayAdapter(context) }
+ private val subnetListObserver by lazy { Observer> { subnetListAdapter.setElements(it) } }
+
+ override fun onCreate(savedInstanceState: Bundle?) {
+ super.onCreate(savedInstanceState)
+ subnetListViewModel.nodeList.observe(this, subnetListObserver)
+ }
+
+ override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
+ return inflater.inflate(R.layout.status_subnet_list_fragment, container, false)
+ }
+
+ override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
+ status_subnet_list.hideTopSeparator()
+ status_subnet_list.hideBottomSeparator()
+ status_subnet_list.onItemClickListener = null
+ status_subnet_list.adapter = subnetListAdapter
+ }
+}
diff --git a/app/src/main/java/org/pacien/tincapp/activities/status/subnets/SubnetListLiveData.kt b/app/src/main/java/org/pacien/tincapp/activities/status/subnets/SubnetListLiveData.kt
new file mode 100644
index 0000000..1dbd8c3
--- /dev/null
+++ b/app/src/main/java/org/pacien/tincapp/activities/status/subnets/SubnetListLiveData.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.status.subnets
+
+import org.pacien.tincapp.activities.common.SelfRefreshingLiveData
+import org.pacien.tincapp.commands.Tinc
+import java.util.concurrent.TimeUnit
+
+/**
+ * @author pacien
+ */
+class SubnetListLiveData(private val netName: String) : SelfRefreshingLiveData>(1, TimeUnit.SECONDS) {
+ private val tincCtl = Tinc
+
+ override fun onRefresh() {
+ val subnetList = tincCtl.dumpSubnets(netName)
+ .thenApply { list -> list.map { SubnetInfo.ofSubnetDump(it) } }
+ .get()
+
+ postValue(subnetList)
+ }
+}
diff --git a/app/src/main/java/org/pacien/tincapp/activities/status/subnets/SubnetListViewModel.kt b/app/src/main/java/org/pacien/tincapp/activities/status/subnets/SubnetListViewModel.kt
new file mode 100644
index 0000000..4390aad
--- /dev/null
+++ b/app/src/main/java/org/pacien/tincapp/activities/status/subnets/SubnetListViewModel.kt
@@ -0,0 +1,30 @@
+/*
+ * 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.status.subnets
+
+import android.arch.lifecycle.ViewModel
+import org.pacien.tincapp.service.TincVpnService
+
+/**
+ * @author pacien
+ */
+class SubnetListViewModel : ViewModel() {
+ private val netName by lazy { TincVpnService.getCurrentNetName()!! }
+ val nodeList by lazy { SubnetListLiveData(netName) }
+}
diff --git a/app/src/main/java/org/pacien/tincapp/commands/Tinc.kt b/app/src/main/java/org/pacien/tincapp/commands/Tinc.kt
index 7f684c4..4d00805 100644
--- a/app/src/main/java/org/pacien/tincapp/commands/Tinc.kt
+++ b/app/src/main/java/org/pacien/tincapp/commands/Tinc.kt
@@ -43,6 +43,10 @@ object Tinc {
if (reachable) newCommand(netName).withArguments("dump", "reachable", "nodes")
else newCommand(netName).withArguments("dump", "nodes"))
+ fun dumpSubnets(netName: String): CompletableFuture> =
+ Executor.call(
+ newCommand(netName).withArguments("dump", "subnets"))
+
fun info(netName: String, node: String): CompletableFuture =
Executor.call(newCommand(netName).withArguments("info", node))
.thenApply { it.joinToString("\n") }
diff --git a/app/src/main/res/layout/status_node_list.xml b/app/src/main/res/layout/status_node_list.xml
deleted file mode 100644
index 923d20e..0000000
--- a/app/src/main/res/layout/status_node_list.xml
+++ /dev/null
@@ -1,39 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
diff --git a/app/src/main/res/layout/status_node_list_fragment.xml b/app/src/main/res/layout/status_node_list_fragment.xml
new file mode 100644
index 0000000..fd000bb
--- /dev/null
+++ b/app/src/main/res/layout/status_node_list_fragment.xml
@@ -0,0 +1,38 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/app/src/main/res/layout/status_subnet_list_fragment.xml b/app/src/main/res/layout/status_subnet_list_fragment.xml
new file mode 100644
index 0000000..1470bad
--- /dev/null
+++ b/app/src/main/res/layout/status_subnet_list_fragment.xml
@@ -0,0 +1,38 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/app/src/main/res/layout/status_subnet_list_item.xml b/app/src/main/res/layout/status_subnet_list_item.xml
new file mode 100644
index 0000000..8f8e2c4
--- /dev/null
+++ b/app/src/main/res/layout/status_subnet_list_item.xml
@@ -0,0 +1,50 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index 3f2e800..827e996 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -115,8 +115,9 @@
Connected to %s
- Nodes
Network
+ Nodes
+ Subnets
Network name
IP addresses
@@ -140,6 +141,7 @@
Loading…
Node info
Close
+ Loading…
Log level: %s
diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml
index 813b040..fc4a6c3 100644
--- a/app/src/main/res/values/styles.xml
+++ b/app/src/main/res/values/styles.xml
@@ -60,6 +60,7 @@
- @dimen/activity_horizontal_margin
- @dimen/activity_vertical_margin
- vertical
+ - true