1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-03 09:49:19 +02:00

StaticObject class

This commit is contained in:
Nikolay Pultsin 2012-03-30 19:50:01 +01:00
parent 8a9d0d4c64
commit 86a07a0d1b
10 changed files with 111 additions and 44 deletions

View file

@ -41,7 +41,7 @@ LOCAL_SRC_FILES := \
NativeFormats/JavaNativeFormatPlugin.cpp \ NativeFormats/JavaNativeFormatPlugin.cpp \
NativeFormats/JavaPluginCollection.cpp \ NativeFormats/JavaPluginCollection.cpp \
NativeFormats/util/AndroidUtil.cpp \ NativeFormats/util/AndroidUtil.cpp \
NativeFormats/util/Method.cpp \ NativeFormats/util/JniEnvelope.cpp \
NativeFormats/zlibrary/core/src/constants/ZLXMLNamespace.cpp \ NativeFormats/zlibrary/core/src/constants/ZLXMLNamespace.cpp \
NativeFormats/zlibrary/core/src/encoding/DummyEncodingConverter.cpp \ NativeFormats/zlibrary/core/src/encoding/DummyEncodingConverter.cpp \
NativeFormats/zlibrary/core/src/encoding/JavaEncodingConverter.cpp \ NativeFormats/zlibrary/core/src/encoding/JavaEncodingConverter.cpp \

View file

