use FileInputStream for file:// URLs when UIL loads images

This saves the levels of indirection that leads to a FileInputStream being
created in LocalFileDownloader.  Since there are already special cases for
assets:// and drawable://, it seems a natural place to put the file://
case.  Also, since this is used to load icons when scrolling through lists
of apps, this is particularly sensitive to inefficient loading.

This also removes custom code that UIL provides better.
This commit is contained in:
Hans-Christoph Steiner 2018-03-26 15:02:51 +02:00
parent b10fa425b5
commit 44fcfd36f9
3 changed files with 10 additions and 75 deletions

View File

@ -3,7 +3,6 @@ package org.fdroid.fdroid.net;
import android.content.Context; import android.content.Context;
import android.net.Uri; import android.net.Uri;
import android.support.v4.content.LocalBroadcastManager; import android.support.v4.content.LocalBroadcastManager;
import org.fdroid.fdroid.data.Repo; import org.fdroid.fdroid.data.Repo;
import org.fdroid.fdroid.data.RepoProvider; import org.fdroid.fdroid.data.RepoProvider;
import org.fdroid.fdroid.data.Schema; import org.fdroid.fdroid.data.Schema;
@ -41,11 +40,9 @@ public class DownloaderFactory {
localBroadcastManager = LocalBroadcastManager.getInstance(context); localBroadcastManager = LocalBroadcastManager.getInstance(context);
} }
if (isBluetoothAddress(url)) { if ("bluetooth".equalsIgnoreCase(url.getProtocol())) {
String macAddress = url.getHost().replace("-", ":"); String macAddress = url.getHost().replace("-", ":");
downloader = new BluetoothDownloader(macAddress, url, destFile); downloader = new BluetoothDownloader(macAddress, url, destFile);
} else if (isLocalFile(url)) {
downloader = new LocalFileDownloader(url, destFile);
} else { } else {
final String[] projection = {Schema.RepoTable.Cols.USERNAME, Schema.RepoTable.Cols.PASSWORD}; final String[] projection = {Schema.RepoTable.Cols.USERNAME, Schema.RepoTable.Cols.PASSWORD};
Repo repo = RepoProvider.Helper.findByUrl(context, Uri.parse(url.toString()), projection); Repo repo = RepoProvider.Helper.findByUrl(context, Uri.parse(url.toString()), projection);
@ -57,12 +54,4 @@ public class DownloaderFactory {
} }
return downloader; return downloader;
} }
private static boolean isBluetoothAddress(URL url) {
return "bluetooth".equalsIgnoreCase(url.getProtocol());
}
private static boolean isLocalFile(URL url) {
return "file".equalsIgnoreCase(url.getProtocol());
}
} }

View File

@ -1,15 +1,18 @@
package org.fdroid.fdroid.net; package org.fdroid.fdroid.net;
import android.content.Context; import android.content.Context;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader; import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
/** /**
* Class used by the Universal Image Loader library (UIL) to fetch images for displaying in F-Droid. * Class used by the Universal Image Loader library (UIL) to fetch images for
* See {@link org.fdroid.fdroid.FDroidApp} for where this gets configured. * displaying in F-Droid. A custom subclass is needed since F-Droid's
* {@link HttpDownloader} provides support for Tor, proxying, and automatic
* mirror failover.
*
* @see org.fdroid.fdroid.FDroidApp#onCreate() for where this is setup
*/ */
public class ImageLoaderForUIL implements com.nostra13.universalimageloader.core.download.ImageDownloader { public class ImageLoaderForUIL implements com.nostra13.universalimageloader.core.download.ImageDownloader {
@ -22,15 +25,10 @@ public class ImageLoaderForUIL implements com.nostra13.universalimageloader.core
@Override @Override
public InputStream getStream(String imageUri, Object extra) throws IOException { public InputStream getStream(String imageUri, Object extra) throws IOException {
switch (Scheme.ofUri(imageUri)) { switch (Scheme.ofUri(imageUri)) {
case ASSETS: case HTTP:
return context.getAssets().open(Scheme.ASSETS.crop(imageUri)); case HTTPS:
case DRAWABLE:
return new BaseImageDownloader(context).getStream(imageUri, extra);
default:
return DownloaderFactory.create(context, imageUri).getInputStream(); return DownloaderFactory.create(context, imageUri).getInputStream();
} }
return new BaseImageDownloader(context).getStream(imageUri, extra);
} }
} }

View File

@ -1,52 +0,0 @@
package org.fdroid.fdroid.net;
import org.fdroid.fdroid.Utils;
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 {
private InputStream inputStream;
LocalFileDownloader(URL url, File destFile) throws FileNotFoundException, MalformedURLException {
super(url, destFile);
}
@Override
protected InputStream getDownloadersInputStream() throws IOException {
inputStream = new FileInputStream(new File(sourceUrl.getPath()));
return inputStream;
}
@Override
protected void close() {
if (inputStream != null) {
Utils.closeQuietly(inputStream);
}
}
@Override
public boolean hasChanged() {
return false;
}
@Override
public int totalDownloadSize() {
return 0;
}
@Override
public void download() throws IOException, InterruptedException {
if (new File(sourceUrl.getPath()).exists()) {
downloadFromStream(1024 * 50, false);
} else {
notFound = true;
}
}
}