diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/Constants.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/Constants.java index d1e13e24f..75b3ccb44 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/Constants.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/Constants.java @@ -96,12 +96,7 @@ public final class Constants { public static final String USE_TOR_PROXY = "useTorProxy"; public static final String PROXY_HOST = "proxyHost"; public static final String PROXY_PORT = "proxyPort"; - } - - public static final class ProxyOrbot { - public static final String PROXY_HOST = "127.0.0.1"; - public static final int PROXY_HTTP_PORT = 8118; - public static final int PROXY_SOCKS_PORT = 9050; + public static final String PROXY_TYPE = "proxyType"; } public static final class Defaults { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java index 00d2d7a1b..22ff051d2 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java @@ -22,12 +22,7 @@ import android.content.Context; import android.content.Intent; import android.os.Build; import android.os.Bundle; -import android.preference.CheckBoxPreference; -import android.preference.EditTextPreference; -import android.preference.Preference; -import android.preference.PreferenceActivity; -import android.preference.PreferenceFragment; -import android.preference.PreferenceScreen; +import android.preference.*; import android.support.v7.widget.Toolbar; import android.view.View; import android.view.ViewGroup; @@ -279,6 +274,7 @@ public class SettingsActivity extends PreferenceActivity { private CheckBoxPreference mUseNormalProxy; private EditTextPreference mProxyHost; private EditTextPreference mProxyPort; + private ListPreference mProxyType; @Override public void onCreate(Bundle savedInstanceState) { @@ -291,10 +287,12 @@ public class SettingsActivity extends PreferenceActivity { mUseNormalProxy = (CheckBoxPreference) findPreference(Constants.Pref.USE_NORMAL_PROXY); mProxyHost = (EditTextPreference) findPreference(Constants.Pref.PROXY_HOST); mProxyPort = (EditTextPreference) findPreference(Constants.Pref.PROXY_PORT); + mProxyType = (ListPreference) findPreference(Constants.Pref.PROXY_TYPE); initializeUseTorPref(); initializeUseNormalProxyPref(); - initialiseEditTextPreferences(); + initializeEditTextPreferences(); + initializeProxyTypePreference(); if (mUseTor.isChecked()) disableNormalProxyPrefs(); else if (mUseNormalProxy.isChecked()) disableUseTorPrefs(); @@ -341,7 +339,7 @@ public class SettingsActivity extends PreferenceActivity { }); } - private void initialiseEditTextPreferences() { + private void initializeEditTextPreferences() { mProxyHost.setSummary(mProxyHost.getText()); mProxyPort.setSummary(mProxyPort.getText()); @@ -390,17 +388,32 @@ public class SettingsActivity extends PreferenceActivity { }); } + private void initializeProxyTypePreference() { + mProxyType.setSummary(mProxyType.getEntry()); + + mProxyType.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + CharSequence entry = mProxyType.getEntries()[mProxyType.findIndexOfValue((String) newValue)]; + mProxyType.setSummary(entry); + return true; + } + }); + } + private void disableNormalProxyPrefs() { mUseNormalProxy.setChecked(false); mUseNormalProxy.setEnabled(false); mProxyHost.setEnabled(false); mProxyPort.setEnabled(false); + mProxyType.setEnabled(false); } private void enableNormalProxyPrefs() { mUseNormalProxy.setEnabled(true); mProxyHost.setEnabled(true); mProxyPort.setEnabled(true); + mProxyType.setEnabled(true); } private void disableUseTorPrefs() { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java index 1363092f4..8bc5b777f 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java @@ -21,9 +21,12 @@ package org.sufficientlysecure.keychain.util; import android.content.Context; import android.content.SharedPreferences; +import android.content.res.Resources; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.Constants.Pref; +import org.sufficientlysecure.keychain.R; +import java.net.Proxy; import java.util.ArrayList; import java.util.Arrays; import java.util.ListIterator; @@ -35,6 +38,7 @@ import java.util.Vector; public class Preferences { private static Preferences sPreferences; private SharedPreferences mSharedPreferences; + private Resources mResources; public static synchronized Preferences getPreferences(Context context) { return getPreferences(context, false); @@ -51,6 +55,7 @@ public class Preferences { } private Preferences(Context context) { + mResources = context.getResources(); updateSharedPreferences(context); } @@ -224,6 +229,8 @@ public class Preferences { return mSharedPreferences.getBoolean(Pref.ENCRYPT_FILENAMES, true); } + // proxy preference functions start here + public boolean getUseNormalProxy() { return mSharedPreferences.getBoolean(Constants.Pref.USE_NORMAL_PROXY, false); } @@ -256,13 +263,16 @@ public class Preferences { /** * we store port as String for easy interfacing with EditTextPreference, but return it as an integer + * * @return port number of proxy */ public int getProxyPort() { return Integer.parseInt(mSharedPreferences.getString(Pref.PROXY_PORT, "-1")); } + /** * we store port as String for easy interfacing with EditTextPreference, but return it as an integer + * * @param port proxy port */ public void setProxyPort(String port) { @@ -271,6 +281,22 @@ public class Preferences { editor.commit(); } + public Proxy.Type getProxyType() { + final String typeHttp = mResources.getString(R.string.pref_proxy_type_value_http); + final String typeSocks = mResources.getString(R.string.pref_proxy_type_value_socks); + + String type = mSharedPreferences.getString(Pref.PROXY_TYPE, typeHttp); + + if(type.equals(typeHttp)) return Proxy.Type.HTTP; + else if(type.equals(typeSocks)) return Proxy.Type.SOCKS; + else { // shouldn't happen + Log.e(Constants.TAG, "Invalid Proxy Type in preferences"); + return null; + } + } + + // proxy preference functions ends here + public CloudSearchPrefs getCloudSearchPrefs() { return new CloudSearchPrefs(mSharedPreferences.getBoolean(Pref.SEARCH_KEYSERVER, true), mSharedPreferences.getBoolean(Pref.SEARCH_KEYBASE, true), diff --git a/OpenKeychain/src/main/res/values/arrays.xml b/OpenKeychain/src/main/res/values/arrays.xml index 44bbe00cc..241f530d8 100644 --- a/OpenKeychain/src/main/res/values/arrays.xml +++ b/OpenKeychain/src/main/res/values/arrays.xml @@ -29,6 +29,14 @@ 28800 -1 + + @string/pref_proxy_type_choice_http + @string/pref_proxy_type_choice_socks + + + @string/pref_proxy_type_value_http + @string/pref_proxy_type_value_socks + @string/key_size_2048 @string/key_size_4096 diff --git a/OpenKeychain/src/main/res/values/strings.xml b/OpenKeychain/src/main/res/values/strings.xml index 8e9f38163..67662503c 100644 --- a/OpenKeychain/src/main/res/values/strings.xml +++ b/OpenKeychain/src/main/res/values/strings.xml @@ -167,14 +167,20 @@ "Search keys on keybase.io" - "Don't use a proxy" - "Enable Tor" + "Enable Tor" "Requires Orbot to be installed" "Enable other proxy" - "Proxy Host" + "Proxy Host" "Proxy host cannot be empty" - "Proxy Port" + "Proxy Port" "Invalid port number entered" + "Proxy Type" + + + "HTTP" + "SOCKS" + "proxyHttp" + "proxySocks" "<no name>" "<none>" diff --git a/OpenKeychain/src/main/res/xml/proxy_prefs.xml b/OpenKeychain/src/main/res/xml/proxy_prefs.xml index e77ac6d71..ab9c5a3e3 100644 --- a/OpenKeychain/src/main/res/xml/proxy_prefs.xml +++ b/OpenKeychain/src/main/res/xml/proxy_prefs.xml @@ -3,7 +3,7 @@ @@ -21,7 +21,14 @@ android:key="proxyPort" android:defaultValue="8118" android:persistent="true" - android:title="@string/pref_proxy_port" + android:title="@string/pref_proxy_port_label" android:textCursorDrawable="@null" android:inputType="number" /> +