Grundaufbau der App
[DHBWCampusApp.git] / app / src / main / java / com / codebutler / farebot / card / desfire / DesfireFile.java
1 /*
2  * DesfireFile.java
3  *
4  * Copyright (C) 2011 Eric Butler
5  *
6  * Authors:
7  * Eric Butler <eric@codebutler.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 com.codebutler.farebot.card.desfire;
24
25 import org.apache.commons.lang3.ArrayUtils;
26
27 import android.os.Parcel;
28 import android.os.Parcelable;
29
30 import com.codebutler.farebot.card.desfire.DesfireFileSettings.RecordDesfireFileSettings;
31
32 public class DesfireFile implements Parcelable {
33     private int                 mId;
34     private DesfireFileSettings mSettings;
35     private byte[]              mData;
36
37     public static DesfireFile create (int fileId, DesfireFileSettings fileSettings, byte[] fileData) {
38         if (fileSettings instanceof RecordDesfireFileSettings)
39             return new RecordDesfireFile(fileId, fileSettings, fileData);
40         else
41             return new DesfireFile(fileId, fileSettings, fileData);
42     }
43
44     private DesfireFile (int fileId, DesfireFileSettings fileSettings, byte[] fileData) {
45         mId       = fileId;
46         mSettings = fileSettings;
47         mData     = fileData;
48     }
49
50     public DesfireFileSettings getFileSettings () {
51         return mSettings;
52     }
53
54     public int getId () {
55         return mId;
56     }
57
58     public byte[] getData () {
59         return mData;
60     }
61
62     public static final Parcelable.Creator<DesfireFile> CREATOR = new Parcelable.Creator<DesfireFile>() {
63         public DesfireFile createFromParcel(Parcel source) {
64             int fileId = source.readInt();
65
66             boolean isError = (source.readInt() == 1);
67
68             if (!isError) {
69                 DesfireFileSettings fileSettings = (DesfireFileSettings) source.readParcelable(DesfireFileSettings.class.getClassLoader());
70                 int    dataLength = source.readInt();
71                 byte[] fileData   = new byte[dataLength];
72                 source.readByteArray(fileData);
73
74                 return DesfireFile.create(fileId, fileSettings, fileData);
75             } else {
76                 return new InvalidDesfireFile(fileId, source.readString());
77             }
78         }
79
80         public DesfireFile[] newArray (int size) {
81             return new DesfireFile[size];
82         }
83     };
84
85     public void writeToParcel (Parcel parcel, int flags) {
86         parcel.writeInt(mId);
87         if (this instanceof InvalidDesfireFile) {
88             parcel.writeInt(1);
89             parcel.writeString(((InvalidDesfireFile)this).getErrorMessage());
90         } else {
91             parcel.writeInt(0);
92             parcel.writeParcelable(mSettings, 0);
93             parcel.writeInt(mData.length);
94             parcel.writeByteArray(mData);
95         }
96     }
97
98     public int describeContents () {
99         return 0;
100     }
101
102     public static class RecordDesfireFile extends DesfireFile {
103         private DesfireRecord[] mRecords;
104
105         private RecordDesfireFile (int fileId, DesfireFileSettings fileSettings, byte[] fileData) {
106             super(fileId, fileSettings, fileData);
107
108             RecordDesfireFileSettings settings = (RecordDesfireFileSettings) fileSettings;
109
110             DesfireRecord[] records = new DesfireRecord[settings.curRecords];
111             for (int i = 0; i < settings.curRecords; i++) {
112                 int offset = settings.recordSize * i;
113                 records[i] = new DesfireRecord(ArrayUtils.subarray(getData(), offset, offset + settings.recordSize));
114             }
115             mRecords = records;
116         }
117
118         public DesfireRecord[] getRecords () {
119             return mRecords;
120         }
121     }
122
123     public static class InvalidDesfireFile extends DesfireFile {
124         private String mErrorMessage;
125
126         public InvalidDesfireFile (int fileId, String errorMessage) {
127             super(fileId, null, new byte[0]);
128             mErrorMessage = errorMessage;
129         }
130
131         public String getErrorMessage () {
132             return mErrorMessage;
133         }
134     }
135 }