Merge pull request #9750 from JosJuice/android-jstringarraytovector

Android: Use DeleteLocalRef more in AndroidCommon
This commit is contained in:
Mai M 2021-05-24 16:50:55 -04:00 committed by GitHub
commit ff08b85740
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 27 deletions

View File

@ -39,7 +39,11 @@ std::vector<std::string> JStringArrayToVector(JNIEnv* env, jobjectArray array)
result.reserve(size); result.reserve(size);
for (jsize i = 0; i < size; ++i) for (jsize i = 0; i < size; ++i)
result.push_back(GetJString(env, (jstring)env->GetObjectArrayElement(array, i))); {
jstring str = reinterpret_cast<jstring>(env->GetObjectArrayElement(array, i));
result.push_back(GetJString(env, str));
env->DeleteLocalRef(str);
}
return result; return result;
} }
@ -48,7 +52,11 @@ jobjectArray VectorToJStringArray(JNIEnv* env, std::vector<std::string> vector)
{ {
jobjectArray result = env->NewObjectArray(vector.size(), IDCache::GetStringClass(), nullptr); jobjectArray result = env->NewObjectArray(vector.size(), IDCache::GetStringClass(), nullptr);
for (jsize i = 0; i < vector.size(); ++i) for (jsize i = 0; i < vector.size(); ++i)
env->SetObjectArrayElement(result, i, ToJString(env, vector[i])); {
jstring str = ToJString(env, vector[i]);
env->SetObjectArrayElement(result, i, str);
env->DeleteLocalRef(str);
}
return result; return result;
} }
@ -59,7 +67,7 @@ bool IsPathAndroidContent(const std::string& uri)
std::string OpenModeToAndroid(std::string mode) std::string OpenModeToAndroid(std::string mode)
{ {
// The 'b' specifier is not supported. Since we're on POSIX, it's fine to just skip it. // The 'b' specifier is not supported by Android. Since we're on POSIX, it's fine to just skip it.
if (!mode.empty() && mode.back() == 'b') if (!mode.empty() && mode.back() == 'b')
mode.pop_back(); mode.pop_back();
@ -92,7 +100,7 @@ std::string OpenModeToAndroid(std::ios_base::openmode mode)
if ((mode & t) == t) if ((mode & t) == t)
result += 't'; result += 't';
// The 'b' specifier is not supported. Since we're on POSIX, it's fine to just skip it. // The 'b' specifier is not supported by Android. Since we're on POSIX, it's fine to just skip it.
return result; return result;
} }
@ -123,19 +131,34 @@ jlong GetAndroidContentSizeAndIsDirectory(const std::string& uri)
std::string GetAndroidContentDisplayName(const std::string& uri) std::string GetAndroidContentDisplayName(const std::string& uri)
{ {
JNIEnv* env = IDCache::GetEnvForThread(); JNIEnv* env = IDCache::GetEnvForThread();
jobject display_name =
jstring jresult = reinterpret_cast<jstring>(
env->CallStaticObjectMethod(IDCache::GetContentHandlerClass(), env->CallStaticObjectMethod(IDCache::GetContentHandlerClass(),
IDCache::GetContentHandlerGetDisplayName(), ToJString(env, uri)); IDCache::GetContentHandlerGetDisplayName(), ToJString(env, uri)));
return display_name ? GetJString(env, reinterpret_cast<jstring>(display_name)) : "";
if (!jresult)
return "";
std::string result = GetJString(env, jresult);
env->DeleteLocalRef(jresult);
return result;
} }
std::vector<std::string> GetAndroidContentChildNames(const std::string& uri) std::vector<std::string> GetAndroidContentChildNames(const std::string& uri)
{ {
JNIEnv* env = IDCache::GetEnvForThread(); JNIEnv* env = IDCache::GetEnvForThread();
jobject children = env->CallStaticObjectMethod(IDCache::GetContentHandlerClass(),
IDCache::GetContentHandlerGetChildNames(), jobjectArray jresult = reinterpret_cast<jobjectArray>(env->CallStaticObjectMethod(
ToJString(env, uri), false); IDCache::GetContentHandlerClass(), IDCache::GetContentHandlerGetChildNames(),
return JStringArrayToVector(env, reinterpret_cast<jobjectArray>(children)); ToJString(env, uri), false));
std::vector<std::string> result = JStringArrayToVector(env, jresult);
env->DeleteLocalRef(jresult);
return result;
} }
std::vector<std::string> DoFileSearchAndroidContent(const std::string& directory, std::vector<std::string> DoFileSearchAndroidContent(const std::string& directory,
@ -143,10 +166,16 @@ std::vector<std::string> DoFileSearchAndroidContent(const std::string& directory
bool recursive) bool recursive)
{ {
JNIEnv* env = IDCache::GetEnvForThread(); JNIEnv* env = IDCache::GetEnvForThread();
jobject result = env->CallStaticObjectMethod(
jobjectArray jresult = reinterpret_cast<jobjectArray>(env->CallStaticObjectMethod(
IDCache::GetContentHandlerClass(), IDCache::GetContentHandlerDoFileSearch(), IDCache::GetContentHandlerClass(), IDCache::GetContentHandlerDoFileSearch(),
ToJString(env, directory), VectorToJStringArray(env, extensions), recursive); ToJString(env, directory), VectorToJStringArray(env, extensions), recursive));
return JStringArrayToVector(env, reinterpret_cast<jobjectArray>(result));
std::vector<std::string> result = JStringArrayToVector(env, jresult);
env->DeleteLocalRef(jresult);
return result;
} }
int GetNetworkIpAddress() int GetNetworkIpAddress()

View File

@ -69,20 +69,8 @@ JNIEXPORT jobject JNICALL Java_org_dolphinemu_dolphinemu_model_GameFileCache_add
JNIEXPORT jboolean JNICALL Java_org_dolphinemu_dolphinemu_model_GameFileCache_update( JNIEXPORT jboolean JNICALL Java_org_dolphinemu_dolphinemu_model_GameFileCache_update(
JNIEnv* env, jobject obj, jobjectArray folder_paths, jboolean recursive_scan) JNIEnv* env, jobject obj, jobjectArray folder_paths, jboolean recursive_scan)
{ {
jsize size = env->GetArrayLength(folder_paths);
std::vector<std::string> folder_paths_vector;
folder_paths_vector.reserve(size);
for (jsize i = 0; i < size; ++i)
{
const auto path = reinterpret_cast<jstring>(env->GetObjectArrayElement(folder_paths, i));
folder_paths_vector.push_back(GetJString(env, path));
env->DeleteLocalRef(path);
}
return GetPointer(env, obj)->Update( return GetPointer(env, obj)->Update(
UICommon::FindAllGamePaths(folder_paths_vector, recursive_scan)); UICommon::FindAllGamePaths(JStringArrayToVector(env, folder_paths), recursive_scan));
} }
JNIEXPORT jboolean JNICALL JNIEXPORT jboolean JNICALL