Skip to content

Commit d88e9aa

Browse files
committed
Use classnames instead of ints for NFC techs.
This makes the system more flexible and allows adding new technology types without having to update the API. Change-Id: Iaee6b633965e501a70e8afc3f1d54d9d94a4d05a
1 parent 5e81a6e commit d88e9aa

File tree

11 files changed

+172
-64
lines changed

11 files changed

+172
-64
lines changed

api/current.xml

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -100829,7 +100829,9 @@
100829100829
</parameter>
100830100830
<parameter name="intent" type="android.app.PendingIntent">
100831100831
</parameter>
100832-
<parameter name="filters" type="android.content.IntentFilter...">
100832+
<parameter name="filters" type="android.content.IntentFilter[]">
100833+
</parameter>
100834+
<parameter name="techLists" type="java.lang.String...">
100833100835
</parameter>
100834100836
</method>
100835100837
<method name="enableForegroundNdefPush"
@@ -101018,8 +101020,8 @@
101018101020
visibility="public"
101019101021
>
101020101022
</method>
101021-
<method name="getTechnologyList"
101022-
return="int[]"
101023+
<method name="getTechList"
101024+
return="java.lang.String[]"
101023101025
abstract="false"
101024101026
native="false"
101025101027
synchronized="false"
@@ -101130,17 +101132,6 @@
101130101132
visibility="public"
101131101133
>
101132101134
</method>
101133-
<method name="getTechnologyId"
101134-
return="int"
101135-
abstract="false"
101136-
native="false"
101137-
synchronized="false"
101138-
static="false"
101139-
final="false"
101140-
deprecated="not deprecated"
101141-
visibility="public"
101142-
>
101143-
</method>
101144101135
<method name="isConnected"
101145101136
return="boolean"
101146101137
abstract="false"

core/java/android/nfc/INfcAdapter.aidl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import android.content.ComponentName;
2121
import android.content.IntentFilter;
2222
import android.nfc.NdefMessage;
2323
import android.nfc.Tag;
24+
import android.nfc.TechListParcel;
2425
import android.nfc.ILlcpSocket;
2526
import android.nfc.ILlcpServiceSocket;
2627
import android.nfc.ILlcpConnectionlessSocket;
@@ -48,7 +49,7 @@ interface INfcAdapter
4849
void localSet(in NdefMessage message);
4950
void openTagConnection(in Tag tag);
5051
void enableForegroundDispatch(in ComponentName activity, in PendingIntent intent,
51-
in IntentFilter[] filters);
52+
in IntentFilter[] filters, in TechListParcel techLists);
5253
void disableForegroundDispatch(in ComponentName activity);
5354
void enableForegroundNdefPush(in ComponentName activity, in NdefMessage msg);
5455
void disableForegroundNdefPush(in ComponentName activity);

