Skip to content

Commit

Permalink
Make download progress updates thread safe
Browse files Browse the repository at this point in the history
Issue: #5978
PiperOrigin-RevId: 308076851
  • Loading branch information
ojw28 authored and icbaker committed Apr 27, 2020
1 parent 25f17ac commit 63db847
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ public void handleMessage(Message message) {
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();
Expand Down Expand Up @@ -1030,9 +1030,8 @@ private void syncRemovingDownload(@Nullable Task activeTask, Download download)

// 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) {
Expand Down Expand Up @@ -1325,7 +1324,13 @@ public void onProgress(long contentLength, long bytesDownloaded, float percentDo
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();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ interface ProgressListener {
/**
* 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.
Expand Down

0 comments on commit 63db847

Please sign in to comment.