Invoke CacheDataSource EventListener.onCacheIgnored once per request

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=193480939
This commit is contained in:
eguven 2018-04-19 02:00:15 -07:00 committed by Oliver Woodman
parent 8bbdb1b143
commit 35e3892dbf

View File

@ -85,10 +85,13 @@ public final class CacheDataSource implements DataSource {
@IntDef({CACHE_IGNORED_REASON_ERROR, CACHE_IGNORED_REASON_UNSET_LENGTH}) @IntDef({CACHE_IGNORED_REASON_ERROR, CACHE_IGNORED_REASON_UNSET_LENGTH})
public @interface CacheIgnoredReason {} public @interface CacheIgnoredReason {}
/** Cache ignored due to a cache related error */ /** Cache not ignored. */
private static final int CACHE_NOT_IGNORED = -1;
/** Cache ignored due to a cache related error. */
public static final int CACHE_IGNORED_REASON_ERROR = 0; public static final int CACHE_IGNORED_REASON_ERROR = 0;
/** Cache ignored due to a request with an unset length */ /** Cache ignored due to a request with an unset length. */
public static final int CACHE_IGNORED_REASON_UNSET_LENGTH = 1; public static final int CACHE_IGNORED_REASON_UNSET_LENGTH = 1;
/** /**
@ -221,8 +224,13 @@ public final class CacheDataSource implements DataSource {
actualUri = loadRedirectedUriOrReturnGivenUri(cache, key, uri); actualUri = loadRedirectedUriOrReturnGivenUri(cache, key, uri);
flags = dataSpec.flags; flags = dataSpec.flags;
readPosition = dataSpec.position; readPosition = dataSpec.position;
currentRequestIgnoresCache = (ignoreCacheOnError && seenCacheError)
|| (dataSpec.length == C.LENGTH_UNSET && ignoreCacheForUnsetLengthRequests); int reason = shouldIgnoreCacheForRequest(dataSpec);
currentRequestIgnoresCache = reason != CACHE_NOT_IGNORED;
if (currentRequestIgnoresCache) {
notifyCacheIgnored(reason);
}
if (dataSpec.length != C.LENGTH_UNSET || currentRequestIgnoresCache) { if (dataSpec.length != C.LENGTH_UNSET || currentRequestIgnoresCache) {
bytesRemaining = dataSpec.length; bytesRemaining = dataSpec.length;
} else { } else {
@ -317,13 +325,6 @@ public final class CacheDataSource implements DataSource {
CacheSpan nextSpan; CacheSpan nextSpan;
if (currentRequestIgnoresCache) { if (currentRequestIgnoresCache) {
nextSpan = null; nextSpan = null;
if (eventListener != null) {
int reason =
ignoreCacheOnError && seenCacheError
? CACHE_IGNORED_REASON_ERROR
: CACHE_IGNORED_REASON_UNSET_LENGTH;
eventListener.onCacheIgnored(reason);
}
} else if (blockOnCache) { } else if (blockOnCache) {
try { try {
nextSpan = cache.startReadWrite(key, readPosition); nextSpan = cache.startReadWrite(key, readPosition);
@ -501,6 +502,22 @@ public final class CacheDataSource implements DataSource {
} }
} }
private int shouldIgnoreCacheForRequest(DataSpec dataSpec) {
if (ignoreCacheOnError && seenCacheError) {
return CACHE_IGNORED_REASON_ERROR;
} else if (ignoreCacheForUnsetLengthRequests && dataSpec.length == C.LENGTH_UNSET) {
return CACHE_IGNORED_REASON_UNSET_LENGTH;
} else {
return CACHE_NOT_IGNORED;
}
}
private void notifyCacheIgnored(@CacheIgnoredReason int reason) {
if (eventListener != null) {
eventListener.onCacheIgnored(reason);
}
}
private void notifyBytesRead() { private void notifyBytesRead() {
if (eventListener != null && totalCachedBytesRead > 0) { if (eventListener != null && totalCachedBytesRead > 0) {
eventListener.onCachedBytesRead(cache.getCacheSpace(), totalCachedBytesRead); eventListener.onCachedBytesRead(cache.getCacheSpace(), totalCachedBytesRead);