Android: Add Android types and converters
This is a part of API review changed of Qt 6.8. These private APIs are being moved from QtDeclarative to QtBase. Pick-to: 6.8 Task-number: QTBUG-126976 Task-number: QTBUG-127087 Change-Id: I9bfdb7dea12a2fb6a179d87639364ae28c39243b Reviewed-by: Assam Boudjelthia <assam.boudjelthia@qt.io>
This commit is contained in:
parent
cb40a1fa7f
commit
4413c81c48
@ -1071,6 +1071,8 @@ qt_internal_extend_target(Core CONDITION ANDROID
|
|||||||
kernel/qjnihelpers.cpp kernel/qjnihelpers_p.h
|
kernel/qjnihelpers.cpp kernel/qjnihelpers_p.h
|
||||||
platform/android/qandroidextras_p.h platform/android/qandroidextras.cpp
|
platform/android/qandroidextras_p.h platform/android/qandroidextras.cpp
|
||||||
platform/android/qandroidnativeinterface.cpp
|
platform/android/qandroidnativeinterface.cpp
|
||||||
|
platform/android/qandroidtypes_p.h
|
||||||
|
platform/android/qandroidtypeconverter_p.h
|
||||||
NO_UNITY_BUILD_SOURCES
|
NO_UNITY_BUILD_SOURCES
|
||||||
platform/android/qandroidextras.cpp
|
platform/android/qandroidextras.cpp
|
||||||
# qtNativeClassName conflicts with similar symbols in android headers
|
# qtNativeClassName conflicts with similar symbols in android headers
|
||||||
|
99
src/corelib/platform/android/qandroidtypeconverter_p.h
Normal file
99
src/corelib/platform/android/qandroidtypeconverter_p.h
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
// Copyright (C) 2024 The Qt Company Ltd.
|
||||||
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
|
||||||
|
|
||||||
|
#ifndef QANDROIDTYPECONVERTER_P_H
|
||||||
|
#define QANDROIDTYPECONVERTER_P_H
|
||||||
|
|
||||||
|
//
|
||||||
|
// W A R N I N G
|
||||||
|
// -------------
|
||||||
|
//
|
||||||
|
// This file is not part of the Qt API. It exists purely as an
|
||||||
|
// implementation detail. This header file may change from version to
|
||||||
|
// version without notice, or even be removed.
|
||||||
|
//
|
||||||
|
// We mean it.
|
||||||
|
|
||||||
|
#include <QtQuick/private/qandroidtypes_p.h>
|
||||||
|
#include <QtQuick/private/qandroiditemmodelproxy_p.h>
|
||||||
|
|
||||||
|
#include <QtCore/qjniobject.h>
|
||||||
|
#include <QtCore/qjnienvironment.h>
|
||||||
|
#include <QtCore/qjnitypes.h>
|
||||||
|
|
||||||
|
QT_BEGIN_NAMESPACE
|
||||||
|
|
||||||
|
namespace QAndroidTypeConverter
|
||||||
|
{
|
||||||
|
[[maybe_unused]] static QVariant toQVariant(const QJniObject &object)
|
||||||
|
{
|
||||||
|
using namespace QtJniTypes;
|
||||||
|
if (!object.isValid())
|
||||||
|
return QVariant{};
|
||||||
|
const QByteArray classname(object.className());
|
||||||
|
|
||||||
|
if (classname == Traits<String>::className())
|
||||||
|
return object.toString();
|
||||||
|
else if (classname == Traits<Integer>::className())
|
||||||
|
return object.callMethod<jint>("intValue");
|
||||||
|
else if (classname == Traits<Long>::className())
|
||||||
|
return QVariant::fromValue<long>(object.callMethod<jlong>("longValue"));
|
||||||
|
else if (classname == Traits<Double>::className())
|
||||||
|
return object.callMethod<jdouble>("doubleValue");
|
||||||
|
else if (classname == Traits<Float>::className())
|
||||||
|
return object.callMethod<jfloat>("floatValue");
|
||||||
|
else if (classname == Traits<Boolean>::className())
|
||||||
|
return QVariant::fromValue<bool>(object.callMethod<jboolean>("booleanValue"));
|
||||||
|
else {
|
||||||
|
QJniEnvironment env;
|
||||||
|
const jclass className = env.findClass(Traits<JQtAbstractItemModel>::className());
|
||||||
|
if (env->IsInstanceOf(object.object(), className))
|
||||||
|
return QVariant::fromValue(QAndroidItemModelProxy::createNativeProxy(object));
|
||||||
|
}
|
||||||
|
|
||||||
|
return QVariant{};
|
||||||
|
}
|
||||||
|
|
||||||
|
[[maybe_unused]] Q_REQUIRED_RESULT static jobject toJavaObject(const QVariant &var, JNIEnv *env)
|
||||||
|
{
|
||||||
|
Q_ASSERT(env);
|
||||||
|
switch (var.typeId()) {
|
||||||
|
case QMetaType::Type::Int:
|
||||||
|
return env->NewLocalRef(QJniObject::construct<QtJniTypes::Integer>(
|
||||||
|
get<int>(var))
|
||||||
|
.object());
|
||||||
|
case QMetaType::Type::Long:
|
||||||
|
case QMetaType::Type::LongLong:
|
||||||
|
return env->NewLocalRef(QJniObject::construct<QtJniTypes::Long>(
|
||||||
|
get<jlong>(var))
|
||||||
|
.object());
|
||||||
|
case QMetaType::Type::Double:
|
||||||
|
return env->NewLocalRef(QJniObject::construct<QtJniTypes::Double>(
|
||||||
|
get<double>(var))
|
||||||
|
.object());
|
||||||
|
case QMetaType::Type::Float:
|
||||||
|
return env->NewLocalRef(QJniObject::construct<QtJniTypes::Float>(
|
||||||
|
get<float>(var))
|
||||||
|
.object());
|
||||||
|
case QMetaType::Type::Bool:
|
||||||
|
return env->NewLocalRef(QJniObject::construct<QtJniTypes::Boolean>(
|
||||||
|
get<bool>(var))
|
||||||
|
.object());
|
||||||
|
case QMetaType::Type::QString:
|
||||||
|
return env->NewLocalRef(
|
||||||
|
QJniObject::fromString(get<QString>(var)).object());
|
||||||
|
default:
|
||||||
|
if (var.canConvert<QAbstractItemModel *>()) {
|
||||||
|
return env->NewLocalRef(
|
||||||
|
QAndroidItemModelProxy::createProxy(var.value<QAbstractItemModel *>())
|
||||||
|
.object());
|
||||||
|
} else
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
QT_END_NAMESPACE
|
||||||
|
|
||||||
|
#endif // QANDROIDTYPECONVERTER_P_H
|
43
src/corelib/platform/android/qandroidtypes_p.h
Normal file
43
src/corelib/platform/android/qandroidtypes_p.h
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
// Copyright (C) 2024 The Qt Company Ltd.
|
||||||
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
|
||||||
|
|
||||||
|
#ifndef QANDROIDTYPES_P_H
|
||||||
|
#define QANDROIDTYPES_P_H
|
||||||
|
|
||||||
|
//
|
||||||
|
// W A R N I N G
|
||||||
|
// -------------
|
||||||
|
//
|
||||||
|
// This file is not part of the Qt API. It exists purely as an
|
||||||
|
// implementation detail. This header file may change from version to
|
||||||
|
// version without notice, or even be removed.
|
||||||
|
//
|
||||||
|
// We mean it.
|
||||||
|
|
||||||
|
#include <QtCore/qjniobject.h>
|
||||||
|
#include <QtCore/qjnienvironment.h>
|
||||||
|
#include <QtCore/qjnitypes.h>
|
||||||
|
|
||||||
|
QT_BEGIN_NAMESPACE
|
||||||
|
|
||||||
|
#ifndef QT_DECLARE_JNI_CLASS_STANDARD_TYPES
|
||||||
|
Q_DECLARE_JNI_CLASS(Void, "java/lang/Void");
|
||||||
|
Q_DECLARE_JNI_CLASS(Integer, "java/lang/Integer");
|
||||||
|
Q_DECLARE_JNI_CLASS(Long, "java/lang/Long");
|
||||||
|
Q_DECLARE_JNI_CLASS(Double, "java/lang/Double");
|
||||||
|
Q_DECLARE_JNI_CLASS(Float, "java/lang/Float");
|
||||||
|
Q_DECLARE_JNI_CLASS(Boolean, "java/lang/Boolean");
|
||||||
|
Q_DECLARE_JNI_CLASS(String, "java/lang/String");
|
||||||
|
Q_DECLARE_JNI_CLASS(Class, "java/lang/Class");
|
||||||
|
|
||||||
|
Q_DECLARE_JNI_CLASS(HashMap, "java/util/HashMap")
|
||||||
|
Q_DECLARE_JNI_CLASS(Set, "java/util/Set")
|
||||||
|
#endif
|
||||||
|
|
||||||
|
Q_DECLARE_JNI_CLASS(JQtAbstractItemModel, "org/qtproject/qt/android/QtAbstractItemModel")
|
||||||
|
Q_DECLARE_JNI_CLASS(JQtAndroidItemModelProxy, "org/qtproject/qt/android/QtAndroidItemModelProxy")
|
||||||
|
Q_DECLARE_JNI_CLASS(JQtModelIndex, "org/qtproject/qt/android/QtModelIndex")
|
||||||
|
|
||||||
|
QT_END_NAMESPACE
|
||||||
|
|
||||||
|
#endif // QANDROIDTYPES_P_H
|
Loading…
x
Reference in New Issue
Block a user