ANDROID: Reduce differences with ScummVM

This commit is contained in:
Dries Harnie 2020-05-23 16:58:25 +02:00
parent 1a49825573
commit fb7260c406
4 changed files with 54 additions and 25 deletions

View file

@ -98,22 +98,18 @@ bool AssetInputStream::seek(int32 offset, int whence) {
} else if (whence == SEEK_END) { } else if (whence == SEEK_END) {
_pos = _len + offset; _pos = _len + offset;
} }
assert(_pos < _len); assert(_pos <= _len);
_eos = false; _eos = false;
return true; return true;
} }
AndroidAssetArchive::AndroidAssetArchive(jobject am) : _hasCached(false) {
AndroidAssetArchive::AndroidAssetArchive(jobject am) {
JNIEnv *env = JNI::getEnv(); JNIEnv *env = JNI::getEnv();
_am = AAssetManager_fromJava(env, am); _am = AAssetManager_fromJava(env, am);
_cachedMembers = NULL;
} }
AndroidAssetArchive::~AndroidAssetArchive() { AndroidAssetArchive::~AndroidAssetArchive() {
delete _cachedMembers;
_cachedMembers = NULL;
} }
bool AndroidAssetArchive::hasFile(const Common::String &name) const { bool AndroidAssetArchive::hasFile(const Common::String &name) const {
@ -127,32 +123,30 @@ bool AndroidAssetArchive::hasFile(const Common::String &name) const {
} }
int AndroidAssetArchive::listMembers(Common::ArchiveMemberList &member_list) const { int AndroidAssetArchive::listMembers(Common::ArchiveMemberList &member_list) const {
if (_cachedMembers) { if (_hasCached) {
member_list.insert(member_list.end(), _cachedMembers->begin(), _cachedMembers->end()); member_list.insert(member_list.end(), _cachedMembers.begin(), _cachedMembers.end());
return _cachedMembers->size(); return _cachedMembers.size();
} }
// ResidualVM specific: multiple directories
Common::List<Common::String> dirs; Common::List<Common::String> dirs;
dirs.push_back(""); dirs.push_back("");
dirs.push_back("shaders"); dirs.push_back("shaders");
int count = 0; int count = 0;
while (!dirs.empty()) { for (const auto& currentDir : dirs) {
Common::String currentDir = dirs.front();
dirs.pop_front();
AAssetDir *dir = AAssetManager_openDir(_am, currentDir.c_str()); AAssetDir *dir = AAssetManager_openDir(_am, currentDir.c_str());
const char *file = AAssetDir_getNextFileName(dir); const char *file = AAssetDir_getNextFileName(dir);
while (file) { while (file) {
Common::String f(file); member_list.push_back(getMember(currentDir + Common::String(file)));
f = currentDir + f;
member_list.push_back(getMember(f));
++count; ++count;
file = AAssetDir_getNextFileName(dir); file = AAssetDir_getNextFileName(dir);
} }
AAssetDir_close(dir); AAssetDir_close(dir);
} }
_cachedMembers = new Common::ArchiveMemberList(member_list); _cachedMembers = Common::ArchiveMemberList(member_list);
_hasCached = true;
return count; return count;
} }

View file

@ -46,7 +46,8 @@ public:
private: private:
AAssetManager *_am; AAssetManager *_am;
mutable Common::ArchiveMemberList *_cachedMembers; mutable Common::ArchiveMemberList _cachedMembers;
mutable bool _hasCached;
}; };
#endif #endif

View file

