diff --git a/androidnsd.cpp b/androidnsd.cpp index 2fd25e8..69de66b 100644 --- a/androidnsd.cpp +++ b/androidnsd.cpp @@ -25,7 +25,11 @@ --------------------------------------------------------------------------------------------------- **************************************************************************************************/ #include +#include #include "androidnsd_p.h" +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) +using QAndroidJniEnvironment = QJniEnvironment; +#endif Q_DECLARE_METATYPE(QHostAddress) @@ -53,7 +57,11 @@ QZeroConfPrivate::QZeroConfPrivate(QZeroConf *parent) // There seems to be no straight forward way to match the "thiz" pointer from JNI calls to our pointer of the Java class // Passing "this" as ID down to Java so we can access "this" in callbacks. // Note: needs to be quint64 as uintptr_t might be 32 or 64 bit depending on the system, while Java expects a jlong which is always 64 bit. +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) nsdManager = QAndroidJniObject("qtzeroconf/QZeroConfNsdManager", "(JLandroid/content/Context;)V", reinterpret_cast(this), QtAndroid::androidActivity().object()); +#else + nsdManager = QAndroidJniObject("qtzeroconf/QZeroConfNsdManager", "(JLandroid/content/Context;)V", reinterpret_cast(this), QNativeInterface::QAndroidApplication::context()); +#endif if (nsdManager.isValid()) { jclass objectClass = env->GetObjectClass(nsdManager.object()); env->RegisterNatives(objectClass, methods, sizeof(methods) / sizeof(methods[0])); @@ -80,7 +88,11 @@ void QZeroConfPrivate::startServicePublish(const char *name, const char *type, q QAndroidJniObject ref(nsdManager); publishName = name; publishType = type; +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) QtAndroid::runOnAndroidThread([=](){ +#else + QNativeInterface::QAndroidApplication::runOnAndroidMainThread([=]() { +#endif QAndroidJniObject txtMap("java/util/HashMap"); foreach (const QByteArray &key, txtRecords.keys()) { txtMap.callObjectMethod("put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", @@ -105,7 +117,11 @@ void QZeroConfPrivate::stopServicePublish() if (qGuiApp->applicationState() == Qt::ApplicationSuspended) { ref.callMethod("unregisterService"); } else { +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) QtAndroid::runOnAndroidThread([ref]() { +#else + QNativeInterface::QAndroidApplication::runOnAndroidMainThread([ref]() { +#endif ref.callMethod("unregisterService"); }); } @@ -115,7 +131,11 @@ void QZeroConfPrivate::startBrowser(QString type, QAbstractSocket::NetworkLayerP { Q_UNUSED(protocol) QAndroidJniObject ref(nsdManager); +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) QtAndroid::runOnAndroidThread([ref, type]() { +#else + QNativeInterface::QAndroidApplication::runOnAndroidMainThread([ref, type]() { +#endif ref.callMethod("discoverServices", "(Ljava/lang/String;)V", QAndroidJniObject::fromString(type).object()); }); } @@ -128,7 +148,11 @@ void QZeroConfPrivate::stopBrowser() if (qGuiApp->applicationState() == Qt::ApplicationSuspended) { ref.callMethod("stopServiceDiscovery"); } else { +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) QtAndroid::runOnAndroidThread([ref]() { +#else + QNativeInterface::QAndroidApplication::runOnAndroidMainThread([ref]() { +#endif ref.callMethod("stopServiceDiscovery"); }); } @@ -230,7 +254,7 @@ void QZeroConfPrivate::onServiceResolved(const QString &name, const QString &typ zcs->m_type = type; // A previous implementation (based on avahi) returned service type as "_http._tcp" but Android API return "._http._tcp" // Stripping leading dot for backwards compatibility. FIXME: Still not in line with bonjour, which adds a trailing dot. - zcs->m_type.remove(QRegExp("^.")); + zcs->m_type.remove(QRegularExpression("^.")); zcs->m_host = hostname; zcs->m_port = port; zcs->m_ip = address; diff --git a/androidnsd_p.h b/androidnsd_p.h index f0f6da6..916110c 100644 --- a/androidnsd_p.h +++ b/androidnsd_p.h @@ -25,10 +25,16 @@ --------------------------------------------------------------------------------------------------- **************************************************************************************************/ #include "qzeroconf.h" +#include +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) #include #include #include +#else +#include +using QAndroidJniObject = QJniObject; +#endif class QZeroConfPrivate: QObject { diff --git a/qtzeroconf.pri b/qtzeroconf.pri index 19fc37f..d84497e 100644 --- a/qtzeroconf.pri +++ b/qtzeroconf.pri @@ -122,7 +122,10 @@ ubports: { } android: { - QT += androidextras + lessThan(QT_MAJOR_VERSION, 6) { + QT += androidextras + } + QT += gui HEADERS += $$PWD/qzeroconf.h $$PWD/androidnsd_p.h SOURCES += $$PWD/androidnsd.cpp DISTFILES += $$PWD/QZeroConfNsdManager.java