package com.vandalsoftware.android.spdyexample;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;

public class LogView extends View {
    private int mY;
    private String mMessage;
    private final TextPaint mPaint;
    private Bitmap mBitmap;
    private Canvas mBmCanvas;
    private boolean mClear;
    private int mBottom;

    public LogView(Context context) {
        this(context, null);
    }

    public LogView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LogView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        mClear = true;
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        int h = getMeasuredHeight() - getPaddingBottom() - getPaddingTop();
        mBitmap = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(),
                Bitmap.Config.ARGB_8888);
        mBmCanvas = new Canvas(mBitmap);
        mBottom = h;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Canvas bc = mBmCanvas;
        if (mClear) {
            Paint.Style oldStyle = mPaint.getStyle();
            mPaint.setStyle(Paint.Style.FILL);
            mPaint.setColor(Color.WHITE);
            bc.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), mPaint);
            mPaint.setStyle(oldStyle);
            mClear = false;
        }
        if (mMessage != null) {
            mPaint.setColor(Color.BLACK);
            bc.drawText(mMessage, getPaddingLeft(), getPaddingTop() + mY, mPaint);
            mMessage = null;
        }
        canvas.drawBitmap(mBitmap, 0, 0, mPaint);
    }

    public void log(String message) {
        final Paint.FontMetricsInt metrics = mPaint.getFontMetricsInt();
        int fh = metrics.descent - metrics.ascent;
        if (mY >= mBottom) {
            mY = fh;
            mClear = true;
        } else {
            mY += fh;
            mClear = false;
        }
        mMessage = message;
        invalidate();
    }
}

@@ -196,24 +196,50 @@
 hics.Paint;%0A
+import android.os.Looper;%0A
 import andro
@@ -318,16 +318,45 @@
 .View;%0A%0A
+import java.util.ArrayList;%0A%0A
 public c
@@ -419,23 +419,70 @@
 ate 
+ArrayList%3C
 String
+%3E
  m
-Message
+PendingMessages = new ArrayList%3CString%3E(20)
 ;%0A  
@@ -589,22 +589,19 @@
 ate 
-boolean mClear
+int mBottom
 ;%0A  
@@ -615,22 +615,26 @@
 te int m
-Bottom
+FontHeight
 ;%0A%0A    p
@@ -968,39 +968,16 @@
 _FLAG);%0A
-        mClear = true;%0A
     %7D%0A%0A 
@@ -1395,16 +1395,146 @@
 tom = h;
+%0A%0A        final Paint.FontMetricsInt metrics = mPaint.getFontMetricsInt();%0A        mFontHeight = metrics.descent - metrics.ascent;
 %0A    %7D%0A%0A
@@ -1647,24 +1647,116 @@
  mBmCanvas;%0A
+        boolean clear = mY + (mPendingMessages.size() * mFontHeight) %3E= mBottom %7C%7C mY == 0;%0A
         if (
@@ -1759,18 +1759,17 @@
 if (
-mC
+c
 lear) %7B%0A
     
@@ -1760,24 +1760,54 @@
 f (clear) %7B%0A
+            mY = mFontHeight;%0A
             
@@ -1933,13 +1933,13 @@
 lor.
-WHITE
+BLACK
 );%0A 
@@ -2068,66 +2068,49 @@
     
-    mClear = false;%0A        %7D%0A        if (mMessage != null
+%7D%0A        if (!mPendingMessages.isEmpty()
 ) %7B%0A
@@ -2139,32 +2139,90 @@
 Color(Color.
-BLACK
+WHITE
 );%0A
+            for (String message : mPendingMessages) %7B%0A    
             
@@ -2234,17 +2234,16 @@
 awText(m
-M
 essage, 
@@ -2307,23 +2307,81 @@
     
-mMessage = null
+    mY += mFontHeight;%0A            %7D%0A            mPendingMessages.clear()
 ;%0A  
@@ -2495,266 +2495,155 @@
     
-final Paint.FontMetricsInt metrics = mPaint.getFontMetricsInt();%0A        int fh = metrics.descent - metrics.ascent;%0A        if (mY %3E= mBottom) %7B%0A            mY = fh;%0A            mClear = true;%0A        %7D else %7B%0A            mY += fh;%0A            mClear = false
+if (Thread.currentThread() != Looper.getMainLooper().getThread()) %7B%0A            throw new IllegalThreadStateException(%22Calling from wrong thread.%22)
 ;%0A  
@@ -2663,25 +2663,36 @@
    m
+Pending
 Message
- = 
+s.add(
 message
+)
 ;%0A  

Fix LogView.