Some quick cleanup of Tag.apk using new NDEF APIs
Change-Id: Ib3825f7abb27f7552ad0c0a93ce5ceb3af18d262
diff --git a/src/com/android/apps/tag/TagViewer.java b/src/com/android/apps/tag/TagViewer.java
index b4e9474..682dba5 100644
--- a/src/com/android/apps/tag/TagViewer.java
+++ b/src/com/android/apps/tag/TagViewer.java
@@ -23,7 +23,6 @@
import android.app.Activity;
import android.content.Intent;
import android.nfc.NdefMessage;
-import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.os.Bundle;
import android.os.Parcelable;
diff --git a/src/com/android/apps/tag/message/NdefMessageParser.java b/src/com/android/apps/tag/message/NdefMessageParser.java
index c627c32..54d2888 100644
--- a/src/com/android/apps/tag/message/NdefMessageParser.java
+++ b/src/com/android/apps/tag/message/NdefMessageParser.java
@@ -51,12 +51,12 @@
public static List<ParsedNdefRecord> getRecords(NdefRecord[] records) {
List<ParsedNdefRecord> elements = new ArrayList<ParsedNdefRecord>();
for (NdefRecord record : records) {
- if (UriRecord.isUri(record)) {
+ if (SmartPoster.isPoster(record)) {
+ elements.add(SmartPoster.parse(record));
+ } else if (UriRecord.isUri(record)) {
elements.add(UriRecord.parse(record));
} else if (TextRecord.isText(record)) {
elements.add(TextRecord.parse(record));
- } else if (SmartPoster.isPoster(record)) {
- elements.add(SmartPoster.parse(record));
} else if (ImageRecord.isImage(record)) {
elements.add(ImageRecord.parse(record));
} else if (VCardRecord.isVCard(record)) {
diff --git a/src/com/android/apps/tag/record/ImageRecord.java b/src/com/android/apps/tag/record/ImageRecord.java
index 942b5f2..34af2ee 100644
--- a/src/com/android/apps/tag/record/ImageRecord.java
+++ b/src/com/android/apps/tag/record/ImageRecord.java
@@ -51,11 +51,11 @@
}
public static ImageRecord parse(NdefRecord record) {
- MimeRecord underlyingRecord = MimeRecord.parse(record);
- Preconditions.checkArgument(underlyingRecord.getMimeType().startsWith("image/"));
+ String mimeType = record.toMimeType();
+ Preconditions.checkArgument(mimeType.startsWith("image/"));
// Try to ensure it's a legal, valid image
- byte[] content = underlyingRecord.getContent();
+ byte[] content = record.getPayload();
Bitmap bitmap = BitmapFactory.decodeByteArray(content, 0, content.length);
if (bitmap == null) {
throw new IllegalArgumentException("not a valid image file");
@@ -76,6 +76,6 @@
ByteArrayOutputStream out = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
byte[] content = out.toByteArray();
- return MimeRecord.newMimeRecord("image/jpeg", content);
+ return NdefRecord.createMime("image/jpeg", content);
}
}
diff --git a/src/com/android/apps/tag/record/MimeRecord.java b/src/com/android/apps/tag/record/MimeRecord.java
index 11ceb20..ccb5392 100644
--- a/src/com/android/apps/tag/record/MimeRecord.java
+++ b/src/com/android/apps/tag/record/MimeRecord.java
@@ -68,27 +68,15 @@
}
public static MimeRecord parse(NdefRecord record) {
- Preconditions.checkArgument(record.getTnf() == NdefRecord.TNF_MIME_MEDIA);
- String type = new String(record.getType(), Charset.forName("US-ASCII"));
- byte[] payload = record.getPayload();
- return new MimeRecord(type, payload);
+ Preconditions.checkArgument(record.toMimeType() != null);
+ return new MimeRecord(record.toMimeType(), record.getPayload());
}
public static boolean isMime(NdefRecord record) {
- try {
- parse(record);
- return true;
- } catch (IllegalArgumentException e) {
- return false;
- }
+ return record.toMimeType() != null;
}
public static NdefRecord newMimeRecord(String type, byte[] data) {
- Preconditions.checkNotNull(type);
- Preconditions.checkNotNull(data);
-
- byte[] typeBytes = type.getBytes(Charset.forName("US-ASCII"));
-
- return new NdefRecord(NdefRecord.TNF_MIME_MEDIA, typeBytes, new byte[0], data);
+ return NdefRecord.createMime(type, data);
}
}
diff --git a/src/com/android/apps/tag/record/TextRecord.java b/src/com/android/apps/tag/record/TextRecord.java
index 1ecdec3..e398579 100644
--- a/src/com/android/apps/tag/record/TextRecord.java
+++ b/src/com/android/apps/tag/record/TextRecord.java
@@ -23,24 +23,15 @@
import android.app.Activity;
import android.content.Context;
-import android.content.Intent;
-import android.content.pm.PackageManager;
-import android.content.pm.ResolveInfo;
-import android.net.Uri;
import android.nfc.NdefRecord;
-import android.os.Parcel;
-import android.os.Parcelable;
-import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
-import android.widget.ImageView;
import android.widget.TextView;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.Arrays;
-import java.util.List;
import java.util.Locale;
/**
diff --git a/src/com/android/apps/tag/record/UriRecord.java b/src/com/android/apps/tag/record/UriRecord.java
index f7e152f..2846e83 100644
--- a/src/com/android/apps/tag/record/UriRecord.java
+++ b/src/com/android/apps/tag/record/UriRecord.java
@@ -56,51 +56,6 @@
public static final String RECORD_TYPE = "UriRecord";
- /**
- * NFC Forum "URI Record Type Definition"
- *
- * This is a mapping of "URI Identifier Codes" to URI string prefixes,
- * per section 3.2.2 of the NFC Forum URI Record Type Definition document.
- */
- private static final BiMap<Byte, String> URI_PREFIX_MAP = ImmutableBiMap.<Byte, String>builder()
- .put((byte) 0x00, "")
- .put((byte) 0x01, "http://www.")
- .put((byte) 0x02, "https://www.")
- .put((byte) 0x03, "http://")
- .put((byte) 0x04, "https://")
- .put((byte) 0x05, "tel:")
- .put((byte) 0x06, "mailto:")
- .put((byte) 0x07, "ftp://anonymous:anonymous@")
- .put((byte) 0x08, "ftp://ftp.")
- .put((byte) 0x09, "ftps://")
- .put((byte) 0x0A, "sftp://")
- .put((byte) 0x0B, "smb://")
- .put((byte) 0x0C, "nfs://")
- .put((byte) 0x0D, "ftp://")
- .put((byte) 0x0E, "dav://")
- .put((byte) 0x0F, "news:")
- .put((byte) 0x10, "telnet://")
- .put((byte) 0x11, "imap:")
- .put((byte) 0x12, "rtsp://")
- .put((byte) 0x13, "urn:")
- .put((byte) 0x14, "pop:")
- .put((byte) 0x15, "sip:")
- .put((byte) 0x16, "sips:")
- .put((byte) 0x17, "tftp:")
- .put((byte) 0x18, "btspp://")
- .put((byte) 0x19, "btl2cap://")
- .put((byte) 0x1A, "btgoep://")
- .put((byte) 0x1B, "tcpobex://")
- .put((byte) 0x1C, "irdaobex://")
- .put((byte) 0x1D, "file://")
- .put((byte) 0x1E, "urn:epc:id:")
- .put((byte) 0x1F, "urn:epc:tag:")
- .put((byte) 0x20, "urn:epc:pat:")
- .put((byte) 0x21, "urn:epc:raw:")
- .put((byte) 0x22, "urn:epc:")
- .put((byte) 0x23, "urn:nfc:")
- .build();
-
private final Uri mUri;
private UriRecord(Uri uri) {
@@ -174,78 +129,19 @@
* record containing a URI.
*/
public static UriRecord parse(NdefRecord record) {
- short tnf = record.getTnf();
- if (tnf == NdefRecord.TNF_WELL_KNOWN) {
- return parseWellKnown(record);
- } else if (tnf == NdefRecord.TNF_ABSOLUTE_URI) {
- return parseAbsolute(record);
- }
- throw new IllegalArgumentException("Unknown TNF " + tnf);
- }
-
- /** Parse and absolute URI record */
- private static UriRecord parseAbsolute(NdefRecord record) {
- byte[] payload = record.getPayload();
- Uri uri = Uri.parse(new String(payload, Charset.forName("UTF-8")));
- return new UriRecord(uri);
- }
-
- /** Parse an well known URI record */
- private static UriRecord parseWellKnown(NdefRecord record) {
- Preconditions.checkArgument(Arrays.equals(record.getType(), NdefRecord.RTD_URI));
-
- byte[] payload = record.getPayload();
- Preconditions.checkArgument(payload.length > 0);
-
- /*
- * payload[0] contains the URI Identifier Code, per the
- * NFC Forum "URI Record Type Definition" section 3.2.2.
- *
- * payload[1]...payload[payload.length - 1] contains the rest of
- * the URI.
- */
-
- String prefix = URI_PREFIX_MAP.get(payload[0]);
- Preconditions.checkArgument(prefix != null);
-
- byte[] fullUri = Bytes.concat(
- prefix.getBytes(Charset.forName("UTF-8")),
- Arrays.copyOfRange(payload, 1, payload.length));
-
- Uri uri = Uri.parse(new String(fullUri, Charset.forName("UTF-8")));
+ Uri uri = record.toUri();
+ if (uri == null) throw new IllegalArgumentException("not a uri");
return new UriRecord(uri);
}
public static boolean isUri(NdefRecord record) {
- try {
- parse(record);
- return true;
- } catch (IllegalArgumentException e) {
- return false;
- }
+ return record.toUri() != null;
}
- private static final byte[] EMPTY = new byte[0];
-
/**
* Convert a {@link Uri} to an {@link NdefRecord}
*/
public static NdefRecord newUriRecord(Uri uri) {
- byte[] uriBytes = uri.toString().getBytes(Charset.forName("UTF-8"));
-
- /*
- * We prepend 0x00 to the bytes of the URI to indicate that this
- * is the entire URI, and we are not taking advantage of the
- * URI shortening rules in the NFC Forum URI spec section 3.2.2.
- * This produces a NdefRecord which is slightly larger than
- * necessary.
- *
- * In the future, we should use the URI shortening rules in 3.2.2
- * to create a smaller NdefRecord.
- */
- byte[] payload = Bytes.concat(new byte[] { 0x00 }, uriBytes);
-
- return new NdefRecord(NdefRecord.TNF_WELL_KNOWN,
- NdefRecord.RTD_URI, EMPTY, payload);
+ return NdefRecord.createUri(uri);
}
}
diff --git a/src/com/android/apps/tag/record/VCardRecord.java b/src/com/android/apps/tag/record/VCardRecord.java
index 9b96807..7d2099f 100644
--- a/src/com/android/apps/tag/record/VCardRecord.java
+++ b/src/com/android/apps/tag/record/VCardRecord.java
@@ -153,12 +153,12 @@
MimeRecord underlyingRecord = MimeRecord.parse(record);
// TODO: Add support for other vcard mime types.
- Preconditions.checkArgument("text/x-vCard".equals(underlyingRecord.getMimeType()));
+ Preconditions.checkArgument("text/x-vcard".equals(underlyingRecord.getMimeType()));
return new VCardRecord(underlyingRecord.getContent());
}
public static NdefRecord newVCardRecord(byte[] data) {
- return MimeRecord.newMimeRecord("text/x-vCard", data);
+ return MimeRecord.newMimeRecord("text/x-vcard", data);
}
@Override