From 910f9a68a6e0167867c94ca6cc3a8d7484d2e2c2 Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Tue, 27 May 2014 21:45:09 -0400 Subject: [PATCH] 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. --- src/org/fdroid/fdroid/net/ApkDownloader.java | 4 ++-- src/org/fdroid/fdroid/net/Downloader.java | 9 ++++---- .../fdroid/fdroid/net/DownloaderFactory.java | 20 ++++++++++++++++ src/org/fdroid/fdroid/net/HttpDownloader.java | 23 ++++--------------- .../fdroid/fdroid/updater/RepoUpdater.java | 4 ++-- 5 files changed, 33 insertions(+), 27 deletions(-) create mode 100644 src/org/fdroid/fdroid/net/DownloaderFactory.java diff --git a/src/org/fdroid/fdroid/net/ApkDownloader.java b/src/org/fdroid/fdroid/net/ApkDownloader.java index baddfd5ef..c8c6bcc15 100644 --- a/src/org/fdroid/fdroid/net/ApkDownloader.java +++ b/src/org/fdroid/fdroid/net/ApkDownloader.java @@ -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; diff --git a/src/org/fdroid/fdroid/net/Downloader.java b/src/org/fdroid/fdroid/net/Downloader.java index 8c9f7cf77..4f6ee766d 100644 --- a/src/org/fdroid/fdroid/net/Downloader.java +++ b/src/org/fdroid/fdroid/net/Downloader.java @@ -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; diff --git a/src/org/fdroid/fdroid/net/DownloaderFactory.java b/src/org/fdroid/fdroid/net/DownloaderFactory.java new file mode 100644 index 000000000..8206db94b --- /dev/null +++ b/src/org/fdroid/fdroid/net/DownloaderFactory.java @@ -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); + } +} diff --git a/src/org/fdroid/fdroid/net/HttpDownloader.java b/src/org/fdroid/fdroid/net/HttpDownloader.java index 177fa6de1..7f5cc8e96 100644 --- a/src/org/fdroid/fdroid/net/HttpDownloader.java +++ b/src/org/fdroid/fdroid/net/HttpDownloader.java @@ -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; } diff --git a/src/org/fdroid/fdroid/updater/RepoUpdater.java b/src/org/fdroid/fdroid/updater/RepoUpdater.java index eab4291d7..7cb788ed3 100644 --- a/src/org/fdroid/fdroid/updater/RepoUpdater.java +++ b/src/org/fdroid/fdroid/updater/RepoUpdater.java @@ -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