diff options
author | pacien | 2017-08-22 14:14:59 +0200 |
---|---|---|
committer | pacien | 2017-08-22 14:14:59 +0200 |
commit | 91adc78116f074f0a50bfdcc2069382677ea05cf (patch) | |
tree | c03ce5e4dc1cff122941bbf426921a109f2704a5 /app/src/main/java/org/pacien/tincapp/context | |
parent | fbaddc26b5416445ef5f24b5a2f9cfb55f84343f (diff) | |
download | tincapp-91adc78116f074f0a50bfdcc2069382677ea05cf.tar.gz |
Better error handling
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/context')
-rw-r--r-- | app/src/main/java/org/pacien/tincapp/context/App.kt | 21 | ||||
-rw-r--r-- | app/src/main/java/org/pacien/tincapp/context/AppPaths.kt | 3 |
2 files changed, 24 insertions, 0 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/context/App.kt b/app/src/main/java/org/pacien/tincapp/context/App.kt index 4b7e44e..7aaa3d0 100644 --- a/app/src/main/java/org/pacien/tincapp/context/App.kt +++ b/app/src/main/java/org/pacien/tincapp/context/App.kt | |||
@@ -2,6 +2,12 @@ package org.pacien.tincapp.context | |||
2 | 2 | ||
3 | import android.app.Application | 3 | import android.app.Application |
4 | import android.content.Context | 4 | import android.content.Context |
5 | import android.content.Intent | ||
6 | import android.net.Uri | ||
7 | import android.support.annotation.StringRes | ||
8 | import android.support.v7.app.AlertDialog | ||
9 | import android.view.WindowManager | ||
10 | import org.pacien.tincapp.R | ||
5 | 11 | ||
6 | /** | 12 | /** |
7 | * @author pacien | 13 | * @author pacien |
@@ -14,9 +20,24 @@ class App : Application() { | |||
14 | } | 20 | } |
15 | 21 | ||
16 | companion object { | 22 | companion object { |
23 | |||
17 | private var appContext: Context? = null | 24 | private var appContext: Context? = null |
25 | |||
18 | fun getContext() = appContext!! | 26 | fun getContext() = appContext!! |
19 | fun getResources() = getContext().resources!! | 27 | fun getResources() = getContext().resources!! |
28 | |||
29 | fun alert(@StringRes title: Int, msg: String, manualLink: String? = null) = | ||
30 | AlertDialog.Builder(getContext(), R.style.Theme_AppCompat_Dialog) | ||
31 | .setTitle(title).setMessage(msg) | ||
32 | .apply { if (manualLink != null) setNeutralButton(R.string.action_open_manual) { _, _ -> openURL(manualLink) } } | ||
33 | .setPositiveButton(R.string.action_close, dismissAction) | ||
34 | .create().apply { window.setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR) }.show() | ||
35 | |||
36 | fun openURL(url: String) = | ||
37 | appContext?.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url)).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)) | ||
38 | |||
39 | val dismissAction = { _: Any, _: Any -> /* nop */ } | ||
40 | |||
20 | } | 41 | } |
21 | 42 | ||
22 | } | 43 | } |
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 c745d4d..673faa7 100644 --- a/app/src/main/java/org/pacien/tincapp/context/AppPaths.kt +++ b/app/src/main/java/org/pacien/tincapp/context/AppPaths.kt | |||
@@ -1,6 +1,7 @@ | |||
1 | package org.pacien.tincapp.context | 1 | package org.pacien.tincapp.context |
2 | 2 | ||
3 | import java.io.File | 3 | import java.io.File |
4 | import java.io.FileNotFoundException | ||
4 | 5 | ||
5 | /** | 6 | /** |
6 | * @author pacien | 7 | * @author pacien |
@@ -30,6 +31,8 @@ object AppPaths { | |||
30 | fun logFile(netName: String) = File(cacheDir(), String.format(LOGFILE_FORMAT, netName)) | 31 | fun logFile(netName: String) = File(cacheDir(), String.format(LOGFILE_FORMAT, netName)) |
31 | fun pidFile(netName: String) = File(App.getContext().cacheDir, String.format(PIDFILE_FORMAT, netName)) | 32 | fun pidFile(netName: String) = File(App.getContext().cacheDir, String.format(PIDFILE_FORMAT, netName)) |
32 | 33 | ||
34 | fun existing(f: File) = f.apply { if (!exists()) throw FileNotFoundException(f.absolutePath) } | ||
35 | |||
33 | fun tincd() = File(binDir(), TINCD_BIN) | 36 | fun tincd() = File(binDir(), TINCD_BIN) |
34 | fun tinc() = File(binDir(), TINC_BIN) | 37 | fun tinc() = File(binDir(), TINC_BIN) |
35 | 38 | ||