From 643e970a27450148249a3af5ad910d7bcc219be3 Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Mon, 25 May 2020 22:16:18 +0200 Subject: [PATCH] Metadata improvements for local feeds --- .../antennapod/fragment/AddFeedFragment.java | 2 ++ .../core/feed/LocalFeedUpdater.java | 35 ++++++++++--------- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/AddFeedFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/AddFeedFragment.java index d29a36871..cc261c708 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/AddFeedFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/AddFeedFragment.java @@ -23,6 +23,7 @@ import de.danoeh.antennapod.activity.OnlineFeedViewActivity; import de.danoeh.antennapod.activity.OpmlImportActivity; import de.danoeh.antennapod.core.feed.Feed; import de.danoeh.antennapod.core.storage.DBTasks; +import de.danoeh.antennapod.core.util.SortOrder; import de.danoeh.antennapod.discovery.CombinedSearcher; import de.danoeh.antennapod.discovery.FyydPodcastSearcher; import de.danoeh.antennapod.discovery.ItunesPodcastSearcher; @@ -153,6 +154,7 @@ public class AddFeedFragment extends Fragment { Feed dirFeed = new Feed(Feed.PREFIX_LOCAL_FOLDER + uri.toString(), null, documentFile.getName()); dirFeed.setDescription(getString(R.string.local_feed_description)); dirFeed.setItems(Collections.emptyList()); + dirFeed.setSortOrder(SortOrder.EPISODE_TITLE_A_Z); DBTasks.forceRefreshFeed(getContext(), dirFeed, true); ((MainActivity) getActivity()) .showSnackbarAbovePlayer(R.string.add_local_folder_success, Snackbar.LENGTH_SHORT); diff --git a/core/src/main/java/de/danoeh/antennapod/core/feed/LocalFeedUpdater.java b/core/src/main/java/de/danoeh/antennapod/core/feed/LocalFeedUpdater.java index da3d6f19d..04d2afb0b 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/feed/LocalFeedUpdater.java +++ b/core/src/main/java/de/danoeh/antennapod/core/feed/LocalFeedUpdater.java @@ -3,11 +3,13 @@ package de.danoeh.antennapod.core.feed; import android.content.Context; import android.media.MediaMetadataRetriever; import android.net.Uri; +import android.text.TextUtils; import android.util.Log; import androidx.documentfile.provider.DocumentFile; import de.danoeh.antennapod.core.service.download.DownloadStatus; import de.danoeh.antennapod.core.storage.DBTasks; import de.danoeh.antennapod.core.storage.DBWriter; +import de.danoeh.antennapod.core.util.DateUtils; import de.danoeh.antennapod.core.util.DownloadError; import java.util.ArrayList; @@ -46,12 +48,12 @@ public class LocalFeedUpdater { List newItems = feed.getItems(); for (DocumentFile f : mediaFiles) { - FeedItem found = feedContainsFile(feed, f.getName()); - if (found != null) { - //TODO make sure the media has not changed (type, duration) + FeedItem oldItem = feedContainsFile(feed, f.getName()); + FeedItem newItem = createFeedItem(feed, f, context); + if (oldItem == null) { + newItems.add(newItem); } else { - FeedItem item = createFeedItem(feed, f, context); - newItems.add(item); + oldItem.updateFromOther(newItem); } } @@ -78,15 +80,21 @@ public class LocalFeedUpdater { } private static FeedItem createFeedItem(Feed feed, DocumentFile file, Context context) { - //create item - long globalId = 0; - Date date = new Date(); String uuid = UUID.randomUUID().toString(); - FeedItem item = new FeedItem(globalId, file.getName(), uuid, file.getName(), date, FeedItem.UNPLAYED, feed); + FeedItem item = new FeedItem(0, file.getName(), uuid, file.getName(), new Date(), + FeedItem.UNPLAYED, feed); item.setAutoDownload(false); + MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever(); + mediaMetadataRetriever.setDataSource(context, file.getUri()); + String durationStr = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION); + String title = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE); + if (!TextUtils.isEmpty(title)) { + item.setTitle(title); + } + //add the media to the item - long duration = getFileDuration(file, context); + long duration = Long.parseLong(durationStr); long size = file.length(); FeedMedia media = new FeedMedia(0, item, (int) duration, 0, size, file.getType(), file.getUri().toString(), file.getUri().toString(), false, null, 0, 0); @@ -101,11 +109,4 @@ public class LocalFeedUpdater { DBWriter.addDownloadStatus(status); DBWriter.setFeedLastUpdateFailed(feed.getId(), true); } - - private static long getFileDuration(DocumentFile f, Context context) { - MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever(); - mediaMetadataRetriever.setDataSource(context, f.getUri()); - String durationStr = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION); - return Long.parseLong(durationStr); - } }