Android: send SDL_LOCALECHANGED when locale changes
authorSylvain Becker <sylvain.becker@gmail.com>
Fri, 08 May 2020 21:40:28 +0200
changeset 138001e7e8c85a056
parent 13799 4a352d1323d8
child 13801 4298bf108b06
Android: send SDL_LOCALECHANGED when locale changes
android-project/app/src/main/AndroidManifest.xml
android-project/app/src/main/java/org/libsdl/app/SDLActivity.java
src/core/android/SDL_android.c
     1.1 --- a/android-project/app/src/main/AndroidManifest.xml	Fri May 08 11:15:38 2020 +0200
     1.2 +++ b/android-project/app/src/main/AndroidManifest.xml	Fri May 08 21:40:28 2020 +0200
     1.3 @@ -70,7 +70,7 @@
     1.4              android:label="@string/app_name"
     1.5              android:alwaysRetainTaskState="true"
     1.6              android:launchMode="singleInstance"
     1.7 -            android:configChanges="orientation|uiMode|screenLayout|screenSize|smallestScreenSize|keyboard|keyboardHidden|navigation"
     1.8 +            android:configChanges="layoutDirection|locale|orientation|uiMode|screenLayout|screenSize|smallestScreenSize|keyboard|keyboardHidden|navigation"
     1.9              >
    1.10              <intent-filter>
    1.11                  <action android:name="android.intent.action.MAIN" />
     2.1 --- a/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java	Fri May 08 11:15:38 2020 +0200
     2.2 +++ b/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java	Fri May 08 21:40:28 2020 +0200
     2.3 @@ -4,6 +4,7 @@
     2.4  import java.io.InputStream;
     2.5  import java.util.Arrays;
     2.6  import java.util.Hashtable;
     2.7 +import java.util.Locale;
     2.8  import java.lang.reflect.Method;
     2.9  import java.lang.Math;
    2.10  
    2.11 @@ -62,6 +63,7 @@
    2.12      protected static final int SDL_ORIENTATION_PORTRAIT_FLIPPED = 4;
    2.13  
    2.14      protected static int mCurrentOrientation;
    2.15 +    protected static Locale mCurrentLocale;
    2.16  
    2.17      // Handle the state of the native layer
    2.18      public enum NativeState {
    2.19 @@ -258,6 +260,15 @@
    2.20          // Only record current orientation
    2.21          SDLActivity.onNativeOrientationChanged(mCurrentOrientation);
    2.22  
    2.23 +        try {
    2.24 +            if (Build.VERSION.SDK_INT < 24) {
    2.25 +                mCurrentLocale = getContext().getResources().getConfiguration().locale;
    2.26 +            } else {
    2.27 +                mCurrentLocale = getContext().getResources().getConfiguration().getLocales().get(0);
    2.28 +            }
    2.29 +        } catch(Exception ignored) {
    2.30 +        }
    2.31 +
    2.32          setContentView(mLayout);
    2.33  
    2.34          setWindowStyle(false);
    2.35 @@ -408,6 +419,17 @@
    2.36      }
    2.37  
    2.38      @Override
    2.39 +    public void onConfigurationChanged(Configuration newConfig) {
    2.40 +        Log.v(TAG, "onConfigurationChanged()");
    2.41 +        super.onConfigurationChanged(newConfig);
    2.42 +
    2.43 +        if (!mCurrentLocale.equals(newConfig.locale)) {
    2.44 +            mCurrentLocale = newConfig.locale;
    2.45 +            SDLActivity.onNativeLocaleChanged();
    2.46 +        }
    2.47 +    }
    2.48 +
    2.49 +    @Override
    2.50      protected void onDestroy() {
    2.51          Log.v(TAG, "onDestroy()");
    2.52  
    2.53 @@ -784,6 +806,7 @@
    2.54      public static native void onNativeOrientationChanged(int orientation);
    2.55      public static native void nativeAddTouch(int touchId, String name);
    2.56      public static native void nativePermissionResult(int requestCode, boolean result);
    2.57 +    public static native void onNativeLocaleChanged();
    2.58  
    2.59      /**
    2.60       * This method is called by SDL using JNI.
     3.1 --- a/src/core/android/SDL_android.c	Fri May 08 11:15:38 2020 +0200
     3.2 +++ b/src/core/android/SDL_android.c	Fri May 08 21:40:28 2020 +0200
     3.3 @@ -127,6 +127,9 @@
     3.4  JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeLowMemory)(
     3.5          JNIEnv *env, jclass cls);
     3.6  
     3.7 +JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeLocaleChanged)(
     3.8 +        JNIEnv *env, jclass cls);
     3.9 +
    3.10  JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSendQuit)(
    3.11          JNIEnv *env, jclass cls);
    3.12  
    3.13 @@ -180,6 +183,7 @@
    3.14      { "onNativeAccel",              "(FFF)V", SDL_JAVA_INTERFACE(onNativeAccel) },
    3.15      { "onNativeClipboardChanged",   "()V", SDL_JAVA_INTERFACE(onNativeClipboardChanged) },
    3.16      { "nativeLowMemory",            "()V", SDL_JAVA_INTERFACE(nativeLowMemory) },
    3.17 +    { "onNativeLocaleChanged",      "()V", SDL_JAVA_INTERFACE(onNativeLocaleChanged) },
    3.18      { "nativeSendQuit",             "()V", SDL_JAVA_INTERFACE(nativeSendQuit) },
    3.19      { "nativeQuit",                 "()V", SDL_JAVA_INTERFACE(nativeQuit) },
    3.20      { "nativePause",                "()V", SDL_JAVA_INTERFACE(nativePause) },
    3.21 @@ -1142,6 +1146,15 @@
    3.22      SDL_SendAppEvent(SDL_APP_LOWMEMORY);
    3.23  }
    3.24  
    3.25 +/* Locale
    3.26 + * requires android:configChanges="layoutDirection|locale" in AndroidManifest.xml */
    3.27 +JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeLocaleChanged)(
    3.28 +                                    JNIEnv *env, jclass cls)
    3.29 +{
    3.30 +    SDL_SendAppEvent(SDL_LOCALECHANGED);
    3.31 +}
    3.32 +
    3.33 +
    3.34  /* Send Quit event to "SDLThread" thread */
    3.35  JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSendQuit)(
    3.36                                      JNIEnv *env, jclass cls)
    3.37 @@ -2859,6 +2872,9 @@
    3.38  
    3.39      SDL_assert(buflen > 6);
    3.40  
    3.41 +    /* Need to re-create the asset manager if locale has changed (SDL_LOCALECHANGED) */
    3.42 +    Internal_Android_Destroy_AssetManager();
    3.43 +
    3.44      if (asset_manager == NULL) {
    3.45          Internal_Android_Create_AssetManager();
    3.46      }