[android] Potential ANR fix

Signed-off-by: Alexander Borsuk <me@alex.bio>
This commit is contained in:
Alexander Borsuk 2022-11-10 23:42:46 +01:00 committed by Alexander Borsuk
parent f6ceb3f8d9
commit 594f726bb4
3 changed files with 8 additions and 5 deletions

View file

@ -1258,7 +1258,8 @@ Java_app_organicmaps_Framework_nativeGenerateRouteAltitudeChartBits(JNIEnv * env
}
// Passing route limits.
jclass const routeAltitudeLimitsClass = env->GetObjectClass(routeAltitudeLimits);
// Do not use jni::GetGlobalClassRef, because this class is used only to init static fieldId vars.
static jclass const routeAltitudeLimitsClass = env->GetObjectClass(routeAltitudeLimits);
ASSERT(routeAltitudeLimitsClass, ());
static jfieldID const totalAscentField = env->GetFieldID(routeAltitudeLimitsClass, "totalAscent", "I");
@ -1558,7 +1559,7 @@ Java_app_organicmaps_Framework_nativeGet3dMode(JNIEnv * env, jclass, jobject res
bool buildings;
g_framework->Get3dMode(enabled, buildings);
jclass const resultClass = env->GetObjectClass(result);
static jclass const resultClass = env->GetObjectClass(result);
static jfieldID const enabledField = env->GetFieldID(resultClass, "enabled", "Z");
env->SetBooleanField(result, enabledField, enabled);

View file

@ -107,6 +107,7 @@ JavaVM * GetJVM()
jmethodID GetMethodID(JNIEnv * env, jobject obj, char const * name, char const * signature)
{
// GetObjectClass may hang in WaitHoldingLocks.
TScopedLocalClassRef clazz(env, env->GetObjectClass(obj));
ASSERT(clazz.get(), ("Can't get class: ", DescribeException()));

View file

@ -8,6 +8,7 @@ class HttpThread
{
private:
jobject m_self;
jclass m_klass;
public:
HttpThread(std::string const & url,
@ -18,8 +19,8 @@ public:
std::string const & pb)
{
JNIEnv * env = jni::GetEnv();
static jclass const klass = jni::GetGlobalClassRef(env, "app/organicmaps/downloader/ChunkTask");
m_klass = klass;
// public ChunkTask(long httpCallbackID, String url, long beg, long end,
// long expectedFileSize, byte[] postBody, String userAgent)
static jmethodID const initMethodId = jni::GetConstructorID(env, klass, "(JLjava/lang/String;JJJ[B)V");
@ -51,8 +52,8 @@ public:
~HttpThread()
{
JNIEnv * env = jni::GetEnv();
jmethodID methodId = jni::GetMethodID(env, m_self, "cancel", "(Z)Z");
env->CallBooleanMethod(m_self, methodId, false);
static jmethodID const cancelMethodId = env->GetMethodID(m_klass, "cancel", "(Z)Z");
env->CallBooleanMethod(m_self, cancelMethodId, false);
env->DeleteGlobalRef(m_self);
}
};