Skip to content

Commit b0d9163

Browse files
committed
rebase on 2.28.1
1 parent 4761467 commit b0d9163

3 files changed

Lines changed: 88 additions & 0 deletions

File tree

include/SDL_system.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,31 @@ extern DECLSPEC int SDLCALL SDL_AndroidGetExternalStorageState(void);
415415
*/
416416
extern DECLSPEC const char * SDLCALL SDL_AndroidGetExternalStoragePath(void);
417417

418+
/**
419+
* Get the path used for external storage for this application.
420+
*
421+
* This path is unique to your application, but is public and can be written
422+
* to by other applications.
423+
*
424+
* Your external storage path is typically:
425+
* `/storage/sdcard0/Android/data/your.app.package/files`.
426+
*
427+
* \returns the path used for external storage for this application on success
428+
* or NULL on failure; call SDL_GetError() for more information.
429+
*
430+
* \since This function is available since SDL 2.0.0.
431+
*
432+
* \sa SDL_AndroidGetExternalStorageState
433+
*/
434+
extern DECLSPEC const char * SDLCALL SDL_AndroidGetTopExternalStoragePath(void);
435+
436+
/**
437+
* Recursively copy all asset files and folders to the given directory
438+
*
439+
* \param destpath The target directory to which to copy all assets.
440+
*/
441+
extern DECLSPEC void SDLCALL SDL_AndroidCopyAssetFilesToDir(const char *destpath);
442+
418443
/**
419444
* Request permissions at runtime.
420445
*

src/core/android/SDL_android.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ static jmethodID midManualBackButton;
324324
static jmethodID midMinimizeWindow;
325325
static jmethodID midOpenURL;
326326
static jmethodID midRequestPermission;
327+
static jmethodID midCopyAssetFilesToDir;
327328
static jmethodID midShowToast;
328329
static jmethodID midSendMessage;
329330
static jmethodID midSetActivityTitle;
@@ -612,6 +613,7 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetupJNI)(JNIEnv *env, jclass cl
612613
midMinimizeWindow = (*env)->GetStaticMethodID(env, mActivityClass, "minimizeWindow", "()V");
613614
midOpenURL = (*env)->GetStaticMethodID(env, mActivityClass, "openURL", "(Ljava/lang/String;)I");
614615
midRequestPermission = (*env)->GetStaticMethodID(env, mActivityClass, "requestPermission", "(Ljava/lang/String;I)V");
616+
midCopyAssetFilesToDir = (*env)->GetStaticMethodID(env, mActivityClass, "copyAssetFilesToDir", "(Ljava/lang/String;)V");
615617
midShowToast = (*env)->GetStaticMethodID(env, mActivityClass, "showToast", "(Ljava/lang/String;IIII)I");
616618
midSendMessage = (*env)->GetStaticMethodID(env, mActivityClass, "sendMessage", "(II)Z");
617619
midSetActivityTitle = (*env)->GetStaticMethodID(env, mActivityClass, "setActivityTitle", "(Ljava/lang/String;)Z");
@@ -2531,11 +2533,69 @@ const char *SDL_AndroidGetExternalStoragePath(void)
25312533
return s_AndroidExternalFilesPath;
25322534
}
25332535

2536+
const char * SDL_AndroidGetTopExternalStoragePath(void)
2537+
{
2538+
static char *s_AndroidExternalFilesPath = NULL;
2539+
2540+
if (!s_AndroidExternalFilesPath) {
2541+
struct LocalReferenceHolder refs = LocalReferenceHolder_Setup(__FUNCTION__);
2542+
jclass cls;
2543+
jmethodID mid;
2544+
jobject fileObject;
2545+
jstring pathString;
2546+
const char *path;
2547+
2548+
JNIEnv *env = Android_JNI_GetEnv();
2549+
if (!LocalReferenceHolder_Init(&refs, env)) {
2550+
LocalReferenceHolder_Cleanup(&refs);
2551+
return NULL;
2552+
}
2553+
2554+
/* fileObj = context.getExternalStorageDirectory(); */
2555+
cls = (*env)->FindClass(env, "android/os/Environment");
2556+
mid = (*env)->GetStaticMethodID(env, cls,
2557+
"getExternalStorageDirectory", "()Ljava/io/File;");
2558+
fileObject = (*env)->CallStaticObjectMethod(env, cls, mid);
2559+
if (!fileObject) {
2560+
SDL_SetError("Couldn't get external directory");
2561+
LocalReferenceHolder_Cleanup(&refs);
2562+
return NULL;
2563+
}
2564+
2565+
/* path = fileObject.getAbsolutePath(); */
2566+
mid = (*env)->GetMethodID(env, (*env)->GetObjectClass(env, fileObject),
2567+
"getAbsolutePath", "()Ljava/lang/String;");
2568+
pathString = (jstring)(*env)->CallObjectMethod(env, fileObject, mid);
2569+
2570+
path = (*env)->GetStringUTFChars(env, pathString, NULL);
2571+
s_AndroidExternalFilesPath = SDL_strdup(path);
2572+
(*env)->ReleaseStringUTFChars(env, pathString, path);
2573+
2574+
LocalReferenceHolder_Cleanup(&refs);
2575+
}
2576+
return s_AndroidExternalFilesPath;
2577+
}
2578+
25342579
SDL_bool SDL_AndroidRequestPermission(const char *permission)
25352580
{
25362581
return Android_JNI_RequestPermission(permission);
25372582
}
25382583

2584+
void SDL_AndroidCopyAssetFilesToDir(const char* destpath)
2585+
{
2586+
Android_JNI_CopyAssetFilesToDir(destpath);
2587+
}
2588+
2589+
void Android_JNI_CopyAssetFilesToDir(const char *destpath)
2590+
{
2591+
JNIEnv *env = Android_JNI_GetEnv();
2592+
jstring jdestpath;
2593+
2594+
jdestpath = (*env)->NewStringUTF(env, destpath);
2595+
(*env)->CallStaticVoidMethod(env, mActivityClass, midCopyAssetFilesToDir, jdestpath);
2596+
(*env)->DeleteLocalRef(env, jdestpath);
2597+
}
2598+
25392599
int SDL_AndroidShowToast(const char *message, int duration, int gravity, int xOffset, int yOffset)
25402600
{
25412601
return Android_JNI_ShowToast(message, duration, gravity, xOffset, yOffset);

src/core/android/SDL_android.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ SDL_bool Android_JNI_SetRelativeMouseEnabled(SDL_bool enabled);
130130
/* Request permission */
131131
SDL_bool Android_JNI_RequestPermission(const char *permission);
132132

133+
/* Copy asset files */
134+
void Android_JNI_CopyAssetFilesToDir(const char *destpath);
135+
133136
/* Show toast notification */
134137
int Android_JNI_ShowToast(const char *message, int duration, int gravity, int xOffset, int yOffset);
135138

0 commit comments

Comments
 (0)