Skip to content

Commit

Permalink
Merge branch 'enqueue_respect_download_start_order2_2448' into enqueu…
Browse files Browse the repository at this point in the history
…e_keep_inprogress_front_2652_respect_download_start_order_2448
  • Loading branch information
orionlee committed May 27, 2018
2 parents a4d7427 + bc09eda commit 00d2251
Show file tree
Hide file tree
Showing 5 changed files with 269 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,10 @@ public void run() {

}.start();
}
// #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);
// Then, download them
for (FeedItem item : items) {
if (item.getMedia() != null
&& !requester.isDownloadingFile(item.getMedia())
Expand Down
43 changes: 39 additions & 4 deletions core/src/main/java/de/danoeh/antennapod/core/storage/DBWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import de.danoeh.antennapod.core.feed.EventDistributor;
import de.danoeh.antennapod.core.feed.Feed;
import de.danoeh.antennapod.core.feed.FeedEvent;
import de.danoeh.antennapod.core.feed.FeedFile;
import de.danoeh.antennapod.core.feed.FeedImage;
import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.feed.FeedMedia;
Expand Down Expand Up @@ -384,7 +385,7 @@ public static Future<?> addQueueItem(final Context context, final boolean perfor
new ItemEnqueuePositionCalculator.Options()
.setEnqueueAtFront(UserPreferences.enqueueAtFront())
.setKeepInProgressAtFront(UserPreferences.keepInProgressAtFront())
);
);

for (int i = 0; i < itemIds.length; i++) {
if (!itemListContains(queue, itemIds[i])) {
Expand Down Expand Up @@ -452,6 +453,12 @@ public Options setKeepInProgressAtFront(boolean keepInProgressAtFront) {

private final @NonNull Options options;

/**
* The logic needs to use {@link DownloadRequester#isDownloadingFile(FeedFile)} method only
*/
@VisibleForTesting
FeedFileDownloadStatusRequesterInterface requester = DownloadRequester.getInstance();

public ItemEnqueuePositionCalculator(@NonNull Options options) {
this.options = options;
}
Expand All @@ -473,16 +480,44 @@ public int calcPosition(int positionAmongToAdd, FeedItem item, List<FeedItem> cu
curQueue.size() > 0 &&
curQueue.get(0).getMedia() != null &&
curQueue.get(0).getMedia().isInProgress()) {
return positionAmongToAdd + 1; // leave the front in progress item at the front
// leave the front in progress item at the front
return getPositionOf1stNonDownloadingItem(positionAmongToAdd + 1, curQueue);
} else { // typical case
// return NOT 0, so that when a list of items are inserted, the items inserted
// keep the same order. Returning 0 will reverse the order
return positionAmongToAdd;
return getPositionOf1stNonDownloadingItem(positionAmongToAdd, curQueue);
}
} else {
return curQueue.size();
}
}

private int getPositionOf1stNonDownloadingItem(int startPosition, List<FeedItem> curQueue) {
final int curQueueSize = curQueue.size();
for (int i = startPosition; i < curQueueSize; i++) {
if (!isItemAtPositionDownloading(i, curQueue)) {
return i;
} // else continue to search;
}
return curQueueSize;
}

private boolean isItemAtPositionDownloading(int position, List<FeedItem> curQueue) {
FeedItem curItem;
try {
curItem = curQueue.get(position);
} catch (IndexOutOfBoundsException e) {
curItem = null;
}

if (curItem != null &&
curItem.getMedia() != null &&
requester.isDownloadingFile(curItem.getMedia())) {
return true;
} else {
return false;
}
}
}

/**
Expand Down Expand Up @@ -603,7 +638,7 @@ public static Future<?> moveQueueItemToBottom(final long itemId,
int index = queueIdList.indexOf(itemId);
if (index >= 0) {
moveQueueItemHelper(index, queueIdList.size() - 1,
broadcastUpdate);
broadcastUpdate);
} else {
Log.e(TAG, "moveQueueItemToBottom: item not found");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* Sends download requests to the DownloadService. This class should always be used for starting downloads,
* otherwise they won't work correctly.
*/
public class DownloadRequester {
public class DownloadRequester implements FeedFileDownloadStatusRequesterInterface {
private static final String TAG = "DownloadRequester";

public static final String IMAGE_DOWNLOADPATH = "images/";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package de.danoeh.antennapod.core.storage;

import android.support.annotation.NonNull;

import de.danoeh.antennapod.core.feed.FeedFile;

public interface FeedFileDownloadStatusRequesterInterface {
/**
* @return {@code true} if the named feedfile is in the downloads list
*/
boolean isDownloadingFile(@NonNull FeedFile item);

}
Loading

0 comments on commit 00d2251

Please sign in to comment.