JNI: port QTimeZone on Android to QJniArray

Replace code that manually operates on jarray with QJniArray.

The comment about using separate jobject and QJniObject didn't make
sense; we didn't construct the QJniObject via fromLocalRef(), so
allocated a new local reference anyway, in addition to the local
reference returned by GetObjectArrayElement.

The implicit code now creates a QJniObject (-like object;
QtJniTypes::String contains a QJniObject) as well, but it's also
just a temporary. It would be cheaper to iterate over a
QJniArray<jstring>, but only if we then duplicate the code from
QJniObject::toString to not create an intermediate QJniObject.

Change-Id: I9f10541f533dd2fbd2f7ba6fdacc7d79b3ac3ae9
Reviewed-by: Assam Boudjelthia <assam.boudjelthia@qt.io>
(cherry picked from commit 64127a7a0db657cbc844938f02b623f90cdd78ee)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Volker Hilsheimer 2024-06-10 13:53:51 +02:00 committed by Qt Cherry-pick Bot
parent 3e320e45bf
commit aaeb4970fd

View File

@ -14,7 +14,7 @@ QT_BEGIN_NAMESPACE
Q_DECLARE_JNI_CLASS(TimeZone, "java/util/TimeZone");
Q_DECLARE_JNI_CLASS(Locale, "java/util/Locale");
Q_DECLARE_JNI_CLASS(Date, "java/util/Date");
Q_DECLARE_JNI_TYPE(StringArray, "[Ljava/lang/String;")
Q_DECLARE_JNI_CLASS(String, "java/lang/String")
/*
Private
@ -209,23 +209,14 @@ bool QAndroidTimeZonePrivate::isTimeZoneIdAvailable(const QByteArray &ianaId) co
QList<QByteArray> QAndroidTimeZonePrivate::availableTimeZoneIds() const
{
using namespace QtJniTypes;
const QJniArray androidAvailableIdList = TimeZone::callStaticMethod<String[]>("getAvailableIDs");
QList<QByteArray> availableTimeZoneIdList;
QJniObject androidAvailableIdList = QJniObject::callStaticMethod<QtJniTypes::StringArray>(
QtJniTypes::Traits<QtJniTypes::TimeZone>::className(), "getAvailableIDs");
QJniEnvironment jniEnv;
int androidTZcount = jniEnv->GetArrayLength(androidAvailableIdList.object<jarray>());
// need separate jobject and QJniObject here so that we can delete (DeleteLocalRef) the reference to the jobject
// (or else the JNI reference table fills after 512 entries from GetObjectArrayElement)
jobject androidTZobject;
QJniObject androidTZ;
for (int i = 0; i < androidTZcount; i++) {
androidTZobject = jniEnv->GetObjectArrayElement(androidAvailableIdList.object<jobjectArray>(), i);
androidTZ = androidTZobject;
availableTimeZoneIdList.append(androidTZ.toString().toUtf8());
jniEnv->DeleteLocalRef(androidTZobject);
}
availableTimeZoneIdList.reserve(androidAvailableIdList.size());
for (const auto &id : androidAvailableIdList)
availableTimeZoneIdList.append(id.toString().toUtf8());
return availableTimeZoneIdList;
}