summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorSylvain Fonteneau <sylvain.fonteneau@trusted-logic.com>2010-10-17 15:32:33 -0700
committerNick Pelly <npelly@google.com>2010-10-18 19:09:26 -0700
commitdd7341f775ca4d86091f4106d6dfef9214073a7d (patch)
treedb9c4a8ecf76071f21cb7ff76607e855785b0e35 /core
parent453c6d449b6469dde9aae6044dedfe3be0da76c5 (diff)
downloadframeworks_base-dd7341f775ca4d86091f4106d6dfef9214073a7d.zip
frameworks_base-dd7341f775ca4d86091f4106d6dfef9214073a7d.tar.gz
frameworks_base-dd7341f775ca4d86091f4106d6dfef9214073a7d.tar.bz2
Added missing implementation for NdefRecord.
Implemented constructor using a byte array to parse. Added missing exception in method declaration, leading to update api/current.xml Change-Id: I2660484aef1225e90c2f32a572041a2c3aecf288
Diffstat (limited to 'core')
-rw-r--r--core/java/android/nfc/NdefRecord.java14
-rw-r--r--core/jni/android_nfc_NdefRecord.cpp84
2 files changed, 96 insertions, 2 deletions
diff --git a/core/java/android/nfc/NdefRecord.java b/core/java/android/nfc/NdefRecord.java
index 23fd2ca..edc5ab9 100644
--- a/core/java/android/nfc/NdefRecord.java
+++ b/core/java/android/nfc/NdefRecord.java
@@ -200,8 +200,17 @@ public class NdefRecord implements Parcelable {
*
* @throws FormatException if the data is not a valid NDEF record
*/
- public NdefRecord(byte[] data) {
- throw new UnsupportedOperationException();
+ public NdefRecord(byte[] data) throws FormatException {
+ /* Prevent compiler to complain about unassigned final fields */
+ mFlags = 0;
+ mTnf = 0;
+ mType = null;
+ mId = null;
+ mPayload = null;
+ /* Perform actual parsing */
+ if (parseNdefRecord(data) == -1) {
+ throw new FormatException("Error while parsing NDEF record");
+ }
}
/**
@@ -280,5 +289,6 @@ public class NdefRecord implements Parcelable {
}
};
+ private native int parseNdefRecord(byte[] data);
private native byte[] generate(short flags, short tnf, byte[] type, byte[] id, byte[] data);
} \ No newline at end of file
diff --git a/core/jni/android_nfc_NdefRecord.cpp b/core/jni/android_nfc_NdefRecord.cpp
index 8ce1837..9d20d6d 100644
--- a/core/jni/android_nfc_NdefRecord.cpp
+++ b/core/jni/android_nfc_NdefRecord.cpp
@@ -80,8 +80,92 @@ end:
return result;
}
+static jint android_nfc_NdefRecord_parseNdefRecord(JNIEnv *e, jobject o,
+ jbyteArray array)
+{
+ uint16_t status;
+ jbyte *raw_record;
+ jsize raw_record_size;
+ jint ret = -1;
+ phFriNfc_NdefRecord_t record;
+
+ jfieldID mType, mId, mPayload, mTnf;
+ jbyteArray type = NULL;
+ jbyteArray id = NULL;
+ jbyteArray payload = NULL;
+
+ jclass record_cls = e->GetObjectClass(o);
+
+ raw_record_size = e->GetArrayLength(array);
+ raw_record = e->GetByteArrayElements(array, NULL);
+ if (raw_record == NULL) {
+ goto clean_and_return;
+ }
+
+ LOGD("phFriNfc_NdefRecord_Parse()");
+ status = phFriNfc_NdefRecord_Parse(&record, (uint8_t *)raw_record);
+ if (status) {
+ LOGE("phFriNfc_NdefRecord_Parse() returned 0x%04x", status);
+ goto clean_and_return;
+ }
+ LOGD("phFriNfc_NdefRecord_Parse() returned 0x%04x", status);
+
+ /* Set TNF field */
+ mTnf = e->GetFieldID(record_cls, "mTnf", "S");
+ e->SetShortField(o, mTnf, record.Tnf);
+
+ /* Set type field */
+ mType = e->GetFieldID(record_cls, "mType", "[B");
+ type = e->NewByteArray(record.TypeLength);
+ if (type == NULL) {
+ goto clean_and_return;
+ }
+ e->SetByteArrayRegion(type, 0, record.TypeLength,
+ (jbyte *)record.Type);
+ e->SetObjectField(o, mType, type);
+
+ /* Set id field */
+ mId = e->GetFieldID(record_cls, "mId", "[B");
+ id = e->NewByteArray(record.IdLength);
+ if (id == NULL) {
+ goto clean_and_return;
+ }
+ e->SetByteArrayRegion(id, 0, record.IdLength,
+ (jbyte *)record.Id);
+ e->SetObjectField(o, mId, id);
+
+ /* Set payload field */
+ mPayload = e->GetFieldID(record_cls, "mPayload", "[B");
+ payload = e->NewByteArray(record.PayloadLength);
+ if (payload == NULL) {
+ goto clean_and_return;
+ }
+ e->SetByteArrayRegion(payload, 0, record.PayloadLength,
+ (jbyte *)record.PayloadData);
+ e->SetObjectField(o, mPayload, payload);
+
+ ret = 0;
+
+clean_and_return:
+ if (type != NULL) {
+ e->DeleteLocalRef(type);
+ }
+ if (id != NULL) {
+ e->DeleteLocalRef(id);
+ }
+ if (payload != NULL) {
+ e->DeleteLocalRef(payload);
+ }
+ if (raw_record != NULL) {
+ e->ReleaseByteArrayElements(array, raw_record, JNI_ABORT);
+ }
+
+ return ret;
+}
+
static JNINativeMethod gMethods[] = {
{"generate", "(SS[B[B[B)[B", (void *)android_nfc_NdefRecord_generate},
+ {"parseNdefRecord", "([B)I", (void *)android_nfc_NdefRecord_parseNdefRecord},
};
int register_android_nfc_NdefRecord(JNIEnv *e)