From 5d4bdf61391b6252f4f367a1b8a5e80330180273 Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Fri, 19 Jun 2015 16:16:06 -0400 Subject: [PATCH] all Downloaders have a URL, so make the base class store it for use This lets the local broadcasts include the URL as an extra, and other tricks. --- .../src/org/fdroid/fdroid/RepoUpdater.java | 9 +++-- .../src/org/fdroid/fdroid/net/Downloader.java | 3 +- .../fdroid/fdroid/net/DownloaderFactory.java | 33 ++++++++++++++----- .../org/fdroid/fdroid/net/HttpDownloader.java | 5 ++- .../fdroid/fdroid/net/TorHttpDownloader.java | 3 +- 5 files changed, 36 insertions(+), 17 deletions(-) diff --git a/F-Droid/src/org/fdroid/fdroid/RepoUpdater.java b/F-Droid/src/org/fdroid/fdroid/RepoUpdater.java index b67915f73..a38c7fa1d 100644 --- a/F-Droid/src/org/fdroid/fdroid/RepoUpdater.java +++ b/F-Droid/src/org/fdroid/fdroid/RepoUpdater.java @@ -22,6 +22,8 @@ import org.xml.sax.XMLReader; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; import java.security.CodeSigner; import java.security.cert.Certificate; import java.security.cert.X509Certificate; @@ -71,14 +73,15 @@ public class RepoUpdater { public List getApks() { return apks; } - protected String getIndexAddress() { + protected URL getIndexAddress() throws MalformedURLException { + String urlString = repo.address + "/index.jar"; try { String versionName = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName; - return repo.address + "/index.jar?client_version=" + versionName; + urlString += "?client_version=" + versionName; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } - return repo.address + "/index.jar"; + return new URL(urlString); } Downloader downloadIndex() throws UpdateException { diff --git a/F-Droid/src/org/fdroid/fdroid/net/Downloader.java b/F-Droid/src/org/fdroid/fdroid/net/Downloader.java index 751f51b96..4f8f8bc2c 100644 --- a/F-Droid/src/org/fdroid/fdroid/net/Downloader.java +++ b/F-Droid/src/org/fdroid/fdroid/net/Downloader.java @@ -33,9 +33,10 @@ public abstract class Downloader { public abstract InputStream getInputStream() throws IOException; - Downloader(Context context, File destFile) + Downloader(Context context, URL url, File destFile) throws FileNotFoundException, MalformedURLException { this.context = context; + this.sourceUrl = url; outputFile = destFile; outputStream = new FileOutputStream(outputFile); } diff --git a/F-Droid/src/org/fdroid/fdroid/net/DownloaderFactory.java b/F-Droid/src/org/fdroid/fdroid/net/DownloaderFactory.java index 765723b4b..fdc7b3255 100644 --- a/F-Droid/src/org/fdroid/fdroid/net/DownloaderFactory.java +++ b/F-Droid/src/org/fdroid/fdroid/net/DownloaderFactory.java @@ -4,23 +4,38 @@ import android.content.Context; import java.io.File; import java.io.IOException; +import java.net.URL; public class DownloaderFactory { /** * Downloads to a temporary file, which *you must delete yourself when - * you are done + * you are done. It is stored in {@link Context#getCacheDir()} and starts + * with the prefix {@code dl-}. */ - public static Downloader create(Context context, String url) + public static Downloader create(Context context, String urlString) + throws IOException { + return create(context, new URL(urlString)); + } + + /** + * Downloads to a temporary file, which *you must delete yourself when + * you are done. It is stored in {@link Context#getCacheDir()} and starts + * with the prefix {@code dl-}. + */ + public static Downloader create(Context context, URL url) throws IOException { File destFile = File.createTempFile("dl-", "", context.getCacheDir()); - if (isOnionAddress(url)) { - return new TorHttpDownloader(context, url, destFile); - } - return new HttpDownloader(context, url, destFile); + destFile.deleteOnExit(); // this probably does nothing, but maybe... + return create(context, url, destFile); } - public static Downloader create(Context context, String url, File destFile) + public static Downloader create(Context context, String urlString, File destFile) + throws IOException { + return create(context, new URL(urlString), destFile); + } + + public static Downloader create(Context context, URL url, File destFile) throws IOException { if (isOnionAddress(url)) { return new TorHttpDownloader(context, url, destFile); @@ -28,7 +43,7 @@ public class DownloaderFactory { return new HttpDownloader(context, url, destFile); } - private static boolean isOnionAddress(String url) { - return url.matches("^[a-zA-Z0-9]+://[^/]+\\.onion/.*"); + private static boolean isOnionAddress(URL url) { + return url.getHost().endsWith(".onion"); } } diff --git a/F-Droid/src/org/fdroid/fdroid/net/HttpDownloader.java b/F-Droid/src/org/fdroid/fdroid/net/HttpDownloader.java index 3a93de690..d134c95d0 100644 --- a/F-Droid/src/org/fdroid/fdroid/net/HttpDownloader.java +++ b/F-Droid/src/org/fdroid/fdroid/net/HttpDownloader.java @@ -27,10 +27,9 @@ public class HttpDownloader extends Downloader { protected HttpURLConnection connection; private int statusCode = -1; - HttpDownloader(Context context, String source, File destFile) + HttpDownloader(Context context, URL url, File destFile) throws FileNotFoundException, MalformedURLException { - super(context, destFile); - sourceUrl = new URL(source); + super(context, url, destFile); } @Override diff --git a/F-Droid/src/org/fdroid/fdroid/net/TorHttpDownloader.java b/F-Droid/src/org/fdroid/fdroid/net/TorHttpDownloader.java index 8a781a18d..c0eaf4d45 100644 --- a/F-Droid/src/org/fdroid/fdroid/net/TorHttpDownloader.java +++ b/F-Droid/src/org/fdroid/fdroid/net/TorHttpDownloader.java @@ -10,10 +10,11 @@ import java.net.InetSocketAddress; import java.net.MalformedURLException; import java.net.Proxy; import java.net.SocketAddress; +import java.net.URL; public class TorHttpDownloader extends HttpDownloader { - TorHttpDownloader(Context context, String url, File destFile) + TorHttpDownloader(Context context, URL url, File destFile) throws FileNotFoundException, MalformedURLException { super(context, url, destFile); }