From d5b19694faca3ecb73c40601760ddcac564f4615 Mon Sep 17 00:00:00 2001 From: eguven Date: Tue, 2 Apr 2019 15:34:18 +0100 Subject: [PATCH] Add percentage field to CacheUtil.CachingCounters PiperOrigin-RevId: 241524766 --- .../exoplayer2/offline/ProgressiveDownloader.java | 5 +---- .../exoplayer2/upstream/cache/CacheUtil.java | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/library/core/src/main/java/com/google/android/exoplayer2/offline/ProgressiveDownloader.java b/library/core/src/main/java/com/google/android/exoplayer2/offline/ProgressiveDownloader.java index e712f9ddc3..8cf5820fa6 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/offline/ProgressiveDownloader.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/offline/ProgressiveDownloader.java @@ -103,10 +103,7 @@ public final class ProgressiveDownloader implements Downloader { @Override public float getDownloadPercentage() { - long contentLength = cachingCounters.contentLength; - return contentLength == C.LENGTH_UNSET - ? C.PERCENTAGE_UNSET - : ((cachingCounters.totalCachedBytes() * 100f) / contentLength); + return cachingCounters.percentage; } @Override diff --git a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheUtil.java b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheUtil.java index e41fa98eeb..96561260ed 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheUtil.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheUtil.java @@ -42,6 +42,8 @@ public final class CacheUtil { public volatile long newlyCachedBytes; /** The length of the content being cached in bytes, or {@link C#LENGTH_UNSET} if unknown. */ public volatile long contentLength = C.LENGTH_UNSET; + /** The percentage of cached data, or {@link C#PERCENTAGE_UNSET} if unavailable. */ + public volatile float percentage; /** * Returns the sum of {@link #alreadyCachedBytes} and {@link #newlyCachedBytes}. @@ -49,6 +51,16 @@ public final class CacheUtil { public long totalCachedBytes() { return alreadyCachedBytes + newlyCachedBytes; } + + /** Updates {@link #percentage} value using other values. */ + public void updatePercentage() { + // Take local snapshot of the volatile field + long contentLength = this.contentLength; + percentage = + contentLength == C.LENGTH_UNSET + ? C.PERCENTAGE_UNSET + : ((totalCachedBytes() * 100f) / contentLength); + } } /** Default buffer size to be used while caching. */ @@ -105,6 +117,7 @@ public final class CacheUtil { start += blockLength; left -= left == C.LENGTH_UNSET ? 0 : blockLength; } + counters.updatePercentage(); } /** @@ -293,6 +306,7 @@ public final class CacheUtil { } totalRead += read; counters.newlyCachedBytes += read; + counters.updatePercentage(); } return totalRead; } catch (PriorityTaskManager.PriorityTooLowException exception) {