deduplicate error prone proxy settings code

This commit is contained in:
Michael Pöhn 2019-04-01 22:56:47 +02:00
parent 292950898e
commit 88f3a68f83
3 changed files with 14 additions and 23 deletions

View File

@ -388,7 +388,8 @@ public class FDroidApp extends Application {
PRNGFixes.apply();
curTheme = preferences.getTheme();
preferences.configureProxy();
configureProxy(preferences);
// bug specific to exactly 5.0 makes it only work with the old index
// which includes an ugly, hacky workaround
@ -485,8 +486,6 @@ public class FDroidApp extends Application {
}
});
configureTor(preferences.isTorEnabled());
if (preferences.isKeepingInstallHistory()) {
InstallHistoryService.register(this);
}
@ -641,11 +640,15 @@ public class FDroidApp extends Application {
}
/**
* Set the proxy settings based on whether Tor should be enabled or not.
* Put proxy settings (or Tor settings) globally into effect based on whats configured in Preferences.
*
* Must be called on App startup and after every proxy configuration change.
*/
private static void configureTor(boolean enabled) {
if (enabled) {
public static void configureProxy(Preferences preferences) {
if (preferences.isTorEnabled()) {
NetCipher.useTor();
} else if (preferences.isProxyEnabled()) {
NetCipher.setProxy(preferences.getProxyHost(), preferences.getProxyPort());
} else {
NetCipher.clearProxy();
}

View File

@ -481,22 +481,10 @@ public final class Preferences implements SharedPreferences.OnSharedPreferenceCh
return preferences.getBoolean(PREF_USE_TOR, IGNORED_B);
}
private boolean isProxyEnabled() {
public boolean isProxyEnabled() {
return preferences.getBoolean(PREF_ENABLE_PROXY, IGNORED_B);
}
/**
* 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

@ -455,12 +455,12 @@ public class PreferencesFragment extends PreferenceFragment
if ((Boolean) enabled) {
final Activity activity = getActivity();
enableProxyCheckPref.setEnabled(false);
if (OrbotHelper.isOrbotInstalled(activity)) {
NetCipher.useTor();
} else {
if (!OrbotHelper.isOrbotInstalled(activity)) {
Intent intent = OrbotHelper.getOrbotInstallIntent(activity);
activity.startActivityForResult(intent, REQUEST_INSTALL_ORBOT);
}
// NetCipher gets configured to use Tor in onPause()
// via a call to FDroidApp.configureProxy()
} else {
enableProxyCheckPref.setEnabled(true);
NetCipher.clearProxy();
@ -491,7 +491,7 @@ public class PreferencesFragment extends PreferenceFragment
public void onPause() {
super.onPause();
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
Preferences.get().configureProxy();
FDroidApp.configureProxy(Preferences.get());
if (updateIntervalPrevious != updateIntervalSeekBar.getValue()) {
UpdateService.schedule(getActivity());