make DownloaderFactory for creating any kind of Downloader
This will ultimately be used to create the right Downloader subclass instance based on the URL of the file to download (i.e. rfcomm://, .onion address, ssh://, new socket protocols, etc). Also delete unused constructors, they can trivially be readded if they are ever used, and they are currently just clutter.
This commit is contained in:
parent
91e06b8496
commit
910f9a68a6
@ -22,6 +22,7 @@ package org.fdroid.fdroid.net;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
|
||||
import org.fdroid.fdroid.Hasher;
|
||||
import org.fdroid.fdroid.ProgressListener;
|
||||
import org.fdroid.fdroid.data.Apk;
|
||||
@ -172,8 +173,7 @@ public class ApkDownloader implements AsyncDownloadWrapper.Listener {
|
||||
Log.d(TAG, "Downloading apk from " + remoteAddress);
|
||||
|
||||
try {
|
||||
|
||||
Downloader downloader = new HttpDownloader(remoteAddress, localFile);
|
||||
Downloader downloader = DownloaderFactory.create(remoteAddress, localFile);
|
||||
dlWrapper = new AsyncDownloadWrapper(downloader, this);
|
||||
dlWrapper.download();
|
||||
return true;
|
||||
|
@ -30,24 +30,23 @@ public abstract class Downloader {
|
||||
public abstract InputStream inputStream() throws IOException;
|
||||
|
||||
// The context is required for opening the file to write to.
|
||||
public Downloader(String destFile, Context ctx)
|
||||
Downloader(String destFile, Context ctx)
|
||||
throws FileNotFoundException, MalformedURLException {
|
||||
this(new File(ctx.getFilesDir() + File.separator + destFile));
|
||||
}
|
||||
|
||||
// The context is required for opening the file to write to.
|
||||
public Downloader(Context ctx) throws IOException {
|
||||
Downloader(Context ctx) throws IOException {
|
||||
this(File.createTempFile("dl-", "", ctx.getCacheDir()));
|
||||
}
|
||||
|
||||
public Downloader(File destFile)
|
||||
Downloader(File destFile)
|
||||
throws FileNotFoundException, MalformedURLException {
|
||||
// http://developer.android.com/guide/topics/data/data-storage.html#InternalCache
|
||||
outputFile = destFile;
|
||||
outputStream = new FileOutputStream(outputFile);
|
||||
}
|
||||
|
||||
public Downloader(OutputStream output)
|
||||
Downloader(OutputStream output)
|
||||
throws MalformedURLException {
|
||||
outputStream = output;
|
||||
outputFile = null;
|
||||
|
20
src/org/fdroid/fdroid/net/DownloaderFactory.java
Normal file
20
src/org/fdroid/fdroid/net/DownloaderFactory.java
Normal file
@ -0,0 +1,20 @@
|
||||
|
||||
package org.fdroid.fdroid.net;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class DownloaderFactory {
|
||||
|
||||
public static Downloader create(String url, Context context)
|
||||
throws IOException {
|
||||
return new HttpDownloader(url, context);
|
||||
}
|
||||
|
||||
public static Downloader create(String url, File destFile)
|
||||
throws IOException {
|
||||
return new HttpDownloader(url, destFile);
|
||||
}
|
||||
}
|
@ -7,11 +7,9 @@ import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import javax.net.ssl.SSLHandshakeException;
|
||||
|
||||
@ -26,14 +24,7 @@ public class HttpDownloader extends Downloader {
|
||||
private int statusCode = -1;
|
||||
|
||||
// The context is required for opening the file to write to.
|
||||
public HttpDownloader(String source, String destFile, Context ctx)
|
||||
throws FileNotFoundException, MalformedURLException {
|
||||
super(destFile, ctx);
|
||||
sourceUrl = new URL(source);
|
||||
}
|
||||
|
||||
// The context is required for opening the file to write to.
|
||||
public HttpDownloader(String source, File destFile)
|
||||
HttpDownloader(String source, File destFile)
|
||||
throws FileNotFoundException, MalformedURLException {
|
||||
super(destFile);
|
||||
sourceUrl = new URL(source);
|
||||
@ -42,19 +33,14 @@ public class HttpDownloader extends Downloader {
|
||||
/**
|
||||
* Downloads to a temporary file, which *you must delete yourself when
|
||||
* you are done*.
|
||||
* @see org.fdroid.fdroid.net.HttpDownloader#getFile()
|
||||
* @see org.fdroid.fdroid.net.Downloader#getFile()
|
||||
*/
|
||||
public HttpDownloader(String source, Context ctx) throws IOException {
|
||||
HttpDownloader(String source, Context ctx) throws IOException {
|
||||
super(ctx);
|
||||
sourceUrl = new URL(source);
|
||||
}
|
||||
|
||||
public HttpDownloader(String source, OutputStream output)
|
||||
throws MalformedURLException {
|
||||
super(output);
|
||||
sourceUrl = new URL(source);
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream inputStream() throws IOException {
|
||||
return connection.getInputStream();
|
||||
}
|
||||
@ -93,6 +79,7 @@ public class HttpDownloader extends Downloader {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCached() {
|
||||
return wantToCheckCache() && statusCode == 304;
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ import org.fdroid.fdroid.data.App;
|
||||
import org.fdroid.fdroid.data.Repo;
|
||||
import org.fdroid.fdroid.data.RepoProvider;
|
||||
import org.fdroid.fdroid.net.Downloader;
|
||||
import org.fdroid.fdroid.net.HttpDownloader;
|
||||
import org.fdroid.fdroid.net.DownloaderFactory;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.SAXException;
|
||||
import org.xml.sax.XMLReader;
|
||||
@ -89,7 +89,7 @@ abstract public class RepoUpdater {
|
||||
protected Downloader downloadIndex() throws UpdateException {
|
||||
Downloader downloader = null;
|
||||
try {
|
||||
downloader = new HttpDownloader(getIndexAddress(), context);
|
||||
downloader = DownloaderFactory.create(getIndexAddress(), context);
|
||||
downloader.setCacheTag(repo.lastetag);
|
||||
|
||||
if (progressListener != null) { // interactive session, show progress
|
||||
|
Loading…
x
Reference in New Issue
Block a user