android: Make log view more efficient

This bunches several log messages together before posting Runnables.

Fixes #2148.
This commit is contained in:
Tobias Brunner 2017-04-21 14:30:16 +02:00
parent 9c4607b454
commit 74d44e15dc

View File

@ -1,6 +1,6 @@
/* /*
* Copyright (C) 2012 Tobias Brunner * Copyright (C) 2012-2017 Tobias Brunner
* Hochschule fuer Technik Rapperswil * HSR Hochschule fuer Technik Rapperswil
* *
* This program is free software; you can redistribute it and/or modify it * This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the * under the terms of the GNU General Public License as published by the
@ -32,6 +32,7 @@ import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileReader; import java.io.FileReader;
import java.io.StringReader; import java.io.StringReader;
import java.util.ArrayList;
public class LogFragment extends Fragment implements Runnable public class LogFragment extends Fragment implements Runnable
{ {
@ -121,16 +122,20 @@ public class LogFragment extends Fragment implements Runnable
* Write the given log line to the TextView. We strip the prefix off to save * Write the given log line to the TextView. We strip the prefix off to save
* some space (it is not that helpful for regular users anyway). * some space (it is not that helpful for regular users anyway).
* *
* @param line log line to log * @param lines log lines to log
*/ */
public void logLine(final String line) public void logLines(final ArrayList<String> lines)
{ {
mLogHandler.post(new Runnable() { mLogHandler.post(new Runnable() {
@Override @Override
public void run() public void run()
{ {
/* strip off prefix (month=3, day=2, time=8, thread=2, spaces=3) */ mLogView.beginBatchEdit();
for (String line : lines)
{ /* strip off prefix (month=3, day=2, time=8, thread=2, spaces=3) */
mLogView.append((line.length() > 18 ? line.substring(18) : line) + '\n'); mLogView.append((line.length() > 18 ? line.substring(18) : line) + '\n');
}
mLogView.endBatchEdit();
/* calling autoScroll() directly does not work, probably because content /* calling autoScroll() directly does not work, probably because content
* is not yet updated, so we post this to be done later */ * is not yet updated, so we post this to be done later */
mScrollView.post(new Runnable() { mScrollView.post(new Runnable() {
@ -147,18 +152,30 @@ public class LogFragment extends Fragment implements Runnable
@Override @Override
public void run() public void run()
{ {
ArrayList<String> lines = null;
while (mRunning) while (mRunning)
{ {
try try
{ /* this works as long as the file is not truncated */ { /* this works as long as the file is not truncated */
String line = mReader.readLine(); String line = mReader.readLine();
if (line == null) if (line == null)
{ /* wait until there is more to log */ {
if (lines != null)
{
logLines(lines);
lines = null;
}
/* wait until there is more to log */
Thread.sleep(1000); Thread.sleep(1000);
} }
else else
{ {
logLine(line); if (lines == null)
{
lines = new ArrayList<>();
}
lines.add(line);
} }
} }
catch (Exception e) catch (Exception e)
@ -166,6 +183,10 @@ public class LogFragment extends Fragment implements Runnable
break; break;
} }
} }
if (lines != null)
{
logLines(lines);
}
} }
/** /**