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:
parent
7f10be18c6
commit
4a9ed54f42
@ -1,5 +1,6 @@
|
||||
package org.fdroid.fdroid.net;
|
||||
|
||||
import org.fdroid.fdroid.ProgressListener;
|
||||
import org.fdroid.fdroid.Utils;
|
||||
|
||||
import java.io.File;
|
||||
@ -36,19 +37,10 @@ public abstract class Downloader {
|
||||
protected final URL sourceUrl;
|
||||
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}
|
||||
*/
|
||||
private volatile DownloaderProgressListener downloaderProgressListener;
|
||||
private volatile ProgressListener downloaderProgressListener;
|
||||
|
||||
protected abstract InputStream getDownloadersInputStream() throws IOException;
|
||||
|
||||
@ -64,7 +56,7 @@ public abstract class Downloader {
|
||||
return new WrappedInputStream(getDownloadersInputStream());
|
||||
}
|
||||
|
||||
public void setListener(DownloaderProgressListener listener) {
|
||||
public void setListener(ProgressListener listener) {
|
||||
this.downloaderProgressListener = listener;
|
||||
}
|
||||
|
||||
@ -194,7 +186,7 @@ public abstract class Downloader {
|
||||
@Override
|
||||
public void run() {
|
||||
if (downloaderProgressListener != null) {
|
||||
downloaderProgressListener.sendProgress(sourceUrl, bytesRead, totalBytes);
|
||||
downloaderProgressListener.onProgress(sourceUrl, bytesRead, totalBytes);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -34,6 +34,7 @@ import android.support.v4.content.LocalBroadcastManager;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import org.fdroid.fdroid.ProgressListener;
|
||||
import org.fdroid.fdroid.Utils;
|
||||
import org.fdroid.fdroid.data.SanitizedFile;
|
||||
|
||||
@ -192,9 +193,9 @@ public class DownloaderService extends Service {
|
||||
|
||||
try {
|
||||
downloader = DownloaderFactory.create(this, uri, localFile);
|
||||
downloader.setListener(new Downloader.DownloaderProgressListener() {
|
||||
downloader.setListener(new ProgressListener() {
|
||||
@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.setData(uri);
|
||||
intent.putExtra(Downloader.EXTRA_BYTES_READ, bytesRead);
|
||||
|
@ -1,6 +1,7 @@
|
||||
|
||||
package org.fdroid.fdroid.net;
|
||||
|
||||
import org.fdroid.fdroid.ProgressListener;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
@ -48,9 +49,9 @@ public class HttpDownloaderTest {
|
||||
URL url = new URL(urlString);
|
||||
File destFile = File.createTempFile("dl-", "");
|
||||
final HttpDownloader httpDownloader = new HttpDownloader(url, destFile);
|
||||
httpDownloader.setListener(new Downloader.DownloaderProgressListener() {
|
||||
httpDownloader.setListener(new ProgressListener() {
|
||||
@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);
|
||||
receivedProgress = true;
|
||||
}
|
||||
@ -111,9 +112,9 @@ public class HttpDownloaderTest {
|
||||
URL url = new URL("https://f-droid.org/repo/index.jar");
|
||||
File destFile = File.createTempFile("dl-", "");
|
||||
final HttpDownloader httpDownloader = new HttpDownloader(url, destFile);
|
||||
httpDownloader.setListener(new Downloader.DownloaderProgressListener() {
|
||||
httpDownloader.setListener(new ProgressListener() {
|
||||
@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);
|
||||
receivedProgress = true;
|
||||
latch.countDown();
|
||||
|
Loading…
x
Reference in New Issue
Block a user