implemented IconDownloader for UIL downloads with FDroid classes

This lets UniversalImageLoader (UIL) use FDroid's generic Downloader
infrastucture so that connection configuration all happens based on the URL
in DownloaderFactory.

refs #2598 https://dev.guardianproject.info/issues/2598
refs #2367 https://dev.guardianproject.info/issues/2367
This commit is contained in:
Hans-Christoph Steiner 2014-05-28 19:45:18 -04:00
parent d19e77049a
commit c1b5bf5279
4 changed files with 45 additions and 16 deletions

View File

@ -47,6 +47,7 @@ import org.fdroid.fdroid.data.AppProvider;
import org.fdroid.fdroid.data.InstalledAppCacheUpdater;
import org.fdroid.fdroid.data.Repo;
import org.fdroid.fdroid.localrepo.LocalRepoService;
import org.fdroid.fdroid.net.IconDownloader;
import org.fdroid.fdroid.net.WifiStateChangeService;
import org.thoughtcrime.ssl.pinning.PinningTrustManager;
import org.thoughtcrime.ssl.pinning.SystemKeyStore;
@ -152,7 +153,8 @@ public class FDroidApp extends Application {
bluetoothAdapter = getBluetoothAdapter();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
.discCache(new LimitedAgeDiscCache(
.imageDownloader(new IconDownloader(getApplicationContext()))
.diskCache(new LimitedAgeDiscCache(
new File(StorageUtils.getCacheDirectory(getApplicationContext(), true),
"icons"),
null,

View File

@ -41,6 +41,8 @@ public class HttpDownloader extends Downloader {
@Override
public InputStream getInputStream() throws IOException {
setupConnection();
// TODO check out BaseImageDownloader.getStreamFromNetwork() for optims
return connection.getInputStream();
}
@ -53,7 +55,7 @@ public class HttpDownloader extends Downloader {
@Override
public void download() throws IOException, InterruptedException {
try {
connection = (HttpURLConnection) sourceUrl.openConnection();
setupConnection();
doDownload();
} catch (SSLHandshakeException e) {
// TODO this should be handled better, it is not internationalised here
@ -65,6 +67,12 @@ public class HttpDownloader extends Downloader {
}
}
protected void setupConnection() throws IOException {
if (connection != null)
return;
connection = (HttpURLConnection) sourceUrl.openConnection();
}
protected void doDownload() throws IOException, InterruptedException {
if (wantToCheckCache()) {
setupCacheCheck();

View File

@ -0,0 +1,32 @@
package org.fdroid.fdroid.net;
import android.content.Context;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import java.io.IOException;
import java.io.InputStream;
public class IconDownloader extends BaseImageDownloader {
public IconDownloader(Context context) {
super(context);
}
public IconDownloader(Context context, int connectTimeout, int readTimeout) {
super(context, connectTimeout, readTimeout);
}
@Override
public InputStream getStream(String imageUri, Object extra) throws IOException {
switch (Scheme.ofUri(imageUri)) {
case HTTP:
case HTTPS:
Downloader downloader = DownloaderFactory.create(imageUri, context);
return downloader.getInputStream();
default:
return super.getStream(imageUri, extra);
}
}
}

View File

@ -2,7 +2,6 @@
package org.fdroid.fdroid.net;
import android.content.Context;
import android.util.Log;
import java.io.File;
import java.io.FileNotFoundException;
@ -13,8 +12,6 @@ import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.SocketAddress;
import javax.net.ssl.SSLHandshakeException;
public class TorHttpDownloader extends HttpDownloader {
TorHttpDownloader(String url, Context ctx) throws IOException {
@ -27,19 +24,9 @@ public class TorHttpDownloader extends HttpDownloader {
}
@Override
public void download() throws IOException, InterruptedException {
try {
protected void setupConnection() throws IOException {
SocketAddress sa = new InetSocketAddress("127.0.0.1", 8118);
Proxy tor = new Proxy(Proxy.Type.HTTP, sa);
connection = (HttpURLConnection) sourceUrl.openConnection(tor);
doDownload();
} catch (SSLHandshakeException e) {
throw new IOException(
"A problem occurred while establishing an SSL " +
"connection. If this problem persists, AND you have a " +
"very old device, you could try using http instead of " +
"https for the repo URL." + Log.getStackTraceString(e));
}
}
}