diff options
| author | 2010-10-19 11:17:52 -0700 | |
|---|---|---|
| committer | 2010-10-19 11:17:52 -0700 | |
| commit | ca2a75ad5640efc22873ee14043c586444358997 (patch) | |
| tree | 3a4e334895f6b30af47d5b54ff9e79e125a61d39 | |
| parent | 32343128d9630d9a677db4af91f2a2f897114229 (diff) | |
| parent | 824de4b3e378a5ef41de1704ee3f50a49d408a66 (diff) | |
am 824de4b3: am dd7341f7: Added missing implementation for NdefRecord.
Merge commit '824de4b3e378a5ef41de1704ee3f50a49d408a66'
* commit '824de4b3e378a5ef41de1704ee3f50a49d408a66':
Added missing implementation for NdefRecord.
| -rw-r--r-- | api/current.xml | 2 | ||||
| -rw-r--r-- | core/java/android/nfc/NdefRecord.java | 14 | ||||
| -rw-r--r-- | core/jni/android_nfc_NdefRecord.cpp | 84 |
3 files changed, 98 insertions, 2 deletions
diff --git a/api/current.xml b/api/current.xml index 42f059197ffa..29de36dca6f4 100644 --- a/api/current.xml +++ b/api/current.xml @@ -112097,6 +112097,8 @@ > <parameter name="data" type="byte[]"> </parameter> +<exception name="FormatException" type="android.nfc.FormatException"> +</exception> </constructor> <method name="describeContents" return="int" diff --git a/core/java/android/nfc/NdefRecord.java b/core/java/android/nfc/NdefRecord.java index 23fd2ca9b8b9..edc5ab921f1d 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 8ce1837fbf08..9d20d6df714f 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) |