refactor native code

This commit is contained in:
SSimco
2025-04-28 08:34:02 +03:00
parent d2e7ddf613
commit 09eb9e5b48
4 changed files with 21 additions and 15 deletions
@@ -14,12 +14,11 @@ class AndroidFilesystemCallbacks : public FilesystemAndroid::FilesystemCallbacks
bool callBooleanFunction(const std::filesystem::path& uri, jmethodID methodId)
{
bool result = false;
std::thread([&, this]() {
JNIUtils::ScopedJNIENV env;
JNIUtils::fiberSafeJNICall([&](JNIEnv* env) {
jstring uriString = JNIUtils::toJString(env, uri);
result = env->CallStaticBooleanMethod(*m_fileUtilClass, methodId, uriString);
env->DeleteLocalRef(uriString);
}).join();
});
return result;
}
@@ -38,20 +37,18 @@ class AndroidFilesystemCallbacks : public FilesystemAndroid::FilesystemCallbacks
int openContentUri(const std::filesystem::path& uri) override
{
int fd = -1;
std::thread([&, this]() {
JNIUtils::ScopedJNIENV env;
JNIUtils::fiberSafeJNICall([&](JNIEnv* env) {
jstring uriString = JNIUtils::toJString(env, uri);
fd = env->CallStaticIntMethod(*m_fileUtilClass, m_openContentUriMid, uriString);
env->DeleteLocalRef(uriString);
}).join();
});
return fd;
}
std::vector<std::filesystem::path> listFiles(const std::filesystem::path& uri) override
{
std::vector<std::filesystem::path> paths;
std::thread([&, this]() {
JNIUtils::ScopedJNIENV env;
JNIUtils::fiberSafeJNICall([&](JNIEnv* env) {
jstring uriString = JNIUtils::toJString(env, uri);
jobjectArray pathsObjArray = static_cast<jobjectArray>(env->CallStaticObjectMethod(*m_fileUtilClass, m_listFilesMid, uriString));
env->DeleteLocalRef(uriString);
@@ -64,7 +61,7 @@ class AndroidFilesystemCallbacks : public FilesystemAndroid::FilesystemCallbacks
env->DeleteLocalRef(pathStr);
}
env->DeleteLocalRef(pathsObjArray);
}).join();
});
return paths;
}
@@ -11,17 +11,16 @@ AndroidSwkbdCallbacks::AndroidSwkbdCallbacks()
void AndroidSwkbdCallbacks::showSoftwareKeyboard(const std::string& initialText, sint32 maxLength)
{
std::thread([&, this]() {
JNIUtils::ScopedJNIENV env;
JNIUtils::fiberSafeJNICall([&](JNIEnv* env) {
jstring j_initialText = JNIUtils::toJString(env, initialText);
JNIUtils::ScopedJNIENV()->CallStaticVoidMethod(*m_emulationActivityClass, m_showSoftwareKeyboardMethodID, j_initialText, maxLength);
env->DeleteLocalRef(j_initialText);
}).join();
});
}
void AndroidSwkbdCallbacks::hideSoftwareKeyboard()
{
std::thread([this]() {
JNIUtils::ScopedJNIENV()->CallStaticVoidMethod(*m_emulationActivityClass, m_hideSoftwareKeyboardMethodID);
}).join();
JNIUtils::fiberSafeJNICall([&](JNIEnv* env) {
env->CallStaticVoidMethod(*m_emulationActivityClass, m_hideSoftwareKeyboardMethodID);
});
}
@@ -87,3 +87,11 @@ jobject JNIUtils::createJavaStringArrayList(JNIEnv* env, const std::vector<std::
}
return arrayListObject;
}
void JNIUtils::fiberSafeJNICall(const std::function<void(JNIEnv*)>& func)
{
std::thread([&]() {
ScopedJNIENV env;
func(*env);
}).join();
}
+2
View File
@@ -190,4 +190,6 @@ namespace JNIUtils
env->DeleteLocalRef(javaClass);
return obj;
}
void fiberSafeJNICall(const std::function<void(JNIEnv*)>& func);
} // namespace JNIUtils