LogViewerActivity: only scroll every quarter for the first 2.5 seconds of dumping

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2020-03-28 23:05:38 -06:00
parent 03a838ba2d
commit 574ee5d0bb

View File

@ -204,6 +204,9 @@ class LogViewerActivity : AppCompatActivity() {
return@withContext return@withContext
} }
val stdout = BufferedReader(InputStreamReader(process!!.inputStream, StandardCharsets.UTF_8)) val stdout = BufferedReader(InputStreamReader(process!!.inputStream, StandardCharsets.UTF_8))
var haveScrolled = false
val start = System.nanoTime()
var startPeriod = start
while (true) { while (true) {
val line = stdout.readLine() ?: break val line = stdout.readLine() ?: break
rawLogLines.append(line) rawLogLines.append(line)
@ -212,10 +215,10 @@ class LogViewerActivity : AppCompatActivity() {
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
if (logLine != null) { if (logLine != null) {
recyclerView?.let { recyclerView?.let {
val shouldScroll = it.canScrollVertically(1) val shouldScroll = haveScrolled && !it.canScrollVertically(1)
logLines.add(logLine) logLines.add(logLine)
logAdapter.notifyDataSetChanged() if (haveScrolled) logAdapter.notifyDataSetChanged()
if (!shouldScroll) if (shouldScroll)
it.scrollToPosition(logLines.size - 1) it.scrollToPosition(logLines.size - 1)
} }
} else { } else {
@ -224,7 +227,17 @@ class LogViewerActivity : AppCompatActivity() {
* However, as of writing, that causes the kotlin compiler to freak out and crash, spewing bytecode. * However, as of writing, that causes the kotlin compiler to freak out and crash, spewing bytecode.
*/ */
logLines.lastOrNull()?.apply { msg += "\n$line" } logLines.lastOrNull()?.apply { msg += "\n$line" }
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() logAdapter.notifyDataSetChanged()
recyclerView?.scrollToPosition(logLines.size - 1)
startPeriod = end
}
if (scroll) haveScrolled = true
} }
} }
} }