[NFC] Fix for NFCTag manual test.
authorTomasz Marciniak <t.marciniak@samsung.com>
Thu, 21 May 2015 13:59:15 +0000 (15:59 +0200)
committerTomasz Marciniak <t.marciniak@samsung.com>
Thu, 21 May 2015 14:11:36 +0000 (16:11 +0200)
[Feature] Create NDEFRecord before passing it
to NDEFMessage constructor.

[Verification] Code compiles without errors.
Manual test NFCTag_writeNDEF_with_successCallback passes.

Change-Id: I9ac72702a351d1c28e767f470c11d5c761901f49
Signed-off-by: Tomasz Marciniak <t.marciniak@samsung.com>
src/nfc/nfc_api.js
src/nfc/nfc_message_utils.cc

index e7c9e0d41db4dda66ea777fbb787ba04b2ea25e6..e43460cd10df1a67aa4871ac8e1cc8324ffc066d 100644 (file)
@@ -567,7 +567,7 @@ NFCAdapter.prototype.getCachedMessage = function() {
     return new tizen.NDEFMessage();
   }
 
-  return new tizen.NDEFMessage(result.records);
+  return new tizen.NDEFMessage(toRecordsArray(result.records));
 };
 
 NFCAdapter.prototype.setExclusiveModeForTransaction = function() {
@@ -849,6 +849,47 @@ NFCAdapter.prototype.getAIDsForCategory = function(type, category, successCallba
   native_.call('NFCAdapter_getAIDsForCategory', data, callback);
 };
 
+function InternalRecordData(tnf, type, payload, id) {
+  this.tnf = tnf;
+  this.type = type;
+  this.payload = payload;
+  this.id = id;
+};
+
+var toRecordsArray = function(array) {
+  var result = [];
+  if (type_.isNullOrUndefined(array) || !type_.isArray(array)) {
+    return result;
+  }
+
+  for (var i = 0; i < array.length; i++) {
+    var data = new InternalRecordData(array[i].tnf, array[i].type, array[i].payload, array[i].id);
+
+    if (array[i].recordType == 'Record') {
+      result.push(new tizen.NDEFRecord(data.tnf_, data.type_, data.payload_, data.id_));
+      continue;
+    }
+
+    if (array[i].recordType == 'RecordText') {
+      result.push(new tizen.NDEFRecordText(array[i].text, array[i].languageCode,
+          array[i].encoding, data));
+      continue;
+    }
+
+    if (array[i].recordType == 'RecordURI') {
+      result.push(new tizen.NDEFRecordURI(array[i].uri, data));
+      continue;
+    }
+
+    if (array[i].recordType == 'RecordMedia') {
+      result.push(new tizen.NDEFRecordMedia(array[i].mimeType, array[i].data, data));
+      continue;
+    }
+  }
+
+  return result;
+};
+
 //////////////////NFCTag /////////////////
 
 function NFCTag(tagid) {
@@ -941,7 +982,7 @@ function NFCTag(tagid) {
               args.errorCallback(native_.getErrorObject(result));
             }
           } else {
-            var message = new tizen.NDEFMessage(result.records);
+            var message = new tizen.NDEFMessage(toRecordsArray(result.records));
             args.readCallback(message);
           }
         });
@@ -1317,7 +1358,7 @@ tizen.NDEFRecord = function(first, type, payload, id) {
 };
 
 //////////////////NDEFRecordText /////////////////
