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_NDEBUG 1
18 #define LOG_TAG "ICU"
19
20 #include <memory>
21 #include <vector>
22
23 #include <aicu/AIcu.h>
24
25 #include <log/log.h>
26 #include <nativehelper/JNIHelp.h>
27 #include <nativehelper/ScopedUtfChars.h>
28 #include <nativehelper/jni_macros.h>
29 #include <nativehelper/toStringArray.h>
30
31 #include "IcuUtilities.h"
32 #include "JniException.h"
33 #include "ScopedIcuULoc.h"
34 #include "unicode/char16ptr.h"
35 #include "unicode/uloc.h"
36 #include "unicode/ustring.h"
37
38 #define U_ICUDATA_CURR U_ICUDATA_NAME "-" "curr"
39
ICU_getScript(JNIEnv * env,jclass,jstring javaLocaleName)40 static jstring ICU_getScript(JNIEnv* env, jclass, jstring javaLocaleName) {
41 ScopedIcuULoc icuLocale(env, javaLocaleName);
42 if (!icuLocale.valid()) {
43 return NULL;
44 }
45 // Normal script part is 4-char long. Being conservative for allocation size
46 // because if the locale contains script part, it should not be longer than the locale itself.
47 int32_t capacity = std::max(ULOC_SCRIPT_CAPACITY, icuLocale.locale_length() + 1);
48 std::unique_ptr<char[]> buffer(new char(capacity));
49 UErrorCode status = U_ZERO_ERROR;
50 uloc_getScript(icuLocale.locale(), buffer.get(), capacity, &status);
51 if (U_FAILURE(status)) {
52 return NULL;
53 }
54 return env->NewStringUTF(buffer.get());
55 }
56
ICU_getISO3Country(JNIEnv * env,jclass,jstring javaLanguageTag)57 static jstring ICU_getISO3Country(JNIEnv* env, jclass, jstring javaLanguageTag) {
58 ScopedIcuULoc icuLocale(env, javaLanguageTag);
59 if (!icuLocale.valid()) {
60 return NULL;
61 }
62 return env->NewStringUTF(uloc_getISO3Country(icuLocale.locale()));
63 }
64
ICU_getISO3Language(JNIEnv * env,jclass,jstring javaLanguageTag)65 static jstring ICU_getISO3Language(JNIEnv* env, jclass, jstring javaLanguageTag) {
66 ScopedIcuULoc icuLocale(env, javaLanguageTag);
67 if (!icuLocale.valid()) {
68 return NULL;
69 }
70 return env->NewStringUTF(uloc_getISO3Language(icuLocale.locale()));
71 }
72
ICU_getISOCountriesNative(JNIEnv * env,jclass)73 static jobjectArray ICU_getISOCountriesNative(JNIEnv* env, jclass) {
74 return toStringArray(env, uloc_getISOCountries());
75 }
76
ICU_getISOLanguagesNative(JNIEnv * env,jclass)77 static jobjectArray ICU_getISOLanguagesNative(JNIEnv* env, jclass) {
78 return toStringArray(env, uloc_getISOLanguages());
79 }
80
ICU_getAvailableLocalesNative(JNIEnv * env,jclass)81 static jobjectArray ICU_getAvailableLocalesNative(JNIEnv* env, jclass) {
82 return toStringArray(env, uloc_countAvailable, uloc_getAvailable);
83 }
84
ICU_setDefaultLocale(JNIEnv * env,jclass,jstring javaLanguageTag)85 static void ICU_setDefaultLocale(JNIEnv* env, jclass, jstring javaLanguageTag) {
86 ScopedIcuULoc icuLocale(env, javaLanguageTag);
87 if (!icuLocale.valid()) {
88 return;
89 }
90
91 UErrorCode status = U_ZERO_ERROR;
92 uloc_setDefault(icuLocale.locale(), &status);
93 maybeThrowIcuException(env, "uloc_setDefault", status);
94 }
95
ICU_getDefaultLocale(JNIEnv * env,jclass)96 static jstring ICU_getDefaultLocale(JNIEnv* env, jclass) {
97 return env->NewStringUTF(uloc_getDefault());
98 }
99
100 static JNINativeMethod gMethods[] = {
101 NATIVE_METHOD(ICU, getAvailableLocalesNative, "()[Ljava/lang/String;"),
102 NATIVE_METHOD(ICU, getDefaultLocale, "()Ljava/lang/String;"),
103 NATIVE_METHOD(ICU, getISO3Country, "(Ljava/lang/String;)Ljava/lang/String;"),
104 NATIVE_METHOD(ICU, getISO3Language, "(Ljava/lang/String;)Ljava/lang/String;"),
105 NATIVE_METHOD(ICU, getISOCountriesNative, "()[Ljava/lang/String;"),
106 NATIVE_METHOD(ICU, getISOLanguagesNative, "()[Ljava/lang/String;"),
107 NATIVE_METHOD(ICU, getScript, "(Ljava/lang/String;)Ljava/lang/String;"),
108 NATIVE_METHOD(ICU, setDefaultLocale, "(Ljava/lang/String;)V"),
109 };
110
111 //
112 // Global initialization & Teardown for ICU Setup
113 // - Contains handlers for JNI_OnLoad and JNI_OnUnload
114 //
115
116 // Init ICU, configuring it and loading the data files.
register_libcore_icu_ICU(JNIEnv * env)117 void register_libcore_icu_ICU(JNIEnv* env) {
118 AIcu_register();
119
120 jniRegisterNativeMethods(env, "libcore/icu/ICU", gMethods, NELEM(gMethods));
121 }
122
123 // De-init ICU, unloading the data files. Do the opposite of the above function.
unregister_libcore_icu_ICU()124 void unregister_libcore_icu_ICU() {
125 // Skip unregistering JNI methods explicitly, class unloading takes care of
126 // it.
127 AIcu_deregister();
128 }
129