diff --git a/Android.mk b/Android.mk index 97d59de498269..1757e62406ec4 100644 --- a/Android.mk +++ b/Android.mk @@ -123,6 +123,7 @@ LOCAL_SRC_FILES += \ core/java/android/nfc/INfcTag.aidl \ core/java/android/nfc/IP2pInitiator.aidl \ core/java/android/nfc/IP2pTarget.aidl \ + core/java/android/nfc/INfcSecureElement.aidl \ core/java/android/os/IHardwareService.aidl \ core/java/android/os/IMessenger.aidl \ core/java/android/os/INetworkManagementService.aidl \ diff --git a/core/java/android/nfc/INfcAdapter.aidl b/core/java/android/nfc/INfcAdapter.aidl index 7743ceb00282b..a663fb84f82d0 100644 --- a/core/java/android/nfc/INfcAdapter.aidl +++ b/core/java/android/nfc/INfcAdapter.aidl @@ -24,6 +24,7 @@ import android.nfc.ILlcpConnectionlessSocket; import android.nfc.INfcTag; import android.nfc.IP2pTarget; import android.nfc.IP2pInitiator; +import android.nfc.INfcSecureElement; /** * @hide @@ -36,6 +37,7 @@ interface INfcAdapter INfcTag getNfcTagInterface(); IP2pTarget getP2pTargetInterface(); IP2pInitiator getP2pInitiatorInterface(); + INfcSecureElement getNfcSecureElementInterface(); // NfcAdapter-class related methods boolean isEnabled(); diff --git a/core/java/android/nfc/INfcSecureElement.aidl b/core/java/android/nfc/INfcSecureElement.aidl new file mode 100755 index 0000000000000..aa98dd26f8f68 --- /dev/null +++ b/core/java/android/nfc/INfcSecureElement.aidl @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2010 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 android.nfc; + +/** + * {@hide} + */ +interface INfcSecureElement { + int openSecureElementConnection(); + int closeSecureElementConnection(int nativeHandle); + byte[] exchangeAPDU(int nativeHandle, in byte[] data); + int[] getSecureElementTechList(int nativeHandle); + byte[] getSecureElementUid(int nativeHandle); +} \ No newline at end of file diff --git a/core/java/android/nfc/NfcAdapter.java b/core/java/android/nfc/NfcAdapter.java index b28cfdaedd32b..a1c22bfe503a8 100644 --- a/core/java/android/nfc/NfcAdapter.java +++ b/core/java/android/nfc/NfcAdapter.java @@ -1,12 +1,17 @@ /* - * Copyright (C) 2010 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. + * Copyright (C) 2010 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 android.nfc; @@ -354,4 +359,17 @@ public final class NfcAdapter { return null; } } + + /** + * Create an Nfc Secure Element Connection + * @hide + */ + public NfcSecureElement createNfcSecureElementConnection() { + try { + return new NfcSecureElement(mService.getNfcSecureElementInterface()); + } catch (RemoteException e) { + Log.e(TAG, "createNfcSecureElementConnection failed", e); + return null; + } + } } diff --git a/core/java/android/nfc/NfcSecureElement.java b/core/java/android/nfc/NfcSecureElement.java new file mode 100755 index 0000000000000..5f4c06699903a --- /dev/null +++ b/core/java/android/nfc/NfcSecureElement.java @@ -0,0 +1,138 @@ +/* + * Copyright (C) 2010 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 android.nfc; + +import android.nfc.technology.TagTechnology; +import android.os.RemoteException; +import android.util.Log; + +import java.io.IOException; + +//import android.util.Log; + +/** + * This class provides the primary API for managing all aspects Secure Element. + * Get an instance of this class by calling + * Context.getSystemService(Context.NFC_SERVICE). + * @hide + */ +public final class NfcSecureElement { + + private static final String TAG = "NfcSecureElement"; + + private INfcSecureElement mService; + + + /** + * @hide + */ + public NfcSecureElement(INfcSecureElement mSecureElementService) { + mService = mSecureElementService; + } + + public int openSecureElementConnection(String seType) throws IOException { + if (seType.equals("SmartMX")) { + try { + int handle = mService.openSecureElementConnection(); + // Handle potential errors + if (handle != 0) { + return handle; + } else { + throw new IOException("SmartMX connection not allowed"); + } + } catch (RemoteException e) { + Log.e(TAG, "RemoteException in openSecureElementConnection(): ", e); + return 0; + } + + } else if (seType.equals("UICC")) { + return 0; + } else { + throw new IOException("Wrong Secure Element type"); + } + } + + + public byte [] exchangeAPDU(int handle,byte [] data) throws IOException { + + + // Perform exchange APDU + try { + byte[] response = mService.exchangeAPDU(handle, data); + // Handle potential errors + if (response == null) { + throw new IOException("Exchange APDU failed"); + } + return response; + } catch (RemoteException e) { + Log.e(TAG, "RemoteException in exchangeAPDU(): ", e); + return null; + } + } + + public void closeSecureElementConnection(int handle) throws IOException { + + try { + int status = mService.closeSecureElementConnection(handle); + // Handle potential errors + if (ErrorCodes.isError(status)) { + throw new IOException("Error during the conection close"); + }; + } catch (RemoteException e) { + Log.e(TAG, "RemoteException in closeSecureElement(): ", e); + } + } + + + /** + * Returns target type. constants. + * + * @return Secure Element technology type. The possible values are defined in + * {@link TagTechnology} + * + */ + public int[] getSecureElementTechList(int handle) throws IOException { + try { + return mService.getSecureElementTechList(handle); + } catch (RemoteException e) { + Log.e(TAG, "RemoteException in getType(): ", e); + return null; + } + } + + /** + * Returns Secure Element UID. + * + * @return Secure Element UID. + */ + public byte[] getSecureElementUid(int handle) throws IOException { + + byte[] uid = null; + try { + uid = mService.getSecureElementUid(handle); + // Handle potential errors + if (uid == null) { + throw new IOException("Get Secure Element UID failed"); + } + return uid; + } catch (RemoteException e) { + Log.e(TAG, "RemoteException in getType(): ", e); + return null; + } + } + +}