diff --git a/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadManager.java b/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadManager.java index 9bd127583b..b6228025cf 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadManager.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadManager.java @@ -666,28 +666,21 @@ public final class DownloadManager { /* package */ static Download mergeRequest( Download download, DownloadRequest request, int stopReason, long nowMs) { - DownloadRequest mergedRequest = download.request.copyWithMergedRequest(request); - @Download.State int state = download.state; // Treat the merge as creating a new download if we're currently removing the existing one, or - // if the existing download has failed. Else treat the merge as updating the existing download. + // if the existing download is in a terminal state. Else treat the merge as updating the + // existing download. long startTimeMs = - state == STATE_REMOVING || state == STATE_FAILED ? nowMs : download.startTimeMs; - - if (state == STATE_COMPLETED - && mergedRequest.streamKeys.size() == download.request.streamKeys.size()) { - // A completed download is still completed if no new streams are added. - state = STATE_COMPLETED; - } else if (state == STATE_REMOVING || state == STATE_RESTARTING) { + state == STATE_REMOVING || download.isTerminalState() ? nowMs : download.startTimeMs; + if (state == STATE_REMOVING || state == STATE_RESTARTING) { state = STATE_RESTARTING; } else if (stopReason != STOP_REASON_NONE) { state = STATE_STOPPED; } else { state = STATE_QUEUED; } - return new Download( - mergedRequest, + download.request.copyWithMergedRequest(request), state, startTimeMs, /* updateTimeMs= */ nowMs, diff --git a/library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadManagerTest.java b/library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadManagerTest.java index 980aa49ac7..1444b0484c 100644 --- a/library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadManagerTest.java +++ b/library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadManagerTest.java @@ -635,45 +635,22 @@ public class DownloadManagerTest { } @Test - public void mergeRequest_completedWithStopReason_mergesNewKeyAndBecomesStopped() { - StreamKey streamKey1 = new StreamKey(/* groupIndex= */ 0, /* trackIndex= */ 0); - StreamKey streamKey2 = new StreamKey(/* groupIndex= */ 0, /* trackIndex= */ 1); - - DownloadRequest downloadRequest1 = createDownloadRequest(ID1, streamKey1); + public void mergeRequest_completedWithStopReason_becomesStopped() { + DownloadRequest downloadRequest = createDownloadRequest(ID1); DownloadBuilder downloadBuilder = - new DownloadBuilder(downloadRequest1) + new DownloadBuilder(downloadRequest) .setState(Download.STATE_COMPLETED) .setStopReason(/* stopReason= */ 1); Download download = downloadBuilder.build(); - DownloadRequest downloadRequest2 = createDownloadRequest(ID1, streamKey2); Download mergedDownload = - DownloadManager.mergeRequest(download, downloadRequest2, download.stopReason, NOW_MS); + DownloadManager.mergeRequest(download, downloadRequest, download.stopReason, NOW_MS); Download expectedDownload = - downloadBuilder - .setStreamKeys(streamKey1, streamKey2) - .setState(Download.STATE_STOPPED) - .build(); + downloadBuilder.setStartTimeMs(NOW_MS).setState(Download.STATE_STOPPED).build(); assertEqualIgnoringUpdateTime(mergedDownload, expectedDownload); } - @Test - public void mergeRequest_completed_staysCompleted() { - StreamKey streamKey = new StreamKey(/* groupIndex= */ 0, /* trackIndex= */ 0); - - DownloadRequest downloadRequest1 = createDownloadRequest(ID1, streamKey); - DownloadBuilder downloadBuilder = - new DownloadBuilder(downloadRequest1).setState(Download.STATE_COMPLETED); - Download download = downloadBuilder.build(); - - DownloadRequest downloadRequest2 = createDownloadRequest(ID1, streamKey); - Download mergedDownload = - DownloadManager.mergeRequest(download, downloadRequest2, /* stopReason= */ 0, NOW_MS); - - assertEqualIgnoringUpdateTime(mergedDownload, download); - } - private void setupDownloadManager(int maxParallelDownloads) throws Exception { if (downloadManager != null) { releaseDownloadManager();