diff options
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/context/AppPaths.kt')
-rw-r--r-- | app/src/main/java/org/pacien/tincapp/context/AppPaths.kt | 41 |
1 files changed, 22 insertions, 19 deletions
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 06bb318..58c6de2 100644 --- a/app/src/main/java/org/pacien/tincapp/context/AppPaths.kt +++ b/app/src/main/java/org/pacien/tincapp/context/AppPaths.kt | |||
@@ -1,8 +1,7 @@ | |||
1 | package org.pacien.tincapp.context | 1 | package org.pacien.tincapp.context |
2 | 2 | ||
3 | import android.content.Context | ||
4 | |||
5 | import java.io.File | 3 | import java.io.File |
4 | import java.io.Serializable | ||
6 | 5 | ||
7 | /** | 6 | /** |
8 | * @author pacien | 7 | * @author pacien |
@@ -11,9 +10,8 @@ import java.io.File | |||
11 | */ | 10 | */ |
12 | object AppPaths { | 11 | object AppPaths { |
13 | 12 | ||
14 | private val CONFDIR = "conf" | 13 | enum class Storage { INTERNAL, EXTERNAL } |
15 | private val LOGDIR = "log" | 14 | data class NetConf(val storage: Storage, val netName: String) : Serializable |
16 | private val PIDDIR = "pid" | ||
17 | 15 | ||
18 | private val TINCD_BIN = "libtincd.so" | 16 | private val TINCD_BIN = "libtincd.so" |
19 | private val TINC_BIN = "libtinc.so" | 17 | private val TINC_BIN = "libtinc.so" |
@@ -23,21 +21,26 @@ object AppPaths { | |||
23 | 21 | ||
24 | private val NET_CONF_FILE = "network.conf" | 22 | private val NET_CONF_FILE = "network.conf" |
25 | 23 | ||
26 | private fun createDirIfNotExists(basePath: File, newDir: String): File { | 24 | fun filesDir(storage: Storage): File = when (storage) { |
27 | val f = File(basePath, newDir) | 25 | Storage.INTERNAL -> App.getContext().filesDir |
28 | f.mkdirs() | 26 | Storage.EXTERNAL -> App.getContext().getExternalFilesDir(null) |
29 | return f | 27 | } |
28 | |||
29 | fun cacheDir(storage: Storage): File = when (storage) { | ||
30 | Storage.INTERNAL -> App.getContext().cacheDir | ||
31 | Storage.EXTERNAL -> App.getContext().externalCacheDir | ||
30 | } | 32 | } |
31 | 33 | ||
32 | fun confDir(): File = App.getContext().getDir(CONFDIR, Context.MODE_PRIVATE) | 34 | fun binDir() = File(App.getContext().applicationInfo.nativeLibraryDir) |
33 | fun confDir(netName: String): File = File(confDir(), netName) | 35 | |
34 | fun logDir(): File = createDirIfNotExists(App.getContext().cacheDir, LOGDIR) | 36 | fun confDir(storage: Storage) = filesDir(storage) |
35 | fun pidDir(): File = createDirIfNotExists(App.getContext().cacheDir, PIDDIR) | 37 | fun confDir(netConf: NetConf) = File(confDir(netConf.storage), netConf.netName) |
36 | fun logFile(netName: String): File = File(logDir(), String.format(LOGFILE_FORMAT, netName)) | 38 | |
37 | fun pidFile(netName: String): File = File(pidDir(), String.format(PIDFILE_FORMAT, netName)) | 39 | fun netConfFile(netConf: NetConf) = File(confDir(netConf), NET_CONF_FILE) |
38 | fun netConfFile(netName: String): File = File(confDir(netName), NET_CONF_FILE) | 40 | fun logFile(netConf: NetConf) = File(cacheDir(netConf.storage), String.format(LOGFILE_FORMAT, netConf.netName)) |
39 | fun binDir(): File = File(App.getContext().applicationInfo.nativeLibraryDir) | 41 | fun pidFile(netConf: NetConf) = File(cacheDir(Storage.INTERNAL), String.format(PIDFILE_FORMAT, netConf.netName)) |
40 | fun tincd(): File = File(binDir(), TINCD_BIN) | 42 | |
41 | fun tinc(): File = File(binDir(), TINC_BIN) | 43 | fun tincd() = File(binDir(), TINCD_BIN) |
44 | fun tinc() = File(binDir(), TINC_BIN) | ||
42 | 45 | ||
43 | } | 46 | } |