make all Downloaders get a Context so the base class can use those tricks

Having a Context in Downloader means that the communications can be changed
to a LocalBroadcastManager, following the pattern that is in a lot of this
app already.
This commit is contained in:
Hans-Christoph Steiner 2015-06-19 15:18:00 -04:00
parent 80063eb786
commit 9a9bf92126
7 changed files with 21 additions and 15 deletions

View File

@ -84,7 +84,7 @@ public class RepoUpdater {
Downloader downloadIndex() throws UpdateException { Downloader downloadIndex() throws UpdateException {
Downloader downloader = null; Downloader downloader = null;
try { try {
downloader = DownloaderFactory.create( downloader = DownloaderFactory.create(context,
getIndexAddress(), File.createTempFile("index-", "-downloaded", context.getCacheDir())); getIndexAddress(), File.createTempFile("index-", "-downloaded", context.getCacheDir()));
downloader.setCacheTag(repo.lastetag); downloader.setCacheTag(repo.lastetag);

View File

@ -63,6 +63,7 @@ public class ApkDownloader implements AsyncDownloadWrapper.Listener {
public static final String EVENT_DATA_ERROR_TYPE = "apkDownloadErrorType"; public static final String EVENT_DATA_ERROR_TYPE = "apkDownloadErrorType";
@NonNull private final Apk curApk; @NonNull private final Apk curApk;
@NonNull private final Context context;
@NonNull private final String repoAddress; @NonNull private final String repoAddress;
@NonNull private final SanitizedFile localFile; @NonNull private final SanitizedFile localFile;
@NonNull private final SanitizedFile potentiallyCachedFile; @NonNull private final SanitizedFile potentiallyCachedFile;
@ -84,6 +85,7 @@ public class ApkDownloader implements AsyncDownloadWrapper.Listener {
} }
public ApkDownloader(@NonNull final Context context, @NonNull final Apk apk, @NonNull final String repoAddress) { public ApkDownloader(@NonNull final Context context, @NonNull final Apk apk, @NonNull final String repoAddress) {
this.context = context;
curApk = apk; curApk = apk;
this.repoAddress = repoAddress; this.repoAddress = repoAddress;
localFile = new SanitizedFile(Utils.getApkDownloadDir(context), apk.apkName); localFile = new SanitizedFile(Utils.getApkDownloadDir(context), apk.apkName);
@ -191,7 +193,7 @@ public class ApkDownloader implements AsyncDownloadWrapper.Listener {
Log.d(TAG, "Downloading apk from " + remoteAddress + " to " + localFile); Log.d(TAG, "Downloading apk from " + remoteAddress + " to " + localFile);
try { try {
Downloader downloader = DownloaderFactory.create(remoteAddress, localFile); Downloader downloader = DownloaderFactory.create(context, remoteAddress, localFile);
dlWrapper = new AsyncDownloadWrapper(downloader, this); dlWrapper = new AsyncDownloadWrapper(downloader, this);
dlWrapper.download(); dlWrapper.download();
return true; return true;

View File

@ -23,6 +23,7 @@ public abstract class Downloader {
private ProgressListener progressListener = null; private ProgressListener progressListener = null;
private Bundle eventData = null; private Bundle eventData = null;
private final Context context;
private final File outputFile; private final File outputFile;
protected URL sourceUrl; protected URL sourceUrl;
@ -32,8 +33,9 @@ public abstract class Downloader {
public abstract InputStream getInputStream() throws IOException; public abstract InputStream getInputStream() throws IOException;
Downloader(File destFile) Downloader(Context context, File destFile)
throws FileNotFoundException, MalformedURLException { throws FileNotFoundException, MalformedURLException {
this.context = context;
outputFile = destFile; outputFile = destFile;
outputStream = new FileOutputStream(outputFile); outputStream = new FileOutputStream(outputFile);
} }

View File

@ -11,21 +11,21 @@ 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
*/ */
public static Downloader create(String url, Context context) public static Downloader create(Context context, String url)
throws IOException { throws IOException {
File destFile = File.createTempFile("dl-", "", context.getCacheDir()); File destFile = File.createTempFile("dl-", "", context.getCacheDir());
if (isOnionAddress(url)) { if (isOnionAddress(url)) {
return new TorHttpDownloader(url, destFile); return new TorHttpDownloader(context, url, destFile);
} }
return new HttpDownloader(url, destFile); return new HttpDownloader(context, url, destFile);
} }
public static Downloader create(String url, File destFile) public static Downloader create(Context context, String url, File destFile)
throws IOException { throws IOException {
if (isOnionAddress(url)) { if (isOnionAddress(url)) {
return new TorHttpDownloader(url, destFile); return new TorHttpDownloader(context, url, destFile);
} }
return new HttpDownloader(url, destFile); return new HttpDownloader(context, url, destFile);
} }
private static boolean isOnionAddress(String url) { private static boolean isOnionAddress(String url) {

View File

@ -1,5 +1,6 @@
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;
@ -26,10 +27,9 @@ public class HttpDownloader extends Downloader {
protected HttpURLConnection connection; protected HttpURLConnection connection;
private int statusCode = -1; private int statusCode = -1;
// The context is required for opening the file to write to. HttpDownloader(Context context, String source, File destFile)
HttpDownloader(String source, File destFile)
throws FileNotFoundException, MalformedURLException { throws FileNotFoundException, MalformedURLException {
super(destFile); super(context, destFile);
sourceUrl = new URL(source); sourceUrl = new URL(source);
} }

View File

@ -22,7 +22,7 @@ public class IconDownloader extends BaseImageDownloader {
switch (Scheme.ofUri(imageUri)) { switch (Scheme.ofUri(imageUri)) {
case HTTP: case HTTP:
case HTTPS: case HTTPS:
Downloader downloader = DownloaderFactory.create(imageUri, context); Downloader downloader = DownloaderFactory.create(context, imageUri);
return downloader.getInputStream(); return downloader.getInputStream();
default: default:
return super.getStream(imageUri, extra); return super.getStream(imageUri, extra);

View File

@ -1,5 +1,7 @@
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;
@ -11,9 +13,9 @@ import java.net.SocketAddress;
public class TorHttpDownloader extends HttpDownloader { public class TorHttpDownloader extends HttpDownloader {
TorHttpDownloader(String url, File destFile) TorHttpDownloader(Context context, String url, File destFile)
throws FileNotFoundException, MalformedURLException { throws FileNotFoundException, MalformedURLException {
super(url, destFile); super(context, url, destFile);
} }
@Override @Override