Commit 5782bbc6 by eguven Committed by Andrew Lewis

Use DownloadState in DownloadManager

PiperOrigin-RevId: 233587184
parent 399a963e
......@@ -172,11 +172,6 @@ public final class DownloadAction {
return output.toByteArray();
}
/** Returns whether this is an action for the same media as the {@code other}. */
public boolean isSameMedia(DownloadAction other) {
return id.equals(other.id);
}
/** Returns keys of streams to be downloaded. */
public List<StreamKey> getKeys() {
return keys;
......
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.exoplayer2.offline;
import com.google.android.exoplayer2.util.Assertions;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.HashSet;
/** {@link DownloadAction} related utility methods. */
public class DownloadActionUtil {
private DownloadActionUtil() {}
/**
* Merge {@link DownloadAction}s in {@code actionQueue} to minimum number of actions.
*
* <p>All actions must have the same type and must be for the same media.
*
* @param actionQueue Queue of actions. Must not be empty.
* @return The first action in the queue.
*/
public static DownloadAction mergeActions(ArrayDeque<DownloadAction> actionQueue) {
DownloadAction removeAction = null;
DownloadAction downloadAction = null;
HashSet<StreamKey> keys = new HashSet<>();
boolean downloadAllTracks = false;
DownloadAction firstAction = Assertions.checkNotNull(actionQueue.peek());
while (!actionQueue.isEmpty()) {
DownloadAction action = actionQueue.remove();
Assertions.checkState(action.type.equals(firstAction.type));
Assertions.checkState(action.isSameMedia(firstAction));
if (action.isRemoveAction) {
removeAction = action;
downloadAction = null;
keys.clear();
downloadAllTracks = false;
} else {
if (!downloadAllTracks) {
if (action.keys.isEmpty()) {
downloadAllTracks = true;
keys.clear();
} else {
keys.addAll(action.keys);
}
}
downloadAction = action;
}
}
if (removeAction != null) {
actionQueue.add(removeAction);
}
if (downloadAction != null) {
actionQueue.add(
DownloadAction.createDownloadAction(
downloadAction.type,
downloadAction.uri,
new ArrayList<>(keys),
downloadAction.customCacheKey,
downloadAction.data));
}
return Assertions.checkNotNull(actionQueue.peek());
}
}
......@@ -247,7 +247,7 @@ public final class DownloadState {
type,
action.uri,
action.customCacheKey,
getNextState(action, state),
getNextState(state, action.isRemoveAction),
/* downloadPercentage= */ C.PERCENTAGE_UNSET,
downloadedBytes,
/* totalBytes= */ C.LENGTH_UNSET,
......@@ -256,25 +256,25 @@ public final class DownloadState {
notMetRequirements,
manualStopReason,
startTimeMs,
updateTimeMs,
/* updateTimeMs= */ System.currentTimeMillis(),
mergeStreamKeys(this, action),
action.data);
}
private static int getNextState(DownloadAction action, int currentState) {
int newState;
if (action.isRemoveAction) {
newState = STATE_REMOVING;
private static int getNextState(int currentState, boolean remove) {
int nextState;
if (remove) {
nextState = STATE_REMOVING;
} else {
if (currentState == STATE_REMOVING || currentState == STATE_RESTARTING) {
newState = STATE_RESTARTING;
nextState = STATE_RESTARTING;
} else if (currentState == STATE_STOPPED) {
newState = STATE_STOPPED;
nextState = STATE_STOPPED;
} else {
newState = STATE_QUEUED;
nextState = STATE_QUEUED;
}
}
return newState;
return nextState;
}
private static StreamKey[] mergeStreamKeys(DownloadState downloadState, DownloadAction action) {
......
......@@ -66,38 +66,38 @@ public class DownloadActionTest {
}
@Test
public void testSameUri_IsSameMedia() {
public void testSameUri_hasSameId() {
DownloadAction action1 = createDownloadAction(uri1);
DownloadAction action2 = createDownloadAction(uri1);
assertThat(action1.isSameMedia(action2)).isTrue();
assertThat(action1.id.equals(action2.id)).isTrue();
}
@Test
public void testSameUriDifferentAction_IsSameMedia() {
public void testSameUriDifferentAction_hasSameId() {
DownloadAction action1 = createDownloadAction(uri1);
DownloadAction action2 = createRemoveAction(uri1);
assertThat(action1.isSameMedia(action2)).isTrue();
assertThat(action1.id.equals(action2.id)).isTrue();
}
@Test
public void testDifferentUri_IsNotSameMedia() {
DownloadAction action1 = createDownloadAction(uri1);
DownloadAction action2 = createDownloadAction(uri2);
assertThat(action1.isSameMedia(action2)).isFalse();
assertThat(action1.id.equals(action2.id)).isFalse();
}
@Test
public void testSameCacheKeyDifferentUri_IsSameMedia() {
public void testSameCacheKeyDifferentUri_hasSameId() {
DownloadAction action1 = DownloadAction.createRemoveAction(TYPE_DASH, uri1, "key123");
DownloadAction action2 = DownloadAction.createRemoveAction(TYPE_DASH, uri2, "key123");
assertThat(action1.isSameMedia(action2)).isTrue();
assertThat(action1.id.equals(action2.id)).isTrue();
}
@Test
public void testDifferentCacheDifferentUri_IsNotSameMedia() {
public void testDifferentCacheKeyDifferentUri_hasDifferentId() {
DownloadAction action1 = DownloadAction.createRemoveAction(TYPE_DASH, uri1, "key123");
DownloadAction action2 = DownloadAction.createRemoveAction(TYPE_DASH, uri2, "key456");
assertThat(action1.isSameMedia(action2)).isFalse();
assertThat(action1.id.equals(action2.id)).isFalse();
}
@SuppressWarnings("EqualsWithItself")
......
......@@ -252,21 +252,6 @@ public class DownloadManagerTest {
}
@Test
public void secondSameDownloadActionIgnored() throws Throwable {
DownloadRunner runner = new DownloadRunner(uri1);
FakeDownloader downloader1 = runner.getDownloader(0);
runner.postDownloadAction();
downloader1.assertStarted();
runner.postDownloadAction();
downloader1.unblock().assertNotCanceled();
runner.getTask().assertCompleted();
runner.assertCreatedDownloaderCount(1);
downloadManagerListener.blockUntilTasksCompleteAndThrowAnyDownloadError();
}
@Test
public void differentDownloadActionsMerged() throws Throwable {
DownloadRunner runner = new DownloadRunner(uri1);
FakeDownloader downloader1 = runner.getDownloader(0);
......
......@@ -276,10 +276,16 @@ public class DownloadStateTest {
}
static void assertEqual(DownloadState downloadState, DownloadState expected) {
assertThat(areEqual(downloadState, expected)).isTrue();
assertEqual(downloadState, expected, false);
}
private static boolean areEqual(DownloadState downloadState, DownloadState that) {
static void assertEqual(
DownloadState downloadState, DownloadState expected, boolean compareTimeFields) {
assertThat(areEqual(downloadState, expected, compareTimeFields)).isTrue();
}
private static boolean areEqual(
DownloadState downloadState, DownloadState that, boolean compareTimeFields) {
if (downloadState.state != that.state) {
return false;
}
......@@ -292,11 +298,13 @@ public class DownloadStateTest {
if (downloadState.totalBytes != that.totalBytes) {
return false;
}
if (downloadState.startTimeMs != that.startTimeMs) {
return false;
}
if (downloadState.updateTimeMs != that.updateTimeMs) {
return false;
if (compareTimeFields) {
if (downloadState.startTimeMs != that.startTimeMs) {
return false;
}
if (downloadState.updateTimeMs != that.updateTimeMs) {
return false;
}
}
if (downloadState.failureReason != that.failureReason) {
return false;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment