From 9a9bf921262c805e0de460e143520ccdb791f2f3 Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Fri, 19 Jun 2015 15:18:00 -0400 Subject: [PATCH] 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. --- F-Droid/src/org/fdroid/fdroid/RepoUpdater.java | 2 +- F-Droid/src/org/fdroid/fdroid/net/ApkDownloader.java | 4 +++- F-Droid/src/org/fdroid/fdroid/net/Downloader.java | 4 +++- .../src/org/fdroid/fdroid/net/DownloaderFactory.java | 12 ++++++------ .../src/org/fdroid/fdroid/net/HttpDownloader.java | 6 +++--- .../src/org/fdroid/fdroid/net/IconDownloader.java | 2 +- .../src/org/fdroid/fdroid/net/TorHttpDownloader.java | 6 ++++-- 7 files changed, 21 insertions(+), 15 deletions(-) diff --git a/F-Droid/src/org/fdroid/fdroid/RepoUpdater.java b/F-Droid/src/org/fdroid/fdroid/RepoUpdater.java index f3cff0b8b..b67915f73 100644 --- a/F-Droid/src/org/fdroid/fdroid/RepoUpdater.java +++ b/F-Droid/src/org/fdroid/fdroid/RepoUpdater.java @@ -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); diff --git a/F-Droid/src/org/fdroid/fdroid/net/ApkDownloader.java b/F-Droid/src/org/fdroid/fdroid/net/ApkDownloader.java index 185f55bb1..630a913c7 100644 --- a/F-Droid/src/org/fdroid/fdroid/net/ApkDownloader.java +++ b/F-Droid/src/org/fdroid/fdroid/net/ApkDownloader.java @@ -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; diff --git a/F-Droid/src/org/fdroid/fdroid/net/Downloader.java b/F-Droid/src/org/fdroid/fdroid/net/Downloader.java index 7daea82c4..751f51b96 100644 --- a/F-Droid/src/org/fdroid/fdroid/net/Downloader.java +++ b/F-Droid/src/org/fdroid/fdroid/net/Downloader.java @@ -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); } diff --git a/F-Droid/src/org/fdroid/fdroid/net/DownloaderFactory.java b/F-Droid/src/org/fdroid/fdroid/net/DownloaderFactory.java index bf2223b4b..765723b4b 100644 --- a/F-Droid/src/org/fdroid/fdroid/net/DownloaderFactory.java +++ b/F-Droid/src/org/fdroid/fdroid/net/DownloaderFactory.java @@ -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) { diff --git a/F-Droid/src/org/fdroid/fdroid/net/HttpDownloader.java b/F-Droid/src/org/fdroid/fdroid/net/HttpDownloader.java index 1ed22174f..3a93de690 100644 --- a/F-Droid/src/org/fdroid/fdroid/net/HttpDownloader.java +++ b/F-Droid/src/org/fdroid/fdroid/net/HttpDownloader.java @@ -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); } diff --git a/F-Droid/src/org/fdroid/fdroid/net/IconDownloader.java b/F-Droid/src/org/fdroid/fdroid/net/IconDownloader.java index 0ba464755..b90876df8 100644 --- a/F-Droid/src/org/fdroid/fdroid/net/IconDownloader.java +++ b/F-Droid/src/org/fdroid/fdroid/net/IconDownloader.java @@ -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); diff --git a/F-Droid/src/org/fdroid/fdroid/net/TorHttpDownloader.java b/F-Droid/src/org/fdroid/fdroid/net/TorHttpDownloader.java index fc7cd08a8..8a781a18d 100644 --- a/F-Droid/src/org/fdroid/fdroid/net/TorHttpDownloader.java +++ b/F-Droid/src/org/fdroid/fdroid/net/TorHttpDownloader.java @@ -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