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.nfc.NdefMessage; | 7 import android.nfc.NdefMessage; |
8 import android.nfc.NdefRecord; | 8 import android.nfc.NdefRecord; |
9 import android.os.Build; | 9 import android.os.Build; |
10 | 10 |
11 import org.chromium.base.Log; | 11 import org.chromium.base.Log; |
12 import org.chromium.mojom.device.nfc.NfcMessage; | 12 import org.chromium.mojom.device.nfc.NfcMessage; |
13 import org.chromium.mojom.device.nfc.NfcRecord; | 13 import org.chromium.mojom.device.nfc.NfcRecord; |
14 import org.chromium.mojom.device.nfc.NfcRecordType; | 14 import org.chromium.mojom.device.nfc.NfcRecordType; |
15 | 15 |
16 import java.io.UnsupportedEncodingException; | 16 import java.io.UnsupportedEncodingException; |
17 import java.util.ArrayList; | 17 import java.util.ArrayList; |
18 import java.util.Arrays; | |
18 import java.util.List; | 19 import java.util.List; |
19 | 20 |
20 /** | 21 /** |
21 * Utility class that provides convesion between Android NdefMessage | 22 * Utility class that provides convesion between Android NdefMessage |
22 * and mojo NfcMessage data structures. | 23 * and mojo NfcMessage data structures. |
23 */ | 24 */ |
24 public final class NfcTypeConverter { | 25 public final class NfcTypeConverter { |
25 private static final String TAG = "NfcTypeConverter"; | 26 private static final String TAG = "NfcTypeConverter"; |
26 private static final String DOMAIN = "w3.org"; | 27 private static final String DOMAIN = "w3.org"; |
27 private static final String TYPE = "webnfc"; | 28 private static final String TYPE = "webnfc"; |
(...skipping 18 matching lines...) Expand all Loading... | |
46 NdefRecord[] ndefRecords = new NdefRecord[records.size()]; | 47 NdefRecord[] ndefRecords = new NdefRecord[records.size()]; |
47 records.toArray(ndefRecords); | 48 records.toArray(ndefRecords); |
48 return new NdefMessage(ndefRecords); | 49 return new NdefMessage(ndefRecords); |
49 } catch (UnsupportedEncodingException | InvalidNfcMessageException | 50 } catch (UnsupportedEncodingException | InvalidNfcMessageException |
50 | IllegalArgumentException e) { | 51 | IllegalArgumentException e) { |
51 throw new InvalidNfcMessageException(); | 52 throw new InvalidNfcMessageException(); |
52 } | 53 } |
53 } | 54 } |
54 | 55 |
55 /** | 56 /** |
57 * Converts android.nfc.NdefMessage to mojo NfcMessage | |
58 */ | |
59 public static NfcMessage toNfcMessage(NdefMessage ndefMessage) | |
60 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.
| |
61 NdefRecord[] ndefRecords = ndefMessage.getRecords(); | |
62 NfcMessage nfcMessage = new NfcMessage(); | |
63 List<NfcRecord> nfcRecords = new ArrayList<NfcRecord>(); | |
64 | |
65 for (int i = 0; i < ndefRecords.length; i++) { | |
66 if ((ndefRecords[i].getTnf() == NdefRecord.TNF_EXTERNAL_TYPE) | |
67 && (Arrays.equals(ndefRecords[i].getType(), WEBNFC_URN.getBy tes("UTF-8")))) { | |
68 nfcMessage.url = new String(ndefRecords[i].getPayload(), "UTF-8" ); | |
69 continue; | |
70 } | |
71 | |
72 NfcRecord nfcRecord = toNfcRecord(ndefRecords[i]); | |
73 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
| |
74 } | |
75 | |
76 nfcMessage.data = new NfcRecord[nfcRecords.size()]; | |
77 nfcRecords.toArray(nfcMessage.data); | |
78 return nfcMessage; | |
79 } | |
80 | |
81 /** | |
56 * Returns charset of mojo NfcRecord. Only applicable for URL and TEXT recor ds. | 82 * 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. | 83 * If charset cannot be determined, UTF-8 charset is used by default. |
58 */ | 84 */ |
59 private static String getCharset(NfcRecord record) { | 85 private static String getCharset(NfcRecord record) { |
60 if (record.mediaType.endsWith(CHARSET_UTF8)) return "UTF-8"; | 86 if (record.mediaType.endsWith(CHARSET_UTF8)) return "UTF-8"; |
61 | 87 |
62 // When 16bit WTF::String data is converted to bytearray, it is in LE by te order, without | 88 // 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 | 89 // BOM. By default, Android interprets UTF-16 charset without BOM as UTF -16BE, thus, use |
64 // UTF-16LE as encoding for text data. | 90 // UTF-16LE as encoding for text data. |
65 | 91 |
(...skipping 19 matching lines...) Expand all Loading... | |
85 } else { | 111 } else { |
86 return NdefRecord.createMime(TEXT_MIME, record.data); | 112 return NdefRecord.createMime(TEXT_MIME, record.data); |
87 } | 113 } |
88 case NfcRecordType.JSON: | 114 case NfcRecordType.JSON: |
89 case NfcRecordType.OPAQUE_RECORD: | 115 case NfcRecordType.OPAQUE_RECORD: |
90 return NdefRecord.createMime(record.mediaType, record.data); | 116 return NdefRecord.createMime(record.mediaType, record.data); |
91 default: | 117 default: |
92 throw new InvalidNfcMessageException(); | 118 throw new InvalidNfcMessageException(); |
93 } | 119 } |
94 } | 120 } |
95 } | 121 |
122 /** | |
123 * Converts android.nfc.NdefRecord to mojo NfcRecord | |
124 */ | |
125 private static NfcRecord toNfcRecord(NdefRecord ndefRecord) | |
126 throws UnsupportedEncodingException { | |
127 switch (ndefRecord.getTnf()) { | |
128 case NdefRecord.TNF_EMPTY: | |
129 return createEmptyRecord(); | |
130 case NdefRecord.TNF_MIME_MEDIA: | |
131 return createMIMERecord( | |
132 new String(ndefRecord.getType(), "UTF-8"), ndefRecord.ge tPayload()); | |
133 case NdefRecord.TNF_ABSOLUTE_URI: | |
134 return createURLRecord(ndefRecord.getPayload()); | |
135 case NdefRecord.TNF_WELL_KNOWN: | |
136 return createWellKnownRecord(ndefRecord.getType(), ndefRecord.ge tPayload()); | |
137 } | |
138 return null; | |
139 } | |
140 | |
141 /** | |
142 * Constructs empty NdefMessage | |
143 */ | |
144 public static NdefMessage emptyNdefMessage() { | |
145 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
| |
146 } | |
147 | |
148 /** | |
149 * Constructs empty NfcRecord | |
150 */ | |
151 private static NfcRecord createEmptyRecord() { | |
152 NfcRecord nfcRecord = new NfcRecord(); | |
153 nfcRecord.recordType = NfcRecordType.EMPTY; | |
154 return nfcRecord; | |
155 } | |
156 | |
157 /** | |
158 * Constructs URL NfcRecord | |
159 */ | |
160 private static NfcRecord createURLRecord(byte[] payload) { | |
161 NfcRecord nfcRecord = new NfcRecord(); | |
162 nfcRecord.recordType = NfcRecordType.URL; | |
163 nfcRecord.data = payload; | |
164 return nfcRecord; | |
165 } | |
166 | |
167 /** | |
168 * Constructs MIME or JSON NfcRecord | |
169 */ | |
170 private static NfcRecord createMIMERecord(String mediaType, byte[] payload) { | |
171 NfcRecord nfcRecord = new NfcRecord(); | |
172 if (mediaType.equals(JSON_MIME)) { | |
173 nfcRecord.recordType = NfcRecordType.JSON; | |
174 } else { | |
175 nfcRecord.recordType = NfcRecordType.OPAQUE_RECORD; | |
176 } | |
177 nfcRecord.mediaType = mediaType; | |
178 nfcRecord.data = payload; | |
179 return nfcRecord; | |
180 } | |
181 | |
182 /** | |
183 * Constructs TEXT NfcRecord | |
184 */ | |
185 private static NfcRecord createTextRecord(byte[] text) { | |
186 NfcRecord nfcRecord = new NfcRecord(); | |
187 nfcRecord.recordType = NfcRecordType.TEXT; | |
188 nfcRecord.mediaType = TEXT_MIME; | |
189 int langCodeLength = (text[0] & (byte) 0xFF); | |
190 nfcRecord.data = Arrays.copyOfRange(text, langCodeLength + 1, text.lengt h); | |
191 return nfcRecord; | |
192 } | |
193 | |
194 /** | |
195 * Constructs well known type (TEXT or URI) NfcRecord | |
196 */ | |
197 private static NfcRecord createWellKnownRecord(byte[] type, byte[] payload) { | |
198 if (Arrays.equals(type, NdefRecord.RTD_URI)) return createURLRecord(payl oad); | |
199 | |
200 if (Arrays.equals(type, NdefRecord.RTD_TEXT)) return createTextRecord(pa yload); | |
201 | |
202 return null; | |
203 } | |
204 } | |
OLD | NEW |