[Android] AndroidPlatform realization.

TODO: Fix linkage errors.
This commit is contained in:
vng 2011-06-25 22:22:42 +03:00 committed by Alex Zolotarev
parent 398718ae49
commit bb666aaa3d
9 changed files with 160 additions and 71 deletions

View file

@ -3,8 +3,21 @@ LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := mapswithme
LOCAL_SRC_FILES := main_native.cpp
LOCAL_LDLIBS := -llog -L../../omim-android-release/out/release \
LOCAL_HEADER_FILES := \
jni_helper.h \
jni_string.h \
logging.h \
LOCAL_SRC_FILES := \
main_native.cpp \
jni_helper.cpp \
jni_string.cpp \
platform.cpp \
LOCAL_LDLIBS := -llog -lstdc++ -L../../omim-android-debug/out/debug \
-lwords -lmap -lstorage -lversion -lsearch -lindexer -lyg -lplatform \
-lgeometry -lcoding -lbase -lexpat -lfreetype -lfribidi -lzlib -lbzip2 \
-ljansson -ltomcrypt

View file

@ -0,0 +1,20 @@
#include "jni_helper.h"
#include <assert.h>
namespace jni {
// Some examples of sig:
// "()V" - void function returning void;
// "(Ljava/lang/String;)V" - String function returning void;
jmethodID GetJavaMethodID(JNIEnv * env, jobject obj,
char const * fn, char const * sig)
{
jclass cls = env->GetObjectClass(obj);
jmethodID mid = env->GetMethodID(cls, fn, sig);
assert(mid != 0);
return mid;
}
}

27
android/jni/jni_helper.h Normal file
View file

@ -0,0 +1,27 @@
#include <jni.h>
namespace jni
{
// Some examples of sig:
// "()V" - void function returning void;
// "(Ljava/lang/String;)V" - String function returning void;
jmethodID GetJavaMethodID(JNIEnv * env, jobject obj,
char const * fn, char const * sig);
/* Example of usage:
JNIEXPORT void JNICALL
Java_com_mapswithme_maps_MWMActivity_callbackFromJNI(JNIEnv * env, jobject thiz)
{
LOG("Enter callbackFromJNI");
env->CallVoidMethod(thiz,
jni_help::GetJavaMethodID(env, thiz, "callbackVoid", "()V"));
env->CallVoidMethod(
thiz,
jni_help::GetJavaMethodID(env, thiz, "callbackString",
"(Ljava/lang/String;)V"), env->NewStringUTF("Pass string from JNI."));
LOG("Leave callbackFromJNI");
}
*/
}

View file

@ -0,0 +1,26 @@
#include "jni_string.h"
namespace jni {
String::String(JNIEnv * env, jstring s)
: m_env(env), m_s(s)
{
m_ret = m_env->GetStringChars(m_s, NULL);
}
String::~String()
{
m_env->ReleaseStringChars(m_s, m_ret);
}
string String::ToString() const
{
/// @todo
return std::string();
}
string ToString(JNIEnv * env, jstring s)
{
return String(env, s).ToString();
}
}

21
android/jni/jni_string.h Normal file
View file

@ -0,0 +1,21 @@
#include <string.h>
#include <jni.h>
#include "../../std/string.hpp"
namespace jni
{
class String
{
JNIEnv * m_env;
jstring m_s;
jchar const * m_ret;
public:
String(JNIEnv * env, jstring s);
~String();
string ToString() const;
};
string ToString(JNIEnv * env, jstring s);
}

3
android/jni/logging.h Normal file
View file

@ -0,0 +1,3 @@
#include <android/log.h>
#define LOG(...) __android_log_print(ANDROID_LOG_INFO, "mapswithme", __VA_ARGS__)

View file

@ -1,84 +1,20 @@
#include "logging.h"
#include "platform.h"
#include <string.h>
#include <jni.h>
#include <android/log.h>
#include <assert.h>
#define LOG(...) __android_log_print(ANDROID_LOG_INFO, "mapswithme", __VA_ARGS__)
namespace jni_help
{
// Some examples of sig:
// "()V" - void function returning void;
// "(Ljava/lang/String;)V" - String function returning void;
jmethodID GetJavaMethodID(JNIEnv * env, jobject obj,
char const * fn, char const * sig)
{
jclass cls = env->GetObjectClass(obj);
jmethodID mid = env->GetMethodID(cls, fn, sig);
assert(mid != 0);
return mid;
}
class String
{
JNIEnv * m_env;
jstring m_s;
jchar const * m_ret;
public:
String(JNIEnv * env, jstring s) :
m_env(env), m_s(s)
{
LOG("String constructor");
m_ret = m_env->GetStringChars(m_s, NULL);
}
~String()
{
LOG("String destructor");
m_env->ReleaseStringChars(m_s, m_ret);
}
};
}
extern "C"
{
/* Interop example functions.
JNIEXPORT jstring JNICALL
Java_com_mapswithme_maps_MWMActivity_stringsJNI(JNIEnv * env, jobject thiz,
jstring s)
{
LOG("Enter stringsJNI");
{
jni_help::String str(env, s);
}
LOG("Leave stringsJNI");
return env->NewStringUTF("Return string from JNI.");
}
JNIEXPORT void JNICALL
Java_com_mapswithme_maps_MWMActivity_callbackFromJNI(JNIEnv * env, jobject thiz)
{
LOG("Enter callbackFromJNI");
env->CallVoidMethod(thiz,
jni_help::GetJavaMethodID(env, thiz, "callbackVoid", "()V"));
env->CallVoidMethod(
thiz,
jni_help::GetJavaMethodID(env, thiz, "callbackString",
"(Ljava/lang/String;)V"), env->NewStringUTF("Pass string from JNI."));
LOG("Leave callbackFromJNI");
}
*/
///////////////////////////////////////////////////////////////////////////////////
// MWMActivity
///////////////////////////////////////////////////////////////////////////////////
JNIEXPORT void JNICALL
Java_com_mapswithme_maps_MWMActivity_nativeInit(JNIEnv * env, jobject thiz, jstring path)
Java_com_mapswithme_maps_MWMActivity_nativeInit(JNIEnv * env, jobject activity, jstring path)
{
GetAndroidPlatform().Initialize(env, activity, path);
}
///////////////////////////////////////////////////////////////////////////////////

29
android/jni/platform.cpp Normal file
View file

@ -0,0 +1,29 @@
#include "platform.h"
#include "jni_string.h"
void AndroidPlatform::Initialize(JNIEnv * env, jobject activity, jstring path)
{
m_writableDir = m_resourcesDir = jni::ToString(env, path);
}
int AndroidPlatform::CpuCores() const
{
return 1;
}
string AndroidPlatform::DeviceID() const
{
return "Android";
}
AndroidPlatform & GetAndroidPlatform()
{
static AndroidPlatform platform;
return platform;
}
Platform & GetPlatform()
{
return GetAndroidPlatform();
}

14
android/jni/platform.h Normal file
View file

@ -0,0 +1,14 @@
#include "../../platform/platform.hpp"
#include <jni.h>
class AndroidPlatform : public BasePlatformImpl
{
public:
void Initialize(JNIEnv * env, jobject activity, jstring path);
virtual int CpuCores() const;
virtual string DeviceID() const;
};
AndroidPlatform & GetAndroidPlatform();