2 * Copyright 2008 Google Inc.
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;
19 import com.google.zxing.Result;
22 * <p>Recognizes an NDEF message that encodes information according to the
23 * "Smart Poster Record Type Definition" specification.</p>
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>
30 * @author srowen@google.com (Sean Owen)
32 public final class NDEFSmartPosterParsedResult extends AbstractNDEFParsedResult {
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;
43 private NDEFSmartPosterParsedResult() {
44 super(ParsedReaderResultType.NDEF_SMART_POSTER);
45 action = ACTION_UNSPECIFIED;
48 public static NDEFSmartPosterParsedResult parse(Result result) {
49 byte[] bytes = result.getRawBytes();
53 NDEFRecord headerRecord = NDEFRecord.readRecord(bytes, 0);
54 // Yes, header record starts and ends a message
55 if (headerRecord == null || !headerRecord.isMessageBegin() || !headerRecord.isMessageEnd()) {
58 if (!headerRecord.getType().equals(NDEFRecord.SMART_POSTER_WELL_KNOWN_TYPE)) {
64 NDEFRecord ndefRecord = null;
65 byte[] payload = headerRecord.getPayload();
66 NDEFSmartPosterParsedResult smartPosterParsedResult = new NDEFSmartPosterParsedResult();
68 while (offset < payload.length && (ndefRecord = NDEFRecord.readRecord(payload, offset)) != null) {
69 if (recordNumber == 0 && !ndefRecord.isMessageBegin()) {
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];
82 offset += ndefRecord.getTotalRecordLength();
85 if (recordNumber == 0 || (ndefRecord != null && !ndefRecord.isMessageEnd())) {
89 return smartPosterParsedResult;
92 public String getTitle() {
96 public String getURI() {
100 public int getAction() {
104 public String getDisplayResult() {
108 return title + '\n' + uri;