Implemented Paul's solution to the basic/regular build problem -- a sort of pseudo...
[zxing.git] / core / src / com / google / zxing / client / result / optional / NDEFSmartPosterParsedResult.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 import com.google.zxing.client.result.ParsedReaderResultType;
21
22 /**
23  * <p>Recognizes an NDEF message that encodes information according to the
24  * "Smart Poster Record Type Definition" specification.</p>
25  *
26  * <p>This actually only supports some parts of the Smart Poster format: title,
27  * URI, and action records. Icon records are not supported because the size
28  * of these records are infeasibly large for barcodes. Size and type records
29  * are not supported. Multiple titles are not supported.</p>
30  *
31  * @author srowen@google.com (Sean Owen)
32  */
33 public final class NDEFSmartPosterParsedResult extends AbstractNDEFParsedResult {
34
35   public static final int ACTION_UNSPECIFIED = -1;
36   public static final int ACTION_DO = 0;
37   public static final int ACTION_SAVE = 1;
38   public static final int ACTION_OPEN = 2;
39
40   private String title;
41   private String uri;
42   private int action;
43
44   private NDEFSmartPosterParsedResult() {
45     super(ParsedReaderResultType.NDEF_SMART_POSTER);
46     action = ACTION_UNSPECIFIED;
47   }
48
49   public static NDEFSmartPosterParsedResult parse(Result result) {
50     byte[] bytes = result.getRawBytes();
51     if (bytes == null) {
52       return null;
53     }
54     NDEFRecord headerRecord = NDEFRecord.readRecord(bytes, 0);
55     // Yes, header record starts and ends a message
56     if (headerRecord == null || !headerRecord.isMessageBegin() || !headerRecord.isMessageEnd()) {
57       return null;
58     }
59     if (!headerRecord.getType().equals(NDEFRecord.SMART_POSTER_WELL_KNOWN_TYPE)) {
60       return null;
61     }
62
63     int offset = 0;
64     int recordNumber = 0;
65     NDEFRecord ndefRecord = null;
66     byte[] payload = headerRecord.getPayload();
67     NDEFSmartPosterParsedResult smartPosterParsedResult = new NDEFSmartPosterParsedResult();
68
69     while (offset < payload.length && (ndefRecord = NDEFRecord.readRecord(payload, offset)) != null) {
70       if (recordNumber == 0 && !ndefRecord.isMessageBegin()) {
71         return null;
72       }
73       String type = ndefRecord.getType();
74       if (NDEFRecord.TEXT_WELL_KNOWN_TYPE.equals(type)) {
75         String[] languageText = NDEFTextParsedResult.decodeTextPayload(ndefRecord.getPayload());
76         smartPosterParsedResult.title = languageText[1];
77       } else if (NDEFRecord.URI_WELL_KNOWN_TYPE.equals(type)) {
78         smartPosterParsedResult.uri = NDEFURIParsedResult.decodeURIPayload(ndefRecord.getPayload());
79       } else if (NDEFRecord.ACTION_WELL_KNOWN_TYPE.equals(type)) {
80         smartPosterParsedResult.action = ndefRecord.getPayload()[0];
81       }
82       recordNumber++;
83       offset += ndefRecord.getTotalRecordLength();
84     }
85     
86     if (recordNumber == 0 || (ndefRecord != null && !ndefRecord.isMessageEnd())) {
87       return null;
88     }
89
90     return smartPosterParsedResult;
91   }
92
93   public String getTitle() {
94     return title;
95   }
96
97   public String getURI() {
98     return uri;
99   }
100
101   public int getAction() {
102     return action;
103   }
104
105   public String getDisplayResult() {
106     if (title == null) {
107       return uri;
108     } else {
109       return title + '\n' + uri;
110     }
111   }
112
113 }