Tag.java revision 6be655c768a82716612c00fdd156254d8dc00f42
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.nfc.technology.IsoDep;
20import android.nfc.technology.MifareClassic;
21import android.nfc.technology.NfcV;
22import android.nfc.technology.Ndef;
23import android.nfc.technology.NfcA;
24import android.nfc.technology.NfcB;
25import android.nfc.technology.NfcF;
26import android.nfc.technology.TagTechnology;
27import android.os.Bundle;
28import android.os.Parcel;
29import android.os.Parcelable;
30import android.os.RemoteException;
31
32import java.util.Arrays;
33
34/**
35 * Represents a (generic) discovered tag.
36 * <p>
37 * A tag is a passive NFC element, such as NFC Forum Tag's, Mifare class Tags,
38 * Sony Felica Tags.
39 * <p>
40 * Tag's have a type and usually have a UID.
41 * <p>
42 * {@link Tag} objects are passed to applications via the {@link NfcAdapter#EXTRA_TAG} extra
43 * in {@link NfcAdapter#ACTION_TAG_DISCOVERED} intents. A {@link Tag} object is immutable
44 * and represents the state of the tag at the time of discovery. It can be
45 * directly queried for its UID and Type, or used to create a {@link TagTechnology}
46 * (with {@link Tag#getTechnology(int)}).
47 * <p>
48 * A {@link Tag} can  be used to create a {@link TagTechnology} only while the tag is in
49 * range. If it is removed and then returned to range, then the most recent
50 * {@link Tag} object (in {@link NfcAdapter#ACTION_TAG_DISCOVERED}) should be used to create a
51 * {@link TagTechnology}.
52 * <p>This is an immutable data class. All properties are set at Tag discovery
53 * time and calls on this class will retrieve those read-only properties, and
54 * not cause any further RF activity or block. Note however that arrays passed to and
55 * returned by this class are *not* cloned, so be careful not to modify them.
56 * @hide
57 */
58public class Tag implements Parcelable {
59    /*package*/ final byte[] mId;
60    /*package*/ final int[] mTechList;
61    /*package*/ final Bundle[] mTechExtras;
62    /*package*/ final int mServiceHandle;  // for use by NFC service, 0 indicates a mock
63
64    /**
65     * Hidden constructor to be used by NFC service and internal classes.
66     * @hide
67     */
68    public Tag(byte[] id, int[] techList, Bundle[] techListExtras, int serviceHandle) {
69        if (techList == null) {
70            throw new IllegalArgumentException("rawTargets cannot be null");
71        }
72        mId = id;
73        mTechList = Arrays.copyOf(techList, techList.length);
74        Arrays.sort(mTechList);
75        // Ensure mTechExtras is as long as mTechList
76        mTechExtras = Arrays.copyOf(techListExtras, techList.length);
77        mServiceHandle = serviceHandle;
78    }
79
80    /**
81     * Construct a mock Tag.
82     * <p>This is an application constructed tag, so NfcAdapter methods on this
83     * Tag such as {@link #getTechnology} may fail with
84     * {@link IllegalArgumentException} since it does not represent a physical Tag.
85     * <p>This constructor might be useful for mock testing.
86     * @param id The tag identifier, can be null
87     * @param techList must not be null
88     * @return freshly constructed tag
89     */
90    public static Tag createMockTag(byte[] id, int[] techList, Bundle[] techListExtras) {
91        // set serviceHandle to 0 to indicate mock tag
92        return new Tag(id, techList, techListExtras, 0);
93    }
94
95    /**
96     * For use by NfcService only.
97     * @hide
98     */
99    public int getServiceHandle() {
100        return mServiceHandle;
101    }
102
103    /**
104     * Get the Tag Identifier (if it has one).
105     * <p>Tag ID is usually a serial number for the tag.
106     *
107     * @return ID, or null if it does not exist
108     */
109    public byte[] getId() {
110        return mId;
111    }
112
113    /**
114     * Returns technologies present in the tag that this implementation understands,
115     * or a zero length array if there are no supported technologies on this tag.
116     */
117    public int[] getTechnologyList() {
118        return Arrays.copyOf(mTechList, mTechList.length);
119    }
120
121    /**
122     * Returns the technology, or null if not present
123     */
124    public TagTechnology getTechnology(int tech) {
125        int pos = Arrays.binarySearch(mTechList, tech);
126        if (pos < 0) {
127            return null;
128        }
129
130        Bundle extras = mTechExtras[pos];
131        NfcAdapter adapter = null;
132        try {
133            switch (tech) {
134                case TagTechnology.NFC_A: {
135                    return new NfcA(adapter, this, extras);
136                }
137                case TagTechnology.NFC_B: {
138                    return new NfcB(adapter, this, extras);
139                }
140                case TagTechnology.ISO_DEP: {
141                    return new IsoDep(adapter, this, extras);
142                }
143                case TagTechnology.NFC_V: {
144                    return new NfcV(adapter, this, extras);
145                }
146                case TagTechnology.TYPE_1:
147                case TagTechnology.TYPE_2:
148                case TagTechnology.TYPE_3:
149                case TagTechnology.TYPE_4: {
150                    return new Ndef(adapter, this, tech, extras);
151                }
152                case TagTechnology.NFC_F: {
153                    return new NfcF(adapter, this, extras);
154                }
155                case TagTechnology.MIFARE_CLASSIC: {
156                    return new MifareClassic(adapter, this, extras);
157                }
158
159                default: {
160                    throw new UnsupportedOperationException("Tech " + tech + " not supported");
161                }
162            }
163        } catch (RemoteException e) {
164            return null;
165        }
166    }
167
168    @Override
169    public String toString() {
170        StringBuilder sb = new StringBuilder("TAG ")
171            .append("uid = ")
172            .append(mId)
173            .append(" Tech [");
174        for (int i : mTechList) {
175            sb.append(i)
176            .append(", ");
177        }
178        return sb.toString();
179    }
180
181    /*package*/ static byte[] readBytesWithNull(Parcel in) {
182        int len = in.readInt();
183        byte[] result = null;
184        if (len >= 0) {
185            result = new byte[len];
186            in.readByteArray(result);
187        }
188        return result;
189    }
190
191    /*package*/ static void writeBytesWithNull(Parcel out, byte[] b) {
192        if (b == null) {
193            out.writeInt(-1);
194            return;
195        }
196        out.writeInt(b.length);
197        out.writeByteArray(b);
198    }
199
200    @Override
201    public int describeContents() {
202        return 0;
203    }
204
205    @Override
206    public void writeToParcel(Parcel dest, int flags) {
207        writeBytesWithNull(dest, mId);
208        dest.writeInt(mTechList.length);
209        dest.writeIntArray(mTechList);
210        dest.writeTypedArray(mTechExtras, 0);
211        dest.writeInt(mServiceHandle);
212    }
213
214    public static final Parcelable.Creator<Tag> CREATOR =
215            new Parcelable.Creator<Tag>() {
216        @Override
217        public Tag createFromParcel(Parcel in) {
218            // Tag fields
219            byte[] id = Tag.readBytesWithNull(in);
220            int[] techList = new int[in.readInt()];
221            in.readIntArray(techList);
222            Bundle[] techExtras = in.createTypedArray(Bundle.CREATOR);
223            int serviceHandle = in.readInt();
224
225            return new Tag(id, techList, techExtras, serviceHandle);
226        }
227
228        @Override
229        public Tag[] newArray(int size) {
230            return new Tag[size];
231        }
232    };
233}