1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the plugins of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #include "qandroidsystemlocale.h"
41 #include "androidjnimain.h"
42 #include <QtCore/private/qjni_p.h>
43 #include <QtCore/private/qjnihelpers_p.h>
44 #include "qdatetime.h"
45 #include "qstringlist.h"
46 #include "qvariant.h"
47 
48 QT_BEGIN_NAMESPACE
49 
QAndroidSystemLocale()50 QAndroidSystemLocale::QAndroidSystemLocale() : m_locale(QLocale::C)
51 {
52 }
53 
getLocaleFromJava() const54 void QAndroidSystemLocale::getLocaleFromJava() const
55 {
56     QWriteLocker locker(&m_lock);
57 
58     QJNIObjectPrivate javaLocaleObject;
59     QJNIObjectPrivate javaActivity(QtAndroid::activity());
60     if (!javaActivity.isValid())
61         javaActivity = QtAndroid::service();
62     if (javaActivity.isValid()) {
63         QJNIObjectPrivate resources = javaActivity.callObjectMethod("getResources", "()Landroid/content/res/Resources;");
64         QJNIObjectPrivate configuration = resources.callObjectMethod("getConfiguration", "()Landroid/content/res/Configuration;");
65 
66         javaLocaleObject = configuration.getObjectField("locale", "Ljava/util/Locale;");
67     } else {
68         javaLocaleObject = QJNIObjectPrivate::callStaticObjectMethod("java/util/Locale", "getDefault", "()Ljava/util/Locale;");
69     }
70 
71     QString languageCode = javaLocaleObject.callObjectMethod("getLanguage", "()Ljava/lang/String;").toString();
72     QString countryCode = javaLocaleObject.callObjectMethod("getCountry", "()Ljava/lang/String;").toString();
73 
74     m_locale = QLocale(languageCode + QLatin1Char('_') + countryCode);
75 }
76 
query(QueryType type,QVariant in) const77 QVariant QAndroidSystemLocale::query(QueryType type, QVariant in) const
78 {
79     if (type == LocaleChanged) {
80         getLocaleFromJava();
81         return QVariant();
82     }
83 
84     QReadLocker locker(&m_lock);
85 
86     switch (type) {
87     case DecimalPoint:
88         return m_locale.decimalPoint();
89     case GroupSeparator:
90         return m_locale.groupSeparator();
91     case ZeroDigit:
92         return m_locale.zeroDigit();
93     case NegativeSign:
94         return m_locale.negativeSign();
95     case DateFormatLong:
96         return m_locale.dateFormat(QLocale::LongFormat);
97     case DateFormatShort:
98         return m_locale.dateFormat(QLocale::ShortFormat);
99     case TimeFormatLong:
100         return m_locale.timeFormat(QLocale::LongFormat);
101     case TimeFormatShort:
102         return m_locale.timeFormat(QLocale::ShortFormat);
103     case DayNameLong:
104         return m_locale.dayName(in.toInt(), QLocale::LongFormat);
105     case DayNameShort:
106         return m_locale.dayName(in.toInt(), QLocale::ShortFormat);
107     case MonthNameLong:
108         return m_locale.monthName(in.toInt(), QLocale::LongFormat);
109     case MonthNameShort:
110         return m_locale.monthName(in.toInt(), QLocale::ShortFormat);
111     case StandaloneMonthNameLong:
112         return m_locale.standaloneMonthName(in.toInt(), QLocale::LongFormat);
113     case StandaloneMonthNameShort:
114         return m_locale.standaloneMonthName(in.toInt(), QLocale::ShortFormat);
115     case DateToStringLong:
116         return m_locale.toString(in.toDate(), QLocale::LongFormat);
117     case DateToStringShort:
118         return m_locale.toString(in.toDate(), QLocale::ShortFormat);
119     case TimeToStringLong:
120         return m_locale.toString(in.toTime(), QLocale::LongFormat);
121     case TimeToStringShort:
122         return m_locale.toString(in.toTime(), QLocale::ShortFormat);
123     case DateTimeFormatLong:
124         return m_locale.dateTimeFormat(QLocale::LongFormat);
125     case DateTimeFormatShort:
126         return m_locale.dateTimeFormat(QLocale::ShortFormat);
127     case DateTimeToStringLong:
128         return m_locale.toString(in.toDateTime(), QLocale::LongFormat);
129     case DateTimeToStringShort:
130         return m_locale.toString(in.toDateTime(), QLocale::ShortFormat);
131     case PositiveSign:
132         return m_locale.positiveSign();
133     case AMText:
134         return m_locale.amText();
135     case PMText:
136         return m_locale.pmText();
137     case FirstDayOfWeek:
138         return m_locale.firstDayOfWeek();
139     case CurrencySymbol:
140         return m_locale .currencySymbol(QLocale::CurrencySymbolFormat(in.toUInt()));
141     case CurrencyToString: {
142         switch (in.type()) {
143         case QVariant::Int:
144             return m_locale .toCurrencyString(in.toInt());
145         case QVariant::UInt:
146             return m_locale .toCurrencyString(in.toUInt());
147         case QVariant::Double:
148             return m_locale .toCurrencyString(in.toDouble());
149         case QVariant::LongLong:
150             return m_locale .toCurrencyString(in.toLongLong());
151         case QVariant::ULongLong:
152             return m_locale .toCurrencyString(in.toULongLong());
153         default:
154             break;
155         }
156         return QString();
157     }
158     case StringToStandardQuotation:
159         return m_locale.quoteString(in.value<QStringRef>());
160     case StringToAlternateQuotation:
161         return m_locale.quoteString(in.value<QStringRef>(), QLocale::AlternateQuotation);
162     case ListToSeparatedString:
163         return m_locale.createSeparatedList(in.value<QStringList>());
164     case LocaleChanged:
165         Q_ASSERT_X(false, Q_FUNC_INFO, "This can't happen.");
166     case UILanguages: {
167         if (QtAndroidPrivate::androidSdkVersion() >= 24) {
168             QJNIObjectPrivate localeListObject =
169                 QJNIObjectPrivate::callStaticObjectMethod("android/os/LocaleList", "getDefault",
170                                                           "()Landroid/os/LocaleList;");
171             if (localeListObject.isValid()) {
172                 QString lang = localeListObject.callObjectMethod("toLanguageTags",
173                                                                  "()Ljava/lang/String;").toString();
174                 // Some devices return with it enclosed in []'s so check if both exists before
175                 // removing to ensure it is formatted correctly
176                 if (lang.startsWith(QChar('[')) && lang.endsWith(QChar(']')))
177                     lang = lang.mid(1, lang.length() - 2);
178                 return lang.split(QChar(','));
179             }
180         }
181         return QVariant();
182     }
183     default:
184         break;
185     }
186     return QVariant();
187 }
188 
fallbackUiLocale() const189 QLocale QAndroidSystemLocale::fallbackUiLocale() const
190 {
191     QReadLocker locker(&m_lock);
192     return m_locale;
193 }
194 
195 QT_END_NAMESPACE
196