diff --git a/src/android/kiwixlibrary.cpp b/src/android/kiwixlibrary.cpp new file mode 100644 index 000000000..d2d92010e --- /dev/null +++ b/src/android/kiwixlibrary.cpp @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2013 Emmanuel Engelhart + * Copyright (C) 2017 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 +#include "org_kiwix_kiwixlib_JNIKiwixLibrary.h" + +#include "library.h" +#include "reader.h" +#include "utils.h" + +/* Kiwix Reader JNI functions */ +JNIEXPORT jlong JNICALL Java_org_kiwix_kiwixlib_JNIKiwixLibrary_getNativeLibrary( + JNIEnv* env, jobject obj) +{ + __android_log_print(ANDROID_LOG_INFO, "kiwix", "Attempting to create library"); + Lock l; + try { + kiwix::Library* library = new kiwix::Library(); + return reinterpret_cast(new Handle(library)); + } catch (std::exception& e) { + __android_log_print(ANDROID_LOG_WARN, "kiwix", "Error creating ZIM library"); + __android_log_print(ANDROID_LOG_WARN, "kiwix", e.what()); + return 0; + } +} + +JNIEXPORT void JNICALL +Java_org_kiwix_kiwixlib_JNIKiwixLibrary_dispose(JNIEnv* env, jobject obj) +{ + Handle::dispose(env, obj); +} + +#define LIBRARY (Handle::getHandle(env, obj)) + +/* Kiwix library functions */ +JNIEXPORT jboolean JNICALL +Java_org_kiwix_kiwixlib_JNIKiwixLibrary_addBook(JNIEnv* env, jobject obj, jstring path) +{ + std::string cPath = jni2c(path, env); + bool ret; + + try { + kiwix::Reader reader(cPath); + kiwix::Book book; + book.update(reader); + ret = LIBRARY->addBook(book); + } catch (std::exception& e) { + __android_log_print(ANDROID_LOG_ERROR, "kiwix", "Unable to get ZIM main page"); + __android_log_print(ANDROID_LOG_ERROR, "kiwix", e.what()); + ret = false; + } + return ret; +} diff --git a/src/android/kiwixserver.cpp b/src/android/kiwixserver.cpp new file mode 100644 index 000000000..0e6668b1f --- /dev/null +++ b/src/android/kiwixserver.cpp @@ -0,0 +1,99 @@ +/* + * Copyright (C) 2013 Emmanuel Engelhart + * Copyright (C) 2017 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 +#include +#include "org_kiwix_kiwixlib_JNIKiwixServer.h" + +#include "tools/base64.h" +#include "server.h" +#include "utils.h" + +/* Kiwix Reader JNI functions */ +JNIEXPORT jlong JNICALL Java_org_kiwix_kiwixlib_JNIKiwixServer_getNativeServer( + JNIEnv* env, jobject obj, jobject jLibrary) +{ + __android_log_print(ANDROID_LOG_INFO, "kiwix", "Attempting to create server"); + Lock l; + try { + auto library = Handle::getHandle(env, jLibrary); + kiwix::Server* server = new kiwix::Server(*library); + return reinterpret_cast(new Handle(server)); + } catch (std::exception& e) { + __android_log_print(ANDROID_LOG_WARN, "kiwix", "Error creating the server"); + __android_log_print(ANDROID_LOG_WARN, "kiwix", e.what()); + return 0; + } +} + +JNIEXPORT void JNICALL +Java_org_kiwix_kiwixlib_JNIKiwixServer_dispose(JNIEnv* env, jobject obj) +{ + Handle::dispose(env, obj); +} + +#define SERVER (Handle::getHandle(env, obj)) + +/* Kiwix library functions */ +JNIEXPORT void JNICALL +Java_org_kiwix_kiwixlib_JNIKiwixServer_setRoot(JNIEnv* env, jobject obj, jstring jRoot) +{ + std::string root = jni2c(jRoot, env); + SERVER->setRoot(root); +} + +JNIEXPORT void JNICALL +Java_org_kiwix_kiwixlib_JNIKiwixServer_setAddress(JNIEnv* env, jobject obj, jstring jAddress) +{ + std::string address = jni2c(jAddress, env); + SERVER->setAddress(address); +} + +JNIEXPORT void JNICALL +Java_org_kiwix_kiwixlib_JNIKiwixServer_setPort(JNIEnv* env, jobject obj, int port) +{ + SERVER->setPort(port); +} + +JNIEXPORT void JNICALL +Java_org_kiwix_kiwixlib_JNIKiwixServer_setNbThreads(JNIEnv* env, jobject obj, int threads) +{ + SERVER->setNbThreads(threads); +} + +JNIEXPORT void JNICALL +Java_org_kiwix_kiwixlib_JNIKiwixServer_setTaskbar(JNIEnv* env, jobject obj, jboolean withTaskbar, jboolean withLibraryButton) +{ + SERVER->setTaskbar(withTaskbar, withLibraryButton); +} + +JNIEXPORT jboolean JNICALL +Java_org_kiwix_kiwixlib_JNIKiwixServer_start(JNIEnv* env, jobject obj) +{ + return SERVER->start(); +} + +JNIEXPORT void JNICALL +Java_org_kiwix_kiwixlib_JNIKiwixServer_stop(JNIEnv* env, jobject obj) +{ + SERVER->stop(); +} diff --git a/src/android/meson.build b/src/android/meson.build index e871ad3fa..2755f5d2e 100644 --- a/src/android/meson.build +++ b/src/android/meson.build @@ -2,7 +2,9 @@ kiwix_jni = custom_target('jni', input: ['org/kiwix/kiwixlib/JNIICU.java', 'org/kiwix/kiwixlib/JNIKiwixReader.java', + 'org/kiwix/kiwixlib/JNIKiwixLibrary.java', 'org/kiwix/kiwixlib/JNIKiwixSearcher.java', + 'org/kiwix/kiwixlib/JNIKiwixServer.java', 'org/kiwix/kiwixlib/JNIKiwixInt.java', 'org/kiwix/kiwixlib/JNIKiwixString.java', 'org/kiwix/kiwixlib/JNIKiwixBool.java', @@ -10,6 +12,8 @@ kiwix_jni = custom_target('jni', 'org/kiwix/kiwixlib/Pair.java'], output: ['org_kiwix_kiwixlib_JNIKiwix.h', 'org_kiwix_kiwixlib_JNIKiwixReader.h', + 'org_kiwix_kiwixlib_JNIKiwixLibrary.h', + 'org_kiwix_kiwixlib_JNIKiwixServer.h', 'org_kiwix_kiwixlib_JNIKiwixSearcher.h', 'org_kiwix_kiwixlib_JNIKiwixSearcher_Result.h'], command:['javac', '-d', '@OUTDIR@', '-h', '@OUTDIR@', '@INPUT@'] @@ -18,7 +22,9 @@ kiwix_jni = custom_target('jni', kiwix_sources += [ 'android/kiwixicu.cpp', 'android/kiwixreader.cpp', + 'android/kiwixlibrary.cpp', 'android/kiwixsearcher.cpp', + 'android/kiwixserver.cpp', kiwix_jni] install_subdir('org', install_dir: 'kiwix-lib/java') diff --git a/src/android/org/kiwix/kiwixlib/JNIKiwixLibrary.java b/src/android/org/kiwix/kiwixlib/JNIKiwixLibrary.java new file mode 100644 index 000000000..4a916753b --- /dev/null +++ b/src/android/org/kiwix/kiwixlib/JNIKiwixLibrary.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2013 Emmanuel Engelhart + * Copyright (C) 2017 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. + */ + +package org.kiwix.kiwixlib; + +import org.kiwix.kiwixlib.JNIKiwixException; + +public class JNIKiwixLibrary +{ + public native boolean addBook(String path) throws JNIKiwixException; + + public JNIKiwixLibrary(String filename) + { + nativeHandle = getNativeLibrary(); + } + + public native void dispose(); + + private native long getNativeLibrary(); + private long nativeHandle; +} diff --git a/src/android/org/kiwix/kiwixlib/JNIKiwixServer.java b/src/android/org/kiwix/kiwixlib/JNIKiwixServer.java new file mode 100644 index 000000000..75fd11507 --- /dev/null +++ b/src/android/org/kiwix/kiwixlib/JNIKiwixServer.java @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2019 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. + */ + +package org.kiwix.kiwixlib; + +import org.kiwix.kiwixlib.JNIKiwixException; +import org.kiwix.kiwixlib.JNIKiwixLibrary; + +public class JNIKiwixServer +{ + public native void setRoot(String root); + + public native void setAddress(String address); + + public native void setPort(int port); + + public native void setNbThreads(int nbTreads); + + public native void setTaskbar(boolean withTaskBar, boolean witLibraryButton); + + public native boolean start(); + + public native void stop(); + + public JNIKiwixServer(JNIKiwixLibrary library) + { + nativeHandle = getNativeServer(library); + } + + private native long getNativeServer(JNIKiwixLibrary library); + private long nativeHandle; +}