Grundaufbau der App
[DHBWCampusApp.git] / app / src / main / java / de / dhbwloe / campusapp / nfcreader / cardreader / Readers.java
1 /*
2  * Readers.java
3  *
4  * Copyright (C) 2014 Jakob Wenzel
5  *
6  * Authors:
7  * Jakob Wenzel <jakobwenzel92@gmail.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 de.dhbwloe.campusapp.nfcreader.cardreader;
24
25 import android.nfc.Tag;
26 import android.nfc.tech.IsoDep;
27 import android.util.Log;
28
29 import com.codebutler.farebot.card.desfire.DesfireException;
30 import com.codebutler.farebot.card.desfire.DesfireProtocol;
31
32 import java.io.IOException;
33
34 public class Readers implements ICardReader {
35         private static final String TAG = Readers.class.getName();
36         private static Readers instance;
37         private ICardReader[] readers = new ICardReader[]{
38                         new MagnaCartaReader(),
39                         new IntercardReader()};
40
41
42         @Override
43         public NfcCardData readCard(DesfireProtocol card) throws DesfireException {
44                 Log.i(TAG, "Trying all readers");
45                 for (ICardReader reader : readers) {
46                         Log.i(TAG, "Trying " + reader.getClass().getSimpleName());
47                         NfcCardData val = reader.readCard(card);
48                         if (val!=null)
49                                 return val;
50                 }
51                 return null;
52         }
53
54
55         public NfcCardData readTag(Tag tag) throws DesfireException {
56                 Log.i(TAG, "Loading tag");
57                 IsoDep tech = IsoDep.get(tag);
58
59                 try {
60                         tech.connect();
61                 } catch (IOException e) {
62                         //Tag was removed. We fail silently.
63                         e.printStackTrace();
64                         return null;
65                 }
66
67                 try {
68                         DesfireProtocol desfireTag = new DesfireProtocol(tech);
69
70
71                         //Android has a Bug on Devices using a Broadcom NFC chip. See
72                         // http://code.google.com/p/android/issues/detail?id=58773
73                         //A Workaround is to connected to the tag, issue a dummy operation and then reconnect...
74                         try {
75                                 desfireTag.selectApp(0);
76                         }catch (ArrayIndexOutOfBoundsException e) {
77                                 //Exception occurs because the actual response is shorter than the error response
78                                 Log.i(TAG, "Broadcom workaround was needed");
79                         }
80
81                         tech.close();
82                         tech.connect();
83
84                         NfcCardData val = Readers.getInstance().readCard(desfireTag);
85                         return val;
86                 } catch (IOException e) {
87                         //This can only happen on tag close. we ignore this.
88                         e.printStackTrace();
89                         return null;
90                 } finally {
91                         if (tech.isConnected())
92                                 try {
93                                         tech.close();
94                                 } catch (IOException e) {
95                                         e.printStackTrace();
96                                 }
97                 }
98
99         }
100
101         public static Readers getInstance() {
102                 if (instance == null)
103                         instance = new Readers();
104                 return instance;
105         }
106 }