Merge pull request #7479 from sravan1213:dev-v2

PiperOrigin-RevId: 315794031
This commit is contained in:
Oliver Woodman 2020-06-11 10:11:30 +01:00
commit 2fcd759edb
6 changed files with 50 additions and 21 deletions

View File

@ -149,6 +149,8 @@
initialization vector used to encrypt the cache contents. initialization vector used to encrypt the cache contents.
* Add `Requirements.DEVICE_STORAGE_NOT_LOW`, which can be specified as a * Add `Requirements.DEVICE_STORAGE_NOT_LOW`, which can be specified as a
requirement to a `DownloadManager` for it to proceed with downloading. requirement to a `DownloadManager` for it to proceed with downloading.
* For failed downloads, propagate the `Exception` that caused the failure
to `DownloadManager.Listener.onDownloadChanged`.
* Audio: * Audio:
* Add a sample count parameter to `MediaCodecRenderer.processOutputBuffer` * Add a sample count parameter to `MediaCodecRenderer.processOutputBuffer`
and `AudioSink.handleBuffer` to allow batching multiple encoded frames and `AudioSink.handleBuffer` to allow batching multiple encoded frames

View File

@ -20,6 +20,7 @@ import static com.google.android.exoplayer2.demo.DemoApplication.DOWNLOAD_NOTIFI
import android.app.Notification; import android.app.Notification;
import android.content.Context; import android.content.Context;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.offline.Download; import com.google.android.exoplayer2.offline.Download;
import com.google.android.exoplayer2.offline.DownloadManager; import com.google.android.exoplayer2.offline.DownloadManager;
import com.google.android.exoplayer2.offline.DownloadService; import com.google.android.exoplayer2.offline.DownloadService;
@ -94,7 +95,8 @@ public class DemoDownloadService extends DownloadService {
} }
@Override @Override
public void onDownloadChanged(@NonNull DownloadManager manager, @NonNull Download download) { public void onDownloadChanged(
DownloadManager downloadManager, Download download, @Nullable Exception finalException) {
Notification notification; Notification notification;
if (download.state == Download.STATE_COMPLETED) { if (download.state == Download.STATE_COMPLETED) {
notification = notification =

View File

@ -126,7 +126,9 @@ public class DownloadTracker {
@Override @Override
public void onDownloadChanged( public void onDownloadChanged(
@NonNull DownloadManager downloadManager, @NonNull Download download) { @NonNull DownloadManager downloadManager,
@NonNull Download download,
@Nullable Exception finalException) {
downloads.put(download.request.uri, download); downloads.put(download.request.uri, download);
for (Listener listener : listeners) { for (Listener listener : listeners) {
listener.onDownloadsChanged(); listener.onDownloadsChanged();

View File

@ -93,8 +93,11 @@ public final class DownloadManager {
* *
* @param downloadManager The reporting instance. * @param downloadManager The reporting instance.
* @param download The state of the download. * @param download The state of the download.
* @param finalException If the download is transitioning to {@link Download#STATE_FAILED}, this
* is the final exception that resulted in the failure.
*/ */
default void onDownloadChanged(DownloadManager downloadManager, Download download) {} default void onDownloadChanged(
DownloadManager downloadManager, Download download, @Nullable Exception finalException) {}
/** /**
* Called when a download is removed. * Called when a download is removed.
@ -614,7 +617,7 @@ public final class DownloadManager {
} }
} else { } else {
for (Listener listener : listeners) { for (Listener listener : listeners) {
listener.onDownloadChanged(this, updatedDownload); listener.onDownloadChanged(this, updatedDownload, update.finalException);
} }
} }
if (waitingForRequirementsChanged) { if (waitingForRequirementsChanged) {
@ -906,7 +909,8 @@ public final class DownloadManager {
ArrayList<Download> updateList = new ArrayList<>(downloads); ArrayList<Download> updateList = new ArrayList<>(downloads);
for (int i = 0; i < downloads.size(); i++) { for (int i = 0; i < downloads.size(); i++) {
DownloadUpdate update = DownloadUpdate update =
new DownloadUpdate(downloads.get(i), /* isRemove= */ false, updateList); new DownloadUpdate(
downloads.get(i), /* isRemove= */ false, updateList, /* finalException= */ null);
mainHandler.obtainMessage(MSG_DOWNLOAD_UPDATE, update).sendToTarget(); mainHandler.obtainMessage(MSG_DOWNLOAD_UPDATE, update).sendToTarget();
} }
syncTasks(); syncTasks();
@ -1073,9 +1077,9 @@ public final class DownloadManager {
return; return;
} }
@Nullable Throwable finalError = task.finalError; @Nullable Exception finalException = task.finalException;
if (finalError != null) { if (finalException != null) {
Log.e(TAG, "Task failed: " + task.request + ", " + isRemove, finalError); Log.e(TAG, "Task failed: " + task.request + ", " + isRemove, finalException);
} }
Download download = Download download =
@ -1083,7 +1087,7 @@ public final class DownloadManager {
switch (download.state) { switch (download.state) {
case STATE_DOWNLOADING: case STATE_DOWNLOADING:
Assertions.checkState(!isRemove); Assertions.checkState(!isRemove);
onDownloadTaskStopped(download, finalError); onDownloadTaskStopped(download, finalException);
break; break;
case STATE_REMOVING: case STATE_REMOVING:
case STATE_RESTARTING: case STATE_RESTARTING:
@ -1101,16 +1105,16 @@ public final class DownloadManager {
syncTasks(); syncTasks();
} }
private void onDownloadTaskStopped(Download download, @Nullable Throwable finalError) { private void onDownloadTaskStopped(Download download, @Nullable Exception finalException) {
download = download =
new Download( new Download(
download.request, download.request,
finalError == null ? STATE_COMPLETED : STATE_FAILED, finalException == null ? STATE_COMPLETED : STATE_FAILED,
download.startTimeMs, download.startTimeMs,
/* updateTimeMs= */ System.currentTimeMillis(), /* updateTimeMs= */ System.currentTimeMillis(),
download.contentLength, download.contentLength,
download.stopReason, download.stopReason,
finalError == null ? FAILURE_REASON_NONE : FAILURE_REASON_UNKNOWN, finalException == null ? FAILURE_REASON_NONE : FAILURE_REASON_UNKNOWN,
download.progress); download.progress);
// The download is now in a terminal state, so should not be in the downloads list. // The download is now in a terminal state, so should not be in the downloads list.
downloads.remove(getDownloadIndex(download.request.id)); downloads.remove(getDownloadIndex(download.request.id));
@ -1121,7 +1125,8 @@ public final class DownloadManager {
Log.e(TAG, "Failed to update index.", e); Log.e(TAG, "Failed to update index.", e);
} }
DownloadUpdate update = DownloadUpdate update =
new DownloadUpdate(download, /* isRemove= */ false, new ArrayList<>(downloads)); new DownloadUpdate(
download, /* isRemove= */ false, new ArrayList<>(downloads), finalException);
mainHandler.obtainMessage(MSG_DOWNLOAD_UPDATE, update).sendToTarget(); mainHandler.obtainMessage(MSG_DOWNLOAD_UPDATE, update).sendToTarget();
} }
@ -1139,7 +1144,11 @@ public final class DownloadManager {
Log.e(TAG, "Failed to remove from database"); Log.e(TAG, "Failed to remove from database");
} }
DownloadUpdate update = DownloadUpdate update =
new DownloadUpdate(download, /* isRemove= */ true, new ArrayList<>(downloads)); new DownloadUpdate(
download,
/* isRemove= */ true,
new ArrayList<>(downloads),
/* finalException= */ null);
mainHandler.obtainMessage(MSG_DOWNLOAD_UPDATE, update).sendToTarget(); mainHandler.obtainMessage(MSG_DOWNLOAD_UPDATE, update).sendToTarget();
} }
} }
@ -1194,7 +1203,11 @@ public final class DownloadManager {
Log.e(TAG, "Failed to update index.", e); Log.e(TAG, "Failed to update index.", e);
} }
DownloadUpdate update = DownloadUpdate update =
new DownloadUpdate(download, /* isRemove= */ false, new ArrayList<>(downloads)); new DownloadUpdate(
download,
/* isRemove= */ false,
new ArrayList<>(downloads),
/* finalException= */ null);
mainHandler.obtainMessage(MSG_DOWNLOAD_UPDATE, update).sendToTarget(); mainHandler.obtainMessage(MSG_DOWNLOAD_UPDATE, update).sendToTarget();
return download; return download;
} }
@ -1252,7 +1265,7 @@ public final class DownloadManager {
@Nullable private volatile InternalHandler internalHandler; @Nullable private volatile InternalHandler internalHandler;
private volatile boolean isCanceled; private volatile boolean isCanceled;
@Nullable private Throwable finalError; @Nullable private Exception finalException;
private long contentLength; private long contentLength;
@ -1317,8 +1330,10 @@ public final class DownloadManager {
} }
} }
} }
} catch (Throwable e) { } catch (InterruptedException e) {
finalError = e; // The task was canceled. Do nothing.
} catch (Exception e) {
finalException = e;
} }
@Nullable Handler internalHandler = this.internalHandler; @Nullable Handler internalHandler = this.internalHandler;
if (internalHandler != null) { if (internalHandler != null) {
@ -1355,11 +1370,17 @@ public final class DownloadManager {
public final Download download; public final Download download;
public final boolean isRemove; public final boolean isRemove;
public final List<Download> downloads; public final List<Download> downloads;
@Nullable public final Exception finalException;
public DownloadUpdate(Download download, boolean isRemove, List<Download> downloads) { public DownloadUpdate(
Download download,
boolean isRemove,
List<Download> downloads,
@Nullable Exception finalException) {
this.download = download; this.download = download;
this.isRemove = isRemove; this.isRemove = isRemove;
this.downloads = downloads; this.downloads = downloads;
this.finalException = finalException;
} }
} }
} }

View File

@ -974,7 +974,8 @@ public abstract class DownloadService extends Service {
} }
@Override @Override
public void onDownloadChanged(DownloadManager downloadManager, Download download) { public void onDownloadChanged(
DownloadManager downloadManager, Download download, @Nullable Exception finalException) {
if (downloadService != null) { if (downloadService != null) {
downloadService.notifyDownloadChanged(download); downloadService.notifyDownloadChanged(download);
} }

View File

@ -100,7 +100,8 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen
} }
@Override @Override
public void onDownloadChanged(DownloadManager downloadManager, Download download) { public void onDownloadChanged(
DownloadManager downloadManager, Download download, @Nullable Exception finalException) {
if (download.state == Download.STATE_FAILED) { if (download.state == Download.STATE_FAILED) {
failureReason = download.failureReason; failureReason = download.failureReason;
} }