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:
parent
4fd914ac7a
commit
80063eb786
@ -2,7 +2,6 @@ package org.fdroid.fdroid.net;
|
|||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.support.annotation.NonNull;
|
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import org.fdroid.fdroid.ProgressListener;
|
import org.fdroid.fdroid.ProgressListener;
|
||||||
@ -33,29 +32,12 @@ public abstract class Downloader {
|
|||||||
|
|
||||||
public abstract InputStream getInputStream() throws IOException;
|
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)
|
Downloader(File destFile)
|
||||||
throws FileNotFoundException, MalformedURLException {
|
throws FileNotFoundException, MalformedURLException {
|
||||||
outputFile = destFile;
|
outputFile = destFile;
|
||||||
outputStream = new FileOutputStream(outputFile);
|
outputStream = new FileOutputStream(outputFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
Downloader(OutputStream output)
|
|
||||||
throws MalformedURLException {
|
|
||||||
outputStream = output;
|
|
||||||
outputFile = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setProgressListener(ProgressListener listener) {
|
public void setProgressListener(ProgressListener listener) {
|
||||||
setProgressListener(listener, null);
|
setProgressListener(listener, null);
|
||||||
}
|
}
|
||||||
|
@ -7,12 +7,17 @@ import java.io.IOException;
|
|||||||
|
|
||||||
public class DownloaderFactory {
|
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)
|
public static Downloader create(String url, Context context)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
|
File destFile = File.createTempFile("dl-", "", context.getCacheDir());
|
||||||
if (isOnionAddress(url)) {
|
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)
|
public static Downloader create(String url, File destFile)
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package org.fdroid.fdroid.net;
|
package org.fdroid.fdroid.net;
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import org.fdroid.fdroid.Preferences;
|
import org.fdroid.fdroid.Preferences;
|
||||||
@ -34,16 +33,6 @@ public class HttpDownloader extends Downloader {
|
|||||||
sourceUrl = new URL(source);
|
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
|
@Override
|
||||||
public InputStream getInputStream() throws IOException {
|
public InputStream getInputStream() throws IOException {
|
||||||
setupConnection();
|
setupConnection();
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
package org.fdroid.fdroid.net;
|
package org.fdroid.fdroid.net;
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -13,10 +11,6 @@ import java.net.SocketAddress;
|
|||||||
|
|
||||||
public class TorHttpDownloader extends HttpDownloader {
|
public class TorHttpDownloader extends HttpDownloader {
|
||||||
|
|
||||||
TorHttpDownloader(String url, Context ctx) throws IOException {
|
|
||||||
super(url, ctx);
|
|
||||||
}
|
|
||||||
|
|
||||||
TorHttpDownloader(String url, File destFile)
|
TorHttpDownloader(String url, File destFile)
|
||||||
throws FileNotFoundException, MalformedURLException {
|
throws FileNotFoundException, MalformedURLException {
|
||||||
super(url, destFile);
|
super(url, destFile);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user