diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2020-03-28 23:05:38 -0600 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2020-03-28 23:07:50 -0600 |
commit | 574ee5d0bba189e596f655be992b7226e85d75b4 (patch) | |
tree | 540ac5c4679b364732094551296f359607d8f326 /ui/src/main/java/com | |
parent | 03a838ba2d81af6dc1ce34cb8e3ac64cc2edc69e (diff) |
LogViewerActivity: only scroll every quarter for the first 2.5 seconds of dumping
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'ui/src/main/java/com')
-rw-r--r-- | ui/src/main/java/com/wireguard/android/activity/LogViewerActivity.kt | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/ui/src/main/java/com/wireguard/android/activity/LogViewerActivity.kt b/ui/src/main/java/com/wireguard/android/activity/LogViewerActivity.kt index 78efb152..d95913ed 100644 --- a/ui/src/main/java/com/wireguard/android/activity/LogViewerActivity.kt +++ b/ui/src/main/java/com/wireguard/android/activity/LogViewerActivity.kt @@ -204,6 +204,9 @@ class LogViewerActivity : AppCompatActivity() { return@withContext } val stdout = BufferedReader(InputStreamReader(process!!.inputStream, StandardCharsets.UTF_8)) + var haveScrolled = false + val start = System.nanoTime() + var startPeriod = start while (true) { val line = stdout.readLine() ?: break rawLogLines.append(line) @@ -212,10 +215,10 @@ class LogViewerActivity : AppCompatActivity() { withContext(Dispatchers.Main) { if (logLine != null) { recyclerView?.let { - val shouldScroll = it.canScrollVertically(1) + val shouldScroll = haveScrolled && !it.canScrollVertically(1) logLines.add(logLine) - logAdapter.notifyDataSetChanged() - if (!shouldScroll) + if (haveScrolled) logAdapter.notifyDataSetChanged() + if (shouldScroll) it.scrollToPosition(logLines.size - 1) } } else { @@ -224,7 +227,17 @@ class LogViewerActivity : AppCompatActivity() { * However, as of writing, that causes the kotlin compiler to freak out and crash, spewing bytecode. */ logLines.lastOrNull()?.apply { msg += "\n$line" } - logAdapter.notifyDataSetChanged() + if (haveScrolled) logAdapter.notifyDataSetChanged() + } + if (!haveScrolled) { + val end = System.nanoTime() + val scroll = (end - start) > 1000000000L * 2.5 || !stdout.ready() + if (logLines.isNotEmpty() && (scroll || (end - startPeriod) > 1000000000L / 4)) { + logAdapter.notifyDataSetChanged() + recyclerView?.scrollToPosition(logLines.size - 1) + startPeriod = end + } + if (scroll) haveScrolled = true } } } |