Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(377)

Unified Diff: device/nfc/android/java/src/org/chromium/device/nfc/NfcTypeConverter.java

Issue 1765533004: [webnfc] Implement watch method for Android nfc mojo service. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@implement_nfc_watch_in_blink
Patch Set: Rebase and add size limit check when tag is read. Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: device/nfc/android/java/src/org/chromium/device/nfc/NfcTypeConverter.java
diff --git a/device/nfc/android/java/src/org/chromium/device/nfc/NfcTypeConverter.java b/device/nfc/android/java/src/org/chromium/device/nfc/NfcTypeConverter.java
index 1c3a85464aaa697f2b2a369b47d628b0734bcd6b..d387ed0f11d356bf27146a6fa8343dd32f96b62b 100644
--- a/device/nfc/android/java/src/org/chromium/device/nfc/NfcTypeConverter.java
+++ b/device/nfc/android/java/src/org/chromium/device/nfc/NfcTypeConverter.java
@@ -15,6 +15,7 @@ import org.chromium.mojom.device.nfc.NfcRecordType;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.List;
/**
@@ -53,6 +54,31 @@ public final class NfcTypeConverter {
}
/**
+ * Converts android.nfc.NdefMessage to mojo NfcMessage
+ */
+ public static NfcMessage toNfcMessage(NdefMessage ndefMessage)
+ throws UnsupportedEncodingException {
dcheng 2016/09/08 05:02:32 If we're going to enforce max message size, we sho
shalamov 2016/09/22 13:38:40 Yes, it is done in another CL.
+ NdefRecord[] ndefRecords = ndefMessage.getRecords();
+ NfcMessage nfcMessage = new NfcMessage();
+ List<NfcRecord> nfcRecords = new ArrayList<NfcRecord>();
+
+ for (int i = 0; i < ndefRecords.length; i++) {
+ if ((ndefRecords[i].getTnf() == NdefRecord.TNF_EXTERNAL_TYPE)
+ && (Arrays.equals(ndefRecords[i].getType(), WEBNFC_URN.getBytes("UTF-8")))) {
+ nfcMessage.url = new String(ndefRecords[i].getPayload(), "UTF-8");
+ continue;
+ }
+
+ NfcRecord nfcRecord = toNfcRecord(ndefRecords[i]);
+ if (nfcRecord != null) nfcRecords.add(nfcRecord);
dcheng 2016/09/08 05:02:32 What does it mean for toNfcRecord() to return null
shalamov 2016/09/22 13:38:40 According to the spec, records that are not suppor
+ }
+
+ nfcMessage.data = new NfcRecord[nfcRecords.size()];
+ nfcRecords.toArray(nfcMessage.data);
+ return nfcMessage;
+ }
+
+ /**
* Returns charset of mojo NfcRecord. Only applicable for URL and TEXT records.
* If charset cannot be determined, UTF-8 charset is used by default.
*/
@@ -92,4 +118,87 @@ public final class NfcTypeConverter {
throw new InvalidNfcMessageException();
}
}
-}
+
+ /**
+ * Converts android.nfc.NdefRecord to mojo NfcRecord
+ */
+ private static NfcRecord toNfcRecord(NdefRecord ndefRecord)
+ throws UnsupportedEncodingException {
+ switch (ndefRecord.getTnf()) {
+ case NdefRecord.TNF_EMPTY:
+ return createEmptyRecord();
+ case NdefRecord.TNF_MIME_MEDIA:
+ return createMIMERecord(
+ new String(ndefRecord.getType(), "UTF-8"), ndefRecord.getPayload());
+ case NdefRecord.TNF_ABSOLUTE_URI:
+ return createURLRecord(ndefRecord.getPayload());
+ case NdefRecord.TNF_WELL_KNOWN:
+ return createWellKnownRecord(ndefRecord.getType(), ndefRecord.getPayload());
+ }
+ return null;
+ }
+
+ /**
+ * Constructs empty NdefMessage
+ */
+ public static NdefMessage emptyNdefMessage() {
+ return new NdefMessage(new NdefRecord(NdefRecord.TNF_EMPTY, null, null, null));
dcheng 2016/09/08 05:02:32 I'm not sure how common this is in Chromium code,
shalamov 2016/09/22 13:38:40 NfcRecord and NfcMessage are final, I can't add st
+ }
+
+ /**
+ * Constructs empty NfcRecord
+ */
+ private static NfcRecord createEmptyRecord() {
+ NfcRecord nfcRecord = new NfcRecord();
+ nfcRecord.recordType = NfcRecordType.EMPTY;
+ return nfcRecord;
+ }
+
+ /**
+ * Constructs URL NfcRecord
+ */
+ private static NfcRecord createURLRecord(byte[] payload) {
+ NfcRecord nfcRecord = new NfcRecord();
+ nfcRecord.recordType = NfcRecordType.URL;
+ nfcRecord.data = payload;
+ return nfcRecord;
+ }
+
+ /**
+ * Constructs MIME or JSON NfcRecord
+ */
+ private static NfcRecord createMIMERecord(String mediaType, byte[] payload) {
+ NfcRecord nfcRecord = new NfcRecord();
+ if (mediaType.equals(JSON_MIME)) {
+ nfcRecord.recordType = NfcRecordType.JSON;
+ } else {
+ nfcRecord.recordType = NfcRecordType.OPAQUE_RECORD;
+ }
+ nfcRecord.mediaType = mediaType;
+ nfcRecord.data = payload;
+ return nfcRecord;
+ }
+
+ /**
+ * Constructs TEXT NfcRecord
+ */
+ private static NfcRecord createTextRecord(byte[] text) {
+ NfcRecord nfcRecord = new NfcRecord();
+ nfcRecord.recordType = NfcRecordType.TEXT;
+ nfcRecord.mediaType = TEXT_MIME;
+ int langCodeLength = (text[0] & (byte) 0xFF);
+ nfcRecord.data = Arrays.copyOfRange(text, langCodeLength + 1, text.length);
+ return nfcRecord;
+ }
+
+ /**
+ * Constructs well known type (TEXT or URI) NfcRecord
+ */
+ private static NfcRecord createWellKnownRecord(byte[] type, byte[] payload) {
+ if (Arrays.equals(type, NdefRecord.RTD_URI)) return createURLRecord(payload);
+
+ if (Arrays.equals(type, NdefRecord.RTD_TEXT)) return createTextRecord(payload);
+
+ return null;
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698