-tizen.NDEFRecordText = function(text, languageCode, encoding) {
+tizen.NDEFRecordText = function(text, languageCode, encoding, internal_) {
   var text_ = undefined;
   var languageCode_ = undefined;
   var encoding_ = NDEFRecordTextEncoding[encoding] ?
@@ -1327,18 +1368,22 @@ tizen.NDEFRecordText = function(text, languageCode, encoding) {
       text_ = converter_.toString(text);
       languageCode_ = converter_.toString(languageCode);
 
-      var result = native_.callSync(
-          'NDEFRecordText_constructor', {
-            'text': text_,
-            'languageCode' : languageCode_,
-            'encoding' : encoding_
-          }
-          );
-      if (native_.isFailure(result)) {
-        throw native_.getErrorObject(result);
+      if (!type_.isNullOrUndefined(internal_) && (internal_ instanceof InternalRecordData)) {
+        tizen.NDEFRecord.call(this, internal_.tnf_, internal_.type_, internal_.payload_, internal_.id_);
+      } else {
+        var result = native_.callSync(
+            'NDEFRecordText_constructor', {
+              'text': text_,
+              'languageCode' : languageCode_,
+              'encoding' : encoding_
+            }
+            );
+        if (native_.isFailure(result)) {
+          throw native_.getErrorObject(result);
+        }
+        tizen.NDEFRecord.call(this, result.result.tnf, result.result.type,
+            result.result.payload, result.result.id);
       }
-      tizen.NDEFRecord.call(this, result.result.tnf, result.result.type,
-          result.result.payload, result.result.id);
     } else {
       throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR);
     }
@@ -1362,22 +1407,26 @@ tizen.NDEFRecordText.prototype = new tizen.NDEFRecord(new InternalData());
 tizen.NDEFRecordText.prototype.constructor = tizen.NDEFRecordText;
 
 //////////////////NDEFRecordURI /////////////////
-tizen.NDEFRecordURI = function(uri) {
+tizen.NDEFRecordURI = function(uri, internal_) {
   var uri_ = undefined;
   try {
     if (arguments.length >= 1) {
       uri_ = converter_.toString(uri);
 
-      var result = native_.callSync(
-          'NDEFRecordURI_constructor', {
-            'uri': uri_
-          }
-          );
-      if (native_.isFailure(result)) {
-        throw native_.getErrorObject(result);
+      if (!type_.isNullOrUndefined(internal_) && (internal_ instanceof InternalRecordData)) {
+        tizen.NDEFRecord.call(this, internal_.tnf_, internal_.type_, internal_.payload_, internal_.id_);
+      } else {
+        var result = native_.callSync(
+            'NDEFRecordURI_constructor', {
+              'uri': uri_
+            }
+            );
+        if (native_.isFailure(result)) {
+          throw native_.getErrorObject(result);
+        }
+        tizen.NDEFRecord.call(this, result.result.tnf, result.result.type,
+            result.result.payload, result.result.id);
       }
-      tizen.NDEFRecord.call(this, result.result.tnf, result.result.type,
-          result.result.payload, result.result.id);
     } else {
       throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR);
     }
@@ -1397,7 +1446,7 @@ tizen.NDEFRecordURI.prototype = new tizen.NDEFRecord(new InternalData());
 tizen.NDEFRecordURI.prototype.constructor = tizen.NDEFRecordURI;
 
 //////////////////NDEFRecordMedia /////////////////
-tizen.NDEFRecordMedia = function(mimeType, data) {
+tizen.NDEFRecordMedia = function(mimeType, data, internal_) {
   var mimeType_ = undefined;
   var data_ = undefined;
   try {
@@ -1405,18 +1454,22 @@ tizen.NDEFRecordMedia = function(mimeType, data) {
       mimeType_ = converter_.toString(mimeType);
       data_ = toByteArray(data, Math.pow(2, 32) - 1);
 
-      var result = native_.callSync(
-          'NDEFRecordMedia_constructor', {
-            'mimeType': mimeType_,
-            'data': data_,
-            'dataSize': data_.length
-          }
-          );
-      if (native_.isFailure(result)) {
-        throw native_.getErrorObject(result);
+      if (!type_.isNullOrUndefined(internal_) && (internal_ instanceof InternalRecordData)) {
+        tizen.NDEFRecord.call(this, internal_.tnf_, internal_.type_, internal_.payload_, internal_.id_);
+      } else {
+        var result = native_.callSync(
+            'NDEFRecordMedia_constructor', {
+              'mimeType': mimeType_,
+              'data': data_,
+              'dataSize': data_.length
+            }
+            );
+        if (native_.isFailure(result)) {
+          throw native_.getErrorObject(result);
+        }
+        tizen.NDEFRecord.call(this, result.result.tnf, result.result.type,
+            result.result.payload, result.result.id);
       }
-      tizen.NDEFRecord.call(this, result.result.tnf, result.result.type,
-          result.result.payload, result.result.id);
     } else {
       throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR);
     }
index be56d51c2f1baf022ff64d84b0cd126e0e93dd26..b0054f034fbcb509435126d764a216416903c3ca 100644 (file)
@@ -225,6 +225,7 @@ PlatformResult NFCMessageUtils::ToNdefRecords(const nfc_ndef_message_h message,
         if (ret.IsError()) {
           return ret;
         }
+        record_obj.insert(std::make_pair("recordType", picojson::value("RecordMedia")));
         continue;
       } else if (NFC_RECORD_TNF_WELL_KNOWN == tnf) {
         if (!type.empty()) {
@@ -233,6 +234,7 @@ PlatformResult NFCMessageUtils::ToNdefRecords(const nfc_ndef_message_h message,
             if (ret.IsError()) {
               return ret;
             }
+            record_obj.insert(std::make_pair("recordType", picojson::value("RecordText")));
             continue;
           }
           if (RECORD_TYPE_URI == type[0]) {
@@ -240,6 +242,7 @@ PlatformResult NFCMessageUtils::ToNdefRecords(const nfc_ndef_message_h message,
             if (ret.IsError()) {
               return ret;
             }
+            record_obj.insert(std::make_pair("recordType", picojson::value("RecordURI")));
             continue;
           }
         }
@@ -248,6 +251,7 @@ PlatformResult NFCMessageUtils::ToNdefRecords(const nfc_ndef_message_h message,
       if (ret.IsError()) {
         return ret;
       }
+      record_obj.insert(std::make_pair("recordType", picojson::value("Record")));
     }
   }
   return PlatformResult(ErrorCode::NO_ERROR);