diff --git a/demos/main/src/main/java/com/google/android/exoplayer2/demo/DownloadActivity.java b/demos/main/src/main/java/com/google/android/exoplayer2/demo/DownloadActivity.java index dc2efc911e..daf2df58dd 100644 --- a/demos/main/src/main/java/com/google/android/exoplayer2/demo/DownloadActivity.java +++ b/demos/main/src/main/java/com/google/android/exoplayer2/demo/DownloadActivity.java @@ -204,7 +204,8 @@ public class DownloadActivity extends Activity { public RepresentationItem(Parcelable key, String title, float percentDownloaded) { this.key = key; this.title = title; - this.percentDownloaded = (int) percentDownloaded; + this.percentDownloaded = + (int) (percentDownloaded == C.PERCENTAGE_UNSET ? 0 : percentDownloaded); } @Override diff --git a/library/core/src/main/java/com/google/android/exoplayer2/C.java b/library/core/src/main/java/com/google/android/exoplayer2/C.java index 045f3bfc6e..79de146f29 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/C.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/C.java @@ -64,6 +64,9 @@ public final class C { */ public static final int LENGTH_UNSET = -1; + /** Represents an unset or unknown percentage. */ + public static final int PERCENTAGE_UNSET = -1; + /** * The number of microseconds in one second. */ diff --git a/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadManager.java b/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadManager.java index 85675f6c7e..660b3cfabc 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadManager.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadManager.java @@ -541,14 +541,11 @@ public final class DownloadManager { /** The state of the task. See {@link State}. */ public final @State int state; /** - * The download percentage, or {@link Float#NaN} if it can't be calculated or the task is for - * removing. + * The estimated download percentage, or {@link C#PERCENTAGE_UNSET} if no estimate is available + * or if this is a removal task. */ public final float downloadPercentage; - /** - * The downloaded bytes, or {@link C#LENGTH_UNSET} if it hasn't been calculated yet or the task - * is for removing. - */ + /** The total number of downloaded bytes. */ public final long downloadedBytes; /** If {@link #state} is {@link #STATE_ERROR} then this is the cause, otherwise null. */ public final Throwable error; @@ -648,19 +645,16 @@ public final class DownloadManager { } /** - * Returns the download percentage, or {@link Float#NaN} if it can't be calculated yet. This - * value can be an estimation. + * Returns the estimated download percentage, or {@link C#PERCENTAGE_UNSET} if no estimate is + * available. */ public float getDownloadPercentage() { - return downloader != null ? downloader.getDownloadPercentage() : Float.NaN; + return downloader != null ? downloader.getDownloadPercentage() : C.PERCENTAGE_UNSET; } - /** - * Returns the total number of downloaded bytes, or {@link C#LENGTH_UNSET} if it hasn't been - * calculated yet. - */ + /** Returns the total number of downloaded bytes. */ public long getDownloadedBytes() { - return downloader != null ? downloader.getDownloadedBytes() : C.LENGTH_UNSET; + return downloader != null ? downloader.getDownloadedBytes() : 0; } @Override diff --git a/library/core/src/main/java/com/google/android/exoplayer2/offline/Downloader.java b/library/core/src/main/java/com/google/android/exoplayer2/offline/Downloader.java index 9f4b560ac8..3390a4ed44 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/offline/Downloader.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/offline/Downloader.java @@ -29,8 +29,6 @@ public interface Downloader { * @throws DownloadException Thrown if the media cannot be downloaded. * @throws InterruptedException If the thread has been interrupted. * @throws IOException Thrown when there is an io error while reading from cache. - * @see #getDownloadedBytes() - * @see #getDownloadPercentage() */ void init() throws InterruptedException, IOException; @@ -50,20 +48,12 @@ public interface Downloader { */ void remove() throws InterruptedException; - /** - * Returns the total number of downloaded bytes, or {@link C#LENGTH_UNSET} if it hasn't been - * calculated yet. - * - * @see #init() - */ + /** Returns the total number of downloaded bytes. */ long getDownloadedBytes(); /** - * Returns the download percentage, or {@link Float#NaN} if it can't be calculated yet. This - * value can be an estimation. - * - * @see #init() + * Returns the estimated download percentage, or {@link C#PERCENTAGE_UNSET} if no estimate is + * available. */ float getDownloadPercentage(); - } 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 e7ccc13547..ea38357af5 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 @@ -83,7 +83,8 @@ public final class ProgressiveDownloader implements Downloader { @Override public float getDownloadPercentage() { long contentLength = cachingCounters.contentLength; - return contentLength == C.LENGTH_UNSET ? Float.NaN + return contentLength == C.LENGTH_UNSET + ? C.PERCENTAGE_UNSET : ((cachingCounters.totalCachedBytes() * 100f) / contentLength); } diff --git a/library/core/src/main/java/com/google/android/exoplayer2/offline/SegmentDownloader.java b/library/core/src/main/java/com/google/android/exoplayer2/offline/SegmentDownloader.java index fe98bff5f1..2ef9f73e6e 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/offline/SegmentDownloader.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/offline/SegmentDownloader.java @@ -17,6 +17,7 @@ package com.google.android.exoplayer2.offline; import android.net.Uri; import android.support.annotation.NonNull; +import android.util.Pair; import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.upstream.DataSource; import com.google.android.exoplayer2.upstream.DataSpec; @@ -33,9 +34,6 @@ import java.util.List; /** * Base class for multi segment stream downloaders. * - *
All of the methods are blocking. Also they are not thread safe, except {@link
- * #getTotalSegments()}, {@link #getDownloadedSegments()} and {@link #getDownloadedBytes()}.
- *
* @param Except {@link #getTotalSegments()}, {@link #getDownloadedSegments()} and {@link
- * #getDownloadedBytes()}, this class isn't thread safe.
- *
* Example usage:
*
* Except {@link #getTotalSegments()}, {@link #getDownloadedSegments()} and {@link
- * #getDownloadedBytes()}, this class isn't thread safe.
- *
* Example usage:
*
* , Boolean> getSegments(
DataSource dataSource, M manifest, boolean allowIncompleteIndex)
throws InterruptedException, IOException;
private void resetCounters() {
totalSegments = C.LENGTH_UNSET;
- downloadedSegments = C.LENGTH_UNSET;
- downloadedBytes = C.LENGTH_UNSET;
+ downloadedSegments = 0;
+ downloadedBytes = 0;
}
private void remove(Uri uri) {
@@ -289,9 +252,11 @@ public abstract class SegmentDownloader
, Boolean> result = getSegments(dataSource, filteredManifest, offline);
+ List
{@code
@@ -89,29 +87,32 @@ public final class DashDownloader extends SegmentDownloader
, Boolean> getSegments(
DataSource dataSource, DashManifest manifest, boolean allowIndexLoadErrors)
throws InterruptedException, IOException {
ArrayList
, Boolean>create(segments, segmentListComplete);
}
- private static void addSegmentsForAdaptationSet(
+ private static boolean addSegmentsForAdaptationSet(
DataSource dataSource,
AdaptationSet adaptationSet,
long periodStartUs,
@@ -119,6 +120,7 @@ public final class DashDownloader extends SegmentDownloader
, Boolean> getSegments(
+ DataSource dataSource, HlsMasterPlaylist manifest, boolean allowIndexLoadErrors)
+ throws IOException {
HashSet
{@code
@@ -84,9 +82,8 @@ public final class SsDownloader extends SegmentDownloader
, Boolean> getSegments(
+ DataSource dataSource, SsManifest manifest, boolean allowIndexLoadErrors) throws IOException {
ArrayList
, Boolean>create(segments, true);
}
}
diff --git a/library/ui/src/main/java/com/google/android/exoplayer2/ui/DownloadNotificationUtil.java b/library/ui/src/main/java/com/google/android/exoplayer2/ui/DownloadNotificationUtil.java
index e858b37b24..dec28ad5e0 100644
--- a/library/ui/src/main/java/com/google/android/exoplayer2/ui/DownloadNotificationUtil.java
+++ b/library/ui/src/main/java/com/google/android/exoplayer2/ui/DownloadNotificationUtil.java
@@ -19,6 +19,7 @@ import android.app.Notification;
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
+import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.offline.DownloadManager;
import com.google.android.exoplayer2.offline.DownloadManager.DownloadState;
import com.google.android.exoplayer2.util.ErrorMessageProvider;
@@ -48,31 +49,31 @@ public final class DownloadNotificationUtil {
String channelId,
@Nullable String message) {
float totalPercentage = 0;
- int determinatePercentageCount = 0;
- boolean isAnyDownloadActive = false;
+ int downloadTaskCount = 0;
+ boolean allDownloadPercentagesUnknown = true;
+ boolean haveDownloadedBytes = false;
for (DownloadState downloadState : downloadStates) {
if (downloadState.downloadAction.isRemoveAction
|| downloadState.state != DownloadState.STATE_STARTED) {
continue;
}
- float percentage = downloadState.downloadPercentage;
- if (!Float.isNaN(percentage)) {
- totalPercentage += percentage;
- determinatePercentageCount++;
+ if (downloadState.downloadPercentage != C.PERCENTAGE_UNSET) {
+ allDownloadPercentagesUnknown = false;
+ totalPercentage += downloadState.downloadPercentage;
}
- isAnyDownloadActive = true;
+ haveDownloadedBytes |= downloadState.downloadedBytes > 0;
+ downloadTaskCount++;
}
- int titleStringId = isAnyDownloadActive ? R.string.exo_download_downloading : NULL_STRING_ID;
+ boolean haveDownloadTasks = downloadTaskCount > 0;
+ int titleStringId = haveDownloadTasks ? R.string.exo_download_downloading : NULL_STRING_ID;
NotificationCompat.Builder notificationBuilder =
createNotificationBuilder(context, smallIcon, channelId, message, titleStringId);
+ int progress = haveDownloadTasks ? (int) (totalPercentage / downloadTaskCount) : 0;
+ boolean indeterminate = allDownloadPercentagesUnknown && haveDownloadedBytes;
+ notificationBuilder.setProgress(/* max= */ 100, progress, indeterminate);
notificationBuilder.setOngoing(true);
- int max = 100;
- int progress = (int) (totalPercentage / determinatePercentageCount);
- boolean indeterminate = determinatePercentageCount == 0;
- notificationBuilder.setProgress(max, progress, indeterminate);
-
notificationBuilder.setShowWhen(false);
return notificationBuilder.build();
}