[android] RunOnGuiThread implementation

This commit is contained in:
ExMix 2015-05-08 14:42:05 +03:00 committed by r.kuznetsov
parent 73fcc0aa88
commit e8f720413e
9 changed files with 124 additions and 29 deletions

View file

@ -63,6 +63,7 @@ LOCAL_HEADER_FILES := \
com/mapswithme/maps/Framework.hpp \
com/mapswithme/maps/MapStorage.hpp \
com/mapswithme/platform/Platform.hpp \
com/mapswithme/platform/MethodRef.hpp \
com/mapswithme/platform/http_thread_android.hpp \
com/mapswithme/opengl/android_gl_utils.hpp \
com/mapswithme/opengl/androidoglcontext.hpp \
@ -92,6 +93,7 @@ LOCAL_SRC_FILES := \
com/mapswithme/platform/Platform.cpp \
com/mapswithme/platform/HttpThread.cpp \
com/mapswithme/platform/Language.cpp \
com/mapswithme/platform/MethodRef.cpp \
com/mapswithme/platform/PThreadImpl.cpp \
com/mapswithme/utils/StringUtils.cpp \
com/mapswithme/util/Config.cpp \

View file

@ -68,11 +68,6 @@ enum MultiTouchAction
MULTITOUCH_CANCEL = 0x00000004
};
void ShowAllSearchResultsImpl()
{
frm()->ShowAllSearchResults();
}
Framework::Framework()
: m_mask(0),
m_isCleanSingleClick(false),
@ -97,7 +92,7 @@ void Framework::OnLocationError(int errorCode)
void Framework::OnLocationUpdated(location::GpsInfo const & info)
{
Platform::RunOnGuiThreadImpl(bind(&::Framework::OnLocationUpdate, ref(m_work), info));
m_work.OnLocationUpdate(info);
}
void Framework::OnCompassUpdated(location::CompassInfo const & info)
@ -109,7 +104,7 @@ void Framework::OnCompassUpdated(location::CompassInfo const & info)
if (fabs(ang::GetShortestDistance(m_lastCompass, info.m_bearing)) >= COMPASS_THRASHOLD)
{
m_lastCompass = info.m_bearing;
Platform::RunOnGuiThreadImpl(bind(&::Framework::OnCompassUpdate, ref(m_work), info));
m_work.OnCompassUpdate(info);
}
}
@ -420,7 +415,7 @@ void Framework::ShowSearchResult(search::Result const & r)
void Framework::ShowAllSearchResults()
{
m_doLoadState = false;
Platform::RunOnGuiThreadImpl(bind(&ShowAllSearchResultsImpl));
m_work.ShowAllSearchResults();
}
bool Framework::Search(search::SearchParams const & params)
@ -1197,7 +1192,7 @@ extern "C"
JNIEXPORT void JNICALL
Java_com_mapswithme_maps_Framework_nativeLoadBookmarks(JNIEnv * env, jclass thiz)
{
android::Platform::RunOnGuiThreadImpl(bind(&::Framework::LoadBookmarks, frm()));
frm()->LoadBookmarks();
}
JNIEXPORT jboolean JNICALL
@ -1221,7 +1216,7 @@ extern "C"
JNIEXPORT void JNICALL
Java_com_mapswithme_maps_Framework_nativeCloseRouting(JNIEnv * env, jclass thiz)
{
android::Platform::RunOnGuiThreadImpl(bind(&::Framework::CloseRouting, frm()));
frm()->CloseRouting();
}
JNIEXPORT void JNICALL
@ -1229,15 +1224,14 @@ extern "C"
jdouble startLon, jdouble finishLat,
jdouble finishLon)
{
android::Platform::RunOnGuiThreadImpl(bind(&::Framework::BuildRoute, frm(),
MercatorBounds::FromLatLon(startLat, startLon),
MercatorBounds::FromLatLon(finishLat, finishLon), 0 /* timeoutSec */ ));
frm()->BuildRoute(MercatorBounds::FromLatLon(startLat, startLon),
MercatorBounds::FromLatLon(finishLat, finishLon), 0 /* timeoutSec */);
}
JNIEXPORT void JNICALL
Java_com_mapswithme_maps_Framework_nativeFollowRoute(JNIEnv * env, jclass thiz)
{
android::Platform::RunOnGuiThreadImpl(bind(&::Framework::FollowRoute, frm()));
frm()->FollowRoute();
}
JNIEXPORT void JNICALL
@ -1443,7 +1437,7 @@ extern "C"
Java_com_mapswithme_maps_Framework_setMapStyle(JNIEnv * env, jclass thiz, jint mapStyle)
{
MapStyle const val = static_cast<MapStyle>(mapStyle);
android::Platform::RunOnGuiThreadImpl(bind(&android::Framework::SetMapStyle, g_framework, val));
g_framework->SetMapStyle(val);
}
JNIEXPORT jint JNICALL

View file

@ -27,11 +27,10 @@ extern "C"
g_framework = new android::Framework();
}
JNIEXPORT jboolean JNICALL
Java_com_mapswithme_maps_MwmApplication_nativeIsBenchmarking(JNIEnv * env, jobject thiz)
JNIEXPORT void JNICALL
Java_com_mapswithme_maps_MWMApplication_nativeCallOnUIThread(JNIEnv * env, jobject thiz, jlong functorPointer)
{
///@TODO UVR
return JNI_FALSE; //static_cast<jboolean>(g_framework->NativeFramework()->IsBenchmarking());
android::Platform::Instance().CallNativeFunctor(functorPointer);
}
JNIEXPORT jboolean JNICALL

View file

