android: Create utility function for converting strings to Java

This commit is contained in:
JosJuice 2022-08-03 15:36:36 +02:00
parent e65ca8b907
commit 87677be921
5 changed files with 16 additions and 11 deletions

View file

@ -18,3 +18,7 @@ std::string GetJString(JNIEnv *env, jstring jstr) {
env->ReleaseStringUTFChars(jstr, s);
return result;
}
jstring ToJString(JNIEnv* env, const std::string& str) {
return env->NewStringUTF(str.c_str());
}

View file

@ -9,3 +9,4 @@
#include <jni.h>
std::string GetJString(JNIEnv* env, jstring jstr);
jstring ToJString(JNIEnv* env, const std::string& str);

View file

@ -3,6 +3,7 @@
// Refer to the license.txt file included.
#include "common/string_util.h"
#include "jni/android_common/android_common.h"
#include "jni/applets/mii_selector.h"
#include "jni/id_cache.h"
@ -26,7 +27,7 @@ void AndroidMiiSelector::Setup(const Frontend::MiiSelectorConfig& config) {
static_cast<jboolean>(config.enable_cancel_button));
env->SetObjectField(java_config,
env->GetFieldID(s_mii_selector_config_class, "title", "Ljava/lang/String;"),
env->NewStringUTF(config.title.c_str()));
ToJString(env, config.title));
env->SetLongField(
java_config,
env->GetFieldID(s_mii_selector_config_class, "initially_selected_mii_index", "J"),
@ -39,7 +40,7 @@ void AndroidMiiSelector::Setup(const Frontend::MiiSelectorConfig& config) {
env->NewObjectArray(static_cast<jsize>(miis.size()), string_class, nullptr);
for (std::size_t i = 0; i < miis.size(); ++i) {
const auto name = Common::UTF16BufferToUTF8(miis[i].mii_name);
env->SetObjectArrayElement(array, static_cast<jsize>(i), env->NewStringUTF(name.c_str()));
env->SetObjectArrayElement(array, static_cast<jsize>(i), ToJString(env, name));
}
env->SetObjectField(
java_config,

View file

@ -31,15 +31,15 @@ static jobject ToJavaKeyboardConfig(const Frontend::KeyboardConfig& config) {
static_cast<jboolean>(config.multiline_mode));
env->SetObjectField(object,
env->GetFieldID(s_keyboard_config_class, "hint_text", "Ljava/lang/String;"),
env->NewStringUTF(config.hint_text.c_str()));
ToJString(env, config.hint_text));
const jclass string_class = reinterpret_cast<jclass>(env->FindClass("java/lang/String"));
const jobjectArray array =
env->NewObjectArray(static_cast<jsize>(config.button_text.size()), string_class,
env->NewStringUTF(config.button_text[0].c_str()));
ToJString(env, config.button_text[0]));
for (std::size_t i = 1; i < config.button_text.size(); ++i) {
env->SetObjectArrayElement(array, static_cast<jsize>(i),
env->NewStringUTF(config.button_text[i].c_str()));
ToJString(env, config.button_text[i]));
}
env->SetObjectField(
object, env->GetFieldID(s_keyboard_config_class, "button_text", "[Ljava/lang/String;"),
@ -70,8 +70,7 @@ void AndroidKeyboard::Execute(const Frontend::KeyboardConfig& config) {
void AndroidKeyboard::ShowError(const std::string& error) {
JNIEnv* env = IDCache::GetEnvForThread();
env->CallStaticVoidMethod(s_software_keyboard_class, s_swkbd_show_error,
env->NewStringUTF(error.c_str()));
env->CallStaticVoidMethod(s_software_keyboard_class, s_swkbd_show_error, ToJString(env, error));
}
void InitJNI(JNIEnv* env) {

View file

@ -64,8 +64,8 @@ static bool DisplayAlertMessage(const char* caption, const char* text, bool yes_
// Execute the Java method.
jboolean result = env->CallStaticBooleanMethod(
IDCache::GetNativeLibraryClass(), IDCache::GetDisplayAlertMsg(), env->NewStringUTF(caption),
env->NewStringUTF(text), yes_no ? JNI_TRUE : JNI_FALSE);
IDCache::GetNativeLibraryClass(), IDCache::GetDisplayAlertMsg(), ToJString(env, caption),
ToJString(env, text), yes_no ? JNI_TRUE : JNI_FALSE);
return result != JNI_FALSE;
}
@ -74,8 +74,8 @@ static std::string DisplayAlertPrompt(const char* caption, const char* text, int
JNIEnv* env = IDCache::GetEnvForThread();
jstring value = reinterpret_cast<jstring>(env->CallStaticObjectMethod(
IDCache::GetNativeLibraryClass(), IDCache::GetDisplayAlertPrompt(),
env->NewStringUTF(caption), env->NewStringUTF(text), buttonConfig));
IDCache::GetNativeLibraryClass(), IDCache::GetDisplayAlertPrompt(), ToJString(env, caption),
ToJString(env, text), buttonConfig));
return GetJString(env, value);
}