Skip to content
Permalink
Browse files
Merge pull request #9750 from JosJuice/android-jstringarraytovector
Android: Use DeleteLocalRef more in AndroidCommon
  • Loading branch information
lioncash committed May 24, 2021
2 parents 5167192 + 90cf0d6 commit ff08b85
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 27 deletions.
@@ -39,7 +39,11 @@ std::vector<std::string> JStringArrayToVector(JNIEnv* env, jobjectArray array)
result.reserve(size);

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;
}
@@ -48,7 +52,11 @@ jobjectArray VectorToJStringArray(JNIEnv* env, std::vector<std::string> vector)
{
jobjectArray result = env->NewObjectArray(vector.size(), IDCache::GetStringClass(), nullptr);
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;
}

@@ -59,7 +67,7 @@ bool IsPathAndroidContent(const std::string& uri)

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')
mode.pop_back();

@@ -92,7 +100,7 @@ std::string OpenModeToAndroid(std::ios_base::openmode mode)
if ((mode & t) == 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;
}
@@ -123,30 +131,51 @@ jlong GetAndroidContentSizeAndIsDirectory(const std::string& uri)
std::string GetAndroidContentDisplayName(const std::string& uri)
{
JNIEnv* env = IDCache::GetEnvForThread();
jobject display_name =

jstring jresult = reinterpret_cast<jstring>(
env->CallStaticObjectMethod(IDCache::GetContentHandlerClass(),
IDCache::GetContentHandlerGetDisplayName(), ToJString(env, uri));
return display_name ? GetJString(env, reinterpret_cast<jstring>(display_name)) : "";
IDCache::GetContentHandlerGetDisplayName(), ToJString(env, uri)));

if (!jresult)
return "";

std::string result = GetJString(env, jresult);

env->DeleteLocalRef(jresult);

return result;
}

std::vector<std::string> GetAndroidContentChildNames(const std::string& uri)
{
JNIEnv* env = IDCache::GetEnvForThread();
jobject children = env->CallStaticObjectMethod(IDCache::GetContentHandlerClass(),
IDCache::GetContentHandlerGetChildNames(),
ToJString(env, uri), false);
return JStringArrayToVector(env, reinterpret_cast<jobjectArray>(children));

jobjectArray jresult = reinterpret_cast<jobjectArray>(env->CallStaticObjectMethod(
IDCache::GetContentHandlerClass(), IDCache::GetContentHandlerGetChildNames(),
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,
const std::vector<std::string>& extensions,
bool recursive)
{
JNIEnv* env = IDCache::GetEnvForThread();
jobject result = env->CallStaticObjectMethod(

jobjectArray jresult = reinterpret_cast<jobjectArray>(env->CallStaticObjectMethod(
IDCache::GetContentHandlerClass(), IDCache::GetContentHandlerDoFileSearch(),
ToJString(env, directory), VectorToJStringArray(env, extensions), recursive);
return JStringArrayToVector(env, reinterpret_cast<jobjectArray>(result));
ToJString(env, directory), VectorToJStringArray(env, extensions), recursive));

std::vector<std::string> result = JStringArrayToVector(env, jresult);

env->DeleteLocalRef(jresult);

return result;
}

int GetNetworkIpAddress()
@@ -69,20 +69,8 @@ JNIEXPORT jobject JNICALL Java_org_dolphinemu_dolphinemu_model_GameFileCache_add
JNIEXPORT jboolean JNICALL Java_org_dolphinemu_dolphinemu_model_GameFileCache_update(
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(
UICommon::FindAllGamePaths(folder_paths_vector, recursive_scan));
UICommon::FindAllGamePaths(JStringArrayToVector(env, folder_paths), recursive_scan));
}

JNIEXPORT jboolean JNICALL

0 comments on commit ff08b85

Please sign in to comment.