core/java/android/nfc/NfcAdapter.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import android.content.pm.IPackageManager;
2828
import android.content.pm.PackageManager;
2929
import android.os.IBinder;
30+
import android.os.Parcel;
3031
import android.os.RemoteException;
3132
import android.os.ServiceManager;
3233
import android.util.Log;
@@ -54,7 +55,7 @@ public final class NfcAdapter {
5455
/**
5556
* Intent to started when a tag is discovered. The data URI is formated as
5657
* {@code vnd.android.nfc://tag/} with the path having a directory entry for each technology
57-
* in the {@link Tag#getTechnologyList()} is ascending order.
58+
* in the {@link Tag#getTechList()} is sorted ascending order.
5859
*
5960
* This intent is started after {@link #ACTION_NDEF_DISCOVERED} and before
6061
* {@link #ACTION_TAG_DISCOVERED}
@@ -426,7 +427,7 @@ public boolean disable() {
426427
* @throws IllegalStateException
427428
*/
428429
public void enableForegroundDispatch(Activity activity, PendingIntent intent,
429-
IntentFilter... filters) {
430+
IntentFilter[] filters, String[][] techLists) {
430431
if (activity == null || intent == null) {
431432
throw new NullPointerException();
432433
}
@@ -435,9 +436,14 @@ public void enableForegroundDispatch(Activity activity, PendingIntent intent,
435436
"when your activity is resumed");
436437
}
437438
try {
439+
TechListParcel parcel = null;
440+
if (techLists != null && techLists.length > 0) {
441+
parcel = new TechListParcel(techLists);
442+
}
438443
ActivityThread.currentActivityThread().registerOnActivityPausedListener(activity,
439444
mForegroundDispatchListener);
440-
sService.enableForegroundDispatch(activity.getComponentName(), intent, filters);
445+
sService.enableForegroundDispatch(activity.getComponentName(), intent, filters,
446+
parcel);
441447
} catch (RemoteException e) {
442448
attemptDeadServiceRecovery(e);
443449
}

core/java/android/nfc/Tag.java

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@
1616

1717
package android.nfc;
1818

19+
import android.nfc.tech.IsoDep;
20+
import android.nfc.tech.MifareClassic;
21+
import android.nfc.tech.MifareUltralight;
22+
import android.nfc.tech.Ndef;
23+
import android.nfc.tech.NdefFormatable;
24+
import android.nfc.tech.NfcA;
25+
import android.nfc.tech.NfcB;
26+
import android.nfc.tech.NfcF;
27+
import android.nfc.tech.NfcV;
1928
import android.nfc.tech.TagTechnology;
2029
import android.os.Bundle;
2130
import android.os.Parcel;
@@ -49,6 +58,7 @@
4958
public class Tag implements Parcelable {
5059
/*package*/ final byte[] mId;
5160
/*package*/ final int[] mTechList;
61+
/*package*/ final String[] mTechStringList;
5262
/*package*/ final Bundle[] mTechExtras;
5363
/*package*/ final int mServiceHandle; // for use by NFC service, 0 indicates a mock
5464
/*package*/ final INfcTag mTagService;
@@ -66,6 +76,7 @@ public Tag(byte[] id, int[] techList, Bundle[] techListExtras, int serviceHandle
6676
}
6777
mId = id;
6878
mTechList = Arrays.copyOf(techList, techList.length);
79+
mTechStringList = generateTechStringList(techList);
6980
// Ensure mTechExtras is as long as mTechList
7081
mTechExtras = Arrays.copyOf(techListExtras, techList.length);
7182
mServiceHandle = serviceHandle;
@@ -88,6 +99,45 @@ public static Tag createMockTag(byte[] id, int[] techList, Bundle[] techListExtr
8899
return new Tag(id, techList, techListExtras, 0, null);
89100
}
90101

102+
private String[] generateTechStringList(int[] techList) {
103+
final int size = techList.length;
104+
String[] strings = new String[size];
105+
for (int i = 0; i < size; i++) {
106+
switch (techList[i]) {
107+
case TagTechnology.ISO_DEP:
108+
strings[i] = IsoDep.class.getName();
109+
break;
110+
case TagTechnology.MIFARE_CLASSIC:
111+
strings[i] = MifareClassic.class.getName();
112+
break;
113+
case TagTechnology.MIFARE_ULTRALIGHT:
114+
strings[i] = MifareUltralight.class.getName();
115+
break;
116+
case TagTechnology.NDEF:
117+
strings[i] = Ndef.class.getName();
118+
break;
119+
case TagTechnology.NDEF_FORMATABLE:
120+
strings[i] = NdefFormatable.class.getName();
121+
break;
122+
case TagTechnology.NFC_A:
123+
strings[i] = NfcA.class.getName();
124+
break;
125+
case TagTechnology.NFC_B:
126+
strings[i] = NfcB.class.getName();
127+
break;
128+
case TagTechnology.NFC_F:
129+
strings[i] = NfcF.class.getName();
130+
break;
131+
case TagTechnology.NFC_V:
132+
strings[i] = NfcV.class.getName();
133+
break;
134+
default:
135+
throw new IllegalArgumentException("Unknown tech type " + techList[i]);
136+
}
137+
}
138+
return strings;
139+
}
140+
91141
/**
92142
* For use by NfcService only.
93143
* @hide
@@ -110,13 +160,12 @@ public byte[] getId() {
110160
* Returns technologies present in the tag that this implementation understands,
111161
* or a zero length array if there are no supported technologies on this tag.
112162
*
113-
* The elements of the list are guaranteed be one of the constants defined in
114-
* {@link TagTechnology}.
163+
* The elements of the list are the names of the classes implementing the technology.
115164
*
116165
* The ordering of the returned array is undefined and should not be relied upon.
117166
*/
118-
public int[] getTechnologyList() {
119-
return Arrays.copyOf(mTechList, mTechList.length);
167+
public String[] getTechList() {
168+
return mTechStringList;
120169
}
121170

122171
/** @hide */
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright (C) 2011 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+
17+
package android.nfc;
18+
19+
parcelable TechListParcel;
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright (C) 2011 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+
17+
package android.nfc;
18+
19+
import android.os.Parcel;
20+
import android.os.Parcelable;
21+
22+
/** @hide */
23+
public class TechListParcel implements Parcelable {
24+
25+
private String[][] mTechLists;
26+
27+
public TechListParcel(String[]... strings) {
28+
mTechLists = strings;
29+
}
30+
31+
public String[][] getTechLists() {
32+
return mTechLists;
33+
}
34+
35+
@Override
36+
public int describeContents() {
37+
return 0;
38+
}
39+
40+
@Override
41+
public void writeToParcel(Parcel dest, int flags) {
42+
int count = mTechLists.length;
43+
dest.writeInt(count);
44+
for (int i = 0; i < count; i++) {
45+
String[] techList = mTechLists[i];
46+
dest.writeStringArray(techList);
47+
}
48+
}
49+
50+
public static final Creator<TechListParcel> CREATOR = new Creator<TechListParcel>() {
51+
@Override
52+
public TechListParcel createFromParcel(Parcel source) {
53+
int count = source.readInt();
54+
String[][] techLists = new String[count][];
55+
for (int i = 0; i < count; i++) {
56+
techLists[i] = source.readStringArray();
57+
}
58+
return new TechListParcel(techLists);
59+
}
60+
61+
@Override
62+
public TechListParcel[] newArray(int size) {
63+
return new TechListParcel[size];
64+
}
65+
};
66+
}

core/java/android/nfc/tech/BasicTagTechnology.java

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -36,46 +36,23 @@
3636
/*package*/ int mSelectedTechnology;
3737

3838
BasicTagTechnology(Tag tag, int tech) throws RemoteException {
39-
int[] techList = tag.getTechnologyList();
40-
int i;
41-
42-
// Check target validity
43-
for (i = 0; i < techList.length; i++) {
44-
if (tech == techList[i]) {
45-
break;
46-
}
47-
}
48-
if (i >= techList.length) {
49-
// Technology not found
50-
throw new IllegalArgumentException("Technology " + tech + " not present on tag " + tag);
51-
}
52-
5339
mTag = tag;
5440
mSelectedTechnology = tech;
5541
}
5642

57-
BasicTagTechnology(Tag tag) throws RemoteException {
58-
this(tag, tag.getTechnologyList()[0]);
59-
}
60-
6143
@Override
6244
public Tag getTag() {
6345
return mTag;
6446
}
6547

6648
/** Internal helper to throw IllegalStateException if the technology isn't connected */
6749
void checkConnected() {
68-
if ((mTag.getConnectedTechnology() != getTechnologyId()) ||
50+
if ((mTag.getConnectedTechnology() != mSelectedTechnology) ||
6951
(mTag.getConnectedTechnology() == -1)) {
7052
throw new IllegalStateException("Call connect() first!");
7153
}
7254
}
7355

74-
@Override
75-
public int getTechnologyId() {
76-
return mSelectedTechnology;
77-
}
78-
7956
/**
8057
* Helper to indicate if {@link #connect} has succeeded.
8158
* <p>
@@ -101,11 +78,12 @@ public boolean isConnected() {
10178
@Override
10279
public void connect() throws IOException {
10380
try {
104-
int errorCode = mTag.getTagService().connect(mTag.getServiceHandle(), getTechnologyId());
81+
int errorCode = mTag.getTagService().connect(mTag.getServiceHandle(),
82+
mSelectedTechnology);
10583

10684
if (errorCode == ErrorCodes.SUCCESS) {
10785
// Store this in the tag object
108-
mTag.setConnectedTechnology(getTechnologyId());
86+
mTag.setConnectedTechnology(mSelectedTechnology);
10987
mIsConnected = true;
11088
} else {
11189
throw new IOException();
@@ -158,7 +136,8 @@ public void close() {
158136
checkConnected();
159137

160138
try {
161-
TransceiveResult result = mTag.getTagService().transceive(mTag.getServiceHandle(), data, raw);
139+
TransceiveResult result = mTag.getTagService().transceive(mTag.getServiceHandle(),
140+
data, raw);
162141
if (result == null) {
163142
throw new IOException("transceive failed");
164143
} else {

core/java/android/nfc/tech/Ndef.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
/**
3232
* A high-level connection to a {@link Tag} using one of the NFC type 1, 2, 3, or 4 technologies
3333
* to interact with NDEF data. MiFare Classic cards that present NDEF data may also be used
34-
* via this class. To determine the exact technology being used call {@link #getTechnologyId()}
34+
* via this class. To determine the exact technology being used call {@link #getType()}
3535
*
3636
* <p>You can acquire this kind of connection with {@link #get}.
3737
*

core/java/android/nfc/tech/TagTechnology.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,6 @@ public interface TagTechnology {
8888
*/
8989
public static final int MIFARE_ULTRALIGHT = 9;
9090

91-
/**
92-
* Returns the technology type for this tag connection.
93-
* @hide
94-
*/
95-
public int getTechnologyId();
96-
9791
/**
9892
* Get the {@link Tag} object this technology came from.
9993
*/

core/java/com/android/internal/app/ChooserActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,6 @@ protected void onCreate(Bundle savedInstanceState) {
5151
initialIntents[i] = (Intent)pa[i];
5252
}
5353
}
54-
super.onCreate(savedInstanceState, target, title, initialIntents, false);
54+
super.onCreate(savedInstanceState, target, title, initialIntents, null, false);
5555
}
5656
}

0 commit comments

Comments
 (0)