Android Open Source - AndroidAppLog Text View Appender






From Project

Back to project page AndroidAppLog.

License

The source code is released under:

Apache License

If you think the Android project AndroidAppLog listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package me.allenz.androidapplog;
//from  w w w.j a  v a 2  s  . c o m
import java.lang.ref.WeakReference;
import java.util.concurrent.TimeUnit;

import android.content.Context;
import android.os.Handler;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.SpannedString;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;

public class TextViewAppender extends AsyncAppender {

  private static final int MAX_LOG_TEXT_LENGHT_IN_VIEW = 5000;

  private static final long MIN_PRINT_PERIOD = 200;

  private WeakReference<TextView> textViewRef;

  private Handler mainHandler;

  private SpannableStringBuilder logBuilder;

  private long lastUpdateMillis;

  public TextViewAppender() {
    super();
    logBuilder = new SpannableStringBuilder();
  }

  public void bind(final TextView textView) {
    if (textView == null) {
      return;
    }
    textViewRef = new WeakReference<TextView>(textView);
  }

  public void unbind() {
    textViewRef = null;
    lastUpdateMillis = 0;
    logBuilder.clearSpans();
    logBuilder.clear();
    logEventQueue.clear();
  }

  private void postLogToUIThread(final CharSequence log) {
    if (mainHandler == null) {
      final Context context = LoggerFactory.getContext();
      if (context == null) {
        return;
      } else {
        mainHandler = new Handler(context.getMainLooper());
      }
    }
    mainHandler.post(new Runnable() {

      @Override
      public void run() {
        if (textViewRef != null) {
          final TextView textView = textViewRef.get();
          if (textView != null) {
            textView.append(log);
          }
        }
      }

    });
  }

  @Override
  protected void handleEventQueue() throws InterruptedException {
    final LogEvent event = logEventQueue.poll(MIN_PRINT_PERIOD,
        TimeUnit.MILLISECONDS);
    if (textViewRef != null) {
      final TextView textView = textViewRef.get();
      if (textView != null) {
        final long currentMillis = System.currentTimeMillis();
        if (event != null) {
          final int start = logBuilder.length();
          logBuilder.append(event.toString());
          logBuilder.setSpan(new ForegroundColorSpan(event.getLevel()
              .getColor()), start, logBuilder.length(),
              Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
          logBuilder.append("\n");
        }
        if ((currentMillis - lastUpdateMillis > MIN_PRINT_PERIOD || lastUpdateMillis == 0)
            && logBuilder.length() > 0) {
          postLogToUIThread(new SpannedString(logBuilder));
          lastUpdateMillis = currentMillis;
          logBuilder.clearSpans();
          logBuilder.clear();
        }
      }
    }

  }

}




Java Source Code List

me.allenz.androidapplog.AbstractAppender.java
me.allenz.androidapplog.AbstractLogger.java
me.allenz.androidapplog.AppenderSupportLogger.java
me.allenz.androidapplog.Appender.java
me.allenz.androidapplog.AsyncAppender.java
me.allenz.androidapplog.Configure.java
me.allenz.androidapplog.InternalLogger.java
me.allenz.androidapplog.LogCatAppender.java
me.allenz.androidapplog.LogEvent.java
me.allenz.androidapplog.LogLevel.java
me.allenz.androidapplog.LogTextView.java
me.allenz.androidapplog.LoggerConfig.java
me.allenz.androidapplog.LoggerFactory.java
me.allenz.androidapplog.Logger.java
me.allenz.androidapplog.PropertiesParser.java
me.allenz.androidapplog.ReflectUtils.java
me.allenz.androidapplog.Repository.java
me.allenz.androidapplog.RollingFileAppender.java
me.allenz.androidapplog.TextViewAppender.java
me.allenz.androidapplog.UncaughtExceptionLogger.java