Unstringify QTimeZone Android parts

Qt 6.4 introduced new facilities to reduce the plain strings
with Android methods/signatures.

Fixes: QTBUG-104187
Change-Id: I94fc336f253e4969ac96ebcba5c1423989b340ab
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
This commit is contained in:
Juha Vuolle 2022-08-22 15:10:57 +03:00
parent 80d9436488
commit 501dfe1bc7

View File

@ -10,6 +10,11 @@
QT_BEGIN_NAMESPACE 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;")
/* /*
Private Private
@ -25,9 +30,9 @@ QAndroidTimeZonePrivate::QAndroidTimeZonePrivate()
: QTimeZonePrivate() : QTimeZonePrivate()
{ {
// Keep in sync with systemTimeZoneId(): // Keep in sync with systemTimeZoneId():
androidTimeZone = QJniObject::callStaticObjectMethod( androidTimeZone = QJniObject::callStaticMethod<QtJniTypes::TimeZone>(
"java.util.TimeZone", "getDefault", "()Ljava/util/TimeZone;"); QtJniTypes::className<QtJniTypes::TimeZone>(), "getDefault");
const QJniObject id = androidTimeZone.callObjectMethod("getID", "()Ljava/lang/String;"); const QJniObject id = androidTimeZone.callMethod<jstring>("getID");
m_id = id.toString().toUtf8(); m_id = id.toString().toUtf8();
} }
@ -53,20 +58,19 @@ static QString getDisplayName(QJniObject zone, jint style, jboolean dst,
const QLocale &locale) const QLocale &locale)
{ {
QJniObject jbcpTag = QJniObject::fromString(locale.bcp47Name()); QJniObject jbcpTag = QJniObject::fromString(locale.bcp47Name());
QJniObject jlocale = QJniObject::callStaticObjectMethod( QJniObject jlocale = QJniObject::callStaticMethod<QtJniTypes::Locale>(
"java/util/Locale", "forLanguageTag", "(Ljava/lang/String;)Ljava/util/Locale;", QtJniTypes::className<QtJniTypes::Locale>(), "forLanguageTag",
jbcpTag.object<jstring>()); jbcpTag.object<jstring>());
return zone.callObjectMethod("getDisplayName", return zone.callMethod<jstring>("getDisplayName", dst, style,
"(ZILjava/util/Locale;)Ljava/lang/String;", jlocale.object<QtJniTypes::Locale>()).toString();
dst, style, jlocale.object()).toString();
} }
void QAndroidTimeZonePrivate::init(const QByteArray &ianaId) void QAndroidTimeZonePrivate::init(const QByteArray &ianaId)
{ {
const QString iana = QString::fromUtf8(ianaId); const QString iana = QString::fromUtf8(ianaId);
androidTimeZone = QJniObject::callStaticObjectMethod( androidTimeZone = QJniObject::callStaticMethod<QtJniTypes::TimeZone>(
"java.util.TimeZone", "getTimeZone", "(Ljava/lang/String;)Ljava/util/TimeZone;", QtJniTypes::className<QtJniTypes::TimeZone>(), "getTimeZone",
QJniObject::fromString(iana).object<jstring>()); QJniObject::fromString(iana).object<jstring>());
// The ID or display name of the zone we've got, if it looks like what we asked for: // The ID or display name of the zone we've got, if it looks like what we asked for:
@ -81,7 +85,7 @@ void QAndroidTimeZonePrivate::init(const QByteArray &ianaId)
// recognize the name; so check for whether ianaId is a recognized name of // recognize the name; so check for whether ianaId is a recognized name of
// the zone object we got and ignore the zone if not. // the zone object we got and ignore the zone if not.
// Try checking ianaId against getID(), getDisplayName(): // Try checking ianaId against getID(), getDisplayName():
m_id = match(androidTimeZone.callObjectMethod("getID", "()Ljava/lang/String;").toString()); m_id = match(androidTimeZone.callMethod<jstring>("getID").toString());
for (int style = 1; m_id.isEmpty() && style >= 0; --style) { for (int style = 1; m_id.isEmpty() && style >= 0; --style) {
for (int dst = 1; m_id.isEmpty() && dst >= 0; --dst) { for (int dst = 1; m_id.isEmpty() && dst >= 0; --dst) {
for (int pick = 2; m_id.isEmpty() && pick >= 0; --pick) { for (int pick = 2; m_id.isEmpty() && pick >= 0; --pick) {
@ -128,11 +132,13 @@ int QAndroidTimeZonePrivate::offsetFromUtc(qint64 atMSecsSinceEpoch) const
{ {
// offsetFromUtc( ) is invoked when androidTimeZone may not yet be set at startup, // offsetFromUtc( ) is invoked when androidTimeZone may not yet be set at startup,
// so a validity test is needed here // so a validity test is needed here
if ( androidTimeZone.isValid() ) if ( androidTimeZone.isValid() ) {
// the java method getOffset() returns milliseconds, but QTimeZone returns seconds // the java method getOffset() returns milliseconds, but QTimeZone returns seconds
return androidTimeZone.callMethod<jint>( "getOffset", "(J)I", static_cast<jlong>(atMSecsSinceEpoch) ) / 1000; return androidTimeZone.callMethod<jint>(
else "getOffset", static_cast<jlong>(atMSecsSinceEpoch) ) / 1000;
} else {
return 0; return 0;
}
} }
int QAndroidTimeZonePrivate::standardTimeOffset(qint64 atMSecsSinceEpoch) const int QAndroidTimeZonePrivate::standardTimeOffset(qint64 atMSecsSinceEpoch) const
@ -163,8 +169,10 @@ bool QAndroidTimeZonePrivate::hasDaylightTime() const
bool QAndroidTimeZonePrivate::isDaylightTime(qint64 atMSecsSinceEpoch) const bool QAndroidTimeZonePrivate::isDaylightTime(qint64 atMSecsSinceEpoch) const
{ {
if ( androidTimeZone.isValid() ) { if ( androidTimeZone.isValid() ) {
QJniObject jDate( "java/util/Date", "(J)V", static_cast<jlong>(atMSecsSinceEpoch) ); QJniObject jDate = QJniObject::construct<QtJniTypes::Date>(
return androidTimeZone.callMethod<jboolean>("inDaylightTime", "(Ljava/util/Date;)Z", jDate.object() ); static_cast<jlong>(atMSecsSinceEpoch));
return androidTimeZone.callMethod<jboolean>("inDaylightTime",
jDate.object<QtJniTypes::Date>());
} }
else else
return false; return false;
@ -191,16 +199,17 @@ QTimeZonePrivate::Data QAndroidTimeZonePrivate::data(qint64 forMSecsSinceEpoch)
QByteArray QAndroidTimeZonePrivate::systemTimeZoneId() const QByteArray QAndroidTimeZonePrivate::systemTimeZoneId() const
{ {
// Keep in sync with default constructor: // Keep in sync with default constructor:
QJniObject androidSystemTimeZone = QJniObject::callStaticObjectMethod( QJniObject androidSystemTimeZone = QJniObject::callStaticMethod<QtJniTypes::TimeZone>(
"java.util.TimeZone", "getDefault", "()Ljava/util/TimeZone;"); QtJniTypes::className<QtJniTypes::TimeZone>(), "getDefault");
const QJniObject id = androidSystemTimeZone.callObjectMethod<jstring>("getID"); const QJniObject id = androidSystemTimeZone.callMethod<jstring>("getID");
return id.toString().toUtf8(); return id.toString().toUtf8();
} }
QList<QByteArray> QAndroidTimeZonePrivate::availableTimeZoneIds() const QList<QByteArray> QAndroidTimeZonePrivate::availableTimeZoneIds() const
{ {
QList<QByteArray> availableTimeZoneIdList; QList<QByteArray> availableTimeZoneIdList;
QJniObject androidAvailableIdList = QJniObject::callStaticObjectMethod("java.util.TimeZone", "getAvailableIDs", "()[Ljava/lang/String;"); QJniObject androidAvailableIdList = QJniObject::callStaticMethod<QtJniTypes::StringArray>(
QtJniTypes::className<QtJniTypes::TimeZone>(), "getAvailableIDs");
QJniEnvironment jniEnv; QJniEnvironment jniEnv;
int androidTZcount = jniEnv->GetArrayLength(androidAvailableIdList.object<jarray>()); int androidTZcount = jniEnv->GetArrayLength(androidAvailableIdList.object<jarray>());