package net.waynepiekarski.wearsensors;

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

public class GraphView extends View {

    private Paint foreground;
    private Paint background;
    private Paint leader;
    private double mMax;
    private int step;
    private int stepAhead;
    private double array[];
    private Bitmap bitmap;
    private Canvas canvas;
    private Paint paint[];
    private int palette[] = { Color.RED, Color.GREEN, Color.BLUE, Color.CYAN, Color.MAGENTA, Color.YELLOW, Color.GRAY };

    public GraphView(Context context, AttributeSet attrs) {
        super(context, attrs);
        foreground = new Paint();
        foreground.setColor(Color.LTGRAY);
        background = new Paint();
        background.setColor(Color.BLACK);
        leader = new Paint();
        leader.setColor(Color.DKGRAY);
        paint = new Paint[palette.length];
        for (int i = 0; i < palette.length; i++) {
            paint[i] = new Paint();
            paint[i].setColor(palette[i]);
        }
        reset();
        step = 0;
        stepAhead = 1;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
        int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
        int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
        this.setMeasuredDimension(parentWidth, parentHeight);
    }

    @Override
    protected void onDraw(Canvas liveCanvas) {
        super.onDraw(canvas);

        if ((bitmap == null) || (bitmap.getWidth() != canvas.getWidth()) || (bitmap.getHeight() != canvas.getHeight())) {
            bitmap = Bitmap.createBitmap(liveCanvas.getWidth(), liveCanvas.getHeight(), Bitmap.Config.ARGB_8888);
            canvas = new Canvas(bitmap);
            canvas.drawColor(background.getColor());
            canvas.drawRect(0,0,canvas.getWidth()-1,0, foreground);
            canvas.drawRect(canvas.getWidth()-1,0,canvas.getWidth()-1,canvas.getHeight()-1, foreground);
            canvas.drawRect(canvas.getWidth()-1,canvas.getHeight()-1,0,canvas.getHeight()-1, foreground);
            canvas.drawRect(0, canvas.getHeight()-1, 0, 0, foreground);
        }

        // Clear out pixels on the current column before we draw here
        canvas.drawLine(step, 0, step, canvas.getHeight(), background);
        canvas.drawLine(stepAhead, 0, stepAhead, canvas.getHeight(), leader);

        // Plot the latest data at the current column
        if (array != null) {
            for (int i = 0; i < array.length; i++) {
                int x = step;
                int y = (int)(canvas.getHeight()/2.0 + array[i]/mMax*canvas.getHeight() / 2.0);

                canvas.drawLine(x, y - 1, x, y + 1, paint[i]);
            }
        }

        liveCanvas.drawBitmap(bitmap, 0, 0, foreground);

        step += 1;
        if (step > canvas.getWidth())
            step = 0;
        stepAhead += 1;
        if (stepAhead > canvas.getWidth())
            stepAhead = 0;
    }

    public void setSize(int length) {
        if ((array == null) || (length != array.length))
            array = new double[length];
    }

    public void setValues(float[] in) {
        if (in.length != array.length)
            Logging.fatal("Mismatch between incoming length " + array.length + " with existing " + in.length);
        for (int i = 0; i < in.length; i++)
            array[i] = in[i];
        invalidate();
    }

    public void setMaximum(double in) {
        if (mMax != in) {
            mMax = in;
            array = null;
            invalidate();
        }
    }

    public void reset() {
        array = null;
        step = 0;
        mMax = 1.0;
        invalidate();
    }
}

@@ -1200,49 +1200,8 @@
 ();%0A
-        step = 0;%0A        stepAhead = 1;%0A
     
@@ -1501,95 +1501,36 @@
     
-@Override%0A    protec
+priva
 te
-d
  void 
-onDraw(Canvas liveCanvas) %7B%0A        super.onDraw(canvas);%0A
+clearBitmap() %7B
 %0A   
@@ -1546,17 +1546,17 @@
 (bitmap 
-=
+!
 = null) 
@@ -1563,251 +1563,26 @@
 %7C%7C (
-bitmap.getWidth() != canvas.getWidth()) %7C%7C (bitmap.getHeight() != canvas.getHeight())) %7B%0A            bitmap = Bitmap.createBitmap(liveCanvas.getWidth(), liveCanvas.getHeight(), Bitmap.Config.ARGB_8888);%0A            canvas = new Canvas(bitmap);
+canvas != null)) %7B
 %0A   
@@ -1661,18 +1661,20 @@
 wRect(0,
+ 
 0,
+ 
 canvas.g
@@ -1682,19 +1682,22 @@
 tWidth()
--
+ - 
 1,
+ 
 0, foreg
@@ -1749,21 +1749,25 @@
 tWidth()
--
+ - 
 1,
+ 
 0,
+ 
 canvas.g
@@ -1767,35 +1767,38 @@
 anvas.getWidth()
--
+ - 
 1,
+ 
 canvas.getHeight
@@ -1791,33 +1791,35 @@
 nvas.getHeight()
--
+ - 
 1, foreground);%0A
@@ -1863,19 +1863,22 @@
 tWidth()
--
+ - 
 1,
+ 
 canvas.g
@@ -1891,13 +1891,17 @@
 ht()
--
+ - 
 1,
+ 
 0,
+ 
 canv
@@ -1914,17 +1914,19 @@
 Height()
--
+ - 
 1, foreg
@@ -1982,17 +1982,19 @@
 Height()
--
+ - 
 1, 0, 0,
@@ -2008,32 +2008,445 @@
 ound);%0A        %7D
+%0A    %7D%0A%0A    @Override%0A    protected void onDraw(Canvas liveCanvas) %7B%0A        super.onDraw(canvas);%0A%0A        if ((bitmap == null) %7C%7C (bitmap.getWidth() != canvas.getWidth()) %7C%7C (bitmap.getHeight() != canvas.getHeight())) %7B%0A            bitmap = Bitmap.createBitmap(liveCanvas.getWidth(), liveCanvas.getHeight(), Bitmap.Config.ARGB_8888);%0A            canvas = new Canvas(bitmap);%0A            clearBitmap();%0A        %7D
 %0A%0A        // Cle
@@ -3738,24 +3738,45 @@
 ax != in) %7B%0A
+            reset();%0A
             
@@ -3790,34 +3790,8 @@
 in;%0A
-            array = null;%0A
     
@@ -3887,32 +3887,62 @@
       step = 0;%0A
+        stepAhead = step + 1;%0A
         mMax = 1
@@ -3949,34 +3949,35 @@
 .0;%0A        
-invalidate
+clearBitmap
 ();%0A    %7D%0A%7D%0A

Reset the graph whenever we change between sensors