aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorrubenwardy <rw@rubenwardy.com>2021-10-15 17:14:48 +0100
committerrubenwardy <rw@rubenwardy.com>2021-10-18 18:12:03 +0100
commitc61d8cfb857bbc591ae87f662e0e6e67b813a5c4 (patch)
treedabe2aed3eb722eb1167e8dfef2911c26a4adc09 /src
parent27f4195471fbbac33b5cdc19025ec5aa6c118a75 (diff)
downloadminetest-c61d8cfb857bbc591ae87f662e0e6e67b813a5c4.tar.gz
minetest-c61d8cfb857bbc591ae87f662e0e6e67b813a5c4.tar.bz2
minetest-c61d8cfb857bbc591ae87f662e0e6e67b813a5c4.zip
Use scoped app storage on Android (#11466)
From November 2021, the Play Store will no longer be accepting apps which use the deprecated getExternalStorageDirectory() API. Therefore, this commit replaces uses of deprecated API with the new scoped API (`getExternalFilesDir()` and `getExternalCacheDir()`). It also provides a temporary migration to move user data from the shared external directory to new storage. Fixes #2097, #11417 and #11118
Diffstat (limited to 'src')
-rw-r--r--src/porting_android.cpp66
1 files changed, 27 insertions, 39 deletions
diff --git a/src/porting_android.cpp b/src/porting_android.cpp
index f5870c174..c71fe5ad8 100644
--- a/src/porting_android.cpp
+++ b/src/porting_android.cpp
@@ -152,47 +152,35 @@ static std::string javaStringToUTF8(jstring js)
return str;
}
-// Calls static method if obj is NULL
-static std::string getAndroidPath(
- jclass cls, jobject obj, 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
- auto 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;");
- std::string path_storage = getAndroidPath(cls_Env, nullptr,
- mt_getAbsPath, "getExternalStorageDirectory");
-
- path_user = path_storage + DIR_DELIM + PROJECT_NAME_C;
- path_share = path_storage + DIR_DELIM + PROJECT_NAME_C;
- path_cache = getAndroidPath(nativeActivity,
- app_global->activity->clazz, mt_getAbsPath, "getCacheDir");
- migrateCachePath();
+ // Set user and share paths
+ {
+ jmethodID getUserDataPath = jnienv->GetMethodID(nativeActivity,
+ "getUserDataPath", "()Ljava/lang/String;");
+ FATAL_ERROR_IF(getUserDataPath==nullptr,
+ "porting::initializePathsAndroid unable to find Java getUserDataPath method");
+ jobject result = jnienv->CallObjectMethod(app_global->activity->clazz, getUserDataPath);
+ const char *javachars = jnienv->GetStringUTFChars((jstring) result, nullptr);
+ path_user = javachars;
+ path_share = javachars;
+ path_locale = path_share + DIR_DELIM + "locale";
+ jnienv->ReleaseStringUTFChars((jstring) result, javachars);
+ }
+
+ // Set cache path
+ {
+ jmethodID getCachePath = jnienv->GetMethodID(nativeActivity,
+ "getCachePath", "()Ljava/lang/String;");
+ FATAL_ERROR_IF(getCachePath==nullptr,
+ "porting::initializePathsAndroid unable to find Java getCachePath method");
+ jobject result = jnienv->CallObjectMethod(app_global->activity->clazz, getCachePath);
+ const char *javachars = jnienv->GetStringUTFChars((jstring) result, nullptr);
+ path_cache = javachars;
+ jnienv->ReleaseStringUTFChars((jstring) result, javachars);
+
+ migrateCachePath();
+ }
}
void showInputDialog(const std::string &acceptButton, const std::string &hint,