| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.nfc.cardemulation; |
| 18 |
|
| 19 |
import android.app.Activity; |
| 20 |
import android.content.ComponentName; |
| 21 |
import android.content.Context; |
| 22 |
import android.content.pm.PackageManager; |
| 23 |
import android.nfc.INfcFCardEmulation; |
| 24 |
import android.nfc.NfcAdapter; |
| 25 |
import android.os.RemoteException; |
| 26 |
import android.util.Log; |
| 27 |
|
| 28 |
import java.util.HashMap; |
| 29 |
import java.util.List; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public final class NfcFCardEmulation { |
| 44 |
static final String TAG = "NfcFCardEmulation"; |
| 45 |
|
| 46 |
static boolean sIsInitialized = false; |
| 47 |
static HashMap<Context, NfcFCardEmulation> sCardEmus = new HashMap<Context, NfcFCardEmulation>(); |
| 48 |
static INfcFCardEmulation sService; |
| 49 |
|
| 50 |
final Context mContext; |
| 51 |
|
| 52 |
private NfcFCardEmulation(Context context, INfcFCardEmulation service) { |
| 53 |
mContext = context.getApplicationContext(); |
| 54 |
sService = service; |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public static synchronized NfcFCardEmulation getInstance(NfcAdapter adapter) { |
| 64 |
if (adapter == null) throw new NullPointerException("NfcAdapter is null"); |
| 65 |
Context context = adapter.getContext(); |
| 66 |
if (context == null) { |
| 67 |
Log.e(TAG, "NfcAdapter context is null."); |
| 68 |
throw new UnsupportedOperationException(); |
| 69 |
} |
| 70 |
if (!sIsInitialized) { |
| 71 |
PackageManager pm = context.getPackageManager(); |
| 72 |
if (pm == null) { |
| 73 |
Log.e(TAG, "Cannot get PackageManager"); |
| 74 |
throw new UnsupportedOperationException(); |
| 75 |
} |
| 76 |
if (!pm.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION_NFCF)) { |
| 77 |
Log.e(TAG, "This device does not support NFC-F card emulation"); |
| 78 |
throw new UnsupportedOperationException(); |
| 79 |
} |
| 80 |
sIsInitialized = true; |
| 81 |
} |
| 82 |
NfcFCardEmulation manager = sCardEmus.get(context); |
| 83 |
if (manager == null) { |
| 84 |
|
| 85 |
INfcFCardEmulation service = adapter.getNfcFCardEmulationService(); |
| 86 |
if (service == null) { |
| 87 |
Log.e(TAG, "This device does not implement the INfcFCardEmulation interface."); |
| 88 |
throw new UnsupportedOperationException(); |
| 89 |
} |
| 90 |
manager = new NfcFCardEmulation(context, service); |
| 91 |
sCardEmus.put(context, manager); |
| 92 |
} |
| 93 |
return manager; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public String getSystemCodeForService(ComponentName service) throws RuntimeException { |
| 109 |
if (service == null) { |
| 110 |
throw new NullPointerException("service is null"); |
| 111 |
} |
| 112 |
try { |
| 113 |
return sService.getSystemCodeForService(mContext.getUser().getIdentifier(), service); |
| 114 |
} catch (RemoteException e) { |
| 115 |
|
| 116 |
recoverService(); |
| 117 |
if (sService == null) { |
| 118 |
Log.e(TAG, "Failed to recover CardEmulationService."); |
| 119 |
return null; |
| 120 |
} |
| 121 |
try { |
| 122 |
return sService.getSystemCodeForService(mContext.getUser().getIdentifier(), |
| 123 |
service); |
| 124 |
} catch (RemoteException ee) { |
| 125 |
Log.e(TAG, "Failed to reach CardEmulationService."); |
| 126 |
ee.rethrowAsRuntimeException(); |
| 127 |
return null; |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
public boolean registerSystemCodeForService(ComponentName service, String systemCode) |
| 155 |
throws RuntimeException { |
| 156 |
if (service == null || systemCode == null) { |
| 157 |
throw new NullPointerException("service or systemCode is null"); |
| 158 |
} |
| 159 |
try { |
| 160 |
return sService.registerSystemCodeForService(mContext.getUser().getIdentifier(), |
| 161 |
service, systemCode); |
| 162 |
} catch (RemoteException e) { |
| 163 |
|
| 164 |
recoverService(); |
| 165 |
if (sService == null) { |
| 166 |
Log.e(TAG, "Failed to recover CardEmulationService."); |
| 167 |
return false; |
| 168 |
} |
| 169 |
try { |
| 170 |
return sService.registerSystemCodeForService(mContext.getUser().getIdentifier(), |
| 171 |
service, systemCode); |
| 172 |
} catch (RemoteException ee) { |
| 173 |
Log.e(TAG, "Failed to reach CardEmulationService."); |
| 174 |
ee.rethrowAsRuntimeException(); |
| 175 |
return false; |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
public boolean unregisterSystemCodeForService(ComponentName service) throws RuntimeException { |
| 187 |
if (service == null) { |
| 188 |
throw new NullPointerException("service is null"); |
| 189 |
} |
| 190 |
try { |
| 191 |
return sService.removeSystemCodeForService(mContext.getUser().getIdentifier(), service); |
| 192 |
} catch (RemoteException e) { |
| 193 |
|
| 194 |
recoverService(); |
| 195 |
if (sService == null) { |
| 196 |
Log.e(TAG, "Failed to recover CardEmulationService."); |
| 197 |
return false; |
| 198 |
} |
| 199 |
try { |
| 200 |
return sService.removeSystemCodeForService(mContext.getUser().getIdentifier(), |
| 201 |
service); |
| 202 |
} catch (RemoteException ee) { |
| 203 |
Log.e(TAG, "Failed to reach CardEmulationService."); |
| 204 |
ee.rethrowAsRuntimeException(); |
| 205 |
return false; |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
public String getNfcid2ForService(ComponentName service) throws RuntimeException { |
| 223 |
if (service == null) { |
| 224 |
throw new NullPointerException("service is null"); |
| 225 |
} |
| 226 |
try { |
| 227 |
return sService.getNfcid2ForService(mContext.getUser().getIdentifier(), service); |
| 228 |
} catch (RemoteException e) { |
| 229 |
|
| 230 |
recoverService(); |
| 231 |
if (sService == null) { |
| 232 |
Log.e(TAG, "Failed to recover CardEmulationService."); |
| 233 |
return null; |
| 234 |
} |
| 235 |
try { |
| 236 |
return sService.getNfcid2ForService(mContext.getUser().getIdentifier(), service); |
| 237 |
} catch (RemoteException ee) { |
| 238 |
Log.e(TAG, "Failed to reach CardEmulationService."); |
| 239 |
ee.rethrowAsRuntimeException(); |
| 240 |
return null; |
| 241 |
} |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
public boolean setNfcid2ForService(ComponentName service, String nfcid2) |
| 265 |
throws RuntimeException { |
| 266 |
if (service == null || nfcid2 == null) { |
| 267 |
throw new NullPointerException("service or nfcid2 is null"); |
| 268 |
} |
| 269 |
try { |
| 270 |
return sService.setNfcid2ForService(mContext.getUser().getIdentifier(), |
| 271 |
service, nfcid2); |
| 272 |
} catch (RemoteException e) { |
| 273 |
|
| 274 |
recoverService(); |
| 275 |
if (sService == null) { |
| 276 |
Log.e(TAG, "Failed to recover CardEmulationService."); |
| 277 |
return false; |
| 278 |
} |
| 279 |
try { |
| 280 |
return sService.setNfcid2ForService(mContext.getUser().getIdentifier(), |
| 281 |
service, nfcid2); |
| 282 |
} catch (RemoteException ee) { |
| 283 |
Log.e(TAG, "Failed to reach CardEmulationService."); |
| 284 |
ee.rethrowAsRuntimeException(); |
| 285 |
return false; |
| 286 |
} |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
public boolean enableService(Activity activity, ComponentName service) throws RuntimeException { |
| 311 |
if (activity == null || service == null) { |
| 312 |
throw new NullPointerException("activity or service is null"); |
| 313 |
} |
| 314 |
|
| 315 |
if (!activity.isResumed()) { |
| 316 |
throw new IllegalArgumentException("Activity must be resumed."); |
| 317 |
} |
| 318 |
try { |
| 319 |
return sService.enableNfcFForegroundService(service); |
| 320 |
} catch (RemoteException e) { |
| 321 |
|
| 322 |
recoverService(); |
| 323 |
if (sService == null) { |
| 324 |
Log.e(TAG, "Failed to recover CardEmulationService."); |
| 325 |
return false; |
| 326 |
} |
| 327 |
try { |
| 328 |
return sService.enableNfcFForegroundService(service); |
| 329 |
} catch (RemoteException ee) { |
| 330 |
Log.e(TAG, "Failed to reach CardEmulationService."); |
| 331 |
ee.rethrowAsRuntimeException(); |
| 332 |
return false; |
| 333 |
} |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
public boolean disableService(Activity activity) throws RuntimeException { |
| 348 |
if (activity == null) { |
| 349 |
throw new NullPointerException("activity is null"); |
| 350 |
} |
| 351 |
if (!activity.isResumed()) { |
| 352 |
throw new IllegalArgumentException("Activity must be resumed."); |
| 353 |
} |
| 354 |
try { |
| 355 |
return sService.disableNfcFForegroundService(); |
| 356 |
} catch (RemoteException e) { |
| 357 |
|
| 358 |
recoverService(); |
| 359 |
if (sService == null) { |
| 360 |
Log.e(TAG, "Failed to recover CardEmulationService."); |
| 361 |
return false; |
| 362 |
} |
| 363 |
try { |
| 364 |
return sService.disableNfcFForegroundService(); |
| 365 |
} catch (RemoteException ee) { |
| 366 |
Log.e(TAG, "Failed to reach CardEmulationService."); |
| 367 |
ee.rethrowAsRuntimeException(); |
| 368 |
return false; |
| 369 |
} |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
public List<NfcFServiceInfo> getNfcFServices() { |
| 377 |
try { |
| 378 |
return sService.getNfcFServices(mContext.getUser().getIdentifier()); |
| 379 |
} catch (RemoteException e) { |
| 380 |
|
| 381 |
recoverService(); |
| 382 |
if (sService == null) { |
| 383 |
Log.e(TAG, "Failed to recover CardEmulationService."); |
| 384 |
return null; |
| 385 |
} |
| 386 |
try { |
| 387 |
return sService.getNfcFServices(mContext.getUser().getIdentifier()); |
| 388 |
} catch (RemoteException ee) { |
| 389 |
Log.e(TAG, "Failed to reach CardEmulationService."); |
| 390 |
return null; |
| 391 |
} |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
public int getMaxNumOfRegisterableSystemCodes() { |
| 399 |
try { |
| 400 |
return sService.getMaxNumOfRegisterableSystemCodes(); |
| 401 |
} catch (RemoteException e) { |
| 402 |
|
| 403 |
recoverService(); |
| 404 |
if (sService == null) { |
| 405 |
Log.e(TAG, "Failed to recover CardEmulationService."); |
| 406 |
return -1; |
| 407 |
} |
| 408 |
try { |
| 409 |
return sService.getMaxNumOfRegisterableSystemCodes(); |
| 410 |
} catch (RemoteException ee) { |
| 411 |
Log.e(TAG, "Failed to reach CardEmulationService."); |
| 412 |
return -1; |
| 413 |
} |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
public static boolean isValidSystemCode(String systemCode) { |
| 421 |
if (systemCode == null) { |
| 422 |
return false; |
| 423 |
} |
| 424 |
if (systemCode.length() != 4) { |
| 425 |
Log.e(TAG, "System Code " + systemCode + " is not a valid System Code."); |
| 426 |
return false; |
| 427 |
} |
| 428 |
|
| 429 |
if (!systemCode.startsWith("4") || systemCode.toUpperCase().endsWith("FF")) { |
| 430 |
Log.e(TAG, "System Code " + systemCode + " is not a valid System Code."); |
| 431 |
return false; |
| 432 |
} |
| 433 |
try { |
| 434 |
Integer.parseInt(systemCode, 16); |
| 435 |
} catch (NumberFormatException e) { |
| 436 |
Log.e(TAG, "System Code " + systemCode + " is not a valid System Code."); |
| 437 |
return false; |
| 438 |
} |
| 439 |
return true; |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
public static boolean isValidNfcid2(String nfcid2) { |
| 446 |
if (nfcid2 == null) { |
| 447 |
return false; |
| 448 |
} |
| 449 |
if (nfcid2.length() != 16) { |
| 450 |
Log.e(TAG, "NFCID2 " + nfcid2 + " is not a valid NFCID2."); |
| 451 |
return false; |
| 452 |
} |
| 453 |
|
| 454 |
if (!nfcid2.toUpperCase().startsWith("02FE")) { |
| 455 |
Log.e(TAG, "NFCID2 " + nfcid2 + " is not a valid NFCID2."); |
| 456 |
return false; |
| 457 |
} |
| 458 |
try { |
| 459 |
Long.parseLong(nfcid2, 16); |
| 460 |
} catch (NumberFormatException e) { |
| 461 |
Log.e(TAG, "NFCID2 " + nfcid2 + " is not a valid NFCID2."); |
| 462 |
return false; |
| 463 |
} |
| 464 |
return true; |
| 465 |
} |
| 466 |
|
| 467 |
void recoverService() { |
| 468 |
NfcAdapter adapter = NfcAdapter.getDefaultAdapter(mContext); |
| 469 |
sService = adapter.getNfcFCardEmulationService(); |
| 470 |
} |
| 471 |
|
| 472 |
} |
| 473 |
|