Files
OpenRCT2-Web/src/openrct2/platform/Platform.Android.cpp
T

165 lines
4.9 KiB
C++
Raw Normal View History

2017-12-01 00:54:03 +00:00
/*****************************************************************************
2020-07-21 15:04:34 +02:00
* Copyright (c) 2014-2020 OpenRCT2 developers
2018-06-15 14:07:34 +02:00
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
2017-12-01 00:54:03 +00:00
#ifdef __ANDROID__
2018-07-21 16:17:06 +02:00
# include "../core/Guard.hpp"
2022-01-08 12:19:24 +01:00
# include "../localisation/Language.h"
2018-07-21 16:17:06 +02:00
# include "Platform2.h"
2017-12-01 00:54:03 +00:00
2022-01-11 11:57:54 +01:00
# include <SDL.h>
# include <jni.h>
AndroidClassLoader::~AndroidClassLoader()
{
JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv();
env->DeleteGlobalRef(_classLoader);
}
jobject AndroidClassLoader::_classLoader;
jmethodID AndroidClassLoader::_findClassMethod;
static std::shared_ptr<AndroidClassLoader> acl;
2017-12-01 00:54:03 +00:00
namespace Platform
{
std::string GetFolderPath(SPECIAL_FOLDER folder)
{
// Android builds currently only read from /sdcard/openrct2*
switch (folder)
{
2018-06-22 23:04:38 +02:00
case SPECIAL_FOLDER::USER_CACHE:
case SPECIAL_FOLDER::USER_CONFIG:
case SPECIAL_FOLDER::USER_DATA:
case SPECIAL_FOLDER::USER_HOME:
return "/sdcard";
default:
return std::string();
2017-12-01 00:54:03 +00:00
}
}
2018-01-21 02:13:32 +00:00
std::string GetDocsPath()
{
return std::string();
}
2017-12-01 23:48:46 +00:00
std::string GetInstallPath()
{
return "/sdcard/openrct2";
}
std::string GetCurrentExecutablePath()
{
2018-02-08 08:47:48 +01:00
Guard::Assert(false, "GetCurrentExecutablePath() not implemented for Android.");
2017-12-01 23:48:46 +00:00
return std::string();
}
utf8* StrDecompToPrecomp(utf8* input)
{
return input;
}
bool HandleSpecialCommandLineArgument(const char* argument)
{
return false;
}
2022-01-08 12:19:24 +01:00
uint16_t GetLocaleLanguage()
{
return LANGUAGE_ENGLISH_UK;
}
2022-01-08 12:24:57 +01:00
CurrencyType GetLocaleCurrency()
{
return Platform::GetCurrencyValue(NULL);
}
MeasurementFormat GetLocaleMeasurementFormat()
{
return MeasurementFormat::Metric;
}
2022-01-10 13:28:23 +01:00
std::string GetSteamPath()
{
return "";
}
2022-01-10 13:41:12 +01:00
# ifndef NO_TTF
std::string GetFontPath(const TTFFontDescriptor& font)
{
STUB();
return "";
}
# endif
2022-01-11 11:48:07 +01:00
float GetDefaultScale()
{
JNIEnv* env = static_cast<JNIEnv*>(SDL_AndroidGetJNIEnv());
jobject activity = static_cast<jobject>(SDL_AndroidGetActivity());
jclass activityClass = env->GetObjectClass(activity);
jmethodID getDefaultScale = env->GetMethodID(activityClass, "getDefaultScale", "()F");
jfloat displayScale = env->CallFloatMethod(activity, getDefaultScale);
env->DeleteLocalRef(activity);
env->DeleteLocalRef(activityClass);
return displayScale;
}
2022-01-11 11:57:54 +01:00
void AndroidInitClassLoader()
{
acl = std::make_shared<AndroidClassLoader>();
}
jclass AndroidFindClass(JNIEnv* env, std::string_view name)
{
return static_cast<jclass>(env->CallObjectMethod(
AndroidClassLoader::_classLoader, AndroidClassLoader::_findClassMethod,
env->NewStringUTF(std::string(name).c_str())));
}
2018-06-22 23:04:38 +02:00
} // namespace Platform
2017-12-01 00:54:03 +00:00
2022-01-11 11:57:54 +01:00
AndroidClassLoader::AndroidClassLoader()
{
log_info("Obtaining JNI class loader");
// This is a workaround to be able to call JNI's ClassLoader from non-main
// thread, based on https://stackoverflow.com/a/16302771
// Apparently it's OK to use it from across different thread, but JNI
// only looks for ClassLoader in the _current_ thread and fails to find
// it when searched for from a native library's non-main thread.
// The solution below works by obtaining a ClassLoader reference in main
// thread and caching it for future use from any thread, instead of using
// it via env->FindClass(). ClassLoader itself is abstract, so we cannot
// create it directly; instead we take an arbitrary class and call
// getClassLoader() on it to create a reference that way.
// If we're here, SDL's JNI_OnLoad has already been called and set env
JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv();
// Take an arbitrary class. While the class does not really matter, it
// makes sense to use one that's most likely already loaded and is unlikely
// to be removed from code.
auto randomClass = env->FindClass("io/openrct2/MainActivity");
jclass classClass = env->GetObjectClass(randomClass);
// Get its class loader
auto classLoaderClass = env->FindClass("java/lang/ClassLoader");
auto getClassLoaderMethod = env->GetMethodID(classClass, "getClassLoader", "()Ljava/lang/ClassLoader;");
// Store the class loader and its findClass method for future use
_classLoader = env->NewGlobalRef(env->CallObjectMethod(randomClass, getClassLoaderMethod));
_findClassMethod = env->GetMethodID(classLoaderClass, "findClass", "(Ljava/lang/String;)Ljava/lang/Class;");
}
2017-12-01 00:54:03 +00:00
#endif