OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.device.nfc; | 5 package org.chromium.device.nfc; |
6 | 6 |
7 import android.net.Uri; | |
7 import android.nfc.NdefMessage; | 8 import android.nfc.NdefMessage; |
8 import android.nfc.NdefRecord; | 9 import android.nfc.NdefRecord; |
9 import android.os.Build; | 10 import android.os.Build; |
10 | 11 |
11 import org.chromium.base.Log; | 12 import org.chromium.base.Log; |
12 import org.chromium.device.nfc.mojom.NfcMessage; | 13 import org.chromium.device.nfc.mojom.NfcMessage; |
13 import org.chromium.device.nfc.mojom.NfcRecord; | 14 import org.chromium.device.nfc.mojom.NfcRecord; |
14 import org.chromium.device.nfc.mojom.NfcRecordType; | 15 import org.chromium.device.nfc.mojom.NfcRecordType; |
15 | 16 |
16 import java.io.UnsupportedEncodingException; | 17 import java.io.UnsupportedEncodingException; |
17 import java.util.ArrayList; | 18 import java.util.ArrayList; |
19 import java.util.Arrays; | |
18 import java.util.List; | 20 import java.util.List; |
19 | 21 |
20 /** | 22 /** |
21 * Utility class that provides convesion between Android NdefMessage | 23 * Utility class that provides convesion between Android NdefMessage |
22 * and mojo NfcMessage data structures. | 24 * and mojo NfcMessage data structures. |
23 */ | 25 */ |
24 public final class NfcTypeConverter { | 26 public final class NfcTypeConverter { |
25 private static final String TAG = "NfcTypeConverter"; | 27 private static final String TAG = "NfcTypeConverter"; |
26 private static final String DOMAIN = "w3.org"; | 28 private static final String DOMAIN = "w3.org"; |
27 private static final String TYPE = "webnfc"; | 29 private static final String TYPE = "webnfc"; |
28 private static final String WEBNFC_URN = DOMAIN + ":" + TYPE; | 30 private static final String WEBNFC_URN = DOMAIN + ":" + TYPE; |
29 private static final String TEXT_MIME = "text/plain"; | 31 private static final String TEXT_MIME = "text/plain"; |
30 private static final String JSON_MIME = "application/json"; | 32 private static final String JSON_MIME = "application/json"; |
31 private static final String CHARSET_UTF8 = ";charset=UTF-8"; | 33 private static final String CHARSET_UTF8 = ";charset=UTF-8"; |
32 private static final String CHARSET_UTF16 = ";charset=UTF-16"; | 34 private static final String CHARSET_UTF16 = ";charset=UTF-16"; |
33 | 35 |
34 /** | 36 /** |
35 * Converts mojo NfcMessage to android.nfc.NdefMessage | 37 * Converts mojo NfcMessage to android.nfc.NdefMessage |
36 */ | 38 */ |
37 public static NdefMessage toNdefMessage(NfcMessage message) throws InvalidNf cMessageException { | 39 public static NdefMessage toNdefMessage(NfcMessage message) throws InvalidNf cMessageException { |
38 if (message == null || message.data.length == 0) throw new InvalidNfcMes sageException(); | |
39 | |
40 try { | 40 try { |
41 List<NdefRecord> records = new ArrayList<NdefRecord>(); | 41 List<NdefRecord> records = new ArrayList<NdefRecord>(); |
42 for (int i = 0; i < message.data.length; ++i) { | 42 for (int i = 0; i < message.data.length; ++i) { |
43 records.add(toNdefRecord(message.data[i])); | 43 records.add(toNdefRecord(message.data[i])); |
44 } | 44 } |
45 records.add(NdefRecord.createExternal(DOMAIN, TYPE, message.url.getB ytes("UTF-8"))); | 45 records.add(NdefRecord.createExternal(DOMAIN, TYPE, message.url.getB ytes("UTF-8"))); |
46 NdefRecord[] ndefRecords = new NdefRecord[records.size()]; | 46 NdefRecord[] ndefRecords = new NdefRecord[records.size()]; |
47 records.toArray(ndefRecords); | 47 records.toArray(ndefRecords); |
48 return new NdefMessage(ndefRecords); | 48 return new NdefMessage(ndefRecords); |
49 } catch (UnsupportedEncodingException | InvalidNfcMessageException | 49 } catch (UnsupportedEncodingException | InvalidNfcMessageException |
50 | IllegalArgumentException e) { | 50 | IllegalArgumentException e) { |
51 throw new InvalidNfcMessageException(); | 51 throw new InvalidNfcMessageException(); |
52 } | 52 } |
53 } | 53 } |
54 | 54 |
55 /** | 55 /** |
56 * Converts android.nfc.NdefMessage to mojo NfcMessage | |
57 */ | |
58 public static NfcMessage toNfcMessage(NdefMessage ndefMessage) | |
59 throws UnsupportedEncodingException { | |
60 NdefRecord[] ndefRecords = ndefMessage.getRecords(); | |
61 NfcMessage nfcMessage = new NfcMessage(); | |
62 List<NfcRecord> nfcRecords = new ArrayList<NfcRecord>(); | |
63 | |
64 for (int i = 0; i < ndefRecords.length; i++) { | |
65 if ((ndefRecords[i].getTnf() == NdefRecord.TNF_EXTERNAL_TYPE) | |
66 && (Arrays.equals(ndefRecords[i].getType(), WEBNFC_URN.getBy tes("UTF-8")))) { | |
67 nfcMessage.url = new String(ndefRecords[i].getPayload(), "UTF-8" ); | |
68 continue; | |
69 } | |
70 | |
71 NfcRecord nfcRecord = toNfcRecord(ndefRecords[i]); | |
72 if (nfcRecord != null) nfcRecords.add(nfcRecord); | |
73 } | |
74 | |
75 nfcMessage.data = new NfcRecord[nfcRecords.size()]; | |
76 nfcRecords.toArray(nfcMessage.data); | |
77 return nfcMessage; | |
78 } | |
79 | |
80 /** | |
56 * Returns charset of mojo NfcRecord. Only applicable for URL and TEXT recor ds. | 81 * 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. | 82 * If charset cannot be determined, UTF-8 charset is used by default. |
58 */ | 83 */ |
59 private static String getCharset(NfcRecord record) { | 84 private static String getCharset(NfcRecord record) { |
60 if (record.mediaType.endsWith(CHARSET_UTF8)) return "UTF-8"; | 85 if (record.mediaType.endsWith(CHARSET_UTF8)) return "UTF-8"; |
61 | 86 |
62 // When 16bit WTF::String data is converted to bytearray, it is in LE by te order, without | 87 // 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 | 88 // BOM. By default, Android interprets UTF-16 charset without BOM as UTF -16BE, thus, use |
64 // UTF-16LE as encoding for text data. | 89 // UTF-16LE as encoding for text data. |
65 | 90 |
(...skipping 19 matching lines...) Expand all Loading... | |
85 } else { | 110 } else { |
86 return NdefRecord.createMime(TEXT_MIME, record.data); | 111 return NdefRecord.createMime(TEXT_MIME, record.data); |
87 } | 112 } |
88 case NfcRecordType.JSON: | 113 case NfcRecordType.JSON: |
89 case NfcRecordType.OPAQUE_RECORD: | 114 case NfcRecordType.OPAQUE_RECORD: |
90 return NdefRecord.createMime(record.mediaType, record.data); | 115 return NdefRecord.createMime(record.mediaType, record.data); |
91 default: | 116 default: |
92 throw new InvalidNfcMessageException(); | 117 throw new InvalidNfcMessageException(); |
93 } | 118 } |
94 } | 119 } |
120 | |
121 /** | |
122 * Converts android.nfc.NdefRecord to mojo NfcRecord | |
123 */ | |
124 private static NfcRecord toNfcRecord(NdefRecord ndefRecord) | |
125 throws UnsupportedEncodingException { | |
126 switch (ndefRecord.getTnf()) { | |
127 case NdefRecord.TNF_EMPTY: | |
128 return createEmptyRecord(); | |
129 case NdefRecord.TNF_MIME_MEDIA: | |
130 return createMIMERecord( | |
131 new String(ndefRecord.getType(), "UTF-8"), ndefRecord.ge tPayload()); | |
132 case NdefRecord.TNF_ABSOLUTE_URI: | |
133 return createURLRecord(ndefRecord.toUri()); | |
134 case NdefRecord.TNF_WELL_KNOWN: | |
135 return createWellKnownRecord(ndefRecord); | |
136 } | |
137 return null; | |
138 } | |
139 | |
140 /** | |
141 * Constructs empty NdefMessage | |
142 */ | |
143 public static NdefMessage emptyNdefMessage() { | |
144 return new NdefMessage(new NdefRecord(NdefRecord.TNF_EMPTY, null, null, null)); | |
145 } | |
146 | |
147 /** | |
148 * Constructs empty NfcRecord | |
149 */ | |
150 private static NfcRecord createEmptyRecord() { | |
151 NfcRecord nfcRecord = new NfcRecord(); | |
152 nfcRecord.recordType = NfcRecordType.EMPTY; | |
153 nfcRecord.mediaType = new String(); | |
154 nfcRecord.data = new byte[0]; | |
155 return nfcRecord; | |
156 } | |
157 | |
158 /** | |
159 * Constructs URL NfcRecord | |
160 */ | |
161 private static NfcRecord createURLRecord(Uri uri) { | |
162 if (uri == null) return null; | |
163 NfcRecord nfcRecord = new NfcRecord(); | |
164 nfcRecord.recordType = NfcRecordType.URL; | |
165 nfcRecord.mediaType = TEXT_MIME; | |
166 nfcRecord.data = uri.toString().getBytes(); | |
167 return nfcRecord; | |
168 } | |
169 | |
170 /** | |
171 * Constructs MIME or JSON NfcRecord | |
172 */ | |
173 private static NfcRecord createMIMERecord(String mediaType, byte[] payload) { | |
174 NfcRecord nfcRecord = new NfcRecord(); | |
175 if (mediaType.equals(JSON_MIME)) { | |
176 nfcRecord.recordType = NfcRecordType.JSON; | |
177 } else { | |
178 nfcRecord.recordType = NfcRecordType.OPAQUE_RECORD; | |
179 } | |
180 nfcRecord.mediaType = mediaType; | |
181 nfcRecord.data = payload; | |
182 return nfcRecord; | |
183 } | |
184 | |
185 /** | |
186 * Constructs TEXT NfcRecord | |
187 */ | |
188 private static NfcRecord createTextRecord(byte[] text) { | |
189 NfcRecord nfcRecord = new NfcRecord(); | |
190 nfcRecord.recordType = NfcRecordType.TEXT; | |
191 nfcRecord.mediaType = TEXT_MIME; | |
192 // According to NFCForum-TS-RTD_Text_1.0 specification, section 3.2.1 Sy ntax. | |
193 // First byte of the payload is status byte, defined in Table 3: Status Byte Encodings. | |
194 // 0-5: lang code length | |
195 // 6 : must be zero | |
196 // 8 : 0 - text is in UTF-8 encoding, 1 - text is in UTF-16 encoding. | |
197 int langCodeLength = (text[0] & (byte) 0x3F); | |
Reilly Grant (use Gerrit)
2016/10/06 04:59:50
Check text.length >= 1?
shalamov
2016/10/06 13:47:58
I added early check for text.length, but that shou
Reilly Grant (use Gerrit)
2016/10/07 04:16:23
If the Android API says it can't happen then it's
| |
198 int textBodyStartPos = langCodeLength + 1; | |
199 if (textBodyStartPos > text.length) { | |
200 return null; | |
201 } | |
202 nfcRecord.data = Arrays.copyOfRange(text, textBodyStartPos, text.length) ; | |
203 return nfcRecord; | |
204 } | |
205 | |
206 /** | |
207 * Constructs well known type (TEXT or URI) NfcRecord | |
208 */ | |
209 private static NfcRecord createWellKnownRecord(NdefRecord record) { | |
210 if (Arrays.equals(record.getType(), NdefRecord.RTD_URI)) { | |
211 return createURLRecord(record.toUri()); | |
212 } | |
213 | |
214 if (Arrays.equals(record.getType(), NdefRecord.RTD_TEXT)) { | |
215 return createTextRecord(record.getPayload()); | |
216 } | |
217 | |
218 return null; | |
219 } | |
95 } | 220 } |
OLD | NEW |