simplify Downloader constructors

Since all Downloaders are created via the DownloaderFactory, put the temp
file creation there, then we can remove lots of constructors.
This commit is contained in:
Hans-Christoph Steiner 2015-06-19 15:10:50 -04:00
parent 4fd914ac7a
commit 80063eb786
4 changed files with 7 additions and 37 deletions

View File

@ -2,7 +2,6 @@ package org.fdroid.fdroid.net;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.Log;
import org.fdroid.fdroid.ProgressListener;
@ -33,29 +32,12 @@ public abstract class Downloader {
public abstract InputStream getInputStream() throws IOException;
// The context is required for opening the file to write to.
Downloader(String destFile, @NonNull Context ctx)
throws FileNotFoundException, MalformedURLException {
this(new File(ctx.getFilesDir() + File.separator + destFile));
}
// The context is required for opening the file to write to.
Downloader(@NonNull Context ctx) throws IOException {
this(File.createTempFile("dl-", "", ctx.getCacheDir()));
}
Downloader(File destFile)
throws FileNotFoundException, MalformedURLException {
outputFile = destFile;
outputStream = new FileOutputStream(outputFile);
}
Downloader(OutputStream output)
throws MalformedURLException {
outputStream = output;
outputFile = null;
}
public void setProgressListener(ProgressListener listener) {
setProgressListener(listener, null);
}

View File

@ -7,12 +7,17 @@ import java.io.IOException;
public class DownloaderFactory {
/**
* Downloads to a temporary file, which *you must delete yourself when
* you are done
*/
public static Downloader create(String url, Context context)
throws IOException {
File destFile = File.createTempFile("dl-", "", context.getCacheDir());
if (isOnionAddress(url)) {
return new TorHttpDownloader(url, context);
return new TorHttpDownloader(url, destFile);
}
return new HttpDownloader(url, context);
return new HttpDownloader(url, destFile);
}
public static Downloader create(String url, File destFile)

View File

@ -1,6 +1,5 @@
package org.fdroid.fdroid.net;
import android.content.Context;
import android.util.Log;
import org.fdroid.fdroid.Preferences;
@ -34,16 +33,6 @@ public class HttpDownloader extends Downloader {
sourceUrl = new URL(source);
}
/**
* Downloads to a temporary file, which *you must delete yourself when
* you are done*.
* @see org.fdroid.fdroid.net.Downloader#getFile()
*/
HttpDownloader(String source, Context ctx) throws IOException {
super(ctx);
sourceUrl = new URL(source);
}
@Override
public InputStream getInputStream() throws IOException {
setupConnection();

View File

@ -1,7 +1,5 @@
package org.fdroid.fdroid.net;
import android.content.Context;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
@ -13,10 +11,6 @@ import java.net.SocketAddress;
public class TorHttpDownloader extends HttpDownloader {
TorHttpDownloader(String url, Context ctx) throws IOException {
super(url, ctx);
}
TorHttpDownloader(String url, File destFile)
throws FileNotFoundException, MalformedURLException {
super(url, destFile);