aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java
diff options
context:
space:
mode:
authorpacien2018-03-04 19:24:28 +0100
committerpacien2018-03-04 19:24:28 +0100
commit1f30a8838d787f31dbc0af83458d73afef104927 (patch)
tree7615b1f28efe173d1831c61dc4cf37390dbf9932 /app/src/main/java
parent44d00258bf0c7786ec6db9c19809db126e6c8172 (diff)
downloadtincapp-1f30a8838d787f31dbc0af83458d73afef104927.tar.gz
Refactor log viewer, enhance scrolling
Diffstat (limited to 'app/src/main/java')
-rw-r--r--app/src/main/java/org/pacien/tincapp/activities/ViewLogActivity.kt44
1 files changed, 31 insertions, 13 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/activities/ViewLogActivity.kt b/app/src/main/java/org/pacien/tincapp/activities/ViewLogActivity.kt
index ebdd22a..673dedb 100644
--- a/app/src/main/java/org/pacien/tincapp/activities/ViewLogActivity.kt
+++ b/app/src/main/java/org/pacien/tincapp/activities/ViewLogActivity.kt
@@ -1,9 +1,10 @@
1package org.pacien.tincapp.activities 1package org.pacien.tincapp.activities
2 2
3import android.os.Bundle 3import android.os.Bundle
4import android.text.method.ScrollingMovementMethod
5import android.view.Menu 4import android.view.Menu
6import android.view.MenuItem 5import android.view.MenuItem
6import android.view.View
7import android.widget.ScrollView
7import kotlinx.android.synthetic.main.base.* 8import kotlinx.android.synthetic.main.base.*
8import kotlinx.android.synthetic.main.page_viewlog.* 9import kotlinx.android.synthetic.main.page_viewlog.*
9import org.pacien.tincapp.R 10import org.pacien.tincapp.R
@@ -20,7 +21,7 @@ class ViewLogActivity : BaseActivity() {
20 companion object { 21 companion object {
21 private const val LOG_LINES = 250 22 private const val LOG_LINES = 250
22 private const val LOG_LEVEL = 5 23 private const val LOG_LEVEL = 5
23 private const val NEW_LINE = "\n" 24 private const val NEW_LINE = "\n\n"
24 private const val UPDATE_INTERVAL = 250L // ms 25 private const val UPDATE_INTERVAL = 250L // ms
25 } 26 }
26 27
@@ -53,37 +54,35 @@ class ViewLogActivity : BaseActivity() {
53 fun toggleLogging(menuItem: MenuItem) { 54 fun toggleLogging(menuItem: MenuItem) {
54 if (logger == null) { 55 if (logger == null) {
55 startLogging() 56 startLogging()
56 text_log.movementMethod = null
57 text_log.setTextIsSelectable(false)
58 menuItem.setIcon(R.drawable.ic_pause_circle_outline_primary_24dp) 57 menuItem.setIcon(R.drawable.ic_pause_circle_outline_primary_24dp)
59 } else { 58 } else {
60 stopLogging() 59 stopLogging()
61 text_log.movementMethod = ScrollingMovementMethod.getInstance()
62 text_log.setTextIsSelectable(true)
63 menuItem.setIcon(R.drawable.ic_pause_circle_filled_primary_24dp) 60 menuItem.setIcon(R.drawable.ic_pause_circle_filled_primary_24dp)
64 } 61 }
65 } 62 }
66 63
67 private fun startLogging(level: Int = LOG_LEVEL) { 64 private fun startLogging(level: Int = LOG_LEVEL) {
65 disableUserScroll()
68 appendLog(resources.getString(R.string.message_log_level_set, level)) 66 appendLog(resources.getString(R.string.message_log_level_set, level))
69 Tinc.log(TincVpnService.getCurrentNetName()!!, level).let { process -> 67 Tinc.log(TincVpnService.getCurrentNetName()!!, level).let { process ->
70 logger = process 68 logger = process
71 Executor.runAsyncTask { printLog(process) } 69 Executor.runAsyncTask { captureLog(process) }
72 } 70 }
73 logUpdateTimer = timer(period = UPDATE_INTERVAL, action = { updateLog() }) 71 logUpdateTimer = timer(period = UPDATE_INTERVAL, action = { printLog() })
74 } 72 }
75 73
76 private fun stopLogging() { 74 private fun stopLogging() {
75 enableUserScroll()
77 logger?.destroy() 76 logger?.destroy()
78 logger = null 77 logger = null
79 logUpdateTimer?.cancel() 78 logUpdateTimer?.cancel()
80 logUpdateTimer?.purge() 79 logUpdateTimer?.purge()
81 logUpdateTimer = null 80 logUpdateTimer = null
82 appendLog(resources.getString(R.string.message_log_paused)) 81 appendLog(resources.getString(R.string.message_log_paused))
83 updateLog() 82 printLog()
84 } 83 }
85 84
86 private fun printLog(logger: Process) { 85 private fun captureLog(logger: Process) {
87 logger.inputStream?.use { inputStream -> 86 logger.inputStream?.use { inputStream ->
88 inputStream.bufferedReader().useLines { lines -> 87 inputStream.bufferedReader().useLines { lines ->
89 lines.forEach { appendLog(it) } 88 lines.forEach { appendLog(it) }
@@ -96,9 +95,28 @@ class ViewLogActivity : BaseActivity() {
96 log.addLast(line) 95 log.addLast(line)
97 } 96 }
98 97
99 private fun updateLog() = synchronized(this) { 98 private fun printLog() = synchronized(this) {
100 log.joinToString(NEW_LINE + NEW_LINE, NEW_LINE, NEW_LINE).let { 99 log.joinToString(NEW_LINE).let {
101 text_log.post { text_log.text = it } 100 logview_text.post {
101 logview_text.text = it
102 logview_frame.post { logview_frame.fullScroll(View.FOCUS_DOWN) }
103 }
102 } 104 }
103 } 105 }
106
107 private fun enableUserScroll() {
108 logview_text.setTextIsSelectable(true)
109 logview_frame.setState(true)
110 }
111
112 private fun disableUserScroll() {
113 logview_text.setTextIsSelectable(false)
114 logview_frame.setState(false)
115 }
116
117 private fun ScrollView.setState(enabled: Boolean) {
118 if (enabled) setOnTouchListener(null) else setOnTouchListener { _, _ -> true }
119 logview_frame.isSmoothScrollingEnabled = enabled
120 logview_frame.isVerticalScrollBarEnabled = enabled
121 }
104} 122}