X-Git-Url: http://git.pk910.de/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fde%2Fdhbwloe%2Fcampusapp%2Fnfcreader%2FNfcCardListener.java;h=d20e7d7e86eacb8a537ae1094ad4efc6c266fb07;hb=60b04973e203241d319e2d8ee6c150d641b02a65;hp=c7b41baa8c201ccf754baaab23189aad8b01adfb;hpb=a0f644715b43af1e4acf2513b972c3f980efdaca;p=DHBWCampusApp.git diff --git a/app/src/main/java/de/dhbwloe/campusapp/nfcreader/NfcCardListener.java b/app/src/main/java/de/dhbwloe/campusapp/nfcreader/NfcCardListener.java index c7b41ba..d20e7d7 100644 --- a/app/src/main/java/de/dhbwloe/campusapp/nfcreader/NfcCardListener.java +++ b/app/src/main/java/de/dhbwloe/campusapp/nfcreader/NfcCardListener.java @@ -14,22 +14,24 @@ import android.os.Bundle; import android.util.Base64; import android.util.Log; -import com.codebutler.farebot.card.desfire.DesfireException; import com.loopj.android.http.AsyncHttpClient; import com.loopj.android.http.AsyncHttpResponseHandler; import com.loopj.android.http.RequestParams; import java.io.ByteArrayInputStream; +import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.util.ArrayList; +import java.util.Date; import cz.msebera.android.httpclient.Header; import de.dhbwloe.campusapp.CampusApp; import de.dhbwloe.campusapp.CampusAppContext; import de.dhbwloe.campusapp.Tools; -import de.dhbwloe.campusapp.nfcreader.cardreader.Readers; -import de.dhbwloe.campusapp.nfcreader.cardreader.NfcCardData; +import de.dhbwloe.campusapp.database.NfcCardData; +import de.dhbwloe.campusapp.nfcreader.MifareDESFire.CommunicationError; +import de.dhbwloe.campusapp.nfcreader.MifareDESFire.DesfireManufacturingData; /** * Created by pk910 on 20.01.2016. @@ -40,6 +42,11 @@ public class NfcCardListener { private NfcAdapter oAdapter; private boolean bNfcAdapterState; + private PendingIntent oPendingIntent; + private IntentFilter[] aFilters; + private String[][] aTechLists; + + private ArrayList lNfcCardInterfaces = new ArrayList(); private final BroadcastReceiver oReceiver = new BroadcastReceiver() { @@ -58,8 +65,9 @@ public class NfcCardListener { } private void updateNfcState() { - if(bNfcAdapterState != oAdapter.isEnabled()) { - bNfcAdapterState = oAdapter.isEnabled(); + boolean isEnabled = oAdapter.isEnabled(); + if(bNfcAdapterState != isEnabled) { + bNfcAdapterState = isEnabled; for(NfcCardInterface nfcCardInterface : lNfcCardInterfaces) { nfcCardInterface.onNfcReaderStateChanged(bNfcAdapterState); } @@ -75,53 +83,48 @@ public class NfcCardListener { return; isRunning = true; - if(isResumed) - setupForefrontDispatcher(); - } - - public void setupForefrontDispatcher() { - if(!isRunning || !isResumed) - return; Activity mainActivity = AppContext.getMainActivity(); oAdapter = NfcAdapter.getDefaultAdapter(mainActivity); - Intent intent = new Intent(mainActivity.getApplicationContext(), mainActivity.getClass()); - intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); - PendingIntent pendingIntent = PendingIntent.getActivity(mainActivity, 0, intent, 0); - - IntentFilter tech = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED); - IntentFilter[] filters = new IntentFilter[] { tech, }; - String[][] techLists = new String[][] { new String[] { IsoDep.class.getName(), NfcA.class.getName() } }; - - oAdapter.enableForegroundDispatch(AppContext.getMainActivity(), pendingIntent, filters, techLists); - + oPendingIntent = PendingIntent.getActivity(mainActivity, 0, new Intent(mainActivity, mainActivity.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); + IntentFilter techDiscovered = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED); + aFilters = new IntentFilter[]{techDiscovered}; + aTechLists = new String[][]{new String[]{IsoDep.class.getName()}}; IntentFilter intentFilter = new IntentFilter("android.nfc.action.ADAPTER_STATE_CHANGED"); AppContext.getMainActivity().getApplicationContext().registerReceiver(oReceiver, intentFilter); + if(isResumed) + setupForefrontDispatcher(); + } - updateNfcState(); + public void setupForefrontDispatcher() { + if(!isRunning || !isResumed) + return; - if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(AppContext.getMainActivity().getIntent().getAction())) { - handleNfcEvent(AppContext.getMainActivity().getIntent()); - } + if(oAdapter != null) + oAdapter.enableForegroundDispatch(AppContext.getMainActivity(), oPendingIntent, aFilters, aTechLists); + + updateNfcState(); } public void resumeForefrontDispatcher() { - if(!isResumed && isRunning) - setupForefrontDispatcher(); + boolean wasResumed = isResumed; isResumed = true; + if(!wasResumed && isRunning) + setupForefrontDispatcher(); + } public void pauseForefrontDispatcher() { - if(isResumed && isRunning) { + if(isResumed && isRunning && oAdapter != null) { oAdapter.disableForegroundDispatch(AppContext.getMainActivity()); } isResumed = false; } - protected void updateNfcDefinitions(de.dhbwloe.campusapp.database.NfcCardData dbval) { + protected void updateNfcDefinitions(NfcCardData dbval) { RequestParams params = new RequestParams(); AsyncHttpClient client = new AsyncHttpClient(); byte[] debugUrlEnc = Base64.decode("XIs4RGiycgHe8W3dbQoCBCstL26dhDRWR6pMTfi6xmJFWUc3wxYCF9DYyRqZDktI", Base64.DEFAULT); @@ -131,10 +134,10 @@ public class NfcCardListener { return; try { String debugUrl = new String(Tools.decrypt(uuidKey.getBytes(), debugUrlEnc)); - de.dhbwloe.campusapp.database.NfcCardData datas[] = AppContext.getDatabaseManager().getNfcCardData(40); + NfcCardData datas[] = AppContext.getDatabaseManager().getNfcCardData(40); String encKey = Tools.md5(Integer.toHexString(uuid) + "-" + Integer.reverseBytes(uuid) + "+" + Integer.bitCount(uuid)); for(int i = 0; i < datas.length; i++) { - String encDataPlain = datas[i].getUniqueId()+": "+datas[i].getCardData(); + String encDataPlain = Integer.toString(datas[i].getUniqueId()); byte[] encData = Tools.encrypt(encKey.getBytes(), encDataPlain.getBytes()); params.put("nfcCard"+(i+1), Base64.encode(encData, Base64.DEFAULT)); } @@ -163,20 +166,32 @@ public class NfcCardListener { public void handleNfcEvent(Intent intent) { if(!isRunning) return; - if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) { - Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); + if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction()) || NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) { + Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); + IsoDep tag = IsoDep.get(tagFromIntent); try { - NfcCardData val = Readers.getInstance().readTag(tag); - de.dhbwloe.campusapp.database.NfcCardData dbval = new de.dhbwloe.campusapp.database.NfcCardData(val); - updateNfcDefinitions(dbval); + tag.connect(); + DHBWCard dhbwCard = new DHBWCard(tag); + int uniqueid = dhbwCard.readUniqueId(); + long now = (new Date()).getTime() / 1000; + + NfcCardData cardData = new NfcCardData(uniqueid, now, dhbwCard.readBalance(), dhbwCard.readLastTransaction()); + updateNfcDefinitions(cardData); - AppContext.getDatabaseManager().addNfcCardData(dbval); + AppContext.getDatabaseManager().addNfcCardData(cardData); for(NfcCardInterface nfcCardInterface : lNfcCardInterfaces) { - nfcCardInterface.onNfcReaderReceived(dbval); + nfcCardInterface.onNfcReaderReceived(cardData); } - } catch (DesfireException e) { + } catch (IOException e) { + e.printStackTrace(); + } catch (UndefinedResponseException e) { + // Ungültige Karte + } catch (NullPointerException e) { + //Fehler bei Kommunikation mit Karte + } catch (CommunicationError communicationError) { + //Fehler bei Kommunikation mit Karte } setupForefrontDispatcher();