@ -18,6 +18,7 @@
*/ */
#include <AndroidUtil.h> #include <AndroidUtil.h>
#include <JniEnvelope.h>
#include <ZLibrary.h> #include <ZLibrary.h>
#include <ZLFile.h> #include <ZLFile.h>
@ -72,7 +73,7 @@ void PluginCollection::deleteInstance() {
PluginCollection::PluginCollection() { PluginCollection::PluginCollection() {
JNIEnv *env = AndroidUtil::getEnv(); JNIEnv *env = AndroidUtil::getEnv();
jclass cls = env->FindClass(AndroidUtil::Class_PluginCollection); jclass cls = env->FindClass(AndroidUtil::Class_PluginCollection);
jobject instance = env->CallStaticObjectMethod(cls, AndroidUtil::SMID_PluginCollection_Instance); jobject instance = AndroidUtil::StaticMethod_PluginCollection_Instance->call(cls);
myJavaInstance = env->NewGlobalRef(instance); myJavaInstance = env->NewGlobalRef(instance);
env->DeleteLocalRef(instance); env->DeleteLocalRef(instance);
env->DeleteLocalRef(cls); env->DeleteLocalRef(cls);

View file

@ -18,6 +18,7 @@
*/ */
#include <AndroidUtil.h> #include <AndroidUtil.h>
#include <JniEnvelope.h>
#include "Library.h" #include "Library.h"
@ -45,7 +46,7 @@ Library::~Library() {
std::string Library::cacheDirectory() const { std::string Library::cacheDirectory() const {
JNIEnv *env = AndroidUtil::getEnv(); JNIEnv *env = AndroidUtil::getEnv();
jstring res = (jstring)env->CallStaticObjectMethod(myPathsClass, AndroidUtil::SMID_Paths_cacheDirectory); jstring res = (jstring)AndroidUtil::StaticMethod_Paths_cacheDirectory->call(myPathsClass);
const char *data = env->GetStringUTFChars(res, 0); const char *data = env->GetStringUTFChars(res, 0);
std::string str(data); std::string str(data);
env->ReleaseStringUTFChars(res, data); env->ReleaseStringUTFChars(res, data);

View file

@ -21,6 +21,7 @@
#include <algorithm> #include <algorithm>
#include <AndroidUtil.h> #include <AndroidUtil.h>
#include <JniEnvelope.h>
#include <ZLStringUtil.h> #include <ZLStringUtil.h>
@ -138,7 +139,7 @@ jobject Tag::javaTag(JNIEnv *env) const {
jobject javaName = env->NewStringUTF(myName.c_str()); jobject javaName = env->NewStringUTF(myName.c_str());
jclass cls = env->FindClass(AndroidUtil::Class_Tag); jclass cls = env->FindClass(AndroidUtil::Class_Tag);
jobject tag = env->CallStaticObjectMethod(cls, AndroidUtil::SMID_Tag_getTag, parentTag, javaName); jobject tag = AndroidUtil::StaticMethod_Tag_getTag->call(cls, parentTag, javaName);
myJavaTag = env->NewGlobalRef(tag); myJavaTag = env->NewGlobalRef(tag);
env->DeleteLocalRef(tag); env->DeleteLocalRef(tag);
env->DeleteLocalRef(cls); env->DeleteLocalRef(cls);

View file

@ -41,27 +41,27 @@ const char * const AndroidUtil::Class_ZLFileImage = "org/geometerplus/zlibrary/c
const char * const AndroidUtil::Class_Book = "org/geometerplus/fbreader/library/Book"; const char * const AndroidUtil::Class_Book = "org/geometerplus/fbreader/library/Book";
const char * const AndroidUtil::Class_Tag = "org/geometerplus/fbreader/library/Tag"; const char * const AndroidUtil::Class_Tag = "org/geometerplus/fbreader/library/Tag";
const char * const AndroidUtil::Class_NativeBookModel = "org/geometerplus/fbreader/bookmodel/NativeBookModel"; const char * const AndroidUtil::Class_NativeBookModel = "org/geometerplus/fbreader/bookmodel/NativeBookModel";
const char * const AndroidUtil::Class_BookReadingException = "org/geometerplus/fbreader/bookmodel/BookReadingException"; //const char * const AndroidUtil::Class_BookReadingException = "org/geometerplus/fbreader/bookmodel/BookReadingException";
shared_ptr<StringMethod> AndroidUtil::Method_java_lang_String_toLowerCase; shared_ptr<StringMethod> AndroidUtil::Method_java_lang_String_toLowerCase;
shared_ptr<StringMethod> AndroidUtil::Method_java_lang_String_toUpperCase; shared_ptr<StringMethod> AndroidUtil::Method_java_lang_String_toUpperCase;
jmethodID AndroidUtil::MID_java_util_Collection_toArray; jmethodID AndroidUtil::MID_java_util_Collection_toArray;
jmethodID AndroidUtil::SMID_java_util_Locale_getDefault; shared_ptr<StaticObjectMethod> AndroidUtil::StaticMethod_java_util_Locale_getDefault;
shared_ptr<StringMethod> AndroidUtil::Method_java_util_Locale_getLanguage; shared_ptr<StringMethod> AndroidUtil::Method_java_util_Locale_getLanguage;
shared_ptr<VoidMethod> AndroidUtil::Method_java_io_InputStream_close; shared_ptr<VoidMethod> AndroidUtil::Method_java_io_InputStream_close;
shared_ptr<IntMethod> AndroidUtil::Method_java_io_InputStream_read; shared_ptr<IntMethod> AndroidUtil::Method_java_io_InputStream_read;
shared_ptr<LongMethod> AndroidUtil::Method_java_io_InputStream_skip; shared_ptr<LongMethod> AndroidUtil::Method_java_io_InputStream_skip;
jmethodID AndroidUtil::SMID_ZLibrary_Instance; shared_ptr<StaticObjectMethod> AndroidUtil::StaticMethod_ZLibrary_Instance;
shared_ptr<StringMethod> AndroidUtil::Method_ZLibrary_getVersionName; shared_ptr<StringMethod> AndroidUtil::Method_ZLibrary_getVersionName;
jmethodID AndroidUtil::MID_NativeFormatPlugin_init; jmethodID AndroidUtil::MID_NativeFormatPlugin_init;
shared_ptr<StringMethod> AndroidUtil::Method_NativeFormatPlugin_supportedFileType; shared_ptr<StringMethod> AndroidUtil::Method_NativeFormatPlugin_supportedFileType;
jmethodID AndroidUtil::SMID_PluginCollection_Instance; shared_ptr<StaticObjectMethod> AndroidUtil::StaticMethod_PluginCollection_Instance;
shared_ptr<ObjectMethod> AndroidUtil::Method_Encoding_createConverter; shared_ptr<ObjectMethod> AndroidUtil::Method_Encoding_createConverter;
@ -69,12 +69,12 @@ jfieldID AndroidUtil::FID_EncodingConverter_Name;
shared_ptr<IntMethod> AndroidUtil::Method_EncodingConverter_convert; shared_ptr<IntMethod> AndroidUtil::Method_EncodingConverter_convert;
shared_ptr<VoidMethod> AndroidUtil::Method_EncodingConverter_reset; shared_ptr<VoidMethod> AndroidUtil::Method_EncodingConverter_reset;
jmethodID AndroidUtil::SMID_JavaEncodingCollection_Instance; shared_ptr<StaticObjectMethod> AndroidUtil::StaticMethod_JavaEncodingCollection_Instance;
shared_ptr<ObjectMethod> AndroidUtil::Method_JavaEncodingCollection_getEncoding_int; shared_ptr<ObjectMethod> AndroidUtil::Method_JavaEncodingCollection_getEncoding_int;
shared_ptr<ObjectMethod> AndroidUtil::Method_JavaEncodingCollection_getEncoding_String; shared_ptr<ObjectMethod> AndroidUtil::Method_JavaEncodingCollection_getEncoding_String;
shared_ptr<BooleanMethod> AndroidUtil::Method_JavaEncodingCollection_providesConverterFor; shared_ptr<BooleanMethod> AndroidUtil::Method_JavaEncodingCollection_providesConverterFor;
jmethodID AndroidUtil::SMID_ZLFile_createFileByPath; shared_ptr<StaticObjectMethod> AndroidUtil::StaticMethod_ZLFile_createFileByPath;
shared_ptr<ObjectMethod> AndroidUtil::Method_ZLFile_children; shared_ptr<ObjectMethod> AndroidUtil::Method_ZLFile_children;
shared_ptr<BooleanMethod> AndroidUtil::Method_ZLFile_exists; shared_ptr<BooleanMethod> AndroidUtil::Method_ZLFile_exists;
shared_ptr<ObjectMethod> AndroidUtil::Method_ZLFile_getInputStream; shared_ptr<ObjectMethod> AndroidUtil::Method_ZLFile_getInputStream;
@ -84,7 +84,7 @@ shared_ptr<LongMethod> AndroidUtil::Method_ZLFile_size;
jmethodID AndroidUtil::MID_ZLFileImage_init; jmethodID AndroidUtil::MID_ZLFileImage_init;
jmethodID AndroidUtil::SMID_Paths_cacheDirectory; shared_ptr<StaticObjectMethod> AndroidUtil::StaticMethod_Paths_cacheDirectory;
jfieldID AndroidUtil::FID_Book_File; jfieldID AndroidUtil::FID_Book_File;
shared_ptr<StringMethod> AndroidUtil::Method_Book_getTitle; shared_ptr<StringMethod> AndroidUtil::Method_Book_getTitle;
@ -98,7 +98,7 @@ shared_ptr<VoidMethod> AndroidUtil::Method_Book_addAuthor;
shared_ptr<VoidMethod> AndroidUtil::Method_Book_addTag; shared_ptr<VoidMethod> AndroidUtil::Method_Book_addTag;
shared_ptr<BooleanMethod> AndroidUtil::Method_Book_save; shared_ptr<BooleanMethod> AndroidUtil::Method_Book_save;
jmethodID AndroidUtil::SMID_Tag_getTag; shared_ptr<StaticObjectMethod> AndroidUtil::StaticMethod_Tag_getTag;
jfieldID AndroidUtil::FID_NativeBookModel_Book; jfieldID AndroidUtil::FID_NativeBookModel_Book;
shared_ptr<VoidMethod> AndroidUtil::Method_NativeBookModel_initInternalHyperlinks; shared_ptr<VoidMethod> AndroidUtil::Method_NativeBookModel_initInternalHyperlinks;
@ -108,7 +108,7 @@ shared_ptr<VoidMethod> AndroidUtil::Method_NativeBookModel_setBookTextModel;
shared_ptr<VoidMethod> AndroidUtil::Method_NativeBookModel_setFootnoteModel; shared_ptr<VoidMethod> AndroidUtil::Method_NativeBookModel_setFootnoteModel;
shared_ptr<VoidMethod> AndroidUtil::Method_NativeBookModel_addImage; shared_ptr<VoidMethod> AndroidUtil::Method_NativeBookModel_addImage;
jmethodID AndroidUtil::SMID_BookReadingException_throwForFile; //shared_ptr<StaticObjectMethod> AndroidUtil::StaticMethod_BookReadingException_throwForFile;
JNIEnv *AndroidUtil::getEnv() { JNIEnv *AndroidUtil::getEnv() {
JNIEnv *env; JNIEnv *env;
@ -135,7 +135,7 @@ bool AndroidUtil::init(JavaVM* jvm) {
env->DeleteLocalRef(cls); env->DeleteLocalRef(cls);
CHECK_NULL( cls = env->FindClass(Class_java_util_Locale) ); CHECK_NULL( cls = env->FindClass(Class_java_util_Locale) );
CHECK_NULL( SMID_java_util_Locale_getDefault = env->GetStaticMethodID(cls, "getDefault", "()Ljava/util/Locale;") ); StaticMethod_java_util_Locale_getDefault = new StaticObjectMethod(env, cls, "getDefault", "java/util/Locale", "()");
Method_java_util_Locale_getLanguage = new StringMethod(env, cls, "getLanguage", "()"); Method_java_util_Locale_getLanguage = new StringMethod(env, cls, "getLanguage", "()");
env->DeleteLocalRef(cls); env->DeleteLocalRef(cls);
@ -146,7 +146,7 @@ bool AndroidUtil::init(JavaVM* jvm) {
env->DeleteLocalRef(cls); env->DeleteLocalRef(cls);
CHECK_NULL( cls = env->FindClass(Class_ZLibrary) ); CHECK_NULL( cls = env->FindClass(Class_ZLibrary) );
CHECK_NULL( SMID_ZLibrary_Instance = env->GetStaticMethodID(cls, "Instance", "()Lorg/geometerplus/zlibrary/core/library/ZLibrary;") ); StaticMethod_ZLibrary_Instance = new StaticObjectMethod(env, cls, "Instance", "org/geometerplus/zlibrary/core/library/ZLibrary", "()");
Method_ZLibrary_getVersionName = new StringMethod(env, cls, "getVersionName", "()"); Method_ZLibrary_getVersionName = new StringMethod(env, cls, "getVersionName", "()");
env->DeleteLocalRef(cls); env->DeleteLocalRef(cls);
@ -156,7 +156,7 @@ bool AndroidUtil::init(JavaVM* jvm) {
env->DeleteLocalRef(cls); env->DeleteLocalRef(cls);
CHECK_NULL( cls = env->FindClass(Class_PluginCollection) ); CHECK_NULL( cls = env->FindClass(Class_PluginCollection) );
CHECK_NULL( SMID_PluginCollection_Instance = env->GetStaticMethodID(cls, "Instance", "()Lorg/geometerplus/fbreader/formats/PluginCollection;") ); StaticMethod_PluginCollection_Instance = new StaticObjectMethod(env, cls, "Instance", "org/geometerplus/fbreader/formats/PluginCollection", "()");
env->DeleteLocalRef(cls); env->DeleteLocalRef(cls);
CHECK_NULL( cls = env->FindClass(Class_Encoding) ); CHECK_NULL( cls = env->FindClass(Class_Encoding) );
@ -170,14 +170,14 @@ bool AndroidUtil::init(JavaVM* jvm) {
env->DeleteLocalRef(cls); env->DeleteLocalRef(cls);
CHECK_NULL( cls = env->FindClass(Class_JavaEncodingCollection) ); CHECK_NULL( cls = env->FindClass(Class_JavaEncodingCollection) );
CHECK_NULL( SMID_JavaEncodingCollection_Instance = env->GetStaticMethodID(cls, "Instance", "()Lorg/geometerplus/zlibrary/core/encodings/JavaEncodingCollection;") ); StaticMethod_JavaEncodingCollection_Instance = new StaticObjectMethod(env, cls, "Instance", "org/geometerplus/zlibrary/core/encodings/JavaEncodingCollection", "()");
Method_JavaEncodingCollection_getEncoding_String = new ObjectMethod(env, cls, "getEncoding", "org/geometerplus/zlibrary/core/encodings/Encoding", "(Ljava/lang/String;)"); Method_JavaEncodingCollection_getEncoding_String = new ObjectMethod(env, cls, "getEncoding", "org/geometerplus/zlibrary/core/encodings/Encoding", "(Ljava/lang/String;)");
Method_JavaEncodingCollection_getEncoding_int = new ObjectMethod(env, cls, "getEncoding", "org/geometerplus/zlibrary/core/encodings/Encoding", "(I)"); Method_JavaEncodingCollection_getEncoding_int = new ObjectMethod(env, cls, "getEncoding", "org/geometerplus/zlibrary/core/encodings/Encoding", "(I)");
Method_JavaEncodingCollection_providesConverterFor = new BooleanMethod(env, cls, "providesConverterFor", "(Ljava/lang/String;)"); Method_JavaEncodingCollection_providesConverterFor = new BooleanMethod(env, cls, "providesConverterFor", "(Ljava/lang/String;)");
env->DeleteLocalRef(cls); env->DeleteLocalRef(cls);
CHECK_NULL( cls = env->FindClass(Class_ZLFile) ); CHECK_NULL( cls = env->FindClass(Class_ZLFile) );
CHECK_NULL( SMID_ZLFile_createFileByPath = env->GetStaticMethodID(cls, "createFileByPath", "(Ljava/lang/String;)Lorg/geometerplus/zlibrary/core/filesystem/ZLFile;") ); StaticMethod_ZLFile_createFileByPath = new StaticObjectMethod(env, cls, "createFileByPath", "org/geometerplus/zlibrary/core/filesystem/ZLFile", "(Ljava/lang/String;)");
Method_ZLFile_children = new ObjectMethod(env, cls, "children", "java/util/List", "()"); Method_ZLFile_children = new ObjectMethod(env, cls, "children", "java/util/List", "()");
Method_ZLFile_exists = new BooleanMethod(env, cls, "exists", "()"); Method_ZLFile_exists = new BooleanMethod(env, cls, "exists", "()");
Method_ZLFile_isDirectory = new BooleanMethod(env, cls, "isDirectory", "()"); Method_ZLFile_isDirectory = new BooleanMethod(env, cls, "isDirectory", "()");
@ -191,7 +191,7 @@ bool AndroidUtil::init(JavaVM* jvm) {
env->DeleteLocalRef(cls); env->DeleteLocalRef(cls);
CHECK_NULL( cls = env->FindClass(Class_Paths) ); CHECK_NULL( cls = env->FindClass(Class_Paths) );
CHECK_NULL( SMID_Paths_cacheDirectory = env->GetStaticMethodID(cls, "cacheDirectory", "()Ljava/lang/String;") ); StaticMethod_Paths_cacheDirectory = new StaticObjectMethod(env, cls, "cacheDirectory", "java/lang/String", "()");
env->DeleteLocalRef(cls); env->DeleteLocalRef(cls);
CHECK_NULL( cls = env->FindClass(Class_Book) ); CHECK_NULL( cls = env->FindClass(Class_Book) );
@ -209,7 +209,7 @@ bool AndroidUtil::init(JavaVM* jvm) {
env->DeleteLocalRef(cls); env->DeleteLocalRef(cls);
CHECK_NULL( cls = env->FindClass(Class_Tag) ); CHECK_NULL( cls = env->FindClass(Class_Tag) );
CHECK_NULL( SMID_Tag_getTag = env->GetStaticMethodID(cls, "getTag", "(Lorg/geometerplus/fbreader/library/Tag;Ljava/lang/String;)Lorg/geometerplus/fbreader/library/Tag;") ); StaticMethod_Tag_getTag = new StaticObjectMethod(env, cls, "getTag", "org/geometerplus/fbreader/library/Tag", "(Lorg/geometerplus/fbreader/library/Tag;Ljava/lang/String;)");
env->DeleteLocalRef(cls); env->DeleteLocalRef(cls);
CHECK_NULL( cls = env->FindClass(Class_NativeBookModel) ); CHECK_NULL( cls = env->FindClass(Class_NativeBookModel) );
@ -222,9 +222,11 @@ bool AndroidUtil::init(JavaVM* jvm) {
Method_NativeBookModel_addImage = new VoidMethod(env, cls, "addImage", "(Ljava/lang/String;Lorg/geometerplus/zlibrary/core/image/ZLImage;)"); Method_NativeBookModel_addImage = new VoidMethod(env, cls, "addImage", "(Ljava/lang/String;Lorg/geometerplus/zlibrary/core/image/ZLImage;)");
env->DeleteLocalRef(cls); env->DeleteLocalRef(cls);
/*
CHECK_NULL( cls = env->FindClass(Class_BookReadingException) ); CHECK_NULL( cls = env->FindClass(Class_BookReadingException) );
CHECK_NULL( SMID_BookReadingException_throwForFile = env->GetStaticMethodID(cls, "throwForFile", "(Ljava/lang/String;Lorg/geometerplus/zlibrary/core/filesystem/ZLFile;)V") ); CHECK_NULL( StaticMethod_BookReadingException_throwForFile = env->GetStaticMethodID(cls, "throwForFile", "(Ljava/lang/String;Lorg/geometerplus/zlibrary/core/filesystem/ZLFile;)V") );
env->DeleteLocalRef(cls); env->DeleteLocalRef(cls);
*/
return true; return true;
} }
@ -232,7 +234,7 @@ bool AndroidUtil::init(JavaVM* jvm) {
jobject AndroidUtil::createJavaFile(JNIEnv *env, const std::string &path) { jobject AndroidUtil::createJavaFile(JNIEnv *env, const std::string &path) {
jstring javaPath = env->NewStringUTF(path.c_str()); jstring javaPath = env->NewStringUTF(path.c_str());
jclass cls = env->FindClass(Class_ZLFile); jclass cls = env->FindClass(Class_ZLFile);
jobject javaFile = env->CallStaticObjectMethod(cls, SMID_ZLFile_createFileByPath, javaPath); jobject javaFile = StaticMethod_ZLFile_createFileByPath->call(cls, javaPath);
env->DeleteLocalRef(cls); env->DeleteLocalRef(cls);
env->DeleteLocalRef(javaPath); env->DeleteLocalRef(javaPath);
return javaFile; return javaFile;
@ -331,15 +333,17 @@ void AndroidUtil::throwRuntimeException(JNIEnv *env, const std::string &message)
env->ThrowNew(cls, message.c_str()); env->ThrowNew(cls, message.c_str());
} }
/*
void AndroidUtil::throwBookReadingException(const std::string &resourceId, const ZLFile &file) { void AndroidUtil::throwBookReadingException(const std::string &resourceId, const ZLFile &file) {
JNIEnv *env = getEnv(); JNIEnv *env = getEnv();
jclass cls = env->FindClass("org/geometerplus/fbreader/bookmodel/BookReadingException"); jclass cls = env->FindClass("org/geometerplus/fbreader/bookmodel/BookReadingException");
env->CallStaticVoidMethod( env->CallStaticVoidMethod(
cls, cls,
SMID_BookReadingException_throwForFile, StaticMethod_BookReadingException_throwForFile,
AndroidUtil::createJavaString(env, resourceId), AndroidUtil::createJavaString(env, resourceId),
AndroidUtil::createJavaFile(env, file.path()) AndroidUtil::createJavaFile(env, file.path())
); );
// TODO: possible memory leak // TODO: possible memory leak
// TODO: clear cls & ZLFile object references // TODO: clear cls & ZLFile object references
} }
*/

View file

@ -33,6 +33,7 @@ class LongMethod;
class BooleanMethod; class BooleanMethod;
class StringMethod; class StringMethod;
class ObjectMethod; class ObjectMethod;
class StaticObjectMethod;
class ZLFile; class ZLFile;
class ZLFileImage; class ZLFileImage;
@ -59,24 +60,24 @@ public:
static const char * const Class_Book; static const char * const Class_Book;
static const char * const Class_Tag; static const char * const Class_Tag;
static const char * const Class_NativeBookModel; static const char * const Class_NativeBookModel;
static const char * const Class_BookReadingException; //static const char * const Class_BookReadingException;
static shared_ptr<StringMethod> Method_java_lang_String_toLowerCase; static shared_ptr<StringMethod> Method_java_lang_String_toLowerCase;
static shared_ptr<StringMethod> Method_java_lang_String_toUpperCase; static shared_ptr<StringMethod> Method_java_lang_String_toUpperCase;
static jmethodID MID_java_util_Collection_toArray; static jmethodID MID_java_util_Collection_toArray;
static jmethodID SMID_java_util_Locale_getDefault; static shared_ptr<StaticObjectMethod> StaticMethod_java_util_Locale_getDefault;
static shared_ptr<StringMethod> Method_java_util_Locale_getLanguage; static shared_ptr<StringMethod> Method_java_util_Locale_getLanguage;
static shared_ptr<VoidMethod> Method_java_io_InputStream_close; static shared_ptr<VoidMethod> Method_java_io_InputStream_close;
static shared_ptr<IntMethod> Method_java_io_InputStream_read; static shared_ptr<IntMethod> Method_java_io_InputStream_read;
static shared_ptr<LongMethod> Method_java_io_InputStream_skip; static shared_ptr<LongMethod> Method_java_io_InputStream_skip;
static jmethodID SMID_ZLibrary_Instance; static shared_ptr<StaticObjectMethod> StaticMethod_ZLibrary_Instance;
static shared_ptr<StringMethod> Method_ZLibrary_getVersionName; static shared_ptr<StringMethod> Method_ZLibrary_getVersionName;
static jmethodID SMID_ZLFile_createFileByPath; static shared_ptr<StaticObjectMethod> StaticMethod_ZLFile_createFileByPath;
static shared_ptr<ObjectMethod> Method_ZLFile_children; static shared_ptr<ObjectMethod> Method_ZLFile_children;
static shared_ptr<BooleanMethod> Method_ZLFile_exists; static shared_ptr<BooleanMethod> Method_ZLFile_exists;
static shared_ptr<ObjectMethod> Method_ZLFile_getInputStream; static shared_ptr<ObjectMethod> Method_ZLFile_getInputStream;
@ -89,7 +90,7 @@ public:
static jmethodID MID_NativeFormatPlugin_init; static jmethodID MID_NativeFormatPlugin_init;
static shared_ptr<StringMethod> Method_NativeFormatPlugin_supportedFileType; static shared_ptr<StringMethod> Method_NativeFormatPlugin_supportedFileType;
static jmethodID SMID_PluginCollection_Instance; static shared_ptr<StaticObjectMethod> StaticMethod_PluginCollection_Instance;
static shared_ptr<ObjectMethod> Method_Encoding_createConverter; static shared_ptr<ObjectMethod> Method_Encoding_createConverter;
@ -97,12 +98,12 @@ public:
static shared_ptr<IntMethod> Method_EncodingConverter_convert; static shared_ptr<IntMethod> Method_EncodingConverter_convert;
static shared_ptr<VoidMethod> Method_EncodingConverter_reset; static shared_ptr<VoidMethod> Method_EncodingConverter_reset;
static jmethodID SMID_JavaEncodingCollection_Instance; static shared_ptr<StaticObjectMethod> StaticMethod_JavaEncodingCollection_Instance;
static shared_ptr<ObjectMethod> Method_JavaEncodingCollection_getEncoding_String; static shared_ptr<ObjectMethod> Method_JavaEncodingCollection_getEncoding_String;
static shared_ptr<ObjectMethod> Method_JavaEncodingCollection_getEncoding_int; static shared_ptr<ObjectMethod> Method_JavaEncodingCollection_getEncoding_int;
static shared_ptr<BooleanMethod> Method_JavaEncodingCollection_providesConverterFor; static shared_ptr<BooleanMethod> Method_JavaEncodingCollection_providesConverterFor;
static jmethodID SMID_Paths_cacheDirectory; static shared_ptr<StaticObjectMethod> StaticMethod_Paths_cacheDirectory;
static jfieldID FID_Book_File; static jfieldID FID_Book_File;
static shared_ptr<StringMethod> Method_Book_getTitle; static shared_ptr<StringMethod> Method_Book_getTitle;
@ -116,7 +117,7 @@ public:
static shared_ptr<VoidMethod> Method_Book_addTag; static shared_ptr<VoidMethod> Method_Book_addTag;
static shared_ptr<BooleanMethod> Method_Book_save; static shared_ptr<BooleanMethod> Method_Book_save;
static jmethodID SMID_Tag_getTag; static shared_ptr<StaticObjectMethod> StaticMethod_Tag_getTag;
static jfieldID FID_NativeBookModel_Book; static jfieldID FID_NativeBookModel_Book;
static shared_ptr<VoidMethod> Method_NativeBookModel_initInternalHyperlinks; static shared_ptr<VoidMethod> Method_NativeBookModel_initInternalHyperlinks;
@ -126,7 +127,7 @@ public:
static shared_ptr<VoidMethod> Method_NativeBookModel_setFootnoteModel; static shared_ptr<VoidMethod> Method_NativeBookModel_setFootnoteModel;
static shared_ptr<VoidMethod> Method_NativeBookModel_addImage; static shared_ptr<VoidMethod> Method_NativeBookModel_addImage;
static jmethodID SMID_BookReadingException_throwForFile; //static shared_ptr<StaticObjectMethod> StaticMethod_BookReadingException_throwForFile;
public: public:
static bool init(JavaVM* jvm); static bool init(JavaVM* jvm);
@ -144,7 +145,7 @@ public:
static jobjectArray createJavaStringArray(JNIEnv *env, const std::vector<std::string> &data); static jobjectArray createJavaStringArray(JNIEnv *env, const std::vector<std::string> &data);
static void throwRuntimeException(JNIEnv *env, const std::string &message); static void throwRuntimeException(JNIEnv *env, const std::string &message);
static void throwBookReadingException(const std::string &resourceId, const ZLFile &file); //static void throwBookReadingException(const std::string &resourceId, const ZLFile &file);
}; };
#endif /* __ANDROIDUTIL_H__ */ #endif /* __ANDROIDUTIL_H__ */

View file

@ -17,9 +17,14 @@
* 02110-1301, USA. * 02110-1301, USA.
*/ */
#include <ZLLogger.h>
#include "JniEnvelope.h" #include "JniEnvelope.h"
Method::Method(JNIEnv *env, jclass cls, const std::string &name, const std::string &signature) { static const std::string JNI_LOGGER_CLASS = "JniLog";
Method::Method(JNIEnv *env, jclass cls, const std::string &name, const std::string &signature) : myName(name) {
//ZLLogger::Instance().registerClass(JNI_LOGGER_CLASS);
myEnv = env; myEnv = env;
myId = env->GetMethodID(cls, name.c_str(), signature.c_str()); myId = env->GetMethodID(cls, name.c_str(), signature.c_str());
} }
@ -27,24 +32,37 @@ Method::Method(JNIEnv *env, jclass cls, const std::string &name, const std::stri
Method::~Method() { Method::~Method() {
} }
StaticMethod::StaticMethod(JNIEnv *env, jclass cls, const std::string &name, const std::string &signature) : myName(name) {
//ZLLogger::Instance().registerClass(JNI_LOGGER_CLASS);
myEnv = env;
myId = env->GetStaticMethodID(cls, name.c_str(), signature.c_str());
}
StaticMethod::~StaticMethod() {
}
VoidMethod::VoidMethod(JNIEnv *env, jclass cls, const std::string &name, const std::string &signature) : Method(env, cls, name, signature + "V") { VoidMethod::VoidMethod(JNIEnv *env, jclass cls, const std::string &name, const std::string &signature) : Method(env, cls, name, signature + "V") {
} }
void VoidMethod::call(jobject base, ...) { void VoidMethod::call(jobject base, ...) {
ZLLogger::Instance().println(JNI_LOGGER_CLASS, "calling VoidMethod " + myName);
va_list lst; va_list lst;
va_start(lst, base); va_start(lst, base);
myEnv->CallVoidMethod(base, myId, lst); myEnv->CallVoidMethodV(base, myId, lst);
va_end(lst); va_end(lst);
ZLLogger::Instance().println(JNI_LOGGER_CLASS, "finished VoidMethod " + myName);
} }
IntMethod::IntMethod(JNIEnv *env, jclass cls, const std::string &name, const std::string &signature) : Method(env, cls, name, signature + "I") { IntMethod::IntMethod(JNIEnv *env, jclass cls, const std::string &name, const std::string &signature) : Method(env, cls, name, signature + "I") {
} }
jint IntMethod::call(jobject base, ...) { jint IntMethod::call(jobject base, ...) {
ZLLogger::Instance().println(JNI_LOGGER_CLASS, "calling IntMethod " + myName);
va_list lst; va_list lst;
va_start(lst, base); va_start(lst, base);
jint result = myEnv->CallIntMethod(base, myId, lst); jint result = myEnv->CallIntMethodV(base, myId, lst);
va_end(lst); va_end(lst);
ZLLogger::Instance().println(JNI_LOGGER_CLASS, "finished IntMethod " + myName);
return result; return result;
} }
@ -52,10 +70,12 @@ LongMethod::LongMethod(JNIEnv *env, jclass cls, const std::string &name, const s
} }
jlong LongMethod::call(jobject base, ...) { jlong LongMethod::call(jobject base, ...) {
ZLLogger::Instance().println(JNI_LOGGER_CLASS, "calling LongMethod " + myName);
va_list lst; va_list lst;
va_start(lst, base); va_start(lst, base);
jlong result = myEnv->CallLongMethod(base, myId, lst); jlong result = myEnv->CallLongMethodV(base, myId, lst);
va_end(lst); va_end(lst);
ZLLogger::Instance().println(JNI_LOGGER_CLASS, "finished LongMethod " + myName);
return result; return result;
} }
@ -63,10 +83,12 @@ BooleanMethod::BooleanMethod(JNIEnv *env, jclass cls, const std::string &name, c
} }
jboolean BooleanMethod::call(jobject base, ...) { jboolean BooleanMethod::call(jobject base, ...) {
ZLLogger::Instance().println(JNI_LOGGER_CLASS, "calling BooleanMethod " + myName);
va_list lst; va_list lst;
va_start(lst, base); va_start(lst, base);
jboolean result = myEnv->CallBooleanMethod(base, myId, lst); jboolean result = myEnv->CallBooleanMethodV(base, myId, lst);
va_end(lst); va_end(lst);
ZLLogger::Instance().println(JNI_LOGGER_CLASS, "finished BooleanMethod " + myName);
return result; return result;
} }
@ -74,10 +96,12 @@ StringMethod::StringMethod(JNIEnv *env, jclass cls, const std::string &name, con
} }
jstring StringMethod::call(jobject base, ...) { jstring StringMethod::call(jobject base, ...) {
ZLLogger::Instance().println(JNI_LOGGER_CLASS, "calling StringMethod " + myName);
va_list lst; va_list lst;
va_start(lst, base); va_start(lst, base);
jstring result = (jstring)myEnv->CallObjectMethod(base, myId, lst); jstring result = (jstring)myEnv->CallObjectMethodV(base, myId, lst);
va_end(lst); va_end(lst);
ZLLogger::Instance().println(JNI_LOGGER_CLASS, "finished StringMethod " + myName);
return result; return result;
} }
@ -85,9 +109,24 @@ ObjectMethod::ObjectMethod(JNIEnv *env, jclass cls, const std::string &name, con
} }
jobject ObjectMethod::call(jobject base, ...) { jobject ObjectMethod::call(jobject base, ...) {
ZLLogger::Instance().println(JNI_LOGGER_CLASS, "calling ObjectMethod " + myName);
va_list lst; va_list lst;
va_start(lst, base); va_start(lst, base);
jobject result = myEnv->CallObjectMethod(base, myId, lst); jobject result = myEnv->CallObjectMethodV(base, myId, lst);
va_end(lst); va_end(lst);
ZLLogger::Instance().println(JNI_LOGGER_CLASS, "finished ObjectMethod " + myName);
return result;
}
StaticObjectMethod::StaticObjectMethod(JNIEnv *env, jclass cls, const std::string &name, const std::string &returnType, const std::string &signature) : StaticMethod(env, cls, name, signature + "L" + returnType + ";") {
}
jobject StaticObjectMethod::call(jclass cls, ...) {
ZLLogger::Instance().println(JNI_LOGGER_CLASS, "calling StaticObjectMethod " + myName);
va_list lst;
va_start(lst, cls);
jobject result = myEnv->CallStaticObjectMethodV(cls, myId, lst);
va_end(lst);
ZLLogger::Instance().println(JNI_LOGGER_CLASS, "finished StaticObjectMethod " + myName);
return result; return result;
} }

View file

@ -31,6 +31,19 @@ public:
virtual ~Method(); virtual ~Method();
protected: protected:
const std::string myName;
JNIEnv *myEnv;
jmethodID myId;
};
class StaticMethod {
public:
StaticMethod(JNIEnv *env, jclass cls, const std::string &name, const std::string &signature);
virtual ~StaticMethod();
protected:
const std::string myName;
JNIEnv *myEnv; JNIEnv *myEnv;
jmethodID myId; jmethodID myId;
}; };
@ -77,4 +90,11 @@ public:
jobject call(jobject base, ...); jobject call(jobject base, ...);
}; };
class StaticObjectMethod : public StaticMethod {
public:
StaticObjectMethod(JNIEnv *env, jclass cls, const std::string &name, const std::string &returnType, const std::string &signature);
jobject call(jclass cls, ...);
};
#endif /* __JNIENVELOPE_H__ */ #endif /* __JNIENVELOPE_H__ */

View file

@ -50,7 +50,7 @@ bool JavaEncodingConverterProvider::providesConverter(const std::string &encodin
} }
JNIEnv *env = AndroidUtil::getEnv(); JNIEnv *env = AndroidUtil::getEnv();
jclass cls = env->FindClass(AndroidUtil::Class_JavaEncodingCollection); jclass cls = env->FindClass(AndroidUtil::Class_JavaEncodingCollection);
jobject collection = env->CallStaticObjectMethod(cls, AndroidUtil::SMID_JavaEncodingCollection_Instance); jobject collection = AndroidUtil::StaticMethod_JavaEncodingCollection_Instance->call(cls);
jstring encodingName = AndroidUtil::createJavaString(env, encoding); jstring encodingName = AndroidUtil::createJavaString(env, encoding);
jboolean result = AndroidUtil::Method_JavaEncodingCollection_providesConverterFor->call(collection, encodingName); jboolean result = AndroidUtil::Method_JavaEncodingCollection_providesConverterFor->call(collection, encodingName);
env->DeleteLocalRef(encodingName); env->DeleteLocalRef(encodingName);
@ -66,7 +66,7 @@ shared_ptr<ZLEncodingConverter> JavaEncodingConverterProvider::createConverter(c
JavaEncodingConverter::JavaEncodingConverter(const std::string &encoding) { JavaEncodingConverter::JavaEncodingConverter(const std::string &encoding) {
JNIEnv *env = AndroidUtil::getEnv(); JNIEnv *env = AndroidUtil::getEnv();
jclass cls = env->FindClass(AndroidUtil::Class_JavaEncodingCollection); jclass cls = env->FindClass(AndroidUtil::Class_JavaEncodingCollection);
jobject collection = env->CallStaticObjectMethod(cls, AndroidUtil::SMID_JavaEncodingCollection_Instance); jobject collection = AndroidUtil::StaticMethod_JavaEncodingCollection_Instance->call(cls);
jstring encodingName = AndroidUtil::createJavaString(env, encoding); jstring encodingName = AndroidUtil::createJavaString(env, encoding);
jobject javaEncoding = AndroidUtil::Method_JavaEncodingCollection_getEncoding_String->call(collection, encodingName); jobject javaEncoding = AndroidUtil::Method_JavaEncodingCollection_getEncoding_String->call(collection, encodingName);
myJavaConverter = AndroidUtil::Method_Encoding_createConverter->call(javaEncoding); myJavaConverter = AndroidUtil::Method_Encoding_createConverter->call(javaEncoding);

View file

@ -45,7 +45,7 @@ void ZLAndroidLibraryImplementation::init(int &argc, char **&argv) {
std::string ZLibrary::Language() { std::string ZLibrary::Language() {
JNIEnv *env = AndroidUtil::getEnv(); JNIEnv *env = AndroidUtil::getEnv();
jclass cls = env->FindClass(AndroidUtil::Class_java_util_Locale); jclass cls = env->FindClass(AndroidUtil::Class_java_util_Locale);
jobject locale = env->CallStaticObjectMethod(cls, AndroidUtil::SMID_java_util_Locale_getDefault); jobject locale = AndroidUtil::StaticMethod_java_util_Locale_getDefault->call(cls);
jstring javaLang = (jstring)AndroidUtil::Method_java_util_Locale_getLanguage->call(locale); jstring javaLang = (jstring)AndroidUtil::Method_java_util_Locale_getLanguage->call(locale);
const char *langData = env->GetStringUTFChars(javaLang, 0); const char *langData = env->GetStringUTFChars(javaLang, 0);
std::string lang(langData); std::string lang(langData);
@ -59,7 +59,7 @@ std::string ZLibrary::Language() {
std::string ZLibrary::Version() { std::string ZLibrary::Version() {
JNIEnv *env = AndroidUtil::getEnv(); JNIEnv *env = AndroidUtil::getEnv();
jclass cls = env->FindClass(AndroidUtil::Class_ZLibrary); jclass cls = env->FindClass(AndroidUtil::Class_ZLibrary);
jobject zlibrary = env->CallStaticObjectMethod(cls, AndroidUtil::SMID_ZLibrary_Instance); jobject zlibrary = AndroidUtil::StaticMethod_ZLibrary_Instance->call(cls);
jstring javaVersion = (jstring)AndroidUtil::Method_ZLibrary_getVersionName->call(zlibrary); jstring javaVersion = (jstring)AndroidUtil::Method_ZLibrary_getVersionName->call(zlibrary);
const char *versionData = env->GetStringUTFChars(javaVersion, 0); const char *versionData = env->GetStringUTFChars(javaVersion, 0);
std::string version(versionData); std::string version(versionData);