Metadata improvements for local feeds

This commit is contained in:
ByteHamster 2020-05-25 22:16:18 +02:00
parent 05cc4244e6
commit 643e970a27
2 changed files with 20 additions and 17 deletions

View File

@ -23,6 +23,7 @@ import de.danoeh.antennapod.activity.OnlineFeedViewActivity;
import de.danoeh.antennapod.activity.OpmlImportActivity; import de.danoeh.antennapod.activity.OpmlImportActivity;
import de.danoeh.antennapod.core.feed.Feed; import de.danoeh.antennapod.core.feed.Feed;
import de.danoeh.antennapod.core.storage.DBTasks; 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.CombinedSearcher;
import de.danoeh.antennapod.discovery.FyydPodcastSearcher; import de.danoeh.antennapod.discovery.FyydPodcastSearcher;
import de.danoeh.antennapod.discovery.ItunesPodcastSearcher; 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()); Feed dirFeed = new Feed(Feed.PREFIX_LOCAL_FOLDER + uri.toString(), null, documentFile.getName());
dirFeed.setDescription(getString(R.string.local_feed_description)); dirFeed.setDescription(getString(R.string.local_feed_description));
dirFeed.setItems(Collections.emptyList()); dirFeed.setItems(Collections.emptyList());
dirFeed.setSortOrder(SortOrder.EPISODE_TITLE_A_Z);
DBTasks.forceRefreshFeed(getContext(), dirFeed, true); DBTasks.forceRefreshFeed(getContext(), dirFeed, true);
((MainActivity) getActivity()) ((MainActivity) getActivity())
.showSnackbarAbovePlayer(R.string.add_local_folder_success, Snackbar.LENGTH_SHORT); .showSnackbarAbovePlayer(R.string.add_local_folder_success, Snackbar.LENGTH_SHORT);

View File

@ -3,11 +3,13 @@ package de.danoeh.antennapod.core.feed;
import android.content.Context; import android.content.Context;
import android.media.MediaMetadataRetriever; import android.media.MediaMetadataRetriever;
import android.net.Uri; import android.net.Uri;
import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import androidx.documentfile.provider.DocumentFile; import androidx.documentfile.provider.DocumentFile;
import de.danoeh.antennapod.core.service.download.DownloadStatus; import de.danoeh.antennapod.core.service.download.DownloadStatus;
import de.danoeh.antennapod.core.storage.DBTasks; import de.danoeh.antennapod.core.storage.DBTasks;
import de.danoeh.antennapod.core.storage.DBWriter; import de.danoeh.antennapod.core.storage.DBWriter;
import de.danoeh.antennapod.core.util.DateUtils;
import de.danoeh.antennapod.core.util.DownloadError; import de.danoeh.antennapod.core.util.DownloadError;
import java.util.ArrayList; import java.util.ArrayList;
@ -46,12 +48,12 @@ public class LocalFeedUpdater {
List<FeedItem> newItems = feed.getItems(); List<FeedItem> newItems = feed.getItems();
for (DocumentFile f : mediaFiles) { for (DocumentFile f : mediaFiles) {
FeedItem found = feedContainsFile(feed, f.getName()); FeedItem oldItem = feedContainsFile(feed, f.getName());
if (found != null) { FeedItem newItem = createFeedItem(feed, f, context);
//TODO make sure the media has not changed (type, duration) if (oldItem == null) {
newItems.add(newItem);
} else { } else {
FeedItem item = createFeedItem(feed, f, context); oldItem.updateFromOther(newItem);
newItems.add(item);
} }
} }
@ -78,15 +80,21 @@ public class LocalFeedUpdater {
} }
private static FeedItem createFeedItem(Feed feed, DocumentFile file, Context context) { private static FeedItem createFeedItem(Feed feed, DocumentFile file, Context context) {
//create item
long globalId = 0;
Date date = new Date();
String uuid = UUID.randomUUID().toString(); 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); 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 //add the media to the item
long duration = getFileDuration(file, context); long duration = Long.parseLong(durationStr);
long size = file.length(); long size = file.length();
FeedMedia media = new FeedMedia(0, item, (int) duration, 0, size, file.getType(), FeedMedia media = new FeedMedia(0, item, (int) duration, 0, size, file.getType(),
file.getUri().toString(), file.getUri().toString(), false, null, 0, 0); file.getUri().toString(), file.getUri().toString(), false, null, 0, 0);
@ -101,11 +109,4 @@ public class LocalFeedUpdater {
DBWriter.addDownloadStatus(status); DBWriter.addDownloadStatus(status);
DBWriter.setFeedLastUpdateFailed(feed.getId(), true); 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);
}
} }