Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 | |
| 17 | package android.nfc.cardemulation; |
| 18 | |
Mathew Inwood | 57069dc | 2018-07-31 15:33:20 +0100 | [diff] [blame] | 19 | import android.annotation.UnsupportedAppUsage; |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 20 | import android.content.ComponentName; |
Martijn Coenen | 8abf680 | 2015-04-20 13:44:58 +0200 | [diff] [blame] | 21 | import android.content.Context; |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 22 | import android.content.pm.PackageManager; |
| 23 | import android.content.pm.ResolveInfo; |
| 24 | import android.content.pm.ServiceInfo; |
| 25 | import android.content.pm.PackageManager.NameNotFoundException; |
| 26 | import android.content.res.Resources; |
Martijn Coenen | 58d2065 | 2013-09-05 21:05:53 -0700 | [diff] [blame] | 27 | import android.content.res.Resources.NotFoundException; |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 28 | import android.content.res.TypedArray; |
| 29 | import android.content.res.XmlResourceParser; |
| 30 | import android.graphics.drawable.Drawable; |
| 31 | import android.os.Parcel; |
| 32 | import android.os.Parcelable; |
Martijn Coenen | 8abf680 | 2015-04-20 13:44:58 +0200 | [diff] [blame] | 33 | import android.os.ResultReceiver; |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 34 | import android.util.AttributeSet; |
| 35 | import android.util.Log; |
| 36 | import android.util.Xml; |
| 37 | |
| 38 | import org.xmlpull.v1.XmlPullParser; |
| 39 | import org.xmlpull.v1.XmlPullParserException; |
| 40 | |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 41 | import java.io.FileDescriptor; |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 42 | import java.io.IOException; |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 43 | import java.io.PrintWriter; |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 44 | import java.util.ArrayList; |
| 45 | import java.util.HashMap; |
Martijn Coenen | ed3a29e | 2015-01-23 11:29:54 -0800 | [diff] [blame] | 46 | import java.util.List; |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 47 | import java.util.Map; |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 48 | |
| 49 | /** |
| 50 | * @hide |
| 51 | */ |
| 52 | public final class ApduServiceInfo implements Parcelable { |
| 53 | static final String TAG = "ApduServiceInfo"; |
| 54 | |
| 55 | /** |
| 56 | * The service that implements this |
| 57 | */ |
Mathew Inwood | 57069dc | 2018-07-31 15:33:20 +0100 | [diff] [blame] | 58 | @UnsupportedAppUsage |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 59 | final ResolveInfo mService; |
| 60 | |
| 61 | /** |
| 62 | * Description of the service |
| 63 | */ |
| 64 | final String mDescription; |
| 65 | |
| 66 | /** |
Martijn Coenen | 38d3bb7 | 2013-08-07 20:03:03 -0700 | [diff] [blame] | 67 | * Whether this service represents AIDs running on the host CPU |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 68 | */ |
| 69 | final boolean mOnHost; |
| 70 | |
| 71 | /** |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 72 | * Mapping from category to static AID group |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 73 | */ |
Mathew Inwood | 57069dc | 2018-07-31 15:33:20 +0100 | [diff] [blame] | 74 | @UnsupportedAppUsage |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 75 | final HashMap<String, AidGroup> mStaticAidGroups; |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 76 | |
| 77 | /** |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 78 | * Mapping from category to dynamic AID group |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 79 | */ |
Mathew Inwood | 57069dc | 2018-07-31 15:33:20 +0100 | [diff] [blame] | 80 | @UnsupportedAppUsage |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 81 | final HashMap<String, AidGroup> mDynamicAidGroups; |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 82 | |
| 83 | /** |
Martijn Coenen | c3f0044 | 2013-08-28 19:23:41 -0700 | [diff] [blame] | 84 | * Whether this service should only be started when the device is unlocked. |
| 85 | */ |
| 86 | final boolean mRequiresDeviceUnlock; |
| 87 | |
| 88 | /** |
Martijn Coenen | 58d2065 | 2013-09-05 21:05:53 -0700 | [diff] [blame] | 89 | * The id of the service banner specified in XML. |
| 90 | */ |
| 91 | final int mBannerResourceId; |
| 92 | |
| 93 | /** |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 94 | * The uid of the package the service belongs to |
| 95 | */ |
| 96 | final int mUid; |
Martijn Coenen | 8abf680 | 2015-04-20 13:44:58 +0200 | [diff] [blame] | 97 | |
| 98 | /** |
Martijn Coenen | 8abf680 | 2015-04-20 13:44:58 +0200 | [diff] [blame] | 99 | * Settings Activity for this service |
| 100 | */ |
| 101 | final String mSettingsActivityName; |
| 102 | |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 103 | /** |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 104 | * @hide |
| 105 | */ |
Mathew Inwood | 57069dc | 2018-07-31 15:33:20 +0100 | [diff] [blame] | 106 | @UnsupportedAppUsage |
Martijn Coenen | 38d3bb7 | 2013-08-07 20:03:03 -0700 | [diff] [blame] | 107 | public ApduServiceInfo(ResolveInfo info, boolean onHost, String description, |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 108 | ArrayList<AidGroup> staticAidGroups, ArrayList<AidGroup> dynamicAidGroups, |
Martijn Coenen | a2344ea | 2015-06-05 10:45:50 +0200 | [diff] [blame] | 109 | boolean requiresUnlock, int bannerResource, int uid, |
Martijn Coenen | 8abf680 | 2015-04-20 13:44:58 +0200 | [diff] [blame] | 110 | String settingsActivityName) { |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 111 | this.mService = info; |
| 112 | this.mDescription = description; |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 113 | this.mStaticAidGroups = new HashMap<String, AidGroup>(); |
| 114 | this.mDynamicAidGroups = new HashMap<String, AidGroup>(); |
Martijn Coenen | 38d3bb7 | 2013-08-07 20:03:03 -0700 | [diff] [blame] | 115 | this.mOnHost = onHost; |
Martijn Coenen | c3f0044 | 2013-08-28 19:23:41 -0700 | [diff] [blame] | 116 | this.mRequiresDeviceUnlock = requiresUnlock; |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 117 | for (AidGroup aidGroup : staticAidGroups) { |
| 118 | this.mStaticAidGroups.put(aidGroup.category, aidGroup); |
| 119 | } |
| 120 | for (AidGroup aidGroup : dynamicAidGroups) { |
| 121 | this.mDynamicAidGroups.put(aidGroup.category, aidGroup); |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 122 | } |
Martijn Coenen | 58d2065 | 2013-09-05 21:05:53 -0700 | [diff] [blame] | 123 | this.mBannerResourceId = bannerResource; |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 124 | this.mUid = uid; |
Martijn Coenen | 8abf680 | 2015-04-20 13:44:58 +0200 | [diff] [blame] | 125 | this.mSettingsActivityName = settingsActivityName; |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Mathew Inwood | 57069dc | 2018-07-31 15:33:20 +0100 | [diff] [blame] | 128 | @UnsupportedAppUsage |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 129 | public ApduServiceInfo(PackageManager pm, ResolveInfo info, boolean onHost) throws |
| 130 | XmlPullParserException, IOException { |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 131 | ServiceInfo si = info.serviceInfo; |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 132 | XmlResourceParser parser = null; |
| 133 | try { |
Martijn Coenen | 38d3bb7 | 2013-08-07 20:03:03 -0700 | [diff] [blame] | 134 | if (onHost) { |
| 135 | parser = si.loadXmlMetaData(pm, HostApduService.SERVICE_META_DATA); |
| 136 | if (parser == null) { |
Martijn Coenen | 51b6322 | 2013-09-04 14:49:46 -0700 | [diff] [blame] | 137 | throw new XmlPullParserException("No " + HostApduService.SERVICE_META_DATA + |
| 138 | " meta-data"); |
Martijn Coenen | 38d3bb7 | 2013-08-07 20:03:03 -0700 | [diff] [blame] | 139 | } |
| 140 | } else { |
| 141 | parser = si.loadXmlMetaData(pm, OffHostApduService.SERVICE_META_DATA); |
| 142 | if (parser == null) { |
| 143 | throw new XmlPullParserException("No " + OffHostApduService.SERVICE_META_DATA + |
| 144 | " meta-data"); |
| 145 | } |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | int eventType = parser.getEventType(); |
| 149 | while (eventType != XmlPullParser.START_TAG && eventType != XmlPullParser.END_DOCUMENT) { |
| 150 | eventType = parser.next(); |
| 151 | } |
| 152 | |
| 153 | String tagName = parser.getName(); |
Martijn Coenen | 38d3bb7 | 2013-08-07 20:03:03 -0700 | [diff] [blame] | 154 | if (onHost && !"host-apdu-service".equals(tagName)) { |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 155 | throw new XmlPullParserException( |
| 156 | "Meta-data does not start with <host-apdu-service> tag"); |
Martijn Coenen | 38d3bb7 | 2013-08-07 20:03:03 -0700 | [diff] [blame] | 157 | } else if (!onHost && !"offhost-apdu-service".equals(tagName)) { |
| 158 | throw new XmlPullParserException( |
| 159 | "Meta-data does not start with <offhost-apdu-service> tag"); |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | Resources res = pm.getResourcesForApplication(si.applicationInfo); |
| 163 | AttributeSet attrs = Xml.asAttributeSet(parser); |
Martijn Coenen | 38d3bb7 | 2013-08-07 20:03:03 -0700 | [diff] [blame] | 164 | if (onHost) { |
| 165 | TypedArray sa = res.obtainAttributes(attrs, |
| 166 | com.android.internal.R.styleable.HostApduService); |
| 167 | mService = info; |
| 168 | mDescription = sa.getString( |
| 169 | com.android.internal.R.styleable.HostApduService_description); |
Martijn Coenen | c3f0044 | 2013-08-28 19:23:41 -0700 | [diff] [blame] | 170 | mRequiresDeviceUnlock = sa.getBoolean( |
| 171 | com.android.internal.R.styleable.HostApduService_requireDeviceUnlock, |
| 172 | false); |
Martijn Coenen | 58d2065 | 2013-09-05 21:05:53 -0700 | [diff] [blame] | 173 | mBannerResourceId = sa.getResourceId( |
| 174 | com.android.internal.R.styleable.HostApduService_apduServiceBanner, -1); |
Martijn Coenen | 8abf680 | 2015-04-20 13:44:58 +0200 | [diff] [blame] | 175 | mSettingsActivityName = sa.getString( |
| 176 | com.android.internal.R.styleable.HostApduService_settingsActivity); |
Martijn Coenen | 58d2065 | 2013-09-05 21:05:53 -0700 | [diff] [blame] | 177 | sa.recycle(); |
Martijn Coenen | 38d3bb7 | 2013-08-07 20:03:03 -0700 | [diff] [blame] | 178 | } else { |
| 179 | TypedArray sa = res.obtainAttributes(attrs, |
| 180 | com.android.internal.R.styleable.OffHostApduService); |
| 181 | mService = info; |
| 182 | mDescription = sa.getString( |
| 183 | com.android.internal.R.styleable.OffHostApduService_description); |
Martijn Coenen | c3f0044 | 2013-08-28 19:23:41 -0700 | [diff] [blame] | 184 | mRequiresDeviceUnlock = false; |
Martijn Coenen | 58d2065 | 2013-09-05 21:05:53 -0700 | [diff] [blame] | 185 | mBannerResourceId = sa.getResourceId( |
Martijn Coenen | eed4339 | 2013-09-26 11:20:37 -0700 | [diff] [blame] | 186 | com.android.internal.R.styleable.OffHostApduService_apduServiceBanner, -1); |
Martijn Coenen | 8abf680 | 2015-04-20 13:44:58 +0200 | [diff] [blame] | 187 | mSettingsActivityName = sa.getString( |
| 188 | com.android.internal.R.styleable.HostApduService_settingsActivity); |
Martijn Coenen | 58d2065 | 2013-09-05 21:05:53 -0700 | [diff] [blame] | 189 | sa.recycle(); |
Martijn Coenen | 38d3bb7 | 2013-08-07 20:03:03 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 192 | mStaticAidGroups = new HashMap<String, AidGroup>(); |
| 193 | mDynamicAidGroups = new HashMap<String, AidGroup>(); |
Martijn Coenen | 38d3bb7 | 2013-08-07 20:03:03 -0700 | [diff] [blame] | 194 | mOnHost = onHost; |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 195 | |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 196 | final int depth = parser.getDepth(); |
| 197 | AidGroup currentGroup = null; |
| 198 | |
| 199 | // Parsed values for the current AID group |
| 200 | while (((eventType = parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth) |
| 201 | && eventType != XmlPullParser.END_DOCUMENT) { |
| 202 | tagName = parser.getName(); |
| 203 | if (eventType == XmlPullParser.START_TAG && "aid-group".equals(tagName) && |
| 204 | currentGroup == null) { |
| 205 | final TypedArray groupAttrs = res.obtainAttributes(attrs, |
| 206 | com.android.internal.R.styleable.AidGroup); |
| 207 | // Get category of AID group |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 208 | String groupCategory = groupAttrs.getString( |
| 209 | com.android.internal.R.styleable.AidGroup_category); |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 210 | String groupDescription = groupAttrs.getString( |
| 211 | com.android.internal.R.styleable.AidGroup_description); |
Martijn Coenen | 5224608 | 2013-08-30 11:14:46 -0700 | [diff] [blame] | 212 | if (!CardEmulation.CATEGORY_PAYMENT.equals(groupCategory)) { |
| 213 | groupCategory = CardEmulation.CATEGORY_OTHER; |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 214 | } |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 215 | currentGroup = mStaticAidGroups.get(groupCategory); |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 216 | if (currentGroup != null) { |
Martijn Coenen | 5224608 | 2013-08-30 11:14:46 -0700 | [diff] [blame] | 217 | if (!CardEmulation.CATEGORY_OTHER.equals(groupCategory)) { |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 218 | Log.e(TAG, "Not allowing multiple aid-groups in the " + |
| 219 | groupCategory + " category"); |
| 220 | currentGroup = null; |
| 221 | } |
| 222 | } else { |
| 223 | currentGroup = new AidGroup(groupCategory, groupDescription); |
| 224 | } |
Martijn Coenen | 58d2065 | 2013-09-05 21:05:53 -0700 | [diff] [blame] | 225 | groupAttrs.recycle(); |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 226 | } else if (eventType == XmlPullParser.END_TAG && "aid-group".equals(tagName) && |
| 227 | currentGroup != null) { |
| 228 | if (currentGroup.aids.size() > 0) { |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 229 | if (!mStaticAidGroups.containsKey(currentGroup.category)) { |
| 230 | mStaticAidGroups.put(currentGroup.category, currentGroup); |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 231 | } |
| 232 | } else { |
| 233 | Log.e(TAG, "Not adding <aid-group> with empty or invalid AIDs"); |
| 234 | } |
| 235 | currentGroup = null; |
| 236 | } else if (eventType == XmlPullParser.START_TAG && "aid-filter".equals(tagName) && |
| 237 | currentGroup != null) { |
| 238 | final TypedArray a = res.obtainAttributes(attrs, |
| 239 | com.android.internal.R.styleable.AidFilter); |
Martijn Coenen | 62c196a | 2013-08-09 13:43:34 -0700 | [diff] [blame] | 240 | String aid = a.getString(com.android.internal.R.styleable.AidFilter_name). |
| 241 | toUpperCase(); |
Martijn Coenen | b514411 | 2014-07-02 12:44:33 -0700 | [diff] [blame] | 242 | if (CardEmulation.isValidAid(aid) && !currentGroup.aids.contains(aid)) { |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 243 | currentGroup.aids.add(aid); |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 244 | } else { |
| 245 | Log.e(TAG, "Ignoring invalid or duplicate aid: " + aid); |
| 246 | } |
Martijn Coenen | 58d2065 | 2013-09-05 21:05:53 -0700 | [diff] [blame] | 247 | a.recycle(); |
Martijn Coenen | 826a73b | 2014-08-05 07:51:58 -0700 | [diff] [blame] | 248 | } else if (eventType == XmlPullParser.START_TAG && |
| 249 | "aid-prefix-filter".equals(tagName) && currentGroup != null) { |
| 250 | final TypedArray a = res.obtainAttributes(attrs, |
| 251 | com.android.internal.R.styleable.AidFilter); |
| 252 | String aid = a.getString(com.android.internal.R.styleable.AidFilter_name). |
| 253 | toUpperCase(); |
| 254 | // Add wildcard char to indicate prefix |
Martijn Coenen | 0985fb6 | 2014-09-16 10:09:12 -0700 | [diff] [blame] | 255 | aid = aid.concat("*"); |
Martijn Coenen | 826a73b | 2014-08-05 07:51:58 -0700 | [diff] [blame] | 256 | if (CardEmulation.isValidAid(aid) && !currentGroup.aids.contains(aid)) { |
| 257 | currentGroup.aids.add(aid); |
| 258 | } else { |
| 259 | Log.e(TAG, "Ignoring invalid or duplicate aid: " + aid); |
| 260 | } |
| 261 | a.recycle(); |
Love Khanna | e910e8b | 2017-05-12 13:53:42 +0530 | [diff] [blame] | 262 | } else if (eventType == XmlPullParser.START_TAG && |
| 263 | tagName.equals("aid-suffix-filter") && currentGroup != null) { |
| 264 | final TypedArray a = res.obtainAttributes(attrs, |
| 265 | com.android.internal.R.styleable.AidFilter); |
| 266 | String aid = a.getString(com.android.internal.R.styleable.AidFilter_name). |
| 267 | toUpperCase(); |
| 268 | // Add wildcard char to indicate suffix |
| 269 | aid = aid.concat("#"); |
| 270 | if (CardEmulation.isValidAid(aid) && !currentGroup.aids.contains(aid)) { |
| 271 | currentGroup.aids.add(aid); |
| 272 | } else { |
| 273 | Log.e(TAG, "Ignoring invalid or duplicate aid: " + aid); |
| 274 | } |
| 275 | a.recycle(); |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | } catch (NameNotFoundException e) { |
| 279 | throw new XmlPullParserException("Unable to create context for: " + si.packageName); |
| 280 | } finally { |
| 281 | if (parser != null) parser.close(); |
| 282 | } |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 283 | // Set uid |
| 284 | mUid = si.applicationInfo.uid; |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | public ComponentName getComponent() { |
| 288 | return new ComponentName(mService.serviceInfo.packageName, |
| 289 | mService.serviceInfo.name); |
| 290 | } |
| 291 | |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 292 | /** |
| 293 | * Returns a consolidated list of AIDs from the AID groups |
| 294 | * registered by this service. Note that if a service has both |
| 295 | * a static (manifest-based) AID group for a category and a dynamic |
| 296 | * AID group, only the dynamically registered AIDs will be returned |
| 297 | * for that category. |
| 298 | * @return List of AIDs registered by the service |
| 299 | */ |
Martijn Coenen | ed3a29e | 2015-01-23 11:29:54 -0800 | [diff] [blame] | 300 | public List<String> getAids() { |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 301 | final ArrayList<String> aids = new ArrayList<String>(); |
| 302 | for (AidGroup group : getAidGroups()) { |
| 303 | aids.addAll(group.aids); |
| 304 | } |
| 305 | return aids; |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Martijn Coenen | ed3a29e | 2015-01-23 11:29:54 -0800 | [diff] [blame] | 308 | public List<String> getPrefixAids() { |
| 309 | final ArrayList<String> prefixAids = new ArrayList<String>(); |
| 310 | for (AidGroup group : getAidGroups()) { |
| 311 | for (String aid : group.aids) { |
| 312 | if (aid.endsWith("*")) { |
| 313 | prefixAids.add(aid); |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | return prefixAids; |
| 318 | } |
| 319 | |
Love Khanna | e910e8b | 2017-05-12 13:53:42 +0530 | [diff] [blame] | 320 | public List<String> getSubsetAids() { |
| 321 | final ArrayList<String> subsetAids = new ArrayList<String>(); |
| 322 | for (AidGroup group : getAidGroups()) { |
| 323 | for (String aid : group.aids) { |
| 324 | if (aid.endsWith("#")) { |
| 325 | subsetAids.add(aid); |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | return subsetAids; |
| 330 | } |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 331 | /** |
| 332 | * Returns the registered AID group for this category. |
| 333 | */ |
| 334 | public AidGroup getDynamicAidGroupForCategory(String category) { |
| 335 | return mDynamicAidGroups.get(category); |
| 336 | } |
| 337 | |
| 338 | public boolean removeDynamicAidGroupForCategory(String category) { |
| 339 | return (mDynamicAidGroups.remove(category) != null); |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * Returns a consolidated list of AID groups |
| 344 | * registered by this service. Note that if a service has both |
| 345 | * a static (manifest-based) AID group for a category and a dynamic |
| 346 | * AID group, only the dynamically registered AID group will be returned |
| 347 | * for that category. |
| 348 | * @return List of AIDs registered by the service |
| 349 | */ |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 350 | public ArrayList<AidGroup> getAidGroups() { |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 351 | final ArrayList<AidGroup> groups = new ArrayList<AidGroup>(); |
| 352 | for (Map.Entry<String, AidGroup> entry : mDynamicAidGroups.entrySet()) { |
| 353 | groups.add(entry.getValue()); |
| 354 | } |
| 355 | for (Map.Entry<String, AidGroup> entry : mStaticAidGroups.entrySet()) { |
| 356 | if (!mDynamicAidGroups.containsKey(entry.getKey())) { |
| 357 | // Consolidate AID groups - don't return static ones |
| 358 | // if a dynamic group exists for the category. |
| 359 | groups.add(entry.getValue()); |
| 360 | } |
| 361 | } |
| 362 | return groups; |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 363 | } |
| 364 | |
Martijn Coenen | 2f6f3a01 | 2014-04-25 17:00:21 -0700 | [diff] [blame] | 365 | /** |
| 366 | * Returns the category to which this service has attributed the AID that is passed in, |
| 367 | * or null if we don't know this AID. |
| 368 | */ |
| 369 | public String getCategoryForAid(String aid) { |
| 370 | ArrayList<AidGroup> groups = getAidGroups(); |
| 371 | for (AidGroup group : groups) { |
Martijn Coenen | 5e991a1 | 2014-09-16 16:03:28 -0700 | [diff] [blame] | 372 | if (group.aids.contains(aid.toUpperCase())) { |
Martijn Coenen | 2f6f3a01 | 2014-04-25 17:00:21 -0700 | [diff] [blame] | 373 | return group.category; |
| 374 | } |
| 375 | } |
| 376 | return null; |
| 377 | } |
| 378 | |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 379 | public boolean hasCategory(String category) { |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 380 | return (mStaticAidGroups.containsKey(category) || mDynamicAidGroups.containsKey(category)); |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Mathew Inwood | 57069dc | 2018-07-31 15:33:20 +0100 | [diff] [blame] | 383 | @UnsupportedAppUsage |
Martijn Coenen | 38d3bb7 | 2013-08-07 20:03:03 -0700 | [diff] [blame] | 384 | public boolean isOnHost() { |
| 385 | return mOnHost; |
| 386 | } |
| 387 | |
Mathew Inwood | 57069dc | 2018-07-31 15:33:20 +0100 | [diff] [blame] | 388 | @UnsupportedAppUsage |
Martijn Coenen | c3f0044 | 2013-08-28 19:23:41 -0700 | [diff] [blame] | 389 | public boolean requiresUnlock() { |
| 390 | return mRequiresDeviceUnlock; |
| 391 | } |
| 392 | |
Mathew Inwood | 57069dc | 2018-07-31 15:33:20 +0100 | [diff] [blame] | 393 | @UnsupportedAppUsage |
Martijn Coenen | fca35787 | 2013-09-18 17:26:18 +0200 | [diff] [blame] | 394 | public String getDescription() { |
| 395 | return mDescription; |
| 396 | } |
| 397 | |
Mathew Inwood | 57069dc | 2018-07-31 15:33:20 +0100 | [diff] [blame] | 398 | @UnsupportedAppUsage |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 399 | public int getUid() { |
| 400 | return mUid; |
| 401 | } |
| 402 | |
| 403 | public void setOrReplaceDynamicAidGroup(AidGroup aidGroup) { |
| 404 | mDynamicAidGroups.put(aidGroup.getCategory(), aidGroup); |
| 405 | } |
| 406 | |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 407 | public CharSequence loadLabel(PackageManager pm) { |
| 408 | return mService.loadLabel(pm); |
| 409 | } |
| 410 | |
Martijn Coenen | 8abf680 | 2015-04-20 13:44:58 +0200 | [diff] [blame] | 411 | public CharSequence loadAppLabel(PackageManager pm) { |
| 412 | try { |
| 413 | return pm.getApplicationLabel(pm.getApplicationInfo( |
| 414 | mService.resolvePackageName, PackageManager.GET_META_DATA)); |
| 415 | } catch (PackageManager.NameNotFoundException e) { |
| 416 | return null; |
| 417 | } |
| 418 | } |
| 419 | |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 420 | public Drawable loadIcon(PackageManager pm) { |
| 421 | return mService.loadIcon(pm); |
| 422 | } |
| 423 | |
Mathew Inwood | 57069dc | 2018-07-31 15:33:20 +0100 | [diff] [blame] | 424 | @UnsupportedAppUsage |
Martijn Coenen | 58d2065 | 2013-09-05 21:05:53 -0700 | [diff] [blame] | 425 | public Drawable loadBanner(PackageManager pm) { |
| 426 | Resources res; |
| 427 | try { |
| 428 | res = pm.getResourcesForApplication(mService.serviceInfo.packageName); |
| 429 | Drawable banner = res.getDrawable(mBannerResourceId); |
| 430 | return banner; |
| 431 | } catch (NotFoundException e) { |
| 432 | Log.e(TAG, "Could not load banner."); |
| 433 | return null; |
| 434 | } catch (NameNotFoundException e) { |
| 435 | Log.e(TAG, "Could not load banner."); |
| 436 | return null; |
| 437 | } |
| 438 | } |
Martijn Coenen | 8abf680 | 2015-04-20 13:44:58 +0200 | [diff] [blame] | 439 | |
Mathew Inwood | 57069dc | 2018-07-31 15:33:20 +0100 | [diff] [blame] | 440 | @UnsupportedAppUsage |
Martijn Coenen | 8abf680 | 2015-04-20 13:44:58 +0200 | [diff] [blame] | 441 | public String getSettingsActivityName() { return mSettingsActivityName; } |
Martijn Coenen | 58d2065 | 2013-09-05 21:05:53 -0700 | [diff] [blame] | 442 | |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 443 | @Override |
| 444 | public String toString() { |
| 445 | StringBuilder out = new StringBuilder("ApduService: "); |
| 446 | out.append(getComponent()); |
| 447 | out.append(", description: " + mDescription); |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 448 | out.append(", Static AID Groups: "); |
| 449 | for (AidGroup aidGroup : mStaticAidGroups.values()) { |
| 450 | out.append(aidGroup.toString()); |
| 451 | } |
| 452 | out.append(", Dynamic AID Groups: "); |
| 453 | for (AidGroup aidGroup : mDynamicAidGroups.values()) { |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 454 | out.append(aidGroup.toString()); |
| 455 | } |
| 456 | return out.toString(); |
| 457 | } |
| 458 | |
| 459 | @Override |
| 460 | public boolean equals(Object o) { |
| 461 | if (this == o) return true; |
| 462 | if (!(o instanceof ApduServiceInfo)) return false; |
| 463 | ApduServiceInfo thatService = (ApduServiceInfo) o; |
| 464 | |
| 465 | return thatService.getComponent().equals(this.getComponent()); |
| 466 | } |
| 467 | |
| 468 | @Override |
| 469 | public int hashCode() { |
| 470 | return getComponent().hashCode(); |
| 471 | } |
| 472 | |
| 473 | |
| 474 | @Override |
| 475 | public int describeContents() { |
| 476 | return 0; |
| 477 | } |
| 478 | |
| 479 | @Override |
| 480 | public void writeToParcel(Parcel dest, int flags) { |
| 481 | mService.writeToParcel(dest, flags); |
| 482 | dest.writeString(mDescription); |
Martijn Coenen | 38d3bb7 | 2013-08-07 20:03:03 -0700 | [diff] [blame] | 483 | dest.writeInt(mOnHost ? 1 : 0); |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 484 | dest.writeInt(mStaticAidGroups.size()); |
| 485 | if (mStaticAidGroups.size() > 0) { |
| 486 | dest.writeTypedList(new ArrayList<AidGroup>(mStaticAidGroups.values())); |
| 487 | } |
| 488 | dest.writeInt(mDynamicAidGroups.size()); |
| 489 | if (mDynamicAidGroups.size() > 0) { |
| 490 | dest.writeTypedList(new ArrayList<AidGroup>(mDynamicAidGroups.values())); |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 491 | } |
Martijn Coenen | c3f0044 | 2013-08-28 19:23:41 -0700 | [diff] [blame] | 492 | dest.writeInt(mRequiresDeviceUnlock ? 1 : 0); |
Martijn Coenen | 58d2065 | 2013-09-05 21:05:53 -0700 | [diff] [blame] | 493 | dest.writeInt(mBannerResourceId); |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 494 | dest.writeInt(mUid); |
Martijn Coenen | 8abf680 | 2015-04-20 13:44:58 +0200 | [diff] [blame] | 495 | dest.writeString(mSettingsActivityName); |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 496 | }; |
| 497 | |
Mathew Inwood | 57069dc | 2018-07-31 15:33:20 +0100 | [diff] [blame] | 498 | @UnsupportedAppUsage |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 499 | public static final Parcelable.Creator<ApduServiceInfo> CREATOR = |
| 500 | new Parcelable.Creator<ApduServiceInfo>() { |
| 501 | @Override |
| 502 | public ApduServiceInfo createFromParcel(Parcel source) { |
| 503 | ResolveInfo info = ResolveInfo.CREATOR.createFromParcel(source); |
| 504 | String description = source.readString(); |
Martijn Coenen | 5e991a1 | 2014-09-16 16:03:28 -0700 | [diff] [blame] | 505 | boolean onHost = source.readInt() != 0; |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 506 | ArrayList<AidGroup> staticAidGroups = new ArrayList<AidGroup>(); |
| 507 | int numStaticGroups = source.readInt(); |
| 508 | if (numStaticGroups > 0) { |
| 509 | source.readTypedList(staticAidGroups, AidGroup.CREATOR); |
| 510 | } |
| 511 | ArrayList<AidGroup> dynamicAidGroups = new ArrayList<AidGroup>(); |
| 512 | int numDynamicGroups = source.readInt(); |
| 513 | if (numDynamicGroups > 0) { |
| 514 | source.readTypedList(dynamicAidGroups, AidGroup.CREATOR); |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 515 | } |
Martijn Coenen | 5e991a1 | 2014-09-16 16:03:28 -0700 | [diff] [blame] | 516 | boolean requiresUnlock = source.readInt() != 0; |
Martijn Coenen | 58d2065 | 2013-09-05 21:05:53 -0700 | [diff] [blame] | 517 | int bannerResource = source.readInt(); |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 518 | int uid = source.readInt(); |
Martijn Coenen | 8abf680 | 2015-04-20 13:44:58 +0200 | [diff] [blame] | 519 | String settingsActivityName = source.readString(); |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 520 | return new ApduServiceInfo(info, onHost, description, staticAidGroups, |
Martijn Coenen | a2344ea | 2015-06-05 10:45:50 +0200 | [diff] [blame] | 521 | dynamicAidGroups, requiresUnlock, bannerResource, uid, |
Martijn Coenen | 8abf680 | 2015-04-20 13:44:58 +0200 | [diff] [blame] | 522 | settingsActivityName); |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | @Override |
| 526 | public ApduServiceInfo[] newArray(int size) { |
| 527 | return new ApduServiceInfo[size]; |
| 528 | } |
| 529 | }; |
| 530 | |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 531 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 532 | pw.println(" " + getComponent() + |
| 533 | " (Description: " + getDescription() + ")"); |
| 534 | pw.println(" Static AID groups:"); |
| 535 | for (AidGroup group : mStaticAidGroups.values()) { |
| 536 | pw.println(" Category: " + group.category); |
| 537 | for (String aid : group.aids) { |
| 538 | pw.println(" AID: " + aid); |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 539 | } |
| 540 | } |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 541 | pw.println(" Dynamic AID groups:"); |
| 542 | for (AidGroup group : mDynamicAidGroups.values()) { |
| 543 | pw.println(" Category: " + group.category); |
| 544 | for (String aid : group.aids) { |
| 545 | pw.println(" AID: " + aid); |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 546 | } |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 547 | } |
Martijn Coenen | 8abf680 | 2015-04-20 13:44:58 +0200 | [diff] [blame] | 548 | pw.println(" Settings Activity: " + mSettingsActivityName); |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 549 | } |
| 550 | } |