remove Context/LocalBroadcastManager from Downloader for testing
If there is no LocalBroadcastManager, then the Downloader tests can be done with pretty plain JUnit and then do not require the Android emulator to run
This commit is contained in:
parent
35764b90c0
commit
a1d6917ec7
@ -1,6 +1,5 @@
|
||||
package org.fdroid.fdroid.net;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.util.Log;
|
||||
|
||||
@ -25,8 +24,8 @@ public class BluetoothDownloader extends Downloader {
|
||||
private FileDetails fileDetails;
|
||||
private final String sourcePath;
|
||||
|
||||
public BluetoothDownloader(Context context, String macAddress, URL sourceUrl, File destFile) throws IOException {
|
||||
super(context, sourceUrl, destFile);
|
||||
public BluetoothDownloader(String macAddress, URL sourceUrl, File destFile) throws IOException {
|
||||
super(sourceUrl, destFile);
|
||||
this.connection = new BluetoothClient(macAddress).openConnection();
|
||||
this.sourcePath = sourceUrl.getPath();
|
||||
}
|
||||
|
@ -1,9 +1,6 @@
|
||||
package org.fdroid.fdroid.net;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v4.content.LocalBroadcastManager;
|
||||
|
||||
import org.fdroid.fdroid.Utils;
|
||||
|
||||
@ -28,7 +25,6 @@ public abstract class Downloader {
|
||||
|
||||
private final OutputStream outputStream;
|
||||
|
||||
private final LocalBroadcastManager localBroadcastManager;
|
||||
private final File outputFile;
|
||||
|
||||
protected final URL sourceUrl;
|
||||
@ -36,22 +32,31 @@ public abstract class Downloader {
|
||||
private int bytesRead;
|
||||
private int totalBytes;
|
||||
|
||||
interface DownloaderProgressListener {
|
||||
void sendProgress(URL sourceUrl, int bytesRead, int totalBytes);
|
||||
}
|
||||
|
||||
private DownloaderProgressListener downloaderProgressListener;
|
||||
|
||||
protected abstract InputStream getDownloadersInputStream() throws IOException;
|
||||
|
||||
protected abstract void close() throws IOException;
|
||||
|
||||
Downloader(Context context, URL url, File destFile)
|
||||
Downloader(URL url, File destFile)
|
||||
throws FileNotFoundException, MalformedURLException {
|
||||
this.sourceUrl = url;
|
||||
outputFile = destFile;
|
||||
outputStream = new FileOutputStream(outputFile);
|
||||
localBroadcastManager = LocalBroadcastManager.getInstance(context);
|
||||
}
|
||||
|
||||
public final InputStream getInputStream() throws IOException {
|
||||
return new WrappedInputStream(getDownloadersInputStream());
|
||||
}
|
||||
|
||||
public void setListener(DownloaderProgressListener listener) {
|
||||
this.downloaderProgressListener = listener;
|
||||
}
|
||||
|
||||
/**
|
||||
* If you ask for the cacheTag before calling download(), you will get the
|
||||
* same one you passed in (if any). If you call it after download(), you
|
||||
@ -188,11 +193,9 @@ public abstract class Downloader {
|
||||
|
||||
private void sendProgress(int bytesRead, int totalBytes) {
|
||||
this.bytesRead = bytesRead;
|
||||
Intent intent = new Intent(LOCAL_ACTION_PROGRESS);
|
||||
intent.putExtra(EXTRA_ADDRESS, sourceUrl.toString());
|
||||
intent.putExtra(EXTRA_BYTES_READ, bytesRead);
|
||||
intent.putExtra(EXTRA_TOTAL_BYTES, totalBytes);
|
||||
localBroadcastManager.sendBroadcast(intent);
|
||||
if (downloaderProgressListener != null) {
|
||||
downloaderProgressListener.sendProgress(sourceUrl, bytesRead, totalBytes);
|
||||
}
|
||||
}
|
||||
|
||||
public int getBytesRead() {
|
||||
|
@ -3,8 +3,10 @@ package org.fdroid.fdroid.net;
|
||||
import android.annotation.TargetApi;
|
||||
import android.app.DownloadManager;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.database.Cursor;
|
||||
import android.os.Build;
|
||||
import android.support.v4.content.LocalBroadcastManager;
|
||||
|
||||
import org.fdroid.fdroid.Utils;
|
||||
import org.fdroid.fdroid.data.Credentials;
|
||||
@ -14,9 +16,10 @@ import java.io.IOException;
|
||||
import java.net.URL;
|
||||
|
||||
public class DownloaderFactory {
|
||||
|
||||
private static final String TAG = "DownloaderFactory";
|
||||
|
||||
private static LocalBroadcastManager localBroadcastManager;
|
||||
|
||||
/**
|
||||
* Downloads to a temporary file, which *you must delete yourself when
|
||||
* you are done. It is stored in {@link Context#getCacheDir()} and starts
|
||||
@ -51,14 +54,31 @@ public class DownloaderFactory {
|
||||
|
||||
public static Downloader create(Context context, URL url, File destFile, Credentials credentials)
|
||||
throws IOException {
|
||||
Downloader downloader = null;
|
||||
if (localBroadcastManager == null) {
|
||||
localBroadcastManager = LocalBroadcastManager.getInstance(context);
|
||||
}
|
||||
|
||||
if (isBluetoothAddress(url)) {
|
||||
String macAddress = url.getHost().replace("-", ":");
|
||||
return new BluetoothDownloader(context, macAddress, url, destFile);
|
||||
downloader = new BluetoothDownloader(macAddress, url, destFile);
|
||||
} else if (isLocalFile(url)) {
|
||||
downloader = new LocalFileDownloader(url, destFile);
|
||||
} else {
|
||||
downloader = new HttpDownloader(url, destFile, credentials);
|
||||
}
|
||||
if (isLocalFile(url)) {
|
||||
return new LocalFileDownloader(context, url, destFile);
|
||||
|
||||
downloader.setListener(new Downloader.DownloaderProgressListener() {
|
||||
@Override
|
||||
public void sendProgress(URL sourceUrl, int bytesRead, int totalBytes) {
|
||||
Intent intent = new Intent(Downloader.LOCAL_ACTION_PROGRESS);
|
||||
intent.putExtra(Downloader.EXTRA_ADDRESS, sourceUrl.toString());
|
||||
intent.putExtra(Downloader.EXTRA_BYTES_READ, bytesRead);
|
||||
intent.putExtra(Downloader.EXTRA_TOTAL_BYTES, totalBytes);
|
||||
localBroadcastManager.sendBroadcast(intent);
|
||||
}
|
||||
return new HttpDownloader(context, url, destFile, credentials);
|
||||
});
|
||||
return downloader;
|
||||
}
|
||||
|
||||
private static boolean isBluetoothAddress(URL url) {
|
||||
|
@ -1,13 +1,11 @@
|
||||
package org.fdroid.fdroid.net;
|
||||
|
||||
import android.content.Context;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
|
||||
|
||||
import org.fdroid.fdroid.FDroidApp;
|
||||
import org.fdroid.fdroid.Preferences;
|
||||
import org.fdroid.fdroid.Utils;
|
||||
import org.fdroid.fdroid.data.Credentials;
|
||||
|
||||
@ -17,10 +15,7 @@ import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.Proxy;
|
||||
import java.net.SocketAddress;
|
||||
import java.net.URL;
|
||||
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
@ -38,14 +33,14 @@ public class HttpDownloader extends Downloader {
|
||||
private Credentials credentials;
|
||||
private int statusCode = -1;
|
||||
|
||||
HttpDownloader(Context context, URL url, File destFile)
|
||||
HttpDownloader(URL url, File destFile)
|
||||
throws FileNotFoundException, MalformedURLException {
|
||||
this(context, url, destFile, null);
|
||||
this(url, destFile, null);
|
||||
}
|
||||
|
||||
HttpDownloader(Context context, URL url, File destFile, final Credentials credentials)
|
||||
HttpDownloader(URL url, File destFile, final Credentials credentials)
|
||||
throws FileNotFoundException, MalformedURLException {
|
||||
super(context, url, destFile);
|
||||
super(url, destFile);
|
||||
|
||||
this.credentials = credentials;
|
||||
}
|
||||
|
@ -1,7 +1,5 @@
|
||||
package org.fdroid.fdroid.net;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
@ -12,8 +10,8 @@ import java.net.URL;
|
||||
|
||||
public class LocalFileDownloader extends Downloader {
|
||||
|
||||
LocalFileDownloader(Context context, URL url, File destFile) throws FileNotFoundException, MalformedURLException {
|
||||
super(context, url, destFile);
|
||||
LocalFileDownloader(URL url, File destFile) throws FileNotFoundException, MalformedURLException {
|
||||
super(url, destFile);
|
||||
}
|
||||
|
||||
private File getFileToDownload() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user