Grundaufbau der App
[DHBWCampusApp.git] / app / src / main / java / de / dhbwloe / campusapp / nfcreader / cardreader / IntercardReader.java
1 /*
2  * IntercardReader.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.os.Bundle;
26 import android.util.Log;
27
28 import com.codebutler.farebot.Utils;
29 import com.codebutler.farebot.card.desfire.DesfireException;
30 import com.codebutler.farebot.card.desfire.DesfireFileSettings;
31 import com.codebutler.farebot.card.desfire.DesfireProtocol;
32
33 public class IntercardReader implements ICardReader {
34         private static final String TAG = IntercardReader.class.getName();
35
36         @Override
37         public NfcCardData readCard(DesfireProtocol card) throws DesfireException {
38                 NfcCardData valuedata = new NfcCardData();
39                 valuedata.setUniqueId(card.getManufacturingData().uid);
40
41                 try {
42                         int[] appList = card.getAppList();
43                         for(int i = 0; i < appList.length; i++) {
44                                 for(int j = 0; j < 10; j++) {
45                                         try {
46                                                 DesfireFileSettings settings = Utils.selectAppFile(card, appList[i], j);
47                                                 if(settings != null) {
48                                                         Bundle bundle = new Bundle();
49                                                         bundle.putString("type", settings.getFileTypeName());
50                                                         boolean hasValue = false;
51                                                         if (settings instanceof DesfireFileSettings.ValueDesfireFileSettings) {
52                                                                 DesfireFileSettings.ValueDesfireFileSettings value = (DesfireFileSettings.ValueDesfireFileSettings) settings;
53                                                                 bundle.putInt("value", value.value);
54                                                                 hasValue = true;
55                                                                 bundle.putByte("limited", value.limitedCreditEnabled);
56                                                                 bundle.putInt("max", value.upperLimit);
57                                                                 bundle.putInt("min", value.lowerLimit);
58                                                         } else if (settings instanceof DesfireFileSettings.StandardDesfireFileSettings) {
59                                                                 DesfireFileSettings.StandardDesfireFileSettings value = (DesfireFileSettings.StandardDesfireFileSettings) settings;
60                                                                 bundle.putInt("size", value.fileSize);
61                                                         } else if (settings instanceof DesfireFileSettings.RecordDesfireFileSettings) {
62                                                                 DesfireFileSettings.RecordDesfireFileSettings value = (DesfireFileSettings.RecordDesfireFileSettings) settings;
63                                                                 bundle.putInt("records", value.curRecords);
64                                                                 bundle.putInt("size", value.recordSize);
65                                                                 bundle.putInt("max", value.maxRecords);
66                                                                 hasValue = true;
67                                                         }
68
69                                                         try {
70                                                                 bundle.putInt("data", card.readValue(j));
71                                                                 hasValue = true;
72
73                                                         } catch (Exception e) {
74                                                         }
75                                                         try {
76                                                                 byte[] bytes = card.readFile(j);
77                                                                 bundle.putByteArray("file", bytes);
78                                                                 hasValue = true;
79                                                         } catch (Exception e) {
80                                                         }
81                                                         try {
82                                                                 byte[] bytes = card.readRecord(j);
83                                                                 bundle.putByteArray("record", bytes);
84                                                                 hasValue = true;
85                                                         } catch (Exception e) {
86                                                         }
87                                                         if(hasValue)
88                                                                 valuedata.appendPlainData(appList[i], j, bundle);
89                                                 }
90                                         } catch (Exception e) {
91                                                 break;
92                                         }
93                                 }
94
95                         }
96                 } catch (Exception e) {
97                 }
98                 return valuedata;
99         }
100 }