diff --git a/src/android/kiwixserver.cpp b/src/android/kiwixserver.cpp index 0e6668b1f..9d758f8d2 100644 --- a/src/android/kiwixserver.cpp +++ b/src/android/kiwixserver.cpp @@ -35,8 +35,8 @@ JNIEXPORT jlong JNICALL Java_org_kiwix_kiwixlib_JNIKiwixServer_getNativeServer( __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); + auto library = getPtr(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"); diff --git a/src/android/kiwixlibrary.cpp b/src/android/library.cpp similarity index 52% rename from src/android/kiwixlibrary.cpp rename to src/android/library.cpp index 5b2b6be22..7c2fbf348 100644 --- a/src/android/kiwixlibrary.cpp +++ b/src/android/library.cpp @@ -1,6 +1,5 @@ /* - * Copyright (C) 2013 Emmanuel Engelhart - * Copyright (C) 2017 Matthieu Gautier + * Copyright (C) 2019-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 @@ -21,52 +20,43 @@ #include #include -#include "org_kiwix_kiwixlib_JNIKiwixLibrary.h" +#include "org_kiwix_kiwixlib_Library.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) +JNIEXPORT void JNICALL +Java_org_kiwix_kiwixlib_Library_allocate( + JNIEnv* env, jobject thisObj) { - __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; - } + allocate(env, thisObj); } JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixLibrary_dispose(JNIEnv* env, jobject obj) +Java_org_kiwix_kiwixlib_Library_dispose(JNIEnv* env, jobject thisObj) { - Handle::dispose(env, obj); + dispose(env, thisObj); } -#define LIBRARY (Handle::getHandle(env, obj)) +#define LIBRARY (getPtr(env, thisObj)) /* Kiwix library functions */ JNIEXPORT jboolean JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixLibrary_addBook(JNIEnv* env, jobject obj, jstring path) +Java_org_kiwix_kiwixlib_Library_addBook( + JNIEnv* env, jobject thisObj, jstring path) { - std::string cPath = jni2c(path, env); - bool ret; + auto cPath = jni2c(path, env); try { kiwix::Reader reader(cPath); kiwix::Book book; book.update(reader); - ret = LIBRARY->addBook(book); + return LIBRARY->addBook(book); } catch (std::exception& e) { __android_log_print(ANDROID_LOG_ERROR, "kiwix", "Unable to add the book"); __android_log_print(ANDROID_LOG_ERROR, "kiwix", e.what()); - ret = false; } - return ret; + return false; } diff --git a/src/android/manager.cpp b/src/android/manager.cpp index 54bea06f2..7c2b29f75 100644 --- a/src/android/manager.cpp +++ b/src/android/manager.cpp @@ -31,7 +31,7 @@ JNIEXPORT void JNICALL Java_org_kiwix_kiwixlib_Manager_allocate( JNIEnv* env, jobject thisObj, jobject libraryObj) { - auto lib = Handle::getHandle(env, libraryObj); + auto lib = getPtr(env, libraryObj); allocate(env, thisObj, lib); } diff --git a/src/android/meson.build b/src/android/meson.build index fcb09361b..bb23ab274 100644 --- a/src/android/meson.build +++ b/src/android/meson.build @@ -3,7 +3,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/Library.java', 'org/kiwix/kiwixlib/Manager.java', 'org/kiwix/kiwixlib/JNIKiwixSearcher.java', 'org/kiwix/kiwixlib/JNIKiwixServer.java', @@ -15,7 +15,7 @@ kiwix_jni = custom_target('jni', output: ['org_kiwix_kiwixlib_JNIKiwix.h', 'org_kiwix_kiwixlib_Book.h', 'org_kiwix_kiwixlib_JNIKiwixReader.h', - 'org_kiwix_kiwixlib_JNIKiwixLibrary.h', + 'org_kiwix_kiwixlib_Library.h', 'org_kiwix_kiwixlib_Manager.h', 'org_kiwix_kiwixlib_JNIKiwixServer.h', 'org_kiwix_kiwixlib_JNIKiwixSearcher.h', @@ -27,7 +27,7 @@ kiwix_sources += [ 'android/kiwixicu.cpp', 'android/book.cpp', 'android/kiwixreader.cpp', - 'android/kiwixlibrary.cpp', + 'android/library.cpp', 'android/manager.cpp', 'android/kiwixsearcher.cpp', 'android/kiwixserver.cpp', diff --git a/src/android/org/kiwix/kiwixlib/JNIKiwixServer.java b/src/android/org/kiwix/kiwixlib/JNIKiwixServer.java index 75fd11507..578e2f4de 100644 --- a/src/android/org/kiwix/kiwixlib/JNIKiwixServer.java +++ b/src/android/org/kiwix/kiwixlib/JNIKiwixServer.java @@ -20,7 +20,7 @@ package org.kiwix.kiwixlib; import org.kiwix.kiwixlib.JNIKiwixException; -import org.kiwix.kiwixlib.JNIKiwixLibrary; +import org.kiwix.kiwixlib.Library; public class JNIKiwixServer { @@ -38,11 +38,11 @@ public class JNIKiwixServer public native void stop(); - public JNIKiwixServer(JNIKiwixLibrary library) + public JNIKiwixServer(Library library) { nativeHandle = getNativeServer(library); } - private native long getNativeServer(JNIKiwixLibrary library); + private native long getNativeServer(Library library); private long nativeHandle; } diff --git a/src/android/org/kiwix/kiwixlib/JNIKiwixLibrary.java b/src/android/org/kiwix/kiwixlib/Library.java similarity index 75% rename from src/android/org/kiwix/kiwixlib/JNIKiwixLibrary.java rename to src/android/org/kiwix/kiwixlib/Library.java index 5ab779767..526822567 100644 --- a/src/android/org/kiwix/kiwixlib/JNIKiwixLibrary.java +++ b/src/android/org/kiwix/kiwixlib/Library.java @@ -1,6 +1,5 @@ /* - * Copyright (C) 2013 Emmanuel Engelhart - * Copyright (C) 2017 Matthieu Gautier + * Copyright (C) 2019-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 @@ -22,17 +21,18 @@ package org.kiwix.kiwixlib; import org.kiwix.kiwixlib.JNIKiwixException; -public class JNIKiwixLibrary +public class Library { public native boolean addBook(String path) throws JNIKiwixException; - public JNIKiwixLibrary() + public Library() { - nativeHandle = getNativeLibrary(); + allocate(); } - public native void dispose(); - - private native long getNativeLibrary(); + @Override + protected void finalize() { dispose(); } + private native void allocate(); + private native void dispose(); private long nativeHandle; } diff --git a/src/android/org/kiwix/kiwixlib/Manager.java b/src/android/org/kiwix/kiwixlib/Manager.java index a4a1e2830..2772ca942 100644 --- a/src/android/org/kiwix/kiwixlib/Manager.java +++ b/src/android/org/kiwix/kiwixlib/Manager.java @@ -19,7 +19,7 @@ package org.kiwix.kiwixlib; -import org.kiwix.kiwixlib.JNIKiwixLibrary; +import org.kiwix.kiwixlib.Library; public class Manager { @@ -75,16 +75,16 @@ public class Manager String url, boolean checkMetaData); - public Manager(JNIKiwixLibrary library) { + public Manager(Library library) { allocate(library); _library = library; } - private JNIKiwixLibrary _library; + private Library _library; @Override protected void finalize() { dispose(); } - private native void allocate(JNIKiwixLibrary library); + private native void allocate(Library library); private native void dispose(); private long nativeHandle; }