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