diff --git a/F-Droid/src/org/fdroid/fdroid/net/BluetoothDownloader.java b/F-Droid/src/org/fdroid/fdroid/net/BluetoothDownloader.java index cf38c1d2a..63c125f44 100644 --- a/F-Droid/src/org/fdroid/fdroid/net/BluetoothDownloader.java +++ b/F-Droid/src/org/fdroid/fdroid/net/BluetoothDownloader.java @@ -1,6 +1,5 @@ package org.fdroid.fdroid.net; -import android.content.Context; import android.support.annotation.Nullable; import android.util.Log; @@ -25,8 +24,8 @@ public class BluetoothDownloader extends Downloader { private FileDetails fileDetails; private final String sourcePath; - public BluetoothDownloader(Context context, String macAddress, URL sourceUrl, File destFile) throws IOException { - super(context, sourceUrl, destFile); + public BluetoothDownloader(String macAddress, URL sourceUrl, File destFile) throws IOException { + super(sourceUrl, destFile); this.connection = new BluetoothClient(macAddress).openConnection(); this.sourcePath = sourceUrl.getPath(); } diff --git a/F-Droid/src/org/fdroid/fdroid/net/Downloader.java b/F-Droid/src/org/fdroid/fdroid/net/Downloader.java index 24e96af71..b66dcc7ec 100644 --- a/F-Droid/src/org/fdroid/fdroid/net/Downloader.java +++ b/F-Droid/src/org/fdroid/fdroid/net/Downloader.java @@ -1,9 +1,6 @@ package org.fdroid.fdroid.net; -import android.content.Context; -import android.content.Intent; import android.support.annotation.NonNull; -import android.support.v4.content.LocalBroadcastManager; import org.fdroid.fdroid.Utils; @@ -28,7 +25,6 @@ public abstract class Downloader { private final OutputStream outputStream; - private final LocalBroadcastManager localBroadcastManager; private final File outputFile; protected final URL sourceUrl; @@ -36,22 +32,31 @@ public abstract class Downloader { private int bytesRead; private int totalBytes; + interface DownloaderProgressListener { + void sendProgress(URL sourceUrl, int bytesRead, int totalBytes); + } + + private DownloaderProgressListener downloaderProgressListener; + protected abstract InputStream getDownloadersInputStream() throws IOException; protected abstract void close() throws IOException; - Downloader(Context context, URL url, File destFile) + Downloader(URL url, File destFile) throws FileNotFoundException, MalformedURLException { this.sourceUrl = url; outputFile = destFile; outputStream = new FileOutputStream(outputFile); - localBroadcastManager = LocalBroadcastManager.getInstance(context); } public final InputStream getInputStream() throws IOException { return new WrappedInputStream(getDownloadersInputStream()); } + public void setListener(DownloaderProgressListener listener) { + this.downloaderProgressListener = listener; + } + /** * If you ask for the cacheTag before calling download(), you will get the * same one you passed in (if any). If you call it after download(), you @@ -188,11 +193,9 @@ public abstract class Downloader { private void sendProgress(int bytesRead, int totalBytes) { this.bytesRead = bytesRead; - Intent intent = new Intent(LOCAL_ACTION_PROGRESS); - intent.putExtra(EXTRA_ADDRESS, sourceUrl.toString()); - intent.putExtra(EXTRA_BYTES_READ, bytesRead); - intent.putExtra(EXTRA_TOTAL_BYTES, totalBytes); - localBroadcastManager.sendBroadcast(intent); + if (downloaderProgressListener != null) { + downloaderProgressListener.sendProgress(sourceUrl, bytesRead, totalBytes); + } } public int getBytesRead() { diff --git a/F-Droid/src/org/fdroid/fdroid/net/DownloaderFactory.java b/F-Droid/src/org/fdroid/fdroid/net/DownloaderFactory.java index fa012d59f..2ac93be4d 100644 --- a/F-Droid/src/org/fdroid/fdroid/net/DownloaderFactory.java +++ b/F-Droid/src/org/fdroid/fdroid/net/DownloaderFactory.java @@ -3,8 +3,10 @@ package org.fdroid.fdroid.net; import android.annotation.TargetApi; import android.app.DownloadManager; import android.content.Context; +import android.content.Intent; import android.database.Cursor; import android.os.Build; +import android.support.v4.content.LocalBroadcastManager; import org.fdroid.fdroid.Utils; import org.fdroid.fdroid.data.Credentials; @@ -14,9 +16,10 @@ import java.io.IOException; import java.net.URL; public class DownloaderFactory { - private static final String TAG = "DownloaderFactory"; + private static LocalBroadcastManager localBroadcastManager; + /** * Downloads to a temporary file, which *you must delete yourself when * you are done. It is stored in {@link Context#getCacheDir()} and starts @@ -51,14 +54,31 @@ public class DownloaderFactory { public static Downloader create(Context context, URL url, File destFile, Credentials credentials) throws IOException { + Downloader downloader = null; + if (localBroadcastManager == null) { + localBroadcastManager = LocalBroadcastManager.getInstance(context); + } + if (isBluetoothAddress(url)) { String macAddress = url.getHost().replace("-", ":"); - return new BluetoothDownloader(context, macAddress, url, destFile); + downloader = new BluetoothDownloader(macAddress, url, destFile); + } else if (isLocalFile(url)) { + downloader = new LocalFileDownloader(url, destFile); + } else { + downloader = new HttpDownloader(url, destFile, credentials); } - if (isLocalFile(url)) { - return new LocalFileDownloader(context, url, destFile); - } - return new HttpDownloader(context, url, destFile, credentials); + + downloader.setListener(new Downloader.DownloaderProgressListener() { + @Override + public void sendProgress(URL sourceUrl, int bytesRead, int totalBytes) { + Intent intent = new Intent(Downloader.LOCAL_ACTION_PROGRESS); + intent.putExtra(Downloader.EXTRA_ADDRESS, sourceUrl.toString()); + intent.putExtra(Downloader.EXTRA_BYTES_READ, bytesRead); + intent.putExtra(Downloader.EXTRA_TOTAL_BYTES, totalBytes); + localBroadcastManager.sendBroadcast(intent); + } + }); + return downloader; } private static boolean isBluetoothAddress(URL url) { diff --git a/F-Droid/src/org/fdroid/fdroid/net/HttpDownloader.java b/F-Droid/src/org/fdroid/fdroid/net/HttpDownloader.java index aaaddea13..bfe234d09 100644 --- a/F-Droid/src/org/fdroid/fdroid/net/HttpDownloader.java +++ b/F-Droid/src/org/fdroid/fdroid/net/HttpDownloader.java @@ -1,13 +1,11 @@ package org.fdroid.fdroid.net; -import android.content.Context; import android.text.TextUtils; import android.util.Log; import com.nostra13.universalimageloader.core.download.BaseImageDownloader; import org.fdroid.fdroid.FDroidApp; -import org.fdroid.fdroid.Preferences; import org.fdroid.fdroid.Utils; import org.fdroid.fdroid.data.Credentials; @@ -17,10 +15,7 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; -import java.net.InetSocketAddress; import java.net.MalformedURLException; -import java.net.Proxy; -import java.net.SocketAddress; import java.net.URL; import javax.net.ssl.HttpsURLConnection; @@ -38,14 +33,14 @@ public class HttpDownloader extends Downloader { private Credentials credentials; private int statusCode = -1; - HttpDownloader(Context context, URL url, File destFile) + HttpDownloader(URL url, File destFile) throws FileNotFoundException, MalformedURLException { - this(context, url, destFile, null); + this(url, destFile, null); } - HttpDownloader(Context context, URL url, File destFile, final Credentials credentials) + HttpDownloader(URL url, File destFile, final Credentials credentials) throws FileNotFoundException, MalformedURLException { - super(context, url, destFile); + super(url, destFile); this.credentials = credentials; } diff --git a/F-Droid/src/org/fdroid/fdroid/net/LocalFileDownloader.java b/F-Droid/src/org/fdroid/fdroid/net/LocalFileDownloader.java index 3189c1740..ecd23ae86 100644 --- a/F-Droid/src/org/fdroid/fdroid/net/LocalFileDownloader.java +++ b/F-Droid/src/org/fdroid/fdroid/net/LocalFileDownloader.java @@ -1,7 +1,5 @@ package org.fdroid.fdroid.net; -import android.content.Context; - import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; @@ -12,8 +10,8 @@ import java.net.URL; public class LocalFileDownloader extends Downloader { - LocalFileDownloader(Context context, URL url, File destFile) throws FileNotFoundException, MalformedURLException { - super(context, url, destFile); + LocalFileDownloader(URL url, File destFile) throws FileNotFoundException, MalformedURLException { + super(url, destFile); } private File getFileToDownload() {