aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/org
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/org')
-rw-r--r--app/src/main/java/org/pacien/tincapp/activities/status/nodes/NodeInfo.kt99
-rw-r--r--app/src/main/java/org/pacien/tincapp/activities/status/nodes/NodeInfoArrayAdapter.kt45
-rw-r--r--app/src/main/java/org/pacien/tincapp/activities/status/nodes/NodeListFragment.kt15
-rw-r--r--app/src/main/java/org/pacien/tincapp/activities/status/nodes/NodeListLiveData.kt4
4 files changed, 150 insertions, 13 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/activities/status/nodes/NodeInfo.kt b/app/src/main/java/org/pacien/tincapp/activities/status/nodes/NodeInfo.kt
new file mode 100644
index 0000000..06725dc
--- /dev/null
+++ b/app/src/main/java/org/pacien/tincapp/activities/status/nodes/NodeInfo.kt
@@ -0,0 +1,99 @@
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.status.nodes
20
21import org.pacien.tincapp.R
22import org.pacien.tincapp.context.App
23import java.util.regex.Matcher
24import java.util.regex.Pattern
25
26/**
27 * @author pacien
28 */
29data class NodeInfo(val name: String,
30 val id: String,
31 val ip: String,
32 val port: String,
33 val cipher: Int,
34 val digest: Int,
35 val macLength: Int,
36 val compression: Int,
37 val options: Int,
38 val status: Int,
39 val nextHop: String,
40 val via: String,
41 val distance: Int,
42 val pMtu: Int,
43 val minMtu: Int,
44 val maxMtu: Int) {
45
46 // https://github.com/gsliepen/tinc/blob/950bbc8f2f9c580ac85bef7bab9a3ae36ea99c4b/src/info.c#L174
47 fun reachabilityText(): String = when {
48 ip == "MYSELF" -> RESOURCES.getString(R.string.status_node_reachability_this_node)
49 distance == -1 -> RESOURCES.getString(R.string.status_node_reachability_unreachable)
50 minMtu > 0 || nextHop == name -> RESOURCES.getString(R.string.status_node_reachability_direct_connection)
51 distance > 1 -> RESOURCES.getString(R.string.status_node_reachability_via_format, nextHop)
52 else -> RESOURCES.getString(R.string.status_node_reachability_unknown)
53 }
54
55 companion object {
56 private const val NODE_DUMP_PATTERN_STRING =
57 "(\\S+) " +
58 "id (\\S+) " +
59 "at (\\S+) " +
60 "port (\\S+) " +
61 "cipher (\\S+) " +
62 "digest (\\S+) " +
63 "maclength (\\S+) " +
64 "compression (\\S+) " +
65 "options (\\S+) " +
66 "status (\\S+) " +
67 "nexthop (\\S+) " +
68 "via (\\S+) " +
69 "distance (\\S+) " +
70 "pmtu (\\S+) \\(min (\\S+) max (\\S+)\\)"
71
72 private val NODE_DUMP_PATTERN by lazy { Pattern.compile(NODE_DUMP_PATTERN_STRING) }
73 private val RESOURCES by lazy { App.getResources() }
74
75 fun ofNodeDump(line: String) =
76 ofNodeDump(NODE_DUMP_PATTERN.matcher(line).apply { find() })
77
78 private fun ofNodeDump(matcher: Matcher) = NodeInfo(
79 name = matcher[1],
80 id = matcher[2],
81 ip = matcher[3],
82 port = matcher[4],
83 cipher = matcher[5].toInt(),
84 digest = matcher[6].toInt(),
85 macLength = matcher[7].toInt(),
86 compression = matcher[8].toInt(),
87 options = matcher[9].toInt(16),
88 status = matcher[10].toInt(16),
89 nextHop = matcher[11],
90 via = matcher[12],
91 distance = matcher[13].toInt(),
92 pMtu = matcher[14].toInt(),
93 minMtu = matcher[15].toInt(),
94 maxMtu = matcher[16].toInt()
95 )
96
97 private operator fun Matcher.get(index: Int) = group(index)
98 }
99}
diff --git a/app/src/main/java/org/pacien/tincapp/activities/status/nodes/NodeInfoArrayAdapter.kt b/app/src/main/java/org/pacien/tincapp/activities/status/nodes/NodeInfoArrayAdapter.kt
new file mode 100644
index 0000000..ee103ee
--- /dev/null
+++ b/app/src/main/java/org/pacien/tincapp/activities/status/nodes/NodeInfoArrayAdapter.kt
@@ -0,0 +1,45 @@
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.status.nodes
20
21import android.content.Context
22import android.databinding.DataBindingUtil
23import android.view.LayoutInflater
24import android.view.View
25import android.view.ViewGroup
26import android.widget.ArrayAdapter
27import org.pacien.tincapp.databinding.StatusNodeListItemBinding
28
29/**
30 * @author pacien
31 */
32class NodeInfoArrayAdapter(context: Context?, private val onItemClick: (NodeInfo) -> Unit) : ArrayAdapter<NodeInfo>(context, -1) {
33 private val layoutInflater = LayoutInflater.from(context)!!
34
35 override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
36 val binding = when (convertView) {
37 null -> StatusNodeListItemBinding.inflate(layoutInflater, parent, false)
38 else -> DataBindingUtil.getBinding(convertView)!!
39 }
40
41 binding.nodeInfo = getItem(position)
42 binding.onClick = onItemClick
43 return binding.root
44 }
45}
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 115809e..1f762c0 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
@@ -26,9 +26,6 @@ import android.support.v7.app.AlertDialog
26import android.view.LayoutInflater 26import android.view.LayoutInflater
27import android.view.View 27import android.view.View
28import android.view.ViewGroup 28import android.view.ViewGroup
29import android.widget.AdapterView
30import android.widget.ArrayAdapter
31import android.widget.TextView
32import kotlinx.android.synthetic.main.base.* 29import kotlinx.android.synthetic.main.base.*
33import kotlinx.android.synthetic.main.status_node_info_dialog.view.* 30import kotlinx.android.synthetic.main.status_node_info_dialog.view.*
34import kotlinx.android.synthetic.main.status_node_list.* 31import kotlinx.android.synthetic.main.status_node_list.*
@@ -47,8 +44,8 @@ class NodeListFragment : Fragment() {
47 private val tincCtl = Tinc 44 private val tincCtl = Tinc
48 private val netName by lazy { vpnService.getCurrentNetName()!! } 45 private val netName by lazy { vpnService.getCurrentNetName()!! }
49 private val nodeListViewModel by lazy { ViewModelProviders.of(this).get(NodeListViewModel::class.java) } 46 private val nodeListViewModel by lazy { ViewModelProviders.of(this).get(NodeListViewModel::class.java) }
50 private val nodeListAdapter by lazy { ArrayAdapter<String>(context, R.layout.status_node_list_item) } 47 private val nodeListAdapter by lazy { NodeInfoArrayAdapter(context, this::onItemClick) }
51 private val nodeListObserver by lazy { Observer<List<String>> { nodeListAdapter.setElements(it) } } 48 private val nodeListObserver by lazy { Observer<List<NodeInfo>> { nodeListAdapter.setElements(it) } }
52 49
53 override fun onCreate(savedInstanceState: Bundle?) { 50 override fun onCreate(savedInstanceState: Bundle?) {
54 super.onCreate(savedInstanceState) 51 super.onCreate(savedInstanceState)
@@ -63,15 +60,11 @@ class NodeListFragment : Fragment() {
63 status_node_list.hideTopSeparator() 60 status_node_list.hideTopSeparator()
64 status_node_list.hideBottomSeparator() 61 status_node_list.hideBottomSeparator()
65 status_node_list.emptyView = status_node_list_placeholder 62 status_node_list.emptyView = status_node_list_placeholder
66 status_node_list.onItemClickListener = AdapterView.OnItemClickListener(this::onItemClick)
67 status_node_list.adapter = nodeListAdapter 63 status_node_list.adapter = nodeListAdapter
68 } 64 }
69 65
70 @Suppress("UNUSED_PARAMETER") 66 private fun onItemClick(nodeInfo: NodeInfo) =
71 private fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) = when (view) { 67 showNodeInfo(nodeInfo.name)
72 is TextView -> showNodeInfo(view.text.toString())