Merge "Various fixes"
diff --git a/apps/Tag/AndroidManifest.xml b/apps/Tag/AndroidManifest.xml
index 13feddd..adb9373 100644
--- a/apps/Tag/AndroidManifest.xml
+++ b/apps/Tag/AndroidManifest.xml
@@ -22,7 +22,7 @@
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.android.apps.tag">
     <application android:label="Tags">
-        <activity android:name="Tags">
+        <activity android:name="Tags" android:icon="@drawable/ic_launcher_nfc">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN"/>
                 <category android:name="android.intent.category.LAUNCHER"/>
diff --git a/apps/Tag/res/drawable/ic_launcher_nfc.png b/apps/Tag/res/drawable/ic_launcher_nfc.png
new file mode 100644
index 0000000..9b076ab
--- /dev/null
+++ b/apps/Tag/res/drawable/ic_launcher_nfc.png
Binary files differ
diff --git a/apps/Tag/src/com/android/apps/tag/SaveTag.java b/apps/Tag/src/com/android/apps/tag/SaveTag.java
index 877f9a7..2be1288 100644
--- a/apps/Tag/src/com/android/apps/tag/SaveTag.java
+++ b/apps/Tag/src/com/android/apps/tag/SaveTag.java
@@ -21,7 +21,11 @@
 import android.app.Dialog;
 import android.content.DialogInterface;
 import android.os.Bundle;
+import android.util.Log;
 import android.widget.Toast;
+import com.trustedlogic.trustednfc.android.NfcManager;
+import com.trustedlogic.trustednfc.android.NdefMessage;
+
 
 /**
  * @author nnk@google.com (Nick Kralevich)
@@ -32,7 +36,10 @@
     protected void onStart() {
         super.onStart();
         showDialog(1);
-        String s = getIntent().getExtras().toString();
+        NdefMessage msg = getIntent().getParcelableExtra(NfcManager.NDEF_MESSAGE_EXTRA);
+
+        String s = toHexString(msg.toByteArray());
+        Log.d("SaveTag", s);
         Toast.makeText(this.getBaseContext(), "SaveTag: " + s, Toast.LENGTH_SHORT).show();
     }
 
@@ -49,4 +56,19 @@
     public void onClick(DialogInterface dialog, int which) {
         finish();
     }
+
+    protected void onStop() {
+        super.onStop();
+        dismissDialog(1);
+    }
+
+    private static final char[] hexDigits = "0123456789abcdef".toCharArray();
+
+    private static String toHexString(byte[] bytes) {
+        StringBuilder sb = new StringBuilder(3 * bytes.length);
+        for (byte b : bytes) {
+            sb.append("(byte) 0x").append(hexDigits[(b >> 4) & 0xf]).append(hexDigits[b & 0xf]).append(", ");
+        }
+        return sb.toString();
+    }
 }
diff --git a/apps/Tag/src/com/android/apps/tag/TagBroadcastReceiver.java b/apps/Tag/src/com/android/apps/tag/TagBroadcastReceiver.java
index 015e897..d576bdb 100644
--- a/apps/Tag/src/com/android/apps/tag/TagBroadcastReceiver.java
+++ b/apps/Tag/src/com/android/apps/tag/TagBroadcastReceiver.java
@@ -19,14 +19,12 @@
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
-import android.widget.Toast;
 import com.trustedlogic.trustednfc.android.NdefMessage;
-import com.trustedlogic.trustednfc.android.NdefRecord;
 import com.trustedlogic.trustednfc.android.NfcManager;
 
 /**
- * This class doesn't work.  Sorry.  Think of this class as pseudo
- * code for now.
+ * When we receive a new NDEF tag, start the activity to
+ * process the tag.
  */
 public class TagBroadcastReceiver extends BroadcastReceiver {
 
@@ -34,19 +32,10 @@
     public void onReceive(Context context, Intent intent) {
         if (intent.getAction().equals(NfcManager.NDEF_TAG_DISCOVERED_ACTION)) {
             NdefMessage msg = intent.getParcelableExtra(NfcManager.NDEF_MESSAGE_EXTRA);
-            Toast.makeText(context, "got a new message", Toast.LENGTH_SHORT).show();
-            insertIntoDb(msg);
+            Intent i = new Intent(context, SaveTag.class)
+                    .putExtra(NfcManager.NDEF_MESSAGE_EXTRA, msg)
+                    .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+            context.startActivity(i);
         }
     }
-
-    private void insertIntoDb(NdefMessage msg) {
-        for (NdefRecord record : msg.getRecords()) {
-            insertIntoRecordDb(record.getType(), record.getPayload());
-        }
-    }
-
-    private void insertIntoRecordDb(byte[] type, byte[] payload) {
-        // do something...
-    }
-
 }
