diff options
Diffstat (limited to 'app/src/main')
-rw-r--r-- | app/src/main/java/org/pacien/tincapp/activities/ViewLogActivity.kt | 44 | ||||
-rw-r--r-- | app/src/main/res/layout/page_viewlog.xml | 27 |
2 files changed, 47 insertions, 24 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 @@ | |||
1 | package org.pacien.tincapp.activities | 1 | package org.pacien.tincapp.activities |
2 | 2 | ||
3 | import android.os.Bundle | 3 | import android.os.Bundle |
4 | import android.text.method.ScrollingMovementMethod | ||
5 | import android.view.Menu | 4 | import android.view.Menu |
6 | import android.view.MenuItem | 5 | import android.view.MenuItem |
6 | import android.view.View | ||
7 | import android.widget.ScrollView | ||
7 | import kotlinx.android.synthetic.main.base.* | 8 | import kotlinx.android.synthetic.main.base.* |
8 | import kotlinx.android.synthetic.main.page_viewlog.* | 9 | import kotlinx.android.synthetic.main.page_viewlog.* |
9 | import org.pacien.tincapp.R | 10 | import 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 | } |
diff --git a/app/src/main/res/layout/page_viewlog.xml b/app/src/main/res/layout/page_viewlog.xml index a7afed8..0795554 100644 --- a/app/src/main/res/layout/page_viewlog.xml +++ b/app/src/main/res/layout/page_viewlog.xml | |||
@@ -1,13 +1,18 @@ | |||
1 | <TextView | 1 | <ScrollView |
2 | xmlns:android="http://schemas.android.com/apk/res/android" | 2 | xmlns:android="http://schemas.android.com/apk/res/android" |
3 | android:id="@+id/text_log" | 3 | android:id="@+id/logview_frame" |
4 | android:layout_width="match_parent" | 4 | android:layout_width="match_parent" |
5 | android:layout_height="wrap_content" | 5 | android:layout_height="match_parent"> |
6 | android:fontFamily="monospace" | 6 | |
7 | android:gravity="bottom" | 7 | <TextView |
8 | android:paddingLeft="@dimen/activity_horizontal_margin" | 8 | android:id="@+id/logview_text" |
9 | android:paddingRight="@dimen/activity_horizontal_margin" | 9 | android:layout_width="match_parent" |
10 | android:scrollbarStyle="outsideOverlay" | 10 | android:layout_height="wrap_content" |
11 | android:scrollbars="vertical" | 11 | android:fontFamily="monospace" |
12 | android:textColor="@color/colorPrimary" | 12 | android:paddingBottom="@dimen/activity_vertical_margin" |
13 | android:textSize="12sp"/> | 13 | android:paddingLeft="@dimen/activity_horizontal_margin" |
14 | android:paddingRight="@dimen/activity_horizontal_margin" | ||
15 | android:paddingTop="@dimen/activity_vertical_margin" | ||
16 | android:textColor="@color/colorPrimary" | ||
17 | android:textSize="12sp"/> | ||
18 | </ScrollView> | ||