From 3e3135b0c7fba811735a30e7fd155ca1e188c787 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sat, 1 Jul 2017 16:24:22 +0200 Subject: Use global app context --- app/src/main/AndroidManifest.xml | 1 + .../org/pacien/tincapp/activities/BaseActivity.kt | 2 +- .../org/pacien/tincapp/activities/StartActivity.kt | 2 +- .../main/java/org/pacien/tincapp/commands/Tinc.kt | 33 +++++++++++----------- .../main/java/org/pacien/tincapp/commands/Tincd.kt | 13 ++++----- .../main/java/org/pacien/tincapp/context/App.kt | 22 +++++++++++++++ .../java/org/pacien/tincapp/context/AppInfo.kt | 15 +++++----- .../java/org/pacien/tincapp/context/AppPaths.kt | 20 ++++++------- .../org/pacien/tincapp/service/TincVpnService.kt | 6 ++-- 9 files changed, 66 insertions(+), 48 deletions(-) create mode 100644 app/src/main/java/org/pacien/tincapp/context/App.kt diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 2822d37..8321208 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -6,6 +6,7 @@ openWebsite(R.string.app_website_url) } .setPositiveButton(R.string.action_close) { _, _ -> /* nop */ } .show() 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 71d5403..3807ddc 100644 --- a/app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt +++ b/app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt @@ -57,7 +57,7 @@ class StartActivity : BaseActivity() { } fun confDirDialog(@Suppress("UNUSED_PARAMETER") v: View) { - val confDir = AppPaths.confDir(this).path + val confDir = AppPaths.confDir().path AlertDialog.Builder(this) .setTitle(R.string.title_tinc_config_dir) 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 eb5689a..91a2678 100644 --- a/app/src/main/java/org/pacien/tincapp/commands/Tinc.kt +++ b/app/src/main/java/org/pacien/tincapp/commands/Tinc.kt @@ -1,6 +1,5 @@ package org.pacien.tincapp.commands -import android.content.Context import org.pacien.tincapp.context.AppPaths import java.io.IOException @@ -9,22 +8,22 @@ import java.io.IOException */ object Tinc { - private fun newCommand(ctx: Context, netName: String): Command = - Command(AppPaths.tinc(ctx).absolutePath) - .withOption("config", AppPaths.confDir(ctx, netName).absolutePath) - .withOption("pidfile", AppPaths.pidFile(ctx, netName).absolutePath) + private fun newCommand(netName: String): Command = + Command(AppPaths.tinc().absolutePath) + .withOption("config", AppPaths.confDir(netName).absolutePath) + .withOption("pidfile", AppPaths.pidFile(netName).absolutePath) // independently runnable commands @Throws(IOException::class) - fun network(ctx: Context): List = - Executor.call(Command(AppPaths.tinc(ctx).absolutePath) - .withOption("config", AppPaths.confDir(ctx).absolutePath) + fun network(): List = + Executor.call(Command(AppPaths.tinc().absolutePath) + .withOption("config", AppPaths.confDir().absolutePath) .withArguments("network")) @Throws(IOException::class) - fun fsck(ctx: Context, netName: String, fix: Boolean): List { - var cmd = newCommand(ctx, netName).withArguments("fsck") + fun fsck(netName: String, fix: Boolean): List { + var cmd = newCommand(netName).withArguments("fsck") if (fix) cmd = cmd.withOption("force") return Executor.call(cmd) } @@ -32,18 +31,18 @@ object Tinc { // commands requiring a running tinc daemon @Throws(IOException::class) - fun stop(ctx: Context, netName: String) { - Executor.call(newCommand(ctx, netName).withArguments("stop")) + fun stop(netName: String) { + Executor.call(newCommand(netName).withArguments("stop")) } @Throws(IOException::class) - fun dumpNodes(ctx: Context, netName: String, reachable: Boolean): List = + fun dumpNodes(netName: String, reachable: Boolean): List = Executor.call( - if (reachable) newCommand(ctx, netName).withArguments("dump", "reachable", "nodes") - else newCommand(ctx, netName).withArguments("dump", "nodes")) + if (reachable) newCommand(netName).withArguments("dump", "reachable", "nodes") + else newCommand(netName).withArguments("dump", "nodes")) @Throws(IOException::class) - fun info(ctx: Context, netName: String, node: String): String = - Executor.call(newCommand(ctx, netName).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 9f2491e..19ebfbd 100644 --- a/app/src/main/java/org/pacien/tincapp/commands/Tincd.kt +++ b/app/src/main/java/org/pacien/tincapp/commands/Tincd.kt @@ -1,9 +1,6 @@ package org.pacien.tincapp.commands -import android.content.Context - import org.pacien.tincapp.context.AppPaths - import java.io.IOException /** @@ -12,12 +9,12 @@ import java.io.IOException object Tincd { @Throws(IOException::class) - fun start(ctx: Context, netName: String, fd: Int) { - Executor.forkExec(Command(AppPaths.tincd(ctx).absolutePath) + fun start(netName: String, fd: Int) { + Executor.forkExec(Command(AppPaths.tincd().absolutePath) .withOption("no-detach") - .withOption("config", AppPaths.confDir(ctx, netName).absolutePath) - .withOption("pidfile", AppPaths.pidFile(ctx, netName).absolutePath) - .withOption("logfile", AppPaths.logFile(ctx, netName).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/App.kt b/app/src/main/java/org/pacien/tincapp/context/App.kt new file mode 100644 index 0000000..4b7e44e --- /dev/null +++ b/app/src/main/java/org/pacien/tincapp/context/App.kt @@ -0,0 +1,22 @@ +package org.pacien.tincapp.context + +import android.app.Application +import android.content.Context + +/** + * @author pacien + */ +class App : Application() { + + override fun onCreate() { + super.onCreate() + appContext = applicationContext + } + + companion object { + private var appContext: Context? = null + fun getContext() = appContext!! + fun getResources() = getContext().resources!! + } + +} diff --git a/app/src/main/java/org/pacien/tincapp/context/AppInfo.kt b/app/src/main/java/org/pacien/tincapp/context/AppInfo.kt index 2eb2aa1..39ef0f1 100644 --- a/app/src/main/java/org/pacien/tincapp/context/AppInfo.kt +++ b/app/src/main/java/org/pacien/tincapp/context/AppInfo.kt @@ -1,6 +1,5 @@ package org.pacien.tincapp.context -import android.content.res.Resources import android.os.Build import org.pacien.tincapp.BuildConfig import org.pacien.tincapp.R @@ -10,23 +9,23 @@ import org.pacien.tincapp.R */ object AppInfo { - fun appVersion(r: Resources): String = r.getString( + fun appVersion(): String = App.getResources().getString( R.string.info_version_format, BuildConfig.VERSION_NAME, BuildConfig.BUILD_TYPE) - fun androidVersion(r: Resources): String = r.getString( + fun androidVersion(): String = App.getResources().getString( R.string.info_running_on_format, Build.VERSION.CODENAME, Build.VERSION.RELEASE) - fun supportedABIs(r: Resources): String = r.getString( + fun supportedABIs(): String = App.getResources().getString( R.string.info_supported_abis_format, Build.SUPPORTED_ABIS.joinToString(",")) - fun all(r: Resources): String = listOf( - appVersion(r), - androidVersion(r), - supportedABIs(r)).joinToString("\n") + fun all(): String = listOf( + appVersion(), + androidVersion(), + supportedABIs()).joinToString("\n") } 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 21db0a4..06bb318 100644 --- a/app/src/main/java/org/pacien/tincapp/context/AppPaths.kt +++ b/app/src/main/java/org/pacien/tincapp/context/AppPaths.kt @@ -29,15 +29,15 @@ object AppPaths { return f } - fun confDir(ctx: Context): File = ctx.getDir(CONFDIR, Context.MODE_PRIVATE) - fun confDir(ctx: Context, netName: String): File = File(confDir(ctx), netName) - fun logDir(ctx: Context): File = createDirIfNotExists(ctx.cacheDir, LOGDIR) - fun pidDir(ctx: Context): File = createDirIfNotExists(ctx.cacheDir, PIDDIR) - fun logFile(ctx: Context, netName: String): File = File(logDir(ctx), String.format(LOGFILE_FORMAT, netName)) - fun pidFile(ctx: Context, netName: String): File = File(pidDir(ctx), String.format(PIDFILE_FORMAT, netName)) - fun netConfFile(ctx: Context, netName: String): File = File(confDir(ctx, netName), NET_CONF_FILE) - fun binDir(ctx: Context): File = File(ctx.applicationInfo.nativeLibraryDir) - fun tincd(ctx: Context): File = File(binDir(ctx), TINCD_BIN) - fun tinc(ctx: Context): File = File(binDir(ctx), TINC_BIN) + fun confDir(): File = App.getContext().getDir(CONFDIR, Context.MODE_PRIVATE) + fun confDir(netName: String): File = File(confDir(), netName) + fun logDir(): File = createDirIfNotExists(App.getContext().cacheDir, LOGDIR) + fun pidDir(): File = createDirIfNotExists(App.getContext().cacheDir, PIDDIR) + fun logFile(netName: String): File = File(logDir(), String.format(LOGFILE_FORMAT, netName)) + fun pidFile(netName: String): File = File(pidDir(), String.format(PIDFILE_FORMAT, netName)) + fun netConfFile(netName: String): File = File(confDir(netName), NET_CONF_FILE) + fun binDir(): File = File(App.getContext().applicationInfo.nativeLibraryDir) + fun tincd(): File = File(binDir(), TINCD_BIN) + fun tinc(): File = 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 c5e1c51..cb520bb 100644 --- a/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt +++ b/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt @@ -21,11 +21,11 @@ class TincVpnService : VpnService() { this.netName = intent.getStringExtra(INTENT_EXTRA_NET_NAME) val net = Builder().setSession(this.netName) - net.apply(VpnInterfaceConfiguration(AppPaths.netConfFile(this, this.netName))) + net.apply(VpnInterfaceConfiguration(AppPaths.netConfFile(this.netName))) applyIgnoringException(net::addDisallowedApplication, BuildConfig.APPLICATION_ID) try { - Tincd.start(this, this.netName, net.establish().detachFd()) + Tincd.start(this.netName, net.establish().detachFd()) } catch (e: IOException) { e.printStackTrace() } @@ -35,7 +35,7 @@ class TincVpnService : VpnService() { override fun onDestroy() { try { - Tinc.stop(this, this.netName) + Tinc.stop(this.netName) } catch (e: IOException) { e.printStackTrace() } -- cgit v1.2.3