Chromium Code Reviews| 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..855bce57c3813765393b17f59e89aefbd3376ae7 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 |
| @@ -4,6 +4,7 @@ |
| package org.chromium.device.nfc; |
| +import android.net.Uri; |
| import android.nfc.NdefMessage; |
| import android.nfc.NdefRecord; |
| import android.os.Build; |
| @@ -15,6 +16,7 @@ import org.chromium.mojom.device.nfc.NfcRecordType; |
| import java.io.UnsupportedEncodingException; |
| import java.util.ArrayList; |
| +import java.util.Arrays; |
| import java.util.List; |
| /** |
| @@ -35,8 +37,6 @@ public final class NfcTypeConverter { |
| * Converts mojo NfcMessage to android.nfc.NdefMessage |
| */ |
| public static NdefMessage toNdefMessage(NfcMessage message) throws InvalidNfcMessageException { |
| - if (message == null || message.data.length == 0) throw new InvalidNfcMessageException(); |
| - |
| try { |
| List<NdefRecord> records = new ArrayList<NdefRecord>(); |
| for (int i = 0; i < message.data.length; ++i) { |
| @@ -53,6 +53,31 @@ public final class NfcTypeConverter { |
| } |
| /** |
| + * Converts android.nfc.NdefMessage to mojo NfcMessage |
| + */ |
| + public static NfcMessage toNfcMessage(NdefMessage ndefMessage) |
| + throws UnsupportedEncodingException { |
| + 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); |
| + } |
| + |
| + 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 +117,95 @@ 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.toUri()); |
| + case NdefRecord.TNF_WELL_KNOWN: |
| + return createWellKnownRecord(ndefRecord); |
| + } |
| + return null; |
| + } |
| + |
| + /** |
| + * Constructs empty NdefMessage |
| + */ |
| + public static NdefMessage emptyNdefMessage() { |
| + return new NdefMessage(new NdefRecord(NdefRecord.TNF_EMPTY, null, null, null)); |
| + } |
| + |
| + /** |
| + * Constructs empty NfcRecord |
| + */ |
| + private static NfcRecord createEmptyRecord() { |
| + NfcRecord nfcRecord = new NfcRecord(); |
| + nfcRecord.recordType = NfcRecordType.EMPTY; |
| + nfcRecord.mediaType = new String(); |
| + nfcRecord.data = new byte[0]; |
| + return nfcRecord; |
| + } |
| + |
| + /** |
| + * Constructs URL NfcRecord |
| + */ |
| + private static NfcRecord createURLRecord(Uri uri) { |
| + if (uri == null) return null; |
| + NfcRecord nfcRecord = new NfcRecord(); |
| + nfcRecord.recordType = NfcRecordType.URL; |
| + nfcRecord.mediaType = TEXT_MIME; |
| + nfcRecord.data = uri.toString().getBytes(); |
| + 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); |
|
Reilly Grant (use Gerrit)
2016/09/29 04:38:02
Can you add a comment pointing to where the format
shalamov
2016/09/30 11:51:35
Added reference to the spec and made stricter lang
|
| + nfcRecord.data = Arrays.copyOfRange(text, langCodeLength + 1, text.length); |
| + return nfcRecord; |
| + } |
| + |
| + /** |
| + * Constructs well known type (TEXT or URI) NfcRecord |
| + */ |
| + private static NfcRecord createWellKnownRecord(NdefRecord record) { |
| + if (Arrays.equals(record.getType(), NdefRecord.RTD_URI)) { |
| + return createURLRecord(record.toUri()); |
| + } |
| + |
| + if (Arrays.equals(record.getType(), NdefRecord.RTD_TEXT)) { |
| + return createTextRecord(record.getPayload()); |
| + } |
| + |
| + return null; |
| + } |
| +} |