From f9063b5058e6fcb9500c1ad18565424e23d35d43 Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Tue, 22 Mar 2016 20:01:19 +0100 Subject: [PATCH] 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. --- F-Droid/src/org/fdroid/fdroid/FDroidApp.java | 3 +++ F-Droid/src/org/fdroid/fdroid/Preferences.java | 17 +++++++++++++++++ .../org/fdroid/fdroid/net/HttpDownloader.java | 6 ------ .../views/fragments/PreferencesFragment.java | 1 + 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/F-Droid/src/org/fdroid/fdroid/FDroidApp.java b/F-Droid/src/org/fdroid/fdroid/FDroidApp.java index 9db97b91d..df06334e8 100644 --- a/F-Droid/src/org/fdroid/fdroid/FDroidApp.java +++ b/F-Droid/src/org/fdroid/fdroid/FDroidApp.java @@ -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. diff --git a/F-Droid/src/org/fdroid/fdroid/Preferences.java b/F-Droid/src/org/fdroid/fdroid/Preferences.java index ed9bc4df1..c436694a2 100644 --- a/F-Droid/src/org/fdroid/fdroid/Preferences.java +++ b/F-Droid/src/org/fdroid/fdroid/Preferences.java @@ -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); } diff --git a/F-Droid/src/org/fdroid/fdroid/net/HttpDownloader.java b/F-Droid/src/org/fdroid/fdroid/net/HttpDownloader.java index bebd55977..a7bfac24d 100644 --- a/F-Droid/src/org/fdroid/fdroid/net/HttpDownloader.java +++ b/F-Droid/src/org/fdroid/fdroid/net/HttpDownloader.java @@ -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); } diff --git a/F-Droid/src/org/fdroid/fdroid/views/fragments/PreferencesFragment.java b/F-Droid/src/org/fdroid/fdroid/views/fragments/PreferencesFragment.java index 576aaafa7..c6007348e 100644 --- a/F-Droid/src/org/fdroid/fdroid/views/fragments/PreferencesFragment.java +++ b/F-Droid/src/org/fdroid/fdroid/views/fragments/PreferencesFragment.java @@ -317,6 +317,7 @@ public class PreferencesFragment extends PreferenceFragment public void onPause() { super.onPause(); getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this); + Preferences.get().configureProxy(); } @Override