2023-03-25 07:28:45 +00:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
|
2024-02-05 11:07:29 +00:00
|
|
|
#include "android_common.h"
|
2023-03-25 07:28:45 +00:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
|
|
|
|
|
|
|
#include <jni.h>
|
|
|
|
|
2024-02-05 11:07:29 +00:00
|
|
|
#include "common/android/id_cache.h"
|
2023-03-25 07:28:45 +00:00
|
|
|
#include "common/string_util.h"
|
2024-02-05 11:07:29 +00:00
|
|
|
|
|
|
|
namespace Common::Android {
|
2023-03-25 07:28:45 +00:00
|
|
|
|
|
|
|
std::string GetJString(JNIEnv* env, jstring jstr) {
|
|
|
|
if (!jstr) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
const jchar* jchars = env->GetStringChars(jstr, nullptr);
|
|
|
|
const jsize length = env->GetStringLength(jstr);
|
2024-02-05 11:07:29 +00:00
|
|
|
const std::u16string_view string_view(reinterpret_cast<const char16_t*>(jchars),
|
|
|
|
static_cast<u32>(length));
|
2023-03-25 07:28:45 +00:00
|
|
|
const std::string converted_string = Common::UTF16ToUTF8(string_view);
|
|
|
|
env->ReleaseStringChars(jstr, jchars);
|
|
|
|
|
|
|
|
return converted_string;
|
|
|
|
}
|
|
|
|
|
|
|
|
jstring ToJString(JNIEnv* env, std::string_view str) {
|
|
|
|
const std::u16string converted_string = Common::UTF8ToUTF16(str);
|
|
|
|
return env->NewString(reinterpret_cast<const jchar*>(converted_string.data()),
|
|
|
|
static_cast<jint>(converted_string.size()));
|
|
|
|
}
|
|
|
|
|
|
|
|
jstring ToJString(JNIEnv* env, std::u16string_view str) {
|
|
|
|
return ToJString(env, Common::UTF16ToUTF8(str));
|
|
|
|
}
|
2023-12-24 20:42:28 +00:00
|
|
|
|
|
|
|
double GetJDouble(JNIEnv* env, jobject jdouble) {
|
2024-02-05 11:07:29 +00:00
|
|
|
return env->GetDoubleField(jdouble, GetDoubleValueField());
|
2023-12-24 20:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
jobject ToJDouble(JNIEnv* env, double value) {
|
2024-02-05 11:07:29 +00:00
|
|
|
return env->NewObject(GetDoubleClass(), GetDoubleConstructor(), value);
|
2023-12-24 20:42:28 +00:00
|
|
|
}
|
2024-01-19 05:56:43 +00:00
|
|
|
|
|
|
|
s32 GetJInteger(JNIEnv* env, jobject jinteger) {
|
2024-02-05 11:07:29 +00:00
|
|
|
return env->GetIntField(jinteger, GetIntegerValueField());
|
2024-01-19 05:56:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
jobject ToJInteger(JNIEnv* env, s32 value) {
|
2024-02-05 11:07:29 +00:00
|
|
|
return env->NewObject(GetIntegerClass(), GetIntegerConstructor(), value);
|
2024-01-19 05:56:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GetJBoolean(JNIEnv* env, jobject jboolean) {
|
2024-02-05 11:07:29 +00:00
|
|
|
return env->GetBooleanField(jboolean, GetBooleanValueField());
|
2024-01-19 05:56:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
jobject ToJBoolean(JNIEnv* env, bool value) {
|
2024-02-05 11:07:29 +00:00
|
|
|
return env->NewObject(GetBooleanClass(), GetBooleanConstructor(), value);
|
2024-01-19 05:56:43 +00:00
|
|
|
}
|
2024-02-05 11:07:29 +00:00
|
|
|
|
|
|
|
} // namespace Common::Android
|