00b97555e27082457e1c2b644815a1c3e4a29508
[zxing.git] / core / src / com / google / zxing / client / result / optional / NDEFSmartPosterResultParser.java
1 /*
2  * Copyright 2008 ZXing authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.google.zxing.client.result.optional;
18
19 import com.google.zxing.Result;
20
21 /**
22  * <p>Recognizes an NDEF message that encodes information according to the
23  * "Smart Poster Record Type Definition" specification.</p>
24  *
25  * <p>This actually only supports some parts of the Smart Poster format: title,
26  * URI, and action records. Icon records are not supported because the size
27  * of these records are infeasibly large for barcodes. Size and type records
28  * are not supported. Multiple titles are not supported.</p>
29  *
30  * @author srowen@google.com (Sean Owen)
31  */
32 final class NDEFSmartPosterResultParser extends AbstractNDEFResultParser {
33
34   public static NDEFSmartPosterParsedResult parse(Result result) {
35     byte[] bytes = result.getRawBytes();
36     if (bytes == null) {
37       return null;
38     }
39     NDEFRecord headerRecord = NDEFRecord.readRecord(bytes, 0);
40     // Yes, header record starts and ends a message
41     if (headerRecord == null || !headerRecord.isMessageBegin() || !headerRecord.isMessageEnd()) {
42       return null;
43     }
44     if (!headerRecord.getType().equals(NDEFRecord.SMART_POSTER_WELL_KNOWN_TYPE)) {
45       return null;
46     }
47
48     int offset = 0;
49     int recordNumber = 0;
50     NDEFRecord ndefRecord = null;
51     byte[] payload = headerRecord.getPayload();
52     int action = NDEFSmartPosterParsedResult.ACTION_UNSPECIFIED;
53     String title = null;
54     String uri = null;
55
56     while (offset < payload.length && (ndefRecord = NDEFRecord.readRecord(payload, offset)) != null) {
57       if (recordNumber == 0 && !ndefRecord.isMessageBegin()) {
58         return null;
59       }
60
61       String type = ndefRecord.getType();
62       if (NDEFRecord.TEXT_WELL_KNOWN_TYPE.equals(type)) {
63         String[] languageText = NDEFTextResultParser.decodeTextPayload(ndefRecord.getPayload());
64         title = languageText[1];
65       } else if (NDEFRecord.URI_WELL_KNOWN_TYPE.equals(type)) {
66         uri = NDEFURIResultParser.decodeURIPayload(ndefRecord.getPayload());
67       } else if (NDEFRecord.ACTION_WELL_KNOWN_TYPE.equals(type)) {
68         action = ndefRecord.getPayload()[0];
69       }
70       recordNumber++;
71       offset += ndefRecord.getTotalRecordLength();
72     }
73
74     if (recordNumber == 0 || (ndefRecord != null && !ndefRecord.isMessageEnd())) {
75       return null;
76     }
77
78     return new NDEFSmartPosterParsedResult(action, uri, title);
79   }
80
81 }