@ -8,7 +8,6 @@
#include "std/string.hpp"
string ReplaceDeprecatedLanguageCode(string const & lang)
{
// in* -> id

View file

@ -0,0 +1,37 @@
#include "MethodRef.hpp"
#include <base/assert.hpp>
#include <base/logging.hpp>
MethodRef::MethodRef(char const * name, char const * signature)
: m_name(name)
, m_signature(signature)
, m_methodID(nullptr)
, m_object(nullptr)
{
LOG(LDEBUG, ("Name = ", name));
}
MethodRef::~MethodRef()
{
if (m_object != nullptr)
jni::GetEnv()->DeleteGlobalRef(m_object);
}
void MethodRef::Init(JNIEnv * env, jobject obj)
{
m_object = env->NewGlobalRef(obj);
jclass k = env->GetObjectClass(m_object);
m_methodID = env->GetMethodID(k, m_name, m_signature);
ASSERT(m_object != nullptr, ());
ASSERT(m_methodID != nullptr, ());
}
void MethodRef::CallVoid(jlong arg)
{
JNIEnv * jniEnv = jni::GetEnv();
ASSERT(jniEnv != nullptr, ());
ASSERT(m_object != nullptr, ());
ASSERT(m_methodID != nullptr, ());
jniEnv->CallVoidMethod(m_object, m_methodID, static_cast<jlong>(arg));
}

View file

@ -0,0 +1,19 @@
#pragma once
#include "../core/jni_helper.hpp"
class MethodRef
{
public:
MethodRef(char const * name, char const * signature);
~MethodRef();
void Init(JNIEnv * env, jobject obj);
void CallVoid(jlong arg);
private:
char const* m_name;
char const * m_signature;
jmethodID m_methodID;
jobject m_object;
};

View file

@ -70,7 +70,7 @@ string Platform::GetMemoryInfo() const
void Platform::RunOnGuiThread(TFunctor const & fn)
{
android::Platform::RunOnGuiThreadImpl(fn);
android::Platform::Instance().RunOnGuiThread(fn);
}
Platform::EConnectionType Platform::ConnectionStatus()
@ -90,6 +90,11 @@ Platform::EConnectionType Platform::ConnectionStatus()
namespace android
{
Platform::Platform()
: m_runOnUI("runNativeFunctorOnUIThread", "(J)V")
{
}
void Platform::Initialize(JNIEnv * env,
jstring apkPath, jstring storagePath,
jstring tmpPath, jstring obbGooglePath,
@ -140,6 +145,22 @@ namespace android
(void) ConnectionStatus();
}
void Platform::InitAppMethodRefs(JNIEnv * env, jobject appObject)
{
m_runOnUI.Init(env, appObject);
}
void Platform::CallNativeFunctor(jlong functionPointer)
{
TFunctor * fn = reinterpret_cast<TFunctor *>(functionPointer);
(*fn)();
delete fn;
}
void Platform::OnExternalStorageStatusChanged(bool isAvailable)
{
}
string Platform::GetStoragePathPrefix() const
{
size_t const count = m_writableDir.size();
@ -168,10 +189,9 @@ namespace android
return platform;
}
void Platform::RunOnGuiThreadImpl(TFunctor const & fn, bool blocking)
void Platform::RunOnGuiThread(TFunctor const & fn)
{
///@TODO
//postMWMEvent(new TFunctor(fn), blocking);
m_runOnUI.CallVoid(reinterpret_cast<jlong>(new TFunctor(fn)));
}
}

View file

@ -2,29 +2,38 @@
#include <jni.h>
#include "platform/platform.hpp"
#include "MethodRef.hpp"
#include "platform/platform.hpp"
namespace android
{
class Platform : public ::Platform
{
public:
Platform();
void Initialize(JNIEnv * env,
jstring apkPath, jstring storagePath,
jstring tmpPath, jstring obbGooglePath,
jstring flavorName, jstring buildType,
bool isYota, bool isTablet);
void InitAppMethodRefs(JNIEnv * env, jobject appObject);
void CallNativeFunctor(jlong functionPointer);
void OnExternalStorageStatusChanged(bool isAvailable);
/// get storage path without ending "/MapsWithMe/"
string GetStoragePathPrefix() const;
/// assign storage path (should contain ending "/MapsWithMe/")
void SetStoragePath(string const & path);
bool HasAvailableSpaceForWriting(uint64_t size) const;
static void RunOnGuiThreadImpl(TFunctor const & fn, bool blocking = false);
void RunOnGuiThread(TFunctor const & fn);
static Platform & Instance();
private:
MethodRef m_runOnUI;
};
}

View file

@ -4,6 +4,7 @@ import android.app.Application;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Environment;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.util.Log;
@ -42,6 +43,9 @@ public class MwmApplication extends Application
private boolean mAreCountersInitialized;
private boolean mIsFrameworkInitialized;
private boolean mAreStatsInitialised;
private Handler mMainLoopHandler;
public MwmApplication()
{
super();
@ -93,6 +97,7 @@ public class MwmApplication extends Application
public void onCreate()
{
super.onCreate();
mMainLoopHandler = new Handler(getMainLooper());
initPaths();
nativeInitPlatform(getApkPath(), getDataStoragePath(), getTempPath(), getObbGooglePath(),
@ -183,6 +188,18 @@ public class MwmApplication extends Application
System.loadLibrary("mapswithme");
}
public void runNativeFunctorOnUIThread(final long functionPointer)
{
mMainLoopHandler.post(new Runnable()
{
@Override
public void run()
{
nativeCallOnUIThread(functionPointer);
}
});
}
/**
* Initializes native Platform with paths. Should be called before usage of any other native components.
*/
@ -191,8 +208,7 @@ public class MwmApplication extends Application
private native void nativeInitFramework();
public native boolean nativeIsBenchmarking();
private native void nativeCallOnUIThread(long functorPointer);
private native void nativeAddLocalization(String name, String value);
/*