Android Open Source - AGOGCyberStat Text View Appender






From Project

Back to project page AGOGCyberStat.

License

The source code is released under:

MIT License

If you think the Android project AGOGCyberStat 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  www  . ja  v a  2 s  .  c om
import java.lang.ref.WeakReference;
import java.util.concurrent.TimeUnit;

import android.content.Context;
import android.os.Handler;
import android.widget.TextView;

public class TextViewAppender extends AsyncAppender {

  private static final int MAX_LOG_TEXT_LENGHT_IN_VIEW = 50000;

  private static final long MIN_PRINT_PERIOD = 500;

  private WeakReference<TextView> textViewRef;

  private Handler mainHandler;

  private static final StringBuilder logCache = new StringBuilder();

  private long lastUpdateMillis;

  static int sUpdateCount;
  private int mUpdateCount;

  public TextViewAppender(){
    super();
  }

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

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

  private void postLogToUIThread(final String 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.setText(log);
          }
        }
      }

    });
  }

  /* Keep a ring buffer of old events for a newly displayed textview */
  public static void cacheEvent(LogEvent event)
  {
    logCache.append(event.toString());    
    while (logCache.length() > MAX_LOG_TEXT_LENGHT_IN_VIEW) {
      final int index = logCache.indexOf("\n");
      logCache.delete(0, index + 1);
    }
    sUpdateCount++;
  }

  @Override
  protected void handleEventQueue() throws InterruptedException {
    @SuppressWarnings("unused")
    final LogEvent event = logEventQueue.poll(MIN_PRINT_PERIOD, TimeUnit.MILLISECONDS);
   if (sUpdateCount != mUpdateCount && textViewRef != null) {
      final TextView textView = textViewRef.get();
      mUpdateCount = sUpdateCount;
      if (textView != null) {
        final long currentMillis = System.currentTimeMillis();
        if ((currentMillis -
            lastUpdateMillis > MIN_PRINT_PERIOD ||
            lastUpdateMillis == 0) &&
            logCache.length() > 0) {
          postLogToUIThread(logCache.toString());
          lastUpdateMillis = currentMillis;
        }
      }
    }

  }

}




Java Source Code List

com.agog.cyberstat.BrR.java
com.agog.cyberstat.JSONSettings.java
com.agog.cyberstat.MainActivity.java
com.agog.cyberstat.MotisonNetTask.java
com.agog.cyberstat.MotisonXML.java
com.agog.cyberstat.MyPrefs.java
com.agog.cyberstat.NetR.java
com.agog.cyberstat.Trigger.java
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