aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/org
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2017-07-04 18:51:57 +0200
committerPacien TRAN-GIRARD2017-07-04 18:51:57 +0200
commit80394001852e8400fd2c3d30e441306957d32243 (patch)
tree6835941520e9fc0e63324bb42eb08a4b6d50f746 /app/src/main/java/org
parent0b3eef56d244bf40598cba66b1e48479ecbdfaef (diff)
downloadtincapp-80394001852e8400fd2c3d30e441306957d32243.tar.gz
Add status activity
Diffstat (limited to 'app/src/main/java/org')
-rw-r--r--app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt8
-rw-r--r--app/src/main/java/org/pacien/tincapp/activities/StatusActivity.kt88
-rw-r--r--app/src/main/java/org/pacien/tincapp/commands/Executor.kt11
-rw-r--r--app/src/main/java/org/pacien/tincapp/commands/Tinc.kt2
-rw-r--r--app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt44
-rw-r--r--app/src/main/java/org/pacien/tincapp/service/VpnInterfaceConfiguraton.kt28
6 files changed, 151 insertions, 30 deletions
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 c438be0..19d01e6 100644
--- a/app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt
+++ b/app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt
@@ -12,6 +12,7 @@ import kotlinx.android.synthetic.main.base.*
12import kotlinx.android.synthetic.main.page_start.* 12import kotlinx.android.synthetic.main.page_start.*
13import org.pacien.tincapp.R 13import org.pacien.tincapp.R
14import org.pacien.tincapp.context.AppPaths 14import org.pacien.tincapp.context.AppPaths
15import org.pacien.tincapp.service.TincVpnService
15 16
16/** 17/**
17 * @author pacien 18 * @author pacien
@@ -29,6 +30,13 @@ class StartActivity : BaseActivity(), AdapterView.OnItemClickListener {
29 return super.onCreateOptionsMenu(m) 30 return super.onCreateOptionsMenu(m)
30 } 31 }
31 32
33 override fun onResume() {
34 super.onResume()
35
36 if (TincVpnService.isConnected()) startActivity(Intent(this, StatusActivity::class.java)
37 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK))
38 }
39
32 override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) { 40 override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
33 PromptActivity.requestVpnPermission((view as TextView).text.toString()) 41 PromptActivity.requestVpnPermission((view as TextView).text.toString())
34 } 42 }
diff --git a/app/src/main/java/org/pacien/tincapp/activities/StatusActivity.kt b/app/src/main/java/org/pacien/tincapp/activities/StatusActivity.kt
new file mode 100644
index 0000000..ca572ea
--- /dev/null
+++ b/app/src/main/java/org/pacien/tincapp/activities/StatusActivity.kt
@@ -0,0 +1,88 @@
1package org.pacien.tincapp.activities
2
3import android.content.Intent
4import android.os.Bundle
5import android.support.v7.app.AlertDialog
6import android.view.Menu
7import android.view.MenuItem
8import android.view.View
9import android.widget.AdapterView
10import android.widget.ArrayAdapter
11import android.widget.TextView
12import kotlinx.android.synthetic.main.base.*
13import kotlinx.android.synthetic.main.dialog_text_monopsace.view.*
14import kotlinx.android.synthetic.main.fragment_network_status_header.*
15import kotlinx.android.synthetic.main.page_status.*
16import org.pacien.tincapp.R
17import org.pacien.tincapp.commands.Tinc
18import org.pacien.tincapp.service.TincVpnService
19import org.pacien.tincapp.service.VpnInterfaceConfiguration
20
21/**
22 * @author pacien
23 */
24class StatusActivity : BaseActivity(), AdapterView.OnItemClickListener {
25
26 override fun onCreate(savedInstanceState: Bundle?) {
27 super.onCreate(savedInstanceState)
28 layoutInflater.inflate(R.layout.page_status, main_content)
29 writeContent()
30 }
31
32 override fun onCreateOptionsMenu(m: Menu): Boolean {
33 menuInflater.inflate(R.menu.menu_status, m)
34 return super.onCreateOptionsMenu(m)
35 }
36
37 override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
38 val nodeName = (view as TextView).text.toString()
39 val v = layoutInflater.inflate(R.layout.dialog_text_monopsace, main_content, false)
40 v.dialog_text_monospace.text = Tinc.info(TincVpnService.getCurrentNetName()!!, nodeName)
41
42 AlertDialog.Builder(this)
43 .setTitle(R.string.title_node_info)
44 .setView(v)
45 .setPositiveButton(R.string.action_close) { _, _ -> /* nop */ }
46 .show()
47 }
48
49 fun stopVpn(@Suppress("UNUSED_PARAMETER") i: MenuItem) {
50 TincVpnService.stopVpn()
51 startActivity(Intent(this, StartActivity::class.java).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
52 finish()
53 }
54
55 private fun TextView.setText(list: List<String>) {
56 if (list.isNotEmpty()) text = list.joinToString("\n")
57 else text = getString(R.string.value_none)
58 }
59
60 private fun getNodeNames() = Tinc.dumpNodes(TincVpnService.getCurrentNetName()!!).map { it.substringBefore(" ") }
61
62 private fun writeContent() {
63 node_list.addHeaderView(layoutInflater.inflate(R.layout.fragment_network_status_header, node_list, false), null, false)
64 node_list.addFooterView(View(this), null, false)
65 node_list.emptyView = node_list_empty
66 node_list.onItemClickListener = this
67 node_list.adapter = ArrayAdapter<String>(this, R.layout.fragment_list_item, getNodeNames())
68
69 text_network_name.text = TincVpnService.getCurrentNetName() ?: getString(R.string.value_none)
70 writeNetworkInfo(TincVpnService.getCurrentInterfaceCfg() ?: VpnInterfaceConfiguration())
71 }
72
73
74 private fun writeNetworkInfo(cfg: VpnInterfaceConfiguration) {
75 text_network_ip_addresses.setText(cfg.addresses.map { it.toString() })
76 text_network_routes.setText(cfg.routes.map { it.toString() })
77 text_network_dns_servers.setText(cfg.dnsServers)
78 text_network_search_domains.setText(cfg.searchDomains)
79 text_network_allow_bypass.text = getString(if (cfg.allowBypass) R.string.value_yes else R.string.value_no)
80
81 block_network_allowed_applications.visibility = if (cfg.allowedApplications.isNotEmpty()) View.VISIBLE else View.GONE
82 text_network_allowed_applications.setText(cfg.allowedApplications)
83
84 block_network_disallowed_applications.visibility = if (cfg.disallowedApplications.isNotEmpty()) View.VISIBLE else View.GONE
85 text_network_disallowed_applications.setText(cfg.disallowedApplications)
86 }
87
88}
diff --git a/app/src/main/java/org/pacien/tincapp/commands/Executor.kt b/app/src/main/java/org/pacien/tincapp/commands/Executor.kt
index 160f0cd..c93de64 100644
--- a/app/src/main/java/org/pacien/tincapp/commands/Executor.kt
+++ b/app/src/main/java/org/pacien/tincapp/commands/Executor.kt
@@ -3,7 +3,6 @@ package org.pacien.tincapp.commands
3import java.io.BufferedReader 3import java.io.BufferedReader
4import java.io.IOException 4import java.io.IOException
5import java.io.InputStreamReader 5import java.io.InputStreamReader
6import java.util.*
7 6
8/** 7/**
9 * @author pacien 8 * @author pacien
@@ -29,15 +28,7 @@ internal object Executor {
29 fun call(cmd: Command): List<String> { 28 fun call(cmd: Command): List<String> {
30 val proc = ProcessBuilder(cmd.asList()).start() 29 val proc = ProcessBuilder(cmd.asList()).start()
31 val outputReader = BufferedReader(InputStreamReader(proc.inputStream)) 30 val outputReader = BufferedReader(InputStreamReader(proc.inputStream))
32 31 return outputReader.readLines()
33 var line: String? = outputReader.readLine()
34 val list = LinkedList<String>()
35 while (line != null) {
36 line = outputReader.readLine()
37 list.add(line)
38 }
39
40 return list
41 } 32 }
42 33
43} 34}
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 22cbe71..e8ebb21 100644
--- a/app/src/main/java/org/pacien/tincapp/commands/Tinc.kt
+++ b/app/src/main/java/org/pacien/tincapp/commands/Tinc.kt
@@ -30,7 +30,7 @@ object Tinc {
30 } 30 }
31 31
32 @Throws(IOException::class) 32 @Throws(IOException::class)
33 fun dumpNodes(netName: String, reachable: Boolean): List<String> = 33 fun dumpNodes(netName: String, reachable: Boolean = false): List<String> =
34 Executor.call( 34 Executor.call(
35 if (reachable) newCommand(netName).withArguments("dump", "reachable", "nodes") 35 if (reachable) newCommand(netName).withArguments("dump", "reachable", "nodes")
36 else newCommand(netName).withArguments("dump", "nodes")) 36 else newCommand(netName).withArguments("dump", "nodes"))
diff --git a/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt b/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt
index 30e2956..7813601 100644
--- a/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt
+++ b/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt
@@ -3,6 +3,7 @@ package org.pacien.tincapp.service
3import android.app.Service 3import android.app.Service
4import android.content.Intent 4import android.content.Intent
5import android.net.VpnService 5import android.net.VpnService
6import android.os.ParcelFileDescriptor
6import org.pacien.tincapp.BuildConfig 7import org.pacien.tincapp.BuildConfig
7import org.pacien.tincapp.commands.Tinc 8import org.pacien.tincapp.commands.Tinc
8import org.pacien.tincapp.commands.Tincd 9import org.pacien.tincapp.commands.Tincd
@@ -11,48 +12,75 @@ import org.pacien.tincapp.context.AppPaths
11import org.pacien.tincapp.utils.applyIgnoringException 12import org.pacien.tincapp.utils.applyIgnoringException
12import java.io.IOException 13import java.io.IOException
13 14
15
14/** 16/**
15 * @author pacien 17 * @author pacien
16 */ 18 */
17class TincVpnService : VpnService() { 19class TincVpnService : VpnService() {
18 20
19 private var netName: String? = null
20
21 override fun onStartCommand(intent: Intent, flags: Int, s