mirror of
https://github.com/androidx/media.git
synced 2025-04-30 06:46:50 +08:00
Add failureReason to DownloadState
PiperOrigin-RevId: 225154121
This commit is contained in:
parent
4bf42bd2ad
commit
8cedfc46fb
@ -15,6 +15,8 @@
|
|||||||
*/
|
*/
|
||||||
package com.google.android.exoplayer2.offline;
|
package com.google.android.exoplayer2.offline;
|
||||||
|
|
||||||
|
import static com.google.android.exoplayer2.offline.DownloadManager.DownloadState.FAILURE_REASON_NONE;
|
||||||
|
import static com.google.android.exoplayer2.offline.DownloadManager.DownloadState.FAILURE_REASON_UNKNOWN;
|
||||||
import static com.google.android.exoplayer2.offline.DownloadManager.DownloadState.STATE_COMPLETED;
|
import static com.google.android.exoplayer2.offline.DownloadManager.DownloadState.STATE_COMPLETED;
|
||||||
import static com.google.android.exoplayer2.offline.DownloadManager.DownloadState.STATE_FAILED;
|
import static com.google.android.exoplayer2.offline.DownloadManager.DownloadState.STATE_FAILED;
|
||||||
import static com.google.android.exoplayer2.offline.DownloadManager.DownloadState.STATE_QUEUED;
|
import static com.google.android.exoplayer2.offline.DownloadManager.DownloadState.STATE_QUEUED;
|
||||||
@ -461,6 +463,16 @@ public final class DownloadManager {
|
|||||||
/** The download failed. */
|
/** The download failed. */
|
||||||
public static final int STATE_FAILED = 3;
|
public static final int STATE_FAILED = 3;
|
||||||
|
|
||||||
|
/** Failure reasons. Either {@link #FAILURE_REASON_NONE} or {@link #FAILURE_REASON_UNKNOWN}. */
|
||||||
|
@Documented
|
||||||
|
@Retention(RetentionPolicy.SOURCE)
|
||||||
|
@IntDef({FAILURE_REASON_NONE, FAILURE_REASON_UNKNOWN})
|
||||||
|
public @interface FailureReason {}
|
||||||
|
/** The download isn't failed. */
|
||||||
|
public static final int FAILURE_REASON_NONE = 0;
|
||||||
|
/** The download is failed because of unknown reason. */
|
||||||
|
public static final int FAILURE_REASON_UNKNOWN = 1;
|
||||||
|
|
||||||
/** Returns the state string for the given state value. */
|
/** Returns the state string for the given state value. */
|
||||||
public static String getStateString(@State int state) {
|
public static String getStateString(@State int state) {
|
||||||
switch (state) {
|
switch (state) {
|
||||||
@ -477,6 +489,18 @@ public final class DownloadManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Returns the failure string for the given failure reason value. */
|
||||||
|
public static String getFailureString(@FailureReason int failureReason) {
|
||||||
|
switch (failureReason) {
|
||||||
|
case FAILURE_REASON_NONE:
|
||||||
|
return "NO_REASON";
|
||||||
|
case FAILURE_REASON_UNKNOWN:
|
||||||
|
return "UNKNOWN_REASON";
|
||||||
|
default:
|
||||||
|
throw new IllegalStateException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** The unique download id. */
|
/** The unique download id. */
|
||||||
public final int id;
|
public final int id;
|
||||||
/** The action being executed. */
|
/** The action being executed. */
|
||||||
@ -493,8 +517,11 @@ public final class DownloadManager {
|
|||||||
/** The total size of the media, or {@link C#LENGTH_UNSET} if unknown. */
|
/** The total size of the media, or {@link C#LENGTH_UNSET} if unknown. */
|
||||||
public final long totalBytes;
|
public final long totalBytes;
|
||||||
|
|
||||||
/** If {@link #state} is {@link #STATE_FAILED} then this is the cause, otherwise null. */
|
/**
|
||||||
@Nullable public final Throwable error;
|
* If {@link #state} is {@link #STATE_FAILED} then this is the cause, otherwise {@link
|
||||||
|
* #FAILURE_REASON_NONE}.
|
||||||
|
*/
|
||||||
|
@FailureReason public final int failureReason;
|
||||||
|
|
||||||
private DownloadState(
|
private DownloadState(
|
||||||
int id,
|
int id,
|
||||||
@ -503,14 +530,16 @@ public final class DownloadManager {
|
|||||||
float downloadPercentage,
|
float downloadPercentage,
|
||||||
long downloadedBytes,
|
long downloadedBytes,
|
||||||
long totalBytes,
|
long totalBytes,
|
||||||
@Nullable Throwable error) {
|
@FailureReason int failureReason) {
|
||||||
|
Assertions.checkState(
|
||||||
|
failureReason == FAILURE_REASON_NONE ? state != STATE_FAILED : state == STATE_FAILED);
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.action = action;
|
this.action = action;
|
||||||
this.state = state;
|
this.state = state;
|
||||||
this.downloadPercentage = downloadPercentage;
|
this.downloadPercentage = downloadPercentage;
|
||||||
this.downloadedBytes = downloadedBytes;
|
this.downloadedBytes = downloadedBytes;
|
||||||
this.totalBytes = totalBytes;
|
this.totalBytes = totalBytes;
|
||||||
this.error = error;
|
this.failureReason = failureReason;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -539,7 +568,7 @@ public final class DownloadManager {
|
|||||||
|
|
||||||
@MonotonicNonNull private Downloader downloader;
|
@MonotonicNonNull private Downloader downloader;
|
||||||
@MonotonicNonNull private DownloadThread downloadThread;
|
@MonotonicNonNull private DownloadThread downloadThread;
|
||||||
@MonotonicNonNull private Throwable error;
|
@MonotonicNonNull @DownloadState.FailureReason private int failureReason;
|
||||||
|
|
||||||
private Download(
|
private Download(
|
||||||
int id,
|
int id,
|
||||||
@ -586,7 +615,7 @@ public final class DownloadManager {
|
|||||||
totalBytes = downloader.getTotalBytes();
|
totalBytes = downloader.getTotalBytes();
|
||||||
}
|
}
|
||||||
return new DownloadState(
|
return new DownloadState(
|
||||||
id, action, state, downloadPercentage, downloadedBytes, totalBytes, error);
|
id, action, state, downloadPercentage, downloadedBytes, totalBytes, failureReason);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns whether the download is finished. */
|
/** Returns whether the download is finished. */
|
||||||
@ -642,11 +671,11 @@ public final class DownloadManager {
|
|||||||
|
|
||||||
private void onDownloadThreadStopped(@Nullable Throwable finalError) {
|
private void onDownloadThreadStopped(@Nullable Throwable finalError) {
|
||||||
state = targetState;
|
state = targetState;
|
||||||
error = null;
|
failureReason = FAILURE_REASON_NONE;
|
||||||
if (targetState == STATE_COMPLETED) {
|
if (targetState == STATE_COMPLETED) {
|
||||||
if (finalError != null) {
|
if (finalError != null) {
|
||||||
state = STATE_FAILED;
|
state = STATE_FAILED;
|
||||||
error = finalError;
|
failureReason = FAILURE_REASON_UNKNOWN;
|
||||||
} else {
|
} else {
|
||||||
actionQueue.remove();
|
actionQueue.remove();
|
||||||
if (!actionQueue.isEmpty()) {
|
if (!actionQueue.isEmpty()) {
|
||||||
|
@ -34,7 +34,7 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen
|
|||||||
private final HashMap<Integer, ArrayBlockingQueue<Integer>> actionStates;
|
private final HashMap<Integer, ArrayBlockingQueue<Integer>> actionStates;
|
||||||
|
|
||||||
private CountDownLatch downloadFinishedCondition;
|
private CountDownLatch downloadFinishedCondition;
|
||||||
private Throwable downloadError;
|
@DownloadState.FailureReason private int failureReason;
|
||||||
|
|
||||||
public TestDownloadManagerListener(
|
public TestDownloadManagerListener(
|
||||||
DownloadManager downloadManager, DummyMainThread dummyMainThread) {
|
DownloadManager downloadManager, DummyMainThread dummyMainThread) {
|
||||||
@ -48,7 +48,7 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void clearDownloadError() {
|
public void clearDownloadError() {
|
||||||
this.downloadError = null;
|
this.failureReason = DownloadState.FAILURE_REASON_NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -58,8 +58,8 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDownloadStateChanged(DownloadManager downloadManager, DownloadState downloadState) {
|
public void onDownloadStateChanged(DownloadManager downloadManager, DownloadState downloadState) {
|
||||||
if (downloadState.state == DownloadState.STATE_FAILED && downloadError == null) {
|
if (downloadState.state == DownloadState.STATE_FAILED) {
|
||||||
downloadError = downloadState.error;
|
failureReason = downloadState.failureReason;
|
||||||
}
|
}
|
||||||
getStateQueue(downloadState.id).add(downloadState.state);
|
getStateQueue(downloadState.id).add(downloadState.state);
|
||||||
}
|
}
|
||||||
@ -77,8 +77,8 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen
|
|||||||
*/
|
*/
|
||||||
public void blockUntilTasksCompleteAndThrowAnyDownloadError() throws Throwable {
|
public void blockUntilTasksCompleteAndThrowAnyDownloadError() throws Throwable {
|
||||||
blockUntilTasksComplete();
|
blockUntilTasksComplete();
|
||||||
if (downloadError != null) {
|
if (failureReason != DownloadState.FAILURE_REASON_NONE) {
|
||||||
throw new Exception(downloadError);
|
throw new Exception("Failure reason: " + DownloadState.getFailureString(failureReason));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user