Only set tag poll and activation bytes once.
These were previously set many times (both in connect()/reconnect()). In fact
they only need to be set once at discovery time.
Also fixed two JNI memory leaks I came across.
Change-Id: I5a9ecba78f5e5b18d3664566c9f90ffcfbd606b0
diff --git a/jni/com_android_nfc_NativeNfcTag.cpp b/jni/com_android_nfc_NativeNfcTag.cpp
index 73b6f96..106eedc 100644
--- a/jni/com_android_nfc_NativeNfcTag.cpp
+++ b/jni/com_android_nfc_NativeNfcTag.cpp
@@ -302,12 +302,19 @@
phLibNfc_sRemoteDevInformation_t *psRemoteDevInfo)
{
jclass tag_cls = e->GetObjectClass(tag);
+ jfieldID f = e->GetFieldID(tag_cls, "mTechPollBytes", "[[B");
+
+ jobjectArray existingPollBytes = (jobjectArray) e->GetObjectField(tag, f);
+
+ if (existingPollBytes != NULL) {
+ return;
+ }
+
jfieldID techListField = e->GetFieldID(tag_cls, "mTechList", "[I");
jintArray techList = (jintArray) e->GetObjectField(tag, techListField);
jint *techId = e->GetIntArrayElements(techList, 0);
int techListLength = e->GetArrayLength(techList);
- jfieldID f = e->GetFieldID(tag_cls, "mTechPollBytes", "[[B");
jbyteArray pollBytes = e->NewByteArray(0);
jobjectArray techPollBytes = e->NewObjectArray(techListLength,
e->GetObjectClass(pollBytes), 0);
@@ -360,6 +367,8 @@
e->SetObjectField(tag, f, techPollBytes);
+ e->ReleaseIntArrayElements(techList, techId, 0);
+
}
/*
@@ -369,12 +378,19 @@
phLibNfc_sRemoteDevInformation_t *psRemoteDevInfo)
{
jclass tag_cls = e->GetObjectClass(tag);
+
+ jfieldID f = e->GetFieldID(tag_cls, "mTechActBytes", "[[B");
+ jobjectArray existingActBytes = (jobjectArray) e->GetObjectField(tag, f);
+
+ if (existingActBytes != NULL) {
+ return;
+ }
+
jfieldID techListField = e->GetFieldID(tag_cls, "mTechList", "[I");
jintArray techList = (jintArray) e->GetObjectField(tag, techListField);
int techListLength = e->GetArrayLength(techList);
jint *techId = e->GetIntArrayElements(techList, 0);
- jfieldID f = e->GetFieldID(tag_cls, "mTechActBytes", "[[B");
jbyteArray actBytes = e->NewByteArray(0);
jobjectArray techActBytes = e->NewObjectArray(techListLength,
e->GetObjectClass(actBytes), 0);
@@ -398,7 +414,8 @@
e->SetByteArrayRegion(actBytes, 0, psRemoteDevInfo->RemoteDevInfo.Iso14443B_Info.HiLayerRespLength,
(jbyte *)psRemoteDevInfo->RemoteDevInfo.Iso14443B_Info.HiLayerResp);
}
- else {
+ else if (psRemoteDevInfo->RemDevType == phNfc_eISO14443_A_PICC ||
+ psRemoteDevInfo->RemDevType == phNfc_eISO14443_4A_PICC) {
actBytes = e->NewByteArray(psRemoteDevInfo->RemoteDevInfo.Iso14443A_Info.AppDataLength);
e->SetByteArrayRegion(actBytes, 0,
psRemoteDevInfo->RemoteDevInfo.Iso14443A_Info.AppDataLength,
@@ -422,6 +439,8 @@
e->SetObjectArrayElement(techActBytes, tech, actBytes);
}
e->SetObjectField(tag, f, techActBytes);
+
+ e->ReleaseIntArrayElements(techList, techId, 0);
}
static jboolean com_android_nfc_NativeNfcTag_doConnect(JNIEnv *e,
@@ -521,10 +540,6 @@
goto clean_and_return;
}
- // Success, set poll & act bytes
- set_target_pollBytes(e, o, pRemDevInfo);
- set_target_activationBytes(e, o, pRemDevInfo);
-
result = JNI_TRUE;
clean_and_return: