diff options
Diffstat (limited to 'nfc-extras')
-rw-r--r-- | nfc-extras/java/com/android/nfc_extras/NfcAdapterExtras.java | 96 | ||||
-rw-r--r-- | nfc-extras/java/com/android/nfc_extras/NfcExecutionEnvironment.java | 16 | ||||
-rw-r--r-- | nfc-extras/tests/Android.mk | 32 | ||||
-rw-r--r-- | nfc-extras/tests/AndroidManifest.xml | 39 | ||||
-rw-r--r-- | nfc-extras/tests/src/com/android/nfc_extras/BasicNfcEeTest.java | 117 |
5 files changed, 246 insertions, 54 deletions
diff --git a/nfc-extras/java/com/android/nfc_extras/NfcAdapterExtras.java b/nfc-extras/java/com/android/nfc_extras/NfcAdapterExtras.java index 99cbb86..9c87c22 100644 --- a/nfc-extras/java/com/android/nfc_extras/NfcAdapterExtras.java +++ b/nfc-extras/java/com/android/nfc_extras/NfcAdapterExtras.java @@ -16,8 +16,9 @@ package com.android.nfc_extras; -import android.annotation.SdkConstant; -import android.annotation.SdkConstant.SdkConstantType; +import java.util.HashMap; + +import android.content.Context; import android.nfc.INfcAdapterExtras; import android.nfc.NfcAdapter; import android.os.RemoteException; @@ -58,16 +59,22 @@ public final class NfcAdapterExtras { // 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 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); + + // contents protected by NfcAdapterExtras.class + private static final HashMap<NfcAdapter, NfcAdapterExtras> sNfcExtras = new HashMap(); + + private final NfcExecutionEnvironment mEmbeddedEe; + private final CardEmulationRoute mRouteOnWhenScreenOn; + + private final NfcAdapter mAdapter; + final String mPackageName; /** get service handles */ - private static void initService() { - final INfcAdapterExtras service = sAdapter.getNfcAdapterExtrasInterface(); + private static void initService(NfcAdapter adapter) { + final INfcAdapterExtras service = adapter.getNfcAdapterExtrasInterface(); if (service != null) { // Leave stale rather than receive a null value. sService = service; @@ -84,31 +91,32 @@ 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) { - 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; - } - } + 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) { + initService(adapter); + } + NfcAdapterExtras extras = sNfcExtras.get(adapter); + if (extras == null) { + extras = new NfcAdapterExtras(adapter); + sNfcExtras.put(adapter, extras); } - return sSingleton; + return extras; } } - private NfcAdapterExtras() {} + private NfcAdapterExtras(NfcAdapter adapter) { + mAdapter = adapter; + mPackageName = adapter.getContext().getPackageName(); + mEmbeddedEe = new NfcExecutionEnvironment(this); + mRouteOnWhenScreenOn = new CardEmulationRoute(CardEmulationRoute.ROUTE_ON_WHEN_SCREEN_ON, + mEmbeddedEe); + } /** * Immutable data class that describes a card emulation route. @@ -153,8 +161,8 @@ public final class NfcAdapterExtras { */ void attemptDeadServiceRecovery(Exception e) { Log.e(TAG, "NFC Adapter Extras dead - attempting to recover"); - sAdapter.attemptDeadServiceRecovery(e); - initService(); + mAdapter.attemptDeadServiceRecovery(e); + initService(mAdapter); } INfcAdapterExtras getService() { @@ -166,18 +174,16 @@ public final class NfcAdapterExtras { * * <p class="note"> * 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 +195,11 @@ public final class NfcAdapterExtras { * <p class="note"> * 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 +207,7 @@ public final class NfcAdapterExtras { /** * Get the {@link NfcExecutionEnvironment} that is embedded with the - * {@link NFcAdapter}. + * {@link NfcAdapter}. * * <p class="note"> * Requires the {@link android.Manifest.permission#WRITE_SECURE_SETTINGS} permission. @@ -209,7 +215,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 +224,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"); diff --git a/nfc-extras/tests/Android.mk b/nfc-extras/tests/Android.mk new file mode 100644 index 0000000..3eca76d --- /dev/null +++ b/nfc-extras/tests/Android.mk @@ -0,0 +1,32 @@ +# Copyright 2011, The Android Open Source Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +LOCAL_PATH:= $(call my-dir) +include $(CLEAR_VARS) + +# We only want this apk build for tests. +LOCAL_MODULE_TAGS := tests + +LOCAL_JAVA_LIBRARIES := \ + android.test.runner \ + com.android.nfc_extras + +# Include all test java files. +LOCAL_SRC_FILES := $(call all-java-files-under, src) + +LOCAL_PACKAGE_NAME := NfcExtrasTests + +LOCAL_SDK_VERSION := current + +include $(BUILD_PACKAGE) diff --git a/nfc-extras/tests/AndroidManifest.xml b/nfc-extras/tests/AndroidManifest.xml new file mode 100644 index 0000000..0cc6653 --- /dev/null +++ b/nfc-extras/tests/AndroidManifest.xml @@ -0,0 +1,39 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2011 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> + +<!-- package name must be unique so suffix with "tests" so package loader doesn't ignore us --> +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="com.android.nfc_extras.tests"> + + <!-- We add an application tag here just so that we can indicate that + this package needs to link against the android.test library, + which is needed when building test cases. --> + <application> + <uses-library android:name="android.test.runner" /> + <uses-library android:name="com.android.nfc_extras" /> + </application> + + <uses-permission android:name="android.permission.NFC"/> + + <!-- + Run all tests with + adb shell am instrument -w com.android.nfc_extras.tests/android.test.InstrumentationTestRunner + --> + <instrumentation android:name="android.test.InstrumentationTestRunner" + android:targetPackage="com.android.nfc_extras.tests" + android:label="Tests for NFC Extras library"/> + +</manifest> diff --git a/nfc-extras/tests/src/com/android/nfc_extras/BasicNfcEeTest.java b/nfc-extras/tests/src/com/android/nfc_extras/BasicNfcEeTest.java new file mode 100644 index 0000000..e1025aa --- /dev/null +++ b/nfc-extras/tests/src/com/android/nfc_extras/BasicNfcEeTest.java @@ -0,0 +1,117 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.nfc_extras; + +import android.content.Context; +import android.nfc.NfcAdapter; +import android.test.InstrumentationTestCase; +import android.util.Log; + +import com.android.nfc_extras.NfcAdapterExtras; +import com.android.nfc_extras.NfcAdapterExtras.CardEmulationRoute; +import com.android.nfc_extras.NfcExecutionEnvironment; + +import java.io.IOException; +import java.util.Arrays; + +public class BasicNfcEeTest extends InstrumentationTestCase { + private Context mContext; + private NfcAdapterExtras mAdapterExtras; + private NfcExecutionEnvironment mEe; + + public static final byte[] SELECT_CARD_MANAGER_COMMAND = new byte[] { + (byte)0x00, (byte)0xA4, (byte)0x04, (byte)0x00, // command + (byte)0x08, // data length + (byte)0xA0, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x03, (byte)0x00, (byte)0x00, + (byte)0x00, // card manager AID + (byte)0x00 // trailer + }; + + public static final byte[] SELECT_CARD_MANAGER_RESPONSE = new byte[] { + (byte)0x90, (byte)0x00, + }; + + @Override + protected void setUp() throws Exception { + super.setUp(); + mContext = getInstrumentation().getContext(); + mAdapterExtras = NfcAdapterExtras.get(NfcAdapter.getDefaultAdapter(mContext)); + mEe = mAdapterExtras.getEmbeddedExecutionEnvironment(); + } + + public void testSendCardManagerApdu() throws IOException { + mEe.open(); + + try { + byte[] out = mEe.transceive(SELECT_CARD_MANAGER_COMMAND); + assertTrue(out.length >= SELECT_CARD_MANAGER_RESPONSE.length); + byte[] trailing = Arrays.copyOfRange(out, + out.length - SELECT_CARD_MANAGER_RESPONSE.length, + out.length); + assertByteArrayEquals(SELECT_CARD_MANAGER_RESPONSE, trailing); + + } finally { + mEe.close(); + } + + } + + public void testSendCardManagerApduMultiple() throws IOException { + for (int i=0; i<10; i++) { + try { + mEe.open(); + + try { + byte[] out = mEe.transceive(SELECT_CARD_MANAGER_COMMAND); + byte[] trailing = Arrays.copyOfRange(out, + out.length - SELECT_CARD_MANAGER_RESPONSE.length, + out.length); + + } finally { + try {Thread.sleep(1000);} catch (InterruptedException e) {} + mEe.close(); + } + } catch (IOException e) {} + } + + testSendCardManagerApdu(); + + } + + public void testEnableEe() { + mAdapterExtras.setCardEmulationRoute( + new CardEmulationRoute(CardEmulationRoute.ROUTE_ON_WHEN_SCREEN_ON, mEe)); + CardEmulationRoute newRoute = mAdapterExtras.getCardEmulationRoute(); + assertEquals(CardEmulationRoute.ROUTE_ON_WHEN_SCREEN_ON, newRoute.route); + assertEquals(mEe, newRoute.nfcEe); + } + + public void testDisableEe() { + mAdapterExtras.setCardEmulationRoute( + new CardEmulationRoute(CardEmulationRoute.ROUTE_OFF, null)); + CardEmulationRoute newRoute = mAdapterExtras.getCardEmulationRoute(); + assertEquals(CardEmulationRoute.ROUTE_OFF, newRoute.route); + assertNull(newRoute.nfcEe); + } + + private static void assertByteArrayEquals(byte[] b1, byte[] b2) { + assertEquals(b1.length, b2.length); + for (int i = 0; i < b1.length; i++) { + assertEquals(b1[i], b2[i]); + } + } +} |