From 3454b0bdee25a5f85b7baf6917e39f645c01d291 Mon Sep 17 00:00:00 2001 From: Patrick Rohr Date: Tue, 1 Jul 2025 09:59:08 -0700 Subject: [PATCH] Add hidden flags parameter to addInterfaceStateListener And pass it through to EthernetListener. This will be used to optionally include callbacks for NCM interfaces which are not tracked by the regex in tests. Test: TH Flag: EXEMPT mainline Change-Id: If388a37911d3b8ceb2be79f8d303f37560b2678b --- .../src/android/net/EthernetManager.java | 39 +++++++++++++++++-- .../src/android/net/IEthernetManager.aidl | 2 +- .../server/ethernet/EthernetListener.java | 5 ++- .../server/ethernet/EthernetServiceImpl.java | 9 +++-- 4 files changed, 45 insertions(+), 10 deletions(-) diff --git a/framework-t/src/android/net/EthernetManager.java b/framework-t/src/android/net/EthernetManager.java index 719f60d9cb..2f0612bd1b 100644 --- a/framework-t/src/android/net/EthernetManager.java +++ b/framework-t/src/android/net/EthernetManager.java @@ -281,6 +281,36 @@ public class EthernetManager { @SystemApi(client = MODULE_LIBRARIES) public void addInterfaceStateListener(@NonNull Executor executor, @NonNull InterfaceStateListener listener) { + addInterfaceStateListener(executor, listener, LISTENER_FLAG_DEFAULT); + } + + + /** @hide */ + public static final int LISTENER_FLAG_DEFAULT = 0; + + /** + * Include callbacks for NCM interfaces which are not tracked by the regex. + * @hide + */ + public static final int LISTENER_FLAG_INCLUDE_NCM = 1 << 0; + + /** @hide */ + @Retention(RetentionPolicy.SOURCE) + @IntDef(prefix = { "LISTENER_FLAG_" }, value = { + LISTENER_FLAG_DEFAULT, + LISTENER_FLAG_INCLUDE_NCM + }) + public @interface ListenerFlag {} + + /** + * Listen to ethernet interface state changes + * + * Version of {@link #addInterfaceStateListener} that accepts flags for testing. + * @hide + */ + @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) + public void addInterfaceStateListener(@NonNull Executor executor, + @NonNull InterfaceStateListener listener, @ListenerFlag int flags) { if (listener == null || executor == null) { throw new NullPointerException("listener and executor must not be null"); } @@ -297,15 +327,16 @@ public class EthernetManager { } }; synchronized (mListenerLock) { - addServiceListener(serviceListener); + addServiceListener(serviceListener, flags); mIfaceServiceListeners.put(listener, serviceListener); } } @GuardedBy("mListenerLock") - private void addServiceListener(@NonNull final IEthernetServiceListener listener) { + private void addServiceListener(@NonNull final IEthernetServiceListener listener, + @ListenerFlag int flags) { try { - mService.addListener(listener); + mService.addListener(listener, flags); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } @@ -673,7 +704,7 @@ public class EthernetManager { IpConfiguration configuration) {} }; synchronized (mListenerLock) { - addServiceListener(serviceListener); + addServiceListener(serviceListener, LISTENER_FLAG_DEFAULT); mStateServiceListeners.put(listener, serviceListener); } } diff --git a/framework-t/src/android/net/IEthernetManager.aidl b/framework-t/src/android/net/IEthernetManager.aidl index c1efc29d25..ea45a9de7c 100644 --- a/framework-t/src/android/net/IEthernetManager.aidl +++ b/framework-t/src/android/net/IEthernetManager.aidl @@ -36,7 +36,7 @@ interface IEthernetManager IpConfiguration getConfiguration(String iface); void setConfiguration(String iface, in IpConfiguration config); boolean isAvailable(String iface); - void addListener(in IEthernetServiceListener listener); + void addListener(in IEthernetServiceListener listener, int flags); void removeListener(in IEthernetServiceListener listener); void setIncludeTestInterfaces(boolean include); void requestTetheredInterface(in ITetheredInterfaceCallback callback); diff --git a/service-t/src/com/android/server/ethernet/EthernetListener.java b/service-t/src/com/android/server/ethernet/EthernetListener.java index b9704492fd..79b8122d9f 100644 --- a/service-t/src/com/android/server/ethernet/EthernetListener.java +++ b/service-t/src/com/android/server/ethernet/EthernetListener.java @@ -26,10 +26,13 @@ import android.os.RemoteException; public class EthernetListener implements IInterface { private final IEthernetServiceListener mListener; private final boolean mHasUseRestrictedNetworksPermission; + private final int mFlags; - public EthernetListener(IEthernetServiceListener listener, boolean canUseRestrictedNetworks) { + public EthernetListener(IEthernetServiceListener listener, boolean canUseRestrictedNetworks, + int flags) { mListener = listener; mHasUseRestrictedNetworksPermission = canUseRestrictedNetworks; + mFlags = flags; } /** Indicates whether the remote uid has USE_RESTRICTED_NETWORKS permission */ diff --git a/service-t/src/com/android/server/ethernet/EthernetServiceImpl.java b/service-t/src/com/android/server/ethernet/EthernetServiceImpl.java index 30b644876f..c1b808190a 100644 --- a/service-t/src/com/android/server/ethernet/EthernetServiceImpl.java +++ b/service-t/src/com/android/server/ethernet/EthernetServiceImpl.java @@ -24,6 +24,7 @@ import android.annotation.NonNull; import android.annotation.Nullable; import android.content.Context; import android.content.pm.PackageManager; +import android.net.EthernetManager; import android.net.EthernetNetworkSpecifier; import android.net.EthernetNetworkUpdateRequest; import android.net.IEthernetManager; @@ -166,7 +167,7 @@ public class EthernetServiceImpl extends IEthernetManager.Stub { * @param listener A {@link IEthernetServiceListener} to add. */ @Override - public void addListener(IEthernetServiceListener listener) throws RemoteException { + public void addListener(IEthernetServiceListener listener, int flags) throws RemoteException { Objects.requireNonNull(listener, "listener must not be null"); PermissionUtils.enforceAccessNetworkStatePermission(mContext, TAG); final boolean hasUseRestrictedNetworksPermission = hasUseRestrictedNetworksPermission(); @@ -174,7 +175,7 @@ public class EthernetServiceImpl extends IEthernetManager.Stub { final long ident = Binder.clearCallingIdentity(); try { final EthernetListener wrappedListener = - new EthernetListener(listener, hasUseRestrictedNetworksPermission); + new EthernetListener(listener, hasUseRestrictedNetworksPermission, flags); mTracker.addListener(wrappedListener); } finally { Binder.restoreCallingIdentity(ident); @@ -198,8 +199,8 @@ public class EthernetServiceImpl extends IEthernetManager.Stub { // extends IInterface> instead of just IInterface. // Note that hasUseRestrictedNetworksPermission is irrelevant in // this context and is only populated for the sake of completeness. - final EthernetListener wrappedListener = - new EthernetListener(listener, hasUseRestrictedNetworksPermission); + final EthernetListener wrappedListener = new EthernetListener(listener, + hasUseRestrictedNetworksPermission, EthernetManager.LISTENER_FLAG_DEFAULT); mTracker.removeListener(wrappedListener); } finally { Binder.restoreCallingIdentity(ident);