diff options
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/context')
-rw-r--r-- | app/src/main/java/org/pacien/tincapp/context/AppPaths.kt | 24 |
1 files changed, 13 insertions, 11 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 bd8316a..9aa2037 100644 --- a/app/src/main/java/org/pacien/tincapp/context/AppPaths.kt +++ b/app/src/main/java/org/pacien/tincapp/context/AppPaths.kt | |||
@@ -18,7 +18,6 @@ | |||
18 | 18 | ||
19 | package org.pacien.tincapp.context | 19 | package org.pacien.tincapp.context |
20 | 20 | ||
21 | import android.os.Environment | ||
22 | import java.io.File | 21 | import java.io.File |
23 | import java.io.FileNotFoundException | 22 | import java.io.FileNotFoundException |
24 | 23 | ||
@@ -28,6 +27,10 @@ import java.io.FileNotFoundException | |||
28 | * @implNote Logs and PID files are stored in the cache directory for automatic collection. | 27 | * @implNote Logs and PID files are stored in the cache directory for automatic collection. |
29 | */ | 28 | */ |
30 | object AppPaths { | 29 | object AppPaths { |
30 | private const val APP_LOG_DIR = "log" | ||
31 | private const val APP_TINC_RUNTIME_DIR = "run" | ||
32 | private const val APP_TINC_NETWORKS_DIR = "networks" | ||
33 | |||
31 | private const val TINCD_BIN = "libtincd.so" | 34 | private const val TINCD_BIN = "libtincd.so" |
32 | private const val TINC_BIN = "libtinc.so" | 35 | private const val TINC_BIN = "libtinc.so" |
33 | 36 | ||
@@ -46,25 +49,24 @@ object AppPaths { | |||
46 | 49 | ||
47 | private val context by lazy { App.getContext() } | 50 | private val context by lazy { App.getContext() } |
48 | 51 | ||
49 | fun storageAvailable() = | 52 | private fun cacheDir() = context.cacheDir!! |
50 | Environment.getExternalStorageState().let { it == Environment.MEDIA_MOUNTED && it != Environment.MEDIA_MOUNTED_READ_ONLY } | ||
51 | |||
52 | fun internalCacheDir() = context.cacheDir!! | ||
53 | fun cacheDir() = context.externalCacheDir ?: internalCacheDir() | ||
54 | fun confDir() = context.getExternalFilesDir(null)!! | ||
55 | private fun binDir() = File(context.applicationInfo.nativeLibraryDir) | 53 | private fun binDir() = File(context.applicationInfo.nativeLibraryDir) |
54 | fun runtimeDir() = withDir(File(cacheDir(), APP_TINC_RUNTIME_DIR)) | ||
55 | fun logDir() = withDir(File(cacheDir(), APP_LOG_DIR)) | ||
56 | fun confDir() = withDir(File(context.filesDir!!, APP_TINC_NETWORKS_DIR)) | ||
56 | 57 | ||
57 | fun confDir(netName: String) = File(confDir(), netName) | 58 | fun confDir(netName: String) = File(confDir(), netName) |
58 | fun hostsDir(netName: String) = File(confDir(netName), NET_HOSTS_DIR) | 59 | fun hostsDir(netName: String) = File(confDir(netName), NET_HOSTS_DIR) |
59 | fun netConfFile(netName: String) = File(confDir(netName), NET_CONF_FILE) | 60 | fun netConfFile(netName: String) = File(confDir(netName), NET_CONF_FILE) |
60 | fun tincConfFile(netName: String) = File(confDir(netName), NET_TINC_CONF_FILE) | 61 | fun tincConfFile(netName: String) = File(confDir(netName), NET_TINC_CONF_FILE) |
61 | fun invitationFile(netName: String) = File(confDir(netName), NET_INVITATION_FILE) | 62 | fun invitationFile(netName: String) = File(confDir(netName), NET_INVITATION_FILE) |
62 | fun logFile(netName: String) = File(cacheDir(), String.format(LOGFILE_FORMAT, netName)) | 63 | fun logFile(netName: String) = File(logDir(), String.format(LOGFILE_FORMAT, netName)) |
63 | fun pidFile(netName: String) = File(context.cacheDir, String.format(PIDFILE_FORMAT, netName)) | 64 | fun pidFile(netName: String) = File(runtimeDir(), String.format(PIDFILE_FORMAT, netName)) |
64 | fun appLogFile() = File(cacheDir(), APPLOG_FILE) | 65 | fun appLogFile() = File(logDir(), APPLOG_FILE) |
65 | fun crashFlagFile() = File(internalCacheDir(), CRASHFLAG_FILE) | 66 | fun crashFlagFile() = File(cacheDir(), CRASHFLAG_FILE) |
66 | 67 | ||
67 | fun existing(f: File) = f.apply { if (!exists()) throw FileNotFoundException(f.absolutePath) } | 68 | fun existing(f: File) = f.apply { if (!exists()) throw FileNotFoundException(f.absolutePath) } |
69 | fun withDir(f: File) = f.apply { if (!exists()) mkdirs() } | ||
68 | 70 | ||
69 | fun defaultEd25519PrivateKeyFile(netName: String) = File(confDir(netName), NET_DEFAULT_ED25519_PRIVATE_KEY_FILE) | 71 | fun defaultEd25519PrivateKeyFile(netName: String) = File(confDir(netName), NET_DEFAULT_ED25519_PRIVATE_KEY_FILE) |
70 | fun defaultRsaPrivateKeyFile(netName: String) = File(confDir(netName), NET_DEFAULT_RSA_PRIVATE_KEY_FILE) | 72 | fun defaultRsaPrivateKeyFile(netName: String) = File(confDir(netName), NET_DEFAULT_RSA_PRIVATE_KEY_FILE) |