-
Notifications
You must be signed in to change notification settings - Fork 2
/
locale_android.c
243 lines (193 loc) · 7.5 KB
/
locale_android.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// +build android
#include <android/log.h>
#include <jni.h>
#include <stdlib.h>
#include <string.h>
#define LOG_FATAL(...) __android_log_print(ANDROID_LOG_FATAL, "GoLog", __VA_ARGS__)
static const char *jstringToCharCopy(JNIEnv *env, const jstring string)
{
const char *chars = (*env)->GetStringUTFChars(env, string, NULL);
const char *copy = strdup(chars);
(*env)->ReleaseStringUTFChars(env, string, chars);
return copy;
}
static jclass findClass(JNIEnv *env, const char *class_name)
{
jclass clazz = (*env)->FindClass(env, class_name);
if (clazz == NULL)
{
(*env)->ExceptionClear(env);
LOG_FATAL("cannot find %s", class_name);
return NULL;
}
return clazz;
}
static jmethodID findMethod(JNIEnv *env, jclass clazz, const char *name, const char *sig)
{
jmethodID m = (*env)->GetMethodID(env, clazz, name, sig);
if (m == 0)
{
(*env)->ExceptionClear(env);
LOG_FATAL("cannot find method %s %s", name, sig);
return 0;
}
return m;
}
static jfieldID findField(JNIEnv *env, jclass clazz, const char *name, const char *sig)
{
jfieldID f = (*env)->GetFieldID(env, clazz, name, sig);
if (f == 0)
{
(*env)->ExceptionClear(env);
LOG_FATAL("cannot find method %s %s", name, sig);
return 0;
}
return f;
}
static jfieldID getStaticFieldID(JNIEnv *env, jclass clazz, const char *name, const char *sig)
{
jfieldID f = (*env)->GetStaticFieldID(env, clazz, name, sig);
if (f == 0)
{
(*env)->ExceptionClear(env);
LOG_FATAL("cannot find static field %s %s", name, sig);
return 0;
}
return f;
}
static const char *toLanguageTag(JNIEnv *env, jobject locale)
{
const jclass java_util_Locale = findClass(env, "java/util/Locale");
const jstring localeStr =
(*env)->CallObjectMethod(
env,
locale,
(*env)->GetMethodID(env, java_util_Locale, "toLanguageTag", "()Ljava/lang/String;"));
return jstringToCharCopy(env, localeStr);
}
static const char *toLanguageTags(JNIEnv *env, jobject locales, jclass android_os_LocaleList)
{
const jstring localeStr =
(*env)->CallObjectMethod(
env,
locales,
(*env)->GetMethodID(env, android_os_LocaleList, "toLanguageTags", "()Ljava/lang/String;"));
return jstringToCharCopy(env, localeStr);
}
static int getAPIVersion(JNIEnv *env)
{
// VERSION is a nested class within android.os.Build (hence "$" rather than "/")
const jclass versionClass = findClass(env, "android/os/Build$VERSION");
const jfieldID sdkIntFieldID = getStaticFieldID(env, versionClass, "SDK_INT", "I");
int sdkInt = (*env)->GetStaticIntField(env, versionClass, sdkIntFieldID);
return sdkInt;
}
static const jobject getConfiguration(JNIEnv *env, jobject context)
{
const jclass android_content_ContextWrapper = findClass(env, "android/content/ContextWrapper");
const jclass android_content_res_Resources = findClass(env, "android/content/res/Resources");
const jobject resources =
(*env)->CallObjectMethod(
env,
context,
findMethod(env, android_content_ContextWrapper, "getResources", "()Landroid/content/res/Resources;"));
const jobject configuration =
(*env)->CallObjectMethod(
env,
resources,
findMethod(env, android_content_res_Resources, "getConfiguration", "()Landroid/content/res/Configuration;"));
return configuration;
}
static const jobject getLocaleObject(JNIEnv *env, jobject context)
{
const jobject configuration = getConfiguration(env, context);
const jclass android_content_res_Configuration = findClass(env, "android/content/res/Configuration");
int version = getAPIVersion(env);
// Android N or later
// See https://developer.android.com/reference/android/content/res/Configuration#locale
if (version >= 24) {
const jclass android_os_LocaleList = findClass(env, "android/os/LocaleList");
const jobject locales =
(*env)->CallObjectMethod(
env,
configuration,
findMethod(env, android_content_res_Configuration, "getLocales", "()Landroid/os/LocaleList;"));
return (*env)->CallObjectMethod(
env,
locales,
findMethod(env, android_os_LocaleList, "get", "(I)Ljava/util/Locale;"),
0);
} else {
return (*env)->GetObjectField(
env,
configuration,
findField(env, android_content_res_Configuration, "locale", "Ljava/util/Locale;"));
}
}
// Basically the same as `getResources().getConfiguration().getLocales()` for Android N and later,
// or `getResources().getConfiguration().locale` for earlier Android version.
const char *getLocales(uintptr_t java_vm, uintptr_t jni_env, uintptr_t ctx)
{
JavaVM *vm = (JavaVM *)java_vm;
JNIEnv *env = (JNIEnv *)jni_env;
jobject context = (jobject)ctx;
const jobject configuration = getConfiguration(env, context);
const jclass android_content_res_Configuration = findClass(env, "android/content/res/Configuration");
int version = getAPIVersion(env);
// Android N or later
// See https://developer.android.com/reference/android/content/res/Configuration#locale
if (version >= 24) {
const jclass android_os_LocaleList = findClass(env, "android/os/LocaleList");
const jobject locales =
(*env)->CallObjectMethod(
env,
configuration,
findMethod(env, android_content_res_Configuration, "getLocales", "()Landroid/os/LocaleList;"));
return toLanguageTags(env, locales, android_os_LocaleList);
} else {
const jobject locale =
(*env)->GetObjectField(
env,
configuration,
findField(env, android_content_res_Configuration, "locale", "Ljava/util/Locale;"));
return toLanguageTag(env, locale);
}
}
// Basically the same as `getResources().getConfiguration().getLocales().get(0).toString()` for Android N and later,
// or `getResources().getConfiguration().locale` for earlier Android version.
const char *getLocale(uintptr_t java_vm, uintptr_t jni_env, uintptr_t ctx)
{
JavaVM *vm = (JavaVM *)java_vm;
JNIEnv *env = (JNIEnv *)jni_env;
jobject context = (jobject)ctx;
const jobject locale = getLocaleObject(env, context);
return toLanguageTag(env, locale);
}
const char *getLanguage(uintptr_t java_vm, uintptr_t jni_env, uintptr_t ctx)
{
JavaVM *vm = (JavaVM *)java_vm;
JNIEnv *env = (JNIEnv *)jni_env;
jobject context = (jobject)ctx;
const jobject locale = getLocaleObject(env, context);
const jclass java_util_Locale = findClass(env, "java/util/Locale");
const jstring language =
(*env)->CallObjectMethod(
env,
locale,
(*env)->GetMethodID(env, java_util_Locale, "getLanguage", "()Ljava/lang/String;"));
return jstringToCharCopy(env, language);
}
const char *getRegion(uintptr_t java_vm, uintptr_t jni_env, uintptr_t ctx)
{
JavaVM *vm = (JavaVM *)java_vm;
JNIEnv *env = (JNIEnv *)jni_env;
jobject context = (jobject)ctx;
const jobject locale = getLocaleObject(env, context);
const jclass java_util_Locale = findClass(env, "java/util/Locale");
const jstring country =
(*env)->CallObjectMethod(
env,
locale,
(*env)->GetMethodID(env, java_util_Locale, "getCountry", "()Ljava/lang/String;"));
return jstringToCharCopy(env, country);
}