Moved the "geo:" fix to the 'right' place
[zxing.git] / core / src / com / google / zxing / client / result / NDEFSmartPosterParsedResult.java
1 /*
2  * Copyright 2008 Google Inc.
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;
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 public final class NDEFSmartPosterParsedResult extends AbstractNDEFParsedResult {
33
34   public static final int ACTION_UNSPECIFIED = -1;
35   public static final int ACTION_DO = 0;
36   public static final int ACTION_SAVE = 1;
37   public static final int ACTION_OPEN = 2;
38
39   private String title;
40   private String uri;
41   private int action;
42
43   private NDEFSmartPosterParsedResult() {
44     super(ParsedReaderResultType.NDEF_SMART_POSTER);
45     action = ACTION_UNSPECIFIED;
46   }
47
48   public static NDEFSmartPosterParsedResult parse(Result result) {
49     byte[] bytes = result.getRawBytes();
50     if (bytes == null) {
51       return null;
52     }
53     NDEFRecord headerRecord = NDEFRecord.readRecord(bytes, 0);
54     // Yes, header record starts and ends a message
55     if (headerRecord == null || !headerRecord.isMessageBegin() || !headerRecord.isMessageEnd()) {
56       return null;
57     }
58     if (!headerRecord.getType().equals(NDEFRecord.SMART_POSTER_WELL_KNOWN_TYPE)) {
59       return null;
60     }
61
62     int offset = 0;
63     int recordNumber = 0;
64     NDEFRecord ndefRecord = null;
65     byte[] payload = headerRecord.getPayload();
66     NDEFSmartPosterParsedResult smartPosterParsedResult = new NDEFSmartPosterParsedResult();
67
68     while (offset < payload.length && (ndefRecord = NDEFRecord.readRecord(payload, offset)) != null) {
69       if (recordNumber == 0 && !ndefRecord.isMessageBegin()) {
70         return null;
71       }
72       String type = ndefRecord.getType();
73       if (NDEFRecord.TEXT_WELL_KNOWN_TYPE.equals(type)) {
74         String[] languageText = NDEFTextParsedResult.decodeTextPayload(ndefRecord.getPayload());
75         smartPosterParsedResult.title = languageText[1];
76       } else if (NDEFRecord.URI_WELL_KNOWN_TYPE.equals(type)) {
77         smartPosterParsedResult.uri = NDEFURIParsedResult.decodeURIPayload(ndefRecord.getPayload());
78       } else if (NDEFRecord.ACTION_WELL_KNOWN_TYPE.equals(type)) {
79         smartPosterParsedResult.action = ndefRecord.getPayload()[0];
80       }
81       recordNumber++;
82       offset += ndefRecord.getTotalRecordLength();
83     }
84     
85     if (recordNumber == 0 || (ndefRecord != null && !ndefRecord.isMessageEnd())) {
86       return null;
87     }
88
89     return smartPosterParsedResult;
90   }
91
92   public String getTitle() {
93     return title;
94   }
95
96   public String getURI() {
97     return uri;
98   }
99
100   public int getAction() {
101     return action;
102   }
103
104   public String getDisplayResult() {
105     if (title == null) {
106       return uri;
107     } else {
108       return title + '\n' + uri;
109     }
110   }
111
112 }