| 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.annotation.NonNull; |
| 20 |
import android.annotation.Nullable; |
| 21 |
import android.annotation.RequiresPermission; |
| 22 |
import android.annotation.SdkConstant; |
| 23 |
import android.annotation.SdkConstant.SdkConstantType; |
| 24 |
import android.app.Activity; |
| 25 |
import android.content.ComponentName; |
| 26 |
import android.content.Context; |
| 27 |
import android.content.Intent; |
| 28 |
import android.content.pm.PackageManager; |
| 29 |
import android.nfc.INfcCardEmulation; |
| 30 |
import android.nfc.NfcAdapter; |
| 31 |
import android.os.RemoteException; |
| 32 |
import android.os.UserHandle; |
| 33 |
import android.provider.Settings; |
| 34 |
import android.provider.Settings.SettingNotFoundException; |
| 35 |
import android.util.Log; |
| 36 |
|
| 37 |
import java.util.HashMap; |
| 38 |
import java.util.List; |
| 39 |
import java.util.regex.Pattern; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public final class CardEmulation { |
| 54 |
private static final Pattern AID_PATTERN = Pattern.compile("[0-9A-Fa-f]{10,32}\\*?\\#?"); |
| 55 |
static final String TAG = "CardEmulation"; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
@SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) |
| 69 |
public static final String ACTION_CHANGE_DEFAULT = |
| 70 |
"android.nfc.cardemulation.action.ACTION_CHANGE_DEFAULT"; |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public static final String EXTRA_CATEGORY = "category"; |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public static final String EXTRA_SERVICE_COMPONENT = "component"; |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public static final String CATEGORY_PAYMENT = "payment"; |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public static final String CATEGORY_OTHER = "other"; |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public static final int SELECTION_MODE_PREFER_DEFAULT = 0; |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public static final int SELECTION_MODE_ALWAYS_ASK = 1; |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
public static final int SELECTION_MODE_ASK_IF_CONFLICT = 2; |
| 133 |
|
| 134 |
static boolean sIsInitialized = false; |
| 135 |
static HashMap<Context, CardEmulation> sCardEmus = new HashMap<Context, CardEmulation>(); |
| 136 |
static INfcCardEmulation sService; |
| 137 |
|
| 138 |
final Context mContext; |
| 139 |
|
| 140 |
private CardEmulation(Context context, INfcCardEmulation service) { |
| 141 |
mContext = context.getApplicationContext(); |
| 142 |
sService = service; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
public static synchronized CardEmulation getInstance(NfcAdapter adapter) { |
| 152 |
if (adapter == null) throw new NullPointerException("NfcAdapter is null"); |
| 153 |
Context context = adapter.getContext(); |
| 154 |
if (context == null) { |
| 155 |
Log.e(TAG, "NfcAdapter context is null."); |
| 156 |
throw new UnsupportedOperationException(); |
| 157 |
} |
| 158 |
if (!sIsInitialized) { |
| 159 |
PackageManager pm = context.getPackageManager(); |
| 160 |
if (pm == null) { |
| 161 |
Log.e(TAG, "Cannot get PackageManager"); |
| 162 |
throw new UnsupportedOperationException(); |
| 163 |
} |
| 164 |
if (!pm.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) { |
| 165 |
Log.e(TAG, "This device does not support card emulation"); |
| 166 |
throw new UnsupportedOperationException(); |
| 167 |
} |
| 168 |
sIsInitialized = true; |
| 169 |
} |
| 170 |
CardEmulation manager = sCardEmus.get(context); |
| 171 |
if (manager == null) { |
| 172 |
|
| 173 |
INfcCardEmulation service = adapter.getCardEmulationService(); |
| 174 |
if (service == null) { |
| 175 |
Log.e(TAG, "This device does not implement the INfcCardEmulation interface."); |
| 176 |
throw new UnsupportedOperationException(); |
| 177 |
} |
| 178 |
manager = new CardEmulation(context, service); |
| 179 |
sCardEmus.put(context, manager); |
| 180 |
} |
| 181 |
return manager; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
public boolean isDefaultServiceForCategory(ComponentName service, String category) { |
| 204 |
try { |
| 205 |
return sService.isDefaultServiceForCategory(mContext.getUser().getIdentifier(), |
| 206 |
service, category); |
| 207 |
} catch (RemoteException e) { |
| 208 |
|
| 209 |
recoverService(); |
| 210 |
if (sService == null) { |
| 211 |
Log.e(TAG, "Failed to recover CardEmulationService."); |
| 212 |
return false; |
| 213 |
} |
| 214 |
try { |
| 215 |
return sService.isDefaultServiceForCategory(mContext.getUser().getIdentifier(), |
| 216 |
service, category); |
| 217 |
} catch (RemoteException ee) { |
| 218 |
Log.e(TAG, "Failed to recover CardEmulationService."); |
| 219 |
return false; |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
public boolean isDefaultServiceForAid(ComponentName service, String aid) { |
| 236 |
try { |
| 237 |
return sService.isDefaultServiceForAid(mContext.getUser().getIdentifier(), |
| 238 |
service, aid); |
| 239 |
} catch (RemoteException e) { |
| 240 |
|
| 241 |
recoverService(); |
| 242 |
if (sService == null) { |
| 243 |
Log.e(TAG, "Failed to recover CardEmulationService."); |
| 244 |
return false; |
| 245 |
} |
| 246 |
try { |
| 247 |
return sService.isDefaultServiceForAid(mContext.getUser().getIdentifier(), |
| 248 |
service, aid); |
| 249 |
} catch (RemoteException ee) { |
| 250 |
Log.e(TAG, "Failed to reach CardEmulationService."); |
| 251 |
return false; |
| 252 |
} |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
@SuppressWarnings("NonUserGetterCalled") |
| 269 |
public boolean categoryAllowsForegroundPreference(String category) { |
| 270 |
if (CATEGORY_PAYMENT.equals(category)) { |
| 271 |
boolean preferForeground = false; |
| 272 |
Context contextAsUser = mContext.createContextAsUser( |
| 273 |
UserHandle.of(UserHandle.myUserId()), 0); |
| 274 |
try { |
| 275 |
preferForeground = Settings.Secure.getInt( |
| 276 |
contextAsUser.getContentResolver(), |
| 277 |
Settings.Secure.NFC_PAYMENT_FOREGROUND) != 0; |
| 278 |
} catch (SettingNotFoundException e) { |
| 279 |
} |
| 280 |
return preferForeground; |
| 281 |
} else { |
| 282 |
|
| 283 |
return true; |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
public int getSelectionModeForCategory(String category) { |
| 300 |
if (CATEGORY_PAYMENT.equals(category)) { |
| 301 |
boolean paymentRegistered = false; |
| 302 |
try { |
| 303 |
paymentRegistered = sService.isDefaultPaymentRegistered(); |
| 304 |
} catch (RemoteException e) { |
| 305 |
recoverService(); |
| 306 |
if (sService == null) { |
| 307 |
Log.e(TAG, "Failed to recover CardEmulationService."); |
| 308 |
return SELECTION_MODE_ALWAYS_ASK; |
| 309 |
} |
| 310 |
try { |
| 311 |
paymentRegistered = sService.isDefaultPaymentRegistered(); |
| 312 |
} catch (RemoteException ee) { |
| 313 |
Log.e(TAG, "Failed to reach CardEmulationService."); |
| 314 |
return SELECTION_MODE_ALWAYS_ASK; |
| 315 |
} |
| 316 |
} |
| 317 |
if (paymentRegistered) { |
| 318 |
return SELECTION_MODE_PREFER_DEFAULT; |
| 319 |
} else { |
| 320 |
return SELECTION_MODE_ALWAYS_ASK; |
| 321 |
} |
| 322 |
} else { |
| 323 |
return SELECTION_MODE_ASK_IF_CONFLICT; |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
public boolean registerAidsForService(ComponentName service, String category, |
| 348 |
List<String> aids) { |
| 349 |
AidGroup aidGroup = new AidGroup(aids, category); |
| 350 |
try { |
| 351 |
return sService.registerAidGroupForService(mContext.getUser().getIdentifier(), |
| 352 |
service, aidGroup); |
| 353 |
} catch (RemoteException e) { |
| 354 |
|
| 355 |
recoverService(); |
| 356 |
if (sService == null) { |
| 357 |
Log.e(TAG, "Failed to recover CardEmulationService."); |
| 358 |
return false; |
| 359 |
} |
| 360 |
try { |
| 361 |
return sService.registerAidGroupForService(mContext.getUser().getIdentifier(), |
| 362 |
service, aidGroup); |
| 363 |
} catch (RemoteException ee) { |
| 364 |
Log.e(TAG, "Failed to reach CardEmulationService."); |
| 365 |
return false; |
| 366 |
} |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
@RequiresPermission(android.Manifest.permission.NFC) |
| 387 |
@NonNull |
| 388 |
public boolean unsetOffHostForService(@NonNull ComponentName service) { |
| 389 |
NfcAdapter adapter = NfcAdapter.getDefaultAdapter(mContext); |
| 390 |
if (adapter == null) { |
| 391 |
return false; |
| 392 |
} |
| 393 |
|
| 394 |
try { |
| 395 |
return sService.unsetOffHostForService(mContext.getUser().getIdentifier(), service); |
| 396 |
} catch (RemoteException e) { |
| 397 |
|
| 398 |
recoverService(); |
| 399 |
if (sService == null) { |
| 400 |
Log.e(TAG, "Failed to recover CardEmulationService."); |
| 401 |
return false; |
| 402 |
} |
| 403 |
try { |
| 404 |
return sService.unsetOffHostForService(mContext.getUser().getIdentifier(), service); |
| 405 |
} catch (RemoteException ee) { |
| 406 |
Log.e(TAG, "Failed to reach CardEmulationService."); |
| 407 |
return false; |
| 408 |
} |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
). |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
@RequiresPermission(android.Manifest.permission.NFC) |
| 445 |
@NonNull |
| 446 |
public boolean setOffHostForService(@NonNull ComponentName service, |
| 447 |
@NonNull String offHostSecureElement) { |
| 448 |
boolean validSecureElement = false; |
| 449 |
|
| 450 |
NfcAdapter adapter = NfcAdapter.getDefaultAdapter(mContext); |
| 451 |
if (adapter == null || offHostSecureElement == null) { |
| 452 |
return false; |
| 453 |
} |
| 454 |
|
| 455 |
List<String> validSE = adapter.getSupportedOffHostSecureElements(); |
| 456 |
if ((offHostSecureElement.startsWith("eSE") && !validSE.contains("eSE")) |
| 457 |
|| (offHostSecureElement.startsWith("SIM") && !validSE.contains("SIM"))) { |
| 458 |
return false; |
| 459 |
} |
| 460 |
|
| 461 |
if (!offHostSecureElement.startsWith("eSE") && !offHostSecureElement.startsWith("SIM")) { |
| 462 |
return false; |
| 463 |
} |
| 464 |
|
| 465 |
if (offHostSecureElement.equals("eSE")) { |
| 466 |
offHostSecureElement = "eSE1"; |
| 467 |
} else if (offHostSecureElement.equals("SIM")) { |
| 468 |
offHostSecureElement = "SIM1"; |
| 469 |
} |
| 470 |
|
| 471 |
try { |
| 472 |
return sService.setOffHostForService(mContext.getUser().getIdentifier(), service, |
| 473 |
offHostSecureElement); |
| 474 |
} catch (RemoteException e) { |
| 475 |
|
| 476 |
recoverService(); |
| 477 |
if (sService == null) { |
| 478 |
Log.e(TAG, "Failed to recover CardEmulationService."); |
| 479 |
return false; |
| 480 |
} |
| 481 |
try { |
| 482 |
return sService.setOffHostForService(mContext.getUser().getIdentifier(), service, |
| 483 |
offHostSecureElement); |
| 484 |
} catch (RemoteException ee) { |
| 485 |
Log.e(TAG, "Failed to reach CardEmulationService."); |
| 486 |
return false; |
| 487 |
} |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
public List<String> getAidsForService(ComponentName service, String category) { |
| 506 |
try { |
| 507 |
AidGroup group = sService.getAidGroupForService(mContext.getUser().getIdentifier(), |
| 508 |
service, category); |
| 509 |
return (group != null ? group.getAids() : null); |
| 510 |
} catch (RemoteException e) { |
| 511 |
recoverService(); |
| 512 |
if (sService == null) { |
| 513 |
Log.e(TAG, "Failed to recover CardEmulationService."); |
| 514 |
return null; |
| 515 |
} |
| 516 |
try { |
| 517 |
AidGroup group = sService.getAidGroupForService(mContext.getUser().getIdentifier(), |
| 518 |
service, category); |
| 519 |
return (group != null ? group.getAids() : null); |
| 520 |
} catch (RemoteException ee) { |
| 521 |
Log.e(TAG, "Failed to recover CardEmulationService."); |
| 522 |
return null; |
| 523 |
} |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
public boolean removeAidsForService(ComponentName service, String category) { |
| 543 |
try { |
| 544 |
return sService.removeAidGroupForService(mContext.getUser().getIdentifier(), service, |
| 545 |
category); |
| 546 |
} catch (RemoteException e) { |
| 547 |
|
| 548 |
recoverService(); |
| 549 |
if (sService == null) { |
| 550 |
Log.e(TAG, "Failed to recover CardEmulationService."); |
| 551 |
return false; |
| 552 |
} |
| 553 |
try { |
| 554 |
return sService.removeAidGroupForService(mContext.getUser().getIdentifier(), |
| 555 |
service, category); |
| 556 |
} catch (RemoteException ee) { |
| 557 |
Log.e(TAG, "Failed to reach CardEmulationService."); |
| 558 |
return false; |
| 559 |
} |
| 560 |
} |
| 561 |
} |
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
public boolean setPreferredService(Activity activity, ComponentName service) { |
| 593 |
|
| 594 |
if (activity == null || service == null) { |
| 595 |
throw new NullPointerException("activity or service or category is null"); |
| 596 |
} |
| 597 |
if (!activity.isResumed()) { |
| 598 |
throw new IllegalArgumentException("Activity must be resumed."); |
| 599 |
} |
| 600 |
try { |
| 601 |
return sService.setPreferredService(service); |
| 602 |
} catch (RemoteException e) { |
| 603 |
|
| 604 |
recoverService(); |
| 605 |
if (sService == null) { |
| 606 |
Log.e(TAG, "Failed to recover CardEmulationService."); |
| 607 |
return false; |
| 608 |
} |
| 609 |
try { |
| 610 |
return sService.setPreferredService(service); |
| 611 |
} catch (RemoteException ee) { |
| 612 |
Log.e(TAG, "Failed to reach CardEmulationService."); |
| 613 |
return false; |
| 614 |
} |
| 615 |
} |
| 616 |
} |
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
public boolean unsetPreferredService(Activity activity) { |
| 629 |
if (activity == null) { |
| 630 |
throw new NullPointerException("activity is null"); |
| 631 |
} |
| 632 |
if (!activity.isResumed()) { |
| 633 |
throw new IllegalArgumentException("Activity must be resumed."); |
| 634 |
} |
| 635 |
try { |
| 636 |
return sService.unsetPreferredService(); |
| 637 |
} catch (RemoteException e) { |
| 638 |
|
| 639 |
recoverService(); |
| 640 |
if (sService == null) { |
| 641 |
Log.e(TAG, "Failed to recover CardEmulationService."); |
| 642 |
return false; |
| 643 |
} |
| 644 |
try { |
| 645 |
return sService.unsetPreferredService(); |
| 646 |
} catch (RemoteException ee) { |
| 647 |
Log.e(TAG, "Failed to reach CardEmulationService."); |
| 648 |
return false; |
| 649 |
} |
| 650 |
} |
| 651 |
} |
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
public boolean supportsAidPrefixRegistration() { |
| 664 |
try { |
| 665 |
return sService.supportsAidPrefixRegistration(); |
| 666 |
} catch (RemoteException e) { |
| 667 |
recoverService(); |
| 668 |
if (sService == null) { |
| 669 |
Log.e(TAG, "Failed to recover CardEmulationService."); |
| 670 |
return false; |
| 671 |
} |
| 672 |
try { |
| 673 |
return sService.supportsAidPrefixRegistration(); |
| 674 |
} catch (RemoteException ee) { |
| 675 |
Log.e(TAG, "Failed to reach CardEmulationService."); |
| 676 |
return false; |
| 677 |
} |
| 678 |
} |
| 679 |
} |
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
@RequiresPermission(android.Manifest.permission.NFC_PREFERRED_PAYMENT_INFO) |
| 687 |
@Nullable |
| 688 |
public List<String> getAidsForPreferredPaymentService() { |
| 689 |
try { |
| 690 |
ApduServiceInfo serviceInfo = sService.getPreferredPaymentService( |
| 691 |
mContext.getUser().getIdentifier()); |
| 692 |
return (serviceInfo != null ? serviceInfo.getAids() : null); |
| 693 |
} catch (RemoteException e) { |
| 694 |
recoverService(); |
| 695 |
if (sService == null) { |
| 696 |
Log.e(TAG, "Failed to recover CardEmulationService."); |
| 697 |
throw e.rethrowFromSystemServer(); |
| 698 |
} |
| 699 |
try { |
| 700 |
ApduServiceInfo serviceInfo = |
| 701 |
sService.getPreferredPaymentService(mContext.getUser().getIdentifier()); |
| 702 |
return (serviceInfo != null ? serviceInfo.getAids() : null); |
| 703 |
} catch (RemoteException ee) { |
| 704 |
Log.e(TAG, "Failed to recover CardEmulationService."); |
| 705 |
throw e.rethrowFromSystemServer(); |
| 706 |
} |
| 707 |
} |
| 708 |
} |
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
). |
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
@RequiresPermission(android.Manifest.permission.NFC_PREFERRED_PAYMENT_INFO) |
| 727 |
@Nullable |
| 728 |
public String getRouteDestinationForPreferredPaymentService() { |
| 729 |
try { |
| 730 |
ApduServiceInfo serviceInfo = sService.getPreferredPaymentService( |
| 731 |
mContext.getUser().getIdentifier()); |
| 732 |
if (serviceInfo != null) { |
| 733 |
if (!serviceInfo.isOnHost()) { |
| 734 |
return serviceInfo.getOffHostSecureElement() == null ? |
| 735 |
"OffHost" : serviceInfo.getOffHostSecureElement(); |
| 736 |
} |
| 737 |
return "Host"; |
| 738 |
} |
| 739 |
return null; |
| 740 |
} catch (RemoteException e) { |
| 741 |
recoverService(); |
| 742 |
if (sService == null) { |
| 743 |
Log.e(TAG, "Failed to recover CardEmulationService."); |
| 744 |
throw e.rethrowFromSystemServer(); |
| 745 |
} |
| 746 |
try { |
| 747 |
ApduServiceInfo serviceInfo = |
| 748 |
sService.getPreferredPaymentService(mContext.getUser().getIdentifier()); |
| 749 |
if (serviceInfo != null) { |
| 750 |
if (!serviceInfo.isOnHost()) { |
| 751 |
return serviceInfo.getOffHostSecureElement() == null ? |
| 752 |
"Offhost" : serviceInfo.getOffHostSecureElement(); |
| 753 |
} |
| 754 |
return "Host"; |
| 755 |
} |
| 756 |
return null; |
| 757 |
|
| 758 |
} catch (RemoteException ee) { |
| 759 |
Log.e(TAG, "Failed to recover CardEmulationService."); |
| 760 |
throw e.rethrowFromSystemServer(); |
| 761 |
} |
| 762 |
} |
| 763 |
} |
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
@RequiresPermission(android.Manifest.permission.NFC_PREFERRED_PAYMENT_INFO) |
| 771 |
@Nullable |
| 772 |
public CharSequence getDescriptionForPreferredPaymentService() { |
| 773 |
try { |
| 774 |
ApduServiceInfo serviceInfo = sService.getPreferredPaymentService( |
| 775 |
mContext.getUser().getIdentifier()); |
| 776 |
return (serviceInfo != null ? serviceInfo.getDescription() : null); |
| 777 |
} catch (RemoteException e) { |
| 778 |
recoverService(); |
| 779 |
if (sService == null) { |
| 780 |
Log.e(TAG, "Failed to recover CardEmulationService."); |
| 781 |
throw e.rethrowFromSystemServer(); |
| 782 |
} |
| 783 |
try { |
| 784 |
ApduServiceInfo serviceInfo = |
| 785 |
sService.getPreferredPaymentService(mContext.getUser().getIdentifier()); |
| 786 |
return (serviceInfo != null ? serviceInfo.getDescription() : null); |
| 787 |
} catch (RemoteException ee) { |
| 788 |
Log.e(TAG, "Failed to recover CardEmulationService."); |
| 789 |
throw e.rethrowFromSystemServer(); |
| 790 |
} |
| 791 |
} |
| 792 |
} |
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
public boolean setDefaultServiceForCategory(ComponentName service, String category) { |
| 798 |
try { |
| 799 |
return sService.setDefaultServiceForCategory(mContext.getUser().getIdentifier(), |
| 800 |
service, category); |
| 801 |
} catch (RemoteException e) { |
| 802 |
|
| 803 |
recoverService(); |
| 804 |
if (sService == null) { |
| 805 |
Log.e(TAG, "Failed to recover CardEmulationService."); |
| 806 |
return false; |
| 807 |
} |
| 808 |
try { |
| 809 |
return sService.setDefaultServiceForCategory(mContext.getUser().getIdentifier(), |
| 810 |
service, category); |
| 811 |
} catch (RemoteException ee) { |
| 812 |
Log.e(TAG, "Failed to reach CardEmulationService."); |
| 813 |
return false; |
| 814 |
} |
| 815 |
} |
| 816 |
} |
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
public boolean setDefaultForNextTap(ComponentName service) { |
| 822 |
try { |
| 823 |
return sService.setDefaultForNextTap(mContext.getUser().getIdentifier(), service); |
| 824 |
} catch (RemoteException e) { |
| 825 |
|
| 826 |
recoverService(); |
| 827 |
if (sService == null) { |
| 828 |
Log.e(TAG, "Failed to recover CardEmulationService."); |
| 829 |
return false; |
| 830 |
} |
| 831 |
try { |
| 832 |
return sService.setDefaultForNextTap(mContext.getUser().getIdentifier(), service); |
| 833 |
} catch (RemoteException ee) { |
| 834 |
Log.e(TAG, "Failed to reach CardEmulationService."); |
| 835 |
return false; |
| 836 |
} |
| 837 |
} |
| 838 |
} |
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
public boolean setDefaultForNextTap(int userId, ComponentName service) { |
| 844 |
try { |
| 845 |
return sService.setDefaultForNextTap(userId, service); |
| 846 |
} catch (RemoteException e) { |
| 847 |
|
| 848 |
recoverService(); |
| 849 |
if (sService == null) { |
| 850 |
Log.e(TAG, "Failed to recover CardEmulationService."); |
| 851 |
return false; |
| 852 |
} |
| 853 |
try { |
| 854 |
return sService.setDefaultForNextTap(userId, service); |
| 855 |
} catch (RemoteException ee) { |
| 856 |
Log.e(TAG, "Failed to reach CardEmulationService."); |
| 857 |
return false; |
| 858 |
} |
| 859 |
} |
| 860 |
} |
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
public List<ApduServiceInfo> getServices(String category) { |
| 866 |
try { |
| 867 |
return sService.getServices(mContext.getUser().getIdentifier(), category); |
| 868 |
} catch (RemoteException e) { |
| 869 |
|
| 870 |
recoverService(); |
| 871 |
if (sService == null) { |
| 872 |
Log.e(TAG, "Failed to recover CardEmulationService."); |
| 873 |
return null; |
| 874 |
} |
| 875 |
try { |
| 876 |
return sService.getServices(mContext.getUser().getIdentifier(), category); |
| 877 |
} catch (RemoteException ee) { |
| 878 |
Log.e(TAG, "Failed to reach CardEmulationService."); |
| 879 |
return null; |
| 880 |
} |
| 881 |
} |
| 882 |
} |
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
public List<ApduServiceInfo> getServices(String category, int userId) { |
| 888 |
try { |
| 889 |
return sService.getServices(userId, category); |
| 890 |
} catch (RemoteException e) { |
| 891 |
|
| 892 |
recoverService(); |
| 893 |
if (sService == null) { |
| 894 |
Log.e(TAG, "Failed to recover CardEmulationService."); |
| 895 |
return null; |
| 896 |
} |
| 897 |
try { |
| 898 |
return sService.getServices(userId, category); |
| 899 |
} catch (RemoteException ee) { |
| 900 |
Log.e(TAG, "Failed to reach CardEmulationService."); |
| 901 |
return null; |
| 902 |
} |
| 903 |
} |
| 904 |
} |
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
public static boolean isValidAid(String aid) { |
| 920 |
if (aid == null) |
| 921 |
return false; |
| 922 |
|
| 923 |
|
| 924 |
if ((aid.endsWith("*") || aid.endsWith("#")) && ((aid.length() % 2) == 0)) { |
| 925 |
Log.e(TAG, "AID " + aid + " is not a valid AID."); |
| 926 |
return false; |
| 927 |
} |
| 928 |
|
| 929 |
|
| 930 |
if ((!(aid.endsWith("*") || aid.endsWith("#"))) && ((aid.length() % 2) != 0)) { |
| 931 |
Log.e(TAG, "AID " + aid + " is not a valid AID."); |
| 932 |
return false; |
| 933 |
} |
| 934 |
|
| 935 |
|
| 936 |
if (!AID_PATTERN.matcher(aid).matches()) { |
| 937 |
Log.e(TAG, "AID " + aid + " is not a valid AID."); |
| 938 |
return false; |
| 939 |
} |
| 940 |
|
| 941 |
return true; |
| 942 |
} |
| 943 |
|
| 944 |
void recoverService() { |
| 945 |
NfcAdapter adapter = NfcAdapter.getDefaultAdapter(mContext); |
| 946 |
sService = adapter.getCardEmulationService(); |
| 947 |
} |
| 948 |
|
| 949 |
} |