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