summaryrefslogtreecommitdiff
path: root/src/porting_android.cpp
diff options
context:
space:
mode:
authorShadowNinja <shadowninja@minetest.net>2015-08-24 17:00:06 -0400
committerShadowNinja <shadowninja@minetest.net>2015-12-07 13:51:41 -0500
commitea2964f5a168cb52d1b9f74a08f00c7c068c6649 (patch)
tree77785296ca680eced53d3c3e976a5dc948d5df30 /src/porting_android.cpp
parent51e8c2b27786c050f0271eeeaed5eea17d62f0a0 (diff)
downloadminetest-ea2964f5a168cb52d1b9f74a08f00c7c068c6649.tar.gz
minetest-ea2964f5a168cb52d1b9f74a08f00c7c068c6649.tar.bz2
minetest-ea2964f5a168cb52d1b9f74a08f00c7c068c6649.zip
Add seperate cache path
This is set to the XDG cache path where possible. It's set to the app's cache path on Android.
Diffstat (limited to 'src/porting_android.cpp')
-rw-r--r--src/porting_android.cpp78
1 files changed, 56 insertions, 22 deletions
diff --git a/src/porting_android.cpp b/src/porting_android.cpp
index c7e28cc9a..72b625d73 100644
--- a/src/porting_android.cpp
+++ b/src/porting_android.cpp
@@ -164,29 +164,63 @@ void cleanupAndroid()
jvm->DetachCurrentThread();
}
-void setExternalStorageDir(JNIEnv* lJNIEnv)
+static std::string javaStringToUTF8(jstring js)
{
- // Android: Retrieve ablsolute path to external storage device (sdcard)
- jclass ClassEnv = lJNIEnv->FindClass("android/os/Environment");
- jmethodID MethodDir =
- lJNIEnv->GetStaticMethodID(ClassEnv,
- "getExternalStorageDirectory","()Ljava/io/File;");
- jobject ObjectFile = lJNIEnv->CallStaticObjectMethod(ClassEnv, MethodDir);
- jclass ClassFile = lJNIEnv->FindClass("java/io/File");
-
- jmethodID MethodPath =
- lJNIEnv->GetMethodID(ClassFile, "getAbsolutePath",
- "()Ljava/lang/String;");
- jstring StringPath =
- (jstring) lJNIEnv->CallObjectMethod(ObjectFile, MethodPath);
-
- const char *externalPath = lJNIEnv->GetStringUTFChars(StringPath, NULL);
- std::string userPath(externalPath);
- lJNIEnv->ReleaseStringUTFChars(StringPath, externalPath);
-
- path_storage = userPath;
- path_user = userPath + DIR_DELIM + PROJECT_NAME_C;
- path_share = userPath + DIR_DELIM + PROJECT_NAME_C;
+ std::string str;
+ // Get string as a UTF-8 c-string
+ const char *c_str = jnienv->GetStringUTFChars(js, NULL);
+ // Save it
+ str = c_str;
+ // And free the c-string
+ jnienv->ReleaseStringUTFChars(js, c_str);
+ return str;
+}
+
+// Calls static method if obj is NULL
+static std::string getAndroidPath(jclass cls, jobject obj, jclass cls_File,
+ jmethodID mt_getAbsPath, const char *getter)
+{
+ // Get getter method
+ jmethodID mt_getter;
+ if (obj)
+ mt_getter = jnienv->GetMethodID(cls, getter,
+ "()Ljava/io/File;");
+ else
+ mt_getter = jnienv->GetStaticMethodID(cls, getter,
+ "()Ljava/io/File;");
+
+ // Call getter
+ jobject ob_file;
+ if (obj)
+ ob_file = jnienv->CallObjectMethod(obj, mt_getter);
+ else
+ ob_file = jnienv->CallStaticObjectMethod(cls, mt_getter);
+
+ // Call getAbsolutePath
+ jstring js_path = (jstring) jnienv->CallObjectMethod(ob_file,
+ mt_getAbsPath);
+
+ return javaStringToUTF8(js_path);
+}
+
+void initializePathsAndroid()
+{
+ // Get Environment class
+ jclass cls_Env = jnienv->FindClass("android/os/Environment");
+ // Get File class
+ jclass cls_File = jnienv->FindClass("java/io/File");
+ // Get getAbsolutePath method
+ jmethodID mt_getAbsPath = jnienv->GetMethodID(cls_File,
+ "getAbsolutePath", "()Ljava/lang/String;");
+
+ path_cache = getAndroidPath(nativeActivity, app_global->activity->clazz,
+ cls_File, mt_getAbsPath, "getCacheDir");
+ path_storage = getAndroidPath(cls_Env, NULL, cls_File, mt_getAbsPath,
+ "getExternalStorageDirectory");
+ path_user = path_storage + DIR_DELIM + PROJECT_NAME_C;
+ path_share = path_storage + DIR_DELIM + PROJECT_NAME_C;
+
+ migrateCachePath();
}
void showInputDialog(const std::string& acceptButton, const std::string& hint,