SDL_AndroidGetExternalStorageState(): return 0 on success, and fills *state with flags

This commit is contained in:
Sylvain 2023-02-09 20:44:46 +01:00 committed by Sylvain Becker
parent a8e89f2567
commit a2f3711c80
3 changed files with 20 additions and 13 deletions

View File

@ -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.

View File

@ -2395,19 +2395,22 @@ 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");
@ -2415,23 +2418,26 @@ int SDL_AndroidGetExternalStorageState(void)
"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)

View File

@ -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)