diff options
Diffstat (limited to 'src/corelib/time/qtimezoneprivate_android.cpp')
-rw-r--r-- | src/corelib/time/qtimezoneprivate_android.cpp | 133 |
1 files changed, 50 insertions, 83 deletions
diff --git a/src/corelib/time/qtimezoneprivate_android.cpp b/src/corelib/time/qtimezoneprivate_android.cpp index a82af07b58..47fc68b1ac 100644 --- a/src/corelib/time/qtimezoneprivate_android.cpp +++ b/src/corelib/time/qtimezoneprivate_android.cpp @@ -1,51 +1,21 @@ -/**************************************************************************** -** -** Copyright (C) 2021 The Qt Company Ltd. -** Copyright (C) 2014 Drew Parsons <dparsons@emerall.com> -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtCore module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2021 The Qt Company Ltd. +// Copyright (C) 2014 Drew Parsons <dparsons@emerall.com> +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #include "qtimezone.h" #include "qtimezoneprivate_p.h" #include <QtCore/QJniEnvironment> #include <QtCore/QSet> +#include <QtCore/qjnitypes.h> 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 @@ -61,9 +31,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::Traits<QtJniTypes::TimeZone>::className(), "getDefault"); + const QJniObject id = androidTimeZone.callMethod<jstring>("getID"); m_id = id.toString().toUtf8(); } @@ -85,36 +55,27 @@ QAndroidTimeZonePrivate::~QAndroidTimeZonePrivate() { } -static QJniObject getDisplayName(QJniObject zone, jint style, jboolean dst, +static QString getDisplayName(QJniObject zone, jint style, jboolean dst, const QLocale &locale) { - QJniObject jlanguage - = QJniObject::fromString(QLocale::languageToString(locale.language())); - QJniObject jterritory - = QJniObject::fromString(QLocale::territoryToString(locale.territory())); - QJniObject - jvariant = QJniObject::fromString(QLocale::scriptToString(locale.script())); - QJniObject jlocale("java.util.Locale", - "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V", - jlanguage.object<jstring>(), - jterritory.object<jstring>(), - jvariant.object<jstring>()); - - return zone.callObjectMethod("getDisplayName", - "(ZILjava/util/Locale;)Ljava/lang/String;", - dst, style, jlocale.object()); + QJniObject jbcpTag = QJniObject::fromString(locale.bcp47Name()); + QJniObject jlocale = QJniObject::callStaticMethod<QtJniTypes::Locale>( + QtJniTypes::Traits<QtJniTypes::Locale>::className(), "forLanguageTag", + jbcpTag.object<jstring>()); + + 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::Traits<QtJniTypes::TimeZone>::className(), "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: - const auto match = [iana](const QJniObject &jname) -> QByteArray { - const QString name = jname.toString(); + const auto match = [iana](const QString &name) -> QByteArray { if (iana.compare(name, Qt::CaseInsensitive) == 0) return name.toUtf8(); @@ -125,7 +86,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;")); + 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) { @@ -154,7 +115,7 @@ QString QAndroidTimeZonePrivate::displayName(QTimeZone::TimeType timeType, QTime // treat all NameTypes as java TimeZone style LONG (value 1), except of course QTimeZone::ShortName which is style SHORT (value 0); jint style = (nameType == QTimeZone::ShortName ? 0 : 1); - name = getDisplayName(androidTimeZone, style, daylightTime, locale).toString(); + name = getDisplayName(androidTimeZone, style, daylightTime, locale); } return name; @@ -172,11 +133,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 @@ -198,7 +161,7 @@ int QAndroidTimeZonePrivate::daylightTimeOffset(qint64 atMSecsSinceEpoch) const bool QAndroidTimeZonePrivate::hasDaylightTime() const { if ( androidTimeZone.isValid() ) - /* note: the Java function only tests for future DST transtions, not past */ + /* note: the Java function only tests for future DST transitions, not past */ return androidTimeZone.callMethod<jboolean>("useDaylightTime" ); else return false; @@ -207,8 +170,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; @@ -217,16 +182,11 @@ bool QAndroidTimeZonePrivate::isDaylightTime(qint64 atMSecsSinceEpoch) const QTimeZonePrivate::Data QAndroidTimeZonePrivate::data(qint64 forMSecsSinceEpoch) const { if (androidTimeZone.isValid()) { - Data data; - data.atMSecsSinceEpoch = forMSecsSinceEpoch; - data.standardTimeOffset = standardTimeOffset(forMSecsSinceEpoch); - data.offsetFromUtc = offsetFromUtc(forMSecsSinceEpoch); - data.daylightTimeOffset = data.offsetFromUtc - data.standardTimeOffset; - data.abbreviation = abbreviation(forMSecsSinceEpoch); - return data; - } else { - return invalidData(); + return Data(abbreviation(forMSecsSinceEpoch), forMSecsSinceEpoch, + offsetFromUtc(forMSecsSinceEpoch), + standardTimeOffset(forMSecsSinceEpoch)); } + return {}; } // java.util.TimeZone does not directly provide transitions, @@ -235,16 +195,23 @@ 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::Traits<QtJniTypes::TimeZone>::className(), "getDefault"); + const QJniObject id = androidSystemTimeZone.callMethod<jstring>("getID"); return id.toString().toUtf8(); } +bool QAndroidTimeZonePrivate::isTimeZoneIdAvailable(const QByteArray &ianaId) const +{ + QAndroidTimeZonePrivate probe(ianaId); + return probe.isValid(); +} + 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::Traits<QtJniTypes::TimeZone>::className(), "getAvailableIDs"); QJniEnvironment jniEnv; int androidTZcount = jniEnv->GetArrayLength(androidAvailableIdList.object<jarray>()); |