[android] Native bindings.

This commit is contained in:
d-kunin 2013-08-05 16:13:21 +03:00
parent a5b757661c
commit fae65739df
5 changed files with 181 additions and 0 deletions

23
android/jni/Android.mk Normal file
View file

@ -0,0 +1,23 @@
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
TARGET_PLATFORM := android-9
LOCAL_CFLAGS := -ffunction-sections -fdata-sections -Wno-psabi
LOCAL_MODULE := tguide
LOCAL_HEADER_FILES := \
and_storage.hpp \
# Android native files
LOCAL_SRC_FILES := \
and_storage.cpp \
# Storage files
LOCAL_SRC_FILES += \
../../storage/storage.cpp \
../../storage/article_info_storage.cpp \
../../storage/index_storage.cpp \
include $(BUILD_SHARED_LIBRARY)

View file

@ -0,0 +1,5 @@
APP_STL := gnustl_static
APP_PLATFORM := android-9
LOCAL_PATH := $(call my-dir)
APP_CFLAGS += -I$(LOCAL_PATH)/../../3rdparty/boost

View file

@ -0,0 +1,96 @@
#include "and_storage.hpp"
#include "jni_util.hpp"
#include "../../storage/storage.hpp"
#include "../../storage/article_info_storage.hpp"
#include "../../storage/index_storage.hpp"
class AndStorage
{
public:
AndStorage()
: m_storage(new ArticleInfoStorageMock(), new IndexStorageMock()) {}
static AndStorage & Instance()
{
static AndStorage storage;
return storage;
}
void Query(string const & query, bool useLocation, double lat, double lon)
{
if (useLocation)
m_storage.QueryArticleInfos(m_result, query, lat, lon);
else
m_storage.QueryArticleInfos(m_result, query);
}
int GetResultSize() const
{
return static_cast<int>(m_result.size());
}
ArticleInfo const & GetArticleInfoByIndex(int index) const
{
return m_result[index];
}
private:
Storage m_storage;
vector<ArticleInfo> m_result;
};
#ifdef __cplusplus
extern "C"
{
#endif
// TODO: *problem
//#define DECLARE_FN(retType, suffix)
#define STORAGE AndStorage::Instance()
/*
* Class: com_example_travelguide_cpp_Storage
* Method: query
* Signature: (Ljava/lang/String;DD)V
*/
JNIEXPORT void JNICALL Java_com_example_travelguide_cpp_Storage_query(JNIEnv * env, jobject thiz, jstring query,
jboolean useLocation, jdouble lat, jdouble lon)
{
STORAGE.Query(JString2StdString(env, query), useLocation, lat, lon);
}
/*
* Class: com_example_travelguide_cpp_Storage
* Method: getResultSize
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_example_travelguide_cpp_Storage_getResultSize
(JNIEnv * env, jobject thiz)
{
return STORAGE.GetResultSize();
}
/*
* Class: com_example_travelguide_cpp_Storage
* Method: getArticleInfoByIndex
* Signature: (I)Lcom/example/travelguide/article/ArticleInfo;
*/
JNIEXPORT jobject JNICALL Java_com_example_travelguide_cpp_Storage_getArticleInfoByIndex
(JNIEnv * env, jobject thiz, jint index)
{
ArticleInfo const & info = STORAGE.GetArticleInfoByIndex(index);
jclass ArtInfoClass = env->FindClass("com/example/travelguide/article/ArticleInfo");
jmethodID initId = env->GetMethodID(ArtInfoClass, "<init>", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
return env->NewObject(ArtInfoClass, initId,
StdString2JString(env, info.m_url),
StdString2JString(env, info.m_thumbnailUrl),
StdString2JString(env, info.m_title));
}
#ifdef __cplusplus
}
#endif

View file

@ -0,0 +1,37 @@
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_example_travelguide_cpp_Storage */
#ifndef _Included_com_example_travelguide_cpp_Storage
#define _Included_com_example_travelguide_cpp_Storage
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_example_travelguide_cpp_Storage
* Method: query
* Signature: (Ljava/lang/String;ZDD)V
*/
JNIEXPORT void JNICALL Java_com_example_travelguide_cpp_Storage_query
(JNIEnv *, jobject, jstring, jboolean, jdouble, jdouble);
/*
* Class: com_example_travelguide_cpp_Storage
* Method: getResultSize
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_example_travelguide_cpp_Storage_getResultSize
(JNIEnv *, jobject);
/*
* Class: com_example_travelguide_cpp_Storage
* Method: getArticleInfoByIndex
* Signature: (I)Lcom/example/travelguide/article/ArticleInfo;
*/
JNIEXPORT jobject JNICALL Java_com_example_travelguide_cpp_Storage_getArticleInfoByIndex
(JNIEnv *, jobject, jint);
#ifdef __cplusplus
}
#endif
#endif

20
android/jni/jni_util.hpp Normal file
View file

@ -0,0 +1,20 @@
#include <jni.h>
#include "../../std/string.hpp"
string JString2StdString(JNIEnv * env, jstring javaStr)
{
string res;
char const * buffer = env->GetStringUTFChars(javaStr, 0);
if (buffer)
{
res = buffer;
env->ReleaseStringUTFChars(javaStr, buffer);
}
return res;
}
jstring StdString2JString(JNIEnv * env, string stdString)
{
return env->NewStringUTF(stdString.c_str());
}