mirror of
https://github.com/AntennaPod/AntennaPod.git
synced 2024-12-23 07:25:25 +01:00
bugfix respect download order - obey user settings "Enqueue Downloaded"
This commit is contained in:
parent
fb6fa010f8
commit
418d4fa4d4
@ -2,26 +2,31 @@ package de.test.antennapod.storage;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import androidx.test.InstrumentationRegistry;
|
||||
import androidx.test.filters.SmallTest;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import androidx.test.InstrumentationRegistry;
|
||||
import androidx.test.filters.LargeTest;
|
||||
import androidx.test.filters.SmallTest;
|
||||
import de.danoeh.antennapod.core.feed.Feed;
|
||||
import de.danoeh.antennapod.core.feed.FeedItem;
|
||||
import de.danoeh.antennapod.core.feed.FeedMedia;
|
||||
import de.danoeh.antennapod.core.preferences.UserPreferences;
|
||||
import de.danoeh.antennapod.core.storage.DBReader;
|
||||
import de.danoeh.antennapod.core.storage.DBTasks;
|
||||
import de.danoeh.antennapod.core.storage.DBWriter;
|
||||
import de.danoeh.antennapod.core.storage.PodDBAdapter;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static java.util.Collections.singletonList;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
@ -179,4 +184,86 @@ public class DBTasksTest {
|
||||
lastDate = item.getPubDate();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddQueueItemsInDownload_EnqueueEnabled() throws Exception {
|
||||
// Setup test data / environment
|
||||
UserPreferences.setEnqueueDownloadedEpisodes(true);
|
||||
UserPreferences.setEnqueueAtFront(false);
|
||||
|
||||
List<FeedItem> fis1 = createSavedFeed("Feed 1", 2).getItems();
|
||||
List<FeedItem> fis2 = createSavedFeed("Feed 2", 3).getItems();
|
||||
|
||||
DBWriter.addQueueItem(context, fis1.get(0), fis2.get(0)).get();
|
||||
// the first item fis1.get(0) is already in the queue
|
||||
FeedItem[] itemsToDownload = new FeedItem[]{ fis1.get(0), fis1.get(1), fis2.get(2), fis2.get(1) };
|
||||
|
||||
// Expectations:
|
||||
List<FeedItem> expectedEnqueued = Arrays.asList(fis1.get(1), fis2.get(2), fis2.get(1));
|
||||
List<FeedItem> expectedQueue = new ArrayList<>();
|
||||
expectedQueue.addAll(DBReader.getQueue());
|
||||
expectedQueue.addAll(expectedEnqueued);
|
||||
|
||||
// Run actual test and assert results
|
||||
List<? extends FeedItem> actualEnqueued =
|
||||
DBTasks.enqueueFeedItemsToDownload(context, itemsToDownload);
|
||||
|
||||
assertEqualsByIds("Only items not in the queue are enqueued", expectedEnqueued, actualEnqueued);
|
||||
assertEqualsByIds("Queue has new items appended", expectedQueue, DBReader.getQueue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddQueueItemsInDownload_EnqueueDisabled() throws Exception {
|
||||
// Setup test data / environment
|
||||
UserPreferences.setEnqueueDownloadedEpisodes(false);
|
||||
|
||||
List<FeedItem> fis1 = createSavedFeed("Feed 1", 2).getItems();
|
||||
List<FeedItem> fis2 = createSavedFeed("Feed 2", 3).getItems();
|
||||
|
||||
DBWriter.addQueueItem(context, fis1.get(0), fis2.get(0)).get();
|
||||
FeedItem[] itemsToDownload = new FeedItem[]{ fis1.get(0), fis1.get(1), fis2.get(2), fis2.get(1) };
|
||||
|
||||
// Expectations:
|
||||
List<FeedItem> expectedEnqueued = Collections.emptyList();
|
||||
List<FeedItem> expectedQueue = DBReader.getQueue();
|
||||
|
||||
// Run actual test and assert results
|
||||
List<? extends FeedItem> actualEnqueued =
|
||||
DBTasks.enqueueFeedItemsToDownload(context, itemsToDownload);
|
||||
|
||||
assertEqualsByIds("No item is enqueued", expectedEnqueued, actualEnqueued);
|
||||
assertEqualsByIds("Queue is unchanged", expectedQueue, DBReader.getQueue());
|
||||
}
|
||||
|
||||
private void assertEqualsByIds(String msg, List<? extends FeedItem> expected, List<? extends FeedItem> actual) {
|
||||
// assert only the IDs, so that any differences are easily to spot.
|
||||
List<Long> expectedIds = toIds(expected);
|
||||
List<Long> actualIds = toIds(actual);
|
||||
assertEquals(msg, expectedIds, actualIds);
|
||||
}
|
||||
|
||||
private static List<Long> toIds(List<? extends FeedItem> items) {
|
||||
return items.stream().map(FeedItem::getId).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private Feed createSavedFeed(String title, int numFeedItems) {
|
||||
final Feed feed = new Feed("url", null, title);
|
||||
|
||||
if (numFeedItems > 0) {
|
||||
List<FeedItem> items = new ArrayList<>(numFeedItems);
|
||||
for (int i = 1; i <= numFeedItems; i++) {
|
||||
FeedItem item = new FeedItem(0, "item " + i + " of " + title, "id", "link",
|
||||
new Date(), FeedItem.UNPLAYED, feed);
|
||||
items.add(item);
|
||||
}
|
||||
feed.setItems(items);
|
||||
}
|
||||
|
||||
PodDBAdapter adapter = PodDBAdapter.getInstance();
|
||||
adapter.open();
|
||||
adapter.setCompleteFeed(feed);
|
||||
adapter.close();
|
||||
return feed;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -4,12 +4,14 @@ import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.res.Configuration;
|
||||
import android.preference.PreferenceManager;
|
||||
import androidx.annotation.IntRange;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.core.app.NotificationCompat;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.IntRange;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.core.app.NotificationCompat;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
|
||||
@ -285,10 +287,24 @@ public class UserPreferences {
|
||||
return prefs.getBoolean(PREF_ENQUEUE_DOWNLOADED, true);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public static void setEnqueueDownloadedEpisodes(boolean enqueueDownloadedEpisodes) {
|
||||
prefs.edit()
|
||||
.putBoolean(PREF_ENQUEUE_DOWNLOADED, enqueueDownloadedEpisodes)
|
||||
.apply();
|
||||
}
|
||||
|
||||
public static boolean enqueueAtFront() {
|
||||
return prefs.getBoolean(PREF_QUEUE_ADD_TO_FRONT, false);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public static void setEnqueueAtFront(boolean enqueueAtFront) {
|
||||
prefs.edit()
|
||||
.putBoolean(PREF_QUEUE_ADD_TO_FRONT, enqueueAtFront)
|
||||
.apply();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return {@code true} if in enqueuing items/podcast episodes, when the existing front item is
|
||||
|
@ -7,6 +7,8 @@ import android.os.Looper;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
@ -26,6 +28,7 @@ import de.danoeh.antennapod.core.feed.Feed;
|
||||
import de.danoeh.antennapod.core.feed.FeedItem;
|
||||
import de.danoeh.antennapod.core.feed.FeedMedia;
|
||||
import de.danoeh.antennapod.core.feed.FeedPreferences;
|
||||
import de.danoeh.antennapod.core.preferences.UserPreferences;
|
||||
import de.danoeh.antennapod.core.service.GpodnetSyncService;
|
||||
import de.danoeh.antennapod.core.service.download.DownloadStatus;
|
||||
import de.danoeh.antennapod.core.service.playback.PlaybackService;
|
||||
@ -331,7 +334,12 @@ public final class DBTasks {
|
||||
}
|
||||
// #2448: First, add to-download items to the queue before actual download
|
||||
// so that the resulting queue order is the same as when download is clicked
|
||||
DBWriter.addQueueItem(context, items);
|
||||
try {
|
||||
enqueueFeedItemsToDownload(context, items);
|
||||
} catch (Throwable t) {
|
||||
throw new DownloadRequestException("Unexpected exception during enqueue before downloads", t);
|
||||
}
|
||||
|
||||
// Then, download them
|
||||
for (FeedItem item : items) {
|
||||
if (item.getMedia() != null
|
||||
@ -358,6 +366,25 @@ public final class DBTasks {
|
||||
}
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public static List<? extends FeedItem> enqueueFeedItemsToDownload(final Context context,
|
||||
FeedItem... items)
|
||||
throws InterruptedException, ExecutionException {
|
||||
List<FeedItem> itemsToEnqueue = new ArrayList<>();
|
||||
if (UserPreferences.enqueueDownloadedEpisodes()) {
|
||||
LongList queueIDList = DBReader.getQueueIDList();
|
||||
for (FeedItem item : items) {
|
||||
if (!queueIDList.contains(item.getId())) {
|
||||
itemsToEnqueue.add(item);
|
||||
}
|
||||
}
|
||||
DBWriter.addQueueItem(context,
|
||||
itemsToEnqueue.toArray(new FeedItem[0]))
|
||||
.get();
|
||||
}
|
||||
return itemsToEnqueue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Looks for undownloaded episodes in the queue or list of unread items and request a download if
|
||||
* 1. Network is available
|
||||
|
Loading…
Reference in New Issue
Block a user