Make download progress updates thread safe

Issue: #5978
PiperOrigin-RevId: 308076851
This commit is contained in:
olly 2020-04-23 18:10:28 +01:00 committed by Ian Baker
parent 25f17acd21
commit 63db847bf6
3 changed files with 15 additions and 6 deletions

View File

@ -734,7 +734,7 @@ public final class DownloadManager {
break;
case MSG_CONTENT_LENGTH_CHANGED:
task = (Task) message.obj;
onContentLengthChanged(task);
onContentLengthChanged(task, Util.toLong(message.arg1, message.arg2));
return; // No need to post back to mainHandler.
case MSG_UPDATE_PROGRESS:
updateProgress();
@ -1030,9 +1030,8 @@ public final class DownloadManager {
// Task event processing.
private void onContentLengthChanged(Task task) {
private void onContentLengthChanged(Task task, long contentLength) {
String downloadId = task.request.id;
long contentLength = task.contentLength;
Download download =
Assertions.checkNotNull(getDownload(downloadId, /* loadFromIndex= */ false));
if (contentLength == download.contentLength || contentLength == C.LENGTH_UNSET) {
@ -1325,7 +1324,13 @@ public final class DownloadManager {
this.contentLength = contentLength;
@Nullable Handler internalHandler = this.internalHandler;
if (internalHandler != null) {
internalHandler.obtainMessage(MSG_CONTENT_LENGTH_CHANGED, this).sendToTarget();
internalHandler
.obtainMessage(
MSG_CONTENT_LENGTH_CHANGED,
(int) (contentLength >> 32),
(int) contentLength,
this)
.sendToTarget();
}
}
}

View File

@ -21,8 +21,8 @@ import com.google.android.exoplayer2.C;
public class DownloadProgress {
/** The number of bytes that have been downloaded. */
public long bytesDownloaded;
public volatile long bytesDownloaded;
/** The percentage that has been downloaded, or {@link C#PERCENTAGE_UNSET} if unknown. */
public float percentDownloaded;
public volatile float percentDownloaded;
}

View File

@ -28,6 +28,10 @@ public interface Downloader {
/**
* Called when progress is made during a download operation.
*
* <p>May be called directly from {@link #download}, or from any other thread used by the
* downloader. In all cases, {@link #download} is guaranteed not to return until after the last
* call to {@link #onProgress} has finished executing.
*
* @param contentLength The length of the content in bytes, or {@link C#LENGTH_UNSET} if
* unknown.
* @param bytesDownloaded The number of bytes that have been downloaded.