blob: 9dc1cae331ec552292814da516668ccc211c0e88 [file] [log] [blame]
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Elliott Hughes757a7942010-04-16 14:14:28 -070017#define LOG_TAG "ICU"
Elliott Hughesc08f9fb2010-04-16 17:44:12 -070018
Elliott Hughes3aac4dd2013-02-04 15:37:52 -080019#include "IcuUtilities.h"
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080020#include "JNIHelp.h"
Elliott Hughesa9f5c162010-06-16 16:32:18 -070021#include "JniConstants.h"
Elliott Hughesbef9ec32011-04-19 10:26:58 -070022#include "JniException.h"
Elliott Hughes566bbb12011-02-11 18:23:35 -080023#include "ScopedFd.h"
Elliott Hughes4e3714f2010-06-22 14:32:47 -070024#include "ScopedJavaUnicodeString.h"
Elliott Hughesf2816672010-05-11 17:16:02 -070025#include "ScopedLocalRef.h"
Elliott Hughes9de899c2010-04-12 18:32:50 -070026#include "ScopedUtfChars.h"
Elliott Hughes757a7942010-04-16 14:14:28 -070027#include "UniquePtr.h"
Elliott Hughesf2d50622010-01-25 23:13:46 -080028#include "cutils/log.h"
Elliott Hughesddafeb12011-02-18 15:14:29 -080029#include "toStringArray.h"
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080030#include "unicode/calendar.h"
31#include "unicode/datefmt.h"
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080032#include "unicode/dcfmtsym.h"
Elliott Hughes94782d02010-06-15 17:58:00 -070033#include "unicode/decimfmt.h"
34#include "unicode/dtfmtsym.h"
Elliott Hughescb1b9022012-10-04 12:22:20 -070035#include "unicode/dtptngen.h"
Elliott Hughes94782d02010-06-15 17:58:00 -070036#include "unicode/gregocal.h"
37#include "unicode/locid.h"
38#include "unicode/numfmt.h"
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080039#include "unicode/strenum.h"
Elliott Hughes94782d02010-06-15 17:58:00 -070040#include "unicode/ubrk.h"
41#include "unicode/ucal.h"
42#include "unicode/uclean.h"
43#include "unicode/ucol.h"
44#include "unicode/ucurr.h"
45#include "unicode/udat.h"
Fabrice Di Meglio0452e9a2011-06-07 19:17:12 -070046#include "unicode/uloc.h"
Elliott Hughes15d82802013-02-27 12:25:47 -080047#include "unicode/ulocdata.h"
Elliott Hughes94782d02010-06-15 17:58:00 -070048#include "unicode/ustring.h"
claireho947eeb82010-06-03 14:10:30 -070049#include "ureslocs.h"
Elliott Hughes94782d02010-06-15 17:58:00 -070050#include "valueOf.h"
Elliott Hughesddafeb12011-02-18 15:14:29 -080051
Elliott Hughes566bbb12011-02-11 18:23:35 -080052#include <errno.h>
53#include <fcntl.h>
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080054#include <stdlib.h>
55#include <string.h>
Elliott Hughesddafeb12011-02-18 15:14:29 -080056#include <string>
Elliott Hughes566bbb12011-02-11 18:23:35 -080057#include <sys/mman.h>
58#include <sys/stat.h>
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080059#include <sys/time.h>
Elliott Hughes566bbb12011-02-11 18:23:35 -080060#include <sys/types.h>
61#include <time.h>
62#include <unistd.h>
Narayan Kamathc5b1eb12014-01-10 14:54:31 +000063#include <vector>
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080064
Elliott Hughesc4e07972012-08-15 09:27:14 -070065// TODO: put this in a header file and use it everywhere!
66// DISALLOW_COPY_AND_ASSIGN disallows the copy and operator= functions.
67// It goes in the private: declarations in a class.
68#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
69 TypeName(const TypeName&); \
70 void operator=(const TypeName&)
71
Elliott Hughes70fa1932009-12-21 16:36:28 -080072class ScopedResourceBundle {
Elliott Hughesc4e07972012-08-15 09:27:14 -070073 public:
74 ScopedResourceBundle(UResourceBundle* bundle) : bundle_(bundle) {
75 }
76
77 ~ScopedResourceBundle() {
78 if (bundle_ != NULL) {
79 ures_close(bundle_);
Elliott Hughes70fa1932009-12-21 16:36:28 -080080 }
Elliott Hughesc4e07972012-08-15 09:27:14 -070081 }
Elliott Hughes70fa1932009-12-21 16:36:28 -080082
Elliott Hughesc4e07972012-08-15 09:27:14 -070083 UResourceBundle* get() {
84 return bundle_;
85 }
Elliott Hughes70fa1932009-12-21 16:36:28 -080086
Elliott Hughesc4e07972012-08-15 09:27:14 -070087 bool hasKey(const char* key) {
88 UErrorCode status = U_ZERO_ERROR;
89 ures_getStringByKey(bundle_, key, NULL, &status);
90 return U_SUCCESS(status);
91 }
Elliott Hughes70fa1932009-12-21 16:36:28 -080092
Elliott Hughesc4e07972012-08-15 09:27:14 -070093 private:
94 UResourceBundle* bundle_;
95 DISALLOW_COPY_AND_ASSIGN(ScopedResourceBundle);
Elliott Hughes70fa1932009-12-21 16:36:28 -080096};
97
Fabrice Di Meglio0452e9a2011-06-07 19:17:12 -070098static jstring ICU_addLikelySubtags(JNIEnv* env, jclass, jstring javaLocale) {
99 UErrorCode status = U_ZERO_ERROR;
100 ScopedUtfChars localeID(env, javaLocale);
101 char maximizedLocaleID[ULOC_FULLNAME_CAPACITY];
102 uloc_addLikelySubtags(localeID.c_str(), maximizedLocaleID, sizeof(maximizedLocaleID), &status);
103 if (U_FAILURE(status)) {
104 return javaLocale;
105 }
106 return env->NewStringUTF(maximizedLocaleID);
107}
108
Fabrice Di Meglio4c94a612011-06-15 17:23:43 -0700109static jstring ICU_getScript(JNIEnv* env, jclass, jstring javaLocale) {
110 UErrorCode status = U_ZERO_ERROR;
111 ScopedUtfChars localeID(env, javaLocale);
112 char script[ULOC_SCRIPT_CAPACITY];
113 uloc_getScript(localeID.c_str(), script, sizeof(script), &status);
114 if (U_FAILURE(status)) {
115 return NULL;
116 }
117 return env->NewStringUTF(script);
118}
119
Narayan Kamathc5b1eb12014-01-10 14:54:31 +0000120static jstring ICU_localeForLanguageTag(JNIEnv* env, jclass, jstring languageTag, jboolean strict) {
121 ScopedUtfChars languageTagChars(env, languageTag);
122
123 // Naively assume that in the average case, the size of
124 // the normalized language tag will be very nearly the same as the
125 // size of the input. This is generally true for language
126 // tags that are "simple" language-region-variant combinations
127 // that don't contain any grandfathered tags.
128 const size_t initialBufferSize = languageTagChars.size() + 32;
129 std::vector<char> buffer(initialBufferSize);
130 int32_t parsedLength = 0;
131
132 UErrorCode status = U_ZERO_ERROR;
133 while (true) {
134 const size_t outputLength = uloc_forLanguageTag(languageTagChars.c_str(),
135 &buffer[0], buffer.size(), &parsedLength, &status);
136 if (U_FAILURE(status)) {
137 return NULL;
138 }
139
140 // Assume that we've run out of buffer space when this happens. Double
141 // the buffer size and try again. This should happen very infrequently.
142 if (outputLength == buffer.size()) {
143 buffer.resize(buffer.size() << 1);
144 } else {
145 break;
146 }
147 }
148
149 if (parsedLength < 0) {
150 return NULL;
151 }
152
153 // By default, ICU will ignore all subtags starting at the first unparseable
154 // or invalid subtag. Our "strict" mode is specified to throw an error if
155 // that happens.
156 //
157 // NOTE: The cast is safe because parsedLength can never be negative thanks
158 // to the check above. ICU does not document any negative return values for
159 // that field, but check for it anyway.
160 if ((strict == JNI_TRUE) &&
161 (static_cast<uint32_t>(parsedLength) != languageTagChars.size())) {
162 return NULL;
163 }
164
165 return env->NewStringUTF(&buffer[0]);
166}
167
168static jstring ICU_languageTagForLocale(JNIEnv* env, jclass, jstring javaLocaleId) {
169 ScopedUtfChars localeID(env, javaLocaleId);
170
Narayan Kamath11c1f6f2014-03-13 12:15:23 +0000171 // In most common cases, the BCP 47 tag will be the same size as the ICU
172 // locale ID
173 const size_t initialBufferSize = localeID.size() + 1;
Narayan Kamathc5b1eb12014-01-10 14:54:31 +0000174 std::vector<char> buffer(initialBufferSize);
175
176 UErrorCode status = U_ZERO_ERROR;
Narayan Kamath11c1f6f2014-03-13 12:15:23 +0000177 const size_t outputLength = uloc_toLanguageTag(localeID.c_str(),
178 &buffer[0], buffer.size(), false /* strict */, &status);
179 if (status == U_BUFFER_OVERFLOW_ERROR) {
180 buffer.resize(outputLength + 1);
181 status = U_ZERO_ERROR;
182 uloc_toLanguageTag(localeID.c_str(), &buffer[0], buffer.size(),
183 false /* strict */, &status);
184 }
Narayan Kamathc5b1eb12014-01-10 14:54:31 +0000185
Narayan Kamath11c1f6f2014-03-13 12:15:23 +0000186 if (status == U_STRING_NOT_TERMINATED_WARNING) {
187 buffer.resize(buffer.size() + 1);
188 buffer[buffer.size() -1] = '\0';
189 }
190
191 if (maybeThrowIcuException(env, "ICU::languageTagForLocale", status)) {
192 return NULL;
Narayan Kamathc5b1eb12014-01-10 14:54:31 +0000193 }
194
195 return env->NewStringUTF(&buffer[0]);
196}
197
Elliott Hughes52b310a2011-03-03 21:22:39 -0800198static jint ICU_getCurrencyFractionDigits(JNIEnv* env, jclass, jstring javaCurrencyCode) {
Elliott Hughes3aac4dd2013-02-04 15:37:52 -0800199 ScopedJavaUnicodeString currencyCode(env, javaCurrencyCode);
200 if (!currencyCode.valid()) {
201 return 0;
202 }
203 UnicodeString icuCurrencyCode(currencyCode.unicodeString());
204 UErrorCode status = U_ZERO_ERROR;
205 return ucurr_getDefaultFractionDigits(icuCurrencyCode.getTerminatedBuffer(), &status);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800206}
207
Neil Fullerd627bd42014-03-10 11:31:32 +0000208static jint ICU_getCurrencyNumericCode(JNIEnv* env, jclass, jstring javaCurrencyCode) {
209 ScopedJavaUnicodeString currencyCode(env, javaCurrencyCode);
210 if (!currencyCode.valid()) {
211 return 0;
212 }
213 UnicodeString icuCurrencyCode(currencyCode.unicodeString());
214 return ucurr_getNumericCode(icuCurrencyCode.getTerminatedBuffer());
215}
216
Elliott Hughesa49a1e82012-10-18 17:37:35 -0700217// TODO: rewrite this with int32_t ucurr_forLocale(const char* locale, UChar* buff, int32_t buffCapacity, UErrorCode* ec)...
Elliott Hughes52b310a2011-03-03 21:22:39 -0800218static jstring ICU_getCurrencyCode(JNIEnv* env, jclass, jstring javaCountryCode) {
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800219 UErrorCode status = U_ZERO_ERROR;
claireho947eeb82010-06-03 14:10:30 -0700220 ScopedResourceBundle supplData(ures_openDirect(U_ICUDATA_CURR, "supplementalData", &status));
Elliott Hughes70fa1932009-12-21 16:36:28 -0800221 if (U_FAILURE(status)) {
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800222 return NULL;
223 }
224
Elliott Hughes70fa1932009-12-21 16:36:28 -0800225 ScopedResourceBundle currencyMap(ures_getByKey(supplData.get(), "CurrencyMap", NULL, &status));
226 if (U_FAILURE(status)) {
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800227 return NULL;
228 }
229
Elliott Hughes52b310a2011-03-03 21:22:39 -0800230 ScopedUtfChars countryCode(env, javaCountryCode);
231 ScopedResourceBundle currency(ures_getByKey(currencyMap.get(), countryCode.c_str(), NULL, &status));
Elliott Hughes70fa1932009-12-21 16:36:28 -0800232 if (U_FAILURE(status)) {
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800233 return NULL;
234 }
235
Elliott Hughes70fa1932009-12-21 16:36:28 -0800236 ScopedResourceBundle currencyElem(ures_getByIndex(currency.get(), 0, NULL, &status));
237 if (U_FAILURE(status)) {
claireho5b7b7fe2012-01-12 14:51:08 -0800238 return env->NewStringUTF("XXX");
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800239 }
240
Elliott Hughes52b310a2011-03-03 21:22:39 -0800241 // Check if there's a 'to' date. If there is, the currency isn't used anymore.
Elliott Hughes67081ff2009-12-21 20:24:12 -0800242 ScopedResourceBundle currencyTo(ures_getByKey(currencyElem.get(), "to", NULL, &status));
243 if (!U_FAILURE(status)) {
Elliott Hughes67081ff2009-12-21 20:24:12 -0800244 return NULL;
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800245 }
Elliott Hughes52b310a2011-03-03 21:22:39 -0800246 // Ignore the failure to find a 'to' date.
Elliott Hughes67081ff2009-12-21 20:24:12 -0800247 status = U_ZERO_ERROR;
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800248
Elliott Hughes70fa1932009-12-21 16:36:28 -0800249 ScopedResourceBundle currencyId(ures_getByKey(currencyElem.get(), "id", NULL, &status));
250 if (U_FAILURE(status)) {
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800251 // No id defined for this country
claireho5b7b7fe2012-01-12 14:51:08 -0800252 return env->NewStringUTF("XXX");
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800253 }
254
Elliott Hughes52b310a2011-03-03 21:22:39 -0800255 int32_t charCount;
256 const jchar* chars = ures_getString(currencyId.get(), &charCount, &status);
claireho5b7b7fe2012-01-12 14:51:08 -0800257 return (charCount == 0) ? env->NewStringUTF("XXX") : env->NewString(chars, charCount);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800258}
259
Elliott Hughesa49a1e82012-10-18 17:37:35 -0700260static jstring getCurrencyName(JNIEnv* env, jstring javaLocaleName, jstring javaCurrencyCode, UCurrNameStyle nameStyle) {
261 ScopedUtfChars localeName(env, javaLocaleName);
Elliott Hughes3aac4dd2013-02-04 15:37:52 -0800262 if (localeName.c_str() == NULL) {
263 return NULL;
264 }
Elliott Hughesa49a1e82012-10-18 17:37:35 -0700265 ScopedJavaUnicodeString currencyCode(env, javaCurrencyCode);
Elliott Hughes3aac4dd2013-02-04 15:37:52 -0800266 if (!currencyCode.valid()) {
267 return NULL;
268 }
Elliott Hughesa49a1e82012-10-18 17:37:35 -0700269 UnicodeString icuCurrencyCode(currencyCode.unicodeString());
270 UErrorCode status = U_ZERO_ERROR;
271 UBool isChoiceFormat = false;
272 int32_t charCount;
273 const UChar* chars = ucurr_getName(icuCurrencyCode.getTerminatedBuffer(), localeName.c_str(),
274 nameStyle, &isChoiceFormat, &charCount, &status);
275 if (status == U_USING_DEFAULT_WARNING) {
276 if (nameStyle == UCURR_SYMBOL_NAME) {
277 // ICU doesn't distinguish between falling back to the root locale and meeting a genuinely
278 // unknown currency. The Currency class does.
279 if (!ucurr_isAvailable(icuCurrencyCode.getTerminatedBuffer(), U_DATE_MIN, U_DATE_MAX, &status)) {
280 return NULL;
281 }
Elliott Hughes52b310a2011-03-03 21:22:39 -0800282 }
Elliott Hughesa49a1e82012-10-18 17:37:35 -0700283 if (nameStyle == UCURR_LONG_NAME) {
284 // ICU's default is English. We want the ISO 4217 currency code instead.
285 chars = icuCurrencyCode.getBuffer();
286 charCount = icuCurrencyCode.length();
287 }
288 }
289 return (charCount == 0) ? NULL : env->NewString(chars, charCount);
Elliott Hughes52b310a2011-03-03 21:22:39 -0800290}
291
Elliott Hughesa49a1e82012-10-18 17:37:35 -0700292static jstring ICU_getCurrencyDisplayName(JNIEnv* env, jclass, jstring javaLocaleName, jstring javaCurrencyCode) {
293 return getCurrencyName(env, javaLocaleName, javaCurrencyCode, UCURR_LONG_NAME);
294}
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800295
Elliott Hughesa49a1e82012-10-18 17:37:35 -0700296static jstring ICU_getCurrencySymbol(JNIEnv* env, jclass, jstring javaLocaleName, jstring javaCurrencyCode) {
297 return getCurrencyName(env, javaLocaleName, javaCurrencyCode, UCURR_SYMBOL_NAME);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800298}
299
Elliott Hughese22935d2010-08-12 17:27:27 -0700300static jstring ICU_getDisplayCountryNative(JNIEnv* env, jclass, jstring targetLocale, jstring locale) {
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800301 Locale loc = getLocale(env, locale);
302 Locale targetLoc = getLocale(env, targetLocale);
Elliott Hughes70fa1932009-12-21 16:36:28 -0800303 UnicodeString str;
304 targetLoc.getDisplayCountry(loc, str);
305 return env->NewString(str.getBuffer(), str.length());
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800306}
307
Elliott Hughese22935d2010-08-12 17:27:27 -0700308static jstring ICU_getDisplayLanguageNative(JNIEnv* env, jclass, jstring targetLocale, jstring locale) {
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800309 Locale loc = getLocale(env, locale);
310 Locale targetLoc = getLocale(env, targetLocale);
Elliott Hughes70fa1932009-12-21 16:36:28 -0800311 UnicodeString str;
312 targetLoc.getDisplayLanguage(loc, str);
313 return env->NewString(str.getBuffer(), str.length());
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800314}
315
Narayan Kamathc5b1eb12014-01-10 14:54:31 +0000316static jstring ICU_getDisplayScriptNative(JNIEnv* env, jclass, jstring targetLocale, jstring locale) {
317 Locale loc = getLocale(env, locale);
318 Locale targetLoc = getLocale(env, targetLocale);
319 UnicodeString str;
320 targetLoc.getDisplayScript(loc, str);
321 return env->NewString(str.getBuffer(), str.length());
322}
323
Elliott Hughese22935d2010-08-12 17:27:27 -0700324static jstring ICU_getDisplayVariantNative(JNIEnv* env, jclass, jstring targetLocale, jstring locale) {
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800325 Locale loc = getLocale(env, locale);
326 Locale targetLoc = getLocale(env, targetLocale);
Elliott Hughes70fa1932009-12-21 16:36:28 -0800327 UnicodeString str;
328 targetLoc.getDisplayVariant(loc, str);
329 return env->NewString(str.getBuffer(), str.length());
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800330}
331
Elliott Hughese22935d2010-08-12 17:27:27 -0700332static jstring ICU_getISO3CountryNative(JNIEnv* env, jclass, jstring locale) {
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800333 Locale loc = getLocale(env, locale);
Elliott Hughes2e3a41d2009-12-21 10:52:53 -0800334 return env->NewStringUTF(loc.getISO3Country());
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800335}
336
Elliott Hughese22935d2010-08-12 17:27:27 -0700337static jstring ICU_getISO3LanguageNative(JNIEnv* env, jclass, jstring locale) {
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800338 Locale loc = getLocale(env, locale);
Elliott Hughes2e3a41d2009-12-21 10:52:53 -0800339 return env->NewStringUTF(loc.getISO3Language());
340}
341
Elliott Hughese22935d2010-08-12 17:27:27 -0700342static jobjectArray ICU_getISOCountriesNative(JNIEnv* env, jclass) {
Elliott Hughes2e3a41d2009-12-21 10:52:53 -0800343 return toStringArray(env, Locale::getISOCountries());
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800344}
345
Elliott Hughese22935d2010-08-12 17:27:27 -0700346static jobjectArray ICU_getISOLanguagesNative(JNIEnv* env, jclass) {
Elliott Hughes2e3a41d2009-12-21 10:52:53 -0800347 return toStringArray(env, Locale::getISOLanguages());
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800348}
349
Elliott Hughese22935d2010-08-12 17:27:27 -0700350static jobjectArray ICU_getAvailableLocalesNative(JNIEnv* env, jclass) {
Elliott Hughesddafeb12011-02-18 15:14:29 -0800351 return toStringArray(env, uloc_countAvailable, uloc_getAvailable);
Elliott Hughesf9157ea2010-04-01 13:56:32 -0700352}
353
Elliott Hughese22935d2010-08-12 17:27:27 -0700354static jobjectArray ICU_getAvailableBreakIteratorLocalesNative(JNIEnv* env, jclass) {
Elliott Hughesddafeb12011-02-18 15:14:29 -0800355 return toStringArray(env, ubrk_countAvailable, ubrk_getAvailable);
Elliott Hughesf9157ea2010-04-01 13:56:32 -0700356}
357
Elliott Hughese22935d2010-08-12 17:27:27 -0700358static jobjectArray ICU_getAvailableCalendarLocalesNative(JNIEnv* env, jclass) {
Elliott Hughesddafeb12011-02-18 15:14:29 -0800359 return toStringArray(env, ucal_countAvailable, ucal_getAvailable);
Elliott Hughesf9157ea2010-04-01 13:56:32 -0700360}
361
Elliott Hughese22935d2010-08-12 17:27:27 -0700362static jobjectArray ICU_getAvailableCollatorLocalesNative(JNIEnv* env, jclass) {
Elliott Hughesddafeb12011-02-18 15:14:29 -0800363 return toStringArray(env, ucol_countAvailable, ucol_getAvailable);
Elliott Hughesf9157ea2010-04-01 13:56:32 -0700364}
365
Elliott Hughese22935d2010-08-12 17:27:27 -0700366static jobjectArray ICU_getAvailableDateFormatLocalesNative(JNIEnv* env, jclass) {
Elliott Hughesddafeb12011-02-18 15:14:29 -0800367 return toStringArray(env, udat_countAvailable, udat_getAvailable);
Elliott Hughesf9157ea2010-04-01 13:56:32 -0700368}
369
Elliott Hughese22935d2010-08-12 17:27:27 -0700370static jobjectArray ICU_getAvailableNumberFormatLocalesNative(JNIEnv* env, jclass) {
Elliott Hughesddafeb12011-02-18 15:14:29 -0800371 return toStringArray(env, unum_countAvailable, unum_getAvailable);
Elliott Hughesf9157ea2010-04-01 13:56:32 -0700372}
373
Elliott Hughes33aa6eb2009-12-22 17:20:44 -0800374static void setIntegerField(JNIEnv* env, jobject obj, const char* fieldName, int value) {
Elliott Hughes94782d02010-06-15 17:58:00 -0700375 ScopedLocalRef<jobject> integerValue(env, integerValueOf(env, value));
Elliott Hughesa9f5c162010-06-16 16:32:18 -0700376 jfieldID fid = env->GetFieldID(JniConstants::localeDataClass, fieldName, "Ljava/lang/Integer;");
Elliott Hughes94782d02010-06-15 17:58:00 -0700377 env->SetObjectField(obj, fid, integerValue.get());
Elliott Hughes33aa6eb2009-12-22 17:20:44 -0800378}
379
380static void setStringField(JNIEnv* env, jobject obj, const char* fieldName, jstring value) {
Elliott Hughesa9f5c162010-06-16 16:32:18 -0700381 jfieldID fid = env->GetFieldID(JniConstants::localeDataClass, fieldName, "Ljava/lang/String;");
Elliott Hughes33aa6eb2009-12-22 17:20:44 -0800382 env->SetObjectField(obj, fid, value);
Elliott Hughese2377cd2011-06-15 12:59:02 -0700383 env->DeleteLocalRef(value);
Elliott Hughes33aa6eb2009-12-22 17:20:44 -0800384}
385
386static void setStringArrayField(JNIEnv* env, jobject obj, const char* fieldName, jobjectArray value) {
Elliott Hughesa9f5c162010-06-16 16:32:18 -0700387 jfieldID fid = env->GetFieldID(JniConstants::localeDataClass, fieldName, "[Ljava/lang/String;");
Elliott Hughes33aa6eb2009-12-22 17:20:44 -0800388 env->SetObjectField(obj, fid, value);
389}
390
claireho5b7b7fe2012-01-12 14:51:08 -0800391static void setStringArrayField(JNIEnv* env, jobject obj, const char* fieldName, const UnicodeString* valueArray, int32_t size) {
392 ScopedLocalRef<jobjectArray> result(env, env->NewObjectArray(size, JniConstants::stringClass, NULL));
393 for (int32_t i = 0; i < size ; i++) {
394 ScopedLocalRef<jstring> s(env, env->NewString(valueArray[i].getBuffer(),valueArray[i].length()));
395 if (env->ExceptionCheck()) {
396 return;
397 }
398 env->SetObjectArrayElement(result.get(), i, s.get());
399 if (env->ExceptionCheck()) {
400 return;
401 }
402 }
403 setStringArrayField(env, obj, fieldName, result.get());
404}
405
Elliott Hughes33aa6eb2009-12-22 17:20:44 -0800406static void setStringField(JNIEnv* env, jobject obj, const char* fieldName, UResourceBundle* bundle, int index) {
Elliott Hughesc4e07972012-08-15 09:27:14 -0700407 UErrorCode status = U_ZERO_ERROR;
408 int charCount;
409 const UChar* chars = ures_getStringByIndex(bundle, index, &charCount, &status);
410 if (U_SUCCESS(status)) {
411 setStringField(env, obj, fieldName, env->NewString(chars, charCount));
412 } else {
413 ALOGE("Error setting String field %s from ICU resource (index %d): %s", fieldName, index, u_errorName(status));
414 }
415}
416
417static void setStringField(JNIEnv* env, jobject obj, const char* fieldName, UResourceBundle* bundle, const char* key) {
418 UErrorCode status = U_ZERO_ERROR;
419 int charCount;
420 const UChar* chars = ures_getStringByKey(bundle, key, &charCount, &status);
421 if (U_SUCCESS(status)) {
422 setStringField(env, obj, fieldName, env->NewString(chars, charCount));
423 } else {
424 ALOGE("Error setting String field %s from ICU resource (key %s): %s", fieldName, key, u_errorName(status));
425 }
Elliott Hughesb7e820b2010-04-16 11:06:58 -0700426}
427
claireho5b7b7fe2012-01-12 14:51:08 -0800428static void setCharField(JNIEnv* env, jobject obj, const char* fieldName, const UnicodeString& value) {
429 if (value.length() == 0) {
claireho3be12772011-06-09 10:53:06 -0700430 return;
431 }
claireho5b7b7fe2012-01-12 14:51:08 -0800432 jfieldID fid = env->GetFieldID(JniConstants::localeDataClass, fieldName, "C");
433 env->SetCharField(obj, fid, value.charAt(0));
434}
claireho3be12772011-06-09 10:53:06 -0700435
claireho5b7b7fe2012-01-12 14:51:08 -0800436static void setStringField(JNIEnv* env, jobject obj, const char* fieldName, const UnicodeString& value) {
437 const UChar* chars = value.getBuffer();
438 setStringField(env, obj, fieldName, env->NewString(chars, value.length()));
439}
claireho3be12772011-06-09 10:53:06 -0700440
Elliott Hughes007776e2012-11-02 14:54:02 -0700441static void setNumberPatterns(JNIEnv* env, jobject obj, Locale& locale) {
claireho5b7b7fe2012-01-12 14:51:08 -0800442 UErrorCode status = U_ZERO_ERROR;
claireho5b7b7fe2012-01-12 14:51:08 -0800443
444 UnicodeString pattern;
Elliott Hughes007776e2012-11-02 14:54:02 -0700445 UniquePtr<DecimalFormat> fmt(static_cast<DecimalFormat*>(NumberFormat::createInstance(locale, UNUM_CURRENCY, status)));
claireho5b7b7fe2012-01-12 14:51:08 -0800446 pattern = fmt->toPattern(pattern.remove());
447 setStringField(env, obj, "currencyPattern", pattern);
448
Elliott Hughes007776e2012-11-02 14:54:02 -0700449 fmt.reset(static_cast<DecimalFormat*>(NumberFormat::createInstance(locale, UNUM_DECIMAL, status)));
claireho5b7b7fe2012-01-12 14:51:08 -0800450 pattern = fmt->toPattern(pattern.remove());
451 setStringField(env, obj, "numberPattern", pattern);
452
Elliott Hughes007776e2012-11-02 14:54:02 -0700453 fmt.reset(static_cast<DecimalFormat*>(NumberFormat::createInstance(locale, UNUM_PERCENT, status)));
claireho5b7b7fe2012-01-12 14:51:08 -0800454 pattern = fmt->toPattern(pattern.remove());
455 setStringField(env, obj, "percentPattern", pattern);
456}
457
Elliott Hughes007776e2012-11-02 14:54:02 -0700458static void setDecimalFormatSymbolsData(JNIEnv* env, jobject obj, Locale& locale) {
claireho5b7b7fe2012-01-12 14:51:08 -0800459 UErrorCode status = U_ZERO_ERROR;
Elliott Hughes007776e2012-11-02 14:54:02 -0700460 DecimalFormatSymbols dfs(locale, status);
claireho5b7b7fe2012-01-12 14:51:08 -0800461
462 setCharField(env, obj, "decimalSeparator", dfs.getSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol));
463 setCharField(env, obj, "groupingSeparator", dfs.getSymbol(DecimalFormatSymbols::kGroupingSeparatorSymbol));
464 setCharField(env, obj, "patternSeparator", dfs.getSymbol(DecimalFormatSymbols::kPatternSeparatorSymbol));
465 setCharField(env, obj, "percent", dfs.getSymbol(DecimalFormatSymbols::kPercentSymbol));
466 setCharField(env, obj, "perMill", dfs.getSymbol(DecimalFormatSymbols::kPerMillSymbol));
467 setCharField(env, obj, "monetarySeparator", dfs.getSymbol(DecimalFormatSymbols::kMonetarySeparatorSymbol));
Narayan Kamath4f3bca72014-03-13 16:42:27 +0000468 setStringField(env, obj, "minusSign", dfs.getSymbol(DecimalFormatSymbols:: kMinusSignSymbol));
claireho5b7b7fe2012-01-12 14:51:08 -0800469 setStringField(env, obj, "exponentSeparator", dfs.getSymbol(DecimalFormatSymbols::kExponentialSymbol));
470 setStringField(env, obj, "infinity", dfs.getSymbol(DecimalFormatSymbols::kInfinitySymbol));
471 setStringField(env, obj, "NaN", dfs.getSymbol(DecimalFormatSymbols::kNaNSymbol));
472 setCharField(env, obj, "zeroDigit", dfs.getSymbol(DecimalFormatSymbols::kZeroDigitSymbol));
Elliott Hughes33aa6eb2009-12-22 17:20:44 -0800473}
474
Elliott Hughesc4e07972012-08-15 09:27:14 -0700475
476// Iterates up through the locale hierarchy. So "en_US" would return "en_US", "en", "".
477class LocaleNameIterator {
478 public:
479 LocaleNameIterator(const char* locale_name, UErrorCode& status) : status_(status), has_next_(true) {
480 strcpy(locale_name_, locale_name);
481 locale_name_length_ = strlen(locale_name_);
482 }
483
484 const char* Get() {
485 return locale_name_;
486 }
487
488 bool HasNext() {
489 return has_next_;
490 }
491
492 void Up() {
Elliott Hughesc4e07972012-08-15 09:27:14 -0700493 if (locale_name_length_ == 0) {
494 has_next_ = false;
Elliott Hughesf001abe2013-07-24 17:52:54 -0700495 } else {
496 locale_name_length_ = uloc_getParent(locale_name_, locale_name_, sizeof(locale_name_), &status_);
Elliott Hughesc4e07972012-08-15 09:27:14 -0700497 }
498 }
499
500 private:
501 UErrorCode& status_;
502 bool has_next_;
503 char locale_name_[ULOC_FULLNAME_CAPACITY];
504 int32_t locale_name_length_;
505
506 DISALLOW_COPY_AND_ASSIGN(LocaleNameIterator);
507};
508
509static bool getDateTimePatterns(JNIEnv* env, jobject localeData, const char* locale_name) {
510 UErrorCode status = U_ZERO_ERROR;
511 ScopedResourceBundle root(ures_open(NULL, locale_name, &status));
512 if (U_FAILURE(status)) {
513 return false;
514 }
515 ScopedResourceBundle calendar(ures_getByKey(root.get(), "calendar", NULL, &status));
516 if (U_FAILURE(status)) {
517 return false;
518 }
519 ScopedResourceBundle gregorian(ures_getByKey(calendar.get(), "gregorian", NULL, &status));
520 if (U_FAILURE(status)) {
521 return false;
522 }
523 ScopedResourceBundle dateTimePatterns(ures_getByKey(gregorian.get(), "DateTimePatterns", NULL, &status));
524 if (U_FAILURE(status)) {
525 return false;
526 }
527 setStringField(env, localeData, "fullTimeFormat", dateTimePatterns.get(), 0);
528 setStringField(env, localeData, "longTimeFormat", dateTimePatterns.get(), 1);
529 setStringField(env, localeData, "mediumTimeFormat", dateTimePatterns.get(), 2);
530 setStringField(env, localeData, "shortTimeFormat", dateTimePatterns.get(), 3);
531 setStringField(env, localeData, "fullDateFormat", dateTimePatterns.get(), 4);
532 setStringField(env, localeData, "longDateFormat", dateTimePatterns.get(), 5);
533 setStringField(env, localeData, "mediumDateFormat", dateTimePatterns.get(), 6);
534 setStringField(env, localeData, "shortDateFormat", dateTimePatterns.get(), 7);
535 return true;
536}
537
538static bool getYesterdayTodayAndTomorrow(JNIEnv* env, jobject localeData, const char* locale_name) {
539 UErrorCode status = U_ZERO_ERROR;
540 ScopedResourceBundle root(ures_open(NULL, locale_name, &status));
541 if (U_FAILURE(status)) {
542 return false;
543 }
Elliott Hughes0ef99442013-06-19 14:53:18 -0700544 ScopedResourceBundle fields(ures_getByKey(root.get(), "fields", NULL, &status));
Elliott Hughesc4e07972012-08-15 09:27:14 -0700545 if (U_FAILURE(status)) {
546 return false;
547 }
548 ScopedResourceBundle day(ures_getByKey(fields.get(), "day", NULL, &status));
549 if (U_FAILURE(status)) {
550 return false;
551 }
552 ScopedResourceBundle relative(ures_getByKey(day.get(), "relative", NULL, &status));
553 if (U_FAILURE(status)) {
554 return false;
555 }
556 // bn_BD only has a "-2" entry.
557 if (relative.hasKey("-1") && relative.hasKey("0") && relative.hasKey("1")) {
558 setStringField(env, localeData, "yesterday", relative.get(), "-1");
559 setStringField(env, localeData, "today", relative.get(), "0");
560 setStringField(env, localeData, "tomorrow", relative.get(), "1");
561 return true;
562 }
563 return false;
564}
565
Elliott Hughes0fd776f2013-10-31 10:14:28 -0700566static jboolean ICU_initLocaleDataNative(JNIEnv* env, jclass, jstring javaLocaleName, jobject localeData) {
Elliott Hughes007776e2012-11-02 14:54:02 -0700567 ScopedUtfChars localeName(env, javaLocaleName);
Elliott Hughese2377cd2011-06-15 12:59:02 -0700568 if (localeName.c_str() == NULL) {
569 return JNI_FALSE;
570 }
claireho5b7b7fe2012-01-12 14:51:08 -0800571 if (localeName.size() >= ULOC_FULLNAME_CAPACITY) {
Elliott Hughesc4e07972012-08-15 09:27:14 -0700572 return JNI_FALSE; // ICU has a fixed-length limit.
claireho5b7b7fe2012-01-12 14:51:08 -0800573 }
claireho5b7b7fe2012-01-12 14:51:08 -0800574
Elliott Hughesc4e07972012-08-15 09:27:14 -0700575 // Get the DateTimePatterns.
576 UErrorCode status = U_ZERO_ERROR;
577 bool foundDateTimePatterns = false;
578 for (LocaleNameIterator it(localeName.c_str(), status); it.HasNext(); it.Up()) {
579 if (getDateTimePatterns(env, localeData, it.Get())) {
580 foundDateTimePatterns = true;
581 break;
582 }
583 }
584 if (!foundDateTimePatterns) {
585 ALOGE("Couldn't find ICU DateTimePatterns for %s", localeName.c_str());
Elliott Hughes33aa6eb2009-12-22 17:20:44 -0800586 return JNI_FALSE;
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800587 }
588
Elliott Hughesc4e07972012-08-15 09:27:14 -0700589 // Get the "Yesterday", "Today", and "Tomorrow" strings.
590 bool foundYesterdayTodayAndTomorrow = false;
591 for (LocaleNameIterator it(localeName.c_str(), status); it.HasNext(); it.Up()) {
592 if (getYesterdayTodayAndTomorrow(env, localeData, it.Get())) {
593 foundYesterdayTodayAndTomorrow = true;
594 break;
595 }
596 }
597 if (!foundYesterdayTodayAndTomorrow) {
Elliott Hughes680b1e22012-08-15 16:49:05 -0700598 ALOGE("Couldn't find ICU yesterday/today/tomorrow for %s", localeName.c_str());
599 return JNI_FALSE;
Elliott Hughesc4e07972012-08-15 09:27:14 -0700600 }
601
claireho5b7b7fe2012-01-12 14:51:08 -0800602 status = U_ZERO_ERROR;
Elliott Hughes783112d2013-09-03 15:28:43 -0700603 Locale locale = getLocale(env, javaLocaleName);
Elliott Hughes007776e2012-11-02 14:54:02 -0700604 UniquePtr<Calendar> cal(Calendar::createInstance(locale, status));
Elliott Hughes33aa6eb2009-12-22 17:20:44 -0800605 if (U_FAILURE(status)) {
606 return JNI_FALSE;
607 }
Elliott Hughesc4e07972012-08-15 09:27:14 -0700608
claireho5b7b7fe2012-01-12 14:51:08 -0800609 setIntegerField(env, localeData, "firstDayOfWeek", cal->getFirstDayOfWeek());
610 setIntegerField(env, localeData, "minimalDaysInFirstWeek", cal->getMinimalDaysInFirstWeek());
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800611
Elliott Hughesc4e07972012-08-15 09:27:14 -0700612 // Get DateFormatSymbols.
claireho5b7b7fe2012-01-12 14:51:08 -0800613 status = U_ZERO_ERROR;
Elliott Hughes007776e2012-11-02 14:54:02 -0700614 DateFormatSymbols dateFormatSym(locale, status);
Elliott Hughes33aa6eb2009-12-22 17:20:44 -0800615 if (U_FAILURE(status)) {
616 return JNI_FALSE;
617 }
Elliott Hughesc4e07972012-08-15 09:27:14 -0700618
619 // Get AM/PM and BC/AD.
claireho5b7b7fe2012-01-12 14:51:08 -0800620 int32_t count = 0;
claireho5b7b7fe2012-01-12 14:51:08 -0800621 const UnicodeString* amPmStrs = dateFormatSym.getAmPmStrings(count);
622 setStringArrayField(env, localeData, "amPm", amPmStrs, count);
623 const UnicodeString* erasStrs = dateFormatSym.getEras(count);
624 setStringArrayField(env, localeData, "eras", erasStrs, count);
Elliott Hughes33aa6eb2009-12-22 17:20:44 -0800625
claireho5b7b7fe2012-01-12 14:51:08 -0800626 const UnicodeString* longMonthNames =
627 dateFormatSym.getMonths(count, DateFormatSymbols::FORMAT, DateFormatSymbols::WIDE);
628 setStringArrayField(env, localeData, "longMonthNames", longMonthNames, count);
629 const UnicodeString* shortMonthNames =
630 dateFormatSym.getMonths(count, DateFormatSymbols::FORMAT, DateFormatSymbols::ABBREVIATED);
631 setStringArrayField(env, localeData, "shortMonthNames", shortMonthNames, count);
Elliott Hughesad66a882012-08-06 10:47:32 -0700632 const UnicodeString* tinyMonthNames =
633 dateFormatSym.getMonths(count, DateFormatSymbols::FORMAT, DateFormatSymbols::NARROW);
634 setStringArrayField(env, localeData, "tinyMonthNames", tinyMonthNames, count);
claireho5b7b7fe2012-01-12 14:51:08 -0800635 const UnicodeString* longWeekdayNames =
636 dateFormatSym.getWeekdays(count, DateFormatSymbols::FORMAT, DateFormatSymbols::WIDE);
637 setStringArrayField(env, localeData, "longWeekdayNames", longWeekdayNames, count);
638 const UnicodeString* shortWeekdayNames =
639 dateFormatSym.getWeekdays(count, DateFormatSymbols::FORMAT, DateFormatSymbols::ABBREVIATED);
640 setStringArrayField(env, localeData, "shortWeekdayNames", shortWeekdayNames, count);
Elliott Hughesad66a882012-08-06 10:47:32 -0700641 const UnicodeString* tinyWeekdayNames =
642 dateFormatSym.getWeekdays(count, DateFormatSymbols::FORMAT, DateFormatSymbols::NARROW);
643 setStringArrayField(env, localeData, "tinyWeekdayNames", tinyWeekdayNames, count);
Elliott Hughes33aa6eb2009-12-22 17:20:44 -0800644
claireho5b7b7fe2012-01-12 14:51:08 -0800645 const UnicodeString* longStandAloneMonthNames =
646 dateFormatSym.getMonths(count, DateFormatSymbols::STANDALONE, DateFormatSymbols::WIDE);
647 setStringArrayField(env, localeData, "longStandAloneMonthNames", longStandAloneMonthNames, count);
648 const UnicodeString* shortStandAloneMonthNames =
649 dateFormatSym.getMonths(count, DateFormatSymbols::STANDALONE, DateFormatSymbols::ABBREVIATED);
650 setStringArrayField(env, localeData, "shortStandAloneMonthNames", shortStandAloneMonthNames, count);
Elliott Hughesad66a882012-08-06 10:47:32 -0700651 const UnicodeString* tinyStandAloneMonthNames =
652 dateFormatSym.getMonths(count, DateFormatSymbols::STANDALONE, DateFormatSymbols::NARROW);
653 setStringArrayField(env, localeData, "tinyStandAloneMonthNames", tinyStandAloneMonthNames, count);
claireho5b7b7fe2012-01-12 14:51:08 -0800654 const UnicodeString* longStandAloneWeekdayNames =
655 dateFormatSym.getWeekdays(count, DateFormatSymbols::STANDALONE, DateFormatSymbols::WIDE);
656 setStringArrayField(env, localeData, "longStandAloneWeekdayNames", longStandAloneWeekdayNames, count);
657 const UnicodeString* shortStandAloneWeekdayNames =
658 dateFormatSym.getWeekdays(count, DateFormatSymbols::STANDALONE, DateFormatSymbols::ABBREVIATED);
659 setStringArrayField(env, localeData, "shortStandAloneWeekdayNames", shortStandAloneWeekdayNames, count);
Elliott Hughesad66a882012-08-06 10:47:32 -0700660 const UnicodeString* tinyStandAloneWeekdayNames =
661 dateFormatSym.getWeekdays(count, DateFormatSymbols::STANDALONE, DateFormatSymbols::NARROW);
662 setStringArrayField(env, localeData, "tinyStandAloneWeekdayNames", tinyStandAloneWeekdayNames, count);
Elliott Hughese2377cd2011-06-15 12:59:02 -0700663
Elliott Hughes33aa6eb2009-12-22 17:20:44 -0800664 status = U_ZERO_ERROR;
665
claireho3be12772011-06-09 10:53:06 -0700666 // For numberPatterns and symbols.
claireho5b7b7fe2012-01-12 14:51:08 -0800667 setNumberPatterns(env, localeData, locale);
668 setDecimalFormatSymbolsData(env, localeData, locale);
Elliott Hughes33aa6eb2009-12-22 17:20:44 -0800669
Elliott Hughes7ff70102010-11-03 11:30:20 -0700670 jstring countryCode = env->NewStringUTF(Locale::createFromName(localeName.c_str()).getCountry());
Elliott Hughes52b310a2011-03-03 21:22:39 -0800671 jstring internationalCurrencySymbol = ICU_getCurrencyCode(env, NULL, countryCode);
Elliott Hughese2377cd2011-06-15 12:59:02 -0700672 env->DeleteLocalRef(countryCode);
673 countryCode = NULL;
674
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800675 jstring currencySymbol = NULL;
Elliott Hughes33aa6eb2009-12-22 17:20:44 -0800676 if (internationalCurrencySymbol != NULL) {
Elliott Hughes007776e2012-11-02 14:54:02 -0700677 currencySymbol = ICU_getCurrencySymbol(env, NULL, javaLocaleName, internationalCurrencySymbol);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800678 } else {
Elliott Hughes33aa6eb2009-12-22 17:20:44 -0800679 internationalCurrencySymbol = env->NewStringUTF("XXX");
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800680 }
Elliott Hughes33aa6eb2009-12-22 17:20:44 -0800681 if (currencySymbol == NULL) {
682 // This is the UTF-8 encoding of U+00A4 (CURRENCY SIGN).
Hao Fengad884a72009-03-20 16:15:16 +0800683 currencySymbol = env->NewStringUTF("\xc2\xa4");
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800684 }
Elliott Hughes33aa6eb2009-12-22 17:20:44 -0800685 setStringField(env, localeData, "currencySymbol", currencySymbol);
686 setStringField(env, localeData, "internationalCurrencySymbol", internationalCurrencySymbol);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800687
Elliott Hughes33aa6eb2009-12-22 17:20:44 -0800688 return JNI_TRUE;
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800689}
690
Elliott Hughesdbbdffc2010-07-22 18:46:47 -0700691static jstring ICU_toLowerCase(JNIEnv* env, jclass, jstring javaString, jstring localeName) {
Elliott Hughes3aac4dd2013-02-04 15:37:52 -0800692 ScopedJavaUnicodeString scopedString(env, javaString);
693 if (!scopedString.valid()) {
694 return NULL;
695 }
696 UnicodeString& s(scopedString.unicodeString());
697 UnicodeString original(s);
Elliott Hughes8a491f82013-09-03 15:04:38 -0700698 s.toLower(getLocale(env, localeName));
Elliott Hughes3aac4dd2013-02-04 15:37:52 -0800699 return s == original ? javaString : env->NewString(s.getBuffer(), s.length());
Elliott Hughesdbbdffc2010-07-22 18:46:47 -0700700}
701
702static jstring ICU_toUpperCase(JNIEnv* env, jclass, jstring javaString, jstring localeName) {
Elliott Hughes3aac4dd2013-02-04 15:37:52 -0800703 ScopedJavaUnicodeString scopedString(env, javaString);
704 if (!scopedString.valid()) {
705 return NULL;
706 }
707 UnicodeString& s(scopedString.unicodeString());
708 UnicodeString original(s);
Elliott Hughes8a491f82013-09-03 15:04:38 -0700709 s.toUpper(getLocale(env, localeName));
Elliott Hughes3aac4dd2013-02-04 15:37:52 -0800710 return s == original ? javaString : env->NewString(s.getBuffer(), s.length());
Elliott Hughesdbbdffc2010-07-22 18:46:47 -0700711}
712
Elliott Hughese0e56722011-02-10 15:18:33 -0800713static jstring versionString(JNIEnv* env, const UVersionInfo& version) {
714 char versionString[U_MAX_VERSION_STRING_LENGTH];
715 u_versionToString(const_cast<UVersionInfo&>(version), &versionString[0]);
716 return env->NewStringUTF(versionString);
717}
718
Elliott Hughes15d82802013-02-27 12:25:47 -0800719static jstring ICU_getCldrVersion(JNIEnv* env, jclass) {
720 UErrorCode status = U_ZERO_ERROR;
721 UVersionInfo cldrVersion;
722 ulocdata_getCLDRVersion(cldrVersion, &status);
723 return versionString(env, cldrVersion);
724}
725
Elliott Hughese0e56722011-02-10 15:18:33 -0800726static jstring ICU_getIcuVersion(JNIEnv* env, jclass) {
727 UVersionInfo icuVersion;
728 u_getVersion(icuVersion);
729 return versionString(env, icuVersion);
730}
731
732static jstring ICU_getUnicodeVersion(JNIEnv* env, jclass) {
733 UVersionInfo unicodeVersion;
734 u_getUnicodeVersion(unicodeVersion);
735 return versionString(env, unicodeVersion);
736}
737
Elliott Hughes52b310a2011-03-03 21:22:39 -0800738static jobject ICU_getAvailableCurrencyCodes(JNIEnv* env, jclass) {
Elliott Hughes783a57a2013-08-26 14:59:01 -0700739 UErrorCode status = U_ZERO_ERROR;
740 UStringEnumeration e(ucurr_openISOCurrencies(UCURR_COMMON|UCURR_NON_DEPRECATED, &status));
741 return fromStringEnumeration(env, status, "ucurr_openISOCurrencies", &e);
Elliott Hughes52b310a2011-03-03 21:22:39 -0800742}
743
Elliott Hughes0fd776f2013-10-31 10:14:28 -0700744static jstring ICU_getBestDateTimePatternNative(JNIEnv* env, jclass, jstring javaSkeleton, jstring javaLocaleName) {
Elliott Hughescb1b9022012-10-04 12:22:20 -0700745 Locale locale = getLocale(env, javaLocaleName);
746 UErrorCode status = U_ZERO_ERROR;
Carton He364324d2013-09-02 17:19:28 +0800747 UniquePtr<DateTimePatternGenerator> generator(DateTimePatternGenerator::createInstance(locale, status));
Elliott Hughescb1b9022012-10-04 12:22:20 -0700748 if (maybeThrowIcuException(env, "DateTimePatternGenerator::createInstance", status)) {
749 return NULL;
750 }
751
Elliott Hughes480537d2013-05-03 13:07:18 -0700752 ScopedJavaUnicodeString skeletonHolder(env, javaSkeleton);
753 if (!skeletonHolder.valid()) {
Elliott Hughes3aac4dd2013-02-04 15:37:52 -0800754 return NULL;
755 }
Elliott Hughes480537d2013-05-03 13:07:18 -0700756 UnicodeString result(generator->getBestPattern(skeletonHolder.unicodeString(), status));
Elliott Hughescb1b9022012-10-04 12:22:20 -0700757 if (maybeThrowIcuException(env, "DateTimePatternGenerator::getBestPattern", status)) {
758 return NULL;
759 }
760
761 return env->NewString(result.getBuffer(), result.length());
762}
763
Narayan Kamathde0eb682014-04-11 14:30:59 +0100764static void ICU_setDefaultLocale(JNIEnv* env, jclass, jstring javaLocaleName) {
765 Locale locale = getLocale(env, javaLocaleName);
766 UErrorCode status = U_ZERO_ERROR;
767
768 // TODO: Should we check whether locale.isBogus() here ? ICU will
769 // accept bogus locales as the default without complaint. It
770 // shouldn't make a difference in practice, users that set a bogus
771 // locale as the default shouldn't have any realistic expectation that
772 // things like defaults etc. will work correctly.
773 Locale::setDefault(locale, status);
774 maybeThrowIcuException(env, "Locale::setDefault", status);
775}
776
777static jstring ICU_getDefaultLocale(JNIEnv* env, jclass) {
778 return env->NewStringUTF(Locale::getDefault().getName());
779}
780
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800781static JNINativeMethod gMethods[] = {
Fabrice Di Meglio0452e9a2011-06-07 19:17:12 -0700782 NATIVE_METHOD(ICU, addLikelySubtags, "(Ljava/lang/String;)Ljava/lang/String;"),
Elliott Hughese22935d2010-08-12 17:27:27 -0700783 NATIVE_METHOD(ICU, getAvailableBreakIteratorLocalesNative, "()[Ljava/lang/String;"),
784 NATIVE_METHOD(ICU, getAvailableCalendarLocalesNative, "()[Ljava/lang/String;"),
785 NATIVE_METHOD(ICU, getAvailableCollatorLocalesNative, "()[Ljava/lang/String;"),
Elliott Hughes52b310a2011-03-03 21:22:39 -0800786 NATIVE_METHOD(ICU, getAvailableCurrencyCodes, "()[Ljava/lang/String;"),
Elliott Hughese22935d2010-08-12 17:27:27 -0700787 NATIVE_METHOD(ICU, getAvailableDateFormatLocalesNative, "()[Ljava/lang/String;"),
788 NATIVE_METHOD(ICU, getAvailableLocalesNative, "()[Ljava/lang/String;"),
789 NATIVE_METHOD(ICU, getAvailableNumberFormatLocalesNative, "()[Ljava/lang/String;"),
Elliott Hughes0fd776f2013-10-31 10:14:28 -0700790 NATIVE_METHOD(ICU, getBestDateTimePatternNative, "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"),
Elliott Hughes15d82802013-02-27 12:25:47 -0800791 NATIVE_METHOD(ICU, getCldrVersion, "()Ljava/lang/String;"),
Elliott Hughes52b310a2011-03-03 21:22:39 -0800792 NATIVE_METHOD(ICU, getCurrencyCode, "(Ljava/lang/String;)Ljava/lang/String;"),
793 NATIVE_METHOD(ICU, getCurrencyDisplayName, "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"),
794 NATIVE_METHOD(ICU, getCurrencyFractionDigits, "(Ljava/lang/String;)I"),
Neil Fullerd627bd42014-03-10 11:31:32 +0000795 NATIVE_METHOD(ICU, getCurrencyNumericCode, "(Ljava/lang/String;)I"),
Elliott Hughes52b310a2011-03-03 21:22:39 -0800796 NATIVE_METHOD(ICU, getCurrencySymbol, "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"),
Narayan Kamathde0eb682014-04-11 14:30:59 +0100797 NATIVE_METHOD(ICU, getDefaultLocale, "()Ljava/lang/String;"),
Elliott Hughese22935d2010-08-12 17:27:27 -0700798 NATIVE_METHOD(ICU, getDisplayCountryNative, "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"),
799 NATIVE_METHOD(ICU, getDisplayLanguageNative, "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"),
Narayan Kamathc5b1eb12014-01-10 14:54:31 +0000800 NATIVE_METHOD(ICU, getDisplayScriptNative, "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"),
Elliott Hughese22935d2010-08-12 17:27:27 -0700801 NATIVE_METHOD(ICU, getDisplayVariantNative, "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"),
802 NATIVE_METHOD(ICU, getISO3CountryNative, "(Ljava/lang/String;)Ljava/lang/String;"),
803 NATIVE_METHOD(ICU, getISO3LanguageNative, "(Ljava/lang/String;)Ljava/lang/String;"),
804 NATIVE_METHOD(ICU, getISOCountriesNative, "()[Ljava/lang/String;"),
805 NATIVE_METHOD(ICU, getISOLanguagesNative, "()[Ljava/lang/String;"),
Elliott Hughese0e56722011-02-10 15:18:33 -0800806 NATIVE_METHOD(ICU, getIcuVersion, "()Ljava/lang/String;"),
Fabrice Di Meglio4c94a612011-06-15 17:23:43 -0700807 NATIVE_METHOD(ICU, getScript, "(Ljava/lang/String;)Ljava/lang/String;"),
Elliott Hughese0e56722011-02-10 15:18:33 -0800808 NATIVE_METHOD(ICU, getUnicodeVersion, "()Ljava/lang/String;"),
Narayan Kamathc5b1eb12014-01-10 14:54:31 +0000809 NATIVE_METHOD(ICU, languageTagForLocale, "(Ljava/lang/String;)Ljava/lang/String;"),
810 NATIVE_METHOD(ICU, localeForLanguageTag, "(Ljava/lang/String;Z)Ljava/lang/String;"),
Elliott Hughes0fd776f2013-10-31 10:14:28 -0700811 NATIVE_METHOD(ICU, initLocaleDataNative, "(Ljava/lang/String;Llibcore/icu/LocaleData;)Z"),
Narayan Kamathde0eb682014-04-11 14:30:59 +0100812 NATIVE_METHOD(ICU, setDefaultLocale, "(Ljava/lang/String;)V"),
Elliott Hughese22935d2010-08-12 17:27:27 -0700813 NATIVE_METHOD(ICU, toLowerCase, "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"),
814 NATIVE_METHOD(ICU, toUpperCase, "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"),
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800815};
Elliott Hughes7cd67602012-05-03 17:21:04 -0700816void register_libcore_icu_ICU(JNIEnv* env) {
Elliott Hughes566bbb12011-02-11 18:23:35 -0800817 std::string path;
818 path = u_getDataDirectory();
819 path += "/";
820 path += U_ICUDATA_NAME;
821 path += ".dat";
Elliott Hughes9f557fa2011-02-22 11:50:18 -0800822
823 #define FAIL_WITH_STRERROR(s) \
Steve Block679cf682012-01-08 10:18:49 +0000824 ALOGE("Couldn't " s " '%s': %s", path.c_str(), strerror(errno)); \
Elliott Hughes7cd67602012-05-03 17:21:04 -0700825 abort();
Elliott Hughes9f557fa2011-02-22 11:50:18 -0800826 #define MAYBE_FAIL_WITH_ICU_ERROR(s) \
827 if (status != U_ZERO_ERROR) {\
Steve Block679cf682012-01-08 10:18:49 +0000828 ALOGE("Couldn't initialize ICU (" s "): %s (%s)", u_errorName(status), path.c_str()); \
Elliott Hughes7cd67602012-05-03 17:21:04 -0700829 abort(); \
Elliott Hughes9f557fa2011-02-22 11:50:18 -0800830 }
Elliott Hughes566bbb12011-02-11 18:23:35 -0800831
832 // Open the file and get its length.
833 ScopedFd fd(open(path.c_str(), O_RDONLY));
834 if (fd.get() == -1) {
Elliott Hughes9f557fa2011-02-22 11:50:18 -0800835 FAIL_WITH_STRERROR("open");
Elliott Hughes566bbb12011-02-11 18:23:35 -0800836 }
837 struct stat sb;
838 if (fstat(fd.get(), &sb) == -1) {
Elliott Hughes9f557fa2011-02-22 11:50:18 -0800839 FAIL_WITH_STRERROR("stat");
Elliott Hughes566bbb12011-02-11 18:23:35 -0800840 }
841
842 // Map it.
843 void* data = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, fd.get(), 0);
844 if (data == MAP_FAILED) {
Elliott Hughes9f557fa2011-02-22 11:50:18 -0800845 FAIL_WITH_STRERROR("mmap");
Elliott Hughes566bbb12011-02-11 18:23:35 -0800846 }
847
848 // Tell the kernel that accesses are likely to be random rather than sequential.
849 if (madvise(data, sb.st_size, MADV_RANDOM) == -1) {
Elliott Hughes9f557fa2011-02-22 11:50:18 -0800850 FAIL_WITH_STRERROR("madvise(MADV_RANDOM)");
Elliott Hughes566bbb12011-02-11 18:23:35 -0800851 }
852
853 // Tell ICU to use our memory-mapped data.
854 UErrorCode status = U_ZERO_ERROR;
855 udata_setCommonData(data, &status);
Elliott Hughes9f557fa2011-02-22 11:50:18 -0800856 MAYBE_FAIL_WITH_ICU_ERROR("udata_setCommonData");
Elliott Hughes566bbb12011-02-11 18:23:35 -0800857 // Tell ICU it can *only* use our memory-mapped data.
858 udata_setFileAccess(UDATA_NO_FILES, &status);
Elliott Hughes9f557fa2011-02-22 11:50:18 -0800859 MAYBE_FAIL_WITH_ICU_ERROR("udata_setFileAccess");
Elliott Hughes566bbb12011-02-11 18:23:35 -0800860
Elliott Hughes972d9be2010-09-16 16:38:23 -0700861 // Failures to find the ICU data tend to be somewhat obscure because ICU loads its data on first
862 // use, which can be anywhere. Force initialization up front so we can report a nice clear error
863 // and bail.
Elliott Hughes972d9be2010-09-16 16:38:23 -0700864 u_init(&status);
Elliott Hughes9f557fa2011-02-22 11:50:18 -0800865 MAYBE_FAIL_WITH_ICU_ERROR("u_init");
Elliott Hughes7cd67602012-05-03 17:21:04 -0700866 jniRegisterNativeMethods(env, "libcore/icu/ICU", gMethods, NELEM(gMethods));
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800867}