Skip to content

Commit 1126391

Browse files
committed
Updated for Android 2.3.3 - no longer needs internal APIs!
1 parent 972a9c3 commit 1126391

File tree

8 files changed

+131
-172
lines changed

8 files changed

+131
-172
lines changed

AndroidManifest.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
<manifest
2525
xmlns:android="http://schemas.android.com/apk/res/android"
2626
package="com.codebutler.farebot"
27-
android:versionCode="1"
28-
android:versionName="0.1">
27+
android:versionCode="2"
28+
android:versionName="0.2">
2929

30-
<uses-sdk android:minSdkVersion="9" />
30+
<uses-sdk android:minSdkVersion="10" />
3131
<uses-permission android:name="android.permission.NFC" />
3232
<uses-feature android:name="android.hardware.nfc" />
3333

FareBot.ipr

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,7 @@
3131
<module2copyright />
3232
<LanguageOptions name="XML">
3333
<option name="fileTypeOverride" value="3" />
34-
<option name="relativeBefore" value="true" />
35-
<option name="addBlankAfter" value="true" />
36-
<option name="fileLocation" value="1" />
37-
<option name="block" value="true" />
38-
<option name="separateBefore" value="false" />
39-
<option name="separateAfter" value="false" />
4034
<option name="prefixLines" value="false" />
41-
<option name="lenBefore" value="80" />
42-
<option name="lenAfter" value="80" />
43-
<option name="box" value="false" />
44-
<option name="filler" value=" " />
4535
</LanguageOptions>
4636
</component>
4737
<component name="DependencyValidationManager">

src/android/nfc/INfcTag.aidl

Lines changed: 0 additions & 40 deletions
This file was deleted.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* UnsupportedTagException.java
3+
*
4+
* Copyright (C) 2011 Eric Butler
5+
*
6+
* Authors:
7+
* Eric Butler <eric@codebutler.com>
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU General Public License as published by
11+
* the Free Software Foundation, either version 3 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*/
22+
23+
package com.codebutler.farebot;
24+
25+
public class UnsupportedTagException extends Exception
26+
{
27+
private String[] mTechList;
28+
private String mTagId;
29+
30+
public UnsupportedTagException (String[] techList, String tagId)
31+
{
32+
mTechList = techList;
33+
mTagId = tagId;
34+
}
35+
36+
public String[] getTechList () {
37+
return mTechList;
38+
}
39+
40+
public String getTagId () {
41+
return mTagId;
42+
}
43+
44+
@Override
45+
public String getMessage ()
46+
{
47+
StringBuilder builder = new StringBuilder();
48+
for (String tech : mTechList) {
49+
builder.append("\n ");
50+
builder.append(tech.replace("android.nfc.tech.", ""));
51+
}
52+
return String.format("Identifier: %s\n\nTechnologies:%s", mTagId, builder.toString());
53+
}
54+
}

src/com/codebutler/farebot/activities/ReadingTagActivity.java

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,23 @@
2323
package com.codebutler.farebot.activities;
2424

2525
import android.app.Activity;
26+
import android.app.AlertDialog;
2627
import android.content.ContentValues;
28+
import android.content.DialogInterface;
2729
import android.content.Intent;
2830
import android.net.Uri;
29-
import android.nfc.NfcAdapter;
31+
import android.nfc.Tag;
3032
import android.os.AsyncTask;
3133
import android.os.Bundle;
32-
import android.util.Log;
3334
import android.widget.TextView;
3435
import com.codebutler.farebot.R;
36+
import com.codebutler.farebot.UnsupportedTagException;
3537
import com.codebutler.farebot.Utils;
3638
import com.codebutler.farebot.mifare.DesfireCard;
3739
import com.codebutler.farebot.mifare.MifareCard;
3840
import com.codebutler.farebot.provider.CardProvider;
3941
import com.codebutler.farebot.provider.CardsTableColumns;
40-
import com.codebutler.nfc.NfcInternal;
42+
import org.apache.commons.lang.ArrayUtils;
4143

4244
public class ReadingTagActivity extends Activity
4345
{
@@ -63,24 +65,19 @@ private void resolveIntent (Intent intent)
6365
try {
6466
Bundle extras = intent.getExtras();
6567

66-
final byte[] id = extras.getByteArray(NfcAdapter.EXTRA_ID);
67-
Log.d("MainActivity", "Found tag with id: " + Utils.getHexString(id));
68-
69-
final Object tagObject = extras.getParcelable("android.nfc.extra.TAG");
68+
final Tag tag = (Tag) extras.getParcelable("android.nfc.extra.TAG");;
69+
final String[] techs = tag.getTechList();
7070

71-
new AsyncTask<Object, String, MifareCard>() {
71+
new AsyncTask<Void, String, MifareCard>() {
7272
Exception mException;
7373

7474
@Override
75-
protected MifareCard doInBackground (Object... params) {
75+
protected MifareCard doInBackground (Void... params) {
7676
try {
77-
Object tagObject = params[0];
78-
String cardType = NfcInternal.getCardType(tagObject);
79-
80-
if (cardType.equals("Iso14443-4"))
81-
return DesfireCard.dumpTag(id, tagObject);
77+
if (ArrayUtils.contains(techs, "android.nfc.tech.IsoDep"))
78+
return DesfireCard.dumpTag(tag.getId(), tag);
8279
else
83-
throw new Exception(String.format("Unsupported card type: %s\nSerial: %s", cardType, Utils.getHexString(id)));
80+
throw new UnsupportedTagException(techs, Utils.getHexString(tag.getId()));
8481
} catch (Exception ex) {
8582
mException = ex;
8683
return null;
@@ -90,7 +87,21 @@ protected MifareCard doInBackground (Object... params) {
9087
@Override
9188
protected void onPostExecute (MifareCard card) {
9289
if (mException != null) {
93-
Utils.showErrorAndFinish(ReadingTagActivity.this, mException);
90+
if (mException instanceof UnsupportedTagException) {
91+
UnsupportedTagException ex = (UnsupportedTagException) mException;
92+
new AlertDialog.Builder(ReadingTagActivity.this)
93+
.setTitle("Unsupported Tag")
94+
.setMessage(ex.getMessage())
95+
.setCancelable(false)
96+
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
97+
public void onClick (DialogInterface arg0, int arg1) {
98+
finish();
99+
}
100+
})
101+
.show();
102+
} else {
103+
Utils.showErrorAndFinish(ReadingTagActivity.this, mException);
104+
}
94105
return;
95106
}
96107

@@ -115,7 +126,7 @@ protected void onProgressUpdate(String... values) {
115126
textView.setText(values[0]);
116127
}
117128

118-
}.execute(tagObject);
129+
}.execute();
119130

120131
} catch (Exception ex) {
121132
Utils.showErrorAndFinish(this, ex);

src/com/codebutler/farebot/mifare/DesfireCard.java

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
package com.codebutler.farebot.mifare;
2424

25+
import android.nfc.Tag;
26+
import android.nfc.tech.IsoDep;
2527
import android.os.Parcel;
2628
import android.os.Parcelable;
2729
import android.util.Base64;
@@ -39,42 +41,54 @@ public class DesfireCard extends MifareCard
3941
private DesfireManufacturingData mManfData;
4042
private DesfireApplication[] mApplications;
4143

42-
public static DesfireCard dumpTag (byte[] tagId, Object tagObject) throws Exception
44+
public static DesfireCard dumpTag (byte[] tagId, Tag tag) throws Exception
4345
{
4446
List<DesfireApplication> apps = new ArrayList<DesfireApplication>();
4547

46-
DesfireProtocol desfireTag = new DesfireProtocol(tagObject);
48+
IsoDep tech = IsoDep.get(tag);
4749

48-
DesfireManufacturingData manufData = desfireTag.getManufacturingData();
50+
tech.connect();
4951

50-
for (int appId : desfireTag.getAppList()) {
51-
desfireTag.selectApp(appId);
52+
DesfireManufacturingData manufData;
53+
DesfireApplication[] appsArray;
54+
55+
try {
56+
DesfireProtocol desfireTag = new DesfireProtocol(tech);
57+
58+
manufData = desfireTag.getManufacturingData();
59+
60+
for (int appId : desfireTag.getAppList()) {
61+
desfireTag.selectApp(appId);
62+
63+
List<DesfireFile> files = new ArrayList<DesfireFile>();
64+
65+
for (int fileId : desfireTag.getFileList()) {
66+
try {
67+
DesfireFileSettings settings = desfireTag.getFileSettings(fileId);
68+
byte[] data = null;
69+
if (settings instanceof DesfireFileSettings.StandardDesfireFileSettings)
70+
data = desfireTag.readFile(fileId);
71+
else
72+
data = desfireTag.readRecord(fileId);
73+
files.add(DesfireFile.create(fileId, settings, data));
74+
} catch (Exception ex) {
75+
files.add(new DesfireFile.InvalidDesfireFile(fileId, ex.toString()));
76+
}
77+
}
5278

53-
List<DesfireFile> files = new ArrayList<DesfireFile>();
79+
DesfireFile[] filesArray = new DesfireFile[files.size()];
80+
files.toArray(filesArray);
5481

55-
for (int fileId : desfireTag.getFileList()) {
56-
try {
57-
DesfireFileSettings settings = desfireTag.getFileSettings(fileId);
58-
byte[] data = null;
59-
if (settings instanceof DesfireFileSettings.StandardDesfireFileSettings)
60-
data = desfireTag.readFile(fileId);
61-
else
62-
data = desfireTag.readRecord(fileId);
63-
files.add(DesfireFile.create(fileId, settings, data));
64-
} catch (Exception ex) {
65-
files.add(new DesfireFile.InvalidDesfireFile(fileId, ex.toString()));
66-
}
82+
apps.add(new DesfireApplication(appId, filesArray));
6783
}
6884

69-
DesfireFile[] filesArray = new DesfireFile[files.size()];
70-
files.toArray(filesArray);
71-
72-
apps.add(new DesfireApplication(appId, filesArray));
85+
appsArray = new DesfireApplication[apps.size()];
86+
apps.toArray(appsArray);
87+
} finally {
88+
if (tech.isConnected())
89+
tech.close();
7390
}
7491

75-
DesfireApplication[] appsArray = new DesfireApplication[apps.size()];
76-
apps.toArray(appsArray);
77-
7892
return new DesfireCard(tagId, manufData, appsArray);
7993
}
8094

src/com/codebutler/farebot/mifare/DesfireProtocol.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,10 @@
2222

2323
package com.codebutler.farebot.mifare;
2424

25-
import android.nfc.NfcAdapter;
25+
import android.nfc.tech.IsoDep;
2626
import com.codebutler.farebot.Utils;
27-
import com.codebutler.nfc.NfcInternal;
2827

2928
import java.io.ByteArrayOutputStream;
30-
import java.lang.reflect.Method;
3129

3230
public class DesfireProtocol
3331
{
@@ -46,11 +44,11 @@ public class DesfireProtocol
4644
static final byte PERMISSION_DENIED = (byte) 0x9D;
4745
static final byte ADDITIONAL_FRAME = (byte) 0xAF;
4846

49-
private Object mTagObject;
47+
private IsoDep mTagTech;
5048

51-
public DesfireProtocol(Object tagObject)
49+
public DesfireProtocol(IsoDep tagTech)
5250
{
53-
mTagObject = tagObject;
51+
mTagTech = tagTech;
5452
}
5553

5654
public DesfireManufacturingData getManufacturingData() throws Exception
@@ -132,7 +130,7 @@ private byte[] sendRequest (byte command, byte[] parameters) throws Exception
132130
{
133131
ByteArrayOutputStream output = new ByteArrayOutputStream();
134132

135-
byte[] recvBuffer = NfcInternal.transceive(mTagObject, wrapMessage(command, parameters));
133+
byte[] recvBuffer = mTagTech.transceive(wrapMessage(command, parameters));
136134

137135
while (true) {
138136
if (recvBuffer[recvBuffer.length - 2] != (byte) 0x91)
@@ -144,11 +142,11 @@ private byte[] sendRequest (byte command, byte[] parameters) throws Exception
144142
if (status == OPERATION_OK) {
145143
break;
146144
} else if (status == ADDITIONAL_FRAME) {
147-
recvBuffer = NfcInternal.transceive(mTagObject, wrapMessage(GET_ADDITIONAL_FRAME, null));
145+
recvBuffer = mTagTech.transceive(wrapMessage(GET_ADDITIONAL_FRAME, null));
148146
} else if (status == PERMISSION_DENIED) {
149147
throw new Exception("Permission denied");
150148
} else {
151-
throw new Exception("Unknown status code: " + Integer.toHexString(status));
149+
throw new Exception("Unknown status code: " + Integer.toHexString(status & 0xFF));
152150
}
153151
}
154152

0 commit comments

Comments
 (0)