diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt index 885f05172..a10b4d183 100755 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt @@ -398,7 +398,10 @@ class Downloader( return pageObservable // When the page is ready, set page path, progress (just in case) and status .doOnNext { file -> - splitTallImageIfNeeded(page, tmpDir) + val success = splitTallImageIfNeeded(page, tmpDir) + if (success.not()) { + notifier.onError(context.getString(R.string.download_notifier_split_failed), download.chapter.name, download.manga.title) + } page.uri = file.uri page.progress = 100 download.downloadedImages++ @@ -483,8 +486,8 @@ class Downloader( return MimeTypeMap.getSingleton().getExtensionFromMimeType(mime) ?: "jpg" } - private fun splitTallImageIfNeeded(page: Page, tmpDir: UniFile) { - if (!preferences.splitTallImages().get()) return + private fun splitTallImageIfNeeded(page: Page, tmpDir: UniFile): Boolean { + if (!preferences.splitTallImages().get()) return true val filename = String.format("%03d", page.number) val imageFile = tmpDir.listFiles()?.find { it.name!!.startsWith(filename) } @@ -493,8 +496,9 @@ class Downloader( ?: throw Error(context.getString(R.string.download_notifier_split_page_path_not_found, page.number)) // check if the original page was previously splitted before then skip. - if (imageFile.name!!.contains("__")) return - ImageUtil.splitTallImage(imageFile, imageFilePath) + if (imageFile.name!!.contains("__")) return true + + return ImageUtil.splitTallImage(imageFile, imageFilePath) } /** diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourceFilterPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourceFilterPresenter.kt index 3b7a0b55d..267ffbb3e 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourceFilterPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourceFilterPresenter.kt @@ -37,15 +37,12 @@ class SourceFilterPresenter( .catch { exception -> _state.value = SourceFilterState.Error(exception) } - .collectLatest { sourceLangMap -> - val uiModels = sourceLangMap.toFilterUiModels() - _state.value = SourceFilterState.Success(uiModels) - } + .collectLatest(::collectLatestSourceLangMap) } } - private fun Map>.toFilterUiModels(): List { - return this.flatMap { + private fun collectLatestSourceLangMap(sourceLangMap: Map>) { + val uiModels = sourceLangMap.flatMap { val isLangEnabled = it.key in preferences.enabledLanguages().get() val header = listOf(FilterUiModel.Header(it.key, isLangEnabled)) // SY --> @@ -66,6 +63,7 @@ class SourceFilterPresenter( ) } } + _state.value = SourceFilterState.Success(uiModels) } fun toggleSource(source: Source) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/system/ImageUtil.kt b/app/src/main/java/eu/kanade/tachiyomi/util/system/ImageUtil.kt index bd4bc572a..2a888f26b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/system/ImageUtil.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/system/ImageUtil.kt @@ -20,6 +20,7 @@ import androidx.core.graphics.createBitmap import androidx.core.graphics.green import androidx.core.graphics.red import com.hippo.unifile.UniFile +import logcat.LogPriority import tachiyomi.decoder.Format import tachiyomi.decoder.ImageDecoder import java.io.BufferedInputStream @@ -184,7 +185,7 @@ object ImageUtil { * * @return true if the height:width ratio is greater than 3. */ - fun isTallImage(imageStream: InputStream): Boolean { + private fun isTallImage(imageStream: InputStream): Boolean { val options = extractImageOptions(imageStream, false) return (options.outHeight / options.outWidth) > 3 } @@ -192,9 +193,9 @@ object ImageUtil { /** * Splits tall images to improve performance of reader */ - fun splitTallImage(imageFile: UniFile, imageFilePath: String) { + fun splitTallImage(imageFile: UniFile, imageFilePath: String): Boolean { if (isAnimatedAndSupported(imageFile.openInputStream()) || !isTallImage(imageFile.openInputStream())) { - return + return true } val options = extractImageOptions(imageFile.openInputStream(), false).apply { inJustDecodeBounds = false } @@ -215,6 +216,11 @@ object ImageUtil { BitmapRegionDecoder.newInstance(imageFile.openInputStream(), false) } + if (bitmapRegionDecoder == null) { + logcat { "Failed to create new instance of BitmapRegionDecoder" } + return false + } + try { (0 until partCount).forEach { splitIndex -> val splitPath = imageFilePath.substringBeforeLast(".") + "__${"%03d".format(splitIndex + 1)}.jpg" @@ -227,19 +233,21 @@ object ImageUtil { val region = Rect(0, topOffset, imageWidth, bottomOffset) FileOutputStream(splitPath).use { outputStream -> - val splitBitmap = bitmapRegionDecoder!!.decodeRegion(region, options) + val splitBitmap = bitmapRegionDecoder.decodeRegion(region, options) splitBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream) } } imageFile.delete() + return true } catch (e: Exception) { // Image splits were not successfully saved so delete them and keep the original image (0 until partCount) - .map { imageFile.filePath!!.substringBeforeLast(".") + "__${"%03d".format(it + 1)}.jpg" } + .map { imageFilePath.substringBeforeLast(".") + "__${"%03d".format(it + 1)}.jpg" } .forEach { File(it).delete() } - throw e + logcat(LogPriority.ERROR, e) + return false } finally { - bitmapRegionDecoder?.recycle() + bitmapRegionDecoder.recycle() } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3469a96db..0d89849d1 100755 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -820,6 +820,7 @@ Download completed Page %d not found while splitting Couldn\'t find file path of page %d + Couldn\'t split downloaded image Common