From bc82ef294e845f94d671c07a15c1e2893215b166 Mon Sep 17 00:00:00 2001 From: Pawel Polanski Date: Mon, 8 Dec 2014 18:29:37 +0100 Subject: [PATCH] Do not clear the log when buffer is filled Now output behave as a normal console output --- .../java/com/inaka/galgo/GalgoService.java | 29 ++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/inaka/galgo/GalgoService.java b/src/main/java/com/inaka/galgo/GalgoService.java index 78be072..b8e3861 100644 --- a/src/main/java/com/inaka/galgo/GalgoService.java +++ b/src/main/java/com/inaka/galgo/GalgoService.java @@ -24,15 +24,21 @@ import android.os.IBinder; import android.text.Spannable; import android.text.SpannableString; +import android.text.TextUtils; import android.text.style.BackgroundColorSpan; import android.view.WindowManager; import android.widget.TextView; +import java.util.ArrayDeque; +import java.util.Collection; +import java.util.Queue; + public class GalgoService extends Service { private final IBinder mBinder = new LocalBinder(); private TextView mTextView; private GalgoOptions mOptions; + private final Queue mLines = new ArrayDeque<>(); public class LocalBinder extends Binder { public GalgoService getService() { @@ -61,20 +67,23 @@ public void onCreate() { } public void displayText(String text) { - - Spannable spannable = new SpannableString(text); - spannable.setSpan(new BackgroundColorSpan(mOptions.backgroundColor),0, text.length(), - Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); - - if(mTextView.getLineCount() > mOptions.numberOfLines) { - mTextView.setText(spannable); - } else { - mTextView.append(spannable); + mLines.add(text); + if (mLines.size() > mOptions.numberOfLines) { + mLines.poll(); } + redraw(mLines); + } + + private void redraw(Collection texts) { mTextView.setTextSize(mOptions.textSize); mTextView.setTextColor(mOptions.textColor); - mTextView.append("\n"); + + Spannable spannable = new SpannableString(TextUtils.join("\n", texts)); + spannable.setSpan(new BackgroundColorSpan(mOptions.backgroundColor), 0, spannable.length(), + Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); + + mTextView.setText(spannable); } @Override