NdefRecord.java revision dc993791fc3cf7a270921f7419b0c6b875bbd92b
1/*
2 * Copyright (C) 2010 The Android Open Source Project
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
17package android.nfc;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21
22import java.lang.UnsupportedOperationException;
23
24/**
25 * NDEF Record data.
26 * <p>
27 * Immutable data class. An NDEF record always contains
28 * <ul>
29 * <li>3-bit TNF field
30 * <li>Variable length type
31 * <li>Variable length ID
32 * <li>Variable length payload
33 * </ul>
34 * The TNF (Type Name Format) field indicates how to interpret the type field.
35 * <p>
36 * This class represents a logical (unchunked) NDEF record. The underlying
37 * representation may be chunked across several NDEF records when the payload is
38 * large.
39 */
40public class NdefRecord implements Parcelable {
41    /**
42     * Indicates no type, id, or payload is associated with this NDEF Record.
43     * <p>
44     * Type, id and payload fields must all be empty to be a valid TNF_EMPTY
45     * record.
46     */
47    public static final short TNF_EMPTY = 0x00;
48
49    /**
50     * Indicates the type field uses the RTD type name format.
51     * <p>
52     * Use this TNF with RTD types such as RTD_TEXT, RTD_URI.
53     */
54    public static final short TNF_WELL_KNOWN = 0x01;
55
56    /**
57     * Indicates the type field contains a value that follows the media-type BNF
58     * construct defined by RFC 2046.
59     */
60    public static final short TNF_MIME_MEDIA = 0x02;
61
62    /**
63     * Indicates the type field contains a value that follows the absolute-URI
64     * BNF construct defined by RFC 3986.
65     */
66    public static final short TNF_ABSOLUTE_URI = 0x03;
67
68    /**
69     * Indicates the type field contains a value that follows the RTD external
70     * name specification.
71     * <p>
72     * Note this TNF should not be used with RTD_TEXT or RTD_URI constants.
73     * Those are well known RTD constants, not external RTD constants.
74     */
75    public static final short TNF_EXTERNAL_TYPE = 0x04;
76
77    /**
78     * Indicates the payload type is unknown.
79     * <p>
80     * This is similar to the "application/octet-stream" MIME type. The payload
81     * type is not explicitly encoded within the NDEF Message.
82     * <p>
83     * The type field must be empty to be a valid TNF_UNKNOWN record.
84     */
85    public static final short TNF_UNKNOWN = 0x05;
86
87    /**
88     * Indicates the payload is an intermediate or final chunk of a chunked
89     * NDEF Record.
90     * <p>
91     * The payload type is specified in the first chunk, and subsequent chunks
92     * must use TNF_UNCHANGED with an empty type field. TNF_UNCHANGED must not
93     * be used in any other situation.
94     */
95    public static final short TNF_UNCHANGED = 0x06;
96
97    /**
98     * Reserved TNF type.
99     * <p>
100     * The NFC Forum NDEF Specification v1.0 suggests for NDEF parsers to treat this
101     * value like TNF_UNKNOWN.
102     * @hide
103     */
104    public static final short TNF_RESERVED = 0x07;
105
106    /**
107     * RTD Text type. For use with TNF_WELL_KNOWN.
108     */
109    public static final byte[] RTD_TEXT = {0x54};  // "T"
110
111    /**
112     * RTD URI type. For use with TNF_WELL_KNOWN.
113     */
114    public static final byte[] RTD_URI = {0x55};   // "U"
115
116    /**
117     * RTD Smart Poster type. For use with TNF_WELL_KNOWN.
118     */
119    public static final byte[] RTD_SMART_POSTER = {0x53, 0x70};  // "Sp"
120
121    /**
122     * RTD Alternative Carrier type. For use with TNF_WELL_KNOWN.
123     */
124    public static final byte[] RTD_ALTERNATIVE_CARRIER = {0x61, 0x63};  // "ac"
125
126    /**
127     * RTD Handover Carrier type. For use with TNF_WELL_KNOWN.
128     */
129    public static final byte[] RTD_HANDOVER_CARRIER = {0x48, 0x63};  // "Hc"
130
131    /**
132     * RTD Handover Request type. For use with TNF_WELL_KNOWN.
133     */
134    public static final byte[] RTD_HANDOVER_REQUEST = {0x48, 0x72};  // "Hr"
135
136    /**
137     * RTD Handover Select type. For use with TNF_WELL_KNOWN.
138     */
139    public static final byte[] RTD_HANDOVER_SELECT = {0x48, 0x73}; // "Hs"
140
141    /**
142     * Construct an NDEF Record.
143     * <p>
144     * Applications should not attempt to manually chunk NDEF Records - the
145     * implementation of android.nfc will automatically chunk an NDEF Record
146     * when necessary (and only present a single logical NDEF Record to the
147     * application). So applications should not use TNF_UNCHANGED.
148     *
149     * @param tnf  a 3-bit TNF constant
150     * @param type byte array, containing zero to 255 bytes, must not be null
151     * @param id   byte array, containing zero to 255 bytes, must not be null
152     * @param payload byte array, containing zero to (2 ** 32 - 1) bytes,
153     *                must not be null
154     */
155    public NdefRecord(short tnf, byte[] type, byte[] id, byte[] payload) {
156        throw new UnsupportedOperationException();
157    }
158
159    /**
160     * Construct an NDEF Record from raw bytes.
161     * <p>
162     * Validation is performed to make sure the header is valid, and that
163     * the id, type and payload sizes appear to be valid.
164     *
165     * @throws FormatException if the data is not a valid NDEF record
166     */
167    public NdefRecord(byte[] data) {
168        throw new UnsupportedOperationException();
169    }
170
171    /**
172     * Returns the 3-bit TNF.
173     * <p>
174     * TNF is the top-level type.
175     */
176    public short getTnf() {
177        throw new UnsupportedOperationException();
178    }
179
180    /**
181     * Returns the variable length Type field.
182     * <p>
183     * This should be used in conjunction with the TNF field to determine the
184     * payload format.
185     */
186    public byte[] getType() {
187        throw new UnsupportedOperationException();
188    }
189
190    /**
191     * Returns the variable length ID.
192     */
193    public byte[] getId() {
194        throw new UnsupportedOperationException();
195    }
196
197    /**
198     * Returns the variable length payload.
199     */
200    public byte[] getPayload() {
201        throw new UnsupportedOperationException();
202    }
203
204    /**
205     * Return this NDEF Record as a byte array.
206     * @hide
207     */
208    public byte[] toByteArray() {
209        throw new UnsupportedOperationException();
210    }
211
212    @Override
213    public int describeContents() {
214        return 0;
215    }
216
217    @Override
218    public void writeToParcel(Parcel dest, int flags) {
219        throw new UnsupportedOperationException();
220    }
221
222    public static final Parcelable.Creator<NdefRecord> CREATOR =
223            new Parcelable.Creator<NdefRecord>() {
224        public NdefRecord createFromParcel(Parcel in) {
225            throw new UnsupportedOperationException();
226        }
227        public NdefRecord[] newArray(int size) {
228            throw new UnsupportedOperationException();
229        }
230    };
231}