Refactor SocketNetlinkMonitor to make it buildable with system sdk
NetlinkMonitor cannot be used when building with system sdk. This CL
refactored the SocketNetlinkMonitor into a separate class and use
factory method and droidstub to avoid referring NetlinkMonitor when
building with system sdk.
Test: m service-connectivity-mdns-standalone-build-test
Bug: 272392042
Change-Id: I2d97f6fe8a17febd155c81303d86cd558654ddf5
diff --git a/service-t/Android.bp b/service-t/Android.bp
index 5bf2973..7de749c 100644
--- a/service-t/Android.bp
+++ b/service-t/Android.bp
@@ -75,3 +75,47 @@
"//packages/modules/IPsec/tests/iketests",
],
}
+
+// Test building mDNS as a standalone, so that it can be imported into other repositories as-is.
+// The mDNS code is platform code so it should use framework-annotations-lib, contrary to apps that
+// should use sdk_version: "system_current" and only androidx.annotation_annotation. But this build
+// rule verifies that the mDNS code can be built into apps, if code transformations are applied to
+// the annotations.
+// When using "system_current", framework annotations are not available; they would appear as
+// package-private as they are marked as such in the system_current stubs. So build against
+// core_platform and add the stubs manually in "libs". See http://b/147773144#comment7.
+java_library {
+ name: "service-connectivity-mdns-standalone-build-test",
+ sdk_version: "core_platform",
+ srcs: [
+ ":service-mdns-droidstubs",
+ "src/com/android/server/connectivity/mdns/**/*.java",
+ ],
+ exclude_srcs: [
+ "src/com/android/server/connectivity/mdns/internal/SocketNetlinkMonitor.java",
+ "src/com/android/server/connectivity/mdns/SocketNetLinkMonitorFactory.java"
+ ],
+ static_libs: [
+ "net-utils-device-common-mdns-standalone-build-test",
+ ],
+ libs: [
+ "framework-annotations-lib",
+ "android_system_stubs_current",
+ "androidx.annotation_annotation",
+ ],
+ visibility: [
+ "//visibility:private",
+ ],
+}
+
+droidstubs {
+ name: "service-mdns-droidstubs",
+ srcs: ["src/com/android/server/connectivity/mdns/SocketNetLinkMonitorFactory.java"],
+ libs: [
+ "net-utils-device-common-mdns-standalone-build-test",
+ "service-connectivity-tiramisu-pre-jarjar"
+ ],
+ visibility: [
+ "//visibility:private",
+ ],
+}
\ No newline at end of file
diff --git a/service-t/src/com/android/server/connectivity/mdns/ISocketNetLinkMonitor.java b/service-t/src/com/android/server/connectivity/mdns/ISocketNetLinkMonitor.java
new file mode 100644
index 0000000..ef3928c
--- /dev/null
+++ b/service-t/src/com/android/server/connectivity/mdns/ISocketNetLinkMonitor.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2022 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.server.connectivity.mdns;
+
+/**
+ * The interface for netlink monitor.
+ */
+public interface ISocketNetLinkMonitor {
+
+ /**
+ * Returns if the netlink monitor is supported or not. By default, it is not supported.
+ */
+ default boolean isSupported() {
+ return false;
+ }
+
+ /**
+ * Starts the monitor.
+ */
+ default void startMonitoring() {
+ }
+
+ /**
+ * Stops the monitor.
+ */
+ default void stopMonitoring() {
+ }
+}
diff --git a/service-t/src/com/android/server/connectivity/mdns/MdnsSocketProvider.java b/service-t/src/com/android/server/connectivity/mdns/MdnsSocketProvider.java
index 0c709ea..2823f92 100644
--- a/service-t/src/com/android/server/connectivity/mdns/MdnsSocketProvider.java
+++ b/service-t/src/com/android/server/connectivity/mdns/MdnsSocketProvider.java
@@ -29,16 +29,12 @@
import android.net.TetheringManager.TetheringEventCallback;
import android.os.Handler;
import android.os.Looper;
-import android.system.OsConstants;
import android.util.ArrayMap;
import android.util.Log;
import com.android.internal.annotations.VisibleForTesting;
import com.android.net.module.util.CollectionUtils;
import com.android.net.module.util.LinkPropertiesUtils.CompareResult;
-import com.android.net.module.util.ip.NetlinkMonitor;
-import com.android.net.module.util.netlink.NetlinkConstants;
-import com.android.net.module.util.netlink.NetlinkMessage;
import com.android.server.connectivity.mdns.util.MdnsLogger;
import java.io.IOException;
@@ -70,7 +66,7 @@
@NonNull private final Dependencies mDependencies;
@NonNull private final NetworkCallback mNetworkCallback;
@NonNull private final TetheringEventCallback mTetheringEventCallback;
- @NonNull private final NetlinkMonitor mNetlinkMonitor;
+ @NonNull private final ISocketNetLinkMonitor mSocketNetlinkMonitor;
private final ArrayMap<Network, SocketInfo> mNetworkSockets = new ArrayMap<>();
private final ArrayMap<String, SocketInfo> mTetherInterfaceSockets = new ArrayMap<>();
private final ArrayMap<Network, LinkProperties> mActiveNetworksLinkProperties =
@@ -117,7 +113,8 @@
}
};
- mNetlinkMonitor = new SocketNetlinkMonitor(mHandler);
+ mSocketNetlinkMonitor = SocketNetLinkMonitorFactory.createNetLinkMonitor(mHandler,
+ LOGGER.mLog);
}
/**
@@ -156,18 +153,6 @@
}
}
- private static class SocketNetlinkMonitor extends NetlinkMonitor {
- SocketNetlinkMonitor(Handler handler) {
- super(handler, LOGGER.mLog, TAG, OsConstants.NETLINK_ROUTE,
- NetlinkConstants.RTMGRP_IPV4_IFADDR | NetlinkConstants.RTMGRP_IPV6_IFADDR);
- }
-
- @Override
- public void processNetlinkMessage(NetlinkMessage nlMsg, long whenMs) {
- // TODO: Handle netlink message.
- }
- }
-
/*** Ensure that current running thread is same as given handler thread */
public static void ensureRunningOnHandlerThread(Handler handler) {
if (handler.getLooper().getThread() != Thread.currentThread()) {
@@ -192,7 +177,9 @@
final TetheringManager tetheringManager = mContext.getSystemService(TetheringManager.class);
tetheringManager.registerTetheringEventCallback(mHandler::post, mTetheringEventCallback);
- mHandler.post(mNetlinkMonitor::start);
+ if (mSocketNetlinkMonitor.isSupported()) {
+ mHandler.post(mSocketNetlinkMonitor::startMonitoring);
+ }
mMonitoringSockets = true;
}
@@ -209,7 +196,9 @@
TetheringManager.class);
tetheringManager.unregisterTetheringEventCallback(mTetheringEventCallback);
- mHandler.post(mNetlinkMonitor::stop);
+ if (mSocketNetlinkMonitor.isSupported()) {
+ mHandler.post(mSocketNetlinkMonitor::stopMonitoring);
+ }
// Clear all saved status.
mActiveNetworksLinkProperties.clear();
mNetworkSockets.clear();
diff --git a/service-t/src/com/android/server/connectivity/mdns/SocketNetLinkMonitorFactory.java b/service-t/src/com/android/server/connectivity/mdns/SocketNetLinkMonitorFactory.java
new file mode 100644
index 0000000..8f6aecc
--- /dev/null
+++ b/service-t/src/com/android/server/connectivity/mdns/SocketNetLinkMonitorFactory.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2022 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.server.connectivity.mdns;
+
+import android.annotation.NonNull;
+import android.os.Handler;
+
+import com.android.net.module.util.SharedLog;
+import com.android.server.connectivity.mdns.internal.SocketNetlinkMonitor;
+
+/**
+ * The factory class for creating the netlink monitor.
+ */
+public class SocketNetLinkMonitorFactory {
+
+ /**
+ * Creates a new netlink monitor.
+ */
+ public static ISocketNetLinkMonitor createNetLinkMonitor(@NonNull final Handler handler,
+ @NonNull SharedLog log) {
+ return new SocketNetlinkMonitor(handler, log);
+ }
+
+ private SocketNetLinkMonitorFactory() {
+ }
+
+}
diff --git a/service-t/src/com/android/server/connectivity/mdns/internal/SocketNetlinkMonitor.java b/service-t/src/com/android/server/connectivity/mdns/internal/SocketNetlinkMonitor.java
new file mode 100644
index 0000000..e053413
--- /dev/null
+++ b/service-t/src/com/android/server/connectivity/mdns/internal/SocketNetlinkMonitor.java
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2022 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.server.connectivity.mdns.internal;
+
+import android.annotation.NonNull;
+import android.os.Handler;
+import android.system.OsConstants;
+
+import com.android.net.module.util.SharedLog;
+import com.android.net.module.util.ip.NetlinkMonitor;
+import com.android.net.module.util.netlink.NetlinkConstants;
+import com.android.net.module.util.netlink.NetlinkMessage;
+import com.android.server.connectivity.mdns.ISocketNetLinkMonitor;
+
+/**
+ * The netlink monitor for MdnsSocketProvider.
+ */
+public class SocketNetlinkMonitor extends NetlinkMonitor implements ISocketNetLinkMonitor {
+
+ public SocketNetlinkMonitor(@NonNull final Handler handler, @NonNull SharedLog log) {
+ super(handler, log, SocketNetlinkMonitor.class.getSimpleName(), OsConstants.NETLINK_ROUTE,
+ NetlinkConstants.RTMGRP_IPV4_IFADDR | NetlinkConstants.RTMGRP_IPV6_IFADDR);
+ }
+
+ @Override
+ public void processNetlinkMessage(NetlinkMessage nlMsg, long whenMs) {
+
+ }
+
+ @Override
+ public boolean isSupported() {
+ return true;
+ }
+
+ @Override
+ public void startMonitoring() {
+ this.start();
+ }
+
+ @Override
+ public void stopMonitoring() {
+ this.stop();
+ }
+}