From 35e3892dbf96e733c5b4dcf503e1a921ca3a83c9 Mon Sep 17 00:00:00 2001 From: eguven Date: Thu, 19 Apr 2018 02:00:15 -0700 Subject: [PATCH] Invoke CacheDataSource EventListener.onCacheIgnored once per request ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=193480939 --- .../upstream/cache/CacheDataSource.java | 39 +++++++++++++------ 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheDataSource.java b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheDataSource.java index 6de48cb9d2..fa0e909671 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheDataSource.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheDataSource.java @@ -85,10 +85,13 @@ public final class CacheDataSource implements DataSource { @IntDef({CACHE_IGNORED_REASON_ERROR, CACHE_IGNORED_REASON_UNSET_LENGTH}) 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; - /** 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; /** @@ -221,8 +224,13 @@ public final class CacheDataSource implements DataSource { actualUri = loadRedirectedUriOrReturnGivenUri(cache, key, uri); flags = dataSpec.flags; 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) { bytesRemaining = dataSpec.length; } else { @@ -317,13 +325,6 @@ public final class CacheDataSource implements DataSource { CacheSpan nextSpan; if (currentRequestIgnoresCache) { nextSpan = null; - if (eventListener != null) { - int reason = - ignoreCacheOnError && seenCacheError - ? CACHE_IGNORED_REASON_ERROR - : CACHE_IGNORED_REASON_UNSET_LENGTH; - eventListener.onCacheIgnored(reason); - } } else if (blockOnCache) { try { 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() { if (eventListener != null && totalCachedBytesRead > 0) { eventListener.onCachedBytesRead(cache.getCacheSpace(), totalCachedBytesRead);