From b24f4c471782b990c3617705ca575e42d129522e Mon Sep 17 00:00:00 2001 From: Adrian Tiberius Date: Thu, 25 Jun 2015 05:23:28 +0200 Subject: [PATCH] Fix ui thread freeze by adding 1 second timer. --- .../syng/EthereumService.java | 6 +-- .../blockchainsociety/syng/MainActivity.java | 38 +++++++++++++++++-- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/io/blockchainsociety/syng/EthereumService.java b/app/src/main/java/io/blockchainsociety/syng/EthereumService.java index b188daf..c18153c 100644 --- a/app/src/main/java/io/blockchainsociety/syng/EthereumService.java +++ b/app/src/main/java/io/blockchainsociety/syng/EthereumService.java @@ -17,7 +17,7 @@ public class EthereumService extends EthereumAidlService { @Override protected void broadcastMessage(String message) { - updateLog(message); + //updateLog(message); for (IListener listener: clientListeners) { try { listener.trace(message); @@ -32,8 +32,8 @@ public class EthereumService extends EthereumAidlService { EthereumService.log += message; int logLength = EthereumService.log.length(); - if (logLength > 100000) { - EthereumService.log = EthereumService.log.substring(50000); + if (logLength > 5000) { + EthereumService.log = EthereumService.log.substring(2000); } } diff --git a/app/src/main/java/io/blockchainsociety/syng/MainActivity.java b/app/src/main/java/io/blockchainsociety/syng/MainActivity.java index 3d209b1..8a664da 100644 --- a/app/src/main/java/io/blockchainsociety/syng/MainActivity.java +++ b/app/src/main/java/io/blockchainsociety/syng/MainActivity.java @@ -17,12 +17,14 @@ import android.view.WindowManager; import android.widget.TextView; import android.widget.Toast; -import org.ethereum.android.EthereumAidlService; import org.ethereum.android.interop.IAsyncCallback; import org.ethereum.android.interop.IEthereumService; import org.ethereum.android.interop.IListener; import org.ethereum.config.SystemProperties; +import java.util.Timer; +import java.util.TimerTask; + public class MainActivity extends BaseActivity { @@ -38,6 +40,12 @@ public class MainActivity extends BaseActivity { boolean isPaused = false; + private Timer timer; + private TimerTask timerTask; + + private static int CONSOLE_LENGTH = 10000; + private static int CONSOLE_REFRESH = 1000; + TextViewUpdater consoleUpdater = new TextViewUpdater(); private class TextViewUpdater implements Runnable { @@ -105,7 +113,7 @@ public class MainActivity extends BaseActivity { public void trace(String message) throws RemoteException { - logMessage(message); + MainActivity.consoleLog += message + "\n" + "\n"; } }; @@ -113,8 +121,8 @@ public class MainActivity extends BaseActivity { MainActivity.consoleLog += message + "\n"; int consoleLength = MainActivity.consoleLog.length(); - if (consoleLength > 15000) { - MainActivity.consoleLog = MainActivity.consoleLog.substring(consoleLength - 15000); + if (consoleLength > 5000) { + MainActivity.consoleLog = MainActivity.consoleLog.substring(2000); } consoleUpdater.setText(MainActivity.consoleLog); @@ -176,6 +184,7 @@ public class MainActivity extends BaseActivity { super.onPause(); isPaused = true; + timer.cancel(); } @Override @@ -183,6 +192,27 @@ public class MainActivity extends BaseActivity { super.onResume(); isPaused = false; + try { + timer = new Timer(); + timerTask = new TimerTask() { + @Override + public void run() { + MainActivity.this.runOnUiThread(new Runnable() { + public void run() { + int length = MainActivity.consoleLog.length(); + if (length > CONSOLE_LENGTH) { + MainActivity.consoleLog = MainActivity.consoleLog.substring(CONSOLE_LENGTH * ((length/CONSOLE_LENGTH)-1) + length%CONSOLE_LENGTH); + } + consoleUpdater.setText(MainActivity.consoleLog); + MainActivity.this.consoleText.post(consoleUpdater); + } + }); + } + }; + timer.schedule(timerTask, 1000, CONSOLE_REFRESH); + } catch (IllegalStateException e){ + android.util.Log.i("Damn", "resume error"); + } } @Override