Added some platform specific API functions for Android:
authorSam Lantinga <slouken@libsdl.org>
Fri, 02 Nov 2012 02:22:32 -0700
changeset 663055910871076b
parent 6629 8e2c731103e6
child 6631 47ab7ba21530
Added some platform specific API functions for Android:
SDL_AndroidGetJNIEnv()
SDL_AndroidGetActivity()
SDL_AndroidGetInternalStoragePath()
SDL_AndroidGetExternalStorageState()
SDL_AndroidGetExternalStoragePath()
include/SDL_system.h
src/core/android/SDL_android.cpp
     1.1 --- a/include/SDL_system.h	Fri Nov 02 00:35:48 2012 -0700
     1.2 +++ b/include/SDL_system.h	Fri Nov 02 02:22:32 2012 -0700
     1.3 @@ -43,6 +43,11 @@
     1.4  /* *INDENT-ON* */
     1.5  #endif
     1.6  
     1.7 +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
     1.8 +/*
     1.9 +/* Platform specific functions for iOS 
    1.10 +/*
    1.11 +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    1.12  #if __IPHONEOS__
    1.13  
    1.14  extern DECLSPEC int SDLCALL SDL_iPhoneSetAnimationCallback(SDL_Window * window, int interval, void (*callback)(void*), void *callbackParam);
    1.15 @@ -53,7 +58,47 @@
    1.16  #define SDL_iPhoneKeyboardToggle    SDL_ToggleScreenKeyboard
    1.17  #define SDL_iPhoneKeyboardIsShown   SDL_IsScreenKeyboardShown
    1.18  
    1.19 -#endif
    1.20 +#endif /* __IPHONEOS__ */
    1.21 +
    1.22 +
    1.23 +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    1.24 +/*
    1.25 +/* Platform specific functions for Android 
    1.26 +/*
    1.27 +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    1.28 +#if __ANDROID__
    1.29 +
    1.30 +/* Get the JNI environment for the current thread
    1.31 +   This returns JNIEnv*, but the prototype is void* so we don't need jni.h
    1.32 + */
    1.33 +extern DECLSPEC void * SDLCALL SDL_AndroidGetJNIEnv();
    1.34 +
    1.35 +/* Get the SDL Activity object for the application
    1.36 +   This returns jobject, but the prototype is void* so we don't need jni.h
    1.37 + */
    1.38 +extern DECLSPEC void * SDLCALL SDL_AndroidGetActivity();
    1.39 +
    1.40 +/* See the official Android developer guide for more information:
    1.41 +   http://developer.android.com/guide/topics/data/data-storage.html
    1.42 +*/
    1.43 +#define SDL_ANDROID_EXTERNAL_STORAGE_READ   0x01
    1.44 +#define SDL_ANDROID_EXTERNAL_STORAGE_WRITE  0x02
    1.45 +
    1.46 +/* Get the path used for internal storage for this application */
    1.47 +extern DECLSPEC const char * SDLCALL SDL_AndroidGetInternalStoragePath();
    1.48 +
    1.49 +/* Get the current state of external storage, a bitmask of these values:
    1.50 +    SDL_ANDROID_EXTERNAL_STORAGE_READ
    1.51 +    SDL_ANDROID_EXTERNAL_STORAGE_WRITE
    1.52 +   If external storage is currently unavailable, this will return 0.
    1.53 +*/
    1.54 +extern DECLSPEC int SDLCALL SDL_AndroidGetExternalStorageState();
    1.55 +
    1.56 +/* Get the path used for external storage for this application */
    1.57 +extern DECLSPEC const char * SDLCALL SDL_AndroidGetExternalStoragePath();
    1.58 +
    1.59 +#endif /* __ANDROID__ */
    1.60 +
    1.61  
    1.62  /* Ends C function definitions when using C++ */
    1.63  #ifdef __cplusplus
     2.1 --- a/src/core/android/SDL_android.cpp	Fri Nov 02 00:35:48 2012 -0700
     2.2 +++ b/src/core/android/SDL_android.cpp	Fri Nov 02 02:22:32 2012 -0700
     2.3 @@ -24,6 +24,7 @@
     2.4  
     2.5  #ifdef __ANDROID__
     2.6  
     2.7 +#include "SDL_system.h"
     2.8  #include "SDL_android.h"
     2.9  
    2.10  extern "C" {
    2.11 @@ -963,8 +964,6 @@
    2.12  
    2.13  /*extern "C" int Android_JNI_HideTextInput()
    2.14  {
    2.15 -
    2.16 -
    2.17      JNIEnv *env = Android_JNI_GetEnv();
    2.18      if (!env) {
    2.19          return -1;
    2.20 @@ -978,6 +977,155 @@
    2.21      return 0;
    2.22  }*/
    2.23  
    2.24 +//////////////////////////////////////////////////////////////////////////////
    2.25 +//
    2.26 +// Functions exposed to SDL applications in SDL_system.h
    2.27 +//
    2.28 +
    2.29 +extern "C" void *SDL_AndroidGetJNIEnv()
    2.30 +{
    2.31 +    return Android_JNI_GetEnv();
    2.32 +}
    2.33 +
    2.34 +extern "C" void *SDL_AndroidGetActivity()
    2.35 +{
    2.36 +    LocalReferenceHolder refs;
    2.37 +    jmethodID mid;
    2.38 +
    2.39 +    JNIEnv *env = Android_JNI_GetEnv();
    2.40 +    if (!refs.init(env)) {
    2.41 +        return NULL;
    2.42 +    }
    2.43 +
    2.44 +    // return SDLActivity.getContext();
    2.45 +    mid = env->GetStaticMethodID(mActivityClass,
    2.46 +            "getContext","()Landroid/content/Context;");
    2.47 +    return env->CallStaticObjectMethod(mActivityClass, mid);
    2.48 +}
    2.49 +
    2.50 +extern "C" const char * SDL_AndroidGetInternalStoragePath()
    2.51 +{
    2.52 +    static char *s_AndroidInternalFilesPath = NULL;
    2.53 +
    2.54 +    if (!s_AndroidInternalFilesPath) {
    2.55 +        LocalReferenceHolder refs;
    2.56 +        jmethodID mid;
    2.57 +        jobject context;
    2.58 +        jobject fileObject;
    2.59 +        jstring pathString;
    2.60 +        const char *path;
    2.61 +
    2.62 +        JNIEnv *env = Android_JNI_GetEnv();
    2.63 +        if (!refs.init(env)) {
    2.64 +            return NULL;
    2.65 +        }
    2.66 +
    2.67 +        // context = SDLActivity.getContext();
    2.68 +        mid = env->GetStaticMethodID(mActivityClass,
    2.69 +                "getContext","()Landroid/content/Context;");
    2.70 +        context = env->CallStaticObjectMethod(mActivityClass, mid);
    2.71 +
    2.72 +        // fileObj = context.getFilesDir();
    2.73 +        mid = env->GetMethodID(env->GetObjectClass(context),
    2.74 +                "getFilesDir", "()Ljava/io/File;");
    2.75 +        fileObject = env->CallObjectMethod(context, mid);
    2.76 +        if (!fileObject) {
    2.77 +            SDL_SetError("Couldn't get internal directory");
    2.78 +            return NULL;
    2.79 +        }
    2.80 +
    2.81 +        // path = fileObject.getAbsolutePath();
    2.82 +        mid = env->GetMethodID(env->GetObjectClass(fileObject),
    2.83 +                "getAbsolutePath", "()Ljava/lang/String;");
    2.84 +        pathString = (jstring)env->CallObjectMethod(fileObject, mid);
    2.85 +
    2.86 +        path = env->GetStringUTFChars(pathString, NULL);
    2.87 +        s_AndroidInternalFilesPath = SDL_strdup(path);
    2.88 +        env->ReleaseStringUTFChars(pathString, path);
    2.89 +    }
    2.90 +    return s_AndroidInternalFilesPath;
    2.91 +}
    2.92 +
    2.93 +extern "C" int SDL_AndroidGetExternalStorageState()
    2.94 +{
    2.95 +    LocalReferenceHolder refs;
    2.96 +    jmethodID mid;
    2.97 +    jclass cls;
    2.98 +    jstring stateString;
    2.99 +    const char *state;
   2.100 +    int stateFlags;
   2.101 +
   2.102 +    JNIEnv *env = Android_JNI_GetEnv();
   2.103 +    if (!refs.init(env)) {
   2.104 +        return 0;
   2.105 +    }
   2.106 +
   2.107 +    cls = env->FindClass("android/os/Environment");
   2.108 +    mid = env->GetStaticMethodID(cls,
   2.109 +            "getExternalStorageState", "()Ljava/lang/String;");
   2.110 +    stateString = (jstring)env->CallStaticObjectMethod(cls, mid);
   2.111 +
   2.112 +    state = env->GetStringUTFChars(stateString, NULL);
   2.113 +
   2.114 +    // Print an info message so people debugging know the storage state
   2.115 +    __android_log_print(ANDROID_LOG_INFO, "SDL", "external storage state: %s", state);
   2.116 +
   2.117 +    if (SDL_strcmp(state, "mounted") == 0) {
   2.118 +        stateFlags = SDL_ANDROID_EXTERNAL_STORAGE_READ |
   2.119 +                     SDL_ANDROID_EXTERNAL_STORAGE_WRITE;
   2.120 +    } else if (SDL_strcmp(state, "mounted_ro") == 0) {
   2.121 +        stateFlags = SDL_ANDROID_EXTERNAL_STORAGE_READ;
   2.122 +    } else {
   2.123 +        stateFlags = 0;
   2.124 +    }
   2.125 +    env->ReleaseStringUTFChars(stateString, state);
   2.126 +
   2.127 +    return stateFlags;
   2.128 +}
   2.129 +
   2.130 +extern "C" const char * SDL_AndroidGetExternalStoragePath()
   2.131 +{
   2.132 +    static char *s_AndroidExternalFilesPath = NULL;
   2.133 +
   2.134 +    if (!s_AndroidExternalFilesPath) {
   2.135 +        LocalReferenceHolder refs;
   2.136 +        jmethodID mid;
   2.137 +        jobject context;
   2.138 +        jobject fileObject;
   2.139 +        jstring pathString;
   2.140 +        const char *path;
   2.141 +
   2.142 +        JNIEnv *env = Android_JNI_GetEnv();
   2.143 +        if (!refs.init(env)) {
   2.144 +            return NULL;
   2.145 +        }
   2.146 +
   2.147 +        // context = SDLActivity.getContext();
   2.148 +        mid = env->GetStaticMethodID(mActivityClass,
   2.149 +                "getContext","()Landroid/content/Context;");
   2.150 +        context = env->CallStaticObjectMethod(mActivityClass, mid);
   2.151 +
   2.152 +        // fileObj = context.getExternalFilesDir();
   2.153 +        mid = env->GetMethodID(env->GetObjectClass(context),
   2.154 +                "getExternalFilesDir", "(Ljava/lang/String;)Ljava/io/File;");
   2.155 +        fileObject = env->CallObjectMethod(context, mid, NULL);
   2.156 +        if (!fileObject) {
   2.157 +            SDL_SetError("Couldn't get external directory");
   2.158 +            return NULL;
   2.159 +        }
   2.160 +
   2.161 +        // path = fileObject.getAbsolutePath();
   2.162 +        mid = env->GetMethodID(env->GetObjectClass(fileObject),
   2.163 +                "getAbsolutePath", "()Ljava/lang/String;");
   2.164 +        pathString = (jstring)env->CallObjectMethod(fileObject, mid);
   2.165 +
   2.166 +        path = env->GetStringUTFChars(pathString, NULL);
   2.167 +        s_AndroidExternalFilesPath = SDL_strdup(path);
   2.168 +        env->ReleaseStringUTFChars(pathString, path);
   2.169 +    }
   2.170 +    return s_AndroidExternalFilesPath;
   2.171 +}
   2.172 +
   2.173  #endif /* __ANDROID__ */
   2.174  
   2.175  /* vi: set ts=4 sw=4 expandtab: */