diff options
Diffstat (limited to 'nfc-extras/java/com/android/nfc_extras/NfcAdapterExtras.java')
-rw-r--r-- | nfc-extras/java/com/android/nfc_extras/NfcAdapterExtras.java | 70 |
1 files changed, 58 insertions, 12 deletions
diff --git a/nfc-extras/java/com/android/nfc_extras/NfcAdapterExtras.java b/nfc-extras/java/com/android/nfc_extras/NfcAdapterExtras.java index cf38bd1..6001be9 100644 --- a/nfc-extras/java/com/android/nfc_extras/NfcAdapterExtras.java +++ b/nfc-extras/java/com/android/nfc_extras/NfcAdapterExtras.java @@ -18,6 +18,7 @@ package com.android.nfc_extras; import android.annotation.SdkConstant; import android.annotation.SdkConstant.SdkConstantType; +import android.nfc.ApduList; import android.nfc.INfcAdapterExtras; import android.nfc.NfcAdapter; import android.os.RemoteException; @@ -55,14 +56,21 @@ public final class NfcAdapterExtras { public static final String ACTION_RF_FIELD_OFF_DETECTED = "com.android.nfc_extras.action.RF_FIELD_OFF_DETECTED"; - // protected by NfcAdapterExtras.class, and final after first construction + // protected by NfcAdapterExtras.class, and final after first construction, + // except for attemptDeadServiceRecovery() when NFC crashes - we accept a + // best effort recovery + private static NfcAdapter sAdapter; private static INfcAdapterExtras sService; - private static boolean sIsInitialized = false; private static NfcAdapterExtras sSingleton; private static NfcExecutionEnvironment sEmbeddedEe; private static CardEmulationRoute sRouteOff; private static CardEmulationRoute sRouteOnWhenScreenOn; + /** get service handles */ + private static void initService() { + sService = sAdapter.getNfcAdapterExtrasInterface(); + } + /** * Get the {@link NfcAdapterExtras} for the given {@link NfcAdapter}. * @@ -74,14 +82,23 @@ public final class NfcAdapterExtras { */ public static NfcAdapterExtras get(NfcAdapter adapter) { synchronized(NfcAdapterExtras.class) { - if (!sIsInitialized) { - sIsInitialized = true; - sService = adapter.getNfcAdapterExtrasInterface(); - sEmbeddedEe = new NfcExecutionEnvironment(sService); - sRouteOff = new CardEmulationRoute(CardEmulationRoute.ROUTE_OFF, null); - sRouteOnWhenScreenOn = new CardEmulationRoute( - CardEmulationRoute.ROUTE_ON_WHEN_SCREEN_ON, sEmbeddedEe); - sSingleton = new NfcAdapterExtras(); + if (sSingleton == null) { + try { + sAdapter = adapter; + sRouteOff = new CardEmulationRoute(CardEmulationRoute.ROUTE_OFF, null); + sSingleton = new NfcAdapterExtras(); + sEmbeddedEe = new NfcExecutionEnvironment(sSingleton); + sRouteOnWhenScreenOn = new CardEmulationRoute( + CardEmulationRoute.ROUTE_ON_WHEN_SCREEN_ON, sEmbeddedEe); + initService(); + } finally { + if (sSingleton == null) { + sService = null; + sEmbeddedEe = null; + sRouteOff = null; + sRouteOnWhenScreenOn = null; + } + } } return sSingleton; } @@ -128,6 +145,19 @@ public final class NfcAdapterExtras { } /** + * NFC service dead - attempt best effort recovery + */ + void attemptDeadServiceRecovery(Exception e) { + Log.e(TAG, "NFC Adapter Extras dead - attempting to recover"); + sAdapter.attemptDeadServiceRecovery(e); + initService(); + } + + INfcAdapterExtras getService() { + return sService; + } + + /** * Get the routing state of this NFC EE. * * <p class="note"> @@ -142,7 +172,7 @@ public final class NfcAdapterExtras { sRouteOff : sRouteOnWhenScreenOn; } catch (RemoteException e) { - Log.e(TAG, "", e); + attemptDeadServiceRecovery(e); return sRouteOff; } } @@ -161,7 +191,7 @@ public final class NfcAdapterExtras { try { sService.setCardEmulationRoute(route.route); } catch (RemoteException e) { - Log.e(TAG, "", e); + attemptDeadServiceRecovery(e); } } @@ -177,4 +207,20 @@ public final class NfcAdapterExtras { public NfcExecutionEnvironment getEmbeddedExecutionEnvironment() { return sEmbeddedEe; } + + public void registerTearDownApdus(String packageName, ApduList apdus) { + try { + sService.registerTearDownApdus(packageName, apdus); + } catch (RemoteException e) { + attemptDeadServiceRecovery(e); + } + } + + public void unregisterTearDownApdus(String packageName) { + try { + sService.unregisterTearDownApdus(packageName); + } catch (RemoteException e) { + attemptDeadServiceRecovery(e); + } + } } |