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); env->ReleaseStringUTFChars(jstr, s);
return result; 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> #include <jni.h>
std::string GetJString(JNIEnv* env, jstring jstr); 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. // Refer to the license.txt file included.
#include "common/string_util.h" #include "common/string_util.h"
#include "jni/android_common/android_common.h"
#include "jni/applets/mii_selector.h" #include "jni/applets/mii_selector.h"
#include "jni/id_cache.h" #include "jni/id_cache.h"
@ -26,7 +27,7 @@ void AndroidMiiSelector::Setup(const Frontend::MiiSelectorConfig& config) {
static_cast<jboolean>(config.enable_cancel_button)); static_cast<jboolean>(config.enable_cancel_button));
env->SetObjectField(java_config, env->SetObjectField(java_config,
env->GetFieldID(s_mii_selector_config_class, "title", "Ljava/lang/String;"), env->GetFieldID(s_mii_selector_config_class, "title", "Ljava/lang/String;"),
env->NewStringUTF(config.title.c_str())); ToJString(env, config.title));
env->SetLongField( env->SetLongField(
java_config, java_config,
env->GetFieldID(s_mii_selector_config_class, "initially_selected_mii_index", "J"), 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); env->NewObjectArray(static_cast<jsize>(miis.size()), string_class, nullptr);
for (std::size_t i = 0; i < miis.size(); ++i) { for (std::size_t i = 0; i < miis.size(); ++i) {
const auto name = Common::UTF16BufferToUTF8(miis[i].mii_name); 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( env->SetObjectField(
java_config, java_config,

View file

@ -31,15 +31,15 @@ static jobject ToJavaKeyboardConfig(const Frontend::KeyboardConfig& config) {
static_cast<jboolean>(config.multiline_mode)); static_cast<jboolean>(config.multiline_mode));
env->SetObjectField(object, env->SetObjectField(object,
env->GetFieldID(s_keyboard_config_class, "hint_text", "Ljava/lang/String;"), 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 jclass string_class = reinterpret_cast<jclass>(env->FindClass("java/lang/String"));
const jobjectArray array = const jobjectArray array =
env->NewObjectArray(static_cast<jsize>(config.button_text.size()), string_class, 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) { for (std::size_t i = 1; i < config.button_text.size(); ++i) {
env->SetObjectArrayElement(array, static_cast<jsize>(i), env->SetObjectArrayElement(array, static_cast<jsize>(i),
env->NewStringUTF(config.button_text[i].c_str())); ToJString(env, config.button_text[i]));
} }
env->SetObjectField( env->SetObjectField(
object, env->GetFieldID(s_keyboard_config_class, "button_text", "[Ljava/lang/String;"), 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) { void AndroidKeyboard::ShowError(const std::string& error) {
JNIEnv* env = IDCache::GetEnvForThread(); JNIEnv* env = IDCache::GetEnvForThread();
env->CallStaticVoidMethod(s_software_keyboard_class, s_swkbd_show_error, env->CallStaticVoidMethod(s_software_keyboard_class, s_swkbd_show_error, ToJString(env, error));
env->NewStringUTF(error.c_str()));
} }
void InitJNI(JNIEnv* env) { 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. // Execute the Java method.
jboolean result = env->CallStaticBooleanMethod( jboolean result = env->CallStaticBooleanMethod(
IDCache::GetNativeLibraryClass(), IDCache::GetDisplayAlertMsg(), env->NewStringUTF(caption), IDCache::GetNativeLibraryClass(), IDCache::GetDisplayAlertMsg(), ToJString(env, caption),
env->NewStringUTF(text), yes_no ? JNI_TRUE : JNI_FALSE); ToJString(env, text), yes_no ? JNI_TRUE : JNI_FALSE);
return result != 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(); JNIEnv* env = IDCache::GetEnvForThread();
jstring value = reinterpret_cast<jstring>(env->CallStaticObjectMethod( jstring value = reinterpret_cast<jstring>(env->CallStaticObjectMethod(
IDCache::GetNativeLibraryClass(), IDCache::GetDisplayAlertPrompt(), IDCache::GetNativeLibraryClass(), IDCache::GetDisplayAlertPrompt(), ToJString(env, caption),
env->NewStringUTF(caption), env->NewStringUTF(text), buttonConfig)); ToJString(env, text), buttonConfig));
return GetJString(env, value); return GetJString(env, value);
} }