NdefRecord.java revision dd7341f775ca4d86091f4106d6dfef9214073a7d
1dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly/* 2dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * Copyright (C) 2010 The Android Open Source Project 3dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * 4dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * Licensed under the Apache License, Version 2.0 (the "License"); 5dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * you may not use this file except in compliance with the License. 6dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * You may obtain a copy of the License at 7dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * 8dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * http://www.apache.org/licenses/LICENSE-2.0 9dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * 10dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * Unless required by applicable law or agreed to in writing, software 11dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * distributed under the License is distributed on an "AS IS" BASIS, 12dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * See the License for the specific language governing permissions and 14dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * limitations under the License. 15dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly */ 16dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly 17dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pellypackage android.nfc; 18dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly 19dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pellyimport android.os.Parcel; 20dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pellyimport android.os.Parcelable; 21dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly 22dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pellyimport java.lang.UnsupportedOperationException; 23dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly 24dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly/** 25c9f7890a203a013646650a695157277df81b6a17Scott Main * Represents a logical (unchunked) NDEF (NFC Data Exchange Format) record. 26c9f7890a203a013646650a695157277df81b6a17Scott Main * <p>An NDEF record always contains: 27dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * <ul> 28c9f7890a203a013646650a695157277df81b6a17Scott Main * <li>3-bit TNF (Type Name Format) field: Indicates how to interpret the type field 29c9f7890a203a013646650a695157277df81b6a17Scott Main * <li>Variable length type: Describes the record format 30c9f7890a203a013646650a695157277df81b6a17Scott Main * <li>Variable length ID: A unique identifier for the record 31c9f7890a203a013646650a695157277df81b6a17Scott Main * <li>Variable length payload: The actual data payload 32dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * </ul> 33c9f7890a203a013646650a695157277df81b6a17Scott Main * <p>The underlying record 34dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * representation may be chunked across several NDEF records when the payload is 35dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * large. 36c9f7890a203a013646650a695157277df81b6a17Scott Main * <p>This is an immutable data class. 37dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly */ 38dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pellypublic class NdefRecord implements Parcelable { 39dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly /** 40dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * Indicates no type, id, or payload is associated with this NDEF Record. 41dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * <p> 42dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * Type, id and payload fields must all be empty to be a valid TNF_EMPTY 43dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * record. 44dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly */ 45dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly public static final short TNF_EMPTY = 0x00; 46dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly 47dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly /** 48dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * Indicates the type field uses the RTD type name format. 49dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * <p> 50dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * Use this TNF with RTD types such as RTD_TEXT, RTD_URI. 51dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly */ 52dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly public static final short TNF_WELL_KNOWN = 0x01; 53dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly 54dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly /** 55dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * Indicates the type field contains a value that follows the media-type BNF 56dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * construct defined by RFC 2046. 57dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly */ 58dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly public static final short TNF_MIME_MEDIA = 0x02; 59dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly 60dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly /** 61dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * Indicates the type field contains a value that follows the absolute-URI 62dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * BNF construct defined by RFC 3986. 63dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly */ 64dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly public static final short TNF_ABSOLUTE_URI = 0x03; 65dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly 66dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly /** 67dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * Indicates the type field contains a value that follows the RTD external 68dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * name specification. 69dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * <p> 70dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * Note this TNF should not be used with RTD_TEXT or RTD_URI constants. 71dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * Those are well known RTD constants, not external RTD constants. 72dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly */ 73dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly public static final short TNF_EXTERNAL_TYPE = 0x04; 74dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly 75dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly /** 76dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * Indicates the payload type is unknown. 77dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * <p> 78dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * This is similar to the "application/octet-stream" MIME type. The payload 79dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * type is not explicitly encoded within the NDEF Message. 80dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * <p> 81dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * The type field must be empty to be a valid TNF_UNKNOWN record. 82dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly */ 83dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly public static final short TNF_UNKNOWN = 0x05; 84dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly 85dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly /** 86dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * Indicates the payload is an intermediate or final chunk of a chunked 87dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * NDEF Record. 88dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * <p> 89dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * The payload type is specified in the first chunk, and subsequent chunks 90dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * must use TNF_UNCHANGED with an empty type field. TNF_UNCHANGED must not 91dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * be used in any other situation. 92dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly */ 93dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly public static final short TNF_UNCHANGED = 0x06; 94dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly 95dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly /** 96dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * Reserved TNF type. 97dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * <p> 98dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * The NFC Forum NDEF Specification v1.0 suggests for NDEF parsers to treat this 99dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * value like TNF_UNKNOWN. 100dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * @hide 101dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly */ 102dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly public static final short TNF_RESERVED = 0x07; 103dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly 104dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly /** 105dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * RTD Text type. For use with TNF_WELL_KNOWN. 106dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly */ 107dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly public static final byte[] RTD_TEXT = {0x54}; // "T" 108dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly 109dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly /** 110dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * RTD URI type. For use with TNF_WELL_KNOWN. 111dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly */ 112dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly public static final byte[] RTD_URI = {0x55}; // "U" 113dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly 114dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly /** 115dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * RTD Smart Poster type. For use with TNF_WELL_KNOWN. 116dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly */ 117dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly public static final byte[] RTD_SMART_POSTER = {0x53, 0x70}; // "Sp" 118dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly 119dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly /** 120dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * RTD Alternative Carrier type. For use with TNF_WELL_KNOWN. 121dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly */ 122dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly public static final byte[] RTD_ALTERNATIVE_CARRIER = {0x61, 0x63}; // "ac" 123dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly 124dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly /** 125dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * RTD Handover Carrier type. For use with TNF_WELL_KNOWN. 126dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly */ 127dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly public static final byte[] RTD_HANDOVER_CARRIER = {0x48, 0x63}; // "Hc" 128dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly 129dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly /** 130dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * RTD Handover Request type. For use with TNF_WELL_KNOWN. 131dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly */ 132dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly public static final byte[] RTD_HANDOVER_REQUEST = {0x48, 0x72}; // "Hr" 133dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly 134dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly /** 135dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * RTD Handover Select type. For use with TNF_WELL_KNOWN. 136dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly */ 137dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly public static final byte[] RTD_HANDOVER_SELECT = {0x48, 0x73}; // "Hs" 138dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly 139590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly private static final byte FLAG_MB = (byte) 0x80; 140590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly private static final byte FLAG_ME = (byte) 0x40; 141590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly private static final byte FLAG_CF = (byte) 0x20; 142590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly private static final byte FLAG_SR = (byte) 0x10; 143590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly private static final byte FLAG_IL = (byte) 0x08; 144590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly 145590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly private final byte mFlags; 146590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly private final short mTnf; 147590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly private final byte[] mType; 148590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly private final byte[] mId; 149590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly private final byte[] mPayload; 150590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly 151dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly /** 152dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * Construct an NDEF Record. 153dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * <p> 154dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * Applications should not attempt to manually chunk NDEF Records - the 155dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * implementation of android.nfc will automatically chunk an NDEF Record 156dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * when necessary (and only present a single logical NDEF Record to the 157dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * application). So applications should not use TNF_UNCHANGED. 158dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * 159dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * @param tnf a 3-bit TNF constant 160dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * @param type byte array, containing zero to 255 bytes, must not be null 161dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * @param id byte array, containing zero to 255 bytes, must not be null 162dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * @param payload byte array, containing zero to (2 ** 32 - 1) bytes, 163dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * must not be null 164dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly */ 165dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly public NdefRecord(short tnf, byte[] type, byte[] id, byte[] payload) { 166590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly /* check arguments */ 167590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly if ((type == null) || (id == null) || (payload == null)) { 168590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly throw new IllegalArgumentException("Illegal null argument"); 169590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly } 170590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly 1716df23609d4c1f257b11d67b965e5b63cbdf26fcdNick Kralevich if (tnf < 0 || tnf > 0x07) { 1726df23609d4c1f257b11d67b965e5b63cbdf26fcdNick Kralevich throw new IllegalArgumentException("TNF out of range " + tnf); 1736df23609d4c1f257b11d67b965e5b63cbdf26fcdNick Kralevich } 1746df23609d4c1f257b11d67b965e5b63cbdf26fcdNick Kralevich 175590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly /* generate flag */ 176590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly byte flags = FLAG_MB | FLAG_ME; 177590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly 178590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly /* Determine if it is a short record */ 179590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly if(payload.length < 0xFF) { 180590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly flags |= FLAG_SR; 181590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly } 182590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly 183590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly /* Determine if an id is present */ 184590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly if(id.length != 0) { 185590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly flags |= FLAG_IL; 186590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly } 187590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly 188590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly mFlags = flags; 189590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly mTnf = tnf; 190590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly mType = type.clone(); 191590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly mId = id.clone(); 192590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly mPayload = payload.clone(); 193dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly } 194dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly 195dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly /** 196dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * Construct an NDEF Record from raw bytes. 197dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * <p> 198dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * Validation is performed to make sure the header is valid, and that 199dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * the id, type and payload sizes appear to be valid. 200dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * 201dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * @throws FormatException if the data is not a valid NDEF record 202dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly */ 203dd7341f775ca4d86091f4106d6dfef9214073a7dSylvain Fonteneau public NdefRecord(byte[] data) throws FormatException { 204dd7341f775ca4d86091f4106d6dfef9214073a7dSylvain Fonteneau /* Prevent compiler to complain about unassigned final fields */ 205dd7341f775ca4d86091f4106d6dfef9214073a7dSylvain Fonteneau mFlags = 0; 206dd7341f775ca4d86091f4106d6dfef9214073a7dSylvain Fonteneau mTnf = 0; 207dd7341f775ca4d86091f4106d6dfef9214073a7dSylvain Fonteneau mType = null; 208dd7341f775ca4d86091f4106d6dfef9214073a7dSylvain Fonteneau mId = null; 209dd7341f775ca4d86091f4106d6dfef9214073a7dSylvain Fonteneau mPayload = null; 210dd7341f775ca4d86091f4106d6dfef9214073a7dSylvain Fonteneau /* Perform actual parsing */ 211dd7341f775ca4d86091f4106d6dfef9214073a7dSylvain Fonteneau if (parseNdefRecord(data) == -1) { 212dd7341f775ca4d86091f4106d6dfef9214073a7dSylvain Fonteneau throw new FormatException("Error while parsing NDEF record"); 213dd7341f775ca4d86091f4106d6dfef9214073a7dSylvain Fonteneau } 214dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly } 215dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly 216dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly /** 217dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * Returns the 3-bit TNF. 218dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * <p> 219dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * TNF is the top-level type. 220dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly */ 221dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly public short getTnf() { 222590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly return mTnf; 223dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly } 224dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly 225dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly /** 226dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * Returns the variable length Type field. 227dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * <p> 228dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * This should be used in conjunction with the TNF field to determine the 229dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * payload format. 230dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly */ 231dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly public byte[] getType() { 232590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly return mType.clone(); 233dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly } 234dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly 235dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly /** 236dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * Returns the variable length ID. 237dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly */ 238dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly public byte[] getId() { 239590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly return mId.clone(); 240dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly } 241dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly 242dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly /** 243dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * Returns the variable length payload. 244dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly */ 245dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly public byte[] getPayload() { 246590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly return mPayload.clone(); 247dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly } 248dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly 249dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly /** 250dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * Return this NDEF Record as a byte array. 251dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly * @hide 252dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly */ 253dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly public byte[] toByteArray() { 254590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly return generate(mFlags, mTnf, mType, mId, mPayload); 255dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly } 256dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly 257dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly public int describeContents() { 258dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly return 0; 259dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly } 260dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly 261dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly public void writeToParcel(Parcel dest, int flags) { 262590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly dest.writeInt(mTnf); 263590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly dest.writeInt(mType.length); 264590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly dest.writeByteArray(mType); 265590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly dest.writeInt(mId.length); 266590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly dest.writeByteArray(mId); 267590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly dest.writeInt(mPayload.length); 268590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly dest.writeByteArray(mPayload); 269dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly } 270dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly 271dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly public static final Parcelable.Creator<NdefRecord> CREATOR = 272dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly new Parcelable.Creator<NdefRecord>() { 273dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly public NdefRecord createFromParcel(Parcel in) { 274590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly short tnf = (short)in.readInt(); 275590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly int typeLength = in.readInt(); 276590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly byte[] type = new byte[typeLength]; 277590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly in.readByteArray(type); 278590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly int idLength = in.readInt(); 279590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly byte[] id = new byte[idLength]; 280590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly in.readByteArray(id); 281590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly int payloadLength = in.readInt(); 282590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly byte[] payload = new byte[payloadLength]; 283590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly in.readByteArray(payload); 284590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly 285590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly return new NdefRecord(tnf, type, id, payload); 286dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly } 287dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly public NdefRecord[] newArray(int size) { 288590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly return new NdefRecord[size]; 289dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly } 290dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly }; 291590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly 292dd7341f775ca4d86091f4106d6dfef9214073a7dSylvain Fonteneau private native int parseNdefRecord(byte[] data); 293590b73bc5b8e5f7b59bff1d9264a52388a5162e6Nick Pelly private native byte[] generate(short flags, short tnf, byte[] type, byte[] id, byte[] data); 294dc993791fc3cf7a270921f7419b0c6b875bbd92bNick Pelly}