From 11dc0d7e9e4d0948e3dc17cfd564489ab40f971b Mon Sep 17 00:00:00 2001 From: inorichi Date: Sat, 23 Jan 2016 13:58:53 +0100 Subject: [PATCH] Change filename for downloaded chapters, using the last path from the url is not reliable. This will break compatibility with previously downloaded chapters, they have to be deleted and downloaded again. Disable download progress in the chapters view, it will avoid some crashes. --- .../data/download/DownloadManager.java | 10 +++++ .../ui/download/DownloadPresenter.java | 1 + .../tachiyomi/ui/manga/MangaPresenter.java | 8 ++-- .../ui/manga/chapter/ChaptersFragment.java | 12 ------ .../eu/kanade/tachiyomi/util/UrlUtil.java | 37 +++++++++++++++++++ 5 files changed, 52 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.java b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.java index e4f71632b..5523351e7 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.java +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.java @@ -28,6 +28,7 @@ import eu.kanade.tachiyomi.data.source.base.Source; import eu.kanade.tachiyomi.data.source.model.Page; import eu.kanade.tachiyomi.event.DownloadChaptersEvent; import eu.kanade.tachiyomi.util.DiskUtils; +import eu.kanade.tachiyomi.util.UrlUtil; import rx.Observable; import rx.Subscription; import rx.android.schedulers.AndroidSchedulers; @@ -279,6 +280,15 @@ public class DownloadManager { // Get the filename for an image given the page private String getImageFilename(Page page) { String url = page.getImageUrl(); + int number = page.getPageNumber() + 1; + // Try to preserve file extension + if (UrlUtil.isJpg(url)) { + return number + ".jpg"; + } else if (UrlUtil.isPng(url)) { + return number + ".png"; + } else if (UrlUtil.isGif(url)) { + return number + ".gif"; + } return Uri.parse(url).getLastPathSegment().replaceAll("[^\\sa-zA-Z0-9.-]", "_"); } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/download/DownloadPresenter.java b/app/src/main/java/eu/kanade/tachiyomi/ui/download/DownloadPresenter.java index 429180f75..fe69bc884 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/download/DownloadPresenter.java +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/download/DownloadPresenter.java @@ -90,6 +90,7 @@ public class DownloadPresenter extends BasePresenter { .flatMap(tick -> Observable.from(download.pages) .map(Page::getProgress) .reduce((x, y) -> x + y)) + .onBackpressureLatest() .observeOn(AndroidSchedulers.mainThread()) .subscribe(progress -> { if (download.totalProgress != progress) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaPresenter.java b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaPresenter.java index 797ea674c..a24f093e7 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaPresenter.java +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaPresenter.java @@ -19,13 +19,13 @@ public class MangaPresenter extends BasePresenter { @State Manga manga; - private static final int DB_MANGA = 1; + private static final int GET_MANGA = 1; @Override protected void onCreate(Bundle savedState) { super.onCreate(savedState); - restartableLatestCache(DB_MANGA, this::getDbMangaObservable, MangaActivity::setManga); + restartableLatestCache(GET_MANGA, this::getMangaObservable, MangaActivity::setManga); if (savedState == null) registerForStickyEvents(); @@ -38,7 +38,7 @@ public class MangaPresenter extends BasePresenter { EventBus.getDefault().removeStickyEvent(MangaEvent.class); } - private Observable getDbMangaObservable() { + private Observable getMangaObservable() { return Observable.just(manga) .doOnNext(manga -> EventBus.getDefault().postSticky(new MangaEvent(manga))); } @@ -48,7 +48,7 @@ public class MangaPresenter extends BasePresenter { EventBus.getDefault().removeStickyEvent(manga); unregisterForEvents(); this.manga = manga; - start(DB_MANGA); + start(GET_MANGA); } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersFragment.java b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersFragment.java index 0ecd4e939..05c115919 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersFragment.java +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersFragment.java @@ -103,18 +103,6 @@ public class ChaptersFragment extends BaseRxFragment implemen return view; } - @Override - public void onResume() { - super.onResume(); - observeChapterDownloadProgress(); - } - - @Override - public void onPause() { - unsubscribeChapterDownloadProgress(); - super.onPause(); - } - public void onNextChapters(List chapters) { // If the list is empty, fetch chapters from source if the conditions are met // We use presenter chapters instead because they are always unfiltered diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/UrlUtil.java b/app/src/main/java/eu/kanade/tachiyomi/util/UrlUtil.java index 65e0ea618..baf7ce9dc 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/UrlUtil.java +++ b/app/src/main/java/eu/kanade/tachiyomi/util/UrlUtil.java @@ -5,6 +5,10 @@ import java.net.URISyntaxException; public class UrlUtil { + private static final String JPG = ".jpg"; + private static final String PNG = ".png"; + private static final String GIF = ".gif"; + public static String getPath(String s) { try { URI uri = new URI(s); @@ -18,4 +22,37 @@ public class UrlUtil { return s; } } + + public static boolean isJpg(String url) { + return containsIgnoreCase(url, JPG); + } + + public static boolean isPng(String url) { + return containsIgnoreCase(url, PNG); + } + + public static boolean isGif(String url) { + return containsIgnoreCase(url, GIF); + } + + public static boolean containsIgnoreCase(String src, String what) { + final int length = what.length(); + if (length == 0) + return true; // Empty string is contained + + final char firstLo = Character.toLowerCase(what.charAt(0)); + final char firstUp = Character.toUpperCase(what.charAt(0)); + + for (int i = src.length() - length; i >= 0; i--) { + // Quick check before calling the more expensive regionMatches() method: + final char ch = src.charAt(i); + if (ch != firstLo && ch != firstUp) + continue; + + if (src.regionMatches(true, i, what, 0, length)) + return true; + } + + return false; + } }