Skip to content

Commit

Permalink
SDL_AndroidGetExternalStorageState(): return 0 on success, and fills …
Browse files Browse the repository at this point in the history
…*state with flags
  • Loading branch information
1bsyl committed Feb 10, 2023
1 parent a8e89f2 commit a2f3711
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
7 changes: 4 additions & 3 deletions include/SDL3/SDL_system.h
Expand Up @@ -384,14 +384,15 @@ extern DECLSPEC const char * SDLCALL SDL_AndroidGetInternalStoragePath(void);
*
* If external storage is currently unavailable, this will return 0.
*
* \returns the current state of external storage on success or 0 on failure;
* call SDL_GetError() for more information.
* \param state filled with the current state of external storage. 0 if external storage is currently unavailable.
* \returns 0 on success or a negative error code on failure; call
* SDL_GetError() for more information.
*
* \since This function is available since SDL 3.0.0.
*
* \sa SDL_AndroidGetExternalStoragePath
*/
extern DECLSPEC int SDLCALL SDL_AndroidGetExternalStorageState(void);
extern DECLSPEC int SDLCALL SDL_AndroidGetExternalStorageState(Uint32 *state);

/**
* Get the path used for external storage for this application.
Expand Down
24 changes: 15 additions & 9 deletions src/core/android/SDL_android.c
Expand Up @@ -2395,43 +2395,49 @@ const char *SDL_AndroidGetInternalStoragePath(void)
return s_AndroidInternalFilesPath;
}

int SDL_AndroidGetExternalStorageState(void)
int SDL_AndroidGetExternalStorageState(Uint32 *state)
{
struct LocalReferenceHolder refs = LocalReferenceHolder_Setup(__FUNCTION__);
jmethodID mid;
jclass cls;
jstring stateString;
const char *state;
const char *state_string;
int stateFlags;

JNIEnv *env = Android_JNI_GetEnv();
if (!LocalReferenceHolder_Init(&refs, env)) {
LocalReferenceHolder_Cleanup(&refs);
return 0;
if (state) {
*state = 0;
}
return -1;
}

cls = (*env)->FindClass(env, "android/os/Environment");
mid = (*env)->GetStaticMethodID(env, cls,
"getExternalStorageState", "()Ljava/lang/String;");
stateString = (jstring)(*env)->CallStaticObjectMethod(env, cls, mid);

state = (*env)->GetStringUTFChars(env, stateString, NULL);
state_string = (*env)->GetStringUTFChars(env, stateString, NULL);

/* Print an info message so people debugging know the storage state */
__android_log_print(ANDROID_LOG_INFO, "SDL", "external storage state: %s", state);
__android_log_print(ANDROID_LOG_INFO, "SDL", "external storage state: %s", state_string);

if (SDL_strcmp(state, "mounted") == 0) {
if (SDL_strcmp(state_string, "mounted") == 0) {
stateFlags = SDL_ANDROID_EXTERNAL_STORAGE_READ |
SDL_ANDROID_EXTERNAL_STORAGE_WRITE;
} else if (SDL_strcmp(state, "mounted_ro") == 0) {
} else if (SDL_strcmp(state_string, "mounted_ro") == 0) {
stateFlags = SDL_ANDROID_EXTERNAL_STORAGE_READ;
} else {
stateFlags = 0;
}
(*env)->ReleaseStringUTFChars(env, stateString, state);
(*env)->ReleaseStringUTFChars(env, stateString, state_string);

LocalReferenceHolder_Cleanup(&refs);
return stateFlags;
if (state) {
*state = stateFlags;
}
return 0;
}

const char *SDL_AndroidGetExternalStoragePath(void)
Expand Down
2 changes: 1 addition & 1 deletion src/dynapi/SDL_dynapi_procs.h
Expand Up @@ -104,7 +104,7 @@ SDL_DYNAPI_PROC(void,SDL_iPhoneSetEventPump,(SDL_bool a),(a),)
SDL_DYNAPI_PROC(void,SDL_AndroidBackButton,(void),(),)
SDL_DYNAPI_PROC(void*,SDL_AndroidGetActivity,(void),(),return)
SDL_DYNAPI_PROC(const char*,SDL_AndroidGetExternalStoragePath,(void),(),return)
SDL_DYNAPI_PROC(int,SDL_AndroidGetExternalStorageState,(void),(),return)
SDL_DYNAPI_PROC(int,SDL_AndroidGetExternalStorageState,(Uint32 *a),(a),return)
SDL_DYNAPI_PROC(const char*,SDL_AndroidGetInternalStoragePath,(void),(),return)
SDL_DYNAPI_PROC(void*,SDL_AndroidGetJNIEnv,(void),(),return)
SDL_DYNAPI_PROC(SDL_bool,SDL_AndroidRequestPermission,(const char *a),(a),return)
Expand Down

0 comments on commit a2f3711

Please sign in to comment.