diff options
Diffstat (limited to 'app/src/main/kotlin/org/pacien/tincapp/context')
6 files changed, 398 insertions, 0 deletions
diff --git a/app/src/main/kotlin/org/pacien/tincapp/context/App.kt b/app/src/main/kotlin/org/pacien/tincapp/context/App.kt new file mode 100644 index 0000000..359cd23 --- /dev/null +++ b/app/src/main/kotlin/org/pacien/tincapp/context/App.kt | |||
@@ -0,0 +1,85 @@ | |||
1 | /* | ||
2 | * Tinc App, an Android binding and user interface for the tinc mesh VPN daemon | ||
3 | * Copyright (C) 2017-2018 Pacien TRAN-GIRARD | ||
4 | * | ||
5 | * This program is free software: you can redistribute it and/or modify | ||
6 | * it under the terms of the GNU General Public License as published by | ||
7 | * the Free Software Foundation, either version 3 of the License, or | ||
8 | * (at your option) any later version. | ||
9 | * | ||
10 | * This program is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | * GNU General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU General Public License | ||
16 | * along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
17 | */ | ||
18 | |||
19 | package org.pacien.tincapp.context | ||
20 | |||
21 | import android.app.Application | ||
22 | import android.content.Context | ||
23 | import android.content.Intent | ||
24 | import android.net.Uri | ||
25 | import android.os.Build | ||
26 | import android.os.Handler | ||
27 | import android.support.annotation.StringRes | ||
28 | import org.pacien.tincapp.BuildConfig | ||
29 | import org.pacien.tincapp.R | ||
30 | import org.slf4j.LoggerFactory | ||
31 | import java.io.File | ||
32 | |||
33 | /** | ||
34 | * @author pacien | ||
35 | */ | ||
36 | class App : Application() { | ||
37 | override fun onCreate() { | ||
38 | super.onCreate() | ||
39 | appContext = applicationContext | ||
40 | handler = Handler() | ||
41 | AppLogger.configure() | ||
42 | setupCrashHandler() | ||
43 | |||
44 | val logger = LoggerFactory.getLogger(this.javaClass) | ||
45 | logger.info("Starting tinc app {} ({} build), running on {} ({})", | ||
46 | BuildConfig.VERSION_NAME, BuildConfig.BUILD_TYPE, Build.VERSION.CODENAME, Build.VERSION.RELEASE) | ||
47 | } | ||
48 | |||
49 | private fun setupCrashHandler() { | ||
50 | val logger = LoggerFactory.getLogger(this.javaClass) | ||
51 | val systemCrashHandler = Thread.getDefaultUncaughtExceptionHandler() | ||
52 | val crashRecorder = CrashRecorder(logger, systemCrashHandler) | ||
53 | Thread.setDefaultUncaughtExceptionHandler(crashRecorder) | ||
54 | } | ||
55 | |||
56 | companion object { | ||
57 | private var appContext: Context? = null | ||
58 | private var handler: Handler? = null | ||
59 | |||
60 | val notificationManager: AppNotificationManager by lazy { AppNotificationManager(appContext!!) } | ||
61 | |||
62 | fun getContext() = appContext!! | ||
63 | fun getResources() = getContext().resources!! | ||
64 | |||
65 | fun alert(@StringRes title: Int, msg: String, manualLink: String? = null) = | ||
66 | notificationManager.notifyError(appContext!!.getString(title), msg, manualLink) | ||
67 | |||
68 | fun openURL(url: String) { | ||
69 | val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url)) | ||
70 | val chooser = Intent.createChooser(intent, getResources().getString(R.string.action_open_web_page)) | ||
71 | appContext?.startActivity(chooser.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)) | ||
72 | } | ||
73 | |||
74 | fun sendMail(recipient: String, subject: String, body: String? = null, attachment: File? = null) { | ||
75 | val intent = Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:")) | ||
76 | .putExtra(Intent.EXTRA_EMAIL, arrayOf(recipient)) | ||
77 | .putExtra(Intent.EXTRA_SUBJECT, subject) | ||
78 | .apply { if (body != null) putExtra(Intent.EXTRA_TEXT, body) } | ||
79 | .apply { if (attachment != null) putExtra(Intent.EXTRA_STREAM, Uri.fromFile(attachment)) } | ||
80 | |||
81 | val chooser = Intent.createChooser(intent, getResources().getString(R.string.action_send_email)) | ||
82 | appContext?.startActivity(chooser.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)) | ||
83 | } | ||
84 | } | ||
85 | } | ||
diff --git a/app/src/main/kotlin/org/pacien/tincapp/context/AppInfo.kt b/app/src/main/kotlin/org/pacien/tincapp/context/AppInfo.kt new file mode 100644 index 0000000..e0d49f1 --- /dev/null +++ b/app/src/main/kotlin/org/pacien/tincapp/context/AppInfo.kt | |||
@@ -0,0 +1,47 @@ | |||
1 | /* | ||
2 | * Tinc App, an Android binding and user interface for the tinc mesh VPN daemon | ||
3 | * Copyright (C) 2017-2018 Pacien TRAN-GIRARD | ||
4 | * | ||
5 | * This program is free software: you can redistribute it and/or modify | ||
6 | * it under the terms of the GNU General Public License as published by | ||
7 | * the Free Software Foundation, either version 3 of the License, or | ||
8 | * (at your option) any later version. | ||
9 | * | ||
10 | * This program is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | * GNU General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU General Public License | ||
16 | * along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
17 | */ | ||
18 | |||
19 | package org.pacien.tincapp.context | ||
20 | |||
21 | import android.os.Build | ||
22 | import org.pacien.tincapp.BuildConfig | ||
23 | import org.pacien.tincapp.R | ||
24 | |||
25 | /** | ||
26 | * @author pacien | ||
27 | */ | ||
28 | object AppInfo { | ||
29 | private fun appVersion(): String = App.getResources().getString( | ||
30 | R.string.info_version_format, | ||
31 | BuildConfig.VERSION_NAME, | ||
32 | BuildConfig.BUILD_TYPE) | ||
33 | |||
34 | private fun androidVersion(): String = App.getResources().getString( | ||
35 | R.string.info_running_on_format, | ||
36 | Build.VERSION.CODENAME, | ||
37 | Build.VERSION.RELEASE) | ||
38 | |||
39 | private fun supportedABIs(): String = App.getResources().getString( | ||
40 | R.string.info_supported_abis_format, | ||
41 | Build.SUPPORTED_ABIS.joinToString(",")) | ||
42 | |||
43 | fun all(): String = listOf( | ||
44 | appVersion(), | ||
45 | androidVersion(), | ||
46 | supportedABIs()).joinToString("\n") | ||
47 | } | ||
diff --git a/app/src/main/kotlin/org/pacien/tincapp/context/AppLogger.kt b/app/src/main/kotlin/org/pacien/tincapp/context/AppLogger.kt new file mode 100644 index 0000000..3c1be44 --- /dev/null +++ b/app/src/main/kotlin/org/pacien/tincapp/context/AppLogger.kt | |||
@@ -0,0 +1,63 @@ | |||
1 | /* | ||
2 | * Tinc App, an Android binding and user interface for the tinc mesh VPN daemon | ||
3 | * Copyright (C) 2017-2018 Pacien TRAN-GIRARD | ||
4 | * | ||
5 | * This program is free software: you can redistribute it and/or modify | ||
6 | * it under the terms of the GNU General Public License as published by | ||
7 | * the Free Software Foundation, either version 3 of the License, or | ||
8 | * (at your option) any later version. | ||
9 | * | ||
10 | * This program is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | * GNU General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU General Public License | ||
16 | * along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
17 | */ | ||
18 | |||
19 | package org.pacien.tincapp.context | ||
20 | |||
21 | import ch.qos.logback.classic.Logger | ||
22 | import ch.qos.logback.classic.LoggerContext | ||
23 | import ch.qos.logback.classic.android.LogcatAppender | ||
24 | import ch.qos.logback.classic.encoder.PatternLayoutEncoder | ||
25 | import ch.qos.logback.classic.spi.ILoggingEvent | ||
26 | import ch.qos.logback.core.Context | ||
27 | import ch.qos.logback.core.FileAppender | ||
28 | import org.slf4j.LoggerFactory | ||
29 | |||
30 | /** | ||
31 | * @author pacien | ||
32 | */ | ||
33 | object AppLogger { | ||
34 | private const val LOGCAT_PATTERN = "[%thread] %msg%n%rEx" | ||
35 | private const val LOGFILE_PATTERN = "%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n%rEx" | ||
36 | |||
37 | fun configure() { | ||
38 | (LoggerFactory.getILoggerFactory() as LoggerContext) | ||
39 | .apply { reset() } | ||
40 | .let { loggerContext -> | ||
41 | (LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME) as Logger) | ||
42 | .apply { | ||
43 | addAppender(LogcatAppender() | ||
44 | .apply { context = loggerContext } | ||
45 | .apply { encoder = patternEncoder(loggerContext, LOGCAT_PATTERN) } | ||
46 | .apply { start() }) | ||
47 | } | ||
48 | .apply { | ||
49 | addAppender(FileAppender<ILoggingEvent>() | ||
50 | .apply { context = loggerContext } | ||
51 | .apply { encoder = patternEncoder(loggerContext, LOGFILE_PATTERN) } | ||
52 | .apply { file = AppPaths.appLogFile().absolutePath } | ||
53 | .apply { start() }) | ||
54 | } | ||
55 | } | ||
56 | } | ||
57 | |||
58 | private fun patternEncoder(ctx: Context, pat: String) = | ||
59 | PatternLayoutEncoder() | ||
60 | .apply { context = ctx } | ||
61 | .apply { pattern = pat } | ||
62 | .apply { start() } | ||
63 | } | ||
diff --git a/app/src/main/kotlin/org/pacien/tincapp/context/AppNotificationManager.kt b/app/src/main/kotlin/org/pacien/tincapp/context/AppNotificationManager.kt new file mode 100644 index 0000000..d54 |