changes for qt6-Android

- QT += gui
- QT += androidextras ==> only for Qt5
- QAndroidJniEnvironment ==> QJniEnvironment
- QAndroidJniObject ==> QJniObject
- QtAndroid::runOnAndroidThread ==> Qt5
- QNativeInterface::QAndroidApplication::runOnAndroidMainThread ==> Qt6

Signed-off-by: Jonathan Bagg <drwho@infidigm.net>
This commit is contained in:
mariuszmaximus
2023-11-20 18:58:51 +01:00
committed by Jonathan Bagg
parent a2b907b95f
commit 9fbb8dcbc1
3 changed files with 35 additions and 2 deletions

View File

@ -25,7 +25,11 @@
--------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------
**************************************************************************************************/ **************************************************************************************************/
#include <QGuiApplication> #include <QGuiApplication>
#include <QRegularExpression>
#include "androidnsd_p.h" #include "androidnsd_p.h"
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
using QAndroidJniEnvironment = QJniEnvironment;
#endif
Q_DECLARE_METATYPE(QHostAddress) 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 // 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. // 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. // 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<quint64>(this), QtAndroid::androidActivity().object()); nsdManager = QAndroidJniObject("qtzeroconf/QZeroConfNsdManager", "(JLandroid/content/Context;)V", reinterpret_cast<quint64>(this), QtAndroid::androidActivity().object());
#else
nsdManager = QAndroidJniObject("qtzeroconf/QZeroConfNsdManager", "(JLandroid/content/Context;)V", reinterpret_cast<quint64>(this), QNativeInterface::QAndroidApplication::context());
#endif
if (nsdManager.isValid()) { if (nsdManager.isValid()) {
jclass objectClass = env->GetObjectClass(nsdManager.object<jobject>()); jclass objectClass = env->GetObjectClass(nsdManager.object<jobject>());
env->RegisterNatives(objectClass, methods, sizeof(methods) / sizeof(methods[0])); 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); QAndroidJniObject ref(nsdManager);
publishName = name; publishName = name;
publishType = type; publishType = type;
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QtAndroid::runOnAndroidThread([=](){ QtAndroid::runOnAndroidThread([=](){
#else
QNativeInterface::QAndroidApplication::runOnAndroidMainThread([=]() {
#endif
QAndroidJniObject txtMap("java/util/HashMap"); QAndroidJniObject txtMap("java/util/HashMap");
foreach (const QByteArray &key, txtRecords.keys()) { foreach (const QByteArray &key, txtRecords.keys()) {
txtMap.callObjectMethod("put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", txtMap.callObjectMethod("put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;",
@ -105,7 +117,11 @@ void QZeroConfPrivate::stopServicePublish()
if (qGuiApp->applicationState() == Qt::ApplicationSuspended) { if (qGuiApp->applicationState() == Qt::ApplicationSuspended) {
ref.callMethod<void>("unregisterService"); ref.callMethod<void>("unregisterService");
} else { } else {
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QtAndroid::runOnAndroidThread([ref]() { QtAndroid::runOnAndroidThread([ref]() {
#else
QNativeInterface::QAndroidApplication::runOnAndroidMainThread([ref]() {
#endif
ref.callMethod<void>("unregisterService"); ref.callMethod<void>("unregisterService");
}); });
} }
@ -115,7 +131,11 @@ void QZeroConfPrivate::startBrowser(QString type, QAbstractSocket::NetworkLayerP
{ {
Q_UNUSED(protocol) Q_UNUSED(protocol)
QAndroidJniObject ref(nsdManager); QAndroidJniObject ref(nsdManager);
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QtAndroid::runOnAndroidThread([ref, type]() { QtAndroid::runOnAndroidThread([ref, type]() {
#else
QNativeInterface::QAndroidApplication::runOnAndroidMainThread([ref, type]() {
#endif
ref.callMethod<void>("discoverServices", "(Ljava/lang/String;)V", QAndroidJniObject::fromString(type).object<jstring>()); ref.callMethod<void>("discoverServices", "(Ljava/lang/String;)V", QAndroidJniObject::fromString(type).object<jstring>());
}); });
} }
@ -128,7 +148,11 @@ void QZeroConfPrivate::stopBrowser()
if (qGuiApp->applicationState() == Qt::ApplicationSuspended) { if (qGuiApp->applicationState() == Qt::ApplicationSuspended) {
ref.callMethod<void>("stopServiceDiscovery"); ref.callMethod<void>("stopServiceDiscovery");
} else { } else {
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QtAndroid::runOnAndroidThread([ref]() { QtAndroid::runOnAndroidThread([ref]() {
#else
QNativeInterface::QAndroidApplication::runOnAndroidMainThread([ref]() {
#endif
ref.callMethod<void>("stopServiceDiscovery"); ref.callMethod<void>("stopServiceDiscovery");
}); });
} }
@ -230,7 +254,7 @@ void QZeroConfPrivate::onServiceResolved(const QString &name, const QString &typ
zcs->m_type = type; zcs->m_type = type;
// A previous implementation (based on avahi) returned service type as "_http._tcp" but Android API return "._http._tcp" // 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. // 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_host = hostname;
zcs->m_port = port; zcs->m_port = port;
zcs->m_ip = address; zcs->m_ip = address;

View File

@ -25,10 +25,16 @@
--------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------
**************************************************************************************************/ **************************************************************************************************/
#include "qzeroconf.h" #include "qzeroconf.h"
#include <QMap>
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
#include <QtAndroid> #include <QtAndroid>
#include <QtAndroidExtras> #include <QtAndroidExtras>
#include <QAndroidJniObject> #include <QAndroidJniObject>
#else
#include <QJniObject>
using QAndroidJniObject = QJniObject;
#endif
class QZeroConfPrivate: QObject class QZeroConfPrivate: QObject
{ {

View File

@ -122,7 +122,10 @@ ubports: {
} }
android: { android: {
QT += androidextras lessThan(QT_MAJOR_VERSION, 6) {
QT += androidextras
}
QT += gui
HEADERS += $$PWD/qzeroconf.h $$PWD/androidnsd_p.h HEADERS += $$PWD/qzeroconf.h $$PWD/androidnsd_p.h
SOURCES += $$PWD/androidnsd.cpp SOURCES += $$PWD/androidnsd.cpp
DISTFILES += $$PWD/QZeroConfNsdManager.java DISTFILES += $$PWD/QZeroConfNsdManager.java