Add support for tel: URIs
[zxing.git] / core / src / com / google / zxing / client / result / AbstractNDEFParsedResult.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 java.io.UnsupportedEncodingException;
20
21 /**
22  * <p>Superclass for classes encapsulating results in the NDEF format.
23  * See <a href="http://www.nfc-forum.org/specs/">http://www.nfc-forum.org/specs/</a>.</p>
24  *
25  * <p>This code supports a limited subset of NDEF messages, ones that are plausibly
26  * useful in 2D barcode formats. This generally includes 1-record messages, no chunking,
27  * "short record" syntax, no ID field.</p>
28  *
29  * @author srowen@google.com (Sean Owen)
30  */
31 abstract class AbstractNDEFParsedResult extends ParsedReaderResult {
32
33   /**
34    * MB  = 1 (start of record)
35    * ME  = 1 (also end of record)
36    * CF  = 0 (not a chunk)
37    * SR  = 1 (assume short record)
38    * ID  = 0 (ID length field omitted)
39    * TNF = 0 (= 1, well-known type)
40    *       0
41    *       1
42    */
43   private static final int HEADER_VALUE = 0xD1;
44   private static final int MASK = 0xFF;
45
46   AbstractNDEFParsedResult(ParsedReaderResultType type) {
47     super(type);
48   }
49
50   static boolean isMaybeNDEF(byte[] bytes) {
51     return
52         bytes != null &&
53         bytes.length >= 4 &&
54         ((bytes[0] & MASK) == HEADER_VALUE) && 
55         ((bytes[1] & 0xFF) == 1);
56   }
57
58   static String bytesToString(byte[] bytes, int offset, int length, String encoding) {
59     try {
60       return new String(bytes, offset, length, encoding);
61     } catch (UnsupportedEncodingException uee) {
62       throw new RuntimeException("Platform does not support required encoding: " + uee);
63     }
64   }
65
66 }