Display live or recorded sensor stream.

Also remove BPM stub code.
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index b35549b..569549f 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -24,4 +24,6 @@
         </activity>
     </application>
 
+    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
+
 </manifest>
diff --git a/assets/plox.dat b/assets/plox.dat
new file mode 100644
index 0000000..bf1e6c5
--- /dev/null
+++ b/assets/plox.dat
Binary files differ
diff --git a/res/layout/activity_ara_plox.xml b/res/layout/activity_ara_plox.xml
index 66778d3..0d0e8aa 100644
--- a/res/layout/activity_ara_plox.xml
+++ b/res/layout/activity_ara_plox.xml
@@ -10,6 +10,7 @@
                   android:layout_height="0dip"
                   android:layout_weight="2"/>
 
+<!--
     <TextView
         android:id="@+id/textView"
         android:layout_width="fill_parent"
@@ -20,5 +21,6 @@
         android:fontFamily="sans-serif"
         android:textSize="64sp"
         android:text="@string/hello_world" />
+-->
 
 </LinearLayout>
diff --git a/src/com/google/araploxio/AFE4400Thread.java b/src/com/google/araploxio/AFE4400Thread.java
index 4e94ea4..84e949b 100644
--- a/src/com/google/araploxio/AFE4400Thread.java
+++ b/src/com/google/araploxio/AFE4400Thread.java
@@ -25,6 +25,7 @@
 import java.io.IOException;
 import java.io.FileNotFoundException;
 import java.io.DataOutputStream;
+import java.io.DataInputStream;
 import java.io.FileOutputStream;
 
 public class AFE4400Thread extends Thread {
@@ -35,11 +36,14 @@
 
     private static final String preferredI2cBus = "/dev/i2c-4";
 
+    private static final boolean useRecordedData = false;
+
     private Handler handler;
     private I2cManager i2c;
+    private Context context;
 
     private volatile boolean stopped;
-    private DataOutputStream buf = null;
+    private DataInputStream ploxInputStream;
 
     private static final I2cTransaction[] setupWrites = {
         WriteReg(0x00, 0x00, 0x00, 0x00), //AFE4400_CONTROL0
@@ -101,18 +105,48 @@
     };
 
     public AFE4400Thread(Context context, Handler handler) {
+        this.context = context;
         this.i2c = (I2cManager)context.getSystemService(Context.I2C_SERVICE);
         this.stopped = false;
         this.handler = handler;
+    }
+
+    private void startRecordedStream() {
+        Log.d(TAG, "Playing back recorded data");
+
         try {
-            this.buf = new DataOutputStream(new FileOutputStream("/sdcard/plox.dat"));
-        } catch (FileNotFoundException e) {
-            Log.e(TAG, e.toString());
+            ploxInputStream = new DataInputStream(context.getAssets().open("plox.dat"));
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+
+        int x = 0;
+        while (!stopped) {
+            Message msg;
+            try {
+                msg = handler.obtainMessage(0, x++, ploxInputStream.readInt());
+                ploxInputStream.readInt();
+                msg.sendToTarget();
+            } catch (IOException e1) {
+                e1.printStackTrace();
+                requestStop();
+                break;
+            }
+            try {
+                Thread.sleep(10);
+            } catch (InterruptedException e) {
+                e.printStackTrace();
+            }
+        }
+
+        try {
+            ploxInputStream.close();
+        } catch (IOException e) {
+            e.printStackTrace();
         }
     }
 
-    @Override
-    public void run() {
+    private void startSensorStream() {
         String[] buses = i2c.getI2cBuses();
         if (buses.length == 0) {
             setText("no I2C buses found :(");
@@ -126,15 +160,6 @@
         }
         Log.i(TAG, "[pulse ox app] Using I2C bus: " + bus);
 
-        int nsecs = 0;
-        while (nsecs > 0) {
-            setText("starting in " + nsecs + "...");
-            nsecs--;
-            try {
-                Thread.sleep(1000);
-            } catch (InterruptedException e) {}
-        }
-
         I2cTransaction[] results;
         for (I2cTransaction txn: setupWrites) {
             try {
@@ -149,7 +174,6 @@
         while (!isStopped()) {
             results = null;
             boolean stop = true;
-            String updateString = "";
             byte[] data;
             int LED1VAL;
             int LED2VAL;
@@ -160,13 +184,6 @@
                 I2cTransaction.newRead(4),
             };
 
-            I2cTransaction[] txns1 = {
-                I2cTransaction.newWrite(0x01,                 // Select SPI device 1
-                                        0x2f,                 // LED2-ALED2VAL
-                                        0xff, 0xff, 0xff),    // 3 dummy bytes
-                I2cTransaction.newRead(4),
-            };
-
             try {
                 if (stop) {
                     for (I2cTransaction txn: txns0) {
@@ -189,42 +206,24 @@
                       (((int)data[2] & 0xFF) <<  8) |
                       (((int)data[3] & 0xFF));
 
-            try {
-                if (stop) {
-                    for (I2cTransaction txn: txns1) {
-                        results = i2c.performTransactions(bus, address, txn);
-                    }
-                } else {
-                    results = i2c.performTransactions(bus, address, txns1);
-                }
-            } catch (IOException e) {
-                setText("error while reading back: " + e);
-                return;
-            }
-
-            if (stop)
-                data = results[0].data;
-            else
-                data = results[1].data;
-
-            LED2VAL = (((int)data[1]) << 16) |
-                      (((int)data[2] & 0xFF) <<  8) |
-                      (((int)data[3] & 0xFF));
-            try {
-                this.buf.writeInt(LED1VAL);
-                this.buf.writeInt(LED2VAL);
-            } catch (IOException e) {
-                Log.e(TAG, "IOException: " + e);
-            }
-
             Message msg = handler.obtainMessage(0, sample++, LED1VAL);
             msg.sendToTarget();
-        }
 
-        try {
-            this.buf.close();
-        } catch (IOException e) {
-            Log.e(TAG, "IOException: " + e);
+            try {
+                Thread.sleep(5);
+            } catch (Exception e) {
+                e.printStackTrace();
+                requestStop();
+            }
+        }
+    }
+
+    @Override
+    public void run() {
+        if (useRecordedData) {
+            startRecordedStream();
+        } else {
+            startSensorStream();
         }
     }
 
diff --git a/src/com/google/araploxio/AraPloxActivity.java b/src/com/google/araploxio/AraPloxActivity.java
index 19ec770..a0f8def 100644
--- a/src/com/google/araploxio/AraPloxActivity.java
+++ b/src/com/google/araploxio/AraPloxActivity.java
@@ -16,6 +16,7 @@
 import android.os.Message;
 import android.util.Log;
 import android.view.Window;
+import android.view.WindowManager;
 import android.view.ViewGroup.LayoutParams;
 import android.widget.LinearLayout;
 import android.widget.TextView;
@@ -33,7 +34,7 @@
 		Log.d(TAG, "onCreate()");
 		requestWindowFeature(Window.FEATURE_NO_TITLE);
 		setContentView(R.layout.activity_ara_plox);
-		bpmTextView = (TextView)findViewById(R.id.textView);
+        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
 
 		mHandler = new Handler() {
 			public void handleMessage(Message msg) {
@@ -42,10 +43,6 @@
 		};
 	}
 
-	private String[] fakeBPMs = new String[] {
-			"72", "72", "74", "75", "73", "73", "70", "70", "71", "73", "76", "78"
-	};
-	private int fakeBPMCount = 0;
 	private void handleAFE4400Message(Message msg) {
 		//	Log.d(TAG, "Message received: arg1: " + msg.arg1 + " arg2: " + msg.arg2);
 		double x = msg.arg1;
@@ -53,32 +50,29 @@
 
 		xySeries.add(x, y);
 		// Scroll continuously after ~150 points
-		if (x > 150) {
+		if (x > 200) {
 			xySeries.remove(0);
 		}
 
-		// Autoscale
-		double maxX = xySeries.getMaxX();
-		double minX = xySeries.getMinX();
-		double maxY = xySeries.getMaxY();
-		double minY = xySeries.getMinY();
-		renderer.setXAxisMax(maxX);
-		renderer.setXAxisMin(minX);
-		renderer.setYAxisMax(maxY);
-		renderer.setYAxisMin(minY);
+        // Autoscale
+        double maxX = xySeries.getMaxX();
+        double minX = xySeries.getMinX();
+        double maxY = xySeries.getMaxY();
+        double minY = xySeries.getMinY();
 
-		chart_view.repaint();
-		
-		// fake pulse rate...
-		if (x % 10 == 0) {
-			bpmTextView.setText(fakeBPMs[fakeBPMCount++] + " bpm");
-			if (fakeBPMCount == fakeBPMs.length) {
-				fakeBPMCount = 0;
-			}
-		}
-	}
+        maxY *= 1.02;
+        minY *= .98;
 
-	private XYMultipleSeriesRenderer renderer;
+        renderer.setXAxisMax(maxX);
+        renderer.setXAxisMin(minX);
+        renderer.setYAxisMax(maxY);
+        renderer.setYAxisMin(minY);
+
+        chart_view.repaint();
+
+    }
+
+    private XYMultipleSeriesRenderer renderer;
 	private XYMultipleSeriesDataset dataSet;
 	private XYSeries xySeries;
 
@@ -91,7 +85,7 @@
 		PointStyle[] styles = new PointStyle[] {PointStyle.CIRCLE};
 
 		renderer = new XYMultipleSeriesRenderer();
-		
+
 		int length = colors.length;
 		for (int i = 0; i < length; i++) {
 			XYSeriesRenderer r = new XYSeriesRenderer();
@@ -131,7 +125,6 @@
 		}
 	}
 
-
 	protected void onResume() {
 		super.onResume();
 		Log.d(TAG, "onResume()");
@@ -151,7 +144,7 @@
 		}
 
 	}
-	
+
 	protected void onStop() {
 		super.onStop();
 		Log.d(TAG, "onStop()");