From ab577b06a6eaa9b8dd950b79b658ed8c08eef1c9 Mon Sep 17 00:00:00 2001
From: Pacien TRAN-GIRARD
Date: Mon, 3 Jul 2017 17:59:30 +0200
Subject: Drop useless support for configuration in internal storage

---
 .../pacien/tincapp/activities/ConfigureActivity.kt |  6 ++---
 .../org/pacien/tincapp/activities/StartActivity.kt |  9 ++------
 .../main/java/org/pacien/tincapp/commands/Tinc.kt  | 24 ++++++++++----------
 .../main/java/org/pacien/tincapp/commands/Tincd.kt |  8 +++----
 .../java/org/pacien/tincapp/context/AppPaths.kt    | 26 +++++-----------------
 .../org/pacien/tincapp/service/TincVpnService.kt   | 14 ++++++------
 6 files changed, 34 insertions(+), 53 deletions(-)

(limited to 'app/src/main/java')

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 69408d8..3590f99 100644
--- a/app/src/main/java/org/pacien/tincapp/activities/ConfigureActivity.kt
+++ b/app/src/main/java/org/pacien/tincapp/activities/ConfigureActivity.kt
@@ -20,9 +20,9 @@ class ConfigureActivity : BaseActivity() {
     }
 
     private fun writeContent() {
-        text_configuration_directories.text = AppPaths.Storage.values().map { AppPaths.confDir(it) }.joinToString("\n")
-        text_log_directories.text = AppPaths.Storage.values().map { AppPaths.cacheDir(it) }.joinToString("\n")
-        text_tinc_binaries.text = listOf(AppPaths.tinc(), AppPaths.tincd()).joinToString("\n")
+        text_configuration_directory.text = AppPaths.confDir().absolutePath
+        text_log_directory.text = AppPaths.cacheDir().absolutePath
+        text_tinc_binary.text = AppPaths.tinc().absolutePath
     }
 
     fun generateConf(@Suppress("UNUSED_PARAMETER") v: View) = notify("Not implemented yet")
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 709989b..1c4b5a1 100644
--- a/app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt
+++ b/app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt
@@ -13,9 +13,7 @@ import android.view.ViewGroup
 import android.widget.EditText
 import android.widget.FrameLayout
 import kotlinx.android.synthetic.main.base.*
-
 import org.pacien.tincapp.R
