aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/org/pacien/tincapp/activities/viewlog
diff options
context:
space:
mode:
authorpacien2018-08-19 18:04:58 +0200
committerpacien2018-08-19 18:04:58 +0200
commit483e6634e0621d2100ae11cbcd8cba6d21a76c4e (patch)
tree0d2fda7f5e6026c5f091df2188f2e8396de42091 /app/src/main/java/org/pacien/tincapp/activities/viewlog
parent3b353c4037f3c52710287777a17110dad6b9d720 (diff)
downloadtincapp-483e6634e0621d2100ae11cbcd8cba6d21a76c4e.tar.gz
Refactor log viewer activity
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/activities/viewlog')
-rw-r--r--app/src/main/java/org/pacien/tincapp/activities/viewlog/LogLiveData.kt74
-rw-r--r--app/src/main/java/org/pacien/tincapp/activities/viewlog/LogViewModel.kt33
-rw-r--r--app/src/main/java/org/pacien/tincapp/activities/viewlog/ViewLogActivity.kt125
3 files changed, 232 insertions, 0 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/activities/viewlog/LogLiveData.kt b/app/src/main/java/org/pacien/tincapp/activities/viewlog/LogLiveData.kt
new file mode 100644
index 0000000..f410a8c
--- /dev/null
+++ b/app/src/main/java/org/pacien/tincapp/activities/viewlog/LogLiveData.kt
@@ -0,0 +1,74 @@
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.viewlog
20
21import android.arch.lifecycle.LiveData
22import org.pacien.tincapp.commands.Executor
23import org.pacien.tincapp.commands.Tinc
24import java.util.*
25import kotlin.concurrent.timer
26
27/**
28 * @author pacien
29 */
30class LogLiveData(private val netName: String, private val logLevel: Int, private val logLineSize: Int) : LiveData<List<String>>() {
31 private val updateInterval = 250L // milliseconds
32 private val executor = Executor
33 private val log = LinkedList<String>()
34 private var loggerProcess: Process? = null
35 private var logUpdateTimer: Timer? = null
36
37 override fun onActive() {
38 loggerProcess = startNewLogger()
39 logUpdateTimer = timer(period = updateInterval, action = { outputLog() })
40 }
41
42 override fun onInactive() {
43 loggerProcess?.destroy()
44 logUpdateTimer?.apply { cancel() }?.apply { purge() }
45 }
46
47 private fun startNewLogger(): Process {
48 val newProcess = Tinc.log(netName, logLevel)
49 executor.runAsyncTask { captureProcessOutput(newProcess) }
50 return newProcess
51 }
52
53 private fun captureProcessOutput(process: Process) {
54 process.inputStream?.use { inputStream ->
55 inputStream.bufferedReader().useLines { lines ->
56 lines.forEach { appendToLog(it) }
57 }
58 }
59 }
60
61 private fun appendToLog(line: String) {
62 synchronized(log) {
63 if (log.size >= logLineSize) log.removeFirst()
64 log.addLast(line)
65 }
66 }
67
68 private fun outputLog() {
69 synchronized(log) {
70 val logView = ArrayList(log)
71 postValue(logView)
72 }
73 }
74}
diff --git a/app/src/main/java/org/pacien/tincapp/activities/viewlog/LogViewModel.kt b/app/src/main/java/org/pacien/tincapp/activities/viewlog/LogViewModel.kt
new file mode 100644
index 0000000..d9f0017
--- /dev/null
+++ b/app/src/main/java/org/pacien/tincapp/activities/viewlog/LogViewModel.kt
@@ -0,0 +1,33 @@
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.viewlog
20
21import android.arch.lifecycle.ViewModel
22import org.pacien.tincapp.service.TincVpnService
23
24/**
25 * @author pacien
26 */
27class LogViewModel : ViewModel() {
28 private val logLevelNumeric = 5
29 val logLevelText = "DEBUG"
30 val netName by lazy { TincVpnService.getCurrentNetName()!! }
31 val log by lazy { LogLiveData(netName, logLevelNumeric, 250) }
32 var logging = true
33}
diff --git a/app/src/main/java/org/pacien/tincapp/activities/viewlog/ViewLogActivity.kt b/app/src/main/java/org/pacien/tincapp/activities/viewlog/ViewLogActivity.kt
new file mode 100644
index 0000000..a4e2216
--- /dev/null
+++ b/app/src/main/java/org/pacien/tincapp/activities/viewlog/ViewLogActivity.kt
@@ -0,0 +1,125 @@
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.viewlog
20
21import android.arch.lifecycle.Observer
22import android.arch.lifecycle.ViewModelProviders
23import android.content.Intent
24import android.os.Bundle
25import android.view.Menu
26import android.view.MenuItem
27import android.view.View
28import android.widget.ScrollView
29import kotlinx.android.synthetic.main.base.*
30import kotlinx.android.synthetic.main.view_log_activity.*
31import org.pacien.tincapp.R
32import org.pacien.tincapp.activities.BaseActivity
33
34/**
35 * @author pacien
36 */
37class ViewLogActivity : BaseActivity() {
38 private val viewModel by lazy { ViewModelProviders.of(this).get(LogViewModel::class.java) }
39 private val logObserver: Observer<List<String>> = Observer { showLog(it) }
40 private var toggleButton: MenuItem? = null
41
42 override fun onCreate(savedInstanceState: Bundle?) {
43 super.onCreate(savedInstanceState)
44 supportActionBar.setDisplayHomeAsUpEnabled(true)
45 layoutInflater.inflate(R.layout.view_log_activity, main_content)
46 enableLogging(viewModel.logging)
47 }
48
49 override fun onCreateOptionsMenu(m: Menu): Boolean {
50 menuInflater.inflate(R.menu.menu_viewlog, m)
51 toggleButton = m.findItem(R.id.log_viewer_action_toggle)
52 return super.onCreateOptionsMenu(m)
53 }
54
55 override fun onSupportNavigateUp(): Boolean {
56 finish()
57 return true
58 }
59
60 @Suppress("UNUSED_PARAMETER")
61 fun shareLog(m: MenuItem) {
62 val logSnippet = viewModel.log.value?.joinToString("\n")
63 val shareIntent = Intent(Intent.ACTION_SEND)
64 .setType("text/plain")
65 .putExtra(Intent.EXTRA_TEXT, logSnippet)
66
67 startActivityChooser(shareIntent, resources.getString(R.string.log_view_menu_share_log))
68 }
69
70 @Suppress("UNUSED_PARAMETER")
71 fun toggleLogging(m: MenuItem) =
72 enableLogging(!viewModel.logging)
73
74 private fun enableLogging(enable: Boolean) {
75 setLoggingStateSubtitle(enable)
76 setPauseButtonState(!enable)
77 enableScrolling(!enable)
78 viewModel.logging = enable
79
80 if (enable)
81 viewModel.log.observe(this, logObserver)
82 else
83 viewModel.log.removeObservers(this)