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