-import org.pacien.tincapp.context.AppPaths
 import org.pacien.tincapp.service.TincVpnService
 
 /**
@@ -65,10 +63,7 @@ class StartActivity : BaseActivity() {
 
     fun openConfigureActivity(@Suppress("UNUSED_PARAMETER") i: MenuItem) = startActivity(Intent(this, ConfigureActivity::class.java))
 
-    private fun startVpn(netName: String) {
-        startService(Intent(this, TincVpnService::class.java).putExtra(TincVpnService.INTENT_EXTRA_NET_CONF,
-                if (netName.startsWith("external/")) AppPaths.NetConf(AppPaths.Storage.EXTERNAL, netName.substringAfter("/"))
-                else AppPaths.NetConf(AppPaths.Storage.INTERNAL, netName)))
-    }
+    private fun startVpn(netName: String) =
+            startService(Intent(this, TincVpnService::class.java).putExtra(TincVpnService.INTENT_EXTRA_NET_NAME, 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 5116d63..22cbe71 100644
--- a/app/src/main/java/org/pacien/tincapp/commands/Tinc.kt
+++ b/app/src/main/java/org/pacien/tincapp/commands/Tinc.kt
@@ -8,16 +8,16 @@ import java.io.IOException
  */
 object Tinc {
 
-    private fun newCommand(netConf: AppPaths.NetConf): Command =
+    private fun newCommand(netName: String): Command =
             Command(AppPaths.tinc().absolutePath)
-                    .withOption("config", AppPaths.confDir(netConf).absolutePath)
-                    .withOption("pidfile", AppPaths.pidFile(netConf).absolutePath)
+                    .withOption("config", AppPaths.confDir(netName).absolutePath)
+                    .withOption("pidfile", AppPaths.pidFile(netName).absolutePath)
 
     // independently runnable commands
 
     @Throws(IOException::class)
-    fun fsck(netConf: AppPaths.NetConf, fix: Boolean): List<String> {
-        var cmd = newCommand(netConf).withArguments("fsck")
+    fun fsck(netName: String, fix: Boolean): List<String> {
+        var cmd = newCommand(netName).withArguments("fsck")
         if (fix) cmd = cmd.withOption("force")
         return Executor.call(cmd)
     }
@@ -25,18 +25,18 @@ object Tinc {
     // commands requiring a running tinc daemon
 
     @Throws(IOException::class)
-    fun stop(netConf: AppPaths.NetConf) {
-        Executor.call(newCommand(netConf).withArguments("stop"))
+    fun stop(netName: String) {
+        Executor.call(newCommand(netName).withArguments("stop"))
     }
 
     @Throws(IOException::class)
-    fun dumpNodes(netConf: AppPaths.NetConf, reachable: Boolean): List<String> =
+    fun dumpNodes(netName: String, reachable: Boolean): List<String> =
             Executor.call(
-                    if (reachable) newCommand(netConf).withArguments("dump", "reachable", "nodes")
-                    else newCommand(netConf).withArguments("dump", "nodes"))
+                    if (reachable) newCommand(netName).withArguments("dump", "reachable", "nodes")
+                    else newCommand(netName).withArguments("dump", "nodes"))
 
     @Throws(IOException::class)
-    fun info(netConf: AppPaths.NetConf, node: String): String =
-            Executor.call(newCommand(netConf).withArguments("info", node)).joinToString("\n")
+    fun info(netName: String, node: String): String =
+            Executor.call(newCommand(netName).withArguments("info", node)).joinToString("\n")
 
 }
diff --git a/app/src/main/java/org/pacien/tincapp/commands/Tincd.kt b/app/src/main/java/org/pacien/tincapp/commands/Tincd.kt
index c54b328..19ebfbd 100644
--- a/app/src/main/java/org/pacien/tincapp/commands/Tincd.kt
+++ b/app/src/main/java/org/pacien/tincapp/commands/Tincd.kt
@@ -9,12 +9,12 @@ import java.io.IOException
 object Tincd {
 
     @Throws(IOException::class)
-    fun start(netConf: AppPaths.NetConf, fd: Int) {
+    fun start(netName: String, fd: Int) {
         Executor.forkExec(Command(AppPaths.tincd().absolutePath)
                 .withOption("no-detach")
-                .withOption("config", AppPaths.confDir(netConf).absolutePath)
-                .withOption("pidfile", AppPaths.pidFile(netConf).absolutePath)
-                .withOption("logfile", AppPaths.logFile(netConf).absolutePath)
+                .withOption("config", AppPaths.confDir(netName).absolutePath)
+                .withOption("pidfile", AppPaths.pidFile(netName).absolutePath)
+                .withOption("logfile", AppPaths.logFile(netName).absolutePath)
                 .withOption("option", "DeviceType=fd")
                 .withOption("option", "Device=" + fd))
     }
diff --git a/app/src/main/java/org/pacien/tincapp/context/AppPaths.kt b/app/src/main/java/org/pacien/tincapp/context/AppPaths.kt
index 58c6de2..078a81d 100644
--- a/app/src/main/java/org/pacien/tincapp/context/AppPaths.kt
+++ b/app/src/main/java/org/pacien/tincapp/context/AppPaths.kt
@@ -1,7 +1,6 @@
 package org.pacien.tincapp.context
 
 import java.io.File
-import java.io.Serializable
 
 /**
  * @author pacien
@@ -10,9 +9,6 @@ import java.io.Serializable
  */
 object AppPaths {
 
-    enum class Storage { INTERNAL, EXTERNAL }
-    data class NetConf(val storage: Storage, val netName: String) : Serializable
-
     private val TINCD_BIN = "libtincd.so"
     private val TINC_BIN = "libtinc.so"
 
@@ -21,24 +17,14 @@ object AppPaths {
 
     private val NET_CONF_FILE = "network.conf"
 
-    fun filesDir(storage: Storage): File = when (storage) {
-        Storage.INTERNAL -> App.getContext().filesDir
-        Storage.EXTERNAL -> App.getContext().getExternalFilesDir(null)
-    }
-
-    fun cacheDir(storage: Storage): File = when (storage) {
-        Storage.INTERNAL -> App.getContext().cacheDir
-        Storage.EXTERNAL -> App.getContext().externalCacheDir
-    }
-
+    fun cacheDir() = App.getContext().externalCacheDir!!
+    fun confDir() = App.getContext().getExternalFilesDir(null)!!
     fun binDir() = File(App.getContext().applicationInfo.nativeLibraryDir)
 
-    fun confDir(storage: Storage) = filesDir(storage)
-    fun confDir(netConf: NetConf) = File(confDir(netConf.storage), netConf.netName)
-
-    fun netConfFile(netConf: NetConf) = File(confDir(netConf), NET_CONF_FILE)
-    fun logFile(netConf: NetConf) = File(cacheDir(netConf.storage), String.format(LOGFILE_FORMAT, netConf.netName))
-    fun pidFile(netConf: NetConf) = File(cacheDir(Storage.INTERNAL), String.format(PIDFILE_FORMAT, netConf.netName))
+    fun confDir(netName: String) = File(confDir(), netName)
+    fun netConfFile(netName: String) = File(confDir(netName), NET_CONF_FILE)
+    fun logFile(netName: String) = File(cacheDir(), String.format(LOGFILE_FORMAT, netName))
+    fun pidFile(netName: String) = File(App.getContext().cacheDir, String.format(PIDFILE_FORMAT, netName))
 
     fun tincd() = File(binDir(), TINCD_BIN)
     fun tinc() = File(binDir(), TINC_BIN)
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 366e208..31541b3 100644
--- a/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt
+++ b/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt
@@ -15,17 +15,17 @@ import java.io.IOException
  */
 class TincVpnService : VpnService() {
 
-    private var netConf: AppPaths.NetConf? = null
+    private var netName: String? = null
 
     override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
-        netConf = intent.getSerializableExtra(INTENT_EXTRA_NET_CONF)!! as AppPaths.NetConf
+        netName = intent.getStringExtra(INTENT_EXTRA_NET_NAME)!!
 
-        val net = Builder().setSession(netConf!!.netName)
-        net.apply(VpnInterfaceConfiguration(AppPaths.netConfFile(netConf!!)))
+        val net = Builder().setSession(netName)
+        net.apply(VpnInterfaceConfiguration(AppPaths.netConfFile(netName!!)))
         applyIgnoringException(net::addDisallowedApplication, BuildConfig.APPLICATION_ID)
 
         try {
-            Tincd.start(netConf!!, net.establish().detachFd())
+            Tincd.start(netName!!, net.establish().detachFd())
         } catch (e: IOException) {
             e.printStackTrace()
         }
@@ -35,7 +35,7 @@ class TincVpnService : VpnService() {
 
     override fun onDestroy() {
         try {
-            Tinc.stop(netConf!!)
+            Tinc.stop(netName!!)
         } catch (e: IOException) {
             e.printStackTrace()
         }
@@ -43,7 +43,7 @@ class TincVpnService : VpnService() {
     }
 
     companion object {
-        val INTENT_EXTRA_NET_CONF = "netConf"
+        val INTENT_EXTRA_NET_NAME = "netName"
     }
 
 }
-- 
cgit v1.2.3