aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/org/pacien/tincapp/activities/BaseActivity.kt
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/activities/BaseActivity.kt')
-rw-r--r--app/src/main/java/org/pacien/tincapp/activities/BaseActivity.kt114
1 files changed, 114 insertions, 0 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/activities/BaseActivity.kt b/app/src/main/java/org/pacien/tincapp/activities/BaseActivity.kt
new file mode 100644
index 0000000..4dc2381
--- /dev/null
+++ b/app/src/main/java/org/pacien/tincapp/activities/BaseActivity.kt
@@ -0,0 +1,114 @@
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
19package org.pacien.tincapp.activities
20
21import android.os.Bundle
22import android.support.annotation.StringRes
23import android.support.design.widget.Snackbar
24import android.support.v7.app.AlertDialog
25import android.support.v7.app.AppCompatActivity
26import android.view.Menu
27import android.view.MenuItem
28import kotlinx.android.synthetic.main.base.*
29import org.pacien.tincapp.R
30import org.pacien.tincapp.context.App
31import org.pacien.tincapp.context.AppInfo
32import org.pacien.tincapp.context.AppPaths
33import org.pacien.tincapp.context.CrashRecorder
34import org.pacien.tincapp.utils.ProgressModal
35
36/**
37 * @author pacien
38 */
39abstract class BaseActivity : AppCompatActivity() {
40 private var active = false
41
42 override fun onCreate(savedInstanceState: Bundle?) {
43 super.onCreate(savedInstanceState)
44 setContentView(R.layout.base)
45 }
46
47 override fun onCreateOptionsMenu(m: Menu): Boolean {
48 menuInflater.inflate(R.menu.menu_base, m)
49 return true
50 }
51
52 override fun onStart() {
53 super.onStart()
54 active = true
55 }
56
57 override fun onResume() {
58 super.onResume()
59 active = true
60 }
61
62 override fun onPause() {
63 active = false
64 super.onPause()
65 }
66
67 override fun onStop() {
68 active = false
69 super.onStop()
70 }
71
72 fun aboutDialog(@Suppress("UNUSED_PARAMETER") i: MenuItem) {
73 AlertDialog.Builder(this)
74 .setTitle(resources.getString(R.string.app_name))
75 .setMessage(resources.getString(R.string.app_short_desc) + "\n\n" +
76 resources.getString(R.string.app_copyright) + " " +
77 resources.getString(R.string.app_license) + "\n\n" +
78 AppInfo.all())
79 .setNeutralButton(R.string.action_open_project_website) { _, _ -> App.openURL(resources.getString(R.string.app_website_url)) }
80 .setPositiveButton(R.string.action_close) { _, _ -> Unit }
81 .show()
82 }
83
84 fun runOnUiThread(action: () -> Unit) {
85 if (active) super.runOnUiThread(action)
86 }
87
88 fun handleRecentCrash() {
89 if (!CrashRecorder.hasPreviouslyCrashed()) return
90 CrashRecorder.dismissPreviousCrash()
91
92 AlertDialog.Builder(this)
93 .setTitle(R.string.title_app_crash)
94 .setMessage(listOf(
95 resources.getString(R.string.message_app_crash),
96 resources.getString(R.string.message_crash_logged, AppPaths.appLogFile().absolutePath)
97 ).joinToString("\n\n"))
98 .setNeutralButton(R.string.action_send_report) { _, _ ->
99 App.sendMail(
100 resources.getString(R.string.app_dev_email),
101 listOf(R.string.app_name, R.string.title_app_crash).joinToString(" / ", transform = resources::getString),
102 AppPaths.appLogFile().let { if (it.exists()) it.readText() else "" })
103 }
104 .setPositiveButton(R.string.action_close) { _, _ -> Unit }
105 .show()
106 }
107
108 protected fun notify(@StringRes msg: Int) = Snackbar.make(activity_base, msg, Snackbar.LENGTH_LONG).show()
109 protected fun notify(msg: String) = Snackbar.make(activity_base, msg, Snackbar.LENGTH_LONG).show()
110 protected fun showProgressDialog(@StringRes msg: Int): AlertDialog = ProgressModal.show(this, getString(msg))
111 protected fun showErrorDialog(msg: String): AlertDialog = AlertDialog.Builder(this)
112 .setTitle(R.string.title_error).setMessage(msg)
113 .setPositiveButton(R.string.action_close) { _, _ -> Unit }.show()
114}