OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.device.nfc; |
| 6 |
| 7 import android.nfc.NdefMessage; |
| 8 import android.nfc.NdefRecord; |
| 9 import android.os.Build; |
| 10 |
| 11 import org.chromium.base.Log; |
| 12 import org.chromium.mojom.device.nfc.NfcMessage; |
| 13 import org.chromium.mojom.device.nfc.NfcRecord; |
| 14 import org.chromium.mojom.device.nfc.NfcRecordType; |
| 15 |
| 16 import java.io.UnsupportedEncodingException; |
| 17 import java.util.ArrayList; |
| 18 import java.util.List; |
| 19 |
| 20 /** |
| 21 * Utility class that provides convesion between Android NdefMessage |
| 22 * and mojo NfcMessage data structures. |
| 23 */ |
| 24 public final class NfcTypeConverter { |
| 25 private static final String TAG = "NfcTypeConverter"; |
| 26 private static final String DOMAIN = "w3.org"; |
| 27 private static final String TYPE = "webnfc"; |
| 28 private static final String WEBNFC_URN = DOMAIN + ":" + TYPE; |
| 29 private static final String TEXT_MIME = "text/plain"; |
| 30 private static final String JSON_MIME = "application/json"; |
| 31 private static final String CHARSET_UTF8 = ";charset=UTF-8"; |
| 32 private static final String CHARSET_UTF16 = ";charset=UTF-16"; |
| 33 |
| 34 /** |
| 35 * Converts mojo NfcMessage to android.nfc.NdefMessage |
| 36 */ |
| 37 public static NdefMessage toNdefMessage(NfcMessage message) throws InvalidNf
cMessageException { |
| 38 if (message == null || message.data.length == 0) throw new InvalidNfcMes
sageException(); |
| 39 |
| 40 try { |
| 41 List<NdefRecord> records = new ArrayList<NdefRecord>(); |
| 42 for (int i = 0; i < message.data.length; ++i) { |
| 43 records.add(toNdefRecord(message.data[i])); |
| 44 } |
| 45 records.add(NdefRecord.createExternal(DOMAIN, TYPE, message.url.getB
ytes("UTF-8"))); |
| 46 NdefRecord[] ndefRecords = new NdefRecord[records.size()]; |
| 47 records.toArray(ndefRecords); |
| 48 return new NdefMessage(ndefRecords); |
| 49 } catch (UnsupportedEncodingException | InvalidNfcMessageException |
| 50 | IllegalArgumentException e) { |
| 51 throw new InvalidNfcMessageException(); |
| 52 } |
| 53 } |
| 54 |
| 55 /** |
| 56 * Returns charset of mojo NfcRecord. Only applicable for URL and TEXT recor
ds. |
| 57 * If charset cannot be determined, UTF-8 charset is used by default. |
| 58 */ |
| 59 private static String getCharset(NfcRecord record) { |
| 60 if (record.mediaType.endsWith(CHARSET_UTF8)) return "UTF-8"; |
| 61 |
| 62 // When 16bit WTF::String data is converted to bytearray, it is in LE by
te order, without |
| 63 // BOM. By default, Android interprets UTF-16 charset without BOM as UTF
-16BE, thus, use |
| 64 // UTF-16LE as encoding for text data. |
| 65 |
| 66 if (record.mediaType.endsWith(CHARSET_UTF16)) return "UTF-16LE"; |
| 67 |
| 68 Log.w(TAG, "Unknown charset, defaulting to UTF-8."); |
| 69 return "UTF-8"; |
| 70 } |
| 71 |
| 72 /** |
| 73 * Converts mojo NfcRecord to android.nfc.NdefRecord |
| 74 */ |
| 75 private static NdefRecord toNdefRecord(NfcRecord record) throws InvalidNfcMe
ssageException, |
| 76 IllegalArgum
entException, |
| 77 UnsupportedE
ncodingException { |
| 78 switch (record.recordType) { |
| 79 case NfcRecordType.URL: |
| 80 return NdefRecord.createUri(new String(record.data, getCharset(r
ecord))); |
| 81 case NfcRecordType.TEXT: |
| 82 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
| 83 return NdefRecord.createTextRecord( |
| 84 "en-US", new String(record.data, getCharset(record))
); |
| 85 } else { |
| 86 return NdefRecord.createMime(TEXT_MIME, record.data); |
| 87 } |
| 88 case NfcRecordType.JSON: |
| 89 case NfcRecordType.OPAQUE_RECORD: |
| 90 return NdefRecord.createMime(record.mediaType, record.data); |
| 91 default: |
| 92 throw new InvalidNfcMessageException(); |
| 93 } |
| 94 } |
| 95 } |
OLD | NEW |