src/core/android/SDL_android.c
changeset 11678 1f10a52295e3
parent 11669 1fe21a20aa12
child 11688 e4d90d54cb01
     1.1 --- a/src/core/android/SDL_android.c	Sat Nov 04 09:07:33 2017 -0700
     1.2 +++ b/src/core/android/SDL_android.c	Sat Nov 04 09:37:29 2017 -0700
     1.3 @@ -128,6 +128,13 @@
     1.4          JNIEnv* env, jclass cls,
     1.5          jstring name);
     1.6  
     1.7 +JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetenv)(
     1.8 +        JNIEnv* env, jclass cls,
     1.9 +        jstring name, jstring value);
    1.10 +
    1.11 +JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeEnvironmentVariablesSet)(
    1.12 +        JNIEnv* env, jclass cls);
    1.13 +
    1.14  /* Java class SDLInputConnection */
    1.15  JNIEXPORT void JNICALL SDL_JAVA_INTERFACE_INPUT_CONNECTION(nativeCommitText)(
    1.16          JNIEnv* env, jclass cls,
    1.17 @@ -213,7 +220,7 @@
    1.18  static jmethodID midClipboardGetText;
    1.19  static jmethodID midClipboardHasText;
    1.20  static jmethodID midOpenAPKExpansionInputStream;
    1.21 -static jmethodID midGetManifestEnvironmentVariable;
    1.22 +static jmethodID midGetManifestEnvironmentVariables;
    1.23  static jmethodID midGetDisplayDPI;
    1.24  
    1.25  /* audio manager */
    1.26 @@ -244,6 +251,8 @@
    1.27  static float fLastAccelerometer[3];
    1.28  static SDL_bool bHasNewData;
    1.29  
    1.30 +static SDL_bool bHasEnvironmentVariables;
    1.31 +
    1.32  /*******************************************************************************
    1.33                   Functions called by JNI
    1.34  *******************************************************************************/
    1.35 @@ -314,16 +323,16 @@
    1.36      midOpenAPKExpansionInputStream = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
    1.37                                  "openAPKExpansionInputStream", "(Ljava/lang/String;)Ljava/io/InputStream;");
    1.38  
    1.39 -    midGetManifestEnvironmentVariable = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
    1.40 -                                "getManifestEnvironmentVariable", "(Ljava/lang/String;)Ljava/lang/String;");
    1.41 +    midGetManifestEnvironmentVariables = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
    1.42 +                                "getManifestEnvironmentVariables", "()V");
    1.43  
    1.44      midGetDisplayDPI = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, "getDisplayDPI", "()Landroid/util/DisplayMetrics;");
    1.45  
    1.46      if (!midGetNativeSurface ||
    1.47         !midSetActivityTitle || !midSetOrientation || !midGetContext || !midInputGetInputDeviceIds ||
    1.48 -       !midSendMessage || !midShowTextInput || !midIsScreenKeyboardShown || 
    1.49 +       !midSendMessage || !midShowTextInput || !midIsScreenKeyboardShown ||
    1.50         !midClipboardSetText || !midClipboardGetText || !midClipboardHasText ||
    1.51 -       !midOpenAPKExpansionInputStream || !midGetManifestEnvironmentVariable || !midGetDisplayDPI) {
    1.52 +       !midOpenAPKExpansionInputStream || !midGetManifestEnvironmentVariables|| !midGetDisplayDPI) {
    1.53          __android_log_print(ANDROID_LOG_WARN, "SDL", "Missing some Java callbacks, do you have the latest version of SDLActivity.java?");
    1.54      }
    1.55  
    1.56 @@ -808,6 +817,26 @@
    1.57      return result;
    1.58  }
    1.59  
    1.60 +JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetenv)(
    1.61 +                                    JNIEnv* env, jclass cls,
    1.62 +                                    jstring name, jstring value)
    1.63 +{
    1.64 +    const char *utfname = (*env)->GetStringUTFChars(env, name, NULL);
    1.65 +    const char *utfvalue = (*env)->GetStringUTFChars(env, value, NULL);
    1.66 +
    1.67 +    SDL_setenv(utfname, utfvalue, 1);
    1.68 +
    1.69 +    (*env)->ReleaseStringUTFChars(env, name, utfname);
    1.70 +    (*env)->ReleaseStringUTFChars(env, value, utfvalue);
    1.71 +
    1.72 +}
    1.73 +
    1.74 +JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeEnvironmentVariablesSet)(
    1.75 +        JNIEnv* env, jclass cls)
    1.76 +{
    1.77 +    bHasEnvironmentVariables = SDL_TRUE;
    1.78 +}
    1.79 +
    1.80  /*******************************************************************************
    1.81               Functions called by SDL into Java
    1.82  *******************************************************************************/
    1.83 @@ -2105,40 +2134,17 @@
    1.84      return s_AndroidExternalFilesPath;
    1.85  }
    1.86  
    1.87 -// Ugh, but we have to SDL_strdup() our result to pass it safely back
    1.88 -// out into normal SDL_getenv flow.  So we'll just do the same sort
    1.89 -// of trick as on Win32 over in SDL_getenv.c.
    1.90 -char *SDL_AndroidEnvMem;
    1.91 -
    1.92 -char *SDL_AndroidGetManifestEnvironmentVariable(const char *variableName)
    1.93 +void Android_JNI_GetManifestEnvironmentVariables(void)
    1.94  {
    1.95 -    if ((mActivityClass == NULL) || (midGetManifestEnvironmentVariable == 0)) {
    1.96 -        __android_log_print(ANDROID_LOG_WARN, "SDL", "request to get environment variable before JNI is ready: %s", variableName);
    1.97 -        return NULL;
    1.98 +    if (!mActivityClass || !midGetManifestEnvironmentVariables) {
    1.99 +        __android_log_print(ANDROID_LOG_WARN, "SDL", "Request to get environment variables before JNI is ready");
   1.100 +        return;
   1.101      }
   1.102  
   1.103 -    JNIEnv *env = Android_JNI_GetEnv();
   1.104 -
   1.105 -    jstring jVariableName = (*env)->NewStringUTF(env, variableName);
   1.106 -    jstring jResult = (jstring)((*env)->CallStaticObjectMethod(env, mActivityClass, midGetManifestEnvironmentVariable, jVariableName));
   1.107 -    (*env)->DeleteLocalRef(env, jVariableName);
   1.108 -
   1.109 -    if (jResult == NULL) {
   1.110 -        return NULL;        
   1.111 +    if (!bHasEnvironmentVariables) {
   1.112 +        JNIEnv *env = Android_JNI_GetEnv();
   1.113 +        (*env)->CallStaticVoidMethod(env, mActivityClass, midGetManifestEnvironmentVariables);
   1.114      }
   1.115 -
   1.116 -    if (SDL_AndroidEnvMem) {
   1.117 -        SDL_free(SDL_AndroidEnvMem);
   1.118 -        SDL_AndroidEnvMem = NULL;
   1.119 -    }
   1.120 -
   1.121 -    const char *result = (*env)->GetStringUTFChars(env, jResult, NULL);
   1.122 -    SDL_AndroidEnvMem = SDL_strdup(result);
   1.123 -    (*env)->ReleaseStringUTFChars(env, jResult, result);
   1.124 -    (*env)->DeleteLocalRef(env, jResult);
   1.125 -
   1.126 -    __android_log_print(ANDROID_LOG_INFO, "SDL", "environment variable in metadata: %s = %s", variableName, SDL_AndroidEnvMem);
   1.127 -    return SDL_AndroidEnvMem;
   1.128  }
   1.129  
   1.130  #endif /* __ANDROID__ */