Local feeds: honor file creation date (#4607)
This commit is contained in:
parent
8ad4682aa4
commit
370df3a26c
|
@ -10,12 +10,15 @@ import androidx.documentfile.provider.DocumentFile;
|
|||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
@ -24,6 +27,7 @@ import de.danoeh.antennapod.core.R;
|
|||
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;
|
||||
|
||||
public class LocalFeedUpdater {
|
||||
|
@ -130,12 +134,30 @@ public class LocalFeedUpdater {
|
|||
|
||||
private static FeedItem createFeedItem(Feed feed, DocumentFile file, Context context) {
|
||||
String uuid = UUID.randomUUID().toString();
|
||||
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 dateStr = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DATE);
|
||||
|
||||
Date date = null;
|
||||
if (!TextUtils.isEmpty(dateStr)) {
|
||||
try {
|
||||
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss", Locale.getDefault());
|
||||
date = simpleDateFormat.parse(dateStr);
|
||||
} catch (ParseException parseException) {
|
||||
date = DateUtils.parse(dateStr);
|
||||
if (date == null) {
|
||||
date = new Date(file.lastModified());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
date = new Date(file.lastModified());
|
||||
}
|
||||
|
||||
FeedItem item = new FeedItem(0, file.getName(), uuid, file.getName(), date,
|
||||
FeedItem.UNPLAYED, feed);
|
||||
item.setAutoDownload(false);
|
||||
|
||||
String durationStr = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
|
||||
String title = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);
|
||||
if (!TextUtils.isEmpty(title)) {
|
||||
|
|
|
@ -20,6 +20,9 @@ import org.robolectric.RobolectricTestRunner;
|
|||
import org.robolectric.shadows.ShadowMediaMetadataRetriever;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.List;
|
||||
|
||||
import de.danoeh.antennapod.core.ApplicationCallbacks;
|
||||
|
@ -30,6 +33,7 @@ import de.danoeh.antennapod.core.storage.DBReader;
|
|||
import de.danoeh.antennapod.core.storage.PodDBAdapter;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
@ -149,6 +153,32 @@ public class LocalFeedUpdaterTest {
|
|||
assertTrue(feedAfter.getImageUrl().contains(resourceEntryName));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test default feed metadata.
|
||||
*
|
||||
* @see #mapDummyMetadata Title and PubDate are dummy values.
|
||||
*/
|
||||
@Test
|
||||
public void testUpdateFeed_FeedMetadata() {
|
||||
callUpdateFeed(LOCAL_FEED_DIR1);
|
||||
|
||||
Feed feed = verifySingleFeedInDatabase();
|
||||
List<FeedItem> feedItems = DBReader.getFeedItemList(feed);
|
||||
FeedItem feedItem = feedItems.get(0);
|
||||
|
||||
assertEquals("track1.mp3", feedItem.getTitle());
|
||||
|
||||
Date pubDate = feedItem.getPubDate();
|
||||
Calendar calendar = GregorianCalendar.getInstance();
|
||||
calendar.setTime(pubDate);
|
||||
assertEquals(2020, calendar.get(Calendar.YEAR));
|
||||
assertEquals(6 - 1, calendar.get(Calendar.MONTH));
|
||||
assertEquals(1, calendar.get(Calendar.DAY_OF_MONTH));
|
||||
assertEquals(22, calendar.get(Calendar.HOUR_OF_DAY));
|
||||
assertEquals(23, calendar.get(Calendar.MINUTE));
|
||||
assertEquals(24, calendar.get(Calendar.SECOND));
|
||||
}
|
||||
|
||||
/**
|
||||
* Fill ShadowMediaMetadataRetriever with dummy duration and title.
|
||||
*
|
||||
|
@ -162,6 +192,8 @@ public class LocalFeedUpdaterTest {
|
|||
MediaMetadataRetriever.METADATA_KEY_DURATION, "10");
|
||||
ShadowMediaMetadataRetriever.addMetadata(path,
|
||||
MediaMetadataRetriever.METADATA_KEY_TITLE, fileName);
|
||||
ShadowMediaMetadataRetriever.addMetadata(path,
|
||||
MediaMetadataRetriever.METADATA_KEY_DATE, "20200601T222324");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue