nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [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 | */ |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 16 | /****************************************************************************** |
| 17 | * |
| 18 | * The original Work has been changed by NXP. |
| 19 | * |
| 20 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 21 | * you may not use this file except in compliance with the License. |
| 22 | * You may obtain a copy of the License at |
| 23 | * |
| 24 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 25 | * |
| 26 | * Unless required by applicable law or agreed to in writing, software |
| 27 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 28 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 29 | * See the License for the specific language governing permissions and |
| 30 | * limitations under the License. |
| 31 | * |
| 32 | * Copyright 2018-2019 NXP |
| 33 | * |
| 34 | ******************************************************************************/ |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 35 | package com.android.nfc.cardemulation; |
| 36 | |
| 37 | import org.xmlpull.v1.XmlPullParser; |
| 38 | import org.xmlpull.v1.XmlPullParserException; |
| 39 | import org.xmlpull.v1.XmlSerializer; |
| 40 | |
| 41 | import android.app.ActivityManager; |
| 42 | import android.content.BroadcastReceiver; |
| 43 | import android.content.ComponentName; |
| 44 | import android.content.Context; |
| 45 | import android.content.Intent; |
| 46 | import android.content.IntentFilter; |
| 47 | import android.content.pm.PackageManager; |
| 48 | import android.content.pm.ResolveInfo; |
| 49 | import android.content.pm.ServiceInfo; |
| 50 | import android.content.pm.PackageManager.NameNotFoundException; |
| 51 | import android.net.Uri; |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 52 | import android.nfc.cardemulation.NfcAidGroup; |
| 53 | import android.nfc.cardemulation.NfcApduServiceInfo; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 54 | import android.nfc.cardemulation.AidGroup; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 55 | import android.nfc.cardemulation.CardEmulation; |
| 56 | import android.nfc.cardemulation.HostApduService; |
| 57 | import android.nfc.cardemulation.OffHostApduService; |
| 58 | import android.os.UserHandle; |
| 59 | import android.util.AtomicFile; |
| 60 | import android.util.Log; |
| 61 | import android.util.SparseArray; |
| 62 | import android.util.Xml; |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 63 | import com.nxp.nfc.NfcConstants; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 64 | |
| 65 | import com.android.internal.util.FastXmlSerializer; |
| 66 | import com.google.android.collect.Maps; |
| 67 | |
| 68 | import java.io.File; |
| 69 | import java.io.FileDescriptor; |
| 70 | import java.io.FileInputStream; |
| 71 | import java.io.FileOutputStream; |
| 72 | import java.io.IOException; |
| 73 | import java.io.PrintWriter; |
| 74 | import java.util.ArrayList; |
| 75 | import java.util.Collections; |
| 76 | import java.util.HashMap; |
| 77 | import java.util.Iterator; |
| 78 | import java.util.List; |
| 79 | import java.util.Map; |
| 80 | import java.util.concurrent.atomic.AtomicReference; |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 81 | import com.android.nfc.NfcService; |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 82 | import android.os.SystemProperties; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * This class is inspired by android.content.pm.RegisteredServicesCache |
| 86 | * That class was not re-used because it doesn't support dynamically |
| 87 | * registering additional properties, but generates everything from |
| 88 | * the manifest. Since we have some properties that are not in the manifest, |
| 89 | * it's less suited. |
| 90 | */ |
| 91 | public class RegisteredServicesCache { |
| 92 | static final String XML_INDENT_OUTPUT_FEATURE = "http://xmlpull.org/v1/doc/features.html#indent-output"; |
| 93 | static final String TAG = "RegisteredServicesCache"; |
nxf50051 | 4ff9f42 | 2019-03-20 16:28:16 +0530 | [diff] [blame] | 94 | static final boolean DEBUG = |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 95 | ((SystemProperties.get("persist.nfc.ce_debug").equals("1")) ? true : false); |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 96 | static final String SERVICE_STATE_FILE_VERSION="1.0"; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 97 | |
| 98 | final Context mContext; |
| 99 | final AtomicReference<BroadcastReceiver> mReceiver; |
| 100 | |
| 101 | final Object mLock = new Object(); |
| 102 | // All variables below synchronized on mLock |
| 103 | |
| 104 | // mUserServices holds the card emulation services that are running for each user |
| 105 | final SparseArray<UserServices> mUserServices = new SparseArray<UserServices>(); |
| 106 | final Callback mCallback; |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 107 | final AtomicFile mDynamicSettingsFile; |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 108 | final AtomicFile mServiceStateFile; |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 109 | final HashMap<ComponentName, NfcApduServiceInfo> mAllServices = Maps.newHashMap(); |
nxpandroid | fc2aab8 | 2017-04-10 18:19:30 +0530 | [diff] [blame] | 110 | HashMap<String, HashMap<ComponentName, Integer>> installedServices = new HashMap<>(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 111 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 112 | public interface Callback { |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 113 | void onServicesUpdated(int userId, final List<NfcApduServiceInfo> services); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 114 | }; |
| 115 | |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 116 | static class DynamicSettings { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 117 | public final int uid; |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 118 | public final HashMap<String, NfcAidGroup> aidGroups = Maps.newHashMap(); |
| 119 | public String offHostSE; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 120 | |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 121 | DynamicSettings(int uid) { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 122 | this.uid = uid; |
| 123 | } |
| 124 | }; |
| 125 | |
| 126 | private static class UserServices { |
| 127 | /** |
| 128 | * All services that have registered |
| 129 | */ |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 130 | final HashMap<ComponentName, NfcApduServiceInfo> services = |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 131 | Maps.newHashMap(); // Re-built at run-time |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 132 | final HashMap<ComponentName, DynamicSettings> dynamicSettings = |
| 133 | Maps.newHashMap(); // In memory cache of dynamic Setting store |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | private UserServices findOrCreateUserLocked(int userId) { |
| 137 | UserServices services = mUserServices.get(userId); |
| 138 | if (services == null) { |
| 139 | services = new UserServices(); |
| 140 | mUserServices.put(userId, services); |
| 141 | } |
| 142 | return services; |
| 143 | } |
| 144 | |
| 145 | public RegisteredServicesCache(Context context, Callback callback) { |
| 146 | mContext = context; |
| 147 | mCallback = callback; |
| 148 | |
| 149 | final BroadcastReceiver receiver = new BroadcastReceiver() { |
| 150 | @Override |
| 151 | public void onReceive(Context context, Intent intent) { |
| 152 | final int uid = intent.getIntExtra(Intent.EXTRA_UID, -1); |
| 153 | String action = intent.getAction(); |
| 154 | if (DEBUG) Log.d(TAG, "Intent action: " + action); |
| 155 | if (uid != -1) { |
nxpandroid | 5d64ce9 | 2016-11-18 19:48:53 +0530 | [diff] [blame] | 156 | int currentUser = ActivityManager.getCurrentUser(); |
| 157 | if (currentUser == UserHandle.getUserId(uid)) { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 158 | if(Intent.ACTION_PACKAGE_REMOVED.equals(action)) { |
| 159 | Uri uri = intent.getData(); |
| 160 | String pkg = uri != null ? uri.getSchemeSpecificPart() : null; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 161 | } |
nxpandroid | 5d64ce9 | 2016-11-18 19:48:53 +0530 | [diff] [blame] | 162 | boolean replaced = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false) && |
| 163 | (Intent.ACTION_PACKAGE_ADDED.equals(action) || |
| 164 | Intent.ACTION_PACKAGE_REMOVED.equals(action)); |
| 165 | if (!replaced) { |
| 166 | invalidateCache(UserHandle.getUserId(uid)); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 167 | } else { |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 168 | // Cache will automatically be updated on user switch |
| 169 | if (DEBUG) Log.d(TAG, " Not removing service here " + replaced); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 170 | } |
| 171 | } else { |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 172 | if (DEBUG) Log.d(TAG, "Ignoring package intent due to package being replaced."); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | } |
| 176 | }; |
| 177 | mReceiver = new AtomicReference<BroadcastReceiver>(receiver); |
| 178 | |
| 179 | IntentFilter intentFilter = new IntentFilter(); |
| 180 | intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED); |
| 181 | intentFilter.addAction(Intent.ACTION_PACKAGE_CHANGED); |
| 182 | intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 183 | intentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED); |
| 184 | intentFilter.addAction(Intent.ACTION_PACKAGE_FIRST_LAUNCH); |
| 185 | intentFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED); |
| 186 | intentFilter.addDataScheme("package"); |
| 187 | mContext.registerReceiverAsUser(mReceiver.get(), UserHandle.ALL, intentFilter, null, null); |
| 188 | |
| 189 | // Register for events related to sdcard operations |
| 190 | IntentFilter sdFilter = new IntentFilter(); |
| 191 | sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE); |
| 192 | sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE); |
| 193 | mContext.registerReceiverAsUser(mReceiver.get(), UserHandle.ALL, sdFilter, null, null); |
| 194 | |
| 195 | File dataDir = mContext.getFilesDir(); |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 196 | mDynamicSettingsFile = new AtomicFile(new File(dataDir, "dynamic_aids.xml")); |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 197 | mServiceStateFile = new AtomicFile(new File(dataDir, "service_state.xml")); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 198 | } |
| 199 | |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 200 | void dump(ArrayList<NfcApduServiceInfo> services) { |
| 201 | for (NfcApduServiceInfo service : services) { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 202 | if (DEBUG) Log.d(TAG, service.toString()); |
| 203 | } |
| 204 | } |
| 205 | |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 206 | boolean containsServiceLocked(ArrayList<NfcApduServiceInfo> services, ComponentName serviceName) { |
| 207 | for (NfcApduServiceInfo service : services) { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 208 | if (service.getComponent().equals(serviceName)) return true; |
| 209 | } |
| 210 | return false; |
| 211 | } |
| 212 | |
| 213 | public boolean hasService(int userId, ComponentName service) { |
| 214 | return getService(userId, service) != null; |
| 215 | } |
| 216 | |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 217 | public NfcApduServiceInfo getService(int userId, ComponentName service) { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 218 | synchronized (mLock) { |
| 219 | UserServices userServices = findOrCreateUserLocked(userId); |
| 220 | return userServices.services.get(service); |
| 221 | } |
| 222 | } |
| 223 | |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 224 | public List<NfcApduServiceInfo> getServices(int userId) { |
| 225 | final ArrayList<NfcApduServiceInfo> services = new ArrayList<NfcApduServiceInfo>(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 226 | synchronized (mLock) { |
| 227 | UserServices userServices = findOrCreateUserLocked(userId); |
| 228 | services.addAll(userServices.services.values()); |
| 229 | } |
| 230 | return services; |
| 231 | } |
| 232 | |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 233 | public List<NfcApduServiceInfo> getServicesForCategory(int userId, String category) { |
| 234 | final ArrayList<NfcApduServiceInfo> services = new ArrayList<NfcApduServiceInfo>(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 235 | synchronized (mLock) { |
| 236 | UserServices userServices = findOrCreateUserLocked(userId); |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 237 | for (NfcApduServiceInfo service : userServices.services.values()) { |
nxpandroid | ebf53fb | 2016-12-22 18:48:59 +0530 | [diff] [blame] | 238 | if (service.hasCategory(category) && |
| 239 | (service.getAidCacheSizeForCategory(category) > 0)) services.add(service); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | return services; |
| 243 | } |
| 244 | |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 245 | ArrayList<NfcApduServiceInfo> getInstalledServices(int userId) { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 246 | PackageManager pm; |
| 247 | try { |
| 248 | pm = mContext.createPackageContextAsUser("android", 0, |
| 249 | new UserHandle(userId)).getPackageManager(); |
| 250 | } catch (NameNotFoundException e) { |
| 251 | Log.e(TAG, "Could not create user package context"); |
| 252 | return null; |
| 253 | } |
| 254 | mAllServices.clear(); |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 255 | ArrayList<NfcApduServiceInfo> validServices = new ArrayList<NfcApduServiceInfo>(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 256 | |
nxpandroid | 3509fde | 2017-09-14 11:16:20 +0530 | [diff] [blame] | 257 | List<ResolveInfo> resolvedServices = new ArrayList<>(pm.queryIntentServicesAsUser( |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 258 | new Intent(HostApduService.SERVICE_INTERFACE), |
nxpandroid | 3509fde | 2017-09-14 11:16:20 +0530 | [diff] [blame] | 259 | PackageManager.GET_META_DATA, userId)); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 260 | |
| 261 | List<ResolveInfo> resolvedOffHostServices = pm.queryIntentServicesAsUser( |
| 262 | new Intent(OffHostApduService.SERVICE_INTERFACE), |
| 263 | PackageManager.GET_META_DATA, userId); |
| 264 | resolvedServices.addAll(resolvedOffHostServices); |
| 265 | |
| 266 | for (ResolveInfo resolvedService : resolvedServices) { |
| 267 | try { |
| 268 | boolean onHost = !resolvedOffHostServices.contains(resolvedService); |
| 269 | ServiceInfo si = resolvedService.serviceInfo; |
| 270 | ComponentName componentName = new ComponentName(si.packageName, si.name); |
| 271 | // Check if the package holds the NFC permission |
| 272 | if (pm.checkPermission(android.Manifest.permission.NFC, si.packageName) != |
| 273 | PackageManager.PERMISSION_GRANTED) { |
nxpandroid | f8260a2 | 2017-05-12 16:07:09 +0530 | [diff] [blame] | 274 | Log.e(TAG, "Skipping application component " + componentName + |
| 275 | ": it must request the permission " + |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 276 | android.Manifest.permission.NFC); |
| 277 | continue; |
| 278 | } |
| 279 | if (!android.Manifest.permission.BIND_NFC_SERVICE.equals( |
| 280 | si.permission)) { |
| 281 | Log.e(TAG, "Skipping APDU service " + componentName + |
| 282 | ": it does not require the permission " + |
| 283 | android.Manifest.permission.BIND_NFC_SERVICE); |
| 284 | continue; |
| 285 | } |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 286 | NfcApduServiceInfo service = new NfcApduServiceInfo(pm, resolvedService, onHost); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 287 | if (service != null) { |
| 288 | validServices.add(service); |
| 289 | if(!onHost) |
| 290 | mAllServices.put(componentName, service); |
| 291 | } |
| 292 | } catch (XmlPullParserException e) { |
| 293 | Log.w(TAG, "Unable to load component info " + resolvedService.toString(), e); |
| 294 | } catch (IOException e) { |
| 295 | Log.w(TAG, "Unable to load component info " + resolvedService.toString(), e); |
| 296 | } |
| 297 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 298 | return validServices; |
| 299 | } |
| 300 | |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 301 | public ArrayList<NfcApduServiceInfo> getAllServices() { |
| 302 | return new ArrayList<NfcApduServiceInfo>(mAllServices.values());//mAllServices; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 303 | } |
| 304 | |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 305 | public HashMap<ComponentName, NfcApduServiceInfo> getAllStaticHashServices() { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 306 | return mAllServices; |
| 307 | } |
| 308 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 309 | public void invalidateCache(int userId) { |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 310 | final ArrayList<NfcApduServiceInfo> validServices = getInstalledServices(userId); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 311 | if (validServices == null) { |
| 312 | return; |
| 313 | } |
| 314 | synchronized (mLock) { |
| 315 | UserServices userServices = findOrCreateUserLocked(userId); |
| 316 | |
| 317 | // Find removed services |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 318 | Iterator<Map.Entry<ComponentName, NfcApduServiceInfo>> it = |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 319 | userServices.services.entrySet().iterator(); |
| 320 | while (it.hasNext()) { |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 321 | Map.Entry<ComponentName, NfcApduServiceInfo> entry = |
| 322 | (Map.Entry<ComponentName, NfcApduServiceInfo>) it.next(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 323 | if (!containsServiceLocked(validServices, entry.getKey())) { |
| 324 | Log.d(TAG, "Service removed: " + entry.getKey()); |
| 325 | it.remove(); |
| 326 | } |
| 327 | } |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 328 | for (NfcApduServiceInfo service : validServices) { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 329 | if (DEBUG) Log.d(TAG, "Adding service: " + service.getComponent() + |
| 330 | " AIDs: " + service.getAids()); |
| 331 | userServices.services.put(service.getComponent(), service); |
| 332 | } |
| 333 | |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 334 | // Apply dynamic Setting mappings |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 335 | ArrayList<ComponentName> toBeRemoved = new ArrayList<ComponentName>(); |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 336 | for (Map.Entry<ComponentName, DynamicSettings> entry : |
| 337 | userServices.dynamicSettings.entrySet()) { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 338 | // Verify component / uid match |
| 339 | ComponentName component = entry.getKey(); |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 340 | DynamicSettings dynamicSettings = entry.getValue(); |
| 341 | NfcApduServiceInfo serviceInfo = userServices.services.get(component); |
| 342 | if (serviceInfo == null || (serviceInfo.getUid() != dynamicSettings.uid)) { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 343 | toBeRemoved.add(component); |
| 344 | continue; |
| 345 | } else { |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 346 | for (NfcAidGroup group : dynamicSettings.aidGroups.values()) { |
| 347 | serviceInfo.setOrReplaceDynamicNfcAidGroup(group); |
| 348 | } |
| 349 | if (dynamicSettings.offHostSE != null) { |
| 350 | serviceInfo.setOffHostSecureElement(dynamicSettings.offHostSE); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 351 | } |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | if (toBeRemoved.size() > 0) { |
| 356 | for (ComponentName component : toBeRemoved) { |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 357 | Log.d(TAG, "Removing dynamic Settings registered by " + component); |
| 358 | userServices.dynamicSettings.remove(component); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 359 | } |
| 360 | // Persist to filesystem |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 361 | writeDynamicSettingsLocked(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 362 | } |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 363 | updateServiceStateFromFile(userId); |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 364 | Log.e(TAG,"1"+Thread.currentThread().getStackTrace()[2].getMethodName()+":WriteServiceStateToFile"); |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 365 | writeServiceStateToFile(userId); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | mCallback.onServicesUpdated(userId, Collections.unmodifiableList(validServices)); |
| 369 | dump(validServices); |
| 370 | } |
| 371 | |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 372 | private void readDynamicSettingsLocked() { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 373 | FileInputStream fis = null; |
| 374 | try { |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 375 | if (!mDynamicSettingsFile.getBaseFile().exists()) { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 376 | Log.d(TAG, "Dynamic AIDs file does not exist."); |
| 377 | return; |
| 378 | } |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 379 | fis = mDynamicSettingsFile.openRead(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 380 | XmlPullParser parser = Xml.newPullParser(); |
| 381 | parser.setInput(fis, null); |
| 382 | int eventType = parser.getEventType(); |
| 383 | while (eventType != XmlPullParser.START_TAG && |
| 384 | eventType != XmlPullParser.END_DOCUMENT) { |
| 385 | eventType = parser.next(); |
| 386 | } |
| 387 | String tagName = parser.getName(); |
| 388 | if ("services".equals(tagName)) { |
| 389 | boolean inService = false; |
| 390 | ComponentName currentComponent = null; |
| 391 | int currentUid = -1; |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 392 | String currentOffHostSE = null; |
| 393 | ArrayList<NfcAidGroup> currentGroups = new ArrayList<NfcAidGroup>(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 394 | while (eventType != XmlPullParser.END_DOCUMENT) { |
| 395 | tagName = parser.getName(); |
| 396 | if (eventType == XmlPullParser.START_TAG) { |
| 397 | if ("service".equals(tagName) && parser.getDepth() == 2) { |
| 398 | String compString = parser.getAttributeValue(null, "component"); |
| 399 | String uidString = parser.getAttributeValue(null, "uid"); |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 400 | String offHostString = parser.getAttributeValue(null, "offHostSE"); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 401 | if (compString == null || uidString == null) { |
| 402 | Log.e(TAG, "Invalid service attributes"); |
| 403 | } else { |
| 404 | try { |
| 405 | currentUid = Integer.parseInt(uidString); |
| 406 | currentComponent = ComponentName.unflattenFromString(compString); |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 407 | currentOffHostSE = offHostString; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 408 | inService = true; |
| 409 | } catch (NumberFormatException e) { |
| 410 | Log.e(TAG, "Could not parse service uid"); |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | if ("aid-group".equals(tagName) && parser.getDepth() == 3 && inService) { |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 415 | NfcAidGroup group = NfcAidGroup.createFromXml(parser); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 416 | if (group != null) { |
| 417 | currentGroups.add(group); |
| 418 | } else { |
| 419 | Log.e(TAG, "Could not parse AID group."); |
| 420 | } |
| 421 | } |
| 422 | } else if (eventType == XmlPullParser.END_TAG) { |
| 423 | if ("service".equals(tagName)) { |
| 424 | // See if we have a valid service |
| 425 | if (currentComponent != null && currentUid >= 0 && |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 426 | (currentGroups.size() > 0 || currentOffHostSE != null)) { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 427 | final int userId = UserHandle.getUserId(currentUid); |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 428 | DynamicSettings dynSettings = new DynamicSettings(currentUid); |
| 429 | for (NfcAidGroup group : currentGroups) { |
| 430 | dynSettings.aidGroups.put(group.getCategory(), group); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 431 | } |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 432 | dynSettings.offHostSE = currentOffHostSE; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 433 | UserServices services = findOrCreateUserLocked(userId); |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 434 | services.dynamicSettings.put(currentComponent, dynSettings); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 435 | } |
| 436 | currentUid = -1; |
| 437 | currentComponent = null; |
| 438 | currentGroups.clear(); |
| 439 | inService = false; |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 440 | currentOffHostSE = null; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | eventType = parser.next(); |
| 444 | }; |
| 445 | } |
| 446 | } catch (Exception e) { |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 447 | Log.e(TAG, "Could not parse dynamic Settings file, trashing."); |
| 448 | mDynamicSettingsFile.delete(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 449 | } finally { |
| 450 | if (fis != null) { |
| 451 | try { |
| 452 | fis.close(); |
| 453 | } catch (IOException e) { |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 459 | private boolean writeDynamicSettingsLocked() { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 460 | FileOutputStream fos = null; |
| 461 | try { |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 462 | fos = mDynamicSettingsFile.startWrite(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 463 | XmlSerializer out = new FastXmlSerializer(); |
| 464 | out.setOutput(fos, "utf-8"); |
| 465 | out.startDocument(null, true); |
| 466 | out.setFeature(XML_INDENT_OUTPUT_FEATURE, true); |
| 467 | out.startTag(null, "services"); |
| 468 | for (int i = 0; i < mUserServices.size(); i++) { |
| 469 | final UserServices user = mUserServices.valueAt(i); |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 470 | for (Map.Entry<ComponentName, DynamicSettings> service : user.dynamicSettings.entrySet()) { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 471 | out.startTag(null, "service"); |
| 472 | out.attribute(null, "component", service.getKey().flattenToString()); |
| 473 | out.attribute(null, "uid", Integer.toString(service.getValue().uid)); |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 474 | if(service.getValue().offHostSE != null) { |
| 475 | out.attribute(null, "offHostSE", service.getValue().offHostSE); |
| 476 | } |
| 477 | for (NfcAidGroup group : service.getValue().aidGroups.values()) { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 478 | group.writeAsXml(out); |
| 479 | } |
| 480 | out.endTag(null, "service"); |
| 481 | } |
| 482 | } |
| 483 | out.endTag(null, "services"); |
| 484 | out.endDocument(); |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 485 | mDynamicSettingsFile.finishWrite(fos); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 486 | return true; |
| 487 | } catch (Exception e) { |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 488 | Log.e(TAG, "Error writing dynamic Settings", e); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 489 | if (fos != null) { |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 490 | mDynamicSettingsFile.failWrite(fos); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 491 | } |
| 492 | return false; |
| 493 | } |
| 494 | } |
| 495 | |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 496 | private void updateServiceStateFromFile(int currUserId) |
| 497 | { |
| 498 | FileInputStream fis = null; |
| 499 | try { |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 500 | /*if(NfcService.getInstance().getAidRoutingTableStatus() == 0x00) { |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 501 | Log.e(TAG, " Aid Routing Table still availble , No need to disable services"); |
| 502 | return; |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 503 | }*/ |
| 504 | Log.d(TAG, " Reading service state data always from file"); |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 505 | if(!mServiceStateFile.getBaseFile().exists()) { |
| 506 | Log.d(TAG,"mServiceStateFile does not exist"); |
| 507 | return; |
| 508 | } |
| 509 | fis = mServiceStateFile.openRead(); |
| 510 | XmlPullParser parser = Xml.newPullParser(); |
| 511 | parser.setInput(fis , null); |
| 512 | int eventType = parser.getEventType(); |
| 513 | int currUid = -1; |
| 514 | ComponentName currComponent = null; |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 515 | //HashMap<ComponentName ,NfcApduServiceInfo> nfcOffHostServiceMap = mRegisteredNfcServicesCache.getApduservicesMaps(); |
| 516 | int state = NfcConstants.SERVICE_STATE_ENABLED; |
nxpandroid | fc2aab8 | 2017-04-10 18:19:30 +0530 | [diff] [blame] | 517 | |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 518 | while (eventType != XmlPullParser.START_TAG && |
| 519 | eventType != XmlPullParser.END_DOCUMENT) { |
| 520 | eventType = parser.next(); |
| 521 | } |
| 522 | String tagName = parser.getName(); |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 523 | String fileVersion = "null"; |
| 524 | /** |
| 525 | * Get the version of the Service state file. |
| 526 | * if the version is 1.0, service states are stored as integers(0,1,2,3) |
| 527 | * or else service states are stored as boolean (true or false) |
| 528 | */ |
| 529 | if("Version".equals(tagName)){ |
| 530 | fileVersion = parser.getAttributeValue(null ,"FileVersion"); |
| 531 | Log.d(TAG, "ServiceStateFileVersion="+fileVersion); |
| 532 | eventType = parser.next(); |
| 533 | while (eventType != XmlPullParser.START_TAG && |
| 534 | eventType != XmlPullParser.END_DOCUMENT) { |
| 535 | eventType = parser.next(); |
| 536 | } |
| 537 | tagName = parser.getName(); |
| 538 | Log.e(TAG, "Next Tag="+tagName); |
| 539 | } |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 540 | if ("services".equals(tagName)) { |
| 541 | while (eventType != XmlPullParser.END_DOCUMENT) { |
| 542 | tagName = parser.getName(); |
| 543 | if (eventType == XmlPullParser.START_TAG) { |
| 544 | if("service".equals(tagName) && parser.getDepth() == 0x02) { |
| 545 | String compString = parser.getAttributeValue(null ,"component"); |
| 546 | String uidString = parser.getAttributeValue(null ,"uid"); |
| 547 | String stateString = parser.getAttributeValue(null ,"serviceState"); |
nxpandroid | fc2aab8 | 2017-04-10 18:19:30 +0530 | [diff] [blame] | 548 | |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 549 | if(compString == null || uidString == null || stateString == null) { |
| 550 | Log.e(TAG, "Invalid service attributes"); |
| 551 | } else { |
| 552 | try { |
| 553 | currUid = Integer.parseInt(uidString); |
| 554 | currComponent = ComponentName.unflattenFromString(compString); |
| 555 | Log.d(TAG, " curr component "+compString); |
| 556 | Log.d(TAG, " curr uid "+uidString); |
| 557 | Log.d(TAG, " curr state "+stateString); |
nxpandroid | fc2aab8 | 2017-04-10 18:19:30 +0530 | [diff] [blame] | 558 | |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 559 | if(fileVersion.equals("null")){ |
| 560 | if(stateString.equalsIgnoreCase("false")) |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 561 | state = NfcConstants.SERVICE_STATE_DISABLED; |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 562 | else |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 563 | state = NfcConstants.SERVICE_STATE_ENABLED; |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 564 | }else if(fileVersion.equals("1.0")){ |
| 565 | state = Integer.parseInt(stateString); |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 566 | if(state<NfcConstants.SERVICE_STATE_DISABLED || state > NfcConstants.SERVICE_STATE_DISABLING) |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 567 | Log.e(TAG, "Invalid Service state"); |
| 568 | } |
nxpandroid | fc2aab8 | 2017-04-10 18:19:30 +0530 | [diff] [blame] | 569 | /*Load all the servies info into local memory from xml file and |
| 570 | *later update the xml file with updated information |
| 571 | *This way it can retain previous user's information even after switching to different user |
| 572 | * */ |
| 573 | if(installedServices.containsKey(uidString)) |
| 574 | { |
| 575 | Log.e(TAG, "installedServices contains uidString : " +uidString); |
| 576 | HashMap<ComponentName, Integer> componentStates; |
| 577 | componentStates = installedServices.get(uidString); |
| 578 | componentStates.put(currComponent,state); |
| 579 | }else |
| 580 | { |
| 581 | Log.e(TAG, "installedServices no uidString "); |
| 582 | HashMap<ComponentName, Integer> componentStates = new HashMap<>(); |
| 583 | componentStates.put(currComponent,state); |
| 584 | installedServices.put(uidString,componentStates); |
| 585 | } |
| 586 | |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 587 | } catch (NumberFormatException e) { |
| 588 | Log.e(TAG, "could not parse the service attributes"); |
| 589 | } |
| 590 | } |
| 591 | } |
| 592 | } else if (eventType == XmlPullParser.END_TAG) { |
| 593 | if("service".equals(tagName)) { |
| 594 | final int userId = UserHandle.getUserId(currUid); |
nxpandroid | fc2aab8 | 2017-04-10 18:19:30 +0530 | [diff] [blame] | 595 | |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 596 | UserServices serviceCache = findOrCreateUserLocked(userId); |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 597 | NfcApduServiceInfo serviceInfo = serviceCache.services.get(currComponent); |
nxpandroid | fc2aab8 | 2017-04-10 18:19:30 +0530 | [diff] [blame] | 598 | |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 599 | if(serviceInfo != null) { |
| 600 | serviceInfo.setServiceState(CardEmulation.CATEGORY_OTHER ,state); |
| 601 | } |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 602 | } |
| 603 | currUid = -1; |
| 604 | currComponent = null; |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 605 | state = NfcConstants.SERVICE_STATE_ENABLED; |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | eventType = parser.next(); |
| 609 | } |
| 610 | } |
| 611 | } catch(Exception e) { |
| 612 | mServiceStateFile.delete(); |
nxpandroid | fc2aab8 | 2017-04-10 18:19:30 +0530 | [diff] [blame] | 613 | Log.e(TAG, "could not parse the seriveState file , thrashing the file " + e); |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 614 | } finally { |
| 615 | try { |
| 616 | if(fis != null) { |
| 617 | fis.close(); |
| 618 | } |
| 619 | } catch ( Exception e) { |
| 620 | } |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | private boolean writeServiceStateToFile(int currUserId) { |
| 625 | FileOutputStream fos = null; |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 626 | //ArrayList<NfcApduServiceInfo> nfcOffHostServiceCache = mRegisteredNfcServicesCache.getApduservicesList(); |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 627 | /*if(NfcService.getInstance().getAidRoutingTableStatus() == 0x00) { |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 628 | Log.e(TAG, " Aid Routing Table still availble , No need to disable services"); |
| 629 | return false; |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 630 | }*/ |
| 631 | Log.e(TAG, " Writing service state Data Always"); |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 632 | if(currUserId != ActivityManager.getCurrentUser()) { |
| 633 | return false; |
| 634 | } |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 635 | int state = NfcConstants.SERVICE_STATE_ENABLED; |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 636 | try { |
| 637 | fos = mServiceStateFile.startWrite(); |
| 638 | XmlSerializer out = new FastXmlSerializer(); |
| 639 | out.setOutput(fos, "utf-8"); |
| 640 | out.startDocument(null , true); |
| 641 | out.setFeature(XML_INDENT_OUTPUT_FEATURE, true); |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 642 | out.startTag(null ,"Version"); |
| 643 | out.attribute(null, "FileVersion", SERVICE_STATE_FILE_VERSION); |
| 644 | out.endTag(null ,"Version"); |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 645 | out.startTag(null ,"services"); |
| 646 | for(int userId = 0; userId < mUserServices.size(); userId++) { |
| 647 | final UserServices userServices = mUserServices.valueAt(userId); |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 648 | for (NfcApduServiceInfo serviceInfo : userServices.services.values()) { |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 649 | if(!serviceInfo.hasCategory(CardEmulation.CATEGORY_OTHER)) { |
| 650 | continue; |
| 651 | } |
| 652 | out.startTag(null ,"service"); |
| 653 | out.attribute(null, "component", serviceInfo.getComponent().flattenToString()); |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 654 | Log.e(TAG,"component name"+ serviceInfo.getComponent().flattenToString()); |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 655 | out.attribute(null, "uid", Integer.toString(serviceInfo.getUid())); |
nxpandroid | fc2aab8 | 2017-04-10 18:19:30 +0530 | [diff] [blame] | 656 | |
| 657 | boolean isServiceInstalled = false; |
| 658 | if(installedServices.containsKey(Integer.toString(serviceInfo.getUid()))){ |
| 659 | HashMap<ComponentName, Integer> componentStates = installedServices.get(Integer.toString(serviceInfo.getUid())); |
| 660 | if (componentStates.containsKey(serviceInfo.getComponent())) { |
| 661 | state = componentStates.get(serviceInfo.getComponent()); |
| 662 | componentStates.remove(serviceInfo.getComponent()); |
| 663 | if(componentStates.isEmpty()) |
| 664 | { |
| 665 | installedServices.remove(Integer.toString(serviceInfo.getUid())); |
| 666 | } |
| 667 | isServiceInstalled = true; |
| 668 | } |
| 669 | } |
| 670 | if (!isServiceInstalled) { |
| 671 | state = serviceInfo.getServiceState(CardEmulation.CATEGORY_OTHER); |
| 672 | } |
| 673 | out.attribute(null, "serviceState", Integer.toString(state)); |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 674 | out.endTag(null, "service"); |
| 675 | } |
| 676 | } |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 677 | out.endTag(null ,"services"); |
| 678 | out.endDocument(); |
| 679 | mServiceStateFile.finishWrite(fos); |
| 680 | return true; |
| 681 | } catch ( Exception e){ |
| 682 | Log.e(TAG,"Failed to write serviceStateFile xml"); |
| 683 | e.printStackTrace(); |
| 684 | if (fos != null) { |
| 685 | mServiceStateFile.failWrite(fos); |
| 686 | } |
| 687 | return false; |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | public int updateServiceState(int userId , int uid, |
| 692 | Map<String , Boolean> serviceState) { |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 693 | boolean success = false; |
| 694 | //HashMap<ComponentName ,NfcApduServiceInfo> nfcOffHostServiceMap = mRegisteredNfcServicesCache.getApduservicesMaps(); |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 695 | if(NfcService.getInstance().getAidRoutingTableStatus() == 0x00) { |
| 696 | Log.e(TAG, " Aid Routing Table still availble , No need to disable services"); |
| 697 | return 0xFF; |
| 698 | } |
| 699 | synchronized(mLock) { |
| 700 | Iterator<Map.Entry<String , Boolean>> it = |
| 701 | serviceState.entrySet().iterator(); |
| 702 | while(it.hasNext()) { |
| 703 | Map.Entry<String , Boolean> entry = |
| 704 | (Map.Entry<String , Boolean>) it.next(); |
| 705 | ComponentName componentName = ComponentName.unflattenFromString(entry.getKey()); |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 706 | NfcApduServiceInfo serviceInfo = getService(userId, componentName); |
| 707 | Log.e(TAG, "updateServiceState " + entry.getKey()); |
| 708 | Log.e(TAG, "updateServiceState " + entry.getValue()); |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 709 | if (serviceInfo != null) { |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 710 | serviceInfo.enableService(CardEmulation.CATEGORY_OTHER, entry.getValue()); |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 711 | } else { |
| 712 | Log.e(TAG, "Could not find service " + componentName); |
| 713 | return 0xFF; |
| 714 | } |
| 715 | } |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 716 | Log.e(TAG,"2"+Thread.currentThread().getStackTrace()[2].getMethodName()+":WriteServiceStateToFile"); |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 717 | success = writeServiceStateToFile(userId); |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 718 | } |
| 719 | invalidateCache(ActivityManager.getCurrentUser()); |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 720 | return (success?0x00:0xFF); |
| 721 | } |
| 722 | |
| 723 | public boolean setOffHostSecureElement(int userId, int uid, ComponentName componentName, |
| 724 | String offHostSE) { |
| 725 | ArrayList<NfcApduServiceInfo> newServices = null; |
| 726 | synchronized (mLock) { |
| 727 | UserServices services = findOrCreateUserLocked(userId); |
| 728 | // Check if we can find this service |
| 729 | NfcApduServiceInfo serviceInfo = getService(userId, componentName); |
| 730 | if (serviceInfo == null) { |
| 731 | Log.e(TAG, "Service " + componentName + " does not exist."); |
| 732 | return false; |
| 733 | } |
| 734 | if (serviceInfo.getUid() != uid) { |
| 735 | // This is probably a good indication something is wrong here. |
| 736 | // Either newer service installed with different uid (but then |
| 737 | // we should have known about it), or somebody calling us from |
| 738 | // a different uid. |
| 739 | Log.e(TAG, "UID mismatch."); |
| 740 | return false; |
| 741 | } |
| 742 | if (offHostSE == null || serviceInfo.isOnHost()) { |
| 743 | Log.e(TAG, "OffHostSE mismatch with Service type"); |
| 744 | return false; |
| 745 | } |
| 746 | |
| 747 | DynamicSettings dynSettings = services.dynamicSettings.get(componentName); |
| 748 | if (dynSettings == null) { |
| 749 | dynSettings = new DynamicSettings(uid); |
| 750 | } |
| 751 | dynSettings.offHostSE = offHostSE; |
| 752 | boolean success = writeDynamicSettingsLocked(); |
| 753 | if (!success) { |
| 754 | Log.e(TAG, "Failed to persist AID group."); |
| 755 | dynSettings.offHostSE = null; |
| 756 | return false; |
| 757 | } |
| 758 | |
| 759 | serviceInfo.setOffHostSecureElement(offHostSE); |
| 760 | newServices = new ArrayList<NfcApduServiceInfo>(services.services.values()); |
| 761 | } |
| 762 | // Make callback without the lock held |
| 763 | mCallback.onServicesUpdated(userId, newServices); |
| 764 | return true; |
| 765 | } |
| 766 | |
| 767 | public boolean unsetOffHostSecureElement(int userId, int uid, ComponentName componentName) { |
| 768 | ArrayList<NfcApduServiceInfo> newServices = null; |
| 769 | synchronized (mLock) { |
| 770 | UserServices services = findOrCreateUserLocked(userId); |
| 771 | // Check if we can find this service |
| 772 | NfcApduServiceInfo serviceInfo = getService(userId, componentName); |
| 773 | if (serviceInfo == null) { |
| 774 | Log.e(TAG, "Service " + componentName + " does not exist."); |
| 775 | return false; |
| 776 | } |
| 777 | if (serviceInfo.getUid() != uid) { |
| 778 | // This is probably a good indication something is wrong here. |
| 779 | // Either newer service installed with different uid (but then |
| 780 | // we should have known about it), or somebody calling us from |
| 781 | // a different uid. |
| 782 | Log.e(TAG, "UID mismatch."); |
| 783 | return false; |
| 784 | } |
| 785 | if (serviceInfo.isOnHost() || serviceInfo.getOffHostSecureElement() == null) { |
| 786 | Log.e(TAG, "OffHostSE is not set"); |
| 787 | return false; |
| 788 | } |
| 789 | serviceInfo.unsetOffHostSecureElement(); |
| 790 | DynamicSettings dynSettings = services.dynamicSettings.get(componentName); |
| 791 | if (dynSettings != null) { |
| 792 | String offHostSE = dynSettings.offHostSE; |
| 793 | dynSettings.offHostSE = serviceInfo.getOffHostSecureElement(); |
| 794 | boolean success = writeDynamicSettingsLocked(); |
| 795 | if (!success) { |
| 796 | Log.e(TAG, "Failed to persist AID group."); |
| 797 | dynSettings.offHostSE = offHostSE; |
| 798 | return false; |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | newServices = new ArrayList<NfcApduServiceInfo>(services.services.values()); |
| 803 | } |
| 804 | // Make callback without the lock held |
| 805 | mCallback.onServicesUpdated(userId, newServices); |
| 806 | return true; |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 807 | } |
| 808 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 809 | public boolean registerAidGroupForService(int userId, int uid, |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 810 | ComponentName componentName, NfcAidGroup nfcAidGroup) { |
| 811 | ArrayList<NfcApduServiceInfo> newServices = null; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 812 | boolean success; |
| 813 | synchronized (mLock) { |
| 814 | UserServices services = findOrCreateUserLocked(userId); |
| 815 | // Check if we can find this service |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 816 | NfcApduServiceInfo serviceInfo = getService(userId, componentName); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 817 | if (serviceInfo == null) { |
| 818 | Log.e(TAG, "Service " + componentName + " does not exist."); |
| 819 | return false; |
| 820 | } |
| 821 | if (serviceInfo.getUid() != uid) { |
| 822 | // This is probably a good indication something is wrong here. |
| 823 | // Either newer service installed with different uid (but then |
| 824 | // we should have known about it), or somebody calling us from |
| 825 | // a different uid. |
| 826 | Log.e(TAG, "UID mismatch."); |
| 827 | return false; |
| 828 | } |
| 829 | // Do another AID validation, since a caller could have thrown in a modified |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 830 | // NfcAidGroup object with invalid AIDs over Binder. |
| 831 | List<String> aids = nfcAidGroup.getAids(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 832 | for (String aid : aids) { |
| 833 | if (!CardEmulation.isValidAid(aid)) { |
| 834 | Log.e(TAG, "AID " + aid + " is not a valid AID"); |
| 835 | return false; |
| 836 | } |
| 837 | } |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 838 | serviceInfo.setOrReplaceDynamicNfcAidGroup(nfcAidGroup); |
| 839 | DynamicSettings dynSettings = services.dynamicSettings.get(componentName); |
| 840 | if (dynSettings == null) { |
| 841 | dynSettings = new DynamicSettings(uid); |
| 842 | dynSettings.offHostSE = serviceInfo.getOffHostSecureElement(); |
| 843 | services.dynamicSettings.put(componentName, dynSettings); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 844 | } |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 845 | dynSettings.aidGroups.put(nfcAidGroup.getCategory(), nfcAidGroup); |
| 846 | success = writeDynamicSettingsLocked(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 847 | if (success) { |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 848 | newServices = new ArrayList<NfcApduServiceInfo>(services.services.values()); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 849 | } else { |
| 850 | Log.e(TAG, "Failed to persist AID group."); |
| 851 | // Undo registration |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 852 | dynSettings.aidGroups.remove(nfcAidGroup.getCategory()); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 853 | } |
| 854 | } |
| 855 | if (success) { |
| 856 | // Make callback without the lock held |
| 857 | mCallback.onServicesUpdated(userId, newServices); |
| 858 | } |
| 859 | return success; |
| 860 | } |
| 861 | |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 862 | public NfcAidGroup getAidGroupForService(int userId, int uid, ComponentName componentName, |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 863 | String category) { |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 864 | NfcApduServiceInfo serviceInfo = getService(userId, componentName); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 865 | if (serviceInfo != null) { |
| 866 | if (serviceInfo.getUid() != uid) { |
| 867 | Log.e(TAG, "UID mismatch"); |
| 868 | return null; |
| 869 | } |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 870 | return serviceInfo.getDynamicNfcAidGroupForCategory(category); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 871 | } else { |
| 872 | Log.e(TAG, "Could not find service " + componentName); |
| 873 | return null; |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | public boolean removeAidGroupForService(int userId, int uid, ComponentName componentName, |
| 878 | String category) { |
| 879 | boolean success = false; |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 880 | ArrayList<NfcApduServiceInfo> newServices = null; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 881 | synchronized (mLock) { |
| 882 | UserServices services = findOrCreateUserLocked(userId); |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 883 | NfcApduServiceInfo serviceInfo = getService(userId, componentName); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 884 | if (serviceInfo != null) { |
| 885 | if (serviceInfo.getUid() != uid) { |
| 886 | // Calling from different uid |
| 887 | Log.e(TAG, "UID mismatch"); |
| 888 | return false; |
| 889 | } |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 890 | if (!serviceInfo.removeDynamicNfcAidGroupForCategory(category)) { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 891 | Log.e(TAG," Could not find dynamic AIDs for category " + category); |
| 892 | return false; |
| 893 | } |
| 894 | // Remove from local cache |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 895 | DynamicSettings dynAids = services.dynamicSettings.get(componentName); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 896 | if (dynAids != null) { |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 897 | NfcAidGroup deletedGroup = dynAids.aidGroups.remove(category); |
| 898 | success = writeDynamicSettingsLocked(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 899 | if (success) { |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 900 | newServices = new ArrayList<NfcApduServiceInfo>(services.services.values()); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 901 | } else { |
| 902 | Log.e(TAG, "Could not persist deleted AID group."); |
| 903 | dynAids.aidGroups.put(category, deletedGroup); |
| 904 | return false; |
| 905 | } |
| 906 | } else { |
| 907 | Log.e(TAG, "Could not find aid group in local cache."); |
| 908 | } |
| 909 | } else { |
| 910 | Log.e(TAG, "Service " + componentName + " does not exist."); |
| 911 | } |
| 912 | } |
| 913 | if (success) { |
| 914 | mCallback.onServicesUpdated(userId, newServices); |
| 915 | } |
| 916 | return success; |
| 917 | } |
| 918 | |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 919 | void initialize() { |
| 920 | synchronized (mLock) { |
| 921 | readDynamicSettingsLocked(); |
| 922 | } |
| 923 | invalidateCache(ActivityManager.getCurrentUser()); |
| 924 | } |
| 925 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 926 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 927 | pw.println("Registered HCE services for current user: "); |
| 928 | UserServices userServices = findOrCreateUserLocked(ActivityManager.getCurrentUser()); |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 929 | for (NfcApduServiceInfo service : userServices.services.values()) { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 930 | service.dump(fd, pw, args); |
| 931 | pw.println(""); |
| 932 | } |
| 933 | pw.println(""); |
| 934 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 935 | } |