android: Added SDL_AndroidGetCachePath().

Fixes #8408.
This commit is contained in:
Ryan C. Gordon
2024-07-10 23:47:02 -04:00
parent d949673bc9
commit 8779c95905
7 changed files with 92 additions and 3 deletions

View File

@@ -127,6 +127,14 @@ void *SDL_AndroidGetActivity()
return NULL;
}
SDL_DECLSPEC const char *SDLCALL SDL_AndroidGetCachePath(void);
const char* SDL_AndroidGetCachePath()
{
SDL_Unsupported();
return NULL;
}
SDL_DECLSPEC const char *SDLCALL SDL_AndroidGetExternalStoragePath(void);
const char* SDL_AndroidGetExternalStoragePath()
{

View File

@@ -2598,6 +2598,53 @@ const char *SDL_AndroidGetExternalStoragePath(void)
return s_AndroidExternalFilesPath;
}
// this caches a string until the process ends, so there's no need to use SDL_FreeLater.
const char *SDL_AndroidGetCachePath(void)
{
// !!! FIXME: lots of duplication with SDL_AndroidGetExternalStoragePath and SDL_AndroidGetInternalStoragePath; consolidate these functions!
static char *s_AndroidCachePath = NULL;
if (!s_AndroidCachePath) {
struct LocalReferenceHolder refs = LocalReferenceHolder_Setup(__FUNCTION__);
jmethodID mid;
jobject context;
jobject fileObject;
jstring pathString;
const char *path;
JNIEnv *env = Android_JNI_GetEnv();
if (!LocalReferenceHolder_Init(&refs, env)) {
LocalReferenceHolder_Cleanup(&refs);
return NULL;
}
/* context = SDLActivity.getContext(); */
context = (*env)->CallStaticObjectMethod(env, mActivityClass, midGetContext);
/* fileObj = context.getExternalFilesDir(); */
mid = (*env)->GetMethodID(env, (*env)->GetObjectClass(env, context),
"getCacheDir", "(Ljava/lang/String;)Ljava/io/File;");
fileObject = (*env)->CallObjectMethod(env, context, mid, NULL);
if (!fileObject) {
SDL_SetError("Couldn't get cache directory");
LocalReferenceHolder_Cleanup(&refs);
return NULL;
}
/* path = fileObject.getAbsolutePath(); */
mid = (*env)->GetMethodID(env, (*env)->GetObjectClass(env, fileObject),
"getAbsolutePath", "()Ljava/lang/String;");
pathString = (jstring)(*env)->CallObjectMethod(env, fileObject, mid);
path = (*env)->GetStringUTFChars(env, pathString, NULL);
s_AndroidCachePath = SDL_strdup(path);
(*env)->ReleaseStringUTFChars(env, pathString, path);
LocalReferenceHolder_Cleanup(&refs);
}
return s_AndroidCachePath;
}
int SDL_AndroidShowToast(const char *message, int duration, int gravity, int xOffset, int yOffset)
{
return Android_JNI_ShowToast(message, duration, gravity, xOffset, yOffset);

View File

@@ -14,6 +14,7 @@ SDL3_0.0.0 {
SDL_AllocateEventMemory;
SDL_AndroidBackButton;
SDL_AndroidGetActivity;
SDL_AndroidGetCachePath;
SDL_AndroidGetExternalStoragePath;
SDL_AndroidGetExternalStorageState;
SDL_AndroidGetInternalStoragePath;

View File

@@ -39,6 +39,7 @@
#define SDL_AllocateEventMemory SDL_AllocateEventMemory_REAL
#define SDL_AndroidBackButton SDL_AndroidBackButton_REAL
#define SDL_AndroidGetActivity SDL_AndroidGetActivity_REAL
#define SDL_AndroidGetCachePath SDL_AndroidGetCachePath_REAL
#define SDL_AndroidGetExternalStoragePath SDL_AndroidGetExternalStoragePath_REAL
#define SDL_AndroidGetExternalStorageState SDL_AndroidGetExternalStorageState_REAL
#define SDL_AndroidGetInternalStoragePath SDL_AndroidGetInternalStoragePath_REAL

View File

@@ -59,6 +59,7 @@ SDL_DYNAPI_PROC(int,SDL_AddVulkanRenderSemaphores,(SDL_Renderer *a, Uint32 b, Si
SDL_DYNAPI_PROC(void*,SDL_AllocateEventMemory,(size_t a),(a),return)
SDL_DYNAPI_PROC(void,SDL_AndroidBackButton,(void),(),)
SDL_DYNAPI_PROC(void*,SDL_AndroidGetActivity,(void),(),return)
SDL_DYNAPI_PROC(const char*,SDL_AndroidGetCachePath,(void),(),return)
SDL_DYNAPI_PROC(const char*,SDL_AndroidGetExternalStoragePath,(void),(),return)
SDL_DYNAPI_PROC(int,SDL_AndroidGetExternalStorageState,(Uint32 *a),(a),return)
SDL_DYNAPI_PROC(const char*,SDL_AndroidGetInternalStoragePath,(void),(),return)