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.
This commit is contained in:
Hans-Christoph Steiner 2015-06-19 16:16:06 -04:00
parent 9a9bf92126
commit 5d4bdf6139
5 changed files with 36 additions and 17 deletions

View File

@ -22,6 +22,8 @@ import org.xml.sax.XMLReader;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.CodeSigner; import java.security.CodeSigner;
import java.security.cert.Certificate; import java.security.cert.Certificate;
import java.security.cert.X509Certificate; import java.security.cert.X509Certificate;
@ -71,14 +73,15 @@ public class RepoUpdater {
public List<Apk> getApks() { return apks; } public List<Apk> getApks() { return apks; }
protected String getIndexAddress() { protected URL getIndexAddress() throws MalformedURLException {
String urlString = repo.address + "/index.jar";
try { try {
String versionName = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName; 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) { } catch (PackageManager.NameNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
} }
return repo.address + "/index.jar"; return new URL(urlString);
} }
Downloader downloadIndex() throws UpdateException { Downloader downloadIndex() throws UpdateException {

View File

@ -33,9 +33,10 @@ public abstract class Downloader {
public abstract InputStream getInputStream() throws IOException; public abstract InputStream getInputStream() throws IOException;
Downloader(Context context, File destFile) Downloader(Context context, URL url, File destFile)
throws FileNotFoundException, MalformedURLException { throws FileNotFoundException, MalformedURLException {
this.context = context; this.context = context;
this.sourceUrl = url;
outputFile = destFile; outputFile = destFile;
outputStream = new FileOutputStream(outputFile); outputStream = new FileOutputStream(outputFile);
} }

View File

@ -4,23 +4,38 @@ import android.content.Context;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.URL;
public class DownloaderFactory { public class DownloaderFactory {
/** /**
* Downloads to a temporary file, which *you must delete yourself when * 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 { throws IOException {
File destFile = File.createTempFile("dl-", "", context.getCacheDir()); File destFile = File.createTempFile("dl-", "", context.getCacheDir());
if (isOnionAddress(url)) { destFile.deleteOnExit(); // this probably does nothing, but maybe...
return new TorHttpDownloader(context, url, destFile); return create(context, url, destFile);
}
return new HttpDownloader(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 { throws IOException {
if (isOnionAddress(url)) { if (isOnionAddress(url)) {
return new TorHttpDownloader(context, url, destFile); return new TorHttpDownloader(context, url, destFile);
@ -28,7 +43,7 @@ public class DownloaderFactory {
return new HttpDownloader(context, url, destFile); return new HttpDownloader(context, url, destFile);
} }
private static boolean isOnionAddress(String url) { private static boolean isOnionAddress(URL url) {
return url.matches("^[a-zA-Z0-9]+://[^/]+\\.onion/.*"); return url.getHost().endsWith(".onion");
} }
} }

View File

@ -27,10 +27,9 @@ public class HttpDownloader extends Downloader {
protected HttpURLConnection connection; protected HttpURLConnection connection;
private int statusCode = -1; private int statusCode = -1;
HttpDownloader(Context context, String source, File destFile) HttpDownloader(Context context, URL url, File destFile)
throws FileNotFoundException, MalformedURLException { throws FileNotFoundException, MalformedURLException {
super(context, destFile); super(context, url, destFile);
sourceUrl = new URL(source);
} }
@Override @Override

View File

@ -10,10 +10,11 @@ import java.net.InetSocketAddress;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.Proxy; import java.net.Proxy;
import java.net.SocketAddress; import java.net.SocketAddress;
import java.net.URL;
public class TorHttpDownloader extends HttpDownloader { public class TorHttpDownloader extends HttpDownloader {
TorHttpDownloader(Context context, String url, File destFile) TorHttpDownloader(Context context, URL url, File destFile)
throws FileNotFoundException, MalformedURLException { throws FileNotFoundException, MalformedURLException {
super(context, url, destFile); super(context, url, destFile);
} }