From 1a5bd84fad0470b5d32d04c8f77acd7345a95db4 Mon Sep 17 00:00:00 2001 From: Peter Serwylo Date: Sun, 8 Nov 2015 10:00:01 +1100 Subject: [PATCH] Never fallback to UIL for handling image downloads, only use for displaying. Our `IconDownloader` extended `BaseImageDownloader` from UIL. There was an explicit check in the F-Droid `IconDownloader` which looks for HTTP/HTTPS/Bluetooth schemes. If it wasn't one of these, it fell back to the base class. This was what was happening for local cached image files. As such, when the `getInputStream(...)` method was refactored to only use F-Droids `DownloadFactory` and not delegate to the base class, it failed on local "file://" URLs. This change introduces a `LocalFileDownloader` and makes the `DownloaderFactory` aware of it. The `BaseImageDownloader` class only provides support for the following schemes: * HTTP * HTTPS * File * Android content providers * Android assets * Android drawables F-Droid now supports HTTP, HTTPS, and File URLs. There is not currently any need for content proiders, assets or drawables to get icons for apps in F-Droid. If there is a need in the future (e.g. an issue currently discusses loading icons from installed apps if possible) then that specific `Downloader` can get introduced to solve the problem. --- .../fdroid/fdroid/net/DownloaderFactory.java | 9 +++- .../org/fdroid/fdroid/net/IconDownloader.java | 13 ++--- .../fdroid/net/LocalFileDownloader.java | 52 +++++++++++++++++++ 3 files changed, 64 insertions(+), 10 deletions(-) create mode 100644 F-Droid/src/org/fdroid/fdroid/net/LocalFileDownloader.java diff --git a/F-Droid/src/org/fdroid/fdroid/net/DownloaderFactory.java b/F-Droid/src/org/fdroid/fdroid/net/DownloaderFactory.java index 2ca730c25..579b3874c 100644 --- a/F-Droid/src/org/fdroid/fdroid/net/DownloaderFactory.java +++ b/F-Droid/src/org/fdroid/fdroid/net/DownloaderFactory.java @@ -48,9 +48,10 @@ public class DownloaderFactory { if (isBluetoothAddress(url)) { String macAddress = url.getHost().replace("-", ":"); return new BluetoothDownloader(context, macAddress, url, destFile); - } - if (isOnionAddress(url)) { + } else if (isOnionAddress(url)) { return new TorHttpDownloader(context, url, destFile); + } else if (isLocalFile(url)) { + return new LocalFileDownloader(context, url, destFile); } return new HttpDownloader(context, url, destFile); } @@ -59,6 +60,10 @@ public class DownloaderFactory { return "bluetooth".equalsIgnoreCase(url.getProtocol()); } + static boolean isLocalFile(URL url) { + return "file".equalsIgnoreCase(url.getProtocol()); + } + public static AsyncDownloader createAsync(Context context, String urlString, File destFile, String title, String id, AsyncDownloader.Listener listener) throws IOException { return createAsync(context, new URL(urlString), destFile, title, id, listener); } diff --git a/F-Droid/src/org/fdroid/fdroid/net/IconDownloader.java b/F-Droid/src/org/fdroid/fdroid/net/IconDownloader.java index c0389a390..943b23cb7 100644 --- a/F-Droid/src/org/fdroid/fdroid/net/IconDownloader.java +++ b/F-Droid/src/org/fdroid/fdroid/net/IconDownloader.java @@ -2,19 +2,17 @@ package org.fdroid.fdroid.net; import android.content.Context; -import com.nostra13.universalimageloader.core.download.BaseImageDownloader; +import com.nostra13.universalimageloader.core.download.ImageDownloader; import java.io.IOException; import java.io.InputStream; -public class IconDownloader extends BaseImageDownloader { +public class IconDownloader implements ImageDownloader { + + private final Context context; public IconDownloader(Context context) { - super(context); - } - - public IconDownloader(Context context, int connectTimeout, int readTimeout) { - super(context, connectTimeout, readTimeout); + this.context = context; } @Override @@ -22,5 +20,4 @@ public class IconDownloader extends BaseImageDownloader { return DownloaderFactory.create(context, imageUri).getInputStream(); } - } diff --git a/F-Droid/src/org/fdroid/fdroid/net/LocalFileDownloader.java b/F-Droid/src/org/fdroid/fdroid/net/LocalFileDownloader.java new file mode 100644 index 000000000..3189c1740 --- /dev/null +++ b/F-Droid/src/org/fdroid/fdroid/net/LocalFileDownloader.java @@ -0,0 +1,52 @@ +package org.fdroid.fdroid.net; + +import android.content.Context; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; + +public class LocalFileDownloader extends Downloader { + + LocalFileDownloader(Context context, URL url, File destFile) throws FileNotFoundException, MalformedURLException { + super(context, url, destFile); + } + + private File getFileToDownload() { + return new File(sourceUrl.getPath()); + } + + @Override + protected InputStream getDownloadersInputStream() throws IOException { + return new FileInputStream(getFileToDownload()); + } + + @Override + protected void close() throws IOException { + // Do nothing. + } + + @Override + public boolean hasChanged() { + return false; + } + + @Override + public int totalDownloadSize() { + return 0; + } + + @Override + public void download() throws IOException, InterruptedException { + downloadFromStream(1024 * 50); + } + + @Override + public boolean isCached() { + return false; + } +}