use simplified ProgressListener in Downloader and DownloaderService

Now the simplified ProgressListener works as the generic listener again,
enforcing the concept of URL as unique ID throughout the code base.
This commit is contained in:
Hans-Christoph Steiner 2016-05-11 09:42:53 +02:00
parent 7f10be18c6
commit 4a9ed54f42
3 changed files with 12 additions and 18 deletions

View File

@ -1,5 +1,6 @@
package org.fdroid.fdroid.net; package org.fdroid.fdroid.net;
import org.fdroid.fdroid.ProgressListener;
import org.fdroid.fdroid.Utils; import org.fdroid.fdroid.Utils;
import java.io.File; import java.io.File;
@ -36,19 +37,10 @@ public abstract class Downloader {
protected final URL sourceUrl; protected final URL sourceUrl;
protected String cacheTag; protected String cacheTag;
/**
* This is meant only to send progress to {@link DownloaderService}. This
* also keeps this class pure Java so that it can be tested on the JVM,
* without requiring an Android device or emulator.
*/
interface DownloaderProgressListener {
void sendProgress(URL sourceUrl, int bytesRead, int totalBytes);
}
/** /**
* For sending download progress, should only be called in {@link #progressTask} * For sending download progress, should only be called in {@link #progressTask}
*/ */
private volatile DownloaderProgressListener downloaderProgressListener; private volatile ProgressListener downloaderProgressListener;
protected abstract InputStream getDownloadersInputStream() throws IOException; protected abstract InputStream getDownloadersInputStream() throws IOException;
@ -64,7 +56,7 @@ public abstract class Downloader {
return new WrappedInputStream(getDownloadersInputStream()); return new WrappedInputStream(getDownloadersInputStream());
} }
public void setListener(DownloaderProgressListener listener) { public void setListener(ProgressListener listener) {
this.downloaderProgressListener = listener; this.downloaderProgressListener = listener;
} }
@ -194,7 +186,7 @@ public abstract class Downloader {
@Override @Override
public void run() { public void run() {
if (downloaderProgressListener != null) { if (downloaderProgressListener != null) {
downloaderProgressListener.sendProgress(sourceUrl, bytesRead, totalBytes); downloaderProgressListener.onProgress(sourceUrl, bytesRead, totalBytes);
} }
} }
}; };

View File

@ -34,6 +34,7 @@ import android.support.v4.content.LocalBroadcastManager;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import org.fdroid.fdroid.ProgressListener;
import org.fdroid.fdroid.Utils; import org.fdroid.fdroid.Utils;
import org.fdroid.fdroid.data.SanitizedFile; import org.fdroid.fdroid.data.SanitizedFile;
@ -192,9 +193,9 @@ public class DownloaderService extends Service {
try { try {
downloader = DownloaderFactory.create(this, uri, localFile); downloader = DownloaderFactory.create(this, uri, localFile);
downloader.setListener(new Downloader.DownloaderProgressListener() { downloader.setListener(new ProgressListener() {
@Override @Override
public void sendProgress(URL sourceUrl, int bytesRead, int totalBytes) { public void onProgress(URL sourceUrl, int bytesRead, int totalBytes) {
Intent intent = new Intent(Downloader.ACTION_PROGRESS); Intent intent = new Intent(Downloader.ACTION_PROGRESS);
intent.setData(uri); intent.setData(uri);
intent.putExtra(Downloader.EXTRA_BYTES_READ, bytesRead); intent.putExtra(Downloader.EXTRA_BYTES_READ, bytesRead);

View File

@ -1,6 +1,7 @@
package org.fdroid.fdroid.net; package org.fdroid.fdroid.net;
import org.fdroid.fdroid.ProgressListener;
import org.junit.Test; import org.junit.Test;
import java.io.File; import java.io.File;
@ -48,9 +49,9 @@ public class HttpDownloaderTest {
URL url = new URL(urlString); URL url = new URL(urlString);
File destFile = File.createTempFile("dl-", ""); File destFile = File.createTempFile("dl-", "");
final HttpDownloader httpDownloader = new HttpDownloader(url, destFile); final HttpDownloader httpDownloader = new HttpDownloader(url, destFile);
httpDownloader.setListener(new Downloader.DownloaderProgressListener() { httpDownloader.setListener(new ProgressListener() {
@Override @Override
public void sendProgress(URL sourceUrl, int bytesRead, int totalBytes) { public void onProgress(URL sourceUrl, int bytesRead, int totalBytes) {
System.out.println("DownloaderProgressListener.sendProgress " + sourceUrl + " " + bytesRead + " / " + totalBytes); System.out.println("DownloaderProgressListener.sendProgress " + sourceUrl + " " + bytesRead + " / " + totalBytes);
receivedProgress = true; receivedProgress = true;
} }
@ -111,9 +112,9 @@ public class HttpDownloaderTest {
URL url = new URL("https://f-droid.org/repo/index.jar"); URL url = new URL("https://f-droid.org/repo/index.jar");
File destFile = File.createTempFile("dl-", ""); File destFile = File.createTempFile("dl-", "");
final HttpDownloader httpDownloader = new HttpDownloader(url, destFile); final HttpDownloader httpDownloader = new HttpDownloader(url, destFile);
httpDownloader.setListener(new Downloader.DownloaderProgressListener() { httpDownloader.setListener(new ProgressListener() {
@Override @Override
public void sendProgress(URL sourceUrl, int bytesRead, int totalBytes) { public void onProgress(URL sourceUrl, int bytesRead, int totalBytes) {
System.out.println("DownloaderProgressListener.sendProgress " + bytesRead + " / " + totalBytes); System.out.println("DownloaderProgressListener.sendProgress " + bytesRead + " / " + totalBytes);
receivedProgress = true; receivedProgress = true;
latch.countDown(); latch.countDown();