From b8c26508c90b7926ea44b40e5bcb2f530b8d1464 Mon Sep 17 00:00:00 2001 From: Thanasis Antoniou Date: Fri, 8 Nov 2019 16:59:52 +0200 Subject: ANDROID: Fix OSD message display crash or fail For translated message text. Android expected UTF-8 format for the message Also makeToast for OSD needed to be run from the main UI thread --- backends/platform/android/jni.cpp | 20 +++++++++++++++++++- .../android/org/scummvm/scummvm/ScummVMActivity.java | 12 +++++++++--- 2 files changed, 28 insertions(+), 4 deletions(-) (limited to 'backends') diff --git a/backends/platform/android/jni.cpp b/backends/platform/android/jni.cpp index 8c330c0e89..cedee38b7e 100644 --- a/backends/platform/android/jni.cpp +++ b/backends/platform/android/jni.cpp @@ -46,6 +46,7 @@ #include "common/error.h" #include "common/textconsole.h" #include "common/translation.h" +#include "common/encoding.h" #include "engines/engine.h" #include "backends/platform/android/android.h" @@ -225,8 +226,24 @@ void JNI::getDPI(float *values) { } void JNI::displayMessageOnOSD(const char *msg) { + // called from common/osd_message_queue, method: OSDMessageQueue::pollEvent() JNIEnv *env = JNI::getEnv(); - jstring java_msg = env->NewStringUTF(msg); +// LOGD("OSD orig MESSAGE: %s", msg); + Common::String fromEncoding = "ISO-8859-1"; +#ifdef USE_TRANSLATION + if (TransMan.getCurrentCharset() != "ASCII") { + fromEncoding = TransMan.getCurrentCharset(); + } +#endif + Common::Encoding converter("UTF-8", fromEncoding.c_str()); + + const char *utf8Msg = converter.convert(msg, converter.stringLength(msg, fromEncoding) ); + if (utf8Msg == nullptr) { + LOGE("Failed to convert message to UTF-8 for OSD!"); + return; + } +// LOGD("OSD target MESSAGE: %s", utf8Msg); + jstring java_msg = env->NewStringUTF(utf8Msg); env->CallVoidMethod(_jobj, _MID_displayMessageOnOSD, java_msg); @@ -693,6 +710,7 @@ void JNI::setPause(JNIEnv *env, jobject self, jboolean value) { jstring JNI::getCurrentCharset(JNIEnv *env, jobject self) { #ifdef USE_TRANSLATION if (TransMan.getCurrentCharset() != "ASCII") { +// LOGD("getCurrentCharset: %s", TransMan.getCurrentCharset().c_str()); return env->NewStringUTF(TransMan.getCurrentCharset().c_str()); } #endif diff --git a/backends/platform/android/org/scummvm/scummvm/ScummVMActivity.java b/backends/platform/android/org/scummvm/scummvm/ScummVMActivity.java index dca52a92f3..f55639f60b 100644 --- a/backends/platform/android/org/scummvm/scummvm/ScummVMActivity.java +++ b/backends/platform/android/org/scummvm/scummvm/ScummVMActivity.java @@ -76,9 +76,15 @@ public class ScummVMActivity extends Activity { } @Override - protected void displayMessageOnOSD(String msg) { - Log.i(LOG_TAG, "OSD: " + msg); - Toast.makeText(ScummVMActivity.this, msg, Toast.LENGTH_LONG).show(); + protected void displayMessageOnOSD(final String msg) { + if (msg != null) { + Log.i(LOG_TAG, "MessageOnOSD: " + msg + " " + getCurrentCharset()); + runOnUiThread(new Runnable() { + public void run() { + Toast.makeText(ScummVMActivity.this, msg, Toast.LENGTH_SHORT).show(); + } + }); + } } @Override -- cgit v1.2.3