The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1 | /* |
| 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 Hughes | 757a794 | 2010-04-16 14:14:28 -0700 | [diff] [blame] | 17 | #define LOG_TAG "ICU" |
Elliott Hughes | c08f9fb | 2010-04-16 17:44:12 -0700 | [diff] [blame] | 18 | |
Elliott Hughes | 3aac4dd | 2013-02-04 15:37:52 -0800 | [diff] [blame] | 19 | #include "IcuUtilities.h" |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 20 | #include "JNIHelp.h" |
Elliott Hughes | a9f5c16 | 2010-06-16 16:32:18 -0700 | [diff] [blame] | 21 | #include "JniConstants.h" |
Elliott Hughes | bef9ec3 | 2011-04-19 10:26:58 -0700 | [diff] [blame] | 22 | #include "JniException.h" |
Elliott Hughes | 566bbb1 | 2011-02-11 18:23:35 -0800 | [diff] [blame] | 23 | #include "ScopedFd.h" |
Elliott Hughes | 4e3714f | 2010-06-22 14:32:47 -0700 | [diff] [blame] | 24 | #include "ScopedJavaUnicodeString.h" |
Elliott Hughes | f281667 | 2010-05-11 17:16:02 -0700 | [diff] [blame] | 25 | #include "ScopedLocalRef.h" |
Elliott Hughes | 9de899c | 2010-04-12 18:32:50 -0700 | [diff] [blame] | 26 | #include "ScopedUtfChars.h" |
Elliott Hughes | 757a794 | 2010-04-16 14:14:28 -0700 | [diff] [blame] | 27 | #include "UniquePtr.h" |
Elliott Hughes | f2d5062 | 2010-01-25 23:13:46 -0800 | [diff] [blame] | 28 | #include "cutils/log.h" |
Elliott Hughes | ddafeb1 | 2011-02-18 15:14:29 -0800 | [diff] [blame] | 29 | #include "toStringArray.h" |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 30 | #include "unicode/calendar.h" |
| 31 | #include "unicode/datefmt.h" |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 32 | #include "unicode/dcfmtsym.h" |
Elliott Hughes | 94782d0 | 2010-06-15 17:58:00 -0700 | [diff] [blame] | 33 | #include "unicode/decimfmt.h" |
| 34 | #include "unicode/dtfmtsym.h" |
Elliott Hughes | cb1b902 | 2012-10-04 12:22:20 -0700 | [diff] [blame] | 35 | #include "unicode/dtptngen.h" |
Elliott Hughes | 94782d0 | 2010-06-15 17:58:00 -0700 | [diff] [blame] | 36 | #include "unicode/gregocal.h" |
| 37 | #include "unicode/locid.h" |
| 38 | #include "unicode/numfmt.h" |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 39 | #include "unicode/strenum.h" |
Elliott Hughes | 94782d0 | 2010-06-15 17:58:00 -0700 | [diff] [blame] | 40 | #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 Meglio | 0452e9a | 2011-06-07 19:17:12 -0700 | [diff] [blame] | 46 | #include "unicode/uloc.h" |
Elliott Hughes | 15d8280 | 2013-02-27 12:25:47 -0800 | [diff] [blame] | 47 | #include "unicode/ulocdata.h" |
Elliott Hughes | 94782d0 | 2010-06-15 17:58:00 -0700 | [diff] [blame] | 48 | #include "unicode/ustring.h" |
claireho | 947eeb8 | 2010-06-03 14:10:30 -0700 | [diff] [blame] | 49 | #include "ureslocs.h" |
Elliott Hughes | 94782d0 | 2010-06-15 17:58:00 -0700 | [diff] [blame] | 50 | #include "valueOf.h" |
Elliott Hughes | ddafeb1 | 2011-02-18 15:14:29 -0800 | [diff] [blame] | 51 | |
Elliott Hughes | 566bbb1 | 2011-02-11 18:23:35 -0800 | [diff] [blame] | 52 | #include <errno.h> |
| 53 | #include <fcntl.h> |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 54 | #include <stdlib.h> |
| 55 | #include <string.h> |
Elliott Hughes | ddafeb1 | 2011-02-18 15:14:29 -0800 | [diff] [blame] | 56 | #include <string> |
Elliott Hughes | 566bbb1 | 2011-02-11 18:23:35 -0800 | [diff] [blame] | 57 | #include <sys/mman.h> |
| 58 | #include <sys/stat.h> |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 59 | #include <sys/time.h> |
Elliott Hughes | 566bbb1 | 2011-02-11 18:23:35 -0800 | [diff] [blame] | 60 | #include <sys/types.h> |
| 61 | #include <time.h> |
| 62 | #include <unistd.h> |
Narayan Kamath | c5b1eb1 | 2014-01-10 14:54:31 +0000 | [diff] [blame] | 63 | #include <vector> |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 64 | |
Elliott Hughes | c4e0797 | 2012-08-15 09:27:14 -0700 | [diff] [blame] | 65 | // 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 Hughes | 70fa193 | 2009-12-21 16:36:28 -0800 | [diff] [blame] | 72 | class ScopedResourceBundle { |
Elliott Hughes | c4e0797 | 2012-08-15 09:27:14 -0700 | [diff] [blame] | 73 | public: |
| 74 | ScopedResourceBundle(UResourceBundle* bundle) : bundle_(bundle) { |
| 75 | } |
| 76 | |
| 77 | ~ScopedResourceBundle() { |
| 78 | if (bundle_ != NULL) { |
| 79 | ures_close(bundle_); |
Elliott Hughes | 70fa193 | 2009-12-21 16:36:28 -0800 | [diff] [blame] | 80 | } |
Elliott Hughes | c4e0797 | 2012-08-15 09:27:14 -0700 | [diff] [blame] | 81 | } |
Elliott Hughes | 70fa193 | 2009-12-21 16:36:28 -0800 | [diff] [blame] | 82 | |
Elliott Hughes | c4e0797 | 2012-08-15 09:27:14 -0700 | [diff] [blame] | 83 | UResourceBundle* get() { |
| 84 | return bundle_; |
| 85 | } |
Elliott Hughes | 70fa193 | 2009-12-21 16:36:28 -0800 | [diff] [blame] | 86 | |
Elliott Hughes | c4e0797 | 2012-08-15 09:27:14 -0700 | [diff] [blame] | 87 | 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 Hughes | 70fa193 | 2009-12-21 16:36:28 -0800 | [diff] [blame] | 92 | |
Elliott Hughes | c4e0797 | 2012-08-15 09:27:14 -0700 | [diff] [blame] | 93 | private: |
| 94 | UResourceBundle* bundle_; |
| 95 | DISALLOW_COPY_AND_ASSIGN(ScopedResourceBundle); |
Elliott Hughes | 70fa193 | 2009-12-21 16:36:28 -0800 | [diff] [blame] | 96 | }; |
| 97 | |
Fabrice Di Meglio | 0452e9a | 2011-06-07 19:17:12 -0700 | [diff] [blame] | 98 | static 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 Meglio | 4c94a61 | 2011-06-15 17:23:43 -0700 | [diff] [blame] | 109 | static 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 Kamath | c5b1eb1 | 2014-01-10 14:54:31 +0000 | [diff] [blame] | 120 | static 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 | |
| 168 | static jstring ICU_languageTagForLocale(JNIEnv* env, jclass, jstring javaLocaleId) { |
| 169 | ScopedUtfChars localeID(env, javaLocaleId); |
| 170 | |
| 171 | // The conversion from an ICU locale ID to a BCP 47 tag will shrink |
| 172 | // the size of the string unless there's an invalid language or a bad |
| 173 | // parse (which will result in an x-lvariant private use subtag at |
| 174 | // the end of the input). |
| 175 | const size_t initialBufferSize = localeID.size(); |
| 176 | std::vector<char> buffer(initialBufferSize); |
| 177 | |
| 178 | UErrorCode status = U_ZERO_ERROR; |
| 179 | while (true) { |
| 180 | const size_t outputLength = uloc_toLanguageTag(localeID.c_str(), |
| 181 | &buffer[0], buffer.size(), false /* strict */, &status); |
| 182 | if (U_FAILURE(status)) { |
| 183 | return NULL; |
| 184 | } |
| 185 | |
| 186 | if (outputLength == buffer.size()) { |
| 187 | buffer.resize(buffer.size() << 1); |
| 188 | } else { |
| 189 | break; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | return env->NewStringUTF(&buffer[0]); |
| 194 | } |
| 195 | |
Elliott Hughes | 52b310a | 2011-03-03 21:22:39 -0800 | [diff] [blame] | 196 | static jint ICU_getCurrencyFractionDigits(JNIEnv* env, jclass, jstring javaCurrencyCode) { |
Elliott Hughes | 3aac4dd | 2013-02-04 15:37:52 -0800 | [diff] [blame] | 197 | ScopedJavaUnicodeString currencyCode(env, javaCurrencyCode); |
| 198 | if (!currencyCode.valid()) { |
| 199 | return 0; |
| 200 | } |
| 201 | UnicodeString icuCurrencyCode(currencyCode.unicodeString()); |
| 202 | UErrorCode status = U_ZERO_ERROR; |
| 203 | return ucurr_getDefaultFractionDigits(icuCurrencyCode.getTerminatedBuffer(), &status); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 204 | } |
| 205 | |
Elliott Hughes | a49a1e8 | 2012-10-18 17:37:35 -0700 | [diff] [blame] | 206 | // TODO: rewrite this with int32_t ucurr_forLocale(const char* locale, UChar* buff, int32_t buffCapacity, UErrorCode* ec)... |
Elliott Hughes | 52b310a | 2011-03-03 21:22:39 -0800 | [diff] [blame] | 207 | static jstring ICU_getCurrencyCode(JNIEnv* env, jclass, jstring javaCountryCode) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 208 | UErrorCode status = U_ZERO_ERROR; |
claireho | 947eeb8 | 2010-06-03 14:10:30 -0700 | [diff] [blame] | 209 | ScopedResourceBundle supplData(ures_openDirect(U_ICUDATA_CURR, "supplementalData", &status)); |
Elliott Hughes | 70fa193 | 2009-12-21 16:36:28 -0800 | [diff] [blame] | 210 | if (U_FAILURE(status)) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 211 | return NULL; |
| 212 | } |
| 213 | |
Elliott Hughes | 70fa193 | 2009-12-21 16:36:28 -0800 | [diff] [blame] | 214 | ScopedResourceBundle currencyMap(ures_getByKey(supplData.get(), "CurrencyMap", NULL, &status)); |
| 215 | if (U_FAILURE(status)) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 216 | return NULL; |
| 217 | } |
| 218 | |
Elliott Hughes | 52b310a | 2011-03-03 21:22:39 -0800 | [diff] [blame] | 219 | ScopedUtfChars countryCode(env, javaCountryCode); |
| 220 | ScopedResourceBundle currency(ures_getByKey(currencyMap.get(), countryCode.c_str(), NULL, &status)); |
Elliott Hughes | 70fa193 | 2009-12-21 16:36:28 -0800 | [diff] [blame] | 221 | if (U_FAILURE(status)) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 222 | return NULL; |
| 223 | } |
| 224 | |
Elliott Hughes | 70fa193 | 2009-12-21 16:36:28 -0800 | [diff] [blame] | 225 | ScopedResourceBundle currencyElem(ures_getByIndex(currency.get(), 0, NULL, &status)); |
| 226 | if (U_FAILURE(status)) { |
claireho | 5b7b7fe | 2012-01-12 14:51:08 -0800 | [diff] [blame] | 227 | return env->NewStringUTF("XXX"); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 228 | } |
| 229 | |
Elliott Hughes | 52b310a | 2011-03-03 21:22:39 -0800 | [diff] [blame] | 230 | // Check if there's a 'to' date. If there is, the currency isn't used anymore. |
Elliott Hughes | 67081ff | 2009-12-21 20:24:12 -0800 | [diff] [blame] | 231 | ScopedResourceBundle currencyTo(ures_getByKey(currencyElem.get(), "to", NULL, &status)); |
| 232 | if (!U_FAILURE(status)) { |
Elliott Hughes | 67081ff | 2009-12-21 20:24:12 -0800 | [diff] [blame] | 233 | return NULL; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 234 | } |
Elliott Hughes | 52b310a | 2011-03-03 21:22:39 -0800 | [diff] [blame] | 235 | // Ignore the failure to find a 'to' date. |
Elliott Hughes | 67081ff | 2009-12-21 20:24:12 -0800 | [diff] [blame] | 236 | status = U_ZERO_ERROR; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 237 | |
Elliott Hughes | 70fa193 | 2009-12-21 16:36:28 -0800 | [diff] [blame] | 238 | ScopedResourceBundle currencyId(ures_getByKey(currencyElem.get(), "id", NULL, &status)); |
| 239 | if (U_FAILURE(status)) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 240 | // No id defined for this country |
claireho | 5b7b7fe | 2012-01-12 14:51:08 -0800 | [diff] [blame] | 241 | return env->NewStringUTF("XXX"); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 242 | } |
| 243 | |
Elliott Hughes | 52b310a | 2011-03-03 21:22:39 -0800 | [diff] [blame] | 244 | int32_t charCount; |
| 245 | const jchar* chars = ures_getString(currencyId.get(), &charCount, &status); |
claireho | 5b7b7fe | 2012-01-12 14:51:08 -0800 | [diff] [blame] | 246 | return (charCount == 0) ? env->NewStringUTF("XXX") : env->NewString(chars, charCount); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 247 | } |
| 248 | |
Elliott Hughes | a49a1e8 | 2012-10-18 17:37:35 -0700 | [diff] [blame] | 249 | static jstring getCurrencyName(JNIEnv* env, jstring javaLocaleName, jstring javaCurrencyCode, UCurrNameStyle nameStyle) { |
| 250 | ScopedUtfChars localeName(env, javaLocaleName); |
Elliott Hughes | 3aac4dd | 2013-02-04 15:37:52 -0800 | [diff] [blame] | 251 | if (localeName.c_str() == NULL) { |
| 252 | return NULL; |
| 253 | } |
Elliott Hughes | a49a1e8 | 2012-10-18 17:37:35 -0700 | [diff] [blame] | 254 | ScopedJavaUnicodeString currencyCode(env, javaCurrencyCode); |
Elliott Hughes | 3aac4dd | 2013-02-04 15:37:52 -0800 | [diff] [blame] | 255 | if (!currencyCode.valid()) { |
| 256 | return NULL; |
| 257 | } |
Elliott Hughes | a49a1e8 | 2012-10-18 17:37:35 -0700 | [diff] [blame] | 258 | UnicodeString icuCurrencyCode(currencyCode.unicodeString()); |
| 259 | UErrorCode status = U_ZERO_ERROR; |
| 260 | UBool isChoiceFormat = false; |
| 261 | int32_t charCount; |
| 262 | const UChar* chars = ucurr_getName(icuCurrencyCode.getTerminatedBuffer(), localeName.c_str(), |
| 263 | nameStyle, &isChoiceFormat, &charCount, &status); |
| 264 | if (status == U_USING_DEFAULT_WARNING) { |
| 265 | if (nameStyle == UCURR_SYMBOL_NAME) { |
| 266 | // ICU doesn't distinguish between falling back to the root locale and meeting a genuinely |
| 267 | // unknown currency. The Currency class does. |
| 268 | if (!ucurr_isAvailable(icuCurrencyCode.getTerminatedBuffer(), U_DATE_MIN, U_DATE_MAX, &status)) { |
| 269 | return NULL; |
| 270 | } |
Elliott Hughes | 52b310a | 2011-03-03 21:22:39 -0800 | [diff] [blame] | 271 | } |
Elliott Hughes | a49a1e8 | 2012-10-18 17:37:35 -0700 | [diff] [blame] | 272 | if (nameStyle == UCURR_LONG_NAME) { |
| 273 | // ICU's default is English. We want the ISO 4217 currency code instead. |
| 274 | chars = icuCurrencyCode.getBuffer(); |
| 275 | charCount = icuCurrencyCode.length(); |
| 276 | } |
| 277 | } |
| 278 | return (charCount == 0) ? NULL : env->NewString(chars, charCount); |
Elliott Hughes | 52b310a | 2011-03-03 21:22:39 -0800 | [diff] [blame] | 279 | } |
| 280 | |
Elliott Hughes | a49a1e8 | 2012-10-18 17:37:35 -0700 | [diff] [blame] | 281 | static jstring ICU_getCurrencyDisplayName(JNIEnv* env, jclass, jstring javaLocaleName, jstring javaCurrencyCode) { |
| 282 | return getCurrencyName(env, javaLocaleName, javaCurrencyCode, UCURR_LONG_NAME); |
| 283 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 284 | |
Elliott Hughes | a49a1e8 | 2012-10-18 17:37:35 -0700 | [diff] [blame] | 285 | static jstring ICU_getCurrencySymbol(JNIEnv* env, jclass, jstring javaLocaleName, jstring javaCurrencyCode) { |
| 286 | return getCurrencyName(env, javaLocaleName, javaCurrencyCode, UCURR_SYMBOL_NAME); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 287 | } |
| 288 | |
Elliott Hughes | e22935d | 2010-08-12 17:27:27 -0700 | [diff] [blame] | 289 | static jstring ICU_getDisplayCountryNative(JNIEnv* env, jclass, jstring targetLocale, jstring locale) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 290 | Locale loc = getLocale(env, locale); |
| 291 | Locale targetLoc = getLocale(env, targetLocale); |
Elliott Hughes | 70fa193 | 2009-12-21 16:36:28 -0800 | [diff] [blame] | 292 | UnicodeString str; |
| 293 | targetLoc.getDisplayCountry(loc, str); |
| 294 | return env->NewString(str.getBuffer(), str.length()); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 295 | } |
| 296 | |
Elliott Hughes | e22935d | 2010-08-12 17:27:27 -0700 | [diff] [blame] | 297 | static jstring ICU_getDisplayLanguageNative(JNIEnv* env, jclass, jstring targetLocale, jstring locale) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 298 | Locale loc = getLocale(env, locale); |
| 299 | Locale targetLoc = getLocale(env, targetLocale); |
Elliott Hughes | 70fa193 | 2009-12-21 16:36:28 -0800 | [diff] [blame] | 300 | UnicodeString str; |
| 301 | targetLoc.getDisplayLanguage(loc, str); |
| 302 | return env->NewString(str.getBuffer(), str.length()); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 303 | } |
| 304 | |
Narayan Kamath | c5b1eb1 | 2014-01-10 14:54:31 +0000 | [diff] [blame] | 305 | static jstring ICU_getDisplayScriptNative(JNIEnv* env, jclass, jstring targetLocale, jstring locale) { |
| 306 | Locale loc = getLocale(env, locale); |
| 307 | Locale targetLoc = getLocale(env, targetLocale); |
| 308 | UnicodeString str; |
| 309 | targetLoc.getDisplayScript(loc, str); |
| 310 | return env->NewString(str.getBuffer(), str.length()); |
| 311 | } |
| 312 | |
Elliott Hughes | e22935d | 2010-08-12 17:27:27 -0700 | [diff] [blame] | 313 | static jstring ICU_getDisplayVariantNative(JNIEnv* env, jclass, jstring targetLocale, jstring locale) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 314 | Locale loc = getLocale(env, locale); |
| 315 | Locale targetLoc = getLocale(env, targetLocale); |
Elliott Hughes | 70fa193 | 2009-12-21 16:36:28 -0800 | [diff] [blame] | 316 | UnicodeString str; |
| 317 | targetLoc.getDisplayVariant(loc, str); |
| 318 | return env->NewString(str.getBuffer(), str.length()); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 319 | } |
| 320 | |
Elliott Hughes | e22935d | 2010-08-12 17:27:27 -0700 | [diff] [blame] | 321 | static jstring ICU_getISO3CountryNative(JNIEnv* env, jclass, jstring locale) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 322 | Locale loc = getLocale(env, locale); |
Elliott Hughes | 2e3a41d | 2009-12-21 10:52:53 -0800 | [diff] [blame] | 323 | return env->NewStringUTF(loc.getISO3Country()); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 324 | } |
| 325 | |
Elliott Hughes | e22935d | 2010-08-12 17:27:27 -0700 | [diff] [blame] | 326 | static jstring ICU_getISO3LanguageNative(JNIEnv* env, jclass, jstring locale) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 327 | Locale loc = getLocale(env, locale); |
Elliott Hughes | 2e3a41d | 2009-12-21 10:52:53 -0800 | [diff] [blame] | 328 | return env->NewStringUTF(loc.getISO3Language()); |
| 329 | } |
| 330 | |
Elliott Hughes | e22935d | 2010-08-12 17:27:27 -0700 | [diff] [blame] | 331 | static jobjectArray ICU_getISOCountriesNative(JNIEnv* env, jclass) { |
Elliott Hughes | 2e3a41d | 2009-12-21 10:52:53 -0800 | [diff] [blame] | 332 | return toStringArray(env, Locale::getISOCountries()); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 333 | } |
| 334 | |
Elliott Hughes | e22935d | 2010-08-12 17:27:27 -0700 | [diff] [blame] | 335 | static jobjectArray ICU_getISOLanguagesNative(JNIEnv* env, jclass) { |
Elliott Hughes | 2e3a41d | 2009-12-21 10:52:53 -0800 | [diff] [blame] | 336 | return toStringArray(env, Locale::getISOLanguages()); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 337 | } |
| 338 | |
Elliott Hughes | e22935d | 2010-08-12 17:27:27 -0700 | [diff] [blame] | 339 | static jobjectArray ICU_getAvailableLocalesNative(JNIEnv* env, jclass) { |
Elliott Hughes | ddafeb1 | 2011-02-18 15:14:29 -0800 | [diff] [blame] | 340 | return toStringArray(env, uloc_countAvailable, uloc_getAvailable); |
Elliott Hughes | f9157ea | 2010-04-01 13:56:32 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Elliott Hughes | e22935d | 2010-08-12 17:27:27 -0700 | [diff] [blame] | 343 | static jobjectArray ICU_getAvailableBreakIteratorLocalesNative(JNIEnv* env, jclass) { |
Elliott Hughes | ddafeb1 | 2011-02-18 15:14:29 -0800 | [diff] [blame] | 344 | return toStringArray(env, ubrk_countAvailable, ubrk_getAvailable); |
Elliott Hughes | f9157ea | 2010-04-01 13:56:32 -0700 | [diff] [blame] | 345 | } |
| 346 | |
Elliott Hughes | e22935d | 2010-08-12 17:27:27 -0700 | [diff] [blame] | 347 | static jobjectArray ICU_getAvailableCalendarLocalesNative(JNIEnv* env, jclass) { |
Elliott Hughes | ddafeb1 | 2011-02-18 15:14:29 -0800 | [diff] [blame] | 348 | return toStringArray(env, ucal_countAvailable, ucal_getAvailable); |
Elliott Hughes | f9157ea | 2010-04-01 13:56:32 -0700 | [diff] [blame] | 349 | } |
| 350 | |
Elliott Hughes | e22935d | 2010-08-12 17:27:27 -0700 | [diff] [blame] | 351 | static jobjectArray ICU_getAvailableCollatorLocalesNative(JNIEnv* env, jclass) { |
Elliott Hughes | ddafeb1 | 2011-02-18 15:14:29 -0800 | [diff] [blame] | 352 | return toStringArray(env, ucol_countAvailable, ucol_getAvailable); |
Elliott Hughes | f9157ea | 2010-04-01 13:56:32 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Elliott Hughes | e22935d | 2010-08-12 17:27:27 -0700 | [diff] [blame] | 355 | static jobjectArray ICU_getAvailableDateFormatLocalesNative(JNIEnv* env, jclass) { |
Elliott Hughes | ddafeb1 | 2011-02-18 15:14:29 -0800 | [diff] [blame] | 356 | return toStringArray(env, udat_countAvailable, udat_getAvailable); |
Elliott Hughes | f9157ea | 2010-04-01 13:56:32 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Elliott Hughes | e22935d | 2010-08-12 17:27:27 -0700 | [diff] [blame] | 359 | static jobjectArray ICU_getAvailableNumberFormatLocalesNative(JNIEnv* env, jclass) { |
Elliott Hughes | ddafeb1 | 2011-02-18 15:14:29 -0800 | [diff] [blame] | 360 | return toStringArray(env, unum_countAvailable, unum_getAvailable); |
Elliott Hughes | f9157ea | 2010-04-01 13:56:32 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Elliott Hughes | 33aa6eb | 2009-12-22 17:20:44 -0800 | [diff] [blame] | 363 | static void setIntegerField(JNIEnv* env, jobject obj, const char* fieldName, int value) { |
Elliott Hughes | 94782d0 | 2010-06-15 17:58:00 -0700 | [diff] [blame] | 364 | ScopedLocalRef<jobject> integerValue(env, integerValueOf(env, value)); |
Elliott Hughes | a9f5c16 | 2010-06-16 16:32:18 -0700 | [diff] [blame] | 365 | jfieldID fid = env->GetFieldID(JniConstants::localeDataClass, fieldName, "Ljava/lang/Integer;"); |
Elliott Hughes | 94782d0 | 2010-06-15 17:58:00 -0700 | [diff] [blame] | 366 | env->SetObjectField(obj, fid, integerValue.get()); |
Elliott Hughes | 33aa6eb | 2009-12-22 17:20:44 -0800 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | static void setStringField(JNIEnv* env, jobject obj, const char* fieldName, jstring value) { |
Elliott Hughes | a9f5c16 | 2010-06-16 16:32:18 -0700 | [diff] [blame] | 370 | jfieldID fid = env->GetFieldID(JniConstants::localeDataClass, fieldName, "Ljava/lang/String;"); |
Elliott Hughes | 33aa6eb | 2009-12-22 17:20:44 -0800 | [diff] [blame] | 371 | env->SetObjectField(obj, fid, value); |
Elliott Hughes | e2377cd | 2011-06-15 12:59:02 -0700 | [diff] [blame] | 372 | env->DeleteLocalRef(value); |
Elliott Hughes | 33aa6eb | 2009-12-22 17:20:44 -0800 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | static void setStringArrayField(JNIEnv* env, jobject obj, const char* fieldName, jobjectArray value) { |
Elliott Hughes | a9f5c16 | 2010-06-16 16:32:18 -0700 | [diff] [blame] | 376 | jfieldID fid = env->GetFieldID(JniConstants::localeDataClass, fieldName, "[Ljava/lang/String;"); |
Elliott Hughes | 33aa6eb | 2009-12-22 17:20:44 -0800 | [diff] [blame] | 377 | env->SetObjectField(obj, fid, value); |
| 378 | } |
| 379 | |
claireho | 5b7b7fe | 2012-01-12 14:51:08 -0800 | [diff] [blame] | 380 | static void setStringArrayField(JNIEnv* env, jobject obj, const char* fieldName, const UnicodeString* valueArray, int32_t size) { |
| 381 | ScopedLocalRef<jobjectArray> result(env, env->NewObjectArray(size, JniConstants::stringClass, NULL)); |
| 382 | for (int32_t i = 0; i < size ; i++) { |
| 383 | ScopedLocalRef<jstring> s(env, env->NewString(valueArray[i].getBuffer(),valueArray[i].length())); |
| 384 | if (env->ExceptionCheck()) { |
| 385 | return; |
| 386 | } |
| 387 | env->SetObjectArrayElement(result.get(), i, s.get()); |
| 388 | if (env->ExceptionCheck()) { |
| 389 | return; |
| 390 | } |
| 391 | } |
| 392 | setStringArrayField(env, obj, fieldName, result.get()); |
| 393 | } |
| 394 | |
Elliott Hughes | 33aa6eb | 2009-12-22 17:20:44 -0800 | [diff] [blame] | 395 | static void setStringField(JNIEnv* env, jobject obj, const char* fieldName, UResourceBundle* bundle, int index) { |
Elliott Hughes | c4e0797 | 2012-08-15 09:27:14 -0700 | [diff] [blame] | 396 | UErrorCode status = U_ZERO_ERROR; |
| 397 | int charCount; |
| 398 | const UChar* chars = ures_getStringByIndex(bundle, index, &charCount, &status); |
| 399 | if (U_SUCCESS(status)) { |
| 400 | setStringField(env, obj, fieldName, env->NewString(chars, charCount)); |
| 401 | } else { |
| 402 | ALOGE("Error setting String field %s from ICU resource (index %d): %s", fieldName, index, u_errorName(status)); |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | static void setStringField(JNIEnv* env, jobject obj, const char* fieldName, UResourceBundle* bundle, const char* key) { |
| 407 | UErrorCode status = U_ZERO_ERROR; |
| 408 | int charCount; |
| 409 | const UChar* chars = ures_getStringByKey(bundle, key, &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 (key %s): %s", fieldName, key, u_errorName(status)); |
| 414 | } |
Elliott Hughes | b7e820b | 2010-04-16 11:06:58 -0700 | [diff] [blame] | 415 | } |
| 416 | |
claireho | 5b7b7fe | 2012-01-12 14:51:08 -0800 | [diff] [blame] | 417 | static void setCharField(JNIEnv* env, jobject obj, const char* fieldName, const UnicodeString& value) { |
| 418 | if (value.length() == 0) { |
claireho | 3be1277 | 2011-06-09 10:53:06 -0700 | [diff] [blame] | 419 | return; |
| 420 | } |
claireho | 5b7b7fe | 2012-01-12 14:51:08 -0800 | [diff] [blame] | 421 | jfieldID fid = env->GetFieldID(JniConstants::localeDataClass, fieldName, "C"); |
| 422 | env->SetCharField(obj, fid, value.charAt(0)); |
| 423 | } |
claireho | 3be1277 | 2011-06-09 10:53:06 -0700 | [diff] [blame] | 424 | |
claireho | 5b7b7fe | 2012-01-12 14:51:08 -0800 | [diff] [blame] | 425 | static void setStringField(JNIEnv* env, jobject obj, const char* fieldName, const UnicodeString& value) { |
| 426 | const UChar* chars = value.getBuffer(); |
| 427 | setStringField(env, obj, fieldName, env->NewString(chars, value.length())); |
| 428 | } |
claireho | 3be1277 | 2011-06-09 10:53:06 -0700 | [diff] [blame] | 429 | |
Elliott Hughes | 007776e | 2012-11-02 14:54:02 -0700 | [diff] [blame] | 430 | static void setNumberPatterns(JNIEnv* env, jobject obj, Locale& locale) { |
claireho | 5b7b7fe | 2012-01-12 14:51:08 -0800 | [diff] [blame] | 431 | UErrorCode status = U_ZERO_ERROR; |
claireho | 5b7b7fe | 2012-01-12 14:51:08 -0800 | [diff] [blame] | 432 | |
| 433 | UnicodeString pattern; |
Elliott Hughes | 007776e | 2012-11-02 14:54:02 -0700 | [diff] [blame] | 434 | UniquePtr<DecimalFormat> fmt(static_cast<DecimalFormat*>(NumberFormat::createInstance(locale, UNUM_CURRENCY, status))); |
claireho | 5b7b7fe | 2012-01-12 14:51:08 -0800 | [diff] [blame] | 435 | pattern = fmt->toPattern(pattern.remove()); |
| 436 | setStringField(env, obj, "currencyPattern", pattern); |
| 437 | |
Elliott Hughes | 007776e | 2012-11-02 14:54:02 -0700 | [diff] [blame] | 438 | fmt.reset(static_cast<DecimalFormat*>(NumberFormat::createInstance(locale, UNUM_DECIMAL, status))); |
claireho | 5b7b7fe | 2012-01-12 14:51:08 -0800 | [diff] [blame] | 439 | pattern = fmt->toPattern(pattern.remove()); |
| 440 | setStringField(env, obj, "numberPattern", pattern); |
| 441 | |
Elliott Hughes | 007776e | 2012-11-02 14:54:02 -0700 | [diff] [blame] | 442 | fmt.reset(static_cast<DecimalFormat*>(NumberFormat::createInstance(locale, UNUM_PERCENT, status))); |
claireho | 5b7b7fe | 2012-01-12 14:51:08 -0800 | [diff] [blame] | 443 | pattern = fmt->toPattern(pattern.remove()); |
| 444 | setStringField(env, obj, "percentPattern", pattern); |
| 445 | } |
| 446 | |
Elliott Hughes | 007776e | 2012-11-02 14:54:02 -0700 | [diff] [blame] | 447 | static void setDecimalFormatSymbolsData(JNIEnv* env, jobject obj, Locale& locale) { |
claireho | 5b7b7fe | 2012-01-12 14:51:08 -0800 | [diff] [blame] | 448 | UErrorCode status = U_ZERO_ERROR; |
Elliott Hughes | 007776e | 2012-11-02 14:54:02 -0700 | [diff] [blame] | 449 | DecimalFormatSymbols dfs(locale, status); |
claireho | 5b7b7fe | 2012-01-12 14:51:08 -0800 | [diff] [blame] | 450 | |
| 451 | setCharField(env, obj, "decimalSeparator", dfs.getSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol)); |
| 452 | setCharField(env, obj, "groupingSeparator", dfs.getSymbol(DecimalFormatSymbols::kGroupingSeparatorSymbol)); |
| 453 | setCharField(env, obj, "patternSeparator", dfs.getSymbol(DecimalFormatSymbols::kPatternSeparatorSymbol)); |
| 454 | setCharField(env, obj, "percent", dfs.getSymbol(DecimalFormatSymbols::kPercentSymbol)); |
| 455 | setCharField(env, obj, "perMill", dfs.getSymbol(DecimalFormatSymbols::kPerMillSymbol)); |
| 456 | setCharField(env, obj, "monetarySeparator", dfs.getSymbol(DecimalFormatSymbols::kMonetarySeparatorSymbol)); |
| 457 | setCharField(env, obj, "minusSign", dfs.getSymbol(DecimalFormatSymbols:: kMinusSignSymbol)); |
| 458 | setStringField(env, obj, "exponentSeparator", dfs.getSymbol(DecimalFormatSymbols::kExponentialSymbol)); |
| 459 | setStringField(env, obj, "infinity", dfs.getSymbol(DecimalFormatSymbols::kInfinitySymbol)); |
| 460 | setStringField(env, obj, "NaN", dfs.getSymbol(DecimalFormatSymbols::kNaNSymbol)); |
| 461 | setCharField(env, obj, "zeroDigit", dfs.getSymbol(DecimalFormatSymbols::kZeroDigitSymbol)); |
Elliott Hughes | 33aa6eb | 2009-12-22 17:20:44 -0800 | [diff] [blame] | 462 | } |
| 463 | |
Elliott Hughes | c4e0797 | 2012-08-15 09:27:14 -0700 | [diff] [blame] | 464 | |
| 465 | // Iterates up through the locale hierarchy. So "en_US" would return "en_US", "en", "". |
| 466 | class LocaleNameIterator { |
| 467 | public: |
| 468 | LocaleNameIterator(const char* locale_name, UErrorCode& status) : status_(status), has_next_(true) { |
| 469 | strcpy(locale_name_, locale_name); |
| 470 | locale_name_length_ = strlen(locale_name_); |
| 471 | } |
| 472 | |
| 473 | const char* Get() { |
| 474 | return locale_name_; |
| 475 | } |
| 476 | |
| 477 | bool HasNext() { |
| 478 | return has_next_; |
| 479 | } |
| 480 | |
| 481 | void Up() { |
Elliott Hughes | c4e0797 | 2012-08-15 09:27:14 -0700 | [diff] [blame] | 482 | if (locale_name_length_ == 0) { |
| 483 | has_next_ = false; |
Elliott Hughes | f001abe | 2013-07-24 17:52:54 -0700 | [diff] [blame] | 484 | } else { |
| 485 | locale_name_length_ = uloc_getParent(locale_name_, locale_name_, sizeof(locale_name_), &status_); |
Elliott Hughes | c4e0797 | 2012-08-15 09:27:14 -0700 | [diff] [blame] | 486 | } |
| 487 | } |
| 488 | |
| 489 | private: |
| 490 | UErrorCode& status_; |
| 491 | bool has_next_; |
| 492 | char locale_name_[ULOC_FULLNAME_CAPACITY]; |
| 493 | int32_t locale_name_length_; |
| 494 | |
| 495 | DISALLOW_COPY_AND_ASSIGN(LocaleNameIterator); |
| 496 | }; |
| 497 | |
| 498 | static bool getDateTimePatterns(JNIEnv* env, jobject localeData, const char* locale_name) { |
| 499 | UErrorCode status = U_ZERO_ERROR; |
| 500 | ScopedResourceBundle root(ures_open(NULL, locale_name, &status)); |
| 501 | if (U_FAILURE(status)) { |
| 502 | return false; |
| 503 | } |
| 504 | ScopedResourceBundle calendar(ures_getByKey(root.get(), "calendar", NULL, &status)); |
| 505 | if (U_FAILURE(status)) { |
| 506 | return false; |
| 507 | } |
| 508 | ScopedResourceBundle gregorian(ures_getByKey(calendar.get(), "gregorian", NULL, &status)); |
| 509 | if (U_FAILURE(status)) { |
| 510 | return false; |
| 511 | } |
| 512 | ScopedResourceBundle dateTimePatterns(ures_getByKey(gregorian.get(), "DateTimePatterns", NULL, &status)); |
| 513 | if (U_FAILURE(status)) { |
| 514 | return false; |
| 515 | } |
| 516 | setStringField(env, localeData, "fullTimeFormat", dateTimePatterns.get(), 0); |
| 517 | setStringField(env, localeData, "longTimeFormat", dateTimePatterns.get(), 1); |
| 518 | setStringField(env, localeData, "mediumTimeFormat", dateTimePatterns.get(), 2); |
| 519 | setStringField(env, localeData, "shortTimeFormat", dateTimePatterns.get(), 3); |
| 520 | setStringField(env, localeData, "fullDateFormat", dateTimePatterns.get(), 4); |
| 521 | setStringField(env, localeData, "longDateFormat", dateTimePatterns.get(), 5); |
| 522 | setStringField(env, localeData, "mediumDateFormat", dateTimePatterns.get(), 6); |
| 523 | setStringField(env, localeData, "shortDateFormat", dateTimePatterns.get(), 7); |
| 524 | return true; |
| 525 | } |
| 526 | |
| 527 | static bool getYesterdayTodayAndTomorrow(JNIEnv* env, jobject localeData, const char* locale_name) { |
| 528 | UErrorCode status = U_ZERO_ERROR; |
| 529 | ScopedResourceBundle root(ures_open(NULL, locale_name, &status)); |
| 530 | if (U_FAILURE(status)) { |
| 531 | return false; |
| 532 | } |
Elliott Hughes | 0ef9944 | 2013-06-19 14:53:18 -0700 | [diff] [blame] | 533 | ScopedResourceBundle fields(ures_getByKey(root.get(), "fields", NULL, &status)); |
Elliott Hughes | c4e0797 | 2012-08-15 09:27:14 -0700 | [diff] [blame] | 534 | if (U_FAILURE(status)) { |
| 535 | return false; |
| 536 | } |
| 537 | ScopedResourceBundle day(ures_getByKey(fields.get(), "day", NULL, &status)); |
| 538 | if (U_FAILURE(status)) { |
| 539 | return false; |
| 540 | } |
| 541 | ScopedResourceBundle relative(ures_getByKey(day.get(), "relative", NULL, &status)); |
| 542 | if (U_FAILURE(status)) { |
| 543 | return false; |
| 544 | } |
| 545 | // bn_BD only has a "-2" entry. |
| 546 | if (relative.hasKey("-1") && relative.hasKey("0") && relative.hasKey("1")) { |
| 547 | setStringField(env, localeData, "yesterday", relative.get(), "-1"); |
| 548 | setStringField(env, localeData, "today", relative.get(), "0"); |
| 549 | setStringField(env, localeData, "tomorrow", relative.get(), "1"); |
| 550 | return true; |
| 551 | } |
| 552 | return false; |
| 553 | } |
| 554 | |
Elliott Hughes | 0fd776f | 2013-10-31 10:14:28 -0700 | [diff] [blame] | 555 | static jboolean ICU_initLocaleDataNative(JNIEnv* env, jclass, jstring javaLocaleName, jobject localeData) { |
Elliott Hughes | 007776e | 2012-11-02 14:54:02 -0700 | [diff] [blame] | 556 | ScopedUtfChars localeName(env, javaLocaleName); |
Elliott Hughes | e2377cd | 2011-06-15 12:59:02 -0700 | [diff] [blame] | 557 | if (localeName.c_str() == NULL) { |
| 558 | return JNI_FALSE; |
| 559 | } |
claireho | 5b7b7fe | 2012-01-12 14:51:08 -0800 | [diff] [blame] | 560 | if (localeName.size() >= ULOC_FULLNAME_CAPACITY) { |
Elliott Hughes | c4e0797 | 2012-08-15 09:27:14 -0700 | [diff] [blame] | 561 | return JNI_FALSE; // ICU has a fixed-length limit. |
claireho | 5b7b7fe | 2012-01-12 14:51:08 -0800 | [diff] [blame] | 562 | } |
claireho | 5b7b7fe | 2012-01-12 14:51:08 -0800 | [diff] [blame] | 563 | |
Elliott Hughes | c4e0797 | 2012-08-15 09:27:14 -0700 | [diff] [blame] | 564 | // Get the DateTimePatterns. |
| 565 | UErrorCode status = U_ZERO_ERROR; |
| 566 | bool foundDateTimePatterns = false; |
| 567 | for (LocaleNameIterator it(localeName.c_str(), status); it.HasNext(); it.Up()) { |
| 568 | if (getDateTimePatterns(env, localeData, it.Get())) { |
| 569 | foundDateTimePatterns = true; |
| 570 | break; |
| 571 | } |
| 572 | } |
| 573 | if (!foundDateTimePatterns) { |
| 574 | ALOGE("Couldn't find ICU DateTimePatterns for %s", localeName.c_str()); |
Elliott Hughes | 33aa6eb | 2009-12-22 17:20:44 -0800 | [diff] [blame] | 575 | return JNI_FALSE; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 576 | } |
| 577 | |
Elliott Hughes | c4e0797 | 2012-08-15 09:27:14 -0700 | [diff] [blame] | 578 | // Get the "Yesterday", "Today", and "Tomorrow" strings. |
| 579 | bool foundYesterdayTodayAndTomorrow = false; |
| 580 | for (LocaleNameIterator it(localeName.c_str(), status); it.HasNext(); it.Up()) { |
| 581 | if (getYesterdayTodayAndTomorrow(env, localeData, it.Get())) { |
| 582 | foundYesterdayTodayAndTomorrow = true; |
| 583 | break; |
| 584 | } |
| 585 | } |
| 586 | if (!foundYesterdayTodayAndTomorrow) { |
Elliott Hughes | 680b1e2 | 2012-08-15 16:49:05 -0700 | [diff] [blame] | 587 | ALOGE("Couldn't find ICU yesterday/today/tomorrow for %s", localeName.c_str()); |
| 588 | return JNI_FALSE; |
Elliott Hughes | c4e0797 | 2012-08-15 09:27:14 -0700 | [diff] [blame] | 589 | } |
| 590 | |
claireho | 5b7b7fe | 2012-01-12 14:51:08 -0800 | [diff] [blame] | 591 | status = U_ZERO_ERROR; |
Elliott Hughes | 783112d | 2013-09-03 15:28:43 -0700 | [diff] [blame] | 592 | Locale locale = getLocale(env, javaLocaleName); |
Elliott Hughes | 007776e | 2012-11-02 14:54:02 -0700 | [diff] [blame] | 593 | UniquePtr<Calendar> cal(Calendar::createInstance(locale, status)); |
Elliott Hughes | 33aa6eb | 2009-12-22 17:20:44 -0800 | [diff] [blame] | 594 | if (U_FAILURE(status)) { |
| 595 | return JNI_FALSE; |
| 596 | } |
Elliott Hughes | c4e0797 | 2012-08-15 09:27:14 -0700 | [diff] [blame] | 597 | |
claireho | 5b7b7fe | 2012-01-12 14:51:08 -0800 | [diff] [blame] | 598 | setIntegerField(env, localeData, "firstDayOfWeek", cal->getFirstDayOfWeek()); |
| 599 | setIntegerField(env, localeData, "minimalDaysInFirstWeek", cal->getMinimalDaysInFirstWeek()); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 600 | |
Elliott Hughes | c4e0797 | 2012-08-15 09:27:14 -0700 | [diff] [blame] | 601 | // Get DateFormatSymbols. |
claireho | 5b7b7fe | 2012-01-12 14:51:08 -0800 | [diff] [blame] | 602 | status = U_ZERO_ERROR; |
Elliott Hughes | 007776e | 2012-11-02 14:54:02 -0700 | [diff] [blame] | 603 | DateFormatSymbols dateFormatSym(locale, status); |
Elliott Hughes | 33aa6eb | 2009-12-22 17:20:44 -0800 | [diff] [blame] | 604 | if (U_FAILURE(status)) { |
| 605 | return JNI_FALSE; |
| 606 | } |
Elliott Hughes | c4e0797 | 2012-08-15 09:27:14 -0700 | [diff] [blame] | 607 | |
| 608 | // Get AM/PM and BC/AD. |
claireho | 5b7b7fe | 2012-01-12 14:51:08 -0800 | [diff] [blame] | 609 | int32_t count = 0; |
claireho | 5b7b7fe | 2012-01-12 14:51:08 -0800 | [diff] [blame] | 610 | const UnicodeString* amPmStrs = dateFormatSym.getAmPmStrings(count); |
| 611 | setStringArrayField(env, localeData, "amPm", amPmStrs, count); |
| 612 | const UnicodeString* erasStrs = dateFormatSym.getEras(count); |
| 613 | setStringArrayField(env, localeData, "eras", erasStrs, count); |
Elliott Hughes | 33aa6eb | 2009-12-22 17:20:44 -0800 | [diff] [blame] | 614 | |
claireho | 5b7b7fe | 2012-01-12 14:51:08 -0800 | [diff] [blame] | 615 | const UnicodeString* longMonthNames = |
| 616 | dateFormatSym.getMonths(count, DateFormatSymbols::FORMAT, DateFormatSymbols::WIDE); |
| 617 | setStringArrayField(env, localeData, "longMonthNames", longMonthNames, count); |
| 618 | const UnicodeString* shortMonthNames = |
| 619 | dateFormatSym.getMonths(count, DateFormatSymbols::FORMAT, DateFormatSymbols::ABBREVIATED); |
| 620 | setStringArrayField(env, localeData, "shortMonthNames", shortMonthNames, count); |
Elliott Hughes | ad66a88 | 2012-08-06 10:47:32 -0700 | [diff] [blame] | 621 | const UnicodeString* tinyMonthNames = |
| 622 | dateFormatSym.getMonths(count, DateFormatSymbols::FORMAT, DateFormatSymbols::NARROW); |
| 623 | setStringArrayField(env, localeData, "tinyMonthNames", tinyMonthNames, count); |
claireho | 5b7b7fe | 2012-01-12 14:51:08 -0800 | [diff] [blame] | 624 | const UnicodeString* longWeekdayNames = |
| 625 | dateFormatSym.getWeekdays(count, DateFormatSymbols::FORMAT, DateFormatSymbols::WIDE); |
| 626 | setStringArrayField(env, localeData, "longWeekdayNames", longWeekdayNames, count); |
| 627 | const UnicodeString* shortWeekdayNames = |
| 628 | dateFormatSym.getWeekdays(count, DateFormatSymbols::FORMAT, DateFormatSymbols::ABBREVIATED); |
| 629 | setStringArrayField(env, localeData, "shortWeekdayNames", shortWeekdayNames, count); |
Elliott Hughes | ad66a88 | 2012-08-06 10:47:32 -0700 | [diff] [blame] | 630 | const UnicodeString* tinyWeekdayNames = |
| 631 | dateFormatSym.getWeekdays(count, DateFormatSymbols::FORMAT, DateFormatSymbols::NARROW); |
| 632 | setStringArrayField(env, localeData, "tinyWeekdayNames", tinyWeekdayNames, count); |
Elliott Hughes | 33aa6eb | 2009-12-22 17:20:44 -0800 | [diff] [blame] | 633 | |
claireho | 5b7b7fe | 2012-01-12 14:51:08 -0800 | [diff] [blame] | 634 | const UnicodeString* longStandAloneMonthNames = |
| 635 | dateFormatSym.getMonths(count, DateFormatSymbols::STANDALONE, DateFormatSymbols::WIDE); |
| 636 | setStringArrayField(env, localeData, "longStandAloneMonthNames", longStandAloneMonthNames, count); |
| 637 | const UnicodeString* shortStandAloneMonthNames = |
| 638 | dateFormatSym.getMonths(count, DateFormatSymbols::STANDALONE, DateFormatSymbols::ABBREVIATED); |
| 639 | setStringArrayField(env, localeData, "shortStandAloneMonthNames", shortStandAloneMonthNames, count); |
Elliott Hughes | ad66a88 | 2012-08-06 10:47:32 -0700 | [diff] [blame] | 640 | const UnicodeString* tinyStandAloneMonthNames = |
| 641 | dateFormatSym.getMonths(count, DateFormatSymbols::STANDALONE, DateFormatSymbols::NARROW); |
| 642 | setStringArrayField(env, localeData, "tinyStandAloneMonthNames", tinyStandAloneMonthNames, count); |
claireho | 5b7b7fe | 2012-01-12 14:51:08 -0800 | [diff] [blame] | 643 | const UnicodeString* longStandAloneWeekdayNames = |
| 644 | dateFormatSym.getWeekdays(count, DateFormatSymbols::STANDALONE, DateFormatSymbols::WIDE); |
| 645 | setStringArrayField(env, localeData, "longStandAloneWeekdayNames", longStandAloneWeekdayNames, count); |
| 646 | const UnicodeString* shortStandAloneWeekdayNames = |
| 647 | dateFormatSym.getWeekdays(count, DateFormatSymbols::STANDALONE, DateFormatSymbols::ABBREVIATED); |
| 648 | setStringArrayField(env, localeData, "shortStandAloneWeekdayNames", shortStandAloneWeekdayNames, count); |
Elliott Hughes | ad66a88 | 2012-08-06 10:47:32 -0700 | [diff] [blame] | 649 | const UnicodeString* tinyStandAloneWeekdayNames = |
| 650 | dateFormatSym.getWeekdays(count, DateFormatSymbols::STANDALONE, DateFormatSymbols::NARROW); |
| 651 | setStringArrayField(env, localeData, "tinyStandAloneWeekdayNames", tinyStandAloneWeekdayNames, count); |
Elliott Hughes | e2377cd | 2011-06-15 12:59:02 -0700 | [diff] [blame] | 652 | |
Elliott Hughes | 33aa6eb | 2009-12-22 17:20:44 -0800 | [diff] [blame] | 653 | status = U_ZERO_ERROR; |
| 654 | |
claireho | 3be1277 | 2011-06-09 10:53:06 -0700 | [diff] [blame] | 655 | // For numberPatterns and symbols. |
claireho | 5b7b7fe | 2012-01-12 14:51:08 -0800 | [diff] [blame] | 656 | setNumberPatterns(env, localeData, locale); |
| 657 | setDecimalFormatSymbolsData(env, localeData, locale); |
Elliott Hughes | 33aa6eb | 2009-12-22 17:20:44 -0800 | [diff] [blame] | 658 | |
Elliott Hughes | 7ff7010 | 2010-11-03 11:30:20 -0700 | [diff] [blame] | 659 | jstring countryCode = env->NewStringUTF(Locale::createFromName(localeName.c_str()).getCountry()); |
Elliott Hughes | 52b310a | 2011-03-03 21:22:39 -0800 | [diff] [blame] | 660 | jstring internationalCurrencySymbol = ICU_getCurrencyCode(env, NULL, countryCode); |
Elliott Hughes | e2377cd | 2011-06-15 12:59:02 -0700 | [diff] [blame] | 661 | env->DeleteLocalRef(countryCode); |
| 662 | countryCode = NULL; |
| 663 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 664 | jstring currencySymbol = NULL; |
Elliott Hughes | 33aa6eb | 2009-12-22 17:20:44 -0800 | [diff] [blame] | 665 | if (internationalCurrencySymbol != NULL) { |
Elliott Hughes | 007776e | 2012-11-02 14:54:02 -0700 | [diff] [blame] | 666 | currencySymbol = ICU_getCurrencySymbol(env, NULL, javaLocaleName, internationalCurrencySymbol); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 667 | } else { |
Elliott Hughes | 33aa6eb | 2009-12-22 17:20:44 -0800 | [diff] [blame] | 668 | internationalCurrencySymbol = env->NewStringUTF("XXX"); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 669 | } |
Elliott Hughes | 33aa6eb | 2009-12-22 17:20:44 -0800 | [diff] [blame] | 670 | if (currencySymbol == NULL) { |
| 671 | // This is the UTF-8 encoding of U+00A4 (CURRENCY SIGN). |
Hao Feng | ad884a7 | 2009-03-20 16:15:16 +0800 | [diff] [blame] | 672 | currencySymbol = env->NewStringUTF("\xc2\xa4"); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 673 | } |
Elliott Hughes | 33aa6eb | 2009-12-22 17:20:44 -0800 | [diff] [blame] | 674 | setStringField(env, localeData, "currencySymbol", currencySymbol); |
| 675 | setStringField(env, localeData, "internationalCurrencySymbol", internationalCurrencySymbol); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 676 | |
Elliott Hughes | 33aa6eb | 2009-12-22 17:20:44 -0800 | [diff] [blame] | 677 | return JNI_TRUE; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 678 | } |
| 679 | |
Elliott Hughes | dbbdffc | 2010-07-22 18:46:47 -0700 | [diff] [blame] | 680 | static jstring ICU_toLowerCase(JNIEnv* env, jclass, jstring javaString, jstring localeName) { |
Elliott Hughes | 3aac4dd | 2013-02-04 15:37:52 -0800 | [diff] [blame] | 681 | ScopedJavaUnicodeString scopedString(env, javaString); |
| 682 | if (!scopedString.valid()) { |
| 683 | return NULL; |
| 684 | } |
| 685 | UnicodeString& s(scopedString.unicodeString()); |
| 686 | UnicodeString original(s); |
Elliott Hughes | 8a491f8 | 2013-09-03 15:04:38 -0700 | [diff] [blame] | 687 | s.toLower(getLocale(env, localeName)); |
Elliott Hughes | 3aac4dd | 2013-02-04 15:37:52 -0800 | [diff] [blame] | 688 | return s == original ? javaString : env->NewString(s.getBuffer(), s.length()); |
Elliott Hughes | dbbdffc | 2010-07-22 18:46:47 -0700 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | static jstring ICU_toUpperCase(JNIEnv* env, jclass, jstring javaString, jstring localeName) { |
Elliott Hughes | 3aac4dd | 2013-02-04 15:37:52 -0800 | [diff] [blame] | 692 | ScopedJavaUnicodeString scopedString(env, javaString); |
| 693 | if (!scopedString.valid()) { |
| 694 | return NULL; |
| 695 | } |
| 696 | UnicodeString& s(scopedString.unicodeString()); |
| 697 | UnicodeString original(s); |
Elliott Hughes | 8a491f8 | 2013-09-03 15:04:38 -0700 | [diff] [blame] | 698 | s.toUpper(getLocale(env, localeName)); |
Elliott Hughes | 3aac4dd | 2013-02-04 15:37:52 -0800 | [diff] [blame] | 699 | return s == original ? javaString : env->NewString(s.getBuffer(), s.length()); |
Elliott Hughes | dbbdffc | 2010-07-22 18:46:47 -0700 | [diff] [blame] | 700 | } |
| 701 | |
Elliott Hughes | e0e5672 | 2011-02-10 15:18:33 -0800 | [diff] [blame] | 702 | static jstring versionString(JNIEnv* env, const UVersionInfo& version) { |
| 703 | char versionString[U_MAX_VERSION_STRING_LENGTH]; |
| 704 | u_versionToString(const_cast<UVersionInfo&>(version), &versionString[0]); |
| 705 | return env->NewStringUTF(versionString); |
| 706 | } |
| 707 | |
Elliott Hughes | 15d8280 | 2013-02-27 12:25:47 -0800 | [diff] [blame] | 708 | static jstring ICU_getCldrVersion(JNIEnv* env, jclass) { |
| 709 | UErrorCode status = U_ZERO_ERROR; |
| 710 | UVersionInfo cldrVersion; |
| 711 | ulocdata_getCLDRVersion(cldrVersion, &status); |
| 712 | return versionString(env, cldrVersion); |
| 713 | } |
| 714 | |
Elliott Hughes | e0e5672 | 2011-02-10 15:18:33 -0800 | [diff] [blame] | 715 | static jstring ICU_getIcuVersion(JNIEnv* env, jclass) { |
| 716 | UVersionInfo icuVersion; |
| 717 | u_getVersion(icuVersion); |
| 718 | return versionString(env, icuVersion); |
| 719 | } |
| 720 | |
| 721 | static jstring ICU_getUnicodeVersion(JNIEnv* env, jclass) { |
| 722 | UVersionInfo unicodeVersion; |
| 723 | u_getUnicodeVersion(unicodeVersion); |
| 724 | return versionString(env, unicodeVersion); |
| 725 | } |
| 726 | |
Elliott Hughes | 52b310a | 2011-03-03 21:22:39 -0800 | [diff] [blame] | 727 | static jobject ICU_getAvailableCurrencyCodes(JNIEnv* env, jclass) { |
Elliott Hughes | 783a57a | 2013-08-26 14:59:01 -0700 | [diff] [blame] | 728 | UErrorCode status = U_ZERO_ERROR; |
| 729 | UStringEnumeration e(ucurr_openISOCurrencies(UCURR_COMMON|UCURR_NON_DEPRECATED, &status)); |
| 730 | return fromStringEnumeration(env, status, "ucurr_openISOCurrencies", &e); |
Elliott Hughes | 52b310a | 2011-03-03 21:22:39 -0800 | [diff] [blame] | 731 | } |
| 732 | |
Elliott Hughes | 0fd776f | 2013-10-31 10:14:28 -0700 | [diff] [blame] | 733 | static jstring ICU_getBestDateTimePatternNative(JNIEnv* env, jclass, jstring javaSkeleton, jstring javaLocaleName) { |
Elliott Hughes | cb1b902 | 2012-10-04 12:22:20 -0700 | [diff] [blame] | 734 | Locale locale = getLocale(env, javaLocaleName); |
| 735 | UErrorCode status = U_ZERO_ERROR; |
Carton He | 364324d | 2013-09-02 17:19:28 +0800 | [diff] [blame] | 736 | UniquePtr<DateTimePatternGenerator> generator(DateTimePatternGenerator::createInstance(locale, status)); |
Elliott Hughes | cb1b902 | 2012-10-04 12:22:20 -0700 | [diff] [blame] | 737 | if (maybeThrowIcuException(env, "DateTimePatternGenerator::createInstance", status)) { |
| 738 | return NULL; |
| 739 | } |
| 740 | |
Elliott Hughes | 480537d | 2013-05-03 13:07:18 -0700 | [diff] [blame] | 741 | ScopedJavaUnicodeString skeletonHolder(env, javaSkeleton); |
| 742 | if (!skeletonHolder.valid()) { |
Elliott Hughes | 3aac4dd | 2013-02-04 15:37:52 -0800 | [diff] [blame] | 743 | return NULL; |
| 744 | } |
Elliott Hughes | 480537d | 2013-05-03 13:07:18 -0700 | [diff] [blame] | 745 | UnicodeString result(generator->getBestPattern(skeletonHolder.unicodeString(), status)); |
Elliott Hughes | cb1b902 | 2012-10-04 12:22:20 -0700 | [diff] [blame] | 746 | if (maybeThrowIcuException(env, "DateTimePatternGenerator::getBestPattern", status)) { |
| 747 | return NULL; |
| 748 | } |
| 749 | |
| 750 | return env->NewString(result.getBuffer(), result.length()); |
| 751 | } |
| 752 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 753 | static JNINativeMethod gMethods[] = { |
Fabrice Di Meglio | 0452e9a | 2011-06-07 19:17:12 -0700 | [diff] [blame] | 754 | NATIVE_METHOD(ICU, addLikelySubtags, "(Ljava/lang/String;)Ljava/lang/String;"), |
Elliott Hughes | e22935d | 2010-08-12 17:27:27 -0700 | [diff] [blame] | 755 | NATIVE_METHOD(ICU, getAvailableBreakIteratorLocalesNative, "()[Ljava/lang/String;"), |
| 756 | NATIVE_METHOD(ICU, getAvailableCalendarLocalesNative, "()[Ljava/lang/String;"), |
| 757 | NATIVE_METHOD(ICU, getAvailableCollatorLocalesNative, "()[Ljava/lang/String;"), |
Elliott Hughes | 52b310a | 2011-03-03 21:22:39 -0800 | [diff] [blame] | 758 | NATIVE_METHOD(ICU, getAvailableCurrencyCodes, "()[Ljava/lang/String;"), |
Elliott Hughes | e22935d | 2010-08-12 17:27:27 -0700 | [diff] [blame] | 759 | NATIVE_METHOD(ICU, getAvailableDateFormatLocalesNative, "()[Ljava/lang/String;"), |
| 760 | NATIVE_METHOD(ICU, getAvailableLocalesNative, "()[Ljava/lang/String;"), |
| 761 | NATIVE_METHOD(ICU, getAvailableNumberFormatLocalesNative, "()[Ljava/lang/String;"), |
Elliott Hughes | 0fd776f | 2013-10-31 10:14:28 -0700 | [diff] [blame] | 762 | NATIVE_METHOD(ICU, getBestDateTimePatternNative, "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"), |
Elliott Hughes | 15d8280 | 2013-02-27 12:25:47 -0800 | [diff] [blame] | 763 | NATIVE_METHOD(ICU, getCldrVersion, "()Ljava/lang/String;"), |
Elliott Hughes | 52b310a | 2011-03-03 21:22:39 -0800 | [diff] [blame] | 764 | NATIVE_METHOD(ICU, getCurrencyCode, "(Ljava/lang/String;)Ljava/lang/String;"), |
| 765 | NATIVE_METHOD(ICU, getCurrencyDisplayName, "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"), |
| 766 | NATIVE_METHOD(ICU, getCurrencyFractionDigits, "(Ljava/lang/String;)I"), |
| 767 | NATIVE_METHOD(ICU, getCurrencySymbol, "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"), |
Elliott Hughes | e22935d | 2010-08-12 17:27:27 -0700 | [diff] [blame] | 768 | NATIVE_METHOD(ICU, getDisplayCountryNative, "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"), |
| 769 | NATIVE_METHOD(ICU, getDisplayLanguageNative, "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"), |
Narayan Kamath | c5b1eb1 | 2014-01-10 14:54:31 +0000 | [diff] [blame] | 770 | NATIVE_METHOD(ICU, getDisplayScriptNative, "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"), |
Elliott Hughes | e22935d | 2010-08-12 17:27:27 -0700 | [diff] [blame] | 771 | NATIVE_METHOD(ICU, getDisplayVariantNative, "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"), |
| 772 | NATIVE_METHOD(ICU, getISO3CountryNative, "(Ljava/lang/String;)Ljava/lang/String;"), |
| 773 | NATIVE_METHOD(ICU, getISO3LanguageNative, "(Ljava/lang/String;)Ljava/lang/String;"), |
| 774 | NATIVE_METHOD(ICU, getISOCountriesNative, "()[Ljava/lang/String;"), |
| 775 | NATIVE_METHOD(ICU, getISOLanguagesNative, "()[Ljava/lang/String;"), |
Elliott Hughes | e0e5672 | 2011-02-10 15:18:33 -0800 | [diff] [blame] | 776 | NATIVE_METHOD(ICU, getIcuVersion, "()Ljava/lang/String;"), |
Fabrice Di Meglio | 4c94a61 | 2011-06-15 17:23:43 -0700 | [diff] [blame] | 777 | NATIVE_METHOD(ICU, getScript, "(Ljava/lang/String;)Ljava/lang/String;"), |
Elliott Hughes | e0e5672 | 2011-02-10 15:18:33 -0800 | [diff] [blame] | 778 | NATIVE_METHOD(ICU, getUnicodeVersion, "()Ljava/lang/String;"), |
Narayan Kamath | c5b1eb1 | 2014-01-10 14:54:31 +0000 | [diff] [blame] | 779 | NATIVE_METHOD(ICU, languageTagForLocale, "(Ljava/lang/String;)Ljava/lang/String;"), |
| 780 | NATIVE_METHOD(ICU, localeForLanguageTag, "(Ljava/lang/String;Z)Ljava/lang/String;"), |
Elliott Hughes | 0fd776f | 2013-10-31 10:14:28 -0700 | [diff] [blame] | 781 | NATIVE_METHOD(ICU, initLocaleDataNative, "(Ljava/lang/String;Llibcore/icu/LocaleData;)Z"), |
Elliott Hughes | e22935d | 2010-08-12 17:27:27 -0700 | [diff] [blame] | 782 | NATIVE_METHOD(ICU, toLowerCase, "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"), |
| 783 | NATIVE_METHOD(ICU, toUpperCase, "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"), |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 784 | }; |
Elliott Hughes | 7cd6760 | 2012-05-03 17:21:04 -0700 | [diff] [blame] | 785 | void register_libcore_icu_ICU(JNIEnv* env) { |
Elliott Hughes | 566bbb1 | 2011-02-11 18:23:35 -0800 | [diff] [blame] | 786 | std::string path; |
| 787 | path = u_getDataDirectory(); |
| 788 | path += "/"; |
| 789 | path += U_ICUDATA_NAME; |
| 790 | path += ".dat"; |
Elliott Hughes | 9f557fa | 2011-02-22 11:50:18 -0800 | [diff] [blame] | 791 | |
| 792 | #define FAIL_WITH_STRERROR(s) \ |
Steve Block | 679cf68 | 2012-01-08 10:18:49 +0000 | [diff] [blame] | 793 | ALOGE("Couldn't " s " '%s': %s", path.c_str(), strerror(errno)); \ |
Elliott Hughes | 7cd6760 | 2012-05-03 17:21:04 -0700 | [diff] [blame] | 794 | abort(); |
Elliott Hughes | 9f557fa | 2011-02-22 11:50:18 -0800 | [diff] [blame] | 795 | #define MAYBE_FAIL_WITH_ICU_ERROR(s) \ |
| 796 | if (status != U_ZERO_ERROR) {\ |
Steve Block | 679cf68 | 2012-01-08 10:18:49 +0000 | [diff] [blame] | 797 | ALOGE("Couldn't initialize ICU (" s "): %s (%s)", u_errorName(status), path.c_str()); \ |
Elliott Hughes | 7cd6760 | 2012-05-03 17:21:04 -0700 | [diff] [blame] | 798 | abort(); \ |
Elliott Hughes | 9f557fa | 2011-02-22 11:50:18 -0800 | [diff] [blame] | 799 | } |
Elliott Hughes | 566bbb1 | 2011-02-11 18:23:35 -0800 | [diff] [blame] | 800 | |
| 801 | // Open the file and get its length. |
| 802 | ScopedFd fd(open(path.c_str(), O_RDONLY)); |
| 803 | if (fd.get() == -1) { |
Elliott Hughes | 9f557fa | 2011-02-22 11:50:18 -0800 | [diff] [blame] | 804 | FAIL_WITH_STRERROR("open"); |
Elliott Hughes | 566bbb1 | 2011-02-11 18:23:35 -0800 | [diff] [blame] | 805 | } |
| 806 | struct stat sb; |
| 807 | if (fstat(fd.get(), &sb) == -1) { |
Elliott Hughes | 9f557fa | 2011-02-22 11:50:18 -0800 | [diff] [blame] | 808 | FAIL_WITH_STRERROR("stat"); |
Elliott Hughes | 566bbb1 | 2011-02-11 18:23:35 -0800 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | // Map it. |
| 812 | void* data = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, fd.get(), 0); |
| 813 | if (data == MAP_FAILED) { |
Elliott Hughes | 9f557fa | 2011-02-22 11:50:18 -0800 | [diff] [blame] | 814 | FAIL_WITH_STRERROR("mmap"); |
Elliott Hughes | 566bbb1 | 2011-02-11 18:23:35 -0800 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | // Tell the kernel that accesses are likely to be random rather than sequential. |
| 818 | if (madvise(data, sb.st_size, MADV_RANDOM) == -1) { |
Elliott Hughes | 9f557fa | 2011-02-22 11:50:18 -0800 | [diff] [blame] | 819 | FAIL_WITH_STRERROR("madvise(MADV_RANDOM)"); |
Elliott Hughes | 566bbb1 | 2011-02-11 18:23:35 -0800 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | // Tell ICU to use our memory-mapped data. |
| 823 | UErrorCode status = U_ZERO_ERROR; |
| 824 | udata_setCommonData(data, &status); |
Elliott Hughes | 9f557fa | 2011-02-22 11:50:18 -0800 | [diff] [blame] | 825 | MAYBE_FAIL_WITH_ICU_ERROR("udata_setCommonData"); |
Elliott Hughes | 566bbb1 | 2011-02-11 18:23:35 -0800 | [diff] [blame] | 826 | // Tell ICU it can *only* use our memory-mapped data. |
| 827 | udata_setFileAccess(UDATA_NO_FILES, &status); |
Elliott Hughes | 9f557fa | 2011-02-22 11:50:18 -0800 | [diff] [blame] | 828 | MAYBE_FAIL_WITH_ICU_ERROR("udata_setFileAccess"); |
Elliott Hughes | 566bbb1 | 2011-02-11 18:23:35 -0800 | [diff] [blame] | 829 | |
Elliott Hughes | 972d9be | 2010-09-16 16:38:23 -0700 | [diff] [blame] | 830 | // Failures to find the ICU data tend to be somewhat obscure because ICU loads its data on first |
| 831 | // use, which can be anywhere. Force initialization up front so we can report a nice clear error |
| 832 | // and bail. |
Elliott Hughes | 972d9be | 2010-09-16 16:38:23 -0700 | [diff] [blame] | 833 | u_init(&status); |
Elliott Hughes | 9f557fa | 2011-02-22 11:50:18 -0800 | [diff] [blame] | 834 | MAYBE_FAIL_WITH_ICU_ERROR("u_init"); |
Elliott Hughes | 7cd6760 | 2012-05-03 17:21:04 -0700 | [diff] [blame] | 835 | jniRegisterNativeMethods(env, "libcore/icu/ICU", gMethods, NELEM(gMethods)); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 836 | } |