diff options
| -rw-r--r-- | nfc/api/system-current.txt | 13 | ||||
| -rw-r--r-- | nfc/java/android/nfc/INfcAdapter.aidl | 4 | ||||
| -rw-r--r-- | nfc/java/android/nfc/INfcVendorNciCallback.aidl | 24 | ||||
| -rw-r--r-- | nfc/java/android/nfc/NfcAdapter.java | 162 | ||||
| -rw-r--r-- | nfc/java/android/nfc/NfcVendorNciCallbackListener.java | 115 | ||||
| -rw-r--r-- | nfc/java/android/nfc/flags.aconfig | 7 |
6 files changed, 325 insertions, 0 deletions
diff --git a/nfc/api/system-current.txt b/nfc/api/system-current.txt index f8b640c7d5b3..768013644b12 100644 --- a/nfc/api/system-current.txt +++ b/nfc/api/system-current.txt @@ -14,15 +14,23 @@ package android.nfc { method @RequiresPermission(android.Manifest.permission.NFC_SET_CONTROLLER_ALWAYS_ON) public boolean isControllerAlwaysOnSupported(); method @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS) public boolean isTagIntentAppPreferenceSupported(); method @RequiresPermission(android.Manifest.permission.NFC_SET_CONTROLLER_ALWAYS_ON) public void registerControllerAlwaysOnListener(@NonNull java.util.concurrent.Executor, @NonNull android.nfc.NfcAdapter.ControllerAlwaysOnListener); + method @FlaggedApi("android.nfc.nfc_vendor_cmd") @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS) public void registerNfcVendorNciCallback(@NonNull java.util.concurrent.Executor, @NonNull android.nfc.NfcAdapter.NfcVendorNciCallback); method @FlaggedApi("android.nfc.enable_nfc_charging") public void registerWlcStateListener(@NonNull java.util.concurrent.Executor, @NonNull android.nfc.NfcAdapter.WlcStateListener); method @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS) public boolean removeNfcUnlockHandler(android.nfc.NfcAdapter.NfcUnlockHandler); + method @FlaggedApi("android.nfc.nfc_vendor_cmd") @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS) public int sendVendorNciMessage(int, @IntRange(from=0, to=15) int, @IntRange(from=0) int, @NonNull byte[]); method @RequiresPermission(android.Manifest.permission.NFC_SET_CONTROLLER_ALWAYS_ON) public boolean setControllerAlwaysOn(boolean); method @FlaggedApi("android.nfc.enable_nfc_mainline") @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS) public void setReaderModePollingEnabled(boolean); method @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS) public int setTagIntentAppPreferenceForUser(int, @NonNull String, boolean); method @FlaggedApi("android.nfc.enable_nfc_charging") @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS) public boolean setWlcEnabled(boolean); method @RequiresPermission(android.Manifest.permission.NFC_SET_CONTROLLER_ALWAYS_ON) public void unregisterControllerAlwaysOnListener(@NonNull android.nfc.NfcAdapter.ControllerAlwaysOnListener); + method @FlaggedApi("android.nfc.enable_nfc_mainline") @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS) public void unregisterNfcVendorNciCallback(@NonNull android.nfc.NfcAdapter.NfcVendorNciCallback); method @FlaggedApi("android.nfc.enable_nfc_charging") public void unregisterWlcStateListener(@NonNull android.nfc.NfcAdapter.WlcStateListener); field @FlaggedApi("android.nfc.enable_nfc_mainline") public static final String ACTION_REQUIRE_UNLOCK_FOR_NFC = "android.nfc.action.REQUIRE_UNLOCK_FOR_NFC"; + field @FlaggedApi("android.nfc.nfc_vendor_cmd") public static final int MESSAGE_TYPE_COMMAND = 1; // 0x1 + field @FlaggedApi("android.nfc.nfc_vendor_cmd") public static final int SEND_VENDOR_NCI_STATUS_FAILED = 3; // 0x3 + field @FlaggedApi("android.nfc.nfc_vendor_cmd") public static final int SEND_VENDOR_NCI_STATUS_MESSAGE_CORRUPTED = 2; // 0x2 + field @FlaggedApi("android.nfc.nfc_vendor_cmd") public static final int SEND_VENDOR_NCI_STATUS_REJECTED = 1; // 0x1 + field @FlaggedApi("android.nfc.nfc_vendor_cmd") public static final int SEND_VENDOR_NCI_STATUS_SUCCESS = 0; // 0x0 field public static final int TAG_INTENT_APP_PREF_RESULT_PACKAGE_NOT_FOUND = -1; // 0xffffffff field public static final int TAG_INTENT_APP_PREF_RESULT_SUCCESS = 0; // 0x0 field public static final int TAG_INTENT_APP_PREF_RESULT_UNAVAILABLE = -2; // 0xfffffffe @@ -36,6 +44,11 @@ package android.nfc { method public boolean onUnlockAttempted(android.nfc.Tag); } + @FlaggedApi("android.nfc.nfc_vendor_cmd") public static interface NfcAdapter.NfcVendorNciCallback { + method @FlaggedApi("android.nfc.nfc_vendor_cmd") public void onVendorNciNotification(@IntRange(from=9, to=15) int, int, @NonNull byte[]); + method @FlaggedApi("android.nfc.nfc_vendor_cmd") public void onVendorNciResponse(@IntRange(from=0, to=15) int, int, @NonNull byte[]); + } + @FlaggedApi("android.nfc.enable_nfc_charging") public static interface NfcAdapter.WlcStateListener { method public void onWlcStateChanged(@NonNull android.nfc.WlcListenerDeviceInfo); } diff --git a/nfc/java/android/nfc/INfcAdapter.aidl b/nfc/java/android/nfc/INfcAdapter.aidl index 63c3414acc36..293e5d19e77c 100644 --- a/nfc/java/android/nfc/INfcAdapter.aidl +++ b/nfc/java/android/nfc/INfcAdapter.aidl @@ -24,6 +24,7 @@ import android.nfc.TechListParcel; import android.nfc.IAppCallback; import android.nfc.INfcAdapterExtras; import android.nfc.INfcControllerAlwaysOnListener; +import android.nfc.INfcVendorNciCallback; import android.nfc.INfcTag; import android.nfc.INfcCardEmulation; import android.nfc.INfcFCardEmulation; @@ -100,4 +101,7 @@ interface INfcAdapter void notifyPollingLoop(in Bundle frame); void notifyHceDeactivated(); + int sendVendorNciMessage(int mt, int gid, int oid, in byte[] payload); + void registerVendorExtensionCallback(in INfcVendorNciCallback callbacks); + void unregisterVendorExtensionCallback(in INfcVendorNciCallback callbacks); } diff --git a/nfc/java/android/nfc/INfcVendorNciCallback.aidl b/nfc/java/android/nfc/INfcVendorNciCallback.aidl new file mode 100644 index 000000000000..821dc6f6c868 --- /dev/null +++ b/nfc/java/android/nfc/INfcVendorNciCallback.aidl @@ -0,0 +1,24 @@ +/* + * Copyright (C) 2024 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 + */ +oneway interface INfcVendorNciCallback { + void onVendorResponseReceived(int gid, int oid, in byte[] payload); + void onVendorNotificationReceived(int gid, int oid, in byte[] payload); +} diff --git a/nfc/java/android/nfc/NfcAdapter.java b/nfc/java/android/nfc/NfcAdapter.java index 5b917a1a6dcf..3d749801cb0a 100644 --- a/nfc/java/android/nfc/NfcAdapter.java +++ b/nfc/java/android/nfc/NfcAdapter.java @@ -19,6 +19,7 @@ package android.nfc; import android.annotation.CallbackExecutor; import android.annotation.FlaggedApi; import android.annotation.IntDef; +import android.annotation.IntRange; import android.annotation.NonNull; import android.annotation.Nullable; import android.annotation.RequiresPermission; @@ -77,6 +78,7 @@ public final class NfcAdapter { private final NfcControllerAlwaysOnListener mControllerAlwaysOnListener; private final NfcWlcStateListener mNfcWlcStateListener; + private final NfcVendorNciCallbackListener mNfcVendorNciCallbackListener; /** * Intent to start an activity when a tag with NDEF payload is discovered. @@ -866,6 +868,7 @@ public final class NfcAdapter { mLock = new Object(); mControllerAlwaysOnListener = new NfcControllerAlwaysOnListener(getService()); mNfcWlcStateListener = new NfcWlcStateListener(getService()); + mNfcVendorNciCallbackListener = new NfcVendorNciCallbackListener(getService()); } /** @@ -2962,4 +2965,163 @@ public final class NfcAdapter { return null; } } + + /** + * Vendor NCI command success. + * @hide + */ + @SystemApi + @FlaggedApi(Flags.FLAG_NFC_VENDOR_CMD) + public static final int SEND_VENDOR_NCI_STATUS_SUCCESS = 0; + /** + * Vendor NCI command rejected. + * @hide + */ + @SystemApi + @FlaggedApi(Flags.FLAG_NFC_VENDOR_CMD) + public static final int SEND_VENDOR_NCI_STATUS_REJECTED = 1; + /** + * Vendor NCI command corrupted. + * @hide + */ + @SystemApi + @FlaggedApi(Flags.FLAG_NFC_VENDOR_CMD) + public static final int SEND_VENDOR_NCI_STATUS_MESSAGE_CORRUPTED = 2; + /** + * Vendor NCI command failed with unknown reason. + * @hide + */ + @SystemApi + @FlaggedApi(Flags.FLAG_NFC_VENDOR_CMD) + public static final int SEND_VENDOR_NCI_STATUS_FAILED = 3; + + /** + * @hide + */ + @Retention(RetentionPolicy.SOURCE) + @IntDef(value = { + SEND_VENDOR_NCI_STATUS_SUCCESS, + SEND_VENDOR_NCI_STATUS_REJECTED, + SEND_VENDOR_NCI_STATUS_MESSAGE_CORRUPTED, + SEND_VENDOR_NCI_STATUS_FAILED, + }) + @interface SendVendorNciStatus {} + + /** + * Message Type for NCI Command. + * @hide + */ + @SystemApi + @FlaggedApi(Flags.FLAG_NFC_VENDOR_CMD) + public static final int MESSAGE_TYPE_COMMAND = 1; + + /** + * @hide + */ + @Retention(RetentionPolicy.SOURCE) + @IntDef(value = { + MESSAGE_TYPE_COMMAND, + }) + @interface MessageType {} + + /** + * Send Vendor specific Nci Messages with custom message type. + * + * The format of the NCI messages are defined in the NCI specification. The platform is + * responsible for fragmenting the payload if necessary. + * + * Note that mt (message type) is added at the beginning of method parameters as it is more + * distinctive than other parameters and was requested from vendor. + * + * @param mt message Type of the command + * @param gid group ID of the command. This needs to be one of the vendor reserved GIDs from + * the NCI specification + * @param oid opcode ID of the command. This is left to the OEM / vendor to decide + * @param payload containing vendor Nci message payload + * @return message send status + * @hide + */ + @SystemApi + @FlaggedApi(Flags.FLAG_NFC_VENDOR_CMD) + @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS) + public @SendVendorNciStatus int sendVendorNciMessage(@MessageType int mt, + @IntRange(from = 0, to = 15) int gid, @IntRange(from = 0) int oid, + @NonNull byte[] payload) { + Objects.requireNonNull(payload, "Payload must not be null"); + try { + return sService.sendVendorNciMessage(mt, gid, oid, payload); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } + } + + /** + * Register an {@link NfcVendorNciCallback} to listen for Nfc vendor responses and notifications + * <p>The provided callback will be invoked by the given {@link Executor}. + * + * <p>When first registering a callback, the callbacks's + * {@link NfcVendorNciCallback#onVendorNciCallBack(byte[])} is immediately invoked to + * notify the vendor notification. + * + * @param executor an {@link Executor} to execute given callback + * @param callback user implementation of the {@link NfcVendorNciCallback} + * @hide + */ + @SystemApi + @FlaggedApi(Flags.FLAG_NFC_VENDOR_CMD) + @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS) + public void registerNfcVendorNciCallback(@NonNull @CallbackExecutor Executor executor, + @NonNull NfcVendorNciCallback callback) { + mNfcVendorNciCallbackListener.register(executor, callback); + } + + /** + * Unregister the specified {@link NfcVendorNciCallback} + * + * <p>The same {@link NfcVendorNciCallback} object used when calling + * {@link #registerNfcVendorNciCallback(Executor, NfcVendorNciCallback)} must be used. + * + * <p>Callbacks are automatically unregistered when application process goes away + * + * @param callback user implementation of the {@link NfcVendorNciCallback} + * @hide + */ + @SystemApi + @FlaggedApi(Flags.FLAG_ENABLE_NFC_MAINLINE) + @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS) + public void unregisterNfcVendorNciCallback(@NonNull NfcVendorNciCallback callback) { + mNfcVendorNciCallbackListener.unregister(callback); + } + + /** + * Interface for receiving vendor NCI responses and notifications. + * @hide + */ + @SystemApi + @FlaggedApi(Flags.FLAG_NFC_VENDOR_CMD) + public interface NfcVendorNciCallback { + /** + * Invoked when a vendor specific NCI response is received. + * + * @param gid group ID of the command. This needs to be one of the vendor reserved GIDs from + * the NCI specification. + * @param oid opcode ID of the command. This is left to the OEM / vendor to decide. + * @param payload containing vendor Nci message payload. + */ + @FlaggedApi(Flags.FLAG_NFC_VENDOR_CMD) + void onVendorNciResponse( + @IntRange(from = 0, to = 15) int gid, int oid, @NonNull byte[] payload); + + /** + * Invoked when a vendor specific NCI notification is received. + * + * @param gid group ID of the command. This needs to be one of the vendor reserved GIDs from + * the NCI specification. + * @param oid opcode ID of the command. This is left to the OEM / vendor to decide. + * @param payload containing vendor Nci message payload. + */ + @FlaggedApi(Flags.FLAG_NFC_VENDOR_CMD) + void onVendorNciNotification( + @IntRange(from = 9, to = 15) int gid, int oid, @NonNull byte[] payload); + } } diff --git a/nfc/java/android/nfc/NfcVendorNciCallbackListener.java b/nfc/java/android/nfc/NfcVendorNciCallbackListener.java new file mode 100644 index 000000000000..742d75fe4bc3 --- /dev/null +++ b/nfc/java/android/nfc/NfcVendorNciCallbackListener.java @@ -0,0 +1,115 @@ +/* + * Copyright (C) 2024 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.annotation.NonNull; +import android.nfc.NfcAdapter.NfcVendorNciCallback; +import android.os.Binder; +import android.os.RemoteException; +import android.util.Log; + +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.Executor; + +/** + * @hide + */ +public final class NfcVendorNciCallbackListener extends INfcVendorNciCallback.Stub { + private static final String TAG = "Nfc.NfcVendorNciCallbacks"; + private final INfcAdapter mAdapter; + private boolean mIsRegistered = false; + private final Map<NfcVendorNciCallback, Executor> mCallbackMap = new HashMap<>(); + + public NfcVendorNciCallbackListener(@NonNull INfcAdapter adapter) { + mAdapter = adapter; + } + + public void register(@NonNull Executor executor, @NonNull NfcVendorNciCallback callback) { + synchronized (this) { + if (mCallbackMap.containsKey(callback)) { + return; + } + mCallbackMap.put(callback, executor); + if (!mIsRegistered) { + try { + mAdapter.registerVendorExtensionCallback(this); + mIsRegistered = true; + } catch (RemoteException e) { + Log.w(TAG, "Failed to register adapter state callback"); + mCallbackMap.remove(callback); + throw e.rethrowFromSystemServer(); + } + } + } + } + + public void unregister(@NonNull NfcVendorNciCallback callback) { + synchronized (this) { + if (!mCallbackMap.containsKey(callback) || !mIsRegistered) { + return; + } + if (mCallbackMap.size() == 1) { + try { + mAdapter.unregisterVendorExtensionCallback(this); + mIsRegistered = false; + mCallbackMap.remove(callback); + } catch (RemoteException e) { + Log.w(TAG, "Failed to unregister AdapterStateCallback with service"); + throw e.rethrowFromSystemServer(); + } + } else { + mCallbackMap.remove(callback); + } + } + } + + @Override + public void onVendorResponseReceived(int gid, int oid, @NonNull byte[] payload) + throws RemoteException { + synchronized (this) { + final long identity = Binder.clearCallingIdentity(); + try { + for (NfcVendorNciCallback callback : mCallbackMap.keySet()) { + Executor executor = mCallbackMap.get(callback); + executor.execute(() -> callback.onVendorNciResponse(gid, oid, payload)); + } + } catch (RuntimeException ex) { + throw ex; + } finally { + Binder.restoreCallingIdentity(identity); + } + } + } + + @Override + public void onVendorNotificationReceived(int gid, int oid, @NonNull byte[] payload) + throws RemoteException { + synchronized (this) { + final long identity = Binder.clearCallingIdentity(); + try { + for (NfcVendorNciCallback callback : mCallbackMap.keySet()) { + Executor executor = mCallbackMap.get(callback); + executor.execute(() -> callback.onVendorNciNotification(gid, oid, payload)); + } + } catch (RuntimeException ex) { + throw ex; + } finally { + Binder.restoreCallingIdentity(identity); + } + } + } +} diff --git a/nfc/java/android/nfc/flags.aconfig b/nfc/java/android/nfc/flags.aconfig index 01a45708fddf..0a2619c8a19a 100644 --- a/nfc/java/android/nfc/flags.aconfig +++ b/nfc/java/android/nfc/flags.aconfig @@ -69,3 +69,10 @@ flag { description: "Flag for NFC set discovery tech API" bug: "300351519" } + +flag { + name: "nfc_vendor_cmd" + namespace: "nfc" + description: "Enable NFC vendor command support" + bug: "289879306" +} |