From bb951c893973691554f49d2e725985125f866b27 Mon Sep 17 00:00:00 2001 From: Jeff Hamilton Date: Tue, 8 Nov 2011 16:55:13 -0600 Subject: Changes for access control. The package name is now required when using the NFC extras APIs so the context is stored away and used to derive the package name to be sent to the NfcService. Bug: 4515759 Change-Id: I1a3aba3fc026e0090a914b0686fc4b8dec25b927 --- .../com/android/nfc_extras/NfcAdapterExtras.java | 69 ++++++++++++---------- .../nfc_extras/NfcExecutionEnvironment.java | 16 +++-- 2 files changed, 44 insertions(+), 41 deletions(-) (limited to 'nfc-extras') diff --git a/nfc-extras/java/com/android/nfc_extras/NfcAdapterExtras.java b/nfc-extras/java/com/android/nfc_extras/NfcAdapterExtras.java index 99cbb86..62213de 100644 --- a/nfc-extras/java/com/android/nfc_extras/NfcAdapterExtras.java +++ b/nfc-extras/java/com/android/nfc_extras/NfcAdapterExtras.java @@ -16,8 +16,7 @@ package com.android.nfc_extras; -import android.annotation.SdkConstant; -import android.annotation.SdkConstant.SdkConstantType; +import android.content.Context; import android.nfc.INfcAdapterExtras; import android.nfc.NfcAdapter; import android.os.RemoteException; @@ -60,10 +59,14 @@ public final class NfcAdapterExtras { // best effort recovery private static NfcAdapter sAdapter; private static INfcAdapterExtras sService; - private static NfcAdapterExtras sSingleton; - private static NfcExecutionEnvironment sEmbeddedEe; - private static CardEmulationRoute sRouteOff; - private static CardEmulationRoute sRouteOnWhenScreenOn; + private static final CardEmulationRoute ROUTE_OFF = + new CardEmulationRoute(CardEmulationRoute.ROUTE_OFF, null); + + private final NfcExecutionEnvironment mEmbeddedEe; + private final CardEmulationRoute mRouteOnWhenScreenOn; + + final Context mContext; + final String mPackageName; /** get service handles */ private static void initService() { @@ -84,31 +87,35 @@ public final class NfcAdapterExtras { * @return the {@link NfcAdapterExtras} object for the given {@link NfcAdapter} */ public static NfcAdapterExtras get(NfcAdapter adapter) { - synchronized(NfcAdapterExtras.class) { - if (sSingleton == null) { + Context context = adapter.getContext(); + if (context == null) { + throw new UnsupportedOperationException( + "You must pass a context to your NfcAdapter to use the NFC extras APIs"); + } + + synchronized (NfcAdapterExtras.class) { + if (sService == null) { try { sAdapter = adapter; - sSingleton = new NfcAdapterExtras(); - sEmbeddedEe = new NfcExecutionEnvironment(sSingleton); - sRouteOff = new CardEmulationRoute(CardEmulationRoute.ROUTE_OFF, null); - sRouteOnWhenScreenOn = new CardEmulationRoute( - CardEmulationRoute.ROUTE_ON_WHEN_SCREEN_ON, sEmbeddedEe); initService(); } finally { if (sService == null) { - sRouteOnWhenScreenOn = null; - sRouteOff = null; - sEmbeddedEe = null; - sSingleton = null; sAdapter = null; } } } - return sSingleton; } + + return new NfcAdapterExtras(context); } - private NfcAdapterExtras() {} + private NfcAdapterExtras(Context context) { + mContext = context.getApplicationContext(); + mPackageName = context.getPackageName(); + mEmbeddedEe = new NfcExecutionEnvironment(this); + mRouteOnWhenScreenOn = new CardEmulationRoute(CardEmulationRoute.ROUTE_ON_WHEN_SCREEN_ON, + mEmbeddedEe); + } /** * Immutable data class that describes a card emulation route. @@ -166,18 +173,16 @@ public final class NfcAdapterExtras { * *

* Requires the {@link android.Manifest.permission#WRITE_SECURE_SETTINGS} permission. - * - * @return */ public CardEmulationRoute getCardEmulationRoute() { try { - int route = sService.getCardEmulationRoute(); + int route = sService.getCardEmulationRoute(mPackageName); return route == CardEmulationRoute.ROUTE_OFF ? - sRouteOff : - sRouteOnWhenScreenOn; + ROUTE_OFF : + mRouteOnWhenScreenOn; } catch (RemoteException e) { attemptDeadServiceRecovery(e); - return sRouteOff; + return ROUTE_OFF; } } @@ -189,11 +194,11 @@ public final class NfcAdapterExtras { *

* Requires the {@link android.Manifest.permission#WRITE_SECURE_SETTINGS} permission. * - * @param route a {@link #CardEmulationRoute} + * @param route a {@link CardEmulationRoute} */ public void setCardEmulationRoute(CardEmulationRoute route) { try { - sService.setCardEmulationRoute(route.route); + sService.setCardEmulationRoute(mPackageName, route.route); } catch (RemoteException e) { attemptDeadServiceRecovery(e); } @@ -201,7 +206,7 @@ public final class NfcAdapterExtras { /** * Get the {@link NfcExecutionEnvironment} that is embedded with the - * {@link NFcAdapter}. + * {@link NfcAdapter}. * *

* Requires the {@link android.Manifest.permission#WRITE_SECURE_SETTINGS} permission. @@ -209,7 +214,7 @@ public final class NfcAdapterExtras { * @return a {@link NfcExecutionEnvironment}, or null if there is no embedded NFC-EE */ public NfcExecutionEnvironment getEmbeddedExecutionEnvironment() { - return sEmbeddedEe; + return mEmbeddedEe; } /** @@ -218,12 +223,12 @@ public final class NfcAdapterExtras { * Some implementations of NFC Adapter Extras may require applications * to authenticate with a token, before using other methods. * - * @param a implementation specific token - * @throws a {@link java.lang.SecurityException} if authentication failed + * @param token a implementation specific token + * @throws java.lang.SecurityException if authentication failed */ public void authenticate(byte[] token) { try { - sService.authenticate(token); + sService.authenticate(mPackageName, token); } catch (RemoteException e) { attemptDeadServiceRecovery(e); } diff --git a/nfc-extras/java/com/android/nfc_extras/NfcExecutionEnvironment.java b/nfc-extras/java/com/android/nfc_extras/NfcExecutionEnvironment.java index 63c2de2..f47327a 100644 --- a/nfc-extras/java/com/android/nfc_extras/NfcExecutionEnvironment.java +++ b/nfc-extras/java/com/android/nfc_extras/NfcExecutionEnvironment.java @@ -16,20 +16,17 @@ package com.android.nfc_extras; -import java.io.IOException; - import android.annotation.SdkConstant; import android.annotation.SdkConstant.SdkConstantType; -import android.content.Context; -import android.nfc.INfcAdapterExtras; -import android.nfc.NfcAdapter; import android.os.Binder; import android.os.Bundle; -import android.os.IBinder; import android.os.RemoteException; +import java.io.IOException; + public class NfcExecutionEnvironment { private final NfcAdapterExtras mExtras; + private final Binder mToken; /** * Broadcast Action: An ISO-DEP AID was selected. @@ -115,6 +112,7 @@ public class NfcExecutionEnvironment { NfcExecutionEnvironment(NfcAdapterExtras extras) { mExtras = extras; + mToken = new Binder(); } /** @@ -133,7 +131,7 @@ public class NfcExecutionEnvironment { */ public void open() throws IOException { try { - Bundle b = mExtras.getService().open(new Binder()); + Bundle b = mExtras.getService().open(mExtras.mPackageName, mToken); throwBundle(b); } catch (RemoteException e) { mExtras.attemptDeadServiceRecovery(e); @@ -151,7 +149,7 @@ public class NfcExecutionEnvironment { */ public void close() throws IOException { try { - throwBundle(mExtras.getService().close()); + throwBundle(mExtras.getService().close(mExtras.mPackageName, mToken)); } catch (RemoteException e) { mExtras.attemptDeadServiceRecovery(e); throw new IOException("NFC Service was dead"); @@ -169,7 +167,7 @@ public class NfcExecutionEnvironment { public byte[] transceive(byte[] in) throws IOException { Bundle b; try { - b = mExtras.getService().transceive(in); + b = mExtras.getService().transceive(mExtras.mPackageName, in); } catch (RemoteException e) { mExtras.attemptDeadServiceRecovery(e); throw new IOException("NFC Service was dead, need to re-open"); -- cgit v1.1