Skip to content

Commit

Permalink
Merge pull request #308 from virxkane/cr3.2.58-fixes
Browse files Browse the repository at this point in the history
Fixes for 3.2.58
  • Loading branch information
buggins committed Aug 31, 2021
2 parents bec2bec + d0d36f7 commit c4a9f55
Show file tree
Hide file tree
Showing 24 changed files with 466 additions and 168 deletions.
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.0.0'
classpath 'com.android.tools.build:gradle:7.0.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
Expand Down
29 changes: 28 additions & 1 deletion android/jni/cr3engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,32 @@ JNIEXPORT jbyteArray JNICALL Java_org_coolreader_crengine_Engine_scanBookCoverIn
return res;
}

/*
* Class: org_coolreader_crengine_Engine
* Method: isArchiveInternal
* Signature: (Ljava/lang/String;)Z
*/
JNIEXPORT jboolean JNICALL Java_org_coolreader_crengine_Engine_isArchiveInternal
(JNIEnv * _env, jclass, jstring jarcName)
{
jboolean res = JNI_FALSE;
CRJNIEnv env(_env);
lString32 arcName = env.fromJavaString(jarcName);
LVStreamRef stream = LVOpenFileStream(arcName.c_str(), LVOM_READ);
if (!stream.isNull()) {
LVContainerRef arc = LVOpenArchieve(stream);
if (!arc.isNull()) {
if (!DetectEpubFormat(stream) &&
!DetectFb3Format(stream) &&
!DetectDocXFormat(stream) &&
!DetectOpenDocumentFormat(stream)) {
res = JNI_TRUE;
}
}
}
return res;
}

/*
* Class: org_coolreader_crengine_Engine
* Method: getArchiveItemsInternal
Expand Down Expand Up @@ -1179,6 +1205,7 @@ static JNINativeMethod sEngineMethods[] = {
{"setCacheDirectoryInternal", "(Ljava/lang/String;I)Z", (void*)Java_org_coolreader_crengine_Engine_setCacheDirectoryInternal},
{"scanBookPropertiesInternal", "(Lorg/coolreader/crengine/FileInfo;)Z", (void*)Java_org_coolreader_crengine_Engine_scanBookPropertiesInternal},
{"updateFileCRC32Internal", "(Lorg/coolreader/crengine/FileInfo;)Z", (void*)Java_org_coolreader_crengine_Engine_updateFileCRC32Internal},
{"isArchiveInternal", "(Ljava/lang/String;)Z", (void*)Java_org_coolreader_crengine_Engine_isArchiveInternal},
{"getArchiveItemsInternal", "(Ljava/lang/String;)[Ljava/lang/String;", (void*)Java_org_coolreader_crengine_Engine_getArchiveItemsInternal},
{"isLink", "(Ljava/lang/String;)Ljava/lang/String;", (void*)Java_org_coolreader_crengine_Engine_isLink},
{"suspendLongOperationInternal", "()V", (void*)Java_org_coolreader_crengine_Engine_suspendLongOperationInternal},
Expand Down Expand Up @@ -1208,7 +1235,7 @@ static JNINativeMethod sDocViewMethods[] = {
{"getCurrentPageBookmarkInternal", "()Lorg/coolreader/crengine/Bookmark;", (void*)Java_org_coolreader_crengine_DocView_getCurrentPageBookmarkInternal},
{"goToPositionInternal", "(Ljava/lang/String;Z)Z", (void*)Java_org_coolreader_crengine_DocView_goToPositionInternal},
{"getPositionPropsInternal", "(Ljava/lang/String;Z)Lorg/coolreader/crengine/PositionProperties;", (void*)Java_org_coolreader_crengine_DocView_getPositionPropsInternal},
{"updateBookInfoInternal", "(Lorg/coolreader/crengine/BookInfo;)V", (void*)Java_org_coolreader_crengine_DocView_updateBookInfoInternal},
{"updateBookInfoInternal", "(Lorg/coolreader/crengine/BookInfo;Z)V", (void*)Java_org_coolreader_crengine_DocView_updateBookInfoInternal},
{"getTOCInternal", "()Lorg/coolreader/crengine/TOCItem;", (void*)Java_org_coolreader_crengine_DocView_getTOCInternal},
{"clearSelectionInternal", "()V", (void*)Java_org_coolreader_crengine_DocView_clearSelectionInternal},
{"findTextInternal", "(Ljava/lang/String;III)Z", (void*)Java_org_coolreader_crengine_DocView_findTextInternal},
Expand Down
20 changes: 20 additions & 0 deletions android/jni/cr3java.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,26 @@ class CRLongField : public CRFieldAccessor {
void set(lInt64 v) { objacc->SetLongField(objacc.getObject(), fieldid, v); }
};

class CRBooleanField : public CRFieldAccessor {
public:
CRBooleanField( CRObjectAccessor & acc, const char * fieldName )
: CRFieldAccessor( acc, fieldName, "Z" )
{
}
bool get() { return objacc->GetBooleanField(objacc.getObject(), fieldid); }
void set(bool v) { objacc->SetBooleanField(objacc.getObject(), fieldid, v); }
};

class CRObjectField : public CRFieldAccessor {
public:
CRObjectField( CRObjectAccessor & acc, const char * fieldName, const char* signature )
: CRFieldAccessor( acc, fieldName, signature )
{
}
jobject get() { return objacc->GetObjectField(objacc.getObject(), fieldid); }
void set(jobject v) { objacc->SetObjectField(objacc.getObject(), fieldid, v); }
};

class BitmapAccessor : public CRJNIEnv {
private:
jobject bitmap;
Expand Down
51 changes: 44 additions & 7 deletions android/jni/docview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -877,12 +877,12 @@ static DocViewNative * getNative(JNIEnv * env, jobject _this)
return res;
}

void DocViewNative::createDefaultDocument( lString32 title, lString32 message )
void DocViewNative::createDefaultDocument( const lString32& title, const lString32& message )
{
_docview->createDefaultDocument(title, message);
}

bool DocViewNative::loadDocument( lString32 filename )
bool DocViewNative::loadDocument( const lString32& filename )
{
CRLog::info("Loading document %s", LCSTR(filename));
bool res = _docview->LoadDocument(filename.c_str());
Expand All @@ -901,7 +901,7 @@ bool DocViewNative::loadDocument( lString32 filename )
return res;
}

bool DocViewNative::loadDocument( LVStreamRef stream, lString32 contentPath )
bool DocViewNative::loadDocument( LVStreamRef stream, const lString32& contentPath )
{
CRLog::info("Loading document from memory stream, content path: %s", LCSTR(contentPath));
bool res = _docview->LoadDocument(stream, contentPath.c_str(), false);
Expand Down Expand Up @@ -1549,8 +1549,8 @@ JNIEXPORT jboolean JNICALL Java_org_coolreader_crengine_DocView_loadDocumentFrom
}
DocViewCallback callback( _env, p->_docview, _this );
LVStreamRef stream = env.jbyteArrayToStream(buf);
lString32 contentPath16 = env.fromJavaString(contentPath);
bool res = p->loadDocument(stream, contentPath16);
lString32 contentPath32 = env.fromJavaString(contentPath);
bool res = p->loadDocument(stream, contentPath32);
return res ? JNI_TRUE : JNI_FALSE;
}

Expand Down Expand Up @@ -1790,10 +1790,10 @@ JNIEXPORT jobject JNICALL Java_org_coolreader_crengine_DocView_getCurrentPageBoo
/*
* Class: org_coolreader_crengine_DocView
* Method: updateBookInfoInternal
* Signature: (Lorg/coolreader/crengine/BookInfo;)V
* Signature: (Lorg/coolreader/crengine/BookInfo;Z)V
*/
JNIEXPORT void JNICALL Java_org_coolreader_crengine_DocView_updateBookInfoInternal
(JNIEnv * _env, jobject _this, jobject _info)
(JNIEnv * _env, jobject _this, jobject _info, jboolean _updatePath)
{
CRJNIEnv env(_env);
DocViewNative * p = getNative(_env, _this);
Expand Down Expand Up @@ -1838,6 +1838,43 @@ JNIEXPORT void JNICALL Java_org_coolreader_crengine_DocView_updateBookInfoIntern
descriptionField.set(p->_docview->getDescription());
CRLongField crc32Field(fileinfo,"crc32");
crc32Field.set((lInt64)p->_docview->getFileCRC32());
jclass clazz_docformat = env->FindClass("org/coolreader/crengine/DocumentFormat");
jmethodID methodId_byId = NULL;
if (NULL != clazz_docformat) {
methodId_byId = env->GetStaticMethodID(clazz_docformat, "byId", "(I)Lorg/coolreader/crengine/DocumentFormat;");
}
if (NULL != methodId_byId) {
jobject jformat = env->CallStaticObjectMethod(clazz_docformat, methodId_byId, p->_docview->getDocFormat());
CRObjectField formatField(fileinfo, "format", "Lorg/coolreader/crengine/DocumentFormat;");
formatField.set(jformat);
}
if (_updatePath) {
CRPropRef doc_props = p->_docview->getDocProps();
if (!doc_props.isNull()) {
bool isArchive = !doc_props->getStringDef(DOC_PROP_ARC_NAME, "").empty();
if (isArchive) {
lString32 arcname = doc_props->getStringDef(DOC_PROP_ARC_NAME, "");
int arcsize = doc_props->getIntDef(DOC_PROP_ARC_SIZE, 0);
CRStringField arcnameField(fileinfo, "arcname");
arcnameField.set(arcname);
CRLongField arcsizeField(fileinfo, "arcsize");
arcsizeField.set(arcsize);
}
lString32 pathname = doc_props->getStringDef(DOC_PROP_FILE_NAME, "");
lString32 filename = LVExtractFilename(pathname);
int size = doc_props->getIntDef(DOC_PROP_FILE_SIZE, 0);
CRBooleanField isArchiveField(fileinfo, "isArchive");
isArchiveField.set(isArchive);
CRStringField pathnameField(fileinfo, "pathname");
CRStringField pathField(fileinfo, "path");
pathnameField.set(pathname);
pathField.set(pathname);
CRStringField filenameField(fileinfo, "filename");
filenameField.set(filename);
CRLongField sizeField(fileinfo, "size");
sizeField.set(size);
}
}
}

/*
Expand Down
6 changes: 3 additions & 3 deletions android/jni/docview.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ class DocViewNative {
bool closeBook();
bool loadHistory( lString32 filename );
bool saveHistory( lString32 filename );
void createDefaultDocument( lString32 title, lString32 message );
bool loadDocument( lString32 filename );
bool loadDocument( LVStreamRef stream, lString32 contentPath );
void createDefaultDocument( const lString32& title, const lString32& message );
bool loadDocument( const lString32& filename );
bool loadDocument( LVStreamRef stream, const lString32& contentPath );
int doCommand( int cmd, int param );
bool findText( lString32 pattern, int origin, bool reverse, bool caseInsensitive );
void clearSelection();
Expand Down
4 changes: 2 additions & 2 deletions android/jni/org_coolreader_crengine_DocView.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions android/jni/org_coolreader_crengine_Engine.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions android/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -735,4 +735,5 @@
<string name="filter_by_book_language_s">Отфильтровать по языку книги (%s)</string>
<string name="scanning_font_files">Сканирование файлов шрифтов…</string>
<string name="undetermined">Неопределенный</string>
<string name="failed_to_save_memory_stream">Не удалось сохранить внешний поток в хранилище устройства!</string>
</resources>
1 change: 1 addition & 0 deletions android/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -774,4 +774,5 @@
<string name="filter_by_book_language_s">Filter by book language (%s)</string>
<string name="scanning_font_files">Scanning font files…</string>
<string name="undetermined">Undetermined</string>
<string name="failed_to_save_memory_stream">Failed to save memory stream to device storage!</string>
</resources>
4 changes: 2 additions & 2 deletions android/src/org/coolreader/CoolReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -728,11 +728,10 @@ private boolean processIntent(Intent intent) {
}, 500), true);
return true;
} else if (null != uri) {
// TODO: calculate fingerprint for uri and find fileInfo in DB
log.d("URI_TO_OPEN = " + uri);
final String uriString = uri.toString();
mFileToOpenFromExt = uriString;
loadDocumentFromUri(uri, () -> showToast(R.string.opened_from_stream), () -> BackgroundThread.instance().postGUI(() -> {
loadDocumentFromUri(uri, null, () -> BackgroundThread.instance().postGUI(() -> {
// if document not loaded show error & then root window
ErrorDialog errDialog = new ErrorDialog(CoolReader.this, CoolReader.this.getString(R.string.error), CoolReader.this.getString(R.string.cant_open_file, uriString));
errDialog.setOnDismissListener(dialog -> showRootWindow());
Expand Down Expand Up @@ -1422,6 +1421,7 @@ public void loadDocumentFromUri(Uri uri, Runnable doneCallback, Runnable errorCa
InputStream inputStream;
try {
inputStream = contentResolver.openInputStream(uri);
// TODO: Fix this
// Don't save the last opened document from the stream in the cloud, since we still cannot open it later in this program.
mReaderView.loadDocumentFromStream(inputStream, uri.getPath(), doneCallback, errorCallback);
} catch (Exception e) {
Expand Down
43 changes: 11 additions & 32 deletions android/src/org/coolreader/crengine/DocView.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ public void setPageBackgroundTexture(
* create empty document with specified message (e.g. to show errors)
* @param title
* @param message
* @return
*/
public void createDefaultDocument(String title, String message)
{
Expand All @@ -164,36 +163,15 @@ public boolean loadDocument(String fileName) {
}

/**
* Load document from input stream.
* @param inputStream
* @param contentPath
* @return
* Load document from memory buffer.
* @param buffer document content as byte buffer
* @param contentPath Non empty stream name
* @return true if operation successful, false otherwise.
*/
public boolean loadDocumentFromStream(InputStream inputStream, String contentPath) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
int errorCode = 0;
try {
byte [] buf = new byte [4096];
int readBytes;
while (true) {
readBytes = inputStream.read(buf);
if (readBytes > 0)
outputStream.write(buf, 0, readBytes);
else
break;
}
} catch (IOException e1) {
errorCode = 1;
} catch (OutOfMemoryError e2) {
errorCode = 2;
}
if (0 == errorCode) {
synchronized (mutex) {
return loadDocumentFromMemoryInternal(outputStream.toByteArray(), contentPath);
}
public boolean loadDocumentFromBuffer(byte[] buffer, String contentPath) {
synchronized (mutex) {
return loadDocumentFromMemoryInternal(buffer, contentPath);
}
// TODO: pass error code to caller to show message for user.
return false;
}

/**
Expand Down Expand Up @@ -317,10 +295,11 @@ public PositionProperties getPositionProps(String xPath, boolean precise) {
/**
* Fill book info fields using metadata from current book.
* @param info
* @param updatePath
*/
public void updateBookInfo(BookInfo info) {
public void updateBookInfo(BookInfo info, boolean updatePath) {
synchronized(mutex) {
updateBookInfoInternal(info);
updateBookInfoInternal(info, updatePath);
}
}

Expand Down Expand Up @@ -475,7 +454,7 @@ private native boolean applySettingsInternal(

private native PositionProperties getPositionPropsInternal(String xPath, boolean precise);

private native void updateBookInfoInternal(BookInfo info);
private native void updateBookInfoInternal(BookInfo info, boolean updatePath);

private native TOCItem getTOCInternal();

Expand Down
Loading

0 comments on commit c4a9f55

Please sign in to comment.