2 * Copyright 2008 ZXing authors
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.google.zxing.client.result.optional;
19 import com.google.zxing.Result;
20 import com.google.zxing.client.result.ParsedReaderResultType;
23 * <p>Recognizes an NDEF message that encodes information according to the
24 * "Smart Poster Record Type Definition" specification.</p>
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>
31 * @author srowen@google.com (Sean Owen)
33 public final class NDEFSmartPosterParsedResult extends AbstractNDEFParsedResult {
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;
44 private NDEFSmartPosterParsedResult() {
45 super(ParsedReaderResultType.NDEF_SMART_POSTER);
46 action = ACTION_UNSPECIFIED;
49 public static NDEFSmartPosterParsedResult parse(Result result) {
50 byte[] bytes = result.getRawBytes();
54 NDEFRecord headerRecord = NDEFRecord.readRecord(bytes, 0);
55 // Yes, header record starts and ends a message
56 if (headerRecord == null || !headerRecord.isMessageBegin() || !headerRecord.isMessageEnd()) {
59 if (!headerRecord.getType().equals(NDEFRecord.SMART_POSTER_WELL_KNOWN_TYPE)) {
65 NDEFRecord ndefRecord = null;
66 byte[] payload = headerRecord.getPayload();
67 NDEFSmartPosterParsedResult smartPosterParsedResult = new NDEFSmartPosterParsedResult();
69 while (offset < payload.length && (ndefRecord = NDEFRecord.readRecord(payload, offset)) != null) {
70 if (recordNumber == 0 && !ndefRecord.isMessageBegin()) {
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];
83 offset += ndefRecord.getTotalRecordLength();
86 if (recordNumber == 0 || (ndefRecord != null && !ndefRecord.isMessageEnd())) {
90 return smartPosterParsedResult;
93 public String getTitle() {
97 public String getURI() {
101 public int getAction() {
105 public String getDisplayResult() {
109 return title + '\n' + uri;