diff --git a/apps/Tag/src/com/android/apps/tag/TagDBHelper.java b/apps/Tag/src/com/android/apps/tag/TagDBHelper.java
index a105d5c..36ae7a0 100644
--- a/apps/Tag/src/com/android/apps/tag/TagDBHelper.java
+++ b/apps/Tag/src/com/android/apps/tag/TagDBHelper.java
@@ -23,6 +23,7 @@
 import com.google.common.annotations.VisibleForTesting;
 import com.trustedlogic.trustednfc.android.NdefMessage;
 import com.trustedlogic.trustednfc.android.NdefRecord;
+import com.trustedlogic.trustednfc.android.NfcException;
 
 import java.net.URI;
 import java.util.Date;
@@ -44,6 +45,56 @@
     private static final String INSERT =
             "INSERT INTO NdefMessage (bytes, date) values (?, ?)";
 
+    private static final byte[] REAL_NFC_MSG = new byte[] {
+            (byte) 0xd1,
+            (byte) 0x02,
+            (byte) 0x2b,
+            (byte) 0x53,
+            (byte) 0x70,
+            (byte) 0x91,
+            (byte) 0x01,
+            (byte) 0x17,
+            (byte) 0x54,
+            (byte) 0x02,
+            (byte) 0x65,
+            (byte) 0x6e,
+            (byte) 0x4e,
+            (byte) 0x46,
+            (byte) 0x43,
+            (byte) 0x20,
+            (byte) 0x46,
+            (byte) 0x6f,
+            (byte) 0x72,
+            (byte) 0x75,
+            (byte) 0x6d,
+            (byte) 0x20,
+            (byte) 0x54,
+            (byte) 0x79,
+            (byte) 0x70,
+            (byte) 0x65,
+            (byte) 0x20,
+            (byte) 0x34,
+            (byte) 0x20,
+            (byte) 0x54,
+            (byte) 0x61,
+            (byte) 0x67,
+            (byte) 0x51,
+            (byte) 0x01,
+            (byte) 0x0c,
+            (byte) 0x55,
+            (byte) 0x01,
+            (byte) 0x6e,
+            (byte) 0x78,
+            (byte) 0x70,
+            (byte) 0x2e,
+            (byte) 0x63,
+            (byte) 0x6f,
+            (byte) 0x6d,
+            (byte) 0x2f,
+            (byte) 0x6e,
+            (byte) 0x66,
+            (byte) 0x63
+    };
 
     public TagDBHelper(Context context) {
         this(context, "Tags.db");
@@ -71,6 +122,14 @@
 
         insert(db, msg1);
         insert(db, msg2);
+
+        try {
+            // A real message obtained from an NFC Forum Type 4 tag.
+            NdefMessage msg3 = new NdefMessage(REAL_NFC_MSG);
+            insert(db, msg3);
+        } catch (NfcException e) {
+            throw new RuntimeException(e);
+        }
     }
 
     private void insert(SQLiteDatabase db, NdefMessage msg) {