@ -46,6 +46,7 @@
#include "common/error.h" #include "common/error.h"
#include "common/textconsole.h" #include "common/textconsole.h"
#include "common/translation.h" #include "common/translation.h"
#include "common/encoding.h"
#include "engines/engine.h" #include "engines/engine.h"
#include "backends/platform/android/android.h" #include "backends/platform/android/android.h"
@ -84,6 +85,7 @@ jmethodID JNI::_MID_setTextInClipboard = 0;
jmethodID JNI::_MID_isConnectionLimited = 0; jmethodID JNI::_MID_isConnectionLimited = 0;
jmethodID JNI::_MID_setWindowCaption = 0; jmethodID JNI::_MID_setWindowCaption = 0;
jmethodID JNI::_MID_showVirtualKeyboard = 0; jmethodID JNI::_MID_showVirtualKeyboard = 0;
jmethodID JNI::_MID_showKeyboardControl = 0;
jmethodID JNI::_MID_getSysArchives = 0; jmethodID JNI::_MID_getSysArchives = 0;
jmethodID JNI::_MID_getAllStorageLocations = 0; jmethodID JNI::_MID_getAllStorageLocations = 0;
jmethodID JNI::_MID_initSurface = 0; jmethodID JNI::_MID_initSurface = 0;
@ -226,8 +228,23 @@ void JNI::getDPI(float *values) {
} }
void JNI::displayMessageOnOSD(const char *msg) { void JNI::displayMessageOnOSD(const char *msg) {
// called from common/osd_message_queue, method: OSDMessageQueue::pollEvent()
JNIEnv *env = JNI::getEnv(); JNIEnv *env = JNI::getEnv();
jstring java_msg = env->NewStringUTF(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) {
// Show a placeholder indicative of the translation error instead of silent failing
utf8Msg = "?";
LOGE("Failed to convert message to UTF-8 for OSD!");
}
jstring java_msg = env->NewStringUTF(utf8Msg);
env->CallVoidMethod(_jobj, _MID_displayMessageOnOSD, java_msg); env->CallVoidMethod(_jobj, _MID_displayMessageOnOSD, java_msg);
@ -261,9 +278,8 @@ bool JNI::openUrl(const char *url) {
} }
bool JNI::hasTextInClipboard() { bool JNI::hasTextInClipboard() {
bool hasText = false;
JNIEnv *env = JNI::getEnv(); JNIEnv *env = JNI::getEnv();
hasText = env->CallBooleanMethod(_jobj, _MID_hasTextInClipboard); bool hasText = env->CallBooleanMethod(_jobj, _MID_hasTextInClipboard);
if (env->ExceptionCheck()) { if (env->ExceptionCheck()) {
LOGE("Failed to check the contents of the clipboard"); LOGE("Failed to check the contents of the clipboard");
@ -300,13 +316,12 @@ Common::String JNI::getTextFromClipboard() {
} }
bool JNI::setTextInClipboard(const Common::String &text) { bool JNI::setTextInClipboard(const Common::String &text) {
bool success = true;
JNIEnv *env = JNI::getEnv(); JNIEnv *env = JNI::getEnv();
jbyteArray javaText = env->NewByteArray(text.size()); jbyteArray javaText = env->NewByteArray(text.size());
env->SetByteArrayRegion(javaText, 0, text.size(), reinterpret_cast<const jbyte*>(text.c_str())); env->SetByteArrayRegion(javaText, 0, text.size(), reinterpret_cast<const jbyte*>(text.c_str()));
success = env->CallBooleanMethod(_jobj, _MID_setTextInClipboard, javaText); bool success = env->CallBooleanMethod(_jobj, _MID_setTextInClipboard, javaText);
if (env->ExceptionCheck()) { if (env->ExceptionCheck()) {
LOGE("Failed to add text to the clipboard"); LOGE("Failed to add text to the clipboard");
@ -321,9 +336,8 @@ bool JNI::setTextInClipboard(const Common::String &text) {
} }
bool JNI::isConnectionLimited() { bool JNI::isConnectionLimited() {
bool limited = false;
JNIEnv *env = JNI::getEnv(); JNIEnv *env = JNI::getEnv();
limited = env->CallBooleanMethod(_jobj, _MID_isConnectionLimited); bool limited = env->CallBooleanMethod(_jobj, _MID_isConnectionLimited);
if (env->ExceptionCheck()) { if (env->ExceptionCheck()) {
LOGE("Failed to check whether connection's limited"); LOGE("Failed to check whether connection's limited");
@ -365,6 +379,19 @@ void JNI::showVirtualKeyboard(bool enable) {
} }
} }
void JNI::showKeyboardControl(bool enable) {
JNIEnv *env = JNI::getEnv();
env->CallVoidMethod(_jobj, _MID_showKeyboardControl, enable);
if (env->ExceptionCheck()) {
LOGE("Error trying to show virtual keyboard control");
env->ExceptionDescribe();
env->ExceptionClear();
}
}
void JNI::addSysArchivesToSearchSet(Common::SearchSet &s, int priority) { void JNI::addSysArchivesToSearchSet(Common::SearchSet &s, int priority) {
JNIEnv *env = JNI::getEnv(); JNIEnv *env = JNI::getEnv();
@ -521,6 +548,7 @@ void JNI::create(JNIEnv *env, jobject self, jobject asset_manager,
FIND_METHOD(, setTextInClipboard, "([B)Z"); FIND_METHOD(, setTextInClipboard, "([B)Z");
FIND_METHOD(, isConnectionLimited, "()Z"); FIND_METHOD(, isConnectionLimited, "()Z");
FIND_METHOD(, showVirtualKeyboard, "(Z)V"); FIND_METHOD(, showVirtualKeyboard, "(Z)V");
FIND_METHOD(, showKeyboardControl, "(Z)V");
FIND_METHOD(, getSysArchives, "()[Ljava/lang/String;"); FIND_METHOD(, getSysArchives, "()[Ljava/lang/String;");
FIND_METHOD(, getAllStorageLocations, "()[Ljava/lang/String;"); FIND_METHOD(, getAllStorageLocations, "()[Ljava/lang/String;");
FIND_METHOD(, initSurface, "()Ljavax/microedition/khronos/egl/EGLSurface;"); FIND_METHOD(, initSurface, "()Ljavax/microedition/khronos/egl/EGLSurface;");
@ -554,7 +582,11 @@ void JNI::destroy(JNIEnv *env, jobject self) {
delete _asset_archive; delete _asset_archive;
_asset_archive = 0; _asset_archive = 0;
delete _system; // _system is a pointer of OSystem_Android <--- ModularBackend <--- BaseBacked <--- Common::OSystem
// It's better to call destroy() rather than just delete here
// to avoid mutex issues if a Common::String is used after this point
_system->destroy();
g_system = 0; g_system = 0;
_system = 0; _system = 0;

View file

@ -64,6 +64,7 @@ public:
static bool setTextInClipboard(const Common::String &text); static bool setTextInClipboard(const Common::String &text);
static bool isConnectionLimited(); static bool isConnectionLimited();
static void showVirtualKeyboard(bool enable); static void showVirtualKeyboard(bool enable);
static void showKeyboardControl(bool enable);
static void addSysArchivesToSearchSet(Common::SearchSet &s, int priority); static void addSysArchivesToSearchSet(Common::SearchSet &s, int priority);
static inline bool haveSurface(); static inline bool haveSurface();
@ -103,6 +104,7 @@ private:
static jmethodID _MID_isConnectionLimited; static jmethodID _MID_isConnectionLimited;
static jmethodID _MID_setWindowCaption; static jmethodID _MID_setWindowCaption;
static jmethodID _MID_showVirtualKeyboard; static jmethodID _MID_showVirtualKeyboard;
static jmethodID _MID_showKeyboardControl;
static jmethodID _MID_getSysArchives; static jmethodID _MID_getSysArchives;
static jmethodID _MID_getAllStorageLocations; static jmethodID _MID_getAllStorageLocations;
static jmethodID _MID_initSurface; static jmethodID _MID_initSurface;