Propagate download exception through onDownloadChanged callback

This commit is contained in:
sravan1213 2020-06-08 18:42:48 +05:30
parent 226583f01c
commit c3282c9a37
5 changed files with 26 additions and 11 deletions

View file

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

View file

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

View file

@ -93,8 +93,10 @@ public final class DownloadManager {
*
* @param downloadManager The reporting instance.
* @param download The state of the download.
* @param error exception occurred when a download is failed
*/
default void onDownloadChanged(DownloadManager downloadManager, Download download) {}
default void onDownloadChanged(
DownloadManager downloadManager, Download download, @Nullable Throwable error) {}
/**
* Called when a download is removed.
@ -614,7 +616,7 @@ public final class DownloadManager {
}
} else {
for (Listener listener : listeners) {
listener.onDownloadChanged(this, updatedDownload);
listener.onDownloadChanged(this, updatedDownload, update.error);
}
}
if (waitingForRequirementsChanged) {
@ -906,7 +908,7 @@ public final class DownloadManager {
ArrayList<Download> updateList = new ArrayList<>(downloads);
for (int i = 0; i < downloads.size(); i++) {
DownloadUpdate update =
new DownloadUpdate(downloads.get(i), /* isRemove= */ false, updateList);
new DownloadUpdate(downloads.get(i), /* isRemove= */ false, updateList, null);
mainHandler.obtainMessage(MSG_DOWNLOAD_UPDATE, update).sendToTarget();
}
syncTasks();
@ -1121,7 +1123,11 @@ public final class DownloadManager {
Log.e(TAG, "Failed to update index.", e);
}
DownloadUpdate update =
new DownloadUpdate(download, /* isRemove= */ false, new ArrayList<>(downloads));
new DownloadUpdate(
download,
/* isRemove= */ false,
new ArrayList<>(downloads),
finalError);
mainHandler.obtainMessage(MSG_DOWNLOAD_UPDATE, update).sendToTarget();
}
@ -1139,7 +1145,7 @@ public final class DownloadManager {
Log.e(TAG, "Failed to remove from database");
}
DownloadUpdate update =
new DownloadUpdate(download, /* isRemove= */ true, new ArrayList<>(downloads));
new DownloadUpdate(download, /* isRemove= */ true, new ArrayList<>(downloads), null);
mainHandler.obtainMessage(MSG_DOWNLOAD_UPDATE, update).sendToTarget();
}
}
@ -1194,7 +1200,7 @@ public final class DownloadManager {
Log.e(TAG, "Failed to update index.", e);
}
DownloadUpdate update =
new DownloadUpdate(download, /* isRemove= */ false, new ArrayList<>(downloads));
new DownloadUpdate(download, /* isRemove= */ false, new ArrayList<>(downloads), null);
mainHandler.obtainMessage(MSG_DOWNLOAD_UPDATE, update).sendToTarget();
return download;
}
@ -1355,11 +1361,14 @@ public final class DownloadManager {
public final Download download;
public final boolean isRemove;
public final List<Download> downloads;
@Nullable public final Throwable error;
public DownloadUpdate(Download download, boolean isRemove, List<Download> downloads) {
public DownloadUpdate(
Download download, boolean isRemove, List<Download> downloads, @Nullable Throwable error) {
this.download = download;
this.isRemove = isRemove;
this.downloads = downloads;
this.error = error;
}
}
}

View file

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

View file

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