Skip to content

Commit 8e22194

Browse files
kyle-sylvestreslouken
authored andcommitted
get preferred locales on android
1 parent c08b104 commit 8e22194

File tree

3 files changed

+62
-57
lines changed

3 files changed

+62
-57
lines changed

android-project/app/proguard-rules.pro

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
boolean supportsRelativeMouse();
5151
int openFileDescriptor(java.lang.String, java.lang.String);
5252
boolean showFileDialog(java.lang.String[], boolean, boolean, int);
53+
java.lang.String getPreferredLocales();
54+
java.lang.String formatLocale(java.util.Locale);
5355
}
5456

5557
-keep,includedescriptorclasses,allowoptimization class org.libsdl.app.HIDDeviceManager {

android-project/app/src/main/java/org/libsdl/app/SDLActivity.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import android.os.Build;
2424
import android.os.Bundle;
2525
import android.os.Handler;
26+
import android.os.LocaleList;
2627
import android.os.Message;
2728
import android.os.ParcelFileDescriptor;
2829
import android.util.DisplayMetrics;
@@ -2116,6 +2117,48 @@ static class SDLFileDialogState {
21162117
int requestCode;
21172118
boolean multipleChoice;
21182119
}
2120+
2121+
/**
2122+
* This method is called by SDL using JNI.
2123+
*/
2124+
public static String getPreferredLocales() {
2125+
String result = "";
2126+
if (Build.VERSION.SDK_INT >= 24 /* Android 7 (N) */) {
2127+
LocaleList locales = LocaleList.getAdjustedDefault();
2128+
for (int i = 0; i < locales.size(); i++) {
2129+
if (i != 0) result += ",";
2130+
result += formatLocale(locales.get(i));
2131+
}
2132+
}
2133+
else if (mCurrentLocale != null) {
2134+
result = formatLocale(mCurrentLocale);
2135+
}
2136+
return result;
2137+
}
2138+
2139+
public static String formatLocale(Locale locale) {
2140+
String result = "";
2141+
String lang = "";
2142+
if (locale.getLanguage() == "in") {
2143+
// Indonesian is "id" according to ISO 639.2, but on Android is "in" because of Java backwards compatibility
2144+
lang = "id";
2145+
}
2146+
else if (locale.getLanguage() == "") {
2147+
// Make sure language is never empty
2148+
lang = "und";
2149+
}
2150+
else {
2151+
lang = locale.getLanguage();
2152+
}
2153+
2154+
if (locale.getCountry() == "") {
2155+
result = lang;
2156+
}
2157+
else {
2158+
result = lang + "_" + locale.getCountry();
2159+
}
2160+
return result;
2161+
}
21192162
}
21202163

21212164
/**

src/core/android/SDL_android.c

Lines changed: 17 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ static jmethodID midShowTextInput;
371371
static jmethodID midSupportsRelativeMouse;
372372
static jmethodID midOpenFileDescriptor;
373373
static jmethodID midShowFileDialog;
374+
static jmethodID midGetPreferredLocales;
374375

375376
// audio manager
376377
static jclass mAudioManagerClass;
@@ -660,6 +661,7 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetupJNI)(JNIEnv *env, jclass cl
660661
midSupportsRelativeMouse = (*env)->GetStaticMethodID(env, mActivityClass, "supportsRelativeMouse", "()Z");
661662
midOpenFileDescriptor = (*env)->GetStaticMethodID(env, mActivityClass, "openFileDescriptor", "(Ljava/lang/String;Ljava/lang/String;)I");
662663
midShowFileDialog = (*env)->GetStaticMethodID(env, mActivityClass, "showFileDialog", "([Ljava/lang/String;ZZI)Z");
664+
midGetPreferredLocales = (*env)->GetStaticMethodID(env, mActivityClass, "getPreferredLocales", "()Ljava/lang/String;");
663665

664666
if (!midClipboardGetText ||
665667
!midClipboardHasText ||
@@ -691,7 +693,8 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetupJNI)(JNIEnv *env, jclass cl
691693
!midShowTextInput ||
692694
!midSupportsRelativeMouse ||
693695
!midOpenFileDescriptor ||
694-
!midShowFileDialog) {
696+
!midShowFileDialog ||
697+
!midGetPreferredLocales) {
695698
__android_log_print(ANDROID_LOG_WARN, "SDL", "Missing some Java callbacks, do you have the latest version of SDLActivity.java?");
696699
}
697700

@@ -2585,65 +2588,22 @@ bool Android_JNI_ShowToast(const char *message, int duration, int gravity, int x
25852588

25862589
bool Android_JNI_GetLocale(char *buf, size_t buflen)
25872590
{
2588-
AConfiguration *cfg;
2589-
2590-
SDL_assert(buflen > 6);
2591-
2592-
// Need to re-create the asset manager if locale has changed (SDL_EVENT_LOCALE_CHANGED)
2593-
Internal_Android_Destroy_AssetManager();
2594-
2595-
if (!asset_manager) {
2596-
Internal_Android_Create_AssetManager();
2597-
}
2598-
2599-
if (!asset_manager) {
2600-
return false;
2601-
}
2602-
2603-
cfg = AConfiguration_new();
2604-
if (!cfg) {
2605-
return false;
2606-
}
2607-
2608-
{
2609-
char language[2] = {};
2610-
char country[2] = {};
2611-
size_t id = 0;
2612-
2613-
AConfiguration_fromAssetManager(cfg, asset_manager);
2614-
AConfiguration_getLanguage(cfg, language);
2615-
AConfiguration_getCountry(cfg, country);
2616-
2617-
// Indonesian is "id" according to ISO 639.2, but on Android is "in" because of Java backwards compatibility
2618-
if (language[0] == 'i' && language[1] == 'n') {
2619-
language[1] = 'd';
2620-
}
2621-
2622-
// copy language (not null terminated)
2623-
if (language[0]) {
2624-
buf[id++] = language[0];
2625-
if (language[1]) {
2626-
buf[id++] = language[1];
2627-
}
2628-
}
2629-
2630-
buf[id++] = '_';
2631-
2632-
// copy country (not null terminated)
2633-
if (country[0]) {
2634-
buf[id++] = country[0];
2635-
if (country[1]) {
2636-
buf[id++] = country[1];
2591+
bool result = false;
2592+
if (buf && buflen > 0) {
2593+
*buf = '\0';
2594+
JNIEnv *env = Android_JNI_GetEnv();
2595+
jstring string = (jstring)(*env)->CallStaticObjectMethod(env, mActivityClass, midGetPreferredLocales);
2596+
if (string) {
2597+
const char *utf8string = (*env)->GetStringUTFChars(env, string, NULL);
2598+
if (utf8string) {
2599+
result = true;
2600+
SDL_strlcpy(buf, utf8string, buflen);
2601+
(*env)->ReleaseStringUTFChars(env, string, utf8string);
26372602
}
2603+
(*env)->DeleteLocalRef(env, string);
26382604
}
2639-
2640-
buf[id++] = '\0';
2641-
SDL_assert(id <= buflen);
26422605
}
2643-
2644-
AConfiguration_delete(cfg);
2645-
2646-
return true;
2606+
return result;
26472607
}
26482608

26492609
bool Android_JNI_OpenURL(const char *url)

0 commit comments

Comments
 (0)