Back to project page AGOGCyberStat.
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.
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; } } } } }