diff options
| -rw-r--r-- | core/api/system-current.txt | 6 | ||||
| -rw-r--r-- | core/java/android/nfc/INfcAdapter.aidl | 3 | ||||
| -rw-r--r-- | core/java/android/nfc/INfcControllerAlwaysOnStateCallback.aidl | 29 | ||||
| -rw-r--r-- | core/java/android/nfc/NfcAdapter.java | 90 | ||||
| -rw-r--r-- | core/java/android/nfc/NfcControllerAlwaysOnStateListener.java | 120 | ||||
| -rw-r--r-- | core/res/AndroidManifest.xml | 1 |
6 files changed, 223 insertions, 26 deletions
diff --git a/core/api/system-current.txt b/core/api/system-current.txt index 628c65cba897..59ae926f071f 100644 --- a/core/api/system-current.txt +++ b/core/api/system-current.txt @@ -7929,12 +7929,18 @@ package android.nfc { method @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS) public boolean enableSecureNfc(boolean); method @RequiresPermission(android.Manifest.permission.NFC_SET_CONTROLLER_ALWAYS_ON) public boolean isControllerAlwaysOn(); method @RequiresPermission(android.Manifest.permission.NFC_SET_CONTROLLER_ALWAYS_ON) public boolean isControllerAlwaysOnSupported(); + method @RequiresPermission(android.Manifest.permission.NFC_SET_CONTROLLER_ALWAYS_ON) public void registerControllerAlwaysOnStateCallback(@NonNull java.util.concurrent.Executor, @NonNull android.nfc.NfcAdapter.ControllerAlwaysOnStateCallback); method @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS) public boolean removeNfcUnlockHandler(android.nfc.NfcAdapter.NfcUnlockHandler); method @RequiresPermission(android.Manifest.permission.NFC_SET_CONTROLLER_ALWAYS_ON) public boolean setControllerAlwaysOn(boolean); method public void setNdefPushMessage(android.nfc.NdefMessage, android.app.Activity, int); + method @RequiresPermission(android.Manifest.permission.NFC_SET_CONTROLLER_ALWAYS_ON) public void unregisterControllerAlwaysOnStateCallback(@NonNull android.nfc.NfcAdapter.ControllerAlwaysOnStateCallback); field public static final int FLAG_NDEF_PUSH_NO_CONFIRM = 1; // 0x1 } + public static interface NfcAdapter.ControllerAlwaysOnStateCallback { + method public void onStateChanged(boolean); + } + public static interface NfcAdapter.NfcUnlockHandler { method public boolean onUnlockAttempted(android.nfc.Tag); } diff --git a/core/java/android/nfc/INfcAdapter.aidl b/core/java/android/nfc/INfcAdapter.aidl index 11445e9aec5a..d5cc01aac0cc 100644 --- a/core/java/android/nfc/INfcAdapter.aidl +++ b/core/java/android/nfc/INfcAdapter.aidl @@ -24,6 +24,7 @@ import android.nfc.Tag; import android.nfc.TechListParcel; import android.nfc.IAppCallback; import android.nfc.INfcAdapterExtras; +import android.nfc.INfcControllerAlwaysOnStateCallback; import android.nfc.INfcTag; import android.nfc.INfcCardEmulation; import android.nfc.INfcFCardEmulation; @@ -75,4 +76,6 @@ interface INfcAdapter boolean setControllerAlwaysOn(boolean value); boolean isControllerAlwaysOn(); boolean isControllerAlwaysOnSupported(); + void registerControllerAlwaysOnStateCallback(in INfcControllerAlwaysOnStateCallback callback); + void unregisterControllerAlwaysOnStateCallback(in INfcControllerAlwaysOnStateCallback callback); } diff --git a/core/java/android/nfc/INfcControllerAlwaysOnStateCallback.aidl b/core/java/android/nfc/INfcControllerAlwaysOnStateCallback.aidl new file mode 100644 index 000000000000..1e4fdd79e831 --- /dev/null +++ b/core/java/android/nfc/INfcControllerAlwaysOnStateCallback.aidl @@ -0,0 +1,29 @@ +/* + * Copyright 2021 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 INfcControllerAlwaysOnStateCallback { + /** + * Called whenever the controller always on state changes + * + * @param isEnabled true if the state is enabled, false otherwise + */ + void onControllerAlwaysOnStateChanged(boolean isEnabled); +} diff --git a/core/java/android/nfc/NfcAdapter.java b/core/java/android/nfc/NfcAdapter.java index eed2c77b06b9..bbf802ca58d8 100644 --- a/core/java/android/nfc/NfcAdapter.java +++ b/core/java/android/nfc/NfcAdapter.java @@ -16,6 +16,7 @@ package android.nfc; +import android.annotation.CallbackExecutor; import android.annotation.NonNull; import android.annotation.RequiresPermission; import android.annotation.SdkConstant; @@ -47,6 +48,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.concurrent.Executor; /** * Represents the local NFC adapter. @@ -65,6 +67,8 @@ import java.util.List; public final class NfcAdapter { static final String TAG = "NFC"; + private final NfcControllerAlwaysOnStateListener mControllerAlwaysOnStateListener; + /** * Intent to start an activity when a tag with NDEF payload is discovered. * @@ -350,22 +354,6 @@ public final class NfcAdapter { "android.nfc.extra.HANDOVER_TRANSFER_STATUS"; /** @hide */ - public static final String ACTION_ALWAYS_ON_STATE_CHANGED = - "android.nfc.action.ALWAYS_ON_STATE_CHANGED"; - - /** - * Used as an int extra field in {@link #ACTION_ALWAYS_ON_STATE_CHANGED} - * intents to request the current power state. Possible values are: - * {@link #STATE_OFF}, - * {@link #STATE_TURNING_ON}, - * {@link #STATE_ON}, - * {@link #STATE_TURNING_OFF}, - * @hide - */ - public static final String EXTRA_ALWAYS_ON_STATE = - "android.nfc.extra.ALWAYS_ON_STATE"; - - /** @hide */ public static final int HANDOVER_TRANSFER_STATUS_SUCCESS = 0; /** @hide */ public static final int HANDOVER_TRANSFER_STATUS_FAILURE = 1; @@ -430,6 +418,22 @@ public final class NfcAdapter { } /** + * A callback to be invoked when NFC controller always on state changes. + * <p>Register your {@code ControllerAlwaysOnStateCallback} implementation with {@link + * NfcAdapter#registerControllerAlwaysOnStateCallback} and disable it with {@link + * NfcAdapter#unregisterControllerAlwaysOnStateCallback}. + * @see #registerControllerAlwaysOnStateCallback + * @hide + */ + @SystemApi + public interface ControllerAlwaysOnStateCallback { + /** + * Called on NFC controller always on state changes + */ + void onStateChanged(boolean isEnabled); + } + + /** * A callback to be invoked when the system successfully delivers your {@link NdefMessage} * to another device. * @see #setOnNdefPushCompleteCallback @@ -744,6 +748,7 @@ public final class NfcAdapter { mNfcUnlockHandlers = new HashMap<NfcUnlockHandler, INfcUnlockHandler>(); mTagRemovedListener = null; mLock = new Object(); + mControllerAlwaysOnStateListener = new NfcControllerAlwaysOnStateListener(getService()); } /** @@ -2239,14 +2244,16 @@ public final class NfcAdapter { /** * Sets NFC controller always on feature. * <p>This API is for the NFCC internal state management. It allows to discriminate - * the controller function from the NFC function by keeping the NFC Controller on without + * the controller function from the NFC function by keeping the NFC controller on without * any NFC RF enabled if necessary. - * <p>This call is asynchronous. Listen for {@link #ACTION_ALWAYS_ON_STATE_CHANGED} - * broadcasts to find out when the operation is complete. - * <p>If this returns true, then either NFCC is already on, or - * a {@link #ACTION_ALWAYS_ON_STATE_CHANGED} broadcast will be sent to indicate - * a state transition. - * If this returns false, then there is some problem that prevents an attempt to turn NFCC on. + * <p>This call is asynchronous. Register a callback {@link #ControllerAlwaysOnStateCallback} + * by {@link #registerControllerAlwaysOnStateCallback} to find out when the operation is + * complete. + * <p>If this returns true, then either NFCC always on state has been set based on the value, + * or a {@link ControllerAlwaysOnStateCallback#onStateChanged(boolean)} will be invoked to + * indicate the state change. + * If this returns false, then there is some problem that prevents an attempt to turn NFCC + * always on. * @param value if true the NFCC will be kept on (with no RF enabled if NFC adapter is * disabled), if false the NFCC will follow completely the Nfc adapter state. * @throws UnsupportedOperationException if FEATURE_NFC is unavailable. @@ -2284,7 +2291,6 @@ public final class NfcAdapter { * @throws UnsupportedOperationException if FEATURE_NFC is unavailable. * @hide */ - @SystemApi @RequiresPermission(android.Manifest.permission.NFC_SET_CONTROLLER_ALWAYS_ON) public boolean isControllerAlwaysOn() { @@ -2313,7 +2319,6 @@ public final class NfcAdapter { * @throws UnsupportedOperationException if FEATURE_NFC is unavailable. * @hide */ - @SystemApi @RequiresPermission(android.Manifest.permission.NFC_SET_CONTROLLER_ALWAYS_ON) public boolean isControllerAlwaysOnSupported() { @@ -2337,4 +2342,39 @@ public final class NfcAdapter { return false; } } + + /** + * Register a {@link ControllerAlwaysOnStateCallback} to listen for NFC controller always on + * state changes + * <p>The provided callback will be invoked by the given {@link Executor}. + * + * @param executor an {@link Executor} to execute given callback + * @param callback user implementation of the {@link ControllerAlwaysOnStateCallback} + * @hide + */ + @SystemApi + @RequiresPermission(android.Manifest.permission.NFC_SET_CONTROLLER_ALWAYS_ON) + public void registerControllerAlwaysOnStateCallback( + @NonNull @CallbackExecutor Executor executor, + @NonNull ControllerAlwaysOnStateCallback callback) { + mControllerAlwaysOnStateListener.register(executor, callback); + } + + /** + * Unregister the specified {@link ControllerAlwaysOnStateCallback} + * <p>The same {@link ControllerAlwaysOnStateCallback} object used when calling + * {@link #registerControllerAlwaysOnStateCallback(Executor, ControllerAlwaysOnStateCallback)} + * must be used. + * + * <p>Callbacks are automatically unregistered when application process goes away + * + * @param callback user implementation of the {@link ControllerAlwaysOnStateCallback} + * @hide + */ + @SystemApi + @RequiresPermission(android.Manifest.permission.NFC_SET_CONTROLLER_ALWAYS_ON) + public void unregisterControllerAlwaysOnStateCallback( + @NonNull ControllerAlwaysOnStateCallback callback) { + mControllerAlwaysOnStateListener.unregister(callback); + } } diff --git a/core/java/android/nfc/NfcControllerAlwaysOnStateListener.java b/core/java/android/nfc/NfcControllerAlwaysOnStateListener.java new file mode 100644 index 000000000000..69a9ec79edb2 --- /dev/null +++ b/core/java/android/nfc/NfcControllerAlwaysOnStateListener.java @@ -0,0 +1,120 @@ +/* + * Copyright 2021 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.ControllerAlwaysOnStateCallback; +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 class NfcControllerAlwaysOnStateListener extends INfcControllerAlwaysOnStateCallback.Stub { + private static final String TAG = "NfcControllerAlwaysOnStateListener"; + + private final INfcAdapter mAdapter; + + private final Map<ControllerAlwaysOnStateCallback, Executor> mCallbackMap = new HashMap<>(); + + private boolean mCurrentState = false; + private boolean mIsRegistered = false; + + public NfcControllerAlwaysOnStateListener(@NonNull INfcAdapter adapter) { + mAdapter = adapter; + } + + /** + * Register a {@link ControllerAlwaysOnStateCallback} with this + * {@link NfcControllerAlwaysOnStateListener} + * + * @param executor an {@link Executor} to execute given callback + * @param callback user implementation of the {@link ControllerAlwaysOnStateCallback} + */ + public void register(@NonNull Executor executor, + @NonNull ControllerAlwaysOnStateCallback callback) { + synchronized (this) { + if (mCallbackMap.containsKey(callback)) { + return; + } + + mCallbackMap.put(callback, executor); + if (!mIsRegistered) { + try { + mAdapter.registerControllerAlwaysOnStateCallback(this); + mIsRegistered = true; + } catch (RemoteException e) { + Log.w(TAG, "Failed to register ControllerAlwaysOnStateListener"); + } + } + } + } + + /** + * Unregister the specified {@link ControllerAlwaysOnStateCallback} + * + * @param callback user implementation of the {@link ControllerAlwaysOnStateCallback} + */ + public void unregister(@NonNull ControllerAlwaysOnStateCallback callback) { + synchronized (this) { + if (!mCallbackMap.containsKey(callback)) { + return; + } + + mCallbackMap.remove(callback); + + if (mCallbackMap.isEmpty() && mIsRegistered) { + try { + mAdapter.unregisterControllerAlwaysOnStateCallback(this); + } catch (RemoteException e) { + Log.w(TAG, "Failed to unregister ControllerAlwaysOnStateListener"); + } + mIsRegistered = false; + } + } + } + + private void sendCurrentState(@NonNull ControllerAlwaysOnStateCallback callback) { + synchronized (this) { + Executor executor = mCallbackMap.get(callback); + + final long identity = Binder.clearCallingIdentity(); + try { + executor.execute(() -> callback.onStateChanged( + mCurrentState)); + } finally { + Binder.restoreCallingIdentity(identity); + } + } + } + + @Override + public void onControllerAlwaysOnStateChanged(boolean isEnabled) { + synchronized (this) { + mCurrentState = isEnabled; + for (ControllerAlwaysOnStateCallback cb : mCallbackMap.keySet()) { + sendCurrentState(cb); + } + } + } +} + diff --git a/core/res/AndroidManifest.xml b/core/res/AndroidManifest.xml index 7641ec3151ab..46f2f3e9286d 100644 --- a/core/res/AndroidManifest.xml +++ b/core/res/AndroidManifest.xml @@ -326,7 +326,6 @@ <protected-broadcast android:name="android.se.omapi.action.SECURE_ELEMENT_STATE_CHANGED" /> <protected-broadcast android:name="android.nfc.action.ADAPTER_STATE_CHANGED" /> - <protected-broadcast android:name="android.nfc.action.ALWAYS_ON_STATE_CHANGED" /> <protected-broadcast android:name="android.nfc.action.PREFERRED_PAYMENT_CHANGED" /> <protected-broadcast android:name="android.nfc.action.TRANSACTION_DETECTED" /> <protected-broadcast android:name="android.nfc.action.REQUIRE_UNLOCK_FOR_NFC" /> |