From 6535dc2e38cbb032046f920c9524ace61f587ece Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Wed, 22 Jan 2020 17:19:32 +0100 Subject: [PATCH] Add a wrapper for the Book class. --- src/android/book.cpp | 67 ++++++++++++++++++++++++ src/android/meson.build | 3 ++ src/android/org/kiwix/kiwixlib/Book.java | 35 +++++++++++++ src/android/utils.h | 23 ++++++++ 4 files changed, 128 insertions(+) create mode 100644 src/android/book.cpp create mode 100644 src/android/org/kiwix/kiwixlib/Book.java diff --git a/src/android/book.cpp b/src/android/book.cpp new file mode 100644 index 000000000..d2b93878f --- /dev/null +++ b/src/android/book.cpp @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2020 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + + +#include +#include "org_kiwix_kiwixlib_Book.h" + +#include "utils.h" +#include "book.h" + +JNIEXPORT void JNICALL +Java_org_kiwix_kiwixlib_Book_allocate( + JNIEnv* env, jobject thisObj) +{ + allocate(env, thisObj); +} + +JNIEXPORT void JNICALL +Java_org_kiwix_kiwixlib_Book_dispose(JNIEnv* env, jobject thisObj) +{ + dispose(env, thisObj); +} + +#define BOOK (getPtr(env, thisObj)) + +#define METHOD(retType, name) JNIEXPORT retType JNICALL \ +Java_org_kiwix_kiwixlib_Book_##name (JNIEnv* env, jobject thisObj) \ +{ \ + auto cRet = BOOK->name(); \ + retType ret = c2jni(cRet, env); \ + return ret; \ +} + +METHOD(jstring, getId) +METHOD(jstring, getPath) +METHOD(jboolean, isPathValid) +METHOD(jstring, getTitle) +METHOD(jstring, getDescription) +METHOD(jstring, getLanguage) +METHOD(jstring, getCreator) +METHOD(jstring, getPublisher) +METHOD(jstring, getDate) +METHOD(jstring, getUrl) +METHOD(jstring, getName) +METHOD(jstring, getTags) +METHOD(jlong, getArticleCount) +METHOD(jlong, getMediaCount) +METHOD(jlong, getSize) +METHOD(jstring, getFavicon) +METHOD(jstring, getFaviconUrl) +METHOD(jstring, getFaviconMimeType) diff --git a/src/android/meson.build b/src/android/meson.build index 2755f5d2e..0b4fb5c19 100644 --- a/src/android/meson.build +++ b/src/android/meson.build @@ -1,6 +1,7 @@ kiwix_jni = custom_target('jni', input: ['org/kiwix/kiwixlib/JNIICU.java', + 'org/kiwix/kiwixlib/Book.java', 'org/kiwix/kiwixlib/JNIKiwixReader.java', 'org/kiwix/kiwixlib/JNIKiwixLibrary.java', 'org/kiwix/kiwixlib/JNIKiwixSearcher.java', @@ -11,6 +12,7 @@ kiwix_jni = custom_target('jni', 'org/kiwix/kiwixlib/JNIKiwixException.java', 'org/kiwix/kiwixlib/Pair.java'], output: ['org_kiwix_kiwixlib_JNIKiwix.h', + 'org_kiwix_kiwixlib_Book.h', 'org_kiwix_kiwixlib_JNIKiwixReader.h', 'org_kiwix_kiwixlib_JNIKiwixLibrary.h', 'org_kiwix_kiwixlib_JNIKiwixServer.h', @@ -21,6 +23,7 @@ kiwix_jni = custom_target('jni', kiwix_sources += [ 'android/kiwixicu.cpp', + 'android/book.cpp', 'android/kiwixreader.cpp', 'android/kiwixlibrary.cpp', 'android/kiwixsearcher.cpp', diff --git a/src/android/org/kiwix/kiwixlib/Book.java b/src/android/org/kiwix/kiwixlib/Book.java new file mode 100644 index 000000000..06b72e11d --- /dev/null +++ b/src/android/org/kiwix/kiwixlib/Book.java @@ -0,0 +1,35 @@ + +package org.kiwix.kiwixlib; + +public class Book +{ + public Book() { allocate(); } + + @Override + protected void finalize() { dispose(); } + + public native String getId(); + public native String getPath(); + public native boolean isPathValid(); + public native String getTitle(); + public native String getDescription(); + public native String getLanguage(); + public native String getCreator(); + public native String getPublisher(); + public native String getDate(); + public native String getUrl(); + public native String getName(); + public native String getTags(); + + public native long getArticleCount(); + public native long getMediaCount(); + public native long getSize(); + + public native String getFavicon(); + public native String getFaviconUrl(); + public native String getFaviconMimeType(); + + private native void allocate(); + private native void dispose(); + private long nativeHandle; +} diff --git a/src/android/utils.h b/src/android/utils.h index 87eac0ca2..513dac771 100644 --- a/src/android/utils.h +++ b/src/android/utils.h @@ -29,6 +29,29 @@ extern pthread_mutex_t globalLock; +template +void allocate(JNIEnv* env, jobject thisObj) +{ + jclass thisClass = env->GetObjectClass(thisObj); + jfieldID fidNumber = env->GetFieldID(thisClass, "nativeHandle", "J"); + T* ptr = new T(); + env->SetLongField(thisObj, fidNumber, reinterpret_cast(ptr)); +} + +template +T* getPtr(JNIEnv* env, jobject thisObj) +{ + jclass thisClass = env->GetObjectClass(thisObj); + jfieldID fidNumber = env->GetFieldID(thisClass, "nativeHandle", "J"); + return reinterpret_cast(env->GetLongField(thisObj, fidNumber)); +} + +template +void dispose(JNIEnv* env, jobject thisObj) +{ + delete getPtr(env, thisObj); +} + inline jfieldID getHandleField(JNIEnv* env, jobject obj) { jclass c = env->GetObjectClass(obj);