Skip to content

Commit

Permalink
Merge branch 'sony_z3'
Browse files Browse the repository at this point in the history
  • Loading branch information
ikarus23 committed Aug 28, 2015
2 parents 23146a6 + 1be31dd commit 7ccd3d1
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -476,11 +476,12 @@ public static void disableNfcForegroundDispatch(Activity targetActivity) {

/**
* For Activities which want to treat new Intents as Intents with a new
* Tag attached. If the given Intent has a Tag extra, the
* {@link #mTag} and {@link #mUID} will be updated and a Toast
* message will be shown in the calling Context (Activity).
* This method will also check if the device/tag supports Mifare Classic
* (see return values and {@link #checkMifareClassicSupport(Tag, Context)}).
* Tag attached. If the given Intent has a Tag extra, it will be patched
* by {@link MCReader#patchTag(Tag)} and {@link #mTag} as well as
* {@link #mUID} will be updated. A Toast message will be shown in the
* Context of the calling Activity. This method will also check if the
* device/tag supports Mifare Classic (see return values and
* {@link #checkMifareClassicSupport(Tag, Context)}).
* @param intent The Intent which should be checked for a new Tag.
* @param context The Context in which the Toast will be shown.
* @return
Expand All @@ -499,6 +500,7 @@ public static int treatAsNewTag(Intent intent, Context context) {
// Check if Intent has a NFC Tag.
if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
tag = MCReader.patchTag(tag);
setTag(tag);

// Show Toast message with UID.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,35 +66,50 @@ public class MCReader {
* @param tag The tag to operate on.
*/
private MCReader(Tag tag) {
MifareClassic tmpMFC;
MifareClassic tmpMFC = null;
try {
tmpMFC = MifareClassic.get(tag);
} catch (NullPointerException e) {
tmpMFC = MifareClassic.get(patchTag(tag));
} catch (Exception e) {
Log.e(LOG_TAG, "Could not create Mifare Classic reader for the"
+ "provided tag (even after patching it).");
throw e;
}
mMFC = tmpMFC;
}

/**
* Patch the broken Tag object of HTC One (m7/m8) devices with Android 5.x.
* "It seems, the reason of this bug is TechExtras of NfcA is null.
* Patch a possibly broken Tag object of HTC One (m7/m8) or Sony
* Xperia Z3 devices (with Android 5.x.)
*
* HTC One: "It seems, the reason of this bug is TechExtras of NfcA is null.
* However, TechList contains MifareClassic." -- bildin.
* This patch will fix this. For more information please refer to
* This method will fix this. For more information please refer to
* https://github.com/ikarus23/MifareClassicTool/issues/52
* This patch was provided by bildin (https://github.com/bildin).
* @param tag The broken tag.
*
* Sony Xperia Z3 (+ emmulated Mifare Classic tag): The buggy tag has
* two NfcA in the TechList with different SAK values and a MifareClassic
* (with the Extra of the second NfcA). Both, the second NfcA and the
* MifareClassic technique, have a SAK of 0x20. According to NXP's
* guidelines on identifying Mifare tags (Page 11), this a Mifare Plus or
* Mifare DESFire tag. This method creates a new Extra with the SAK
* values of both NfcA occurrences ORed (as mentioned in NXP's
* Mifare type identification procedure guide) and replace the Extra of
* the first NfcA with the new one. For more information please refer to
* https://github.com/ikarus23/MifareClassicTool/issues/64
* This patch was provided by bildin (https://github.com/bildin).
*
* @param tag The possibly broken tag.
* @return The fixed tag.
*/
private Tag patchTag(Tag tag)
{
public static Tag patchTag(Tag tag) {
if (tag == null) {
return null;
}

String[] sTechList = tag.getTechList();
Parcel oldParcel;
Parcel newParcel;
oldParcel = Parcel.obtain();
String[] techList = tag.getTechList();

Parcel oldParcel = Parcel.obtain();
tag.writeToParcel(oldParcel, 0);
oldParcel.setDataPosition(0);

Expand All @@ -117,31 +132,57 @@ private Tag patchTag(Tag tag)
}
oldParcel.recycle();

int nfcaIdx=-1;
int mcIdx=-1;
for(int idx = 0; idx < sTechList.length; idx++) {
if(sTechList[idx].equals(NfcA.class.getName())) {
nfcaIdx = idx;
} else if(sTechList[idx].equals(MifareClassic.class.getName())) {
mcIdx = idx;
int nfcaIdx = -1;
int mcIdx = -1;
short sak = 0;
boolean isFirstSak = true;

for (int i = 0; i < techList.length; i++) {
if (techList[i].equals(NfcA.class.getName())) {
if (nfcaIdx == -1) {
nfcaIdx = i;
}
if (oldTechExtras[i] != null
&& oldTechExtras[i].containsKey("sak")) {
sak = (short) (sak
| oldTechExtras[i].getShort("sak"));
isFirstSak = (nfcaIdx == i) ? true : false;
}
} else if (techList[i].equals(MifareClassic.class.getName())) {
mcIdx = i;
}
}

if(nfcaIdx>=0&&mcIdx>=0&&oldTechExtras[mcIdx]==null) {
boolean modified = false;

// Patch the double NfcA issue (with different SAK) for
// Sony Z3 devices.
if (!isFirstSak) {
oldTechExtras[nfcaIdx].putShort("sak", sak);
modified = true;
}

// Patch the wrong index issue for HTC One devices.
if (nfcaIdx != -1 && mcIdx != -1 && oldTechExtras[mcIdx] == null) {
oldTechExtras[mcIdx] = oldTechExtras[nfcaIdx];
} else {
modified = true;
}

if (!modified) {
// Old tag was not modivied. Return the old one.
return tag;
}

newParcel = Parcel.obtain();
// Old tag was modified. Create a new tag with the new data.
Parcel newParcel = Parcel.obtain();
newParcel.writeInt(id.length);
newParcel.writeByteArray(id);
newParcel.writeInt(oldTechList.length);
newParcel.writeIntArray(oldTechList);
newParcel.writeTypedArray(oldTechExtras, 0);
newParcel.writeInt(serviceHandle);
newParcel.writeInt(isMock);
if(isMock == 0) {
if (isMock == 0) {
newParcel.writeStrongBinder(tagService);
}
newParcel.setDataPosition(0);
Expand Down

0 comments on commit 7ccd3d1

Please sign in to comment.