Merge pull request #1521 from kiwix/feature/macgills/#1519-exception-opening-zim-file

#1519 JniKiwixException opening Zim file
This commit is contained in:
Seán Mac Gillicuddy 2019-09-30 10:50:30 +01:00 committed by GitHub
commit ee4c707942
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 19 deletions

View File

@ -99,6 +99,7 @@ import org.kiwix.kiwixmobile.R;
import org.kiwix.kiwixmobile.base.BaseActivity; import org.kiwix.kiwixmobile.base.BaseActivity;
import org.kiwix.kiwixmobile.bookmark.BookmarkItem; import org.kiwix.kiwixmobile.bookmark.BookmarkItem;
import org.kiwix.kiwixmobile.bookmark.BookmarksActivity; import org.kiwix.kiwixmobile.bookmark.BookmarksActivity;
import org.kiwix.kiwixmobile.extensions.ContextExtensionsKt;
import org.kiwix.kiwixmobile.help.HelpActivity; import org.kiwix.kiwixmobile.help.HelpActivity;
import org.kiwix.kiwixmobile.history.HistoryActivity; import org.kiwix.kiwixmobile.history.HistoryActivity;
import org.kiwix.kiwixmobile.history.HistoryListItem; import org.kiwix.kiwixmobile.history.HistoryListItem;
@ -1090,24 +1091,26 @@ public class MainActivity extends BaseActivity implements WebViewCallback,
&& Build.VERSION.SDK_INT != 23)) { && Build.VERSION.SDK_INT != 23)) {
if (file.exists()) { if (file.exists()) {
zimReaderContainer.setZimFile(file); zimReaderContainer.setZimFile(file);
if (clearHistory) { if (zimReaderContainer.getZimFileReader() != null) {
requestClearHistoryAfterLoad = true; if (clearHistory) {
} requestClearHistoryAfterLoad = true;
if (menu != null) { }
initAllMenuItems(); if (menu != null) {
initAllMenuItems();
} else {
// Menu may not be initialized yet. In this case
// signal to menu create to show
requestInitAllMenuItems = true;
}
openMainPage();
presenter.loadCurrentZimBookmarksUrl();
} else { } else {
// Menu may not be initialized yet. In this case ContextExtensionsKt.toast(this, R.string.error_file_invalid, Toast.LENGTH_LONG);
// signal to menu create to show showHomePage();
requestInitAllMenuItems = true;
} }
openMainPage();
presenter.loadCurrentZimBookmarksUrl();
} else { } else {
Log.w(TAG_KIWIX, "ZIM file doesn't exist at " + file.getAbsolutePath()); Log.w(TAG_KIWIX, "ZIM file doesn't exist at " + file.getAbsolutePath());
ContextExtensionsKt.toast(this, R.string.error_file_not_found, Toast.LENGTH_LONG);
Toast.makeText(this, getResources().getString(R.string.error_file_not_found),
Toast.LENGTH_LONG)
.show();
showHomePage(); showHomePage();
} }
} else { } else {

View File

@ -26,6 +26,7 @@ import android.webkit.MimeTypeMap
import androidx.core.net.toUri import androidx.core.net.toUri
import io.reactivex.Single import io.reactivex.Single
import io.reactivex.schedulers.Schedulers import io.reactivex.schedulers.Schedulers
import org.kiwix.kiwixlib.JNIKiwixException
import org.kiwix.kiwixlib.JNIKiwixInt import org.kiwix.kiwixlib.JNIKiwixInt
import org.kiwix.kiwixlib.JNIKiwixReader import org.kiwix.kiwixlib.JNIKiwixReader
import org.kiwix.kiwixlib.JNIKiwixString import org.kiwix.kiwixlib.JNIKiwixString
@ -51,11 +52,15 @@ class ZimFileReader(
private val sharedPreferenceUtil: SharedPreferenceUtil private val sharedPreferenceUtil: SharedPreferenceUtil
) { ) {
interface Factory { interface Factory {
fun create(file: File): ZimFileReader fun create(file: File): ZimFileReader?
class Impl @Inject constructor(val sharedPreferenceUtil: SharedPreferenceUtil) : Factory { class Impl @Inject constructor(val sharedPreferenceUtil: SharedPreferenceUtil) : Factory {
override fun create(file: File) = override fun create(file: File) =
ZimFileReader(file, sharedPreferenceUtil = sharedPreferenceUtil) try {
ZimFileReader(file, sharedPreferenceUtil = sharedPreferenceUtil)
} catch (ignore: JNIKiwixException) {
null
}
} }
} }

View File

@ -1,5 +1,6 @@
package org.kiwix.kiwixmobile.zim_manager.fileselect_view package org.kiwix.kiwixmobile.zim_manager.fileselect_view
import io.reactivex.Flowable
import io.reactivex.functions.BiFunction import io.reactivex.functions.BiFunction
import io.reactivex.schedulers.Schedulers import io.reactivex.schedulers.Schedulers
import org.kiwix.kiwixmobile.database.newdb.dao.FetchDownloadDao import org.kiwix.kiwixmobile.database.newdb.dao.FetchDownloadDao
@ -16,10 +17,10 @@ class StorageObserver @Inject constructor(
private val zimReaderFactory: ZimFileReader.Factory private val zimReaderFactory: ZimFileReader.Factory
) { ) {
val booksOnFileSystem val booksOnFileSystem: Flowable<List<BookOnDisk>>
get() = scanFiles() get() = scanFiles()
.withLatestFrom(downloadDao.downloads(), BiFunction(::toFilesThatAreNotDownloading)) .withLatestFrom(downloadDao.downloads(), BiFunction(::toFilesThatAreNotDownloading))
.map { it.map(::convertToBookOnDisk) } .map { it.mapNotNull(::convertToBookOnDisk) }
private fun scanFiles() = fileSearch.scan().subscribeOn(Schedulers.io()) private fun scanFiles() = fileSearch.scan().subscribeOn(Schedulers.io())
@ -29,5 +30,6 @@ class StorageObserver @Inject constructor(
private fun fileHasNoMatchingDownload(downloads: List<DownloadModel>, file: File) = private fun fileHasNoMatchingDownload(downloads: List<DownloadModel>, file: File) =
downloads.firstOrNull { file.absolutePath.endsWith(it.fileNameFromUrl) } == null downloads.firstOrNull { file.absolutePath.endsWith(it.fileNameFromUrl) } == null
private fun convertToBookOnDisk(file: File) = BookOnDisk(file, zimReaderFactory.create(file)) private fun convertToBookOnDisk(file: File) =
zimReaderFactory.create(file)?.let { BookOnDisk(file, it) }
} }