blob: c79fabf5b054afc22c03e76a849db2ba517e60a1 [file] [log] [blame]
Nick Pellydc993792010-10-04 11:17:25 -07001/*
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
Nick Pellydc993792010-10-04 11:17:25 -070019import android.os.Parcel;
20import android.os.Parcelable;
21
Nick Pellydc993792010-10-04 11:17:25 -070022/**
Scott Mainc9f78902010-10-15 09:15:09 -070023 * Represents an NDEF (NFC Data Exchange Format) data message that contains one or more {@link
24 * NdefRecord}s.
25 * <p>An NDEF message includes "records" that can contain different sets of data, such as
26 * MIME-type media, a URI, or one of the supported RTD types (see {@link NdefRecord}). An NDEF
27 * message always contains zero or more NDEF records.</p>
28 * <p>This is an immutable data class.
Nick Pellydc993792010-10-04 11:17:25 -070029 */
Nick Pelly11b075e2010-10-28 13:39:37 -070030public final class NdefMessage implements Parcelable {
Nick Pelly590b73b2010-10-12 13:00:50 -070031 private static final byte FLAG_MB = (byte) 0x80;
32 private static final byte FLAG_ME = (byte) 0x40;
33
34 private final NdefRecord[] mRecords;
35
Nick Pellydc993792010-10-04 11:17:25 -070036 /**
37 * Create an NDEF message from raw bytes.
38 * <p>
39 * Validation is performed to make sure the Record format headers are valid,
40 * and the ID + TYPE + PAYLOAD fields are of the correct size.
Nick Pelly590b73b2010-10-12 13:00:50 -070041 * @throws FormatException
Nick Pellydc993792010-10-04 11:17:25 -070042 */
Nick Pelly590b73b2010-10-12 13:00:50 -070043 public NdefMessage(byte[] data) throws FormatException {
44 mRecords = null; // stop compiler complaints about final field
45 if (parseNdefMessage(data) == -1) {
46 throw new FormatException("Error while parsing NDEF message");
47 }
Nick Pellydc993792010-10-04 11:17:25 -070048 }
49
50 /**
51 * Create an NDEF message from NDEF records.
52 */
53 public NdefMessage(NdefRecord[] records) {
Nick Pelly590b73b2010-10-12 13:00:50 -070054 mRecords = new NdefRecord[records.length];
55 System.arraycopy(records, 0, mRecords, 0, records.length);
Nick Pellydc993792010-10-04 11:17:25 -070056 }
57
58 /**
59 * Get the NDEF records inside this NDEF message.
60 *
61 * @return array of zero or more NDEF records.
62 */
63 public NdefRecord[] getRecords() {
Nick Pelly590b73b2010-10-12 13:00:50 -070064 return mRecords.clone();
Nick Pellydc993792010-10-04 11:17:25 -070065 }
66
67 /**
Jeff Hamiltonda83f512010-10-21 22:39:30 -050068 * Returns a byte array representation of this entire NDEF message.
Nick Pellydc993792010-10-04 11:17:25 -070069 */
70 public byte[] toByteArray() {
Nick Pelly11b075e2010-10-28 13:39:37 -070071 //TODO: allocate the byte array once, copy each record once
72 //TODO: process MB and ME flags outside loop
Nick Pelly590b73b2010-10-12 13:00:50 -070073 if ((mRecords == null) || (mRecords.length == 0))
Jason parks3ebd59b2010-11-02 20:03:52 -050074 return new byte[0];
Nick Pelly590b73b2010-10-12 13:00:50 -070075
76 byte[] msg = {};
77
78 for (int i = 0; i < mRecords.length; i++) {
79 byte[] record = mRecords[i].toByteArray();
80 byte[] tmp = new byte[msg.length + record.length];
81
82 /* Make sure the Message Begin flag is set only for the first record */
83 if (i == 0) {
84 record[0] |= FLAG_MB;
85 } else {
86 record[0] &= ~FLAG_MB;
87 }
88
89 /* Make sure the Message End flag is set only for the last record */
90 if (i == (mRecords.length - 1)) {
91 record[0] |= FLAG_ME;
92 } else {
93 record[0] &= ~FLAG_ME;
94 }
95
96 System.arraycopy(msg, 0, tmp, 0, msg.length);
97 System.arraycopy(record, 0, tmp, msg.length, record.length);
98
99 msg = tmp;
100 }
101
102 return msg;
Nick Pellydc993792010-10-04 11:17:25 -0700103 }
104
Jason parks3ebd59b2010-11-02 20:03:52 -0500105 @Override
Nick Pellydc993792010-10-04 11:17:25 -0700106 public int describeContents() {
107 return 0;
108 }
109
Jason parks3ebd59b2010-11-02 20:03:52 -0500110 @Override
Nick Pellydc993792010-10-04 11:17:25 -0700111 public void writeToParcel(Parcel dest, int flags) {
Nick Pelly590b73b2010-10-12 13:00:50 -0700112 dest.writeInt(mRecords.length);
113 dest.writeTypedArray(mRecords, flags);
Nick Pellydc993792010-10-04 11:17:25 -0700114 }
115
116 public static final Parcelable.Creator<NdefMessage> CREATOR =
117 new Parcelable.Creator<NdefMessage>() {
Jason parks3ebd59b2010-11-02 20:03:52 -0500118 @Override
Nick Pellydc993792010-10-04 11:17:25 -0700119 public NdefMessage createFromParcel(Parcel in) {
Nick Pelly590b73b2010-10-12 13:00:50 -0700120 int recordsLength = in.readInt();
121 NdefRecord[] records = new NdefRecord[recordsLength];
122 in.readTypedArray(records, NdefRecord.CREATOR);
123 return new NdefMessage(records);
Nick Pellydc993792010-10-04 11:17:25 -0700124 }
Jason parks3ebd59b2010-11-02 20:03:52 -0500125 @Override
Nick Pellydc993792010-10-04 11:17:25 -0700126 public NdefMessage[] newArray(int size) {
Nick Pelly590b73b2010-10-12 13:00:50 -0700127 return new NdefMessage[size];
Nick Pellydc993792010-10-04 11:17:25 -0700128 }
129 };
Nick Pelly590b73b2010-10-12 13:00:50 -0700130
131 private native int parseNdefMessage(byte[] data);
Nick Pellydc993792010-10-04 11:17:25 -0700132}