diff options
author | LukasBrilla5 <114982148+LukasBrilla5@users.noreply.github.com> | 2022-11-18 20:08:48 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-18 20:08:48 +0100 |
commit | d585e37e11d01b61773b287ce2e6cb5ae24380a1 (patch) | |
tree | e8a9d6a21cdb6ac7ff07de30307f6d55fa75329b /core/src/main | |
parent | 63ba5c458f8679f705ddbde458e051668100b70b (diff) | |
download | AntennaPod-d585e37e11d01b61773b287ce2e6cb5ae24380a1.zip |
Add button to refresh episode chapters (#6177)
Diffstat (limited to 'core/src/main')
3 files changed, 24 insertions, 15 deletions
diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/download/handler/MediaDownloadedHandler.java b/core/src/main/java/de/danoeh/antennapod/core/service/download/handler/MediaDownloadedHandler.java index 442e3173d..897300ca8 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/download/handler/MediaDownloadedHandler.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/download/handler/MediaDownloadedHandler.java @@ -59,7 +59,7 @@ public class MediaDownloadedHandler implements Runnable { } if (media.getItem() != null && media.getItem().getPodcastIndexChapterUrl() != null) { - ChapterUtils.loadChaptersFromUrl(media.getItem().getPodcastIndexChapterUrl()); + ChapterUtils.loadChaptersFromUrl(media.getItem().getPodcastIndexChapterUrl(), false); } // Get duration MediaMetadataRetriever mmr = new MediaMetadataRetriever(); diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackServiceTaskManager.java b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackServiceTaskManager.java index 19032ba18..3111d01bc 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackServiceTaskManager.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackServiceTaskManager.java @@ -227,7 +227,7 @@ public class PlaybackServiceTaskManager { if (media.getChapters() == null) { chapterLoaderFuture = Completable.create(emitter -> { - ChapterUtils.loadChapters(media, context); + ChapterUtils.loadChapters(media, context, false); emitter.onComplete(); }) .subscribeOn(Schedulers.io()) diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/ChapterUtils.java b/core/src/main/java/de/danoeh/antennapod/core/util/ChapterUtils.java index cc63bf2b0..084b34e76 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/util/ChapterUtils.java +++ b/core/src/main/java/de/danoeh/antennapod/core/util/ChapterUtils.java @@ -54,8 +54,8 @@ public class ChapterUtils { return chapters.size() - 1; } - public static void loadChapters(Playable playable, Context context) { - if (playable.getChapters() != null) { + public static void loadChapters(Playable playable, Context context, boolean forceRefresh) { + if (playable.getChapters() != null && !forceRefresh) { // Already loaded return; } @@ -73,7 +73,7 @@ public class ChapterUtils { if (!TextUtils.isEmpty(feedMedia.getItem().getPodcastIndexChapterUrl())) { chaptersFromPodcastIndex = ChapterUtils.loadChaptersFromUrl( - feedMedia.getItem().getPodcastIndexChapterUrl()); + feedMedia.getItem().getPodcastIndexChapterUrl(), forceRefresh); } } @@ -135,23 +135,32 @@ public class ChapterUtils { } } - public static List<Chapter> loadChaptersFromUrl(String url) { + public static List<Chapter> loadChaptersFromUrl(String url, boolean forceRefresh) { + if (forceRefresh) { + return loadChaptersFromUrl(url, CacheControl.FORCE_NETWORK); + } + List<Chapter> cachedChapters = loadChaptersFromUrl(url, CacheControl.FORCE_CACHE); + if (cachedChapters == null || cachedChapters.size() <= 1) { + // Some publishers use one dummy chapter before actual chapters are available + return loadChaptersFromUrl(url, CacheControl.FORCE_NETWORK); + } + return cachedChapters; + } + + private static List<Chapter> loadChaptersFromUrl(String url, CacheControl cacheControl) { + Response response = null; try { - Request request = new Request.Builder().url(url).cacheControl(CacheControl.FORCE_CACHE).build(); - Response response = AntennapodHttpClient.getHttpClient().newCall(request).execute(); - if (response.isSuccessful() && response.body() != null) { - List<Chapter> chapters = PodcastIndexChapterParser.parse(response.body().string()); - if (chapters != null && !chapters.isEmpty()) { - return chapters; - } - } - request = new Request.Builder().url(url).build(); + Request request = new Request.Builder().url(url).cacheControl(cacheControl).build(); response = AntennapodHttpClient.getHttpClient().newCall(request).execute(); if (response.isSuccessful() && response.body() != null) { return PodcastIndexChapterParser.parse(response.body().string()); } } catch (IOException e) { e.printStackTrace(); + } finally { + if (response != null) { + response.close(); + } } return null; } |