diff options
author | ByteHamster <info@bytehamster.com> | 2022-01-03 14:26:56 +0100 |
---|---|---|
committer | ByteHamster <info@bytehamster.com> | 2022-01-03 14:26:56 +0100 |
commit | 413df734953dbfbaffac312745f7a467c3ec41ff (patch) | |
tree | c1045571d718d65cdd2144751f409073effb9c41 /core/src/main/java/de/danoeh | |
parent | 9e42e103a902b4da411a1d737f0fd97d40df64ce (diff) | |
parent | 83f564ffcae24fe2ee56303b6b09e963948d18bf (diff) | |
download | AntennaPod-413df734953dbfbaffac312745f7a467c3ec41ff.zip |
Merge branch 'master' into develop
Diffstat (limited to 'core/src/main/java/de/danoeh')
3 files changed, 72 insertions, 31 deletions
diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java index 22378e01a..805956094 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java @@ -1529,8 +1529,8 @@ public class PlaybackService extends MediaBrowserServiceCompat { */ private void pauseIfPauseOnDisconnect() { Log.d(TAG, "pauseIfPauseOnDisconnect()"); + transientPause = (mediaPlayer.getPlayerStatus() == PlayerStatus.PLAYING); if (UserPreferences.isPauseOnHeadsetDisconnect() && !isCasting()) { - transientPause = true; mediaPlayer.pause(!UserPreferences.isPersistNotify(), false); } } diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/DBTasks.java b/core/src/main/java/de/danoeh/antennapod/core/storage/DBTasks.java index 2cb99559a..412914329 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/DBTasks.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/DBTasks.java @@ -354,42 +354,13 @@ public final class DBTasks { */ private static FeedItem searchFeedItemGuessDuplicate(List<FeedItem> items, FeedItem searchItem) { for (FeedItem item : items) { - if ((item.getMedia() != null) - && (searchItem.getMedia() != null) - && !TextUtils.isEmpty(item.getMedia().getStreamUrl()) - && !TextUtils.isEmpty(searchItem.getMedia().getStreamUrl()) - && TextUtils.equals(item.getMedia().getStreamUrl(), searchItem.getMedia().getStreamUrl())) { + if (FeedItemDuplicateGuesser.seemDuplicates(item, searchItem)) { return item; - } else if (titlesLookSimilar(item.getTitle(), searchItem.getTitle())) { - if (searchItem.getPubDate() == null || item.getPubDate() == null) { - continue; - } - long dateOriginal = item.getPubDate().getTime(); - long dateNew = searchItem.getPubDate().getTime(); - if (Math.abs(dateOriginal - dateNew) < 7L * 24L * 3600L * 1000L) { // Same week - return item; - } } } return null; } - private static boolean titlesLookSimilar(String title1, String title2) { - if (TextUtils.isEmpty(title1) || TextUtils.isEmpty(title2)) { - return false; - } - return canonicalizeTitle(title1).equals(canonicalizeTitle(title2)); - } - - private static String canonicalizeTitle(String title) { - return title - .trim() - .replace('“', '"') - .replace('”', '"') - .replace('„', '"') - .replace('—', '-'); - } - /** * Adds new Feeds to the database or updates the old versions if they already exists. If another Feed with the same * identifying value already exists, this method will add new FeedItems from the new Feed to the existing Feed. diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/FeedItemDuplicateGuesser.java b/core/src/main/java/de/danoeh/antennapod/core/storage/FeedItemDuplicateGuesser.java new file mode 100644 index 000000000..35d77ae4a --- /dev/null +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/FeedItemDuplicateGuesser.java @@ -0,0 +1,70 @@ +package de.danoeh.antennapod.core.storage; + +import android.text.TextUtils; +import de.danoeh.antennapod.model.feed.FeedItem; +import de.danoeh.antennapod.model.feed.FeedMedia; + +import java.text.DateFormat; +import java.util.Locale; + +/** + * Publishers sometimes mess up their feed by adding episodes twice or by changing the ID of existing episodes. + * This class tries to guess if publishers actually meant another episode, + * even if their feed explicitly says that the episodes are different. + */ +public class FeedItemDuplicateGuesser { + public static boolean seemDuplicates(FeedItem item1, FeedItem item2) { + if (sameAndNotEmpty(item1.getItemIdentifier(), item2.getItemIdentifier())) { + return true; + } + FeedMedia media1 = item1.getMedia(); + FeedMedia media2 = item2.getMedia(); + if (media1 == null || media2 == null) { + return false; + } + if (sameAndNotEmpty(media1.getStreamUrl(), media2.getStreamUrl())) { + return true; + } + return titlesLookSimilar(item1, item2) + && datesLookSimilar(item1, item2) + && durationsLookSimilar(media1, media2) + && TextUtils.equals(media1.getMime_type(), media2.getMime_type()); + } + + private static boolean sameAndNotEmpty(String string1, String string2) { + if (TextUtils.isEmpty(string1) || TextUtils.isEmpty(string2)) { + return false; + } + return string1.equals(string2); + } + + private static boolean datesLookSimilar(FeedItem item1, FeedItem item2) { + if (item1.getPubDate() == null || item2.getPubDate() == null) { + return false; + } + DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.US); // MM/DD/YY + String dateOriginal = dateFormat.format(item2.getPubDate()); + String dateNew = dateFormat.format(item1.getPubDate()); + return TextUtils.equals(dateOriginal, dateNew); // Same date; time is ignored. + } + + private static boolean durationsLookSimilar(FeedMedia media1, FeedMedia media2) { + return Math.abs(media1.getDuration() - media2.getDuration()) < 10 * 60L * 1000L; + } + + private static boolean titlesLookSimilar(FeedItem item1, FeedItem item2) { + return sameAndNotEmpty(canonicalizeTitle(item1.getTitle()), canonicalizeTitle(item2.getTitle())); + } + + private static String canonicalizeTitle(String title) { + if (title == null) { + return ""; + } + return title + .trim() + .replace('“', '"') + .replace('”', '"') + .replace('„', '"') + .replace('—', '-'); + } +} |