remove Preferences check out of HttpDownloader

This is the last Android code in the whole suite of Downloader subclasses,
so now we can write JUnit tests for them all, and avoid the fragility of
tests running on the emulator.
This commit is contained in:
Hans-Christoph Steiner 2016-03-22 20:01:19 +01:00
parent f2621dcb55
commit f9063b5058
4 changed files with 21 additions and 6 deletions

View File

@ -206,6 +206,9 @@ public class FDroidApp extends Application {
// the database is locked due to the database updater.
InstalledAppCacheUpdater.updateInBackground(getApplicationContext());
// make sure the current proxy stuff is configured
Preferences.get().configureProxy();
// If the user changes the preference to do with filtering rooted apps,
// it is easier to just notify a change in the app provider,
// so that the newly updated list will correctly filter relevant apps.

View File

@ -6,6 +6,9 @@ import android.os.Build;
import android.preference.PreferenceManager;
import android.util.Log;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
@ -14,6 +17,8 @@ import java.util.List;
import java.util.Map;
import java.util.Random;
import info.guardianproject.netcipher.NetCipher;
/**
* Handles shared preferences for FDroid, looking after the names of
* preferences, default values and caching. Needs to be setup in the FDroidApp
@ -178,6 +183,18 @@ public final class Preferences implements SharedPreferences.OnSharedPreferenceCh
return preferences.getBoolean(PREF_ENABLE_PROXY, DEFAULT_ENABLE_PROXY);
}
/**
* Configure the proxy settings based on whether its enabled and set up. This must be
* run once at app startup, then whenever any of these settings changes.
*/
public void configureProxy() {
if (isProxyEnabled()) {
// if "Use Tor" is set, NetCipher will ignore these proxy settings
SocketAddress sa = new InetSocketAddress(getProxyHost(), getProxyPort());
NetCipher.setProxy(new Proxy(Proxy.Type.HTTP, sa));
}
}
public String getProxyHost() {
return preferences.getString(PREF_PROXY_HOST, DEFAULT_PROXY_HOST);
}

View File

@ -95,12 +95,6 @@ public class HttpDownloader extends Downloader {
// swap never works with a proxy, its unrouted IP on the same subnet
connection = (HttpURLConnection) sourceUrl.openConnection();
} else {
Preferences prefs = Preferences.get();
if (prefs.isProxyEnabled()) {
// if "Use Tor" is set, NetCipher will ignore these proxy settings
SocketAddress sa = new InetSocketAddress(prefs.getProxyHost(), prefs.getProxyPort());
NetCipher.setProxy(new Proxy(Proxy.Type.HTTP, sa));
}
connection = NetCipher.getHttpURLConnection(sourceUrl);
}

View File

@ -317,6 +317,7 @@ public class PreferencesFragment extends PreferenceFragment
public void onPause() {
super.onPause();
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
Preferences.get().configureProxy();
}
@Override