Skip to content

Commit dc54a8c

Browse files
jhamAndroid (Google) Code Review
authored andcommitted
Merge "API changes for NFC." into gingerbread
2 parents a314f32 + 4e21e1d commit dc54a8c

File tree

15 files changed

+381
-297
lines changed

15 files changed

+381
-297
lines changed

api/current.xml

Lines changed: 129 additions & 137 deletions
Large diffs are not rendered by default.

core/java/android/nfc/NfcAdapter.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import android.content.IntentFilter;
2727
import android.content.pm.IPackageManager;
2828
import android.content.pm.PackageManager;
29-
import android.nfc.technology.TagTechnology;
29+
import android.nfc.tech.TagTechnology;
3030
import android.os.IBinder;
3131
import android.os.RemoteException;
3232
import android.os.ServiceManager;
@@ -531,16 +531,6 @@ void disableForegroundNdefPushInternal(Activity activity, boolean force) {
531531
}
532532
}
533533

534-
/**
535-
* Retrieve a TagTechnology object used to interact with a Tag that is
536-
* in field.
537-
* <p>
538-
* @return TagTechnology object, or null if not present
539-
*/
540-
public TagTechnology getTechnology(Tag tag, int tech) {
541-
return tag.getTechnology(NfcAdapter.this, tech);
542-
}
543-
544534
/**
545535
* Set the NDEF Message that this NFC adapter should appear as to Tag
546536
* readers.

core/java/android/nfc/NfcSecureElement.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package android.nfc;
1818

19-
import android.nfc.technology.TagTechnology;
19+
import android.nfc.tech.TagTechnology;
2020
import android.os.RemoteException;
2121
import android.util.Log;
2222

core/java/android/nfc/Tag.java

Lines changed: 28 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,10 @@
1616

1717
package android.nfc;
1818

19-
import android.nfc.technology.IsoDep;
20-
import android.nfc.technology.MifareClassic;
21-
import android.nfc.technology.MifareUltralight;
22-
import android.nfc.technology.NfcV;
23-
import android.nfc.technology.Ndef;
24-
import android.nfc.technology.NdefFormatable;
25-
import android.nfc.technology.NfcA;
26-
import android.nfc.technology.NfcB;
27-
import android.nfc.technology.NfcF;
28-
import android.nfc.technology.TagTechnology;
19+
import android.nfc.tech.TagTechnology;
2920
import android.os.Bundle;
3021
import android.os.Parcel;
3122
import android.os.Parcelable;
32-
import android.os.RemoteException;
3323

3424
import java.util.Arrays;
3525

@@ -44,8 +34,8 @@
4434
* {@link Tag} objects are passed to applications via the {@link NfcAdapter#EXTRA_TAG} extra
4535
* in {@link NfcAdapter#ACTION_TAG_DISCOVERED} intents. A {@link Tag} object is immutable
4636
* and represents the state of the tag at the time of discovery. It can be
47-
* directly queried for its UID and Type, or used to create a {@link TagTechnology}
48-
* (with {@link NfcAdapter#getTechnology}).
37+
* directly queried for its UID and Type, or used to create a {@link TagTechnology} using the
38+
* static <code>get()</code> methods on the varios tech classes.
4939
* <p>
5040
* A {@link Tag} can be used to create a {@link TagTechnology} only while the tag is in
5141
* range. If it is removed and then returned to range, then the most recent
@@ -61,14 +51,16 @@ public class Tag implements Parcelable {
6151
/*package*/ final int[] mTechList;
6252
/*package*/ final Bundle[] mTechExtras;
6353
/*package*/ final int mServiceHandle; // for use by NFC service, 0 indicates a mock
54+
/*package*/ final INfcTag mTagService;
6455

6556
/*package*/ int mConnectedTechnology;
6657

6758
/**
6859
* Hidden constructor to be used by NFC service and internal classes.
6960
* @hide
7061
*/
71-
public Tag(byte[] id, int[] techList, Bundle[] techListExtras, int serviceHandle) {
62+
public Tag(byte[] id, int[] techList, Bundle[] techListExtras, int serviceHandle,
63+
INfcTag tagService) {
7264
if (techList == null) {
7365
throw new IllegalArgumentException("rawTargets cannot be null");
7466
}
@@ -77,23 +69,23 @@ public Tag(byte[] id, int[] techList, Bundle[] techListExtras, int serviceHandle
7769
// Ensure mTechExtras is as long as mTechList
7870
mTechExtras = Arrays.copyOf(techListExtras, techList.length);
7971
mServiceHandle = serviceHandle;
72+
mTagService = tagService;
8073

8174
mConnectedTechnology = -1;
8275
}
8376

8477
/**
8578
* Construct a mock Tag.
86-
* <p>This is an application constructed tag, so NfcAdapter methods on this
87-
* Tag such as {@link NfcAdapter#getTechnology} may fail with
88-
* {@link IllegalArgumentException} since it does not represent a physical Tag.
79+
* <p>This is an application constructed tag, so NfcAdapter methods on this Tag may fail
80+
* with {@link IllegalArgumentException} since it does not represent a physical Tag.
8981
* <p>This constructor might be useful for mock testing.
9082
* @param id The tag identifier, can be null
9183
* @param techList must not be null
9284
* @return freshly constructed tag
9385
*/
9486
public static Tag createMockTag(byte[] id, int[] techList, Bundle[] techListExtras) {
9587
// set serviceHandle to 0 to indicate mock tag
96-
return new Tag(id, techList, techListExtras, 0);
88+
return new Tag(id, techList, techListExtras, 0, null);
9789
}
9890

9991
/**
@@ -127,7 +119,16 @@ public int[] getTechnologyList() {
127119
return Arrays.copyOf(mTechList, mTechList.length);
128120
}
129121

130-
/*package*/ TagTechnology getTechnology(NfcAdapter adapter, int tech) {
122+
/** @hide */
123+
public boolean hasTech(int techType) {
124+
for (int tech : mTechList) {
125+
if (tech == techType) return true;
126+
}
127+
return false;
128+
}
129+
130+
/** @hide */
131+
public Bundle getTechExtras(int tech) {
131132
int pos = -1;
132133
for (int idx = 0; idx < mTechList.length; idx++) {
133134
if (mTechList[idx] == tech) {
@@ -139,44 +140,12 @@ public int[] getTechnologyList() {
139140
return null;
140141
}
141142

142-
Bundle extras = mTechExtras[pos];
143-
try {
144-
switch (tech) {
145-
case TagTechnology.NFC_A: {
146-
return new NfcA(adapter, this, extras);
147-
}
148-
case TagTechnology.NFC_B: {
149-
return new NfcB(adapter, this, extras);
150-
}
151-
case TagTechnology.ISO_DEP: {
152-
return new IsoDep(adapter, this, extras);
153-
}
154-
case TagTechnology.NFC_V: {
155-
return new NfcV(adapter, this, extras);
156-
}
157-
case TagTechnology.NDEF: {
158-
return new Ndef(adapter, this, tech, extras);
159-
}
160-
case TagTechnology.NDEF_FORMATABLE: {
161-
return new NdefFormatable(adapter, this, tech, extras);
162-
}
163-
case TagTechnology.NFC_F: {
164-
return new NfcF(adapter, this, extras);
165-
}
166-
case TagTechnology.MIFARE_CLASSIC: {
167-
return new MifareClassic(adapter, this, extras);
168-
}
169-
case TagTechnology.MIFARE_ULTRALIGHT: {
170-
return new MifareUltralight(adapter, this, extras);
171-
}
143+
return mTechExtras[pos];
144+
}
172145

173-
default: {
174-
throw new UnsupportedOperationException("Tech " + tech + " not supported");
175-
}
176-
}
177-
} catch (RemoteException e) {
178-
return null;
179-
}
146+
/** @hide */
147+
public INfcTag getTagService() {
148+
return mTagService;
180149
}
181150

182151
@Override
@@ -223,6 +192,7 @@ public void writeToParcel(Parcel dest, int flags) {
223192
dest.writeIntArray(mTechList);
224193
dest.writeTypedArray(mTechExtras, 0);
225194
dest.writeInt(mServiceHandle);
195+
dest.writeStrongBinder(mTagService.asBinder());
226196
}
227197

228198
public static final Parcelable.Creator<Tag> CREATOR =
@@ -235,8 +205,9 @@ public Tag createFromParcel(Parcel in) {
235205
in.readIntArray(techList);
236206
Bundle[] techExtras = in.createTypedArray(Bundle.CREATOR);
237207
int serviceHandle = in.readInt();
208+
INfcTag tagService = INfcTag.Stub.asInterface(in.readStrongBinder());
238209

239-
return new Tag(id, techList, techExtras, serviceHandle);
210+
return new Tag(id, techList, techExtras, serviceHandle, tagService);
240211
}
241212

242213
@Override

core/java/android/nfc/technology/BasicTagTechnology.java renamed to core/java/android/nfc/tech/BasicTagTechnology.java

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,17 @@
1414
* limitations under the License.
1515
*/
1616

17-
package android.nfc.technology;
17+
package android.nfc.tech;
1818

19-
import java.io.IOException;
20-
21-
import android.nfc.INfcAdapter;
22-
import android.nfc.INfcTag;
23-
import android.nfc.NfcAdapter;
24-
import android.nfc.TransceiveResult;
25-
import android.nfc.Tag;
2619
import android.nfc.ErrorCodes;
20+
import android.nfc.Tag;
2721
import android.nfc.TagLostException;
22+
import android.nfc.TransceiveResult;
2823
import android.os.RemoteException;
2924
import android.util.Log;
3025

26+
import java.io.IOException;
27+
3128
/**
3229
* A base class for tag technologies that are built on top of transceive().
3330
*/
@@ -37,11 +34,8 @@
3734
/*package*/ final Tag mTag;
3835
/*package*/ boolean mIsConnected;
3936
/*package*/ int mSelectedTechnology;
40-
private final NfcAdapter mAdapter;
41-
/*package*/ final INfcAdapter mService;
42-
/*package*/ final INfcTag mTagService;
4337

44-
BasicTagTechnology(NfcAdapter adapter, Tag tag, int tech) throws RemoteException {
38+
BasicTagTechnology(Tag tag, int tech) throws RemoteException {
4539
int[] techList = tag.getTechnologyList();
4640
int i;
4741

@@ -56,15 +50,12 @@
5650
throw new IllegalArgumentException("Technology " + tech + " not present on tag " + tag);
5751
}
5852

59-
mAdapter = adapter;
60-
mService = mAdapter.getService();
61-
mTagService = mAdapter.getTagService();
6253
mTag = tag;
6354
mSelectedTechnology = tech;
6455
}
6556

66-
BasicTagTechnology(NfcAdapter adapter, Tag tag) throws RemoteException {
67-
this(adapter, tag, tag.getTechnologyList()[0]);
57+
BasicTagTechnology(Tag tag) throws RemoteException {
58+
this(tag, tag.getTechnologyList()[0]);
6859
}
6960

7061
@Override
@@ -100,7 +91,7 @@ public boolean isConnected() {
10091
}
10192

10293
try {
103-
return mTagService.isPresent(mTag.getServiceHandle());
94+
return mTag.getTagService().isPresent(mTag.getServiceHandle());
10495
} catch (RemoteException e) {
10596
Log.e(TAG, "NFC service dead", e);
10697
return false;
@@ -110,7 +101,7 @@ public boolean isConnected() {
110101
@Override
111102
public void connect() throws IOException {
112103
try {
113-
int errorCode = mTagService.connect(mTag.getServiceHandle(), getTechnologyId());
104+
int errorCode = mTag.getTagService().connect(mTag.getServiceHandle(), getTechnologyId());
114105

115106
if (errorCode == ErrorCodes.SUCCESS) {
116107
// Store this in the tag object
@@ -132,7 +123,7 @@ public void reconnect() throws IOException {
132123
}
133124

134125
try {
135-
int errorCode = mTagService.reconnect(mTag.getServiceHandle());
126+
int errorCode = mTag.getTagService().reconnect(mTag.getServiceHandle());
136127

137128
if (errorCode != ErrorCodes.SUCCESS) {
138129
mIsConnected = false;
@@ -153,7 +144,7 @@ public void close() {
153144
/* Note that we don't want to physically disconnect the tag,
154145
* but just reconnect to it to reset its state
155146
*/
156-
mTagService.reconnect(mTag.getServiceHandle());
147+
mTag.getTagService().reconnect(mTag.getServiceHandle());
157148
} catch (RemoteException e) {
158149
Log.e(TAG, "NFC service dead", e);
159150
} finally {
@@ -167,7 +158,7 @@ public void close() {
167158
checkConnected();
168159

169160
try {
170-
TransceiveResult result = mTagService.transceive(mTag.getServiceHandle(), data, raw);
161+
TransceiveResult result = mTag.getTagService().transceive(mTag.getServiceHandle(), data, raw);
171162
if (result == null) {
172163
throw new IOException("transceive failed");
173164
} else {

core/java/android/nfc/technology/IsoDep.java renamed to core/java/android/nfc/tech/IsoDep.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17-
package android.nfc.technology;
17+
package android.nfc.tech;
1818

19-
import android.nfc.NfcAdapter;
2019
import android.nfc.Tag;
2120
import android.os.Bundle;
2221
import android.os.RemoteException;
@@ -28,7 +27,7 @@
2827
* A low-level connection to a {@link Tag} using the ISO-DEP technology, also known as
2928
* ISO1443-4.
3029
*
31-
* <p>You can acquire this kind of connection with {@link NfcAdapter#getTechnology}.
30+
* <p>You can acquire this kind of connection with {@link #get}.
3231
* Use this class to send and receive data with {@link #transceive transceive()}.
3332
*
3433
* <p>Applications must implement their own protocol stack on top of
@@ -49,10 +48,26 @@ public final class IsoDep extends BasicTagTechnology {
4948
private byte[] mHiLayerResponse = null;
5049
private byte[] mHistBytes = null;
5150

51+
/**
52+
* Returns an instance of this tech for the given tag. If the tag doesn't support
53+
* this tech type null is returned.
54+
*
55+
* @param tag The tag to get the tech from
56+
*/
57+
public static IsoDep get(Tag tag) {
58+
if (!tag.hasTech(TagTechnology.ISO_DEP)) return null;
59+
try {
60+
return new IsoDep(tag);
61+
} catch (RemoteException e) {
62+
return null;
63+
}
64+
}
65+
5266
/** @hide */
53-
public IsoDep(NfcAdapter adapter, Tag tag, Bundle extras)
67+
public IsoDep(Tag tag)
5468
throws RemoteException {
55-
super(adapter, tag, TagTechnology.ISO_DEP);
69+
super(tag, TagTechnology.ISO_DEP);
70+
Bundle extras = tag.getTechExtras(TagTechnology.ISO_DEP);
5671
if (extras != null) {
5772
mHiLayerResponse = extras.getByteArray(EXTRA_HI_LAYER_RESP);
5873
mHistBytes = extras.getByteArray(EXTRA_HIST_BYTES);
@@ -70,7 +85,7 @@ public IsoDep(NfcAdapter adapter, Tag tag, Bundle extras)
7085
*/
7186
public void setTimeout(int timeout) {
7287
try {
73-
mTagService.setIsoDepTimeout(timeout);
88+
mTag.getTagService().setIsoDepTimeout(timeout);
7489
} catch (RemoteException e) {
7590
Log.e(TAG, "NFC service dead", e);
7691
}
@@ -79,7 +94,7 @@ public void setTimeout(int timeout) {
7994
@Override
8095
public void close() {
8196
try {
82-
mTagService.resetIsoDepTimeout();
97+
mTag.getTagService().resetIsoDepTimeout();
8398
} catch (RemoteException e) {
8499
Log.e(TAG, "NFC service dead", e);
85100
}

0 commit comments

Comments
 (0)