Skip to content

Commit

Permalink
Unstringify QTimeZone Android parts
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-by: Ivan Solovev <[email protected]>
  • Loading branch information
Juha Vuolle committed Aug 23, 2022
1 parent 80d9436 commit 501dfe1
Showing 1 changed file with 29 additions and 20 deletions.
49 changes: 29 additions & 20 deletions src/corelib/time/qtimezoneprivate_android.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@

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
Expand All @@ -25,9 +30,9 @@ QAndroidTimeZonePrivate::QAndroidTimeZonePrivate()
: QTimeZonePrivate()
{
// Keep in sync with systemTimeZoneId():
androidTimeZone = QJniObject::callStaticObjectMethod(
"java.util.TimeZone", "getDefault", "()Ljava/util/TimeZone;");
const QJniObject id = androidTimeZone.callObjectMethod("getID", "()Ljava/lang/String;");
androidTimeZone = QJniObject::callStaticMethod<QtJniTypes::TimeZone>(
QtJniTypes::className<QtJniTypes::TimeZone>(), "getDefault");
const QJniObject id = androidTimeZone.callMethod<jstring>("getID");
m_id = id.toString().toUtf8();
}

Expand All @@ -53,20 +58,19 @@ static QString getDisplayName(QJniObject zone, jint style, jboolean dst,
const QLocale &locale)
{
QJniObject jbcpTag = QJniObject::fromString(locale.bcp47Name());
QJniObject jlocale = QJniObject::callStaticObjectMethod(
"java/util/Locale", "forLanguageTag", "(Ljava/lang/String;)Ljava/util/Locale;",
QJniObject jlocale = QJniObject::callStaticMethod<QtJniTypes::Locale>(
QtJniTypes::className<QtJniTypes::Locale>(), "forLanguageTag",
jbcpTag.object<jstring>());

return zone.callObjectMethod("getDisplayName",
"(ZILjava/util/Locale;)Ljava/lang/String;",
dst, style, jlocale.object()).toString();
return zone.callMethod<jstring>("getDisplayName", dst, style,
jlocale.object<QtJniTypes::Locale>()).toString();
}

void QAndroidTimeZonePrivate::init(const QByteArray &ianaId)
{
const QString iana = QString::fromUtf8(ianaId);
androidTimeZone = QJniObject::callStaticObjectMethod(
"java.util.TimeZone", "getTimeZone", "(Ljava/lang/String;)Ljava/util/TimeZone;",
androidTimeZone = QJniObject::callStaticMethod<QtJniTypes::TimeZone>(
QtJniTypes::className<QtJniTypes::TimeZone>(), "getTimeZone",
QJniObject::fromString(iana).object<jstring>());

// The ID or display name of the zone we've got, if it looks like what we asked for:
Expand All @@ -81,7 +85,7 @@ void QAndroidTimeZonePrivate::init(const QByteArray &ianaId)
// recognize the name; so check for whether ianaId is a recognized name of
// the zone object we got and ignore the zone if not.
// 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 dst = 1; m_id.isEmpty() && dst >= 0; --dst) {
for (int pick = 2; m_id.isEmpty() && pick >= 0; --pick) {
Expand Down Expand Up @@ -128,11 +132,13 @@ int QAndroidTimeZonePrivate::offsetFromUtc(qint64 atMSecsSinceEpoch) const
{
// offsetFromUtc( ) is invoked when androidTimeZone may not yet be set at startup,
// so a validity test is needed here
if ( androidTimeZone.isValid() )
if ( androidTimeZone.isValid() ) {
// the java method getOffset() returns milliseconds, but QTimeZone returns seconds
return androidTimeZone.callMethod<jint>( "getOffset", "(J)I", static_cast<jlong>(atMSecsSinceEpoch) ) / 1000;
else
return androidTimeZone.callMethod<jint>(
"getOffset", static_cast<jlong>(atMSecsSinceEpoch) ) / 1000;
} else {
return 0;
}
}

int QAndroidTimeZonePrivate::standardTimeOffset(qint64 atMSecsSinceEpoch) const
Expand Down Expand Up @@ -163,8 +169,10 @@ bool QAndroidTimeZonePrivate::hasDaylightTime() const
bool QAndroidTimeZonePrivate::isDaylightTime(qint64 atMSecsSinceEpoch) const
{
if ( androidTimeZone.isValid() ) {
QJniObject jDate( "java/util/Date", "(J)V", static_cast<jlong>(atMSecsSinceEpoch) );
return androidTimeZone.callMethod<jboolean>("inDaylightTime", "(Ljava/util/Date;)Z", jDate.object() );
QJniObject jDate = QJniObject::construct<QtJniTypes::Date>(
static_cast<jlong>(atMSecsSinceEpoch));
return androidTimeZone.callMethod<jboolean>("inDaylightTime",
jDate.object<QtJniTypes::Date>());
}
else
return false;
Expand All @@ -191,16 +199,17 @@ QTimeZonePrivate::Data QAndroidTimeZonePrivate::data(qint64 forMSecsSinceEpoch)
QByteArray QAndroidTimeZonePrivate::systemTimeZoneId() const
{
// Keep in sync with default constructor:
QJniObject androidSystemTimeZone = QJniObject::callStaticObjectMethod(
"java.util.TimeZone", "getDefault", "()Ljava/util/TimeZone;");
const QJniObject id = androidSystemTimeZone.callObjectMethod<jstring>("getID");
QJniObject androidSystemTimeZone = QJniObject::callStaticMethod<QtJniTypes::TimeZone>(
QtJniTypes::className<QtJniTypes::TimeZone>(), "getDefault");
const QJniObject id = androidSystemTimeZone.callMethod<jstring>("getID");
return id.toString().toUtf8();
}

QList<QByteArray> QAndroidTimeZonePrivate::availableTimeZoneIds() const
{
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;
int androidTZcount = jniEnv->GetArrayLength(androidAvailableIdList.object<jarray>());
Expand Down

0 comments on commit 501dfe1

Please sign in to comment.