Changed DeviceControlActivity to use a CusorAdapter and Loader to listen for changes on APRSContentProvider. Removed broadcasts of packet data. Removed raw file logging.
Changed DeviceControlActivity to use a CusorAdapter and Loader to listen for changes on APRSContentProvider. Removed broadcasts of packet data. Removed raw file logging.

--- a/src/com/dryerzinia/aprs_x/services/BLETNCService.java
+++ b/src/com/dryerzinia/aprs_x/services/BLETNCService.java
@@ -214,14 +214,10 @@
 	        			RawPacketTable.getContentValues(pkt)
 	        		);
 	
+	        		packetCurrentLength = MAX_PACKET_LENGTH;
+
 	        		Log.d(TAG, pkt.toString());
 
-	        		final Intent intent = new Intent(action);
-	        		intent.putExtra(BLE_TNC_PACKET_DATA, pkt);
-	                sendBroadcast(intent);
-	
-	        		packetCurrentLength = MAX_PACKET_LENGTH;
-	
 	        	} else if(packetData.length < packetCurrentLength && packetCurrentLength != MAX_PACKET_LENGTH){
 	
 	        		Log.e(TAG, "Error: Packet to long!");

--- a/src/com/dryerzinia/aprs_x/test/DeviceControlActivity.java
+++ b/src/com/dryerzinia/aprs_x/test/DeviceControlActivity.java
@@ -17,23 +17,30 @@
 package com.dryerzinia.aprs_x.test;
 
 import android.app.Activity;
+import android.app.LoaderManager;
 import android.bluetooth.BluetoothGattCharacteristic;
 import android.bluetooth.BluetoothGattService;
 import android.content.BroadcastReceiver;
 import android.content.ComponentName;
 import android.content.Context;
+import android.content.CursorLoader;
 import android.content.Intent;
 import android.content.IntentFilter;
+import android.content.Loader;
 import android.content.ServiceConnection;
+import android.database.Cursor;
 import android.graphics.Color;
 import android.graphics.PorterDuff.Mode;
 import android.os.Bundle;
 import android.os.Environment;
 import android.os.IBinder;
 import android.util.Log;
+import android.view.LayoutInflater;
 import android.view.Menu;
 import android.view.MenuItem;
-import android.widget.ArrayAdapter;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.CursorAdapter;
 import android.widget.ListView;
 import android.widget.ProgressBar;
 import android.widget.TextView;
@@ -42,13 +49,13 @@
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
-import java.io.UnsupportedEncodingException;
-import java.util.ArrayList;
 import java.util.List;
 import java.util.UUID;
 
 import com.dryerzinia.aprs_x.R;
 import com.dryerzinia.aprs_x.data_model.packet.RawPacket;
+import com.dryerzinia.aprs_x.db.APRSContentProvider;
+import com.dryerzinia.aprs_x.db.table.RawPacketTable;
 import com.dryerzinia.aprs_x.services.BLETNCService;
 
 /**
@@ -57,7 +64,7 @@
  * communicates with {@code BluetoothLeService}, which in turn interacts with the
  * Bluetooth LE API.
  */
-public class DeviceControlActivity extends Activity {
+public class DeviceControlActivity extends Activity implements LoaderManager.LoaderCallbacks<Cursor> {
 
 	private final static String TAG = DeviceControlActivity.class.getSimpleName();
 
@@ -70,13 +77,11 @@
     private TextView mConnectionState;
     private String mDeviceName;
     private String mDeviceAddress;
-    private ArrayAdapter<String> mPacketListArrayAdapter;
-    private List<String> mPacketList = new ArrayList<String>();
+    private RawPacketCursorAdapter mPacketListCursorAdapter;
     private ListView mPacketListView;
     private ProgressBar volumeBar;
     private BLETNCService mBluetoothLeService;
     private boolean mConnected = false;
-    private OutputStream mLogOutputStream;
 
     // Code to manage Service lifecycle.
     private final ServiceConnection mServiceConnection = new ServiceConnection() {
@@ -97,6 +102,28 @@
             mBluetoothLeService = null;
         }
     };
+
+    public class RawPacketCursorAdapter extends CursorAdapter {
+
+    	private LayoutInflater mInflater;
+
+		public RawPacketCursorAdapter(Context context, Cursor c, int flags) {
+			super(context, c, flags);
+			mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
+		}
+
+		@Override
+		public View newView(Context context, Cursor cursor, ViewGroup parent) {
+			return mInflater.inflate(R.layout.packet_list_item, parent, false);
+		}
+
+		@Override
+		public void bindView(View view, Context context, Cursor cursor) {
+			TextView content = (TextView) view.findViewById(R.id.packet_list_item);
+			byte data[] = cursor.getBlob(cursor.getColumnIndex(RawPacketTable.COLUMN_RAW_DATA));
+			content.setText(RawPacket.fromRawData(data).toString());			
+		}
+    }
 
     // Handles various events fired by the Service.
     // ACTION_GATT_CONNECTED: connected to a GATT server.
@@ -148,22 +175,6 @@
 
                 	}
 
-                } else if(intent.hasExtra(BLETNCService.BLE_TNC_PACKET_DATA)){
-
-                    Log.d(TAG, "Displaying Packet");
-
-                	RawPacket pkt = (RawPacket) intent.getSerializableExtra(BLETNCService.BLE_TNC_PACKET_DATA);
-		
-	                try {
-	                	mLogOutputStream.write(0x00);
-	                	mLogOutputStream.write(pkt.getRawData().length);
-						mLogOutputStream.write(pkt.getRawData());
-						mLogOutputStream.flush();
-					} catch (IOException e1) {
-					}
-	
-            		displayData(pkt.toString());
-
                 }
 
             }
@@ -182,17 +193,16 @@
         // Sets up UI references.
         ((TextView) findViewById(R.id.device_address)).setText(mDeviceAddress);
         mPacketListView = (ListView) findViewById(R.id.packet_list);
-        mPacketListArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mPacketList);
-        mPacketListView.setAdapter(mPacketListArrayAdapter);
+        //mPacketListArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mPacketList);
+        //mPacketListView.setAdapter(mPacketListArrayAdapter);
         mConnectionState = (TextView) findViewById(R.id.connection_state);
         volumeBar = (ProgressBar) findViewById(R.id.volume_bar);
         volumeBar.setProgress(0);
 
-        Log.d(TAG, "EXT: " + Environment.getExternalStorageDirectory());
-      	try {
-			mLogOutputStream = new FileOutputStream(Environment.getExternalStorageDirectory()+"/packets.dat", true);
-		} catch (FileNotFoundException e) {
-		}
+        // Set-up loader
+        getLoaderManager().initLoader(0, null, this);
+        mPacketListCursorAdapter = new RawPacketCursorAdapter(this, null, 0);
+        mPacketListView.setAdapter(mPacketListCursorAdapter);
 
         getActionBar().setTitle(mDeviceName);
         getActionBar().setDisplayHomeAsUpEnabled(true);
@@ -268,8 +278,8 @@
 
         	Log.d(TAG, "Disp: " + data);
 
-        	mPacketList.add(data);
-        	mPacketListArrayAdapter.notifyDataSetChanged();
+        	//mPacketList.add(data);
+        	//mPacketListArrayAdapter.notifyDataSetChanged();
         }
     }
 
@@ -281,5 +291,23 @@
         intentFilter.addAction(BLETNCService.ACTION_DATA_AVAILABLE);
         return intentFilter;
     }
+
+	@Override
+	public Loader<Cursor> onCreateLoader(int id, Bundle args) {
+		String[] projection = { RawPacketTable.COLUMN_ID, RawPacketTable.COLUMN_RAW_DATA };
+		CursorLoader cursorLoader = new CursorLoader(this,
+			APRSContentProvider.RAW_PACKET_URI, projection, null, null, null);
+	    return cursorLoader;
+	}
+
+	@Override
+	public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
+		mPacketListCursorAdapter.swapCursor(data);
+	}
+
+	@Override
+	public void onLoaderReset(Loader<Cursor> loader) {
+		mPacketListCursorAdapter.swapCursor(null);		
+	}
 }