1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Copyright (c) 2015, The Linux Foundation. All rights reserved.
* Not a Contribution.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.nfc.cardemulation;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlSerializer;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;
/**
* The AidGroup class represents a group of Application Identifiers (AIDs).
*
* <p>The format of AIDs is defined in the ISO/IEC 7816-4 specification. This class
* requires the AIDs to be input as a hexadecimal string, with an even amount of
* hexadecimal characters, e.g. "F014811481".
*
* @hide
*/
public class AidGroup implements Parcelable {
/**
* The maximum number of AIDs that can be present in any one group.
*/
public static final int MAX_NUM_AIDS = 256;
static final String TAG = "AidGroup";
protected List<String> aids;
protected String category;
protected String description;
/**
* Creates a new AidGroup object.
*
* @param aids The list of AIDs present in the group
* @param category The category of this group, e.g. {@link CardEmulation#CATEGORY_PAYMENT}
*/
public AidGroup(List<String> aids, String category) {
if (aids == null || aids.size() == 0) {
throw new IllegalArgumentException("No AIDS in AID group.");
}
if (aids.size() > MAX_NUM_AIDS) {
throw new IllegalArgumentException("Too many AIDs in AID group.");
}
for (String aid : aids) {
if (!CardEmulation.isValidAid(aid)) {
throw new IllegalArgumentException("AID " + aid + " is not a valid AID.");
}
}
if (isValidCategory(category)) {
this.category = category;
} else {
this.category = CardEmulation.CATEGORY_OTHER;
}
this.aids = new ArrayList<String>(aids.size());
for (String aid : aids) {
this.aids.add(aid.toUpperCase());
}
this.description = null;
}
AidGroup(String category, String description) {
this.aids = new ArrayList<String>();
this.category = category;
this.description = description;
}
/**
* @return the category of this AID group
*/
public String getCategory() {
return category;
}
/**
* @return the list of AIDs in this group
*/
public List<String> getAids() {
return aids;
}
@Override
public String toString() {
StringBuilder out = new StringBuilder("Category: " + category +
", AIDs:");
for (String aid : aids) {
out.append(aid);
out.append(", ");
}
return out.toString();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(category);
dest.writeInt(aids.size());
if (aids.size() > 0) {
dest.writeStringList(aids);
}
}
public static final Parcelable.Creator<AidGroup> CREATOR =
new Parcelable.Creator<AidGroup>() {
@Override
public AidGroup createFromParcel(Parcel source) {
String category = source.readString();
int listSize = source.readInt();
ArrayList<String> aidList = new ArrayList<String>();
if (listSize > 0) {
source.readStringList(aidList);
}
return new AidGroup(aidList, category);
}
@Override
public AidGroup[] newArray(int size) {
return new AidGroup[size];
}
};
static public AidGroup createFromXml(XmlPullParser parser) throws XmlPullParserException, IOException {
String category = null;
ArrayList<String> aids = new ArrayList<String>();
AidGroup group = null;
boolean inGroup = false;
int eventType = parser.getEventType();
int minDepth = parser.getDepth();
while (eventType != XmlPullParser.END_DOCUMENT && parser.getDepth() >= minDepth) {
String tagName = parser.getName();
if (eventType == XmlPullParser.START_TAG) {
if (tagName.equals("aid")) {
if (inGroup) {
String aid = parser.getAttributeValue(null, "value");
if (aid != null) {
aids.add(aid.toUpperCase());
}
} else {
Log.d(TAG, "Ignoring <aid> tag while not in group");
}
} else if (tagName.equals("aid-group")) {
category = parser.getAttributeValue(null, "category");
if (category == null) {
Log.e(TAG, "<aid-group> tag without valid category");
return null;
}
inGroup = true;
} else {
Log.d(TAG, "Ignoring unexpected tag: " + tagName);
}
} else if (eventType == XmlPullParser.END_TAG) {
if (tagName.equals("aid-group") && inGroup && aids.size() > 0) {
group = new AidGroup(aids, category);
break;
}
}
eventType = parser.next();
}
return group;
}
public void writeAsXml(XmlSerializer out) throws IOException {
out.startTag(null, "aid-group");
out.attribute(null, "category", category);
for (String aid : aids) {
out.startTag(null, "aid");
out.attribute(null, "value", aid);
out.endTag(null, "aid");
}
out.endTag(null, "aid-group");
}
static boolean isValidCategory(String category) {
return CardEmulation.CATEGORY_PAYMENT.equals(category) ||
CardEmulation.CATEGORY_OTHER.equals(category);
}
}
|