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 downloader = null;
try {
downloader = DownloaderFactory.create(
downloader = DownloaderFactory.create(context,
getIndexAddress(), File.createTempFile("index-", "-downloaded", context.getCacheDir()));
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";
@NonNull private final Apk curApk;
@NonNull private final Context context;
@NonNull private final String repoAddress;
@NonNull private final SanitizedFile localFile;
@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) {
this.context = context;
curApk = apk;
this.repoAddress = repoAddress;
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);
try {
Downloader downloader = DownloaderFactory.create(remoteAddress, localFile);
Downloader downloader = DownloaderFactory.create(context, remoteAddress, localFile);
dlWrapper = new AsyncDownloadWrapper(downloader, this);
dlWrapper.download();
return true;

View File

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

View File

@ -11,21 +11,21 @@ public class DownloaderFactory {
* Downloads to a temporary file, which *you must delete yourself when
* you are done
*/
public static Downloader create(String url, Context context)
public static Downloader create(Context context, String url)
throws IOException {
File destFile = File.createTempFile("dl-", "", context.getCacheDir());
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 {
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) {

View File

@ -1,5 +1,6 @@
package org.fdroid.fdroid.net;
import android.content.Context;
import android.util.Log;
import org.fdroid.fdroid.Preferences;
@ -26,10 +27,9 @@ public class HttpDownloader extends Downloader {
protected HttpURLConnection connection;
private int statusCode = -1;
// The context is required for opening the file to write to.
HttpDownloader(String source, File destFile)
HttpDownloader(Context context, String source, File destFile)
throws FileNotFoundException, MalformedURLException {
super(destFile);
super(context, destFile);
sourceUrl = new URL(source);
}

View File

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

View File

@ -1,5 +1,7 @@
package org.fdroid.fdroid.net;
import android.content.Context;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
@ -11,9 +13,9 @@ import java.net.SocketAddress;
public class TorHttpDownloader extends HttpDownloader {
TorHttpDownloader(String url, File destFile)
TorHttpDownloader(Context context, String url, File destFile)
throws FileNotFoundException, MalformedURLException {
super(url, destFile);
super(context, url, destFile);
}
@Override