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 <QRegularExpression>
#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<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()) {
jclass objectClass = env->GetObjectClass(nsdManager.object<jobject>());
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<void>("unregisterService");
} else {
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QtAndroid::runOnAndroidThread([ref]() {
#else
QNativeInterface::QAndroidApplication::runOnAndroidMainThread([ref]() {
#endif
ref.callMethod<void>("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<void>("discoverServices", "(Ljava/lang/String;)V", QAndroidJniObject::fromString(type).object<jstring>());
});
}
@ -128,7 +148,11 @@ void QZeroConfPrivate::stopBrowser()
if (qGuiApp->applicationState() == Qt::ApplicationSuspended) {
ref.callMethod<void>("stopServiceDiscovery");
} else {
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QtAndroid::runOnAndroidThread([ref]() {
#else
QNativeInterface::QAndroidApplication::runOnAndroidMainThread([ref]() {
#endif
ref.callMethod<void>("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;

View File

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

